[BACK]Return to funmap.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/funmap.c, Revision 1.13

1.13    ! cloder      1: /*     $OpenBSD: funmap.c,v 1.12 2005/04/28 07:23:56 otto Exp $        */
1.3       art         2: /*
                      3:  * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
1.1       art        25: #include "def.h"
                     26: #include "kbd.h"
                     27: #include "funmap.h"
                     28:
                     29: /*
                     30:  * If the function is NULL, it must be listed with the
                     31:  * same name in the map_table.
                     32:  */
                     33:
                     34: struct funmap {
1.6       vincent    35:        PF               fn_funct;
                     36:        const            char *fn_name;
1.1       art        37:        struct funmap   *fn_next;
                     38: };
                     39:
                     40: static struct funmap *funs;
                     41:
                     42: static struct funmap functnames[] = {
                     43: #ifndef        NO_HELP
                     44:        {apropos_command, "apropos",},
                     45: #endif /* !NO_HELP */
1.11      db         46:        {auto_execute, "auto-execute", },
1.1       art        47:        {fillmode, "auto-fill-mode",},
                     48:        {indentmode, "auto-indent-mode",},
                     49:        {backchar, "backward-char",},
                     50:        {delbword, "backward-kill-word",},
                     51:        {gotobop, "backward-paragraph",},
                     52:        {backword, "backward-word",},
                     53:        {gotobob, "beginning-of-buffer",},
                     54:        {gotobol, "beginning-of-line",},
                     55:        {blinkparen, "blink-matching-paren",},
                     56:        {showmatch, "blink-matching-paren-hack",},
                     57: #ifdef BSMAP
                     58:        {bsmap, "bsmap-mode",},
                     59: #endif /* BSMAP */
                     60:        {NULL, "c-x 4 prefix",},
                     61:        {NULL, "c-x prefix",},
                     62: #ifndef NO_MACRO
                     63:        {executemacro, "call-last-kbd-macro",},
                     64: #endif /* !NO_MACRO */
                     65:        {capword, "capitalize-word",},
                     66: #ifndef NO_DIR
                     67:        {changedir, "cd",},
                     68: #endif /* !NO_DIR */
                     69:        {copyregion, "copy-region-as-kill",},
                     70: #ifdef REGEX
                     71:        {cntmatchlines, "count-matches",},
                     72:        {cntnonmatchlines, "count-non-matches",},
                     73: #endif /* REGEX */
                     74:        {define_key, "define-key",},
                     75:        {backdel, "delete-backward-char",},
                     76:        {deblank, "delete-blank-lines",},
                     77:        {forwdel, "delete-char",},
                     78:        {delwhite, "delete-horizontal-space",},
                     79: #ifdef REGEX
                     80:        {delmatchlines, "delete-matching-lines",},
                     81:        {delnonmatchlines, "delete-non-matching-lines",},
                     82: #endif /* REGEX */
                     83:        {onlywind, "delete-other-windows",},
                     84:        {delwind, "delete-window",},
                     85: #ifndef NO_HELP
                     86:        {wallchart, "describe-bindings",},
                     87:        {desckey, "describe-key-briefly",},
                     88: #endif /* !NO_HELP */
                     89:        {digit_argument, "digit-argument",},
                     90: #ifndef NO_DIRED
                     91:        {dired, "dired",},
                     92:        {d_undelbak, "dired-backup-unflag",},
                     93:        {d_copy, "dired-copy-file",},
                     94:        {d_expunge, "dired-do-deletions",},
                     95:        {d_findfile, "dired-find-file",},
                     96:        {d_ffotherwindow, "dired-find-file-other-window",},
                     97:        {d_del, "dired-flag-file-deleted",},
                     98:        {d_otherwindow, "dired-other-window",},
                     99:        {d_rename, "dired-rename-file",},
                    100:        {d_undel, "dired-unflag",},
                    101: #endif /* !NO_DIRED */
                    102:        {lowerregion, "downcase-region",},
                    103:        {lowerword, "downcase-word",},
                    104:        {showversion, "emacs-version",},
                    105: #ifndef NO_MACRO
                    106:        {finishmacro, "end-kbd-macro",},
                    107: #endif /* !NO_MACRO */
                    108:        {gotoeob, "end-of-buffer",},
                    109:        {gotoeol, "end-of-line",},
                    110:        {enlargewind, "enlarge-window",},
                    111:        {NULL, "esc prefix",},
                    112: #ifndef NO_STARTUP
                    113:        {evalbuffer, "eval-current-buffer",},
                    114:        {evalexpr, "eval-expression",},
                    115: #endif /* !NO_STARTUP */
                    116:        {swapmark, "exchange-point-and-mark",},
                    117:        {extend, "execute-extended-command",},
                    118:        {fillpara, "fill-paragraph",},
                    119:        {filevisit, "find-file",},
1.8       vincent   120:        {filevisitro, "find-file-read-only",},
1.1       art       121:        {poptofile, "find-file-other-window",},
                    122:        {forwchar, "forward-char",},
                    123:        {gotoeop, "forward-paragraph",},
                    124:        {forwword, "forward-word",},
                    125:        {bindtokey, "global-set-key",},
                    126:        {unbindtokey, "global-unset-key",},
                    127:        {gotoline, "goto-line",},
                    128: #ifndef NO_HELP
                    129:        {NULL, "help",},
                    130:        {help_help, "help-help",},
                    131: #endif /* !NO_HELP */
                    132:        {insert, "insert",},
                    133:        {bufferinsert, "insert-buffer",},
                    134:        {fileinsert, "insert-file",},
                    135:        {fillword, "insert-with-wrap",},
                    136:        {backisearch, "isearch-backward",},
                    137:        {forwisearch, "isearch-forward",},
                    138:        {justone, "just-one-space",},
                    139:        {ctrlg, "keyboard-quit",},
1.10      jfb       140:        {killbuffer_cmd, "kill-buffer",},
1.1       art       141:        {killline, "kill-line",},
                    142:        {killpara, "kill-paragraph",},
                    143:        {killregion, "kill-region",},
                    144:        {delfword, "kill-word",},
                    145:        {listbuffers, "list-buffers",},
                    146: #ifndef NO_STARTUP
                    147:        {evalfile, "load",},
                    148: #endif /* !NO_STARTUP */
                    149:        {localbind, "local-set-key",},
                    150:        {localunbind, "local-unset-key",},
                    151: #ifndef NO_BACKUP
                    152:        {makebkfile, "make-backup-files",},
                    153: #endif /* !NO_BACKUP */
                    154: #ifdef DO_METAKEY
                    155:        {do_meta, "meta-key-mode",},    /* better name, anyone? */
                    156: #endif /* DO_METAKEY */
                    157:        {negative_argument, "negative-argument",},
                    158:        {newline, "newline",},
                    159:        {indent, "newline-and-indent",},
                    160:        {forwline, "next-line",},
                    161: #ifdef NOTAB
                    162:        {notabmode, "no-tab-mode",},
                    163: #endif /* NOTAB */
                    164:        {notmodified, "not-modified",},
                    165:        {openline, "open-line",},
                    166:        {nextwind, "other-window",},
                    167:        {overwrite, "overwrite-mode",},
                    168: #ifdef PREFIXREGION
                    169:        {prefixregion, "prefix-region",},
                    170: #endif /* PREFIXREGION */
                    171:        {backline, "previous-line",},
                    172:        {prevwind, "previous-window",},
                    173:        {spawncli, "push-shell",},
                    174: #ifndef NO_DIR
                    175:        {showcwdir, "pwd",},
                    176: #endif /* !NO_DIR */
                    177:        {queryrepl, "query-replace",},
1.13    ! cloder    178:        {replstr, "replace-string",},
1.1       art       179: #ifdef REGEX
                    180:        {re_queryrepl, "query-replace-regexp",},
                    181: #endif /* REGEX */
                    182:        {quote, "quoted-insert",},
                    183: #ifdef REGEX
                    184:        {re_searchagain, "re-search-again",},
                    185:        {re_backsearch, "re-search-backward",},
                    186:        {re_forwsearch, "re-search-forward",},
                    187: #endif /* REGEX */
                    188:        {reposition, "recenter",},
                    189:        {refresh, "redraw-display",},
                    190:        {filesave, "save-buffer",},
                    191:        {quit, "save-buffers-kill-emacs",},
                    192:        {savebuffers, "save-some-buffers",},
                    193:        {backpage, "scroll-down",},
                    194:        {back1page, "scroll-one-line-down",},
                    195:        {forw1page, "scroll-one-line-up",},
                    196:        {pagenext, "scroll-other-window",},
                    197:        {forwpage, "scroll-up",},
                    198:        {searchagain, "search-again",},
                    199:        {backsearch, "search-backward",},
                    200:        {forwsearch, "search-forward",},
                    201:        {selfinsert, "self-insert-command",},
                    202: #ifdef REGEX
                    203:        {setcasefold, "set-case-fold-search",},
                    204: #endif /* REGEX */
                    205:        {set_default_mode, "set-default-mode",},
                    206:        {setfillcol, "set-fill-column",},
                    207:        {setmark, "set-mark-command",},
                    208: #ifdef PREFIXREGION
                    209:        {setprefix, "set-prefix-string",},
                    210: #endif /* PREFIXREGION */
                    211:        {shrinkwind, "shrink-window",},
                    212: #ifdef NOTAB
                    213:        {space_to_tabstop, "space-to-tabstop",},
                    214: #endif /* NOTAB */
                    215:        {splitwind, "split-window-vertically",},
                    216: #ifndef NO_MACRO
                    217:        {definemacro, "start-kbd-macro",},
                    218: #endif /* !NO_MACRO */
                    219:        {spawncli, "suspend-emacs",},
                    220:        {usebuffer, "switch-to-buffer",},
                    221:        {poptobuffer, "switch-to-buffer-other-window",},
1.8       vincent   222:        {togglereadonly, "toggle-read-only" },
1.1       art       223:        {twiddle, "transpose-chars",},
1.11      db        224:        {undo, "undo", },
                    225:        {undo_dump, "undo-list", },
1.1       art       226:        {universal_argument, "universal-argument",},
                    227:        {upperregion, "upcase-region",},
                    228:        {upperword, "upcase-word",},
                    229:        {showcpos, "what-cursor-position",},
                    230:        {filewrite, "write-file",},
                    231:        {yank, "yank",},
1.11      db        232:        {NULL, NULL,}
1.1       art       233: };
                    234:
                    235: void
                    236: funmap_init(void)
                    237: {
                    238:        struct funmap *fn;
                    239:
                    240:        for (fn = functnames; fn->fn_name != NULL; fn++) {
                    241:                fn->fn_next = funs;
                    242:                funs = fn;
                    243:        }
                    244: }
                    245:
                    246: int
1.6       vincent   247: funmap_add(PF fun, const char *fname)
1.1       art       248: {
                    249:        struct funmap *fn;
                    250:
                    251:        if ((fn = malloc(sizeof(*fn))) == NULL)
1.11      db        252:                return (FALSE);
1.1       art       253:
                    254:        fn->fn_funct = fun;
                    255:        fn->fn_name = fname;
                    256:        fn->fn_next = funs;
                    257:
                    258:        funs = fn;
1.11      db        259:        return (TRUE);
1.1       art       260: }
                    261:
                    262: /*
                    263:  * Translate from function name to function pointer.
                    264:  */
                    265: PF
1.6       vincent   266: name_function(const char *fname)
1.1       art       267: {
                    268:        struct funmap *fn;
                    269:
                    270:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    271:                if (strcmp(fn->fn_name, fname) == 0)
1.11      db        272:                        return (fn->fn_funct);
1.1       art       273:        }
1.11      db        274:        return (NULL);
1.1       art       275: }
                    276:
1.6       vincent   277: const char *
1.1       art       278: function_name(PF fun)
                    279: {
                    280:        struct funmap *fn;
                    281:
                    282:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    283:                if (fn->fn_funct == fun)
1.11      db        284:                        return (fn->fn_name);
1.1       art       285:        }
1.11      db        286:        return (NULL);
1.1       art       287: }
                    288:
                    289: /*
1.11      db        290:  * List possible function name completions.
1.1       art       291:  */
                    292: LIST *
1.12      otto      293: complete_function_list(const char *fname)
1.1       art       294: {
1.11      db        295:        struct funmap   *fn;
                    296:        LIST            *head, *el;
                    297:        int              len;
1.1       art       298:
                    299:        len = strlen(fname);
                    300:        head = NULL;
                    301:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    302:                if (memcmp(fname, fn->fn_name, len) == 0) {
                    303:                        if ((el = malloc(sizeof(*el))) == NULL) {
                    304:                                free_file_list(head);
1.11      db        305:                                return (NULL);
1.1       art       306:                        }
                    307:                        el->l_name = fn->fn_name;
                    308:                        el->l_next = head;
                    309:                        head = el;
                    310:                }
                    311:        }
1.11      db        312:        return (head);
1.1       art       313: }