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

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