]> git.lyx.org Git - lyx.git/blob - src/LyXAction.C
Tiny things found while peering at chset code
[lyx.git] / src / LyXAction.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-1999 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "LyXAction.h"
18 #include "debug.h"
19 #include "gettext.h"
20 #include "support/lstrings.h"
21
22  /*  
23      NAMING RULES FOR USER-COMMANDS
24      Here's the set of rules to apply when a new command name is introduced:
25  
26      1) Use the object.event order. That is, use `word-forward'
27         instead of `forward-word'.
28      2) Don't introduce an alias for an already named object. Same for events.
29      3) Forward movement or focus is called `forward' (not `right').
30      4) Backward movement or focus is called `backward' (not `left').
31      5) Upward movement of focus is called `up'.
32      6) Downward movement is called `down'.
33      7) The begin of an object is called `begin' (not `start').
34      8) The end of an object is called `end'.
35  
36      (May 19 1996, 12:04, RvdK)
37  */
38  
39
40 // These are globals. 
41 LyXAction lyxaction;
42
43 void LyXAction::newFunc(kb_action action, string const & name,
44                         string const & helpText, unsigned int attrib)
45 {
46         lyx_func_map[name] = action;
47         func_info tmpinfo;
48         tmpinfo.name = name;
49         tmpinfo.attrib = attrib;
50         tmpinfo.helpText = helpText;
51         lyx_info_map[action] = tmpinfo;
52 }
53
54
55 struct lfun_item {
56         kb_action action;
57         char const * name;
58         char const * helpText;
59         unsigned int attrib;
60 };
61
62 void LyXAction::init()
63 {
64         // This function was changed to use the array below in initalization
65         // instead of calling newFunc numerous times because of compilation
66         // times. Since the array is not static we get back the memory it
67         // occupies after the init is completed. It compiles several
68         //magnitudes faster.
69         
70         static bool init = false;
71         if (init) return;
72
73         lfun_item items[] = {
74         { LFUN_ACUTE, "accent-acute", "", Noop },
75         { LFUN_BREVE, "accent-breve", "", Noop },
76         { LFUN_CARON, "accent-caron", "", Noop },
77         { LFUN_CEDILLA, "accent-cedilla", "", Noop },
78         { LFUN_CIRCLE, "accent-circle", "", Noop },
79         { LFUN_CIRCUMFLEX, "accent-circumflex", "", Noop },
80         { LFUN_DOT, "accent-dot", "", Noop },
81         { LFUN_GRAVE, "accent-grave", "", Noop },
82         { LFUN_HUNG_UMLAUT, "accent-hungarian-umlaut", "", Noop },
83         { LFUN_MACRON, "accent-macron", "", Noop },
84         { LFUN_OGONEK, "accent-ogonek", "", Noop },
85         { LFUN_SPECIAL_CARON, "accent-special-caron", "", Noop },
86         { LFUN_TIE, "accent-tie", "", Noop },
87         { LFUN_TILDE, "accent-tilde", "", Noop },
88         { LFUN_UMLAUT, "accent-umlaut", "", Noop },
89         { LFUN_UNDERBAR, "accent-underbar", "", Noop },
90         { LFUN_UNDERDOT, "accent-underdot", "", Noop },
91         { LFUN_VECTOR, "accent-vector", "", Noop },
92         { LFUN_APPENDIX, "appendix", N_("Insert appendix"), Noop },
93         { LFUN_APROPOS, "apropos", N_("Describe command"), NoBuffer|ReadOnly },
94         { LFUN_LEFTSEL, "backward-select",
95           N_("Select previous char"), ReadOnly },
96         { LFUN_BIBDB_ADD, "bibtex-database-add", "", Noop },
97         { LFUN_BIBDB_DEL, "bibtex-database-del", "", Noop },
98         { LFUN_INSERT_BIBTEX, "bibtex-insert", N_("Insert bibtex"), Noop },
99         { LFUN_BIBTEX_STYLE, "bibtex-style", "", Noop },
100         { LFUN_BREAKLINE, "break-line", "", Noop },
101         { LFUN_BREAKPARAGRAPH, "break-paragraph", "", Noop },
102         { LFUN_BREAKPARAGRAPHKEEPLAYOUT, "break-paragraph-keep-layout",
103           "", Noop },
104         { LFUN_BREAKPARAGRAPH_SKIP, "break-paragraph-skip", "", Noop },
105         { LFUN_BUILDPROG, "build-program", _("Build program"), Noop },
106         { LFUN_AUTOSAVE, "buffer-auto-save", N_("Autosave"), Noop },
107         { LFUN_BEGINNINGBUF, "buffer-begin",
108           N_("Go to beginning of document"), ReadOnly },
109         { LFUN_BEGINNINGBUFSEL, "buffer-begin-select",
110           N_("Select to beginning of document"), ReadOnly },
111         { LFUN_CHILDINSERT, "buffer-child-insert", "", Noop },
112         { LFUN_CHILDOPEN, "buffer-child-open", "", ReadOnly },
113         { LFUN_RUNCHKTEX, "buffer-chktex", N_("Check TeX"), ReadOnly },
114         { LFUN_CLOSEBUFFER, "buffer-close", N_("Close"), ReadOnly },
115         { LFUN_ENDBUF, "buffer-end",
116           N_("Go to end of document"), ReadOnly },
117         { LFUN_ENDBUFSEL, "buffer-end-select",
118           N_("Select to end of document"), ReadOnly },
119         { LFUN_EXPORT, "buffer-export", N_("Export to"), ReadOnly },
120         { LFUN_FAX, "buffer-fax", N_("Fax"), ReadOnly },
121         { LFUN_INSERTFOOTNOTE, "buffer-float-insert", "", Noop },
122         { LFUN_IMPORT, "buffer-import",
123           N_("Import document"), NoBuffer },
124         { LFUN_BUFFERBULLETSSELECT, "buffer-itemize-bullets-select",
125           "", Noop },
126         { LFUN_MENUNEW, "buffer-new", N_("New document") , NoBuffer },
127         { LFUN_MENUNEWTMPLT,"buffer-new-template",
128           N_("New document from template"), NoBuffer },
129         { LFUN_MENUOPEN, "buffer-open", N_("Open"), NoBuffer },
130         { LFUN_PREVBUFFER, "buffer-previous",
131           N_("Switch to previous document"), ReadOnly },
132         { LFUN_MENUPRINT, "buffer-print", N_("Print"), ReadOnly },
133         { LFUN_MENURELOAD, "buffer-reload",
134           N_("Revert to saved"), ReadOnly },
135         { LFUN_READ_ONLY_TOGGLE, "buffer-toggle-read-only",
136           N_("Toggle read-only"), ReadOnly },
137         { LFUN_RUNLATEX, "buffer-typeset", N_("Update DVI"), ReadOnly },
138         { LFUN_RUNDVIPS, "buffer-typeset-ps",
139           N_("Update PostScript"), ReadOnly },
140         { LFUN_PREVIEW, "buffer-view", N_("View DVI") , ReadOnly },
141         { LFUN_PREVIEWPS, "buffer-view-ps",
142           N_("View PostScript") , ReadOnly },
143         { LFUN_MENUWRITE, "buffer-write", N_("Save"), ReadOnly },
144         { LFUN_MENUWRITEAS, "buffer-write-as", N_("Save As"), ReadOnly },
145         { LFUN_CANCEL, "cancel", N_("Cancel"), NoBuffer },
146         { LFUN_LEFT, "char-backward", N_("Go one char back"), ReadOnly },
147         { LFUN_RIGHT, "char-forward", N_("Go one char forward"), ReadOnly },
148         { LFUN_INSERT_CITATION, "citation-insert",
149           N_("Insert citation"), Noop },
150         { LFUN_EXEC_COMMAND, "command-execute", "", NoBuffer },
151         { LFUN_PREFIX, "command-prefix",
152           N_("Execute command"), NoBuffer },
153         { LFUN_SEQUENCE, "command-sequence", "", Noop },
154         { LFUN_COPY, "copy", N_("Copy"), ReadOnly },
155         { LFUN_CUT, "cut", N_("Cut"), Noop },
156         { LFUN_BACKSPACE, "delete-backward", "", Noop },
157         { LFUN_BACKSPACE_SKIP, "delete-backward-skip", "", Noop },
158         { LFUN_DELETE, "delete-forward", "", Noop },
159         { LFUN_DELETE_SKIP, "delete-forward-skip", "", Noop },
160         { LFUN_DEPTH_MIN, "depth-decrement",
161                 N_("Decrement environment depth"), Noop },
162         { LFUN_DEPTH_PLUS, "depth-increment",
163                 N_("Increment environment depth"), Noop },
164         { LFUN_DEPTH, "depth-next", 
165                 N_("Change environment depth"), Noop },
166         { LFUN_LDOTS, "dots-insert", N_("Insert ... dots"), Noop },
167         { LFUN_DOWN, "down", N_("Go down"), ReadOnly },
168         { LFUN_DOWNSEL, "down-select",
169                 N_("Select next line"), ReadOnly },
170         { LFUN_DROP_LAYOUTS_CHOICE, "drop-layouts-choice",
171                 N_("Choose Paragraph Environment"), ReadOnly },
172         { LFUN_END_OF_SENTENCE, "end-of-sentence-period-insert",
173                 N_("Insert end of sentence period"), Noop },
174         { LFUN_GOTOERROR, "error-next", N_("Go to next error"), Noop },
175         { LFUN_REMOVEERRORS, "error-remove-all",
176                 N_("Remove all error boxes"), ReadOnly },
177         { LFUN_FIGURE, "figure-insert", N_("Insert Figure"), Noop },
178         { LFUN_FILE_INSERT, "file-insert", "", Noop },
179         { LFUN_FILE_INSERT_ASCII, "file-insert-ascii", "", Noop },
180         { LFUN_FILE_NEW, "file-new", "", NoBuffer },
181         { LFUN_FILE_OPEN, "file-open", "", NoBuffer },
182         { LFUN_MENUSEARCH, "find-replace", N_("Find & Replace") , Noop },
183         { LFUN_BOLD, "font-bold", N_("Toggle bold"), Noop },
184         { LFUN_CODE, "font-code", N_("Toggle code style"), Noop },
185         { LFUN_DEFAULT, "font-default", N_("Default font style"), Noop },
186         { LFUN_EMPH, "font-emph", N_("Toggle emphasize"), Noop },
187         { LFUN_FREE, "font-free", N_("Toggle user defined style"), Noop },
188         { LFUN_NOUN, "font-noun", N_("Toggle noun style"), Noop },
189         { LFUN_ROMAN, "font-roman", N_("Toggle roman font style"), Noop },
190         { LFUN_SANS, "font-sans", N_("Toggle sans font style"), Noop },
191         { LFUN_FONT_SIZE, "font-size", N_("Set font size"), Noop },
192         { LFUN_FONT_STATE, "font-state", N_("Show font state"), ReadOnly },
193         { LFUN_UNDERLINE, "font-underline",
194           N_("Toggle font underline"), Noop },
195         { LFUN_FOOTMELT, "footnote-insert", N_("Insert Footnote"), Noop },
196         { LFUN_RIGHTSEL, "forward-select", N_("Select next char"), ReadOnly },
197         { LFUN_HFILL, "hfill-insert",
198           N_("Insert horizontal fill"), Noop },
199         { LFUN_HTMLURL, "html-insert", "", Noop },
200         { LFUN_HYPHENATION, "hyphenation-point-insert",
201           N_("Insert hyphenation point"), Noop },
202         { LFUN_INDEX_INSERT, "index-insert",
203           N_("Insert index item"), Noop },
204         { LFUN_INDEX_INSERT_LAST, "index-insert-last",
205           N_("Insert last index item"), Noop },
206         { LFUN_INDEX_PRINT, "index-print", N_("Insert index list"), Noop },
207         { LFUN_KMAP_OFF, "keymap-off", N_("Turn off keymap"), ReadOnly },
208         { LFUN_KMAP_PRIM, "keymap-primary",
209           N_("Use primary keymap"), ReadOnly },
210         { LFUN_KMAP_SEC, "keymap-secondary",
211           N_("Use secondary keymap"), ReadOnly },
212         { LFUN_KMAP_TOGGLE, "keymap-toggle", N_("Toggle keymap"), ReadOnly },
213         { LFUN_INSERT_LABEL, "label-insert", N_("Insert Label"), Noop },
214         { LFUN_LATEX_LOG, "latex-view-log", N_("View LaTeX log"), ReadOnly },
215         { LFUN_LAYOUT, "layout", "", Noop },
216         { LFUN_LAYOUT_CHARACTER, "layout-character", "", Noop },
217         { LFUN_LAYOUT_COPY, "layout-copy",
218           N_("Copy paragraph environment type"), Noop },
219         { LFUN_LAYOUT_DOCUMENT, "layout-document", "", ReadOnly },
220         { LFUN_LAYOUTNO, "layout-number", "", Noop }, // internal only
221         { LFUN_LAYOUT_PARAGRAPH, "layout-paragraph", "", ReadOnly },
222         { LFUN_LAYOUT_PAPER, "layout-paper", "", ReadOnly },
223         { LFUN_LAYOUT_PASTE, "layout-paste",
224           N_("Paste paragraph environment type"), Noop },
225         { LFUN_LAYOUT_PREAMBLE, "layout-preamble", "", ReadOnly },
226         { LFUN_LAYOUT_QUOTES, "layout-quotes", "", ReadOnly },
227         { LFUN_LAYOUT_SAVE_DEFAULT, "layout-save-default", "", ReadOnly },
228         { LFUN_LAYOUT_TABLE, "layout-table", "", Noop },
229         { LFUN_HOME, "line-begin",
230           N_("Go to beginning of line"), ReadOnly },
231         { LFUN_HOMESEL, "line-begin-select",
232           N_("Select to beginning of line"), ReadOnly },
233         { LFUN_DELETE_LINE_FORWARD, "line-delete-forward", "", Noop },
234         { LFUN_END, "line-end", N_("Go to end of line"), ReadOnly },
235         { LFUN_ENDSEL, "line-end-select",
236           N_("Select to end of line"), ReadOnly },
237         { LFUN_LOA_INSERT, "loa-insert",
238           N_("Insert list of algorithms"), Noop },
239         { LFUN_LOF_INSERT, "lof-insert",
240           N_("Insert list of figures"), Noop },
241         { LFUN_LOT_INSERT, "lot-insert",
242           N_("Insert list of tables"), Noop },
243         { LFUN_QUIT, "lyx-quit", N_("Exit"), NoBuffer },
244         { LFUN_MARGINMELT, "marginpar-insert",
245           N_("Insert Margin note"), Noop },
246         { LFUN_MARK_OFF, "mark-off", "", ReadOnly },
247         { LFUN_MARK_ON, "mark-on", "", ReadOnly },
248         { LFUN_SETMARK, "mark-toggle", "", ReadOnly },
249         { LFUN_MATH_DELIM, "math-delim", "", Noop },
250         { LFUN_MATH_DISPLAY, "math-display", "", Noop },
251         { LFUN_GREEK, "math-greek", N_("Math Greek"), Noop },
252         { LFUN_GREEK_TOGGLE, "math-greek-toggle", "", Noop },
253         { LFUN_INSERT_MATH, "math-insert",
254           N_("Insert math symbol"), Noop },
255         { LFUN_MATH_LIMITS, "math-limits", "", Noop },
256         { LFUN_MATH_MACRO, "math-macro", "", Noop },
257         { LFUN_MATH_MACROARG, "math-macro-arg", "", Noop },
258         { LFUN_INSERT_MATRIX, "math-matrix", "", Noop },
259         { LFUN_MATH_MODE, "math-mode", N_("Math mode"), Noop },
260         { LFUN_MATH_NONUMBER, "math-nonumber", "", Noop },
261         { LFUN_MATH_NUMBER, "math-number", "", Noop },
262         { LFUN_MATH_SIZE, "math-size", "", Noop },
263         { LFUN_MELT, "melt", N_("Melt"), Noop },
264         { LFUN_MENU_OPEN_BY_NAME, "menu-open", "", NoBuffer },
265         { LFUN_MENU_SEPARATOR, "menu-separator-insert", "", Noop },
266         { LFUN_META_FAKE, "meta-prefix", "", NoBuffer },
267         { LFUN_INSERT_NOTE, "note-insert", "", Noop },
268         { LFUN_GOTONOTE, "note-next", "", ReadOnly },
269         { LFUN_OPENSTUFF, "open-stuff", "", ReadOnly },
270         { LFUN_DOWN_PARAGRAPH, "paragraph-down",
271           N_("Go one paragraph down"), ReadOnly },
272         { LFUN_DOWN_PARAGRAPHSEL, "paragraph-down-select",
273           N_("Select next paragraph"), ReadOnly },
274         { LFUN_UP_PARAGRAPH, "paragraph-up",
275           N_("Go one paragraph up"), ReadOnly },
276         { LFUN_UP_PARAGRAPHSEL, "paragraph-up-select",
277           N_("Select previous paragraph"), ReadOnly },
278         { LFUN_PARENTINSERT, "parent-insert", "", Noop },
279         { LFUN_PASTE, "paste", N_("Paste") , Noop },
280         { LFUN_SAVEPREFERENCES, "preferences-save",
281           "Save Preferences", NoBuffer },
282         { LFUN_PASTESELECTION, "primary-selection-paste", "", Noop },
283         { LFUN_PROTECTEDSPACE, "protected-space-insert",
284           N_("Insert protected space"), Noop },
285         { LFUN_QUOTE, "quote-insert", N_("Insert quote"), Noop },
286         { LFUN_RECONFIGURE, "reconfigure",
287           N_("Reconfigure"), NoBuffer },
288         { LFUN_REDO, "redo", N_("Redo"), Noop },
289         { LFUN_REFBACK, "reference-back", "", ReadOnly },
290         { LFUN_REFGOTO, "reference-goto", "", ReadOnly },
291         { LFUN_INSERT_REF, "reference-insert",
292           N_("Insert cross reference"), Noop },
293         { LFUN_REFTOGGLE, "reference-toggle", "", Noop },
294         { LFUN_NEXT, "screen-down", "", ReadOnly },
295         { LFUN_NEXTSEL, "screen-down-select", "", ReadOnly },
296         { LFUN_CENTER, "screen-recenter", "", ReadOnly },
297         { LFUN_PRIOR, "screen-up", "", ReadOnly },
298         { LFUN_PRIORSEL, "screen-up-select", "", ReadOnly },
299         { LFUN_SELFINSERT, "self-insert", "", Noop },
300         { LFUN_CHARATCURSOR, "server-char-after", "", ReadOnly },
301         { LFUN_GETFONT, "server-get-font", "", ReadOnly },
302         { LFUN_GETLATEX, "server-get-latex", "", ReadOnly },
303         { LFUN_GETLAYOUT, "server-get-layout", "", ReadOnly },
304         { LFUN_GETNAME, "server-get-name", "", ReadOnly },
305         { LFUN_GETTIP, "server-get-tip", "", ReadOnly },
306         { LFUN_GETXY, "server-get-xy", "", ReadOnly },
307         { LFUN_GOTOFILEROW, "server-goto-file-row", "", Noop },
308         { LFUN_NOTIFY, "server-notify", "", ReadOnly },
309         { LFUN_SETXY, "server-set-xy", "", ReadOnly },
310         { LFUN_SPELLCHECK, "spellchecker", "", Noop },
311         { LFUN_INSERT_MATH, "symbol-insert", "", Noop },
312         { LFUN_TAB, "tab-forward", "", Noop },
313         { LFUN_TABINSERT, "tab-insert", "", Noop },
314         { LFUN_TABLE, "table-insert", N_("Insert Table"), Noop },
315         { LFUN_TEX, "tex-mode", N_("Toggle TeX style"), Noop },
316         { LFUN_TOC_INSERT, "toc-insert",
317           N_("Insert table of contents"), Noop },
318         { LFUN_TOCVIEW, "toc-view",
319           N_("View table of contents"), ReadOnly },
320         { LFUN_TOGGLECURSORFOLLOW, "toggle-cursor-follows-scrollbar",
321           N_("Toggle cursor does/doesn't follow the scrollbar"),
322           ReadOnly },
323         { LFUN_ADD_TO_TOOLBAR, "toolbar-add-to", "", NoBuffer },
324         { LFUN_PUSH_TOOLBAR, "toolbar-push", "", NoBuffer },
325         { LFUN_UNDO, "undo", N_("Undo"), Noop },
326         { LFUN_UP, "up", "", ReadOnly },
327         { LFUN_UPSEL, "up-select", "", ReadOnly },
328         { LFUN_URL, "url-insert", "", Noop },
329         { LFUN_VC_CHECKIN, "vc-check-in", "", ReadOnly },
330         { LFUN_VC_CHECKOUT, "vc-check-out", "", ReadOnly },
331         { LFUN_VC_HISTORY, "vc-history", "", ReadOnly },
332         { LFUN_VC_REGISTER, "vc-register",
333           N_("Register document under version control"), ReadOnly },
334         { LFUN_VC_REVERT, "vc-revert", "", ReadOnly },
335         { LFUN_VC_UNDO, "vc-undo-last", "", ReadOnly },
336         { LFUN_WORDLEFT, "word-backward", "", ReadOnly },
337         { LFUN_WORDLEFTSEL, "word-backward-select", "", ReadOnly },
338         { LFUN_CAPITALIZE_WORD, "word-capitalize", "", Noop },
339         { LFUN_DELETE_WORD_BACKWARD, "word-delete-backward",
340           "", Noop },
341         { LFUN_DELETE_WORD_FORWARD, "word-delete-forward", "", Noop },
342         { LFUN_WORDFINDBACKWARD, "word-find-backward", "", ReadOnly },
343         { LFUN_WORDFINDFORWARD, "word-find-forward", "", ReadOnly },
344         { LFUN_WORDRIGHT, "word-forward", "", ReadOnly },
345         { LFUN_WORDRIGHTSEL, "word-forward-select", "", ReadOnly },
346         { LFUN_LOWCASE_WORD, "word-lowcase", "", Noop },
347         { LFUN_UPCASE_WORD, "word-upcase", "", Noop },
348         { LFUN_NOACTION, "", "", Noop }
349         };
350
351         int i = 0;
352         while (items[i].action != LFUN_NOACTION) {
353                 newFunc(items[i].action,
354                         items[i].name,
355                         _(items[i].helpText),
356                         items[i].attrib);
357                 ++i;
358         }
359
360         init = true;
361 }
362
363
364 LyXAction::LyXAction()
365 {
366         init();
367 }
368
369
370 // Search for an existent pseudoaction, return LFUN_UNKNOWN_ACTION
371 // if it doesn't exist.
372 int LyXAction::searchActionArg(kb_action action, string const & arg) const
373 {
374         arg_map::const_iterator pit = lyx_arg_map.find(action);
375
376         if (pit == lyx_arg_map.end()) {
377                 // the action does not have any pseudoactions
378                 lyxerr[Debug::ACTION] << "Action " << action
379                                     << " does not have any pseudo actions.\n";
380                 return LFUN_UNKNOWN_ACTION;
381         }
382         
383         arg_item::const_iterator aci = (*pit).second.find(arg);
384
385         if (aci == (*pit).second.end()) {
386                 // the action does not have any pseudoactions with this arg
387                 lyxerr[Debug::ACTION] 
388                         << "Action " << action
389                         << "does not have any pseudoactions with arg "
390                         << arg << '\n';
391                 return LFUN_UNKNOWN_ACTION;
392         }
393
394         // pseudo action exist
395         lyxerr[Debug::ACTION] << "Pseudoaction exist[" 
396                             << action << '|' 
397                             << arg << "] = " << (*aci).second << '\n';
398
399         return (*aci).second;
400 }
401
402
403 // Returns a pseudo-action given an action and its argument.
404 int LyXAction::getPseudoAction(kb_action action, string const & arg) const
405 {
406         int psdaction = searchActionArg(action, arg);
407
408         if (isPseudoAction(psdaction)) return psdaction;
409
410         static unsigned int pseudo_counter = LFUN_LASTACTION;
411
412         // Create new pseudo action.
413         pseudo_func tmp_p;
414         tmp_p.action = action;
415         tmp_p.arg = arg;
416         lyx_pseudo_map[++pseudo_counter] = tmp_p;
417
418         // First ensure that the action is in lyx_arg_map;
419         lyx_arg_map[action];
420         // get the arg_item map
421         arg_map::iterator ami = lyx_arg_map.find(action);
422         // put the new pseudo function in it
423         (*ami).second[arg] = pseudo_counter;
424
425         lyxerr[Debug::ACTION] << "Creating new pseudoaction "
426                             << pseudo_counter << " for [" << action
427                             << '|' << arg << "]\n";
428         
429         return pseudo_counter;
430 }
431
432
433 // Retrieves the real action and its argument.
434 // perhaps a pair<kb_action, string> should be returned?
435 kb_action LyXAction::retrieveActionArg(int pseudo, string & arg) const
436 {
437         pseudo_map::const_iterator pit = lyx_pseudo_map.find(pseudo);
438
439         if (pit != lyx_pseudo_map.end()) {
440                 lyxerr[Debug::ACTION] << "Found the pseudoaction: ["
441                                     << (*pit).second.action << '|'
442                                     << (*pit).second.arg << '\n';
443                 arg = (*pit).second.arg;
444                 return (*pit).second.action;
445         } else {
446                 lyxerr << "Lyx Error: Unrecognized pseudo-action\n";
447                 return LFUN_UNKNOWN_ACTION;
448         }
449         
450 }
451
452
453 // Returns an action tag from a string.
454 int LyXAction::LookupFunc(string const & func) const
455 {
456         if (func.empty()) return LFUN_NOACTION;
457
458         // split action and arg
459         string actstr;
460         string argstr = split(func, actstr, ' ');
461         lyxerr[Debug::ACTION] << "Action: " << actstr << '\n';
462         lyxerr[Debug::ACTION] << "Arg   : " << argstr << '\n';
463
464         func_map::const_iterator fit = lyx_func_map.find(actstr);
465
466         if (!argstr.empty() && fit != lyx_func_map.end()) {
467                 // might be pseudo (or create one)
468                 return getPseudoAction((*fit).second, argstr);
469         }
470
471         return fit != lyx_func_map.end() ? (*fit).second : LFUN_UNKNOWN_ACTION;
472 }
473
474
475 #ifdef WITH_WARNINGS
476 #warning Not working as it should.
477 #endif
478 int LyXAction::getApproxFunc(string const & func) const
479         // This func should perhaps also be able to return a list of all
480         // actions that has func as a prefix. That should actually be quite
481         // easy, just let it return a vector<int> or something.
482 {
483         int action = LookupFunc(func);
484         if (action == LFUN_UNKNOWN_ACTION) {
485                 // func is not an action, but perhaps it is
486                 // part of one...check if it is prefix if one of the
487                 // actions.
488                 // Checking for prefix is not so simple, but
489                 // using a simple bounding function gives
490                 // a similar result.  [ale 19981103]
491                 func_map::const_iterator fit = 
492                         lyx_func_map.lower_bound(func);
493                 
494                 if (fit != lyx_func_map.end()) {
495                         action =  (*fit).second;
496                 }
497         } else {  // Go get the next function
498                 func_map::const_iterator fit = 
499                         lyx_func_map.upper_bound(func);
500                 
501                 if (fit != lyx_func_map.end()) {
502                         action =  (*fit).second;
503                 }
504         }
505         
506         return action;
507 }
508
509
510 string LyXAction::getApproxFuncName(string const & func) const
511 {
512     int f = getApproxFunc(func);
513     // This will return empty string if f isn't an action.
514     return getActionName(f);
515 }
516
517
518 string LyXAction::getActionName(int action) const
519 {
520     info_map::const_iterator iit = lyx_info_map.find(static_cast<kb_action>(action));
521
522     return iit != lyx_info_map.end() ? (*iit).second.name : string();
523 }
524
525
526 // Returns one line help associated with a (pseudo)action, i.e. appends
527 // the argument of the action if necessary
528 string LyXAction::helpText(int pseudoaction) const
529 {
530         string help, arg;
531         kb_action action;
532
533         if (isPseudoAction(pseudoaction)) 
534                 action = retrieveActionArg(pseudoaction, arg);
535         else 
536                 action = static_cast<kb_action>(pseudoaction);
537
538         info_map::const_iterator ici = lyx_info_map.find(action);
539         if (ici != lyx_info_map.end()) {
540                 if (lyxerr.debugging(Debug::ACTION)) {
541                         lyxerr << "Action: " << action << '\n';
542                         lyxerr << "   name: "
543                                << (*ici).second.name << '\n';
544                         lyxerr << " attrib: "
545                                << (*ici).second.attrib << '\n';
546                         lyxerr << "   help: "
547                                << (*ici).second.helpText << '\n';
548                 }
549                 help = (*ici).second.helpText;
550                 // if the is no help text use the name of the func instead.
551                 if (help.empty()) help = (*ici).second.name;
552         }
553
554         if (help.empty()) {
555                 help = _("No description available!");
556         } else if (!arg.empty()) {
557                 help += ' ';
558                 help += arg;
559         }
560
561         return help;
562 }
563
564
565 bool LyXAction::funcHasFlag(kb_action action, 
566                             LyXAction::func_attrib flag) const 
567 {
568     info_map::const_iterator ici = lyx_info_map.find(action);
569
570     if (ici != lyx_info_map.end()) {
571             return (*ici).second.attrib & flag;
572     } else {
573             // it really should exist, but...
574             lyxerr << "No info about kb_action: " << action << '\n';
575             return false;
576     }
577
578 }
579
580
581 ostream & operator<<(ostream & o, kb_action action)
582 {
583         return o << int(action);
584 }