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

1.64    ! op          1: /*     $OpenBSD: funmap.c,v 1.63 2021/04/22 19:50:55 lum Exp $ */
1.28      kjell       2:
                      3: /* This file is in the public domain */
                      4:
1.49      bcallah     5: #include <sys/queue.h>
                      6: #include <signal.h>
                      7: #include <stdio.h>
                      8: #include <stdlib.h>
                      9: #include <string.h>
                     10:
1.1       art        11: #include "def.h"
1.49      bcallah    12: #include "funmap.h"
1.1       art        13: #include "kbd.h"
                     14:
                     15: /*
1.59      lum        16:  * funmap structure: a list of functions and their command-names/#parameters.
                     17:  *
                     18:  * If the function is NULL, it must be listed with the same name in the
                     19:  * map_table.
1.1       art        20:  */
                     21: struct funmap {
1.6       vincent    22:        PF               fn_funct;
                     23:        const            char *fn_name;
1.59      lum        24:        int              fn_nparams;
1.1       art        25:        struct funmap   *fn_next;
                     26: };
                     27: static struct funmap *funs;
                     28:
1.59      lum        29: /*
                     30:  * 3rd column in the functnames structure indicates how many parameters the
                     31:  * function takes in 'normal' usage. This column is only used to identify
                     32:  * function profiles when lines of a buffer are being evaluated via excline().
                     33:  *
                     34:  *  0 = a toggle, non-modifiable insert/delete, region modifier, etc
                     35:  *  1 = value can be string or number value (like: file/buf name, search string)
                     36:  *  2 = multiple type value required, see auto-execute, or global-set-key, etc
1.62      lum        37:  * -1 = error: interactive commmand, unsuitable for interpreter
1.59      lum        38:  *
                     39:  * Some functions when used interactively may ask for a 'y' or 'n' (or another
                     40:  * character) to continue, in excline, a 'y' is assumed. Functions like this
                     41:  * have '0' in the 3rd column below.
                     42:  */
1.1       art        43: static struct funmap functnames[] = {
1.59      lum        44:        {apropos_command, "apropos", 1},
                     45:        {toggleaudiblebell, "audible-bell", 0},
                     46:        {auto_execute, "auto-execute", 2},
                     47:        {fillmode, "auto-fill-mode", 0},
                     48:        {indentmode, "auto-indent-mode", 0},
                     49:        {backtoindent, "back-to-indentation", 0},
                     50:        {backuptohomedir, "backup-to-home-directory", 0},
1.60      lum        51:        {backchar, "backward-char", 1},
                     52:        {delbword, "backward-kill-word", 1},
                     53:        {gotobop, "backward-paragraph", 1},
                     54:        {backword, "backward-word", 1},
1.59      lum        55:        {gotobob, "beginning-of-buffer", 0},
                     56:        {gotobol, "beginning-of-line", 0},
                     57:        {showmatch, "blink-and-insert", 1},             /* startup only */
                     58:        {bsmap, "bsmap-mode", 0},
                     59:        {NULL, "c-x 4 prefix", 0},                      /* internal     */
                     60:        {NULL, "c-x prefix", 0},                        /* internal     */
                     61:        {executemacro, "call-last-kbd-macro", 0},
1.60      lum        62:        {capword, "capitalize-word", 1},
1.59      lum        63:        {changedir, "cd", 1},
                     64:        {clearmark, "clear-mark", 0},
                     65:        {colnotoggle, "column-number-mode", 0},
                     66:        {copyregion, "copy-region-as-kill", 0},
1.1       art        67: #ifdef REGEX
1.59      lum        68:        {cntmatchlines, "count-matches", 1},
                     69:        {cntnonmatchlines, "count-non-matches", 1},
1.1       art        70: #endif /* REGEX */
1.59      lum        71:        {cscreatelist, "cscope-create-list-of-files-to-index", 1},
                     72:        {csfuncalled, "cscope-find-called-functions", 1},
                     73:        {csegrep, "cscope-find-egrep-pattern", 1},
                     74:        {csfindinc, "cscope-find-files-including-file", 1},
                     75:        {cscallerfuncs, "cscope-find-functions-calling-this-function", 1},
                     76:        {csdefinition, "cscope-find-global-definition", 1},
                     77:        {csfindfile, "cscope-find-this-file", 1},
                     78:        {cssymbol, "cscope-find-this-symbol", 1},
                     79:        {csfindtext, "cscope-find-this-text-string", 1},
                     80:        {csnextfile, "cscope-next-file", 0},
                     81:        {csnextmatch, "cscope-next-symbol", 0},
                     82:        {csprevfile, "cscope-prev-file", 0},
                     83:        {csprevmatch, "cscope-prev-symbol", 0},
                     84:        {redefine_key, "define-key", 3},
1.60      lum        85:        {backdel, "delete-backward-char", 1},
1.59      lum        86:        {deblank, "delete-blank-lines", 0},
1.60      lum        87:        {forwdel, "delete-char", 1},
1.59      lum        88:        {delwhite, "delete-horizontal-space", 0},
                     89:        {delleadwhite, "delete-leading-space", 0},
1.1       art        90: #ifdef REGEX
1.59      lum        91:        {delmatchlines, "delete-matching-lines", 1},
                     92:        {delnonmatchlines, "delete-non-matching-lines", 1},
1.1       art        93: #endif /* REGEX */
1.59      lum        94:        {onlywind, "delete-other-windows", 0},
                     95:        {deltrailwhite, "delete-trailing-space", 0},
                     96:        {delwind, "delete-window", 0},
                     97:        {wallchart, "describe-bindings", 0},
                     98:        {desckey, "describe-key-briefly", 1},
                     99:        {diffbuffer, "diff-buffer-with-file", 0},
                    100:        {digit_argument, "digit-argument", 1},
1.61      lum       101:        {dired_jump, "dired-jump", 1},
1.59      lum       102:        {lowerregion, "downcase-region", 0},
1.60      lum       103:        {lowerword, "downcase-word", 1},
1.59      lum       104:        {showversion, "emacs-version", 0},
                    105:        {finishmacro, "end-kbd-macro", 0},
                    106:        {gotoeob, "end-of-buffer", 0},
                    107:        {gotoeol, "end-of-line", 0},
                    108:        {enlargewind, "enlarge-window", 0},
                    109:        {NULL, "esc prefix", 0},                        /* internal     */
                    110:        {evalbuffer, "eval-current-buffer", 0},
                    111:        {evalexpr, "eval-expression", 0},
                    112:        {swapmark, "exchange-point-and-mark", 0},
                    113:        {extend, "execute-extended-command", 1},
                    114:        {fillpara, "fill-paragraph", 0},
                    115:        {filevisitalt, "find-alternate-file", 1},
                    116:        {filevisit, "find-file", 1},
                    117:        {poptofile, "find-file-other-window", 1},
                    118:        {filevisitro, "find-file-read-only", 1},
                    119:        {findtag, "find-tag", 1},
1.60      lum       120:        {forwchar, "forward-char", 1},
                    121:        {gotoeop, "forward-paragraph", 1},
                    122:        {forwword, "forward-word", 1},
1.59      lum       123:        {bindtokey, "global-set-key", 2},
                    124:        {unbindtokey, "global-unset-key", 1},
                    125:        {globalwdtoggle, "global-wd-mode", 0},
                    126:        {gotoline, "goto-line", 1},
                    127:        {help_help, "help-help", 0},
                    128:        {indent, "indent-current-line", 0},
                    129:        {insert, "insert", 1},
                    130:        {bufferinsert, "insert-buffer", 1},
                    131:        {fileinsert, "insert-file", 1},
                    132:        {fillword, "insert-with-wrap", 1},              /* startup only */
                    133:        {backisearch, "isearch-backward", 1},
                    134:        {forwisearch, "isearch-forward", 1},
                    135:        {joinline, "join-line", 0},
                    136:        {justone, "just-one-space", 0},
                    137:        {ctrlg, "keyboard-quit", 0},
                    138:        {killbuffer_cmd, "kill-buffer", 1},
1.60      lum       139:        {killline, "kill-line", 1},
                    140:        {killpara, "kill-paragraph", 1},
1.64    ! op        141:        {zaptochar, "zap-to-char", 1},
        !           142:        {zapuptochar, "zap-up-to-char", 1},
1.59      lum       143:        {killregion, "kill-region", 0},
1.60      lum       144:        {delfword, "kill-word", 1},
1.59      lum       145:        {toggleleavetmp, "leave-tmpdir-backups", 0},
                    146:        {linenotoggle, "line-number-mode", 0},
                    147:        {listbuffers, "list-buffers", 0},
                    148:        {evalfile, "load", 1},
                    149:        {localbind, "local-set-key", 1},
                    150:        {localunbind, "local-unset-key", 1},
                    151:        {makebkfile, "make-backup-files", 0},
                    152:        {makedir, "make-directory", 1},
1.60      lum       153:        {markpara, "mark-paragraph", 1},
1.59      lum       154:        {markbuffer, "mark-whole-buffer", 0},
                    155:        {do_meta, "meta-key-mode", 0},  /* better name, anyone? */
                    156:        {negative_argument, "negative-argument", 1},
1.60      lum       157:        {enewline, "newline", 1},
                    158:        {lfindent, "newline-and-indent", 1},
                    159:        {forwline, "next-line", 1},
1.1       art       160: #ifdef NOTAB
1.59      lum       161:        {notabmode, "no-tab-mode", 0},
1.1       art       162: #endif /* NOTAB */
1.59      lum       163:        {notmodified, "not-modified", 0},
1.60      lum       164:        {openline, "open-line", 1},
1.59      lum       165:        {nextwind, "other-window", 0},
                    166:        {overwrite_mode, "overwrite-mode", 0},
                    167:        {poptag, "pop-tag-mark", 0},
                    168:        {prefixregion, "prefix-region", 0},
1.60      lum       169:        {backline, "previous-line", 1},
1.59      lum       170:        {prevwind, "previous-window", 0},
                    171:        {spawncli, "push-shell", 0},
                    172:        {showcwdir, "pwd", 0},
1.62      lum       173:        {queryrepl, "query-replace", -1},
1.15      deraadt   174: #ifdef REGEX
1.62      lum       175:        {re_queryrepl, "query-replace-regexp", -1},
1.1       art       176: #endif /* REGEX */
1.59      lum       177:        {quote, "quoted-insert", 1},
1.1       art       178: #ifdef REGEX
1.59      lum       179:        {re_searchagain, "re-search-again", 0},
                    180:        {re_backsearch, "re-search-backward", 0},
                    181:        {re_forwsearch, "re-search-forward", 0},
1.1       art       182: #endif /* REGEX */
1.59      lum       183:        {reposition, "recenter", 0},
                    184:        {redraw, "redraw-display", 0},
1.42      florian   185: #ifdef REGEX
1.63      lum       186:        {re_repl, "replace-regexp", 2},
1.59      lum       187:        {replstr, "replace-string", 2},
1.42      florian   188: #endif /* REGEX */
1.59      lum       189:        {revertbuffer, "revert-buffer", 0},
1.60      lum       190:        {filesave, "save-buffer", 1},
1.59      lum       191:        {quit, "save-buffers-kill-emacs", 0},
                    192:        {savebuffers, "save-some-buffers", 0},
1.60      lum       193:        {backpage, "scroll-down", 1},
                    194:        {back1page, "scroll-one-line-down", 1},
                    195:        {forw1page, "scroll-one-line-up", 1},
                    196:        {pagenext, "scroll-other-window", 1},
                    197:        {forwpage, "scroll-up", 1},
1.59      lum       198:        {searchagain, "search-again", 0},
                    199:        {backsearch, "search-backward", 0},
                    200:        {forwsearch, "search-forward", 0},
                    201:        {ask_selfinsert, "self-insert-char", 1},
                    202:        {selfinsert, "self-insert-command", 1},         /* startup only */
                    203:        {sentencespace, "sentence-end-double-space", 0},
1.1       art       204: #ifdef REGEX
1.59      lum       205:        {setcasefold, "set-case-fold-search", 0},
1.1       art       206: #endif /* REGEX */
1.59      lum       207:        {setcasereplace, "set-case-replace", 0},
                    208:        {set_default_mode, "set-default-mode", 1},
                    209:        {setfillcol, "set-fill-column", 1},
                    210:        {setmark, "set-mark-command", 0},
                    211:        {setprefix, "set-prefix-string", 1},
                    212:        {shellcommand, "shell-command", 1},
1.60      lum       213:        {piperegion, "shell-command-on-region", 1},
                    214:        {shrinkwind, "shrink-window", 1},
1.1       art       215: #ifdef NOTAB
1.59      lum       216:        {space_to_tabstop, "space-to-tabstop", 0},
1.1       art       217: #endif /* NOTAB */
1.59      lum       218:        {splitwind, "split-window-vertically", 0},
                    219:        {definemacro, "start-kbd-macro", 0},
                    220:        {spawncli, "suspend-emacs", 0},
                    221:        {usebuffer, "switch-to-buffer", 1},
                    222:        {poptobuffer, "switch-to-buffer-other-window", 1},
                    223:        {togglereadonly, "toggle-read-only", 0},
                    224:        {togglereadonlyall, "toggle-read-only-all", 0},
                    225:        {twiddle, "transpose-chars", 0},
                    226:        {transposepara, "transpose-paragraphs", 0},
                    227:        {transposeword, "transpose-words", 0},
                    228:        {undo, "undo", 0},
                    229:        {undo_add_boundary, "undo-boundary", 0},
                    230:        {undo_boundary_enable, "undo-boundary-toggle", 0},
                    231:        {undo_enable, "undo-enable", 0},
                    232:        {undo_dump, "undo-list", 0},
                    233:        {universal_argument, "universal-argument", 1},
                    234:        {upperregion, "upcase-region", 0},
1.60      lum       235:        {upperword, "upcase-word", 1},
1.59      lum       236:        {togglevisiblebell, "visible-bell", 0},
                    237:        {tagsvisit, "visit-tags-table", 0},
                    238:        {showcpos, "what-cursor-position", 0},
                    239:        {filewrite, "write-file", 1},
1.60      lum       240:        {yank, "yank", 1},
1.59      lum       241:        {NULL, NULL, 0}
1.1       art       242: };
                    243:
                    244: void
                    245: funmap_init(void)
                    246: {
                    247:        struct funmap *fn;
                    248:
                    249:        for (fn = functnames; fn->fn_name != NULL; fn++) {
                    250:                fn->fn_next = funs;
                    251:                funs = fn;
                    252:        }
                    253: }
                    254:
                    255: int
1.59      lum       256: funmap_add(PF fun, const char *fname, int fparams)
1.1       art       257: {
                    258:        struct funmap *fn;
                    259:
                    260:        if ((fn = malloc(sizeof(*fn))) == NULL)
1.11      db        261:                return (FALSE);
1.1       art       262:
                    263:        fn->fn_funct = fun;
                    264:        fn->fn_name = fname;
1.59      lum       265:        fn->fn_nparams = fparams;
1.1       art       266:        fn->fn_next = funs;
                    267:
                    268:        funs = fn;
1.11      db        269:        return (TRUE);
1.1       art       270: }
                    271:
                    272: /*
                    273:  * Translate from function name to function pointer.
                    274:  */
                    275: PF
1.6       vincent   276: name_function(const char *fname)
1.1       art       277: {
                    278:        struct funmap *fn;
                    279:
                    280:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    281:                if (strcmp(fn->fn_name, fname) == 0)
1.11      db        282:                        return (fn->fn_funct);
1.1       art       283:        }
1.11      db        284:        return (NULL);
1.1       art       285: }
                    286:
1.6       vincent   287: const char *
1.1       art       288: function_name(PF fun)
                    289: {
                    290:        struct funmap *fn;
                    291:
                    292:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    293:                if (fn->fn_funct == fun)
1.11      db        294:                        return (fn->fn_name);
1.1       art       295:        }
1.11      db        296:        return (NULL);
1.1       art       297: }
                    298:
                    299: /*
1.11      db        300:  * List possible function name completions.
1.1       art       301:  */
1.17      deraadt   302: struct list *
1.12      otto      303: complete_function_list(const char *fname)
1.1       art       304: {
1.11      db        305:        struct funmap   *fn;
1.23      kjell     306:        struct list     *head, *el;
1.11      db        307:        int              len;
1.1       art       308:
                    309:        len = strlen(fname);
                    310:        head = NULL;
                    311:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    312:                if (memcmp(fname, fn->fn_name, len) == 0) {
                    313:                        if ((el = malloc(sizeof(*el))) == NULL) {
                    314:                                free_file_list(head);
1.11      db        315:                                return (NULL);
1.1       art       316:                        }
1.23      kjell     317:                        el->l_name = strdup(fn->fn_name);
1.1       art       318:                        el->l_next = head;
                    319:                        head = el;
                    320:                }
                    321:        }
1.11      db        322:        return (head);
1.59      lum       323: }
                    324:
                    325: /*
                    326:  * Find number of parameters for function name.
                    327:  */
                    328: int
                    329: numparams_function(PF fun)
                    330: {
                    331:        struct funmap *fn;
                    332:
                    333:        for (fn = funs; fn != NULL; fn = fn->fn_next) {
                    334:                if (fn->fn_funct == fun)
                    335:                        return (fn->fn_nparams);
                    336:        }
                    337:        return (FALSE);
1.1       art       338: }