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