]> git.lyx.org Git - lyx.git/blob - src/LyXAction.C
white-space changes, removed definitions.h several enum changes because of this,...
[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 #include <cstdlib>
14 #include <cstdio>
15 #include <cctype>
16 #include <cstring>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "LyXAction.h"
23 #include "debug.h"
24 #include "gettext.h"
25 #include "support/lstrings.h"
26
27  /*  
28      NAMING RULES FOR USER-COMMANDS
29      Here's the set of rules to apply when a new command name is introduced:
30  
31      1) Use the object.event order. That is, use `word-forward'
32         instead of `forward-word'.
33      2) Don't introduce an alias for an already named object. Same for events.
34      3) Forward movement or focus is called `forward' (not `right').
35      4) Backward movement or focus is called `backward' (not `left').
36      5) Upward movement of focus is called `up'.
37      6) Downward movement is called `down'.
38      7) The begin of an object is called `begin' (not `start').
39      8) The end of an object is called `end'.
40  
41      (May 19 1996, 12:04, RvdK)
42  */
43  
44
45 // These are globals. 
46 LyXAction lyxaction;
47
48
49 /* This table is sorted alphabetically [asierra 14Jan96] */
50 /* This table MUST be sorted alphabetically, incidentally! */ 
51 kb_func_table const lyx_func_table[] = {
52         { "accent-acute",                         LFUN_ACUTE },
53         { "accent-breve",                         LFUN_BREVE },
54         { "accent-caron",                         LFUN_CARON },
55         { "accent-cedilla",                       LFUN_CEDILLA },
56         { "accent-circle",                        LFUN_CIRCLE },
57         { "accent-circumflex",                    LFUN_CIRCUMFLEX },
58         { "accent-dot",                           LFUN_DOT },
59         { "accent-grave",                         LFUN_GRAVE },
60         { "accent-hungarian-umlaut",              LFUN_HUNG_UMLAUT },
61         { "accent-macron",                        LFUN_MACRON },
62         { "accent-ogonek",                        LFUN_OGONEK },
63         { "accent-special-caron",                 LFUN_SPECIAL_CARON },
64         { "accent-tie",                           LFUN_TIE },
65         { "accent-tilde",                         LFUN_TILDE },
66         { "accent-umlaut",                        LFUN_UMLAUT },
67         { "accent-underbar",                      LFUN_UNDERBAR },
68         { "accent-underdot",                      LFUN_UNDERDOT },
69         { "accent-vector",                        LFUN_VECTOR }, 
70         { "appendix",                         LFUN_APPENDIX }, 
71         { "apropos",                              LFUN_APROPOS },
72         { "backward-select",                      LFUN_LEFTSEL },
73         { "bibtex-database-add",                  LFUN_BIBDB_ADD },
74         { "bibtex-database-del",                  LFUN_BIBDB_DEL },
75         { "bibtex-insert",                        LFUN_INSERT_BIBTEX },
76         { "bibtex-style",                         LFUN_BIBTEX_STYLE },
77         { "break-line",                           LFUN_BREAKLINE },
78         { "break-paragraph",                      LFUN_BREAKPARAGRAPH },
79         { "break-paragraph-keep-layout",          LFUN_BREAKPARAGRAPHKEEPLAYOUT },
80         { "break-paragraph-skip",                 LFUN_BREAKPARAGRAPH_SKIP },
81         { "buffer-auto-save",                     LFUN_AUTOSAVE },
82         { "buffer-begin",                         LFUN_BEGINNINGBUF },
83         { "buffer-begin-select",                  LFUN_BEGINNINGBUFSEL },
84         { "buffer-child-insert",                  LFUN_CHILDINSERT },  // ale970521
85         { "buffer-child-open",                    LFUN_CHILDOPEN },  // ale970528
86         { "buffer-chktex",                        LFUN_RUNCHKTEX }, // Asger 971030
87         { "buffer-close",                         LFUN_CLOSEBUFFER },         
88         { "buffer-end",                           LFUN_ENDBUF },
89         { "buffer-end-select",                    LFUN_ENDBUFSEL },
90         { "buffer-export",                        LFUN_EXPORT },
91         { "buffer-fax",                           LFUN_FAX },
92         { "buffer-float-insert",                  LFUN_INSERTFOOTNOTE },
93         { "buffer-import",                        LFUN_IMPORT },
94         { "buffer-itemize-bullets-select",        LFUN_BUFFERBULLETSSELECT },
95         { "buffer-new",                           LFUN_MENUNEW },     
96         { "buffer-new-template",                  LFUN_MENUNEWTMPLT },
97         { "buffer-open",                          LFUN_MENUOPEN },    
98         { "buffer-previous",                      LFUN_PREVBUFFER },  
99         { "buffer-print",                         LFUN_MENUPRINT },   
100         { "buffer-reload",                        LFUN_MENURELOAD },   
101         { "buffer-toggle-read-only",              LFUN_READ_ONLY_TOGGLE },
102         { "buffer-typeset",                       LFUN_RUNLATEX },        // Alejandro's proposal
103         { "buffer-typeset-ps",                    LFUN_RUNDVIPS },
104         { "buffer-view",                          LFUN_PREVIEW },
105         { "buffer-view-ps",                       LFUN_PREVIEWPS },
106         { "buffer-write",                         LFUN_MENUWRITE },   
107         { "buffer-write-as",                      LFUN_MENUWRITEAS }, 
108         { "build-program",                        LFUN_BUILDPROG },
109         { "cancel",                               LFUN_CANCEL },
110         { "char-backward",                        LFUN_LEFT },
111         { "char-forward",                         LFUN_RIGHT },
112         { "citation-insert",                      LFUN_INSERT_CITATION },
113         { "command-execute",                      LFUN_EXEC_COMMAND },
114         { "command-prefix",                       LFUN_PREFIX },
115         { "copy",                                 LFUN_COPY },
116         { "cut",                                  LFUN_CUT },
117         { "delete-backward",                      LFUN_BACKSPACE },
118         { "delete-backward-skip",                 LFUN_BACKSPACE_SKIP },
119         { "delete-forward",                       LFUN_DELETE },
120         { "delete-forward-skip",                  LFUN_DELETE_SKIP },
121         { "depth-decrement",                      LFUN_DEPTH_MIN },
122         { "depth-increment",                      LFUN_DEPTH_PLUS },  
123         { "depth-next",                           LFUN_DEPTH },
124         { "dots-insert",                          LFUN_LDOTS },
125         { "down",                                 LFUN_DOWN },                 
126         { "down-select",                          LFUN_DOWNSEL },
127         { "drop-layouts-choice",                  LFUN_DROP_LAYOUTS_CHOICE },
128         { "end-of-sentence-period-insert",        LFUN_END_OF_SENTENCE },
129         { "error-next",                           LFUN_GOTOERROR },   
130         { "error-remove-all",                     LFUN_REMOVEERRORS },
131         { "figure-insert",                        LFUN_FIGURE },
132         { "file-insert",                          LFUN_FILE_INSERT },
133         { "file-insert-ascii",                    LFUN_FILE_INSERT_ASCII },
134         { "file-new",                             LFUN_FILE_NEW },
135         { "file-open",                            LFUN_FILE_OPEN },
136         { "find-replace",                         LFUN_MENUSEARCH },  
137         { "font-bold",                            LFUN_BOLD },        
138         { "font-code",                            LFUN_CODE },        
139         { "font-default",                         LFUN_DEFAULT },
140         { "font-emph",                            LFUN_EMPH },
141         { "font-free",                            LFUN_FREE },
142         { "font-noun",                            LFUN_NOUN },
143         { "font-roman",                           LFUN_ROMAN },
144         { "font-sans",                            LFUN_SANS },
145         { "font-size",                            LFUN_FONT_SIZE },
146         { "font-state",                           LFUN_FONT_STATE }, 
147         { "font-underline",                       LFUN_UNDERLINE },   
148         { "footnote-insert",                      LFUN_FOOTMELT },    
149         { "forward-select",                       LFUN_RIGHTSEL },
150         { "hfill-insert",                         LFUN_HFILL },
151         { "html-insert",                          LFUN_HTMLURL },
152         { "hyphenation-point-insert",             LFUN_HYPHENATION },
153         { "index-insert",                         LFUN_INDEX_INSERT },
154         { "index-insert-last",                    LFUN_INDEX_INSERT_LAST },
155         { "index-print",                          LFUN_INDEX_PRINT },
156 #if 0
157         { "inset-latex-insert",                   LFUN_INSERT_INSET_LATEX },
158 #endif
159         { "keymap-off",                           LFUN_KMAP_OFF },
160         { "keymap-primary",                       LFUN_KMAP_PRIM },
161         { "keymap-secondary",                     LFUN_KMAP_SEC },
162         { "keymap-toggle",                        LFUN_KMAP_TOGGLE },
163         { "label-insert",                         LFUN_INSERT_LABEL },
164         { "latex-view-log",                       LFUN_LATEX_LOG },
165         { "layout",                               LFUN_LAYOUT },
166         { "layout-character",                     LFUN_LAYOUT_CHARACTER },
167         { "layout-copy",                          LFUN_LAYOUT_COPY },
168         { "layout-document",                      LFUN_LAYOUT_DOCUMENT },
169         //{ "layout-number",                      LFUN_LAYOUTNO }, // internal only
170         { "layout-paper",                         LFUN_LAYOUT_PAPER },
171         { "layout-paragraph",                     LFUN_LAYOUT_PARAGRAPH },
172         { "layout-paste",                         LFUN_LAYOUT_PASTE },
173         { "layout-preamble",                      LFUN_LAYOUT_PREAMBLE },
174         { "layout-quotes",                        LFUN_LAYOUT_QUOTES },
175         { "layout-save-default",                  LFUN_LAYOUT_SAVE_DEFAULT },
176         { "layout-table",                         LFUN_LAYOUT_TABLE },
177         { "line-begin",                           LFUN_HOME },        
178         { "line-begin-select",                    LFUN_HOMESEL },     
179         { "line-delete-forward",                  LFUN_DELETE_LINE_FORWARD },  
180         { "line-end",                             LFUN_END },         
181         { "line-end-select",                      LFUN_ENDSEL },
182         { "loa-insert",                           LFUN_LOA_INSERT },
183         { "lof-insert",                           LFUN_LOF_INSERT },
184         { "lot-insert",                           LFUN_LOT_INSERT },
185         { "lyx-quit",                             LFUN_QUIT },
186         { "marginpar-insert",                     LFUN_MARGINMELT },  
187         { "mark-off",                             LFUN_MARK_OFF },    
188         { "mark-on",                              LFUN_MARK_ON },     
189         { "mark-toggle",                          LFUN_SETMARK }, 
190         { "math-delim",                           LFUN_MATH_DELIM },
191         { "math-display",                         LFUN_MATH_DISPLAY }, // Alejandro's proposal
192         { "math-greek",                           LFUN_GREEK },   
193         { "math-greek-toggle",                    LFUN_GREEK_TOGGLE },   
194         { "math-insert",                          LFUN_INSERT_MATH },   
195         { "math-limits",                          LFUN_MATH_LIMITS },
196         { "math-macro",                           LFUN_MATH_MACRO },
197         { "math-macro-arg",                       LFUN_MATH_MACROARG },
198         { "math-matrix",                          LFUN_INSERT_MATRIX }, 
199         { "math-mode",                            LFUN_MATH_MODE },
200         { "math-nonumber",                        LFUN_MATH_NONUMBER },
201         { "math-number",                          LFUN_MATH_NUMBER }, 
202         { "math-size",                            LFUN_MATH_SIZE }, 
203         { "melt",                                 LFUN_MELT },            // Needs better name. What about "float-disolve" or "float-extract" (Lgb)
204         { "menu-open",                            LFUN_MENU_OPEN_BY_NAME },
205         { "menu-separator-insert",                LFUN_MENU_SEPARATOR },
206         { "meta-prefix",                          LFUN_META_FAKE },
207         { "note-insert",                          LFUN_INSERT_NOTE },
208         { "note-next",                            LFUN_GOTONOTE },
209         { "open-stuff",                           LFUN_OPENSTUFF },       // Needs better name.
210         { "paragraph-down",                       LFUN_DOWN_PARAGRAPH },
211         { "paragraph-down-select",                LFUN_DOWN_PARAGRAPHSEL },
212         { "paragraph-up",                         LFUN_UP_PARAGRAPH },
213         { "paragraph-up-select",                  LFUN_UP_PARAGRAPHSEL },
214         { "parent-insert",                        LFUN_PARENTINSERT },
215         { "paste",                                LFUN_PASTE },
216         { "primary-selection-paste",              LFUN_PASTESELECTION },
217         { "protected-space-insert",               LFUN_PROTECTEDSPACE },
218         { "quote-insert",                         LFUN_QUOTE },
219         { "reconfigure",                          LFUN_RECONFIGURE },
220         { "redo",                                 LFUN_REDO },
221         { "reference-back",                       LFUN_REFBACK },
222         { "reference-goto",                       LFUN_REFGOTO }, 
223         { "reference-insert",                     LFUN_INSERT_REF },
224         { "reference-toggle",                     LFUN_REFTOGGLE },
225         { "screen-down",                          LFUN_NEXT },        
226         { "screen-down-select",                   LFUN_NEXTSEL },     
227         { "screen-recenter",                      LFUN_CENTER },
228         { "screen-up",                            LFUN_PRIOR },       
229         { "screen-up-select",                     LFUN_PRIORSEL },    
230         { "self-insert",                          LFUN_SELFINSERT },
231         { "server-char-after",                    LFUN_CHARATCURSOR },
232         { "server-get-font",                      LFUN_GETFONT },
233         { "server-get-latex",                     LFUN_GETLATEX },
234         { "server-get-layout",                    LFUN_GETLAYOUT },
235         { "server-get-name",                      LFUN_GETNAME },
236         { "server-get-tip",                       LFUN_GETTIP },
237         { "server-get-xy",                        LFUN_GETXY },
238         { "server-goto-file-row",                 LFUN_GOTOFILEROW },
239         { "server-notify",                        LFUN_NOTIFY },
240         { "server-set-xy",                        LFUN_SETXY },
241         { "spellchecker",                         LFUN_SPELLCHECK },  
242         { "symbol-insert",                        LFUN_INSERT_MATH },
243         { "tab-forward",                          LFUN_TAB },
244         { "tab-insert",                           LFUN_TABINSERT },
245         { "table-insert",                         LFUN_TABLE },
246         { "tex-mode",                             LFUN_TEX },
247         { "toc-insert",                           LFUN_TOC_INSERT },
248         { "toc-view",                             LFUN_TOCVIEW },
249         { "toggle-cursor-follows-scrollbar",      LFUN_TOGGLECURSORFOLLOW },
250         { "toolbar-add-to",                       LFUN_ADD_TO_TOOLBAR },
251         { "toolbar-push",                         LFUN_PUSH_TOOLBAR },
252         { "undo",                                 LFUN_UNDO },
253         { "up",                                   LFUN_UP },          
254         { "up-select",                            LFUN_UPSEL },
255         { "url-insert",                           LFUN_URL },
256         { "vc-check-in",                          LFUN_VC_CHECKIN }, 
257         { "vc-check-out",                         LFUN_VC_CHECKOUT }, 
258         { "vc-history",                           LFUN_VC_HISTORY }, 
259         { "vc-register",                          LFUN_VC_REGISTER }, 
260         { "vc-revert",                            LFUN_VC_REVERT }, 
261         { "vc-undo-last",                         LFUN_VC_UNDO }, 
262         { "word-backward",                        LFUN_WORDLEFT },
263         { "word-backward-select",                 LFUN_WORDLEFTSEL },
264         { "word-capitalize",                      LFUN_CAPITALIZE_WORD },
265         { "word-delete-backward",                 LFUN_DELETE_WORD_BACKWARD },
266         { "word-delete-forward",                  LFUN_DELETE_WORD_FORWARD }, 
267         { "word-find-backward",                   LFUN_WORDFINDBACKWARD },
268         { "word-find-forward",                    LFUN_WORDFINDFORWARD },
269         { "word-forward",                         LFUN_WORDRIGHT },
270         { "word-forward-select",                  LFUN_WORDRIGHTSEL },
271         { "word-lowcase",                         LFUN_LOWCASE_WORD },
272         { "word-upcase",                          LFUN_UPCASE_WORD }
273 };
274
275
276 /* 
277    This table contains the actions that modify a buffer and therefore
278    are not allowed for RO files. Do you think we have too much tables?
279    Each single integer in this table replaces 5 lines of code in lyxfunc
280    that include at least 3 function calls (several integers in the 
281    code segment).
282  
283    The table could in the near future be expanded adding a second integer 
284    field with attributes using these tags:
285
286 enum FUNC_ATTRIB {
287     LFAT_NONE= 0,     // Nothing special
288     LFAT_ISRO= 1,     // Is readonly (does not modify a buffer)
289     LFAT_ISIA= 2,     // Is interactive (requires a GUI)
290     LFAT_REQARG= 4,   // Requires argument
291     LFAT_ISMO= 8,     // Only math mode
292     LFAT_ETCETC
293 };
294
295 struct {
296     kb_action action;
297     unsigned  attrib; 
298 };
299
300 Alejandro-970604
301 */
302
303 kb_action func_attrib_table[] = {
304         LFUN_ACUTE,
305         LFUN_AUTOSAVE,
306         LFUN_BACKSPACE,
307         LFUN_BACKSPACE_SKIP,
308         LFUN_BIBDB_ADD,
309         LFUN_BIBDB_DEL,
310         LFUN_BIBTEX_STYLE,
311         LFUN_BOLD,
312         LFUN_BREAKLINE,
313         LFUN_BREAKPARAGRAPH,
314         LFUN_BREAKPARAGRAPHKEEPLAYOUT,
315         LFUN_BREAKPARAGRAPH_SKIP,
316         LFUN_BREVE,
317         LFUN_BUFFERBULLETSSELECT,
318         LFUN_CAPITALIZE_WORD,
319         LFUN_CARON,
320         LFUN_CEDILLA,
321         LFUN_CHILDINSERT,
322         LFUN_CIRCLE,
323         LFUN_CIRCUMFLEX,
324         LFUN_CODE,
325         LFUN_CUT,
326         LFUN_DEFAULT,
327         LFUN_DELETE,
328         LFUN_DELETE_LINE_FORWARD,
329         LFUN_DELETE_SKIP,
330         LFUN_DELETE_WORD_BACKWARD,
331         LFUN_DELETE_WORD_FORWARD,
332         LFUN_DEPTH,
333         LFUN_DEPTH_MIN,
334         LFUN_DEPTH_PLUS,
335         LFUN_DOT,
336         LFUN_EMPH,
337         LFUN_END_OF_SENTENCE,
338         LFUN_FIGURE,
339         LFUN_FILE_INSERT,
340         LFUN_FILE_INSERT_ASCII,
341         LFUN_FONT_SIZE,
342         LFUN_FOOTMELT,
343         LFUN_FREE,
344         LFUN_GRAVE,
345         LFUN_HFILL,
346         LFUN_HTMLURL,
347         LFUN_HUNG_UMLAUT,
348         LFUN_HYPHENATION,
349         LFUN_INDEX_INSERT,
350         LFUN_INDEX_INSERT_LAST,
351         LFUN_INDEX_PRINT,
352         LFUN_INSERTFOOTNOTE,
353         LFUN_INSERT_BIBTEX,
354         LFUN_INSERT_CITATION,
355 #if 0
356         LFUN_INSERT_INSET_LATEX,
357 #endif
358         LFUN_INSERT_LABEL,
359         LFUN_INSERT_MATH,
360         LFUN_INSERT_MATRIX,
361         LFUN_INSERT_NOTE,
362         LFUN_INSERT_REF,
363         LFUN_LAYOUT,
364         LFUN_LAYOUTNO,
365         LFUN_LAYOUT_CHARACTER,
366         LFUN_LAYOUT_PASTE,
367         LFUN_LAYOUT_QUOTES,
368         LFUN_LDOTS,
369         LFUN_LOA_INSERT,
370         LFUN_LOF_INSERT,
371         LFUN_LOT_INSERT,
372         LFUN_LOWCASE_WORD,
373         LFUN_MACRON,
374         LFUN_MARGINMELT,
375         LFUN_MATH_DELIM,
376         LFUN_MATH_DISPLAY,
377         LFUN_MATH_MACRO,
378         LFUN_MATH_MACROARG,
379         LFUN_MATH_MODE,
380         LFUN_MATH_NONUMBER,
381         LFUN_MATH_NUMBER,
382         LFUN_MATH_SIZE,
383         LFUN_MELT,
384         LFUN_MENU_SEPARATOR,
385         LFUN_NOUN,
386         LFUN_OGONEK,
387         LFUN_PARENTINSERT,
388         LFUN_PASTE,
389         LFUN_PASTESELECTION,
390         LFUN_PROTECTEDSPACE,
391         LFUN_QUOTE,
392         LFUN_REDO,
393         LFUN_REFTOGGLE,
394         LFUN_ROMAN,
395         LFUN_SANS,
396         LFUN_SELFINSERT,
397         LFUN_SPECIAL_CARON,
398         LFUN_TABINSERT,
399         LFUN_TABLE,
400         LFUN_TEX,
401         LFUN_TIE,
402         LFUN_TILDE,
403         LFUN_TOC_INSERT,
404         LFUN_UMLAUT,
405         LFUN_UNDERBAR,
406         LFUN_UNDERDOT,
407         LFUN_UNDERLINE,
408         LFUN_UNDO,
409         LFUN_UNKNOWN_ACTION,
410         LFUN_UPCASE_WORD,
411         LFUN_URL,
412         LFUN_VECTOR,
413         LFUN_WORDFINDFORWARD,
414         LFUN_WORDFINDBACKWARD
415 };
416
417 int LyXAction::psd_idx = 0;
418 kb_func_table const * LyXAction::lyx_func_table = &::lyx_func_table[0];
419 kb_func_table* LyXAction::lyx_func_args = 0;
420  
421 /* === code ============================================================== */
422
423 /* This routines allow binding actions with argument.
424  * Provisionally a fixed size array and global functions are used.
425  * [asierra 20Jan96]
426  */
427 #define MAX_PSEUDO_ACTION 128
428
429
430 LyXAction::LyXAction()
431 {
432         lyx_func_args= new kb_func_table[MAX_PSEUDO_ACTION];
433         funcCount = sizeof(::lyx_func_table) / sizeof(::kb_func_table);
434 }
435
436
437 LyXAction::~LyXAction()
438 {
439         if (lyx_func_args) {
440 // This is wrong, so I'll just disable it for now.
441 // The problem is that we might free memory twice in some situations...
442 // It's better to leak a bit that to crash. (Asger)
443 //              for (int i= 0; i < psd_idx; i++) {
444 //                      free(lyx_func_args[i].name);
445 //              }
446                 delete[] lyx_func_args;
447                 lyx_func_args = 0;
448         }
449 }
450
451
452 // Search for an existent pseudoaction, return -1 if it doesn't exist.
453 int  LyXAction::searchActionArg(kb_action action, char const *arg)
454 {
455         kb_func_table *tb = &lyx_func_args[0];
456         for (int i= 0; i<psd_idx; i++) {
457                 if (action == tb->action && !strcmp(tb->name, arg)) {    
458
459                         lyxerr[Debug::KEY] << "Pseudoaction already exist[" 
460                                            << action << '|' 
461                                            << arg << ']' << endl;
462
463                         return LFUN_LASTACTION+i;
464                 }
465                 tb++;
466         }
467         return -1;
468 }
469
470
471 // Returns a pseudo-action given an action and its argument.
472 int LyXAction::getPseudoAction(kb_action action, char const *arg)
473 {
474         // Check if the pseudo-action already exist.
475         int psdaction = searchActionArg(action, arg);
476    
477         if (psdaction >= 0) return psdaction;
478
479         if (psd_idx>= MAX_PSEUDO_ACTION) {
480                 lyxerr << "Lyx Error: No more pseudo-actions allowed"
481                        << "           Tell the developers." << endl;
482                 return -1;
483         }
484         lyx_func_args[psd_idx].name = strdup(arg);
485         lyx_func_args[psd_idx].action = action;
486         psd_idx++;
487         return LFUN_LASTACTION+psd_idx-1;
488 }
489
490
491 // Retrieves the real action and its argument.
492 int LyXAction::retrieveActionArg(int i, char const** arg)
493 {
494         i -= LFUN_LASTACTION;
495         if (i >= 0 &&  i <psd_idx) {
496                 *arg = lyx_func_args[i].name;
497                 return (int)lyx_func_args[i].action;
498         } else {
499                 lyxerr << "Lyx Error: Unrecognized pseudo-action" << endl;
500                 return -1;
501         }
502 }
503
504
505 // Returns an action tag from a string.
506 int LyXAction::LookupFunc(char const * func)
507 {
508         if (!func) 
509                 return LFUN_UNKNOWN_ACTION;         
510         if (func[0] == '\0')
511                 return LFUN_NOACTION;
512  
513    /* In the scan loop below, l can never become r, so set r one past the last
514       valid func table slot. RVDK_PATCH_5 */
515         int k, l= 0, r = funcCount;
516         int action = LFUN_UNKNOWN_ACTION;
517         char * arg = strchr(const_cast<char*>(func), ' '); // const cast to
518         // help a sun complier, will go away when this func is rewritten
519         // to use std::containers and std::string.
520
521         if (arg) *(arg++) = '\0';      
522
523         while (l < r) {
524                 last_action_idx = (l+r)/2;
525                 k = strcmp(lyx_func_table[last_action_idx].name, func);
526                 if (k == 0) {
527                         action = lyx_func_table[last_action_idx].action;
528                         break;
529                 } else
530                         if (k<0) l = last_action_idx+1; else r = last_action_idx;
531         }
532         if (arg && action >= 0) {
533                 action = getPseudoAction((kb_action)action, arg);
534                 lyxerr[Debug::KEY] << "Pseudo action_arg["
535                                    << action << '|' 
536                                    << arg << ']' << endl;
537         }
538         return action;
539 }
540
541
542 int LyXAction::getApproxFunc(char const *func)
543 {
544     int action = LookupFunc(func);
545     if (action<0) {
546         int k = strncmp(lyx_func_table[last_action_idx].name,
547                         func, strlen(func));
548         if (k<0 && last_action_idx<funcCount-1) 
549           last_action_idx++;
550         else if (k>0 && last_action_idx>0)
551           last_action_idx--;
552         
553         action = lyx_func_table[last_action_idx].action;
554     } else if (last_action_idx<funcCount-1)
555       last_action_idx++;
556     
557     return action;
558 }
559
560
561 char const *LyXAction::getApproxFuncName(char const *func)
562 {
563     getApproxFunc(func);
564     return lyx_func_table[last_action_idx].name;
565 }
566
567
568 char const *LyXAction::getActionName(int action) const
569 {
570         // why LFUN_LASTACTION -1? Because LFUN_LASTACTION is too
571         // large an because there is one internal command, hence
572         // lyx_func_table is 2 less then LFUN_LASTACTION (Lgb)
573     for (int i= 0; i < funcCount; i++) {
574         if (lyx_func_table[i].action == action)
575           return lyx_func_table[i].name;
576     }
577     
578     // Hmm.. let's see whether this is a pseudoaction
579     action -= LFUN_LASTACTION;
580     if (action>= 0 && action<psd_idx) {
581         return lyx_func_args[action].name;
582     }
583     
584     return "";
585 }
586
587
588 // Returns one line help associated with function
589 // Now the missing strings are replaced by the command names. (Alejandro)
590 char const *LyXAction::helpText(kb_action action) const
591 {
592         static bool is_sorted = false;
593         static kb_func_table helpTexts[LFUN_LASTACTION] = 
594         {
595                 { _("Describe command"), LFUN_APROPOS },
596                 { _("Select previous char"), LFUN_LEFTSEL },
597                 { _("Insert bibtex"), LFUN_INSERT_BIBTEX },
598                 { _("Autosave"), LFUN_AUTOSAVE },
599                 { _("Go to beginning of document"), LFUN_BEGINNINGBUF },
600                 { _("Select to beginning of document"), LFUN_BEGINNINGBUFSEL },
601                 { _("Close"), LFUN_CLOSEBUFFER },
602                 { _("Go to end of document"), LFUN_ENDBUF },
603                 { _("Select to end of document"), LFUN_ENDBUFSEL },
604                 { _("Fax"), LFUN_FAX },
605                 { _("New document"), LFUN_MENUNEW },
606                 { _("New document from template"), LFUN_MENUNEWTMPLT },
607                 { _("Open"), LFUN_MENUOPEN },
608                 { _("Switch to previous document"), LFUN_PREVBUFFER },
609                 { _("Print"), LFUN_MENUPRINT },
610                 { _("Revert to saved"), LFUN_MENURELOAD },
611                 { _("Update DVI"), LFUN_RUNLATEX },
612                 { _("Update PostScript"), LFUN_RUNDVIPS },
613                 { _("View DVI"), LFUN_PREVIEW },
614                 { _("View PostScript"), LFUN_PREVIEWPS },
615                 { _("Build program"), LFUN_BUILDPROG },
616                 { _("Check TeX"), LFUN_RUNCHKTEX },
617                 { _("Save"), LFUN_MENUWRITE },
618                 { _("Save As"), LFUN_MENUWRITEAS },
619                 { _("Cancel"), LFUN_CANCEL },
620                 { _("Go one char back"), LFUN_LEFT },
621                 { _("Go one char forward"), LFUN_RIGHT },
622                 { _("Insert citation"), LFUN_INSERT_CITATION },
623                 { _("Execute command"), LFUN_EXEC_COMMAND },
624                 { _("Copy"), LFUN_COPY },
625                 { _("Cut"), LFUN_CUT },
626                 { _("Decrement environment depth"), LFUN_DEPTH_MIN },
627                 { _("Increment environment depth"), LFUN_DEPTH_PLUS },
628                 { _("Change environment depth"), LFUN_DEPTH },
629                 { _("Change itemize bullet settings"), LFUN_BUFFERBULLETSSELECT },
630                 { _("Go down"), LFUN_DOWN },
631                 { _("Select next line"), LFUN_DOWNSEL },
632                 { _("Choose Paragraph Environment"), LFUN_DROP_LAYOUTS_CHOICE },
633                 { _("Go to next error"), LFUN_GOTOERROR },
634                 { _("Insert Figure"), LFUN_FIGURE },
635                 { _("Find & Replace"), LFUN_MENUSEARCH },
636                 { _("Toggle cursor does/doesn't follow the scrollbar"), LFUN_TOGGLECURSORFOLLOW },
637                 { _("Toggle bold"), LFUN_BOLD },
638                 { _("Toggle code style"), LFUN_CODE },
639                 { _("Default font style"), LFUN_DEFAULT },
640                 { _("Toggle emphasize"), LFUN_EMPH },
641                 { _("Toggle user defined style"), LFUN_FREE },
642                 { _("Toggle noun style"), LFUN_NOUN },
643                 { _("Toggle roman font style"), LFUN_ROMAN },
644                 { _("Toggle sans font style"), LFUN_SANS },
645                 { _("Set font size"), LFUN_FONT_SIZE },
646                 { _("Show font state"), LFUN_FONT_STATE },
647                 { _("Toggle font underline"), LFUN_UNDERLINE },
648                 { _("Insert Footnote"), LFUN_FOOTMELT },
649                 { _("Select next char"), LFUN_RIGHTSEL },
650                 { _("Insert horizontal fill"), LFUN_HFILL },
651                 { _("Insert hyphenation point"), LFUN_HYPHENATION },
652                 { _("Insert ... dots"), LFUN_LDOTS },
653                 { _("Insert end of sentence period"), LFUN_END_OF_SENTENCE },
654                 { _("Turn off keymap"), LFUN_KMAP_OFF },
655                 { _("Use primary keymap"), LFUN_KMAP_PRIM },
656                 { _("Use secondary keymap"), LFUN_KMAP_SEC },
657                 { _("Toggle keymap"), LFUN_KMAP_TOGGLE },
658                 { _("Insert Label"), LFUN_INSERT_LABEL },
659                 { _("Copy paragraph environment type"), LFUN_LAYOUT_COPY },
660                 { _("Paste paragraph environment type"), LFUN_LAYOUT_PASTE },
661                 { _("Specify paper size and margins"), LFUN_LAYOUT_PAPER },
662                 { _("Go to beginning of line"), LFUN_HOME },
663                 { _("Select to beginning of line"), LFUN_HOMESEL },
664                 { _("Go to end of line"), LFUN_END },
665                 { _("Select to end of line"), LFUN_ENDSEL },
666                 { _("Exit"), LFUN_QUIT },
667                 { _("Insert Margin note"), LFUN_MARGINMELT },
668                 { _("Math Greek"), LFUN_GREEK },   
669                 { _("Math mode"), LFUN_MATH_MODE },
670                 { _("Go one paragraph down"), LFUN_DOWN_PARAGRAPH },
671                 { _("Select next paragraph"), LFUN_DOWN_PARAGRAPHSEL },
672                 { _("Go one paragraph up"), LFUN_UP_PARAGRAPH },
673                 { _("Select previous paragraph"), LFUN_UP_PARAGRAPHSEL },
674                 { _("Paste"), LFUN_PASTE },
675                 { _("Insert protected space"), LFUN_PROTECTEDSPACE },
676                 { _("Insert quote"), LFUN_QUOTE },
677                 { _("Reconfigure"), LFUN_RECONFIGURE },
678                 { _("Redo"), LFUN_REDO },
679                 { _("Insert cross reference"), LFUN_INSERT_REF },
680                 { _("Insert Table"), LFUN_TABLE },
681                 { _("Toggle TeX style"), LFUN_TEX },
682                 { _("Undo"), LFUN_UNDO },
683                 { _("Melt"), LFUN_MELT },
684                 { _("Import document"), LFUN_IMPORT },
685                 { _("Remove all error boxes"), LFUN_REMOVEERRORS },
686                 { _("Insert menu separator"), LFUN_MENU_SEPARATOR }
687 //              { "reference-back", LFUN_REFBACK },
688 //              { "reference-goto", LFUN_REFGOTO }, 
689         };
690         
691         // Sort to make it faster
692         if (!is_sorted) {
693                 int i= 0; 
694                 while (i < LFUN_LASTACTION-1) {
695                     if (helpTexts[i].action == 0) {
696                         helpTexts[i].action = (kb_action)i;
697                         helpTexts[i].name = getActionName(i);
698                         i++;
699                     } else if (helpTexts[i].action != i) {
700                         int k = helpTexts[i].action;
701                         kb_func_table tmp = helpTexts[k];
702                         helpTexts[k] = helpTexts[i];
703                         helpTexts[i] = tmp;
704                         if (k < i) i++;
705                     } else {
706                         i++;
707                     }
708                 }
709                 is_sorted = true;
710         }
711
712         if (action <= 1 || action >= funcCount)
713                 goto no_desc;
714                 
715         if (helpTexts[action].action == action) {
716             return helpTexts[action].name;
717         } 
718         //        + string(' ') + int(is_ro) + string(']'));
719   no_desc:
720         // In an ideal world, this never happens:
721         return _("No description available!");
722 }
723
724
725 // Function to compare items from the attrib table.
726 extern "C" int actioncomp(const void *a, const void *b)
727 {
728     int const *ia= (int const*)a, *ib= (int const*)b;
729     return (*ia)-(*ib);
730 }
731
732  
733 bool LyXAction::isFuncRO(kb_action action) const
734 {
735     static bool is_sorted = false;    
736     static int fCount = sizeof(::func_attrib_table) / sizeof(kb_action);
737     
738     if (!is_sorted) {
739         qsort(func_attrib_table, fCount, sizeof(kb_action), actioncomp);
740         is_sorted = true;
741 //      for (int i= 0; i<fCount; i++) 
742 //        fprintf(stdout, "%d: %d\n", i, func_attrib_table[i]);
743     }
744
745     int m, k, l= 0, r = fCount;
746     int is_ro = false;
747
748     while (l < r) {
749         m = (l+r)/2;
750         k = func_attrib_table[m] - action;
751         if (k == 0) {
752             is_ro = true;
753             break;
754         } else
755           if (k<0) l = m+1; else r = m;
756     }
757     lyxerr.debug() << "RO[" << action
758                    << " " << is_ro << "]" << endl;
759     return is_ro;
760 }
761
762 ostream & operator<<(ostream & o, kb_action action)
763 {
764         return o << int(action);
765 }