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