xref: /openbsd-src/usr.bin/mg/funmap.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: funmap.c,v 1.3 2001/05/24 13:00:45 art Exp $	*/
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  */
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 {
35 	PF		fn_funct;
36 	char		*fn_name;
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 */
46 	{fillmode, "auto-fill-mode",},
47 	{indentmode, "auto-indent-mode",},
48 	{backchar, "backward-char",},
49 	{delbword, "backward-kill-word",},
50 	{gotobop, "backward-paragraph",},
51 	{backword, "backward-word",},
52 	{gotobob, "beginning-of-buffer",},
53 	{gotobol, "beginning-of-line",},
54 	{blinkparen, "blink-matching-paren",},
55 	{showmatch, "blink-matching-paren-hack",},
56 #ifdef BSMAP
57 	{bsmap, "bsmap-mode",},
58 #endif /* BSMAP */
59 	{NULL, "c-x 4 prefix",},
60 	{NULL, "c-x prefix",},
61 #ifndef NO_MACRO
62 	{executemacro, "call-last-kbd-macro",},
63 #endif /* !NO_MACRO */
64 	{capword, "capitalize-word",},
65 #ifndef NO_DIR
66 	{changedir, "cd",},
67 #endif /* !NO_DIR */
68 	{copyregion, "copy-region-as-kill",},
69 #ifdef	REGEX
70 	{cntmatchlines, "count-matches",},
71 	{cntnonmatchlines, "count-non-matches",},
72 #endif /* REGEX */
73 	{define_key, "define-key",},
74 	{backdel, "delete-backward-char",},
75 	{deblank, "delete-blank-lines",},
76 	{forwdel, "delete-char",},
77 	{delwhite, "delete-horizontal-space",},
78 #ifdef	REGEX
79 	{delmatchlines, "delete-matching-lines",},
80 	{delnonmatchlines, "delete-non-matching-lines",},
81 #endif /* REGEX */
82 	{onlywind, "delete-other-windows",},
83 	{delwind, "delete-window",},
84 #ifndef NO_HELP
85 	{wallchart, "describe-bindings",},
86 	{desckey, "describe-key-briefly",},
87 #endif /* !NO_HELP */
88 	{digit_argument, "digit-argument",},
89 #ifndef NO_DIRED
90 	{dired, "dired",},
91 	{d_undelbak, "dired-backup-unflag",},
92 	{d_copy, "dired-copy-file",},
93 	{d_expunge, "dired-do-deletions",},
94 	{d_findfile, "dired-find-file",},
95 	{d_ffotherwindow, "dired-find-file-other-window",},
96 	{d_del, "dired-flag-file-deleted",},
97 	{d_otherwindow, "dired-other-window",},
98 	{d_rename, "dired-rename-file",},
99 	{d_undel, "dired-unflag",},
100 #endif /* !NO_DIRED */
101 	{lowerregion, "downcase-region",},
102 	{lowerword, "downcase-word",},
103 	{showversion, "emacs-version",},
104 #ifndef NO_MACRO
105 	{finishmacro, "end-kbd-macro",},
106 #endif /* !NO_MACRO */
107 	{gotoeob, "end-of-buffer",},
108 	{gotoeol, "end-of-line",},
109 	{enlargewind, "enlarge-window",},
110 	{NULL, "esc prefix",},
111 #ifndef NO_STARTUP
112 	{evalbuffer, "eval-current-buffer",},
113 	{evalexpr, "eval-expression",},
114 #endif /* !NO_STARTUP */
115 	{swapmark, "exchange-point-and-mark",},
116 	{extend, "execute-extended-command",},
117 	{fillpara, "fill-paragraph",},
118 	{filevisit, "find-file",},
119 	{poptofile, "find-file-other-window",},
120 	{forwchar, "forward-char",},
121 	{gotoeop, "forward-paragraph",},
122 	{forwword, "forward-word",},
123 	{bindtokey, "global-set-key",},
124 	{unbindtokey, "global-unset-key",},
125 	{gotoline, "goto-line",},
126 #ifndef NO_HELP
127 	{NULL, "help",},
128 	{help_help, "help-help",},
129 #endif /* !NO_HELP */
130 	{insert, "insert",},
131 	{bufferinsert, "insert-buffer",},
132 	{fileinsert, "insert-file",},
133 	{fillword, "insert-with-wrap",},
134 	{backisearch, "isearch-backward",},
135 	{forwisearch, "isearch-forward",},
136 	{justone, "just-one-space",},
137 	{ctrlg, "keyboard-quit",},
138 	{killbuffer, "kill-buffer",},
139 	{killline, "kill-line",},
140 	{killpara, "kill-paragraph",},
141 	{killregion, "kill-region",},
142 	{delfword, "kill-word",},
143 	{listbuffers, "list-buffers",},
144 #ifndef NO_STARTUP
145 	{evalfile, "load",},
146 #endif /* !NO_STARTUP */
147 	{localbind, "local-set-key",},
148 	{localunbind, "local-unset-key",},
149 #ifndef NO_BACKUP
150 	{makebkfile, "make-backup-files",},
151 #endif /* !NO_BACKUP */
152 #ifdef DO_METAKEY
153 	{do_meta, "meta-key-mode",},	/* better name, anyone? */
154 #endif /* DO_METAKEY */
155 	{negative_argument, "negative-argument",},
156 	{newline, "newline",},
157 	{indent, "newline-and-indent",},
158 	{forwline, "next-line",},
159 #ifdef NOTAB
160 	{notabmode, "no-tab-mode",},
161 #endif /* NOTAB */
162 	{notmodified, "not-modified",},
163 	{openline, "open-line",},
164 	{nextwind, "other-window",},
165 	{overwrite, "overwrite-mode",},
166 #ifdef PREFIXREGION
167 	{prefixregion, "prefix-region",},
168 #endif /* PREFIXREGION */
169 	{backline, "previous-line",},
170 #ifdef GOSMACS
171 	{prevwind, "previous-window",},
172 #endif /* GOSEMACS */
173 	{spawncli, "push-shell",},
174 #ifndef NO_DIR
175 	{showcwdir, "pwd",},
176 #endif /* !NO_DIR */
177 	{queryrepl, "query-replace",},
178 #ifdef REGEX
179 	{re_queryrepl, "query-replace-regexp",},
180 #endif /* REGEX */
181 	{quote, "quoted-insert",},
182 #ifdef REGEX
183 	{re_searchagain, "re-search-again",},
184 	{re_backsearch, "re-search-backward",},
185 	{re_forwsearch, "re-search-forward",},
186 #endif /* REGEX */
187 	{reposition, "recenter",},
188 	{refresh, "redraw-display",},
189 	{filesave, "save-buffer",},
190 	{quit, "save-buffers-kill-emacs",},
191 	{savebuffers, "save-some-buffers",},
192 	{backpage, "scroll-down",},
193 #ifdef GOSMACS
194 	{back1page, "scroll-one-line-down",},
195 	{forw1page, "scroll-one-line-up",},
196 #endif /* GOSMACS */
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",},
223 	{twiddle, "transpose-chars",},
224 	{universal_argument, "universal-argument",},
225 	{upperregion, "upcase-region",},
226 	{upperword, "upcase-word",},
227 	{showcpos, "what-cursor-position",},
228 	{filewrite, "write-file",},
229 	{yank, "yank",},
230 	{NULL, NULL,},
231 };
232 
233 void
234 funmap_init(void)
235 {
236 	struct funmap *fn;
237 
238 	for (fn = functnames; fn->fn_name != NULL; fn++) {
239 		fn->fn_next = funs;
240 		funs = fn;
241 	}
242 }
243 
244 int
245 funmap_add(PF fun, char *fname)
246 {
247 	struct funmap *fn;
248 
249 	if ((fn = malloc(sizeof(*fn))) == NULL)
250 		return FALSE;
251 
252 	fn->fn_funct = fun;
253 	fn->fn_name = fname;
254 	fn->fn_next = funs;
255 
256 	funs = fn;
257 	return TRUE;
258 }
259 
260 /*
261  * Translate from function name to function pointer.
262  */
263 PF
264 name_function(char *fname)
265 {
266 	struct funmap *fn;
267 
268 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
269 		if (strcmp(fn->fn_name, fname) == 0)
270 			return fn->fn_funct;
271 	}
272 	return NULL;
273 }
274 
275 char *
276 function_name(PF fun)
277 {
278 	struct funmap *fn;
279 
280 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
281 		if (fn->fn_funct == fun)
282 			return fn->fn_name;
283 	}
284 	return NULL;
285 }
286 
287 /*
288  * list possible function name completions.
289  */
290 LIST *
291 complete_function_list(char *fname, int c)
292 {
293 	struct funmap *fn;
294 	LIST *head, *el;
295 	int len;
296 
297 	len = strlen(fname);
298 	head = NULL;
299 	for (fn = funs; fn != NULL; fn = fn->fn_next) {
300 		if (memcmp(fname, fn->fn_name, len) == 0) {
301 			if ((el = malloc(sizeof(*el))) == NULL) {
302 				free_file_list(head);
303 				return NULL;
304 			}
305 			el->l_name = fn->fn_name;
306 			el->l_next = head;
307 			head = el;
308 		}
309 	}
310 
311 	return head;
312 }
313 
314 
315 
316