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