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