[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.18

1.18    ! kjell       1: /*     $OpenBSD: funmap.c,v 1.17 2005/11/18 20:56:52 deraadt 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:        {lowerregion, "downcase-region",},
                     91:        {lowerword, "downcase-word",},
                     92:        {showversion, "emacs-version",},
                     93: #ifndef NO_MACRO
                     94:        {finishmacro, "end-kbd-macro",},
                     95: #endif /* !NO_MACRO */
                     96:        {gotoeob, "end-of-buffer",},
                     97:        {gotoeol, "end-of-line",},
                     98:        {enlargewind, "enlarge-window",},
                     99:        {NULL, "esc prefix",},
                    100: #ifndef NO_STARTUP
                    101:        {evalbuffer, "eval-current-buffer",},
                    102:        {evalexpr, "eval-expression",},
                    103: #endif /* !NO_STARTUP */
                    104:        {swapmark, "exchange-point-and-mark",},
                    105:        {extend, "execute-extended-command",},
                    106:        {fillpara, "fill-paragraph",},
                    107:        {filevisit, "find-file",},
1.8       vincent   108:        {filevisitro, "find-file-read-only",},
1.14      jason     109:        {filevisitalt, "find-alternate-file",},
1.1       art       110:        {poptofile, "find-file-other-window",},
                    111:        {forwchar, "forward-char",},
                    112:        {gotoeop, "forward-paragraph",},
                    113:        {forwword, "forward-word",},
                    114:        {bindtokey, "global-set-key",},
                    115:        {unbindtokey, "global-unset-key",},
                    116:        {gotoline, "goto-line",},
                    117: #ifndef NO_HELP
                    118:        {NULL, "help",},
                    119:        {help_help, "help-help",},
                    120: #endif /* !NO_HELP */
                    121:        {insert, "insert",},
                    122:        {bufferinsert, "insert-buffer",},
                    123:        {fileinsert, "insert-file",},
                    124:        {fillword, "insert-with-wrap",},
                    125:        {backisearch, "isearch-backward",},
                    126:        {forwisearch, "isearch-forward",},
                    127:        {justone, "just-one-space",},
                    128:        {ctrlg, "keyboard-quit",},
1.10      jfb       129:        {killbuffer_cmd, "kill-buffer",},
1.1       art       130:        {killline, "kill-line",},
                    131:        {killpara, "kill-paragraph",},
                    132:        {killregion, "kill-region",},
                    133:        {delfword, "kill-word",},
                    134:        {listbuffers, "list-buffers",},
                    135: #ifndef NO_STARTUP
                    136:        {evalfile, "load",},
                    137: #endif /* !NO_STARTUP */
                    138:        {localbind, "local-set-key",},
                    139:        {localunbind, "local-unset-key",},
                    140: #ifndef NO_BACKUP
                    141:        {makebkfile, "make-backup-files",},
                    142: #endif /* !NO_BACKUP */
                    143:        {do_meta, "meta-key-mode",},    /* better name, anyone? */
                    144:        {negative_argument, "negative-argument",},
                    145:        {newline, "newline",},
                    146:        {indent, "newline-and-indent",},
                    147:        {forwline, "next-line",},
                    148: #ifdef NOTAB
                    149:        {notabmode, "no-tab-mode",},
                    150: #endif /* NOTAB */
                    151:        {notmodified, "not-modified",},
                    152:        {openline, "open-line",},
                    153:        {nextwind, "other-window",},
                    154:        {overwrite, "overwrite-mode",},
                    155: #ifdef PREFIXREGION
                    156:        {prefixregion, "prefix-region",},
                    157: #endif /* PREFIXREGION */
                    158:        {backline, "previous-line",},
                    159:        {prevwind, "previous-window",},
                    160:        {spawncli, "push-shell",},
                    161: #ifndef NO_DIR
                    162:        {showcwdir, "pwd",},
                    163: #endif /* !NO_DIR */
                    164:        {queryrepl, "query-replace",},
1.15      deraadt   165: #ifdef REGEX
1.13      cloder    166:        {replstr, "replace-string",},
1.1       art       167:        {re_queryrepl, "query-replace-regexp",},
                    168: #endif /* REGEX */
                    169:        {quote, "quoted-insert",},
                    170: #ifdef REGEX
                    171:        {re_searchagain, "re-search-again",},
                    172:        {re_backsearch, "re-search-backward",},
                    173:        {re_forwsearch, "re-search-forward",},
                    174: #endif /* REGEX */
                    175:        {reposition, "recenter",},
                    176:        {refresh, "redraw-display",},
                    177:        {filesave, "save-buffer",},
                    178:        {quit, "save-buffers-kill-emacs",},
                    179:        {savebuffers, "save-some-buffers",},
                    180:        {backpage, "scroll-down",},
                    181:        {back1page, "scroll-one-line-down",},
                    182:        {forw1page, "scroll-one-line-up",},
                    183:        {pagenext, "scroll-other-window",},
                    184:        {forwpage, "scroll-up",},
                    185:        {searchagain, "search-again",},
                    186:        {backsearch, "search-backward",},
                    187:        {forwsearch, "search-forward",},
                    188:        {selfinsert, "self-insert-command",},
                    189: #ifdef REGEX
                    190:        {setcasefold, "set-case-fold-search",},
                    191: #endif /* REGEX */
                    192:        {set_default_mode, "set-default-mode",},
                    193:        {setfillcol, "set-fill-column",},
                    194:        {setmark, "set-mark-command",},
                    195: #ifdef PREFIXREGION
                    196:        {setprefix, "set-prefix-string",},
                    197: #endif /* PREFIXREGION */
                    198:        {shrinkwind, "shrink-window",},
                    199: #ifdef NOTAB
                    200:        {space_to_tabstop, "space-to-tabstop",},
                    201: #endif /* NOTAB */
                    202:        {splitwind, "split-window-vertically",},
                    203: #ifndef NO_MACRO
                    204:        {definemacro, "start-kbd-macro",},
                    205: #endif /* !NO_MACRO */
                    206:        {spawncli, "suspend-emacs",},
                    207:        {usebuffer, "switch-to-buffer",},
                    208:        {poptobuffer, "switch-to-buffer-other-window",},
1.8       vincent   209:        {togglereadonly, "toggle-read-only" },
1.1       art       210:        {twiddle, "transpose-chars",},
1.11      db        211:        {undo, "undo", },
                    212:        {undo_dump, "undo-list", },
1.1       art       213:        {universal_argument, "universal-argument",},
                    214:        {upperregion, "upcase-region",},
                    215:        {upperword, "upcase-word",},
                    216:        {showcpos, "what-cursor-position",},
                    217:        {filewrite, "write-file",},
                    218:        {yank, "yank",},
1.11      db        219:        {NULL, NULL,}
1.1       art       220: };
                    221:
                    222: void
                    223: funmap_init(void)
                    224: {
                    225:        struct funmap *fn;
                    226:
                    227:        for (fn = functnames; fn->fn_name != NULL; fn++) {
                    228:                fn->fn_next = funs;
                    229:                funs = fn;
                    230:        }
                    231: }
                    232:
                    233: int
1.6       vincent   234: funmap_add(PF fun, const char *fname)
1.1       art       235: {
                    236:        struct funmap *fn;
                    237:
                    238:        if ((fn = malloc(sizeof(*fn))) == NULL)
1.11      db        239:                return (FALSE);
1.1       art       240:
                    241:        fn->fn_funct = fun;
                    242:        fn->fn_name = fname;
                    243:        fn->fn_next = funs;
                    244:
                    245:        funs = fn;
1.11      db        246:        return (TRUE);
1.1       art       247: }
                    248:
                    249: /*
                    250:  * Translate from function name to function pointer.
                    251:  */
                    252: PF
1.6       vincent   253: name_function(const char *fname)
1.1       art       254: {
                    255:        struct funmap *fn;
                    256:
                    257:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    258:                if (strcmp(fn->fn_name, fname) == 0)
1.11      db        259:                        return (fn->fn_funct);
1.1       art       260:        }
1.11      db        261:        return (NULL);
1.1       art       262: }
                    263:
1.6       vincent   264: const char *
1.1       art       265: function_name(PF fun)
                    266: {
                    267:        struct funmap *fn;
                    268:
                    269:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    270:                if (fn->fn_funct == fun)
1.11      db        271:                        return (fn->fn_name);
1.1       art       272:        }
1.11      db        273:        return (NULL);
1.1       art       274: }
                    275:
                    276: /*
1.11      db        277:  * List possible function name completions.
1.1       art       278:  */
1.17      deraadt   279: struct list *
1.12      otto      280: complete_function_list(const char *fname)
1.1       art       281: {
1.11      db        282:        struct funmap   *fn;
1.17      deraadt   283:        struct list             *head, *el;
1.11      db        284:        int              len;
1.1       art       285:
                    286:        len = strlen(fname);
                    287:        head = NULL;
                    288:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    289:                if (memcmp(fname, fn->fn_name, len) == 0) {
                    290:                        if ((el = malloc(sizeof(*el))) == NULL) {
                    291:                                free_file_list(head);
1.11      db        292:                                return (NULL);
1.1       art       293:                        }
                    294:                        el->l_name = fn->fn_name;
                    295:                        el->l_next = head;
                    296:                        head = el;
                    297:                }
                    298:        }
1.11      db        299:        return (head);
1.1       art       300: }