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