xref: /openbsd/usr.bin/mg/funmap.c (revision cca36db2)
1 /*	$OpenBSD: funmap.c,v 1.38 2012/04/12 04:47:59 lum Exp $	*/
2 
3 /* This file is in the public domain */
4 
5 #include "def.h"
6 #include "kbd.h"
7 #include "funmap.h"
8 
9 /*
10  * If the function is NULL, it must be listed with the
11  * same name in the map_table.
12  */
13 
14 struct funmap {
15 	PF		 fn_funct;
16 	const		 char *fn_name;
17 	struct funmap	*fn_next;
18 };
19 
20 static struct funmap *funs;
21 
22 static struct funmap functnames[] = {
23 	{apropos_command, "apropos",},
24 	{auto_execute, "auto-execute", },
25 	{fillmode, "auto-fill-mode",},
26 	{indentmode, "auto-indent-mode",},
27 	{backtoindent, "back-to-indentation",},
28 	{backchar, "backward-char",},
29 	{delbword, "backward-kill-word",},
30 	{gotobop, "backward-paragraph",},
31 	{backword, "backward-word",},
32 	{gotobob, "beginning-of-buffer",},
33 	{gotobol, "beginning-of-line",},
34 	{showmatch, "blink-and-insert",},
35 	{bsmap, "bsmap-mode",},
36 	{NULL, "c-x 4 prefix",},
37 	{NULL, "c-x prefix",},
38 	{executemacro, "call-last-kbd-macro",},
39 	{capword, "capitalize-word",},
40 	{changedir, "cd",},
41 	{clearmark, "clear-mark",},
42 	{copyregion, "copy-region-as-kill",},
43 #ifdef	REGEX
44 	{cntmatchlines, "count-matches",},
45 	{cntnonmatchlines, "count-non-matches",},
46 #endif /* REGEX */
47 	{redefine_key, "define-key",},
48 	{backdel, "delete-backward-char",},
49 	{deblank, "delete-blank-lines",},
50 	{forwdel, "delete-char",},
51 	{delwhite, "delete-horizontal-space",},
52 	{delleadwhite, "delete-leading-space",},
53 	{deltrailwhite, "delete-trailing-space",},
54 #ifdef	REGEX
55 	{delmatchlines, "delete-matching-lines",},
56 	{delnonmatchlines, "delete-non-matching-lines",},
57 #endif /* REGEX */
58 	{onlywind, "delete-other-windows",},
59 	{delwind, "delete-window",},
60 	{wallchart, "describe-bindings",},
61 	{desckey, "describe-key-briefly",},
62 	{digit_argument, "digit-argument",},
63 	{lowerregion, "downcase-region",},
64 	{lowerword, "downcase-word",},
65 	{showversion, "emacs-version",},
66 	{finishmacro, "end-kbd-macro",},
67 	{globalwdtoggle, "global-wd-mode",},
68 	{gotoeob, "end-of-buffer",},
69 	{gotoeol, "end-of-line",},
70 	{enlargewind, "enlarge-window",},
71 	{NULL, "esc prefix",},
72 	{evalbuffer, "eval-current-buffer",},
73 	{evalexpr, "eval-expression",},
74 	{swapmark, "exchange-point-and-mark",},
75 	{extend, "execute-extended-command",},
76 	{fillpara, "fill-paragraph",},
77 	{filevisit, "find-file",},
78 	{filevisitro, "find-file-read-only",},
79 	{filevisitalt, "find-alternate-file",},
80 	{poptofile, "find-file-other-window",},
81 	{forwchar, "forward-char",},
82 	{gotoeop, "forward-paragraph",},
83 	{forwword, "forward-word",},
84 	{bindtokey, "global-set-key",},
85 	{unbindtokey, "global-unset-key",},
86 	{gotoline, "goto-line",},
87 	{help_help, "help-help",},
88 	{insert, "insert",},
89 	{bufferinsert, "insert-buffer",},
90 	{fileinsert, "insert-file",},
91 	{fillword, "insert-with-wrap",},
92 	{backisearch, "isearch-backward",},
93 	{forwisearch, "isearch-forward",},
94 	{joinline, "join-line",},
95 	{justone, "just-one-space",},
96 	{ctrlg, "keyboard-quit",},
97 	{killbuffer_cmd, "kill-buffer",},
98 	{killline, "kill-line",},
99 	{killpara, "kill-paragraph",},
100 	{killregion, "kill-region",},
101 	{delfword, "kill-word",},
102 	{linenotoggle, "line-number-mode",},
103 	{listbuffers, "list-buffers",},
104 	{evalfile, "load",},
105 	{localbind, "local-set-key",},
106 	{localunbind, "local-unset-key",},
107 	{makebkfile, "make-backup-files",},
108 	{markbuffer, "mark-whole-buffer",},
109 	{do_meta, "meta-key-mode",},	/* better name, anyone? */
110 	{negative_argument, "negative-argument",},
111 	{newline, "newline",},
112 	{lfindent, "newline-and-indent",},
113 	{indent, "indent-current-line",},
114 	{forwline, "next-line",},
115 #ifdef NOTAB
116 	{notabmode, "no-tab-mode",},
117 #endif /* NOTAB */
118 	{notmodified, "not-modified",},
119 	{openline, "open-line",},
120 	{nextwind, "other-window",},
121 	{overwrite_mode, "overwrite-mode",},
122 	{prefixregion, "prefix-region",},
123 	{backline, "previous-line",},
124 	{prevwind, "previous-window",},
125 	{poptag, "pop-tag-mark",},
126 	{spawncli, "push-shell",},
127 	{findtag, "find-tag",},
128 	{tagsvisit, "visit-tags-table",},
129 	{showcwdir, "pwd",},
130 	{queryrepl, "query-replace",},
131 #ifdef REGEX
132 	{replstr, "replace-string",},
133 	{re_queryrepl, "query-replace-regexp",},
134 #endif /* REGEX */
135 	{quote, "quoted-insert",},
136 #ifdef REGEX
137 	{re_searchagain, "re-search-again",},
138 	{re_backsearch, "re-search-backward",},
139 	{re_forwsearch, "re-search-forward",},
140 #endif /* REGEX */
141 	{reposition, "recenter",},
142 	{redraw, "redraw-display",},
143 	{filesave, "save-buffer",},
144 	{quit, "save-buffers-kill-emacs",},
145 	{savebuffers, "save-some-buffers",},
146 	{backpage, "scroll-down",},
147 	{back1page, "scroll-one-line-down",},
148 	{forw1page, "scroll-one-line-up",},
149 	{pagenext, "scroll-other-window",},
150 	{forwpage, "scroll-up",},
151 	{searchagain, "search-again",},
152 	{backsearch, "search-backward",},
153 	{forwsearch, "search-forward",},
154 	{selfinsert, "self-insert-command",},
155 #ifdef REGEX
156 	{setcasefold, "set-case-fold-search",},
157 #endif /* REGEX */
158 	{set_default_mode, "set-default-mode",},
159 	{setfillcol, "set-fill-column",},
160 	{setmark, "set-mark-command",},
161 	{setprefix, "set-prefix-string",},
162 	{piperegion, "shell-command-on-region",},
163 	{shrinkwind, "shrink-window",},
164 #ifdef NOTAB
165 	{space_to_tabstop, "space-to-tabstop",},
166 #endif /* NOTAB */
167 	{splitwind, "split-window-vertically",},
168 	{definemacro, "start-kbd-macro",},
169 	{spawncli, "suspend-emacs",},
170 	{usebuffer, "switch-to-buffer",},
171 	{poptobuffer, "switch-to-buffer-other-window",},
172 	{togglereadonly, "toggle-read-only" },
173 	{twiddle, "transpose-chars",},
174 	{undo, "undo", },
175 	{undo_enable, "undo-enable", },
176 	{undo_boundary_enable, "undo-boundary-toggle", },
177 	{undo_add_boundary, "undo-boundary", },
178 	{undo_dump, "undo-list", },
179 	{universal_argument, "universal-argument",},
180 	{upperregion, "upcase-region",},
181 	{upperword, "upcase-word",},
182 	{showcpos, "what-cursor-position",},
183 	{filewrite, "write-file",},
184 	{yank, "yank",},
185 	{NULL, NULL,}
186 };
187 
188 void
189 funmap_init(void)
190 {
191 	struct funmap *fn;
192 
193 	for (fn = functnames; fn->fn_name != NULL; fn++) {
194 		fn->fn_next = funs;
195 		funs = fn;
196 	}
197 }
198 
199 int
200 funmap_add(PF fun, const char *fname)
201 {
202 	struct funmap *fn;
203 
204 	if ((fn = malloc(sizeof(*fn))) == NULL)
205 		return (FALSE);
206 
207 	fn->fn_funct = fun;
208 	fn->fn_name = fname;
209 	fn->fn_next = funs;
210 
211 	funs = fn;
212 	return (TRUE);
213 }
214 
215 /*
216  * Translate from function name to function pointer.
217  */
218 PF
219 name_function(const char *fname)
220 {
221 	struct funmap *fn;
222 
223 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
224 		if (strcmp(fn->fn_name, fname) == 0)
225 			return (fn->fn_funct);
226 	}
227 	return (NULL);
228 }
229 
230 const char *
231 function_name(PF fun)
232 {
233 	struct funmap *fn;
234 
235 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
236 		if (fn->fn_funct == fun)
237 			return (fn->fn_name);
238 	}
239 	return (NULL);
240 }
241 
242 /*
243  * List possible function name completions.
244  */
245 struct list *
246 complete_function_list(const char *fname)
247 {
248 	struct funmap	*fn;
249 	struct list	*head, *el;
250 	int		 len;
251 
252 	len = strlen(fname);
253 	head = NULL;
254 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
255 		if (memcmp(fname, fn->fn_name, len) == 0) {
256 			if ((el = malloc(sizeof(*el))) == NULL) {
257 				free_file_list(head);
258 				return (NULL);
259 			}
260 			el->l_name = strdup(fn->fn_name);
261 			el->l_next = head;
262 			head = el;
263 		}
264 	}
265 	return (head);
266 }
267