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