]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
xforms ? what xforms ?
[lyx.git] / src / lyxfunc.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyxfunc.h"
18 #include "version.h"
19 #include "kbmap.h"
20 #include "lyxrow.h"
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "lyxserver.h"
24 #include "intl.h"
25 #include "lyx_main.h"
26 #include "lyx_cb.h"
27 #include "LyXAction.h"
28 #include "debug.h"
29 #include "lyxrc.h"
30 #include "lyxtext.h"
31 #include "gettext.h"
32 #include "Lsstream.h"
33 #include "trans_mgr.h"
34 #include "layout.h"
35 #include "bufferview_funcs.h"
36 #include "frontends/MiniBuffer.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/lyx_gui.h"
39 #include "vspace.h"
40 #include "FloatList.h"
41 #include "converter.h"
42 #include "exporter.h"
43 #include "importer.h"
44 #include "TextCache.h"
45 #include "lyxfind.h"
46 #include "undo_funcs.h"
47 #include "ParagraphParameters.h"
48
49 #include "insets/inseturl.h"
50 #include "insets/insetlatexaccent.h"
51 #include "insets/insettoc.h"
52 #include "insets/insetref.h"
53 #include "insets/insetparent.h"
54 #include "insets/insetindex.h"
55 #include "insets/insetinclude.h"
56 #include "insets/insetbib.h"
57 #include "insets/insetcite.h"
58 #include "insets/insettext.h"
59 #include "insets/insetert.h"
60 #include "insets/insetexternal.h"
61 #include "insets/insetgraphics.h"
62 #include "insets/insetfoot.h"
63 #include "insets/insetmarginal.h"
64 #include "insets/insetminipage.h"
65 #include "insets/insetfloat.h"
66 #if 0
67 #include "insets/insetlist.h"
68 #include "insets/insettheorem.h"
69 #endif
70 #include "insets/insettabular.h"
71 #include "insets/insetcaption.h"
72
73 #include "mathed/formulamacro.h"
74 #include "mathed/math_cursor.h"
75 #include "mathed/math_inset.h"
76
77 #include "frontends/FileDialog.h"
78 #include "frontends/Dialogs.h"
79 #include "frontends/Toolbar.h"
80 #include "frontends/Menubar.h"
81 #include "frontends/Alert.h"
82
83 #include "graphics/GraphicsCache.h"
84
85 #include "support/lyxalgo.h"
86 #include "support/LAssert.h"
87 #include "support/filetools.h"
88 #include "support/FileInfo.h"
89 #include "support/forkedcontr.h"
90 #include "support/lstrings.h"
91 #include "support/path.h"
92 #include "support/lyxfunctional.h"
93
94 #include <ctime>
95 #include <clocale>
96 #include <cstdlib>
97 #include <cctype>
98
99 #include <utility>
100 #include <algorithm>
101
102 using std::pair;
103 using std::make_pair;
104 using std::endl;
105 using std::find_if;
106 using std::vector;
107 using std::transform;
108 using std::back_inserter;
109
110 extern BufferList bufferlist;
111 extern LyXServer * lyxserver;
112 extern bool selection_possible;
113
114 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
115
116 extern void show_symbols_form(LyXFunc *);
117
118 extern LyXAction lyxaction;
119 // (alkis)
120 extern tex_accent_struct get_accent(kb_action action);
121
122 extern void ShowLatexLog();
123
124
125 /* === globals =========================================================== */
126
127
128 LyXFunc::LyXFunc(LyXView * o)
129         : owner(o),
130         keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
131         cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
132 {
133         meta_fake_bit = key_modifier::none;
134         lyx_dead_action = LFUN_NOACTION;
135         lyx_calling_dead_action = LFUN_NOACTION;
136 }
137
138
139 inline
140 LyXText * LyXFunc::TEXT(bool flag = true) const
141 {
142         if (flag)
143                 return owner->view()->text;
144         return owner->view()->getLyXText();
145 }
146
147
148 // I changed this func slightly. I commented out the ...FinishUndo(),
149 // this means that all places that used to have a moveCursorUpdate, now
150 // have a ...FinishUndo() as the preceeding statement. I have also added
151 // a moveCursorUpdate to some of the functions that updated the cursor, but
152 // that did not show its new position.
153 inline
154 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
155 {
156         if (selecting || TEXT(flag)->selection.mark()) {
157                 TEXT(flag)->setSelection(owner->view());
158                 if (TEXT(flag)->bv_owner)
159                     owner->view()->toggleToggle();
160         }
161         owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
162         owner->view()->showCursor();
163
164         /* ---> Everytime the cursor is moved, show the current font state. */
165         // should this too me moved out of this func?
166         owner->view()->setState();
167 }
168
169
170 void LyXFunc::handleKeyFunc(kb_action action)
171 {
172         char c = keyseq.getLastKeyEncoded();
173
174         if (keyseq.length() > 1) {
175                 c = 0;
176         }
177
178         owner->getIntl()->getTransManager()
179                 .deadkey(c, get_accent(action).accent, TEXT(false));
180         // Need to clear, in case the minibuffer calls these
181         // actions
182         keyseq.clear();
183         // copied verbatim from do_accent_char
184         owner->view()->update(TEXT(false),
185                BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
186         TEXT(false)->selection.cursor = TEXT(false)->cursor;
187 }
188
189
190 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
191                             key_modifier::state state)
192 {
193         string argument;
194
195         if (lyxerr.debugging(Debug::KEY)) {
196                 lyxerr << "KeySym is "
197                        << keysym->getSymbolName()
198                        << endl;
199         }
200         // Do nothing if we have nothing (JMarc)
201         if ( ! keysym->isOK() ) {
202                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
203                                    << endl;
204                 return;
205         }
206
207         if (keysym->isModifier()) {
208                 return;
209         }
210
211         // Do a one-deep top-level lookup for
212         // cancel and meta-fake keys. RVDK_PATCH_5
213         cancel_meta_seq.reset();
214
215         int action = cancel_meta_seq.addkey(keysym, state);
216         if (lyxerr.debugging(Debug::KEY)) {
217                 lyxerr << "action first set to [" << action << "]" << endl;
218         }
219
220         // When not cancel or meta-fake, do the normal lookup.
221         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
222         // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
223         if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
224                 // remove Caps Lock and Mod2 as a modifiers
225                 action = keyseq.addkey(keysym, (state | meta_fake_bit));
226                 if (lyxerr.debugging(Debug::KEY)) {
227                         lyxerr << "action now set to ["
228                                << action << "]" << endl;
229                 }
230         }
231         // Dont remove this unless you know what you are doing.
232         meta_fake_bit = key_modifier::none;
233
234         // can this happen now ?
235         if (action == LFUN_NOACTION) {
236                 action = LFUN_PREFIX;
237         }
238
239         if (lyxerr.debugging(Debug::KEY)) {
240                 lyxerr << "Key [action="
241                        << action << "]["
242                        << keyseq.print() << "]"
243                        << endl;
244         }
245
246         // already here we know if it any point in going further
247         // why not return already here if action == -1 and
248         // num_bytes == 0? (Lgb)
249
250         if (keyseq.length() > 1) {
251                 owner->message(keyseq.print());
252         }
253
254         if (action == LFUN_UNKNOWN_ACTION) {
255                 // It is unknown, but what if we remove all
256                 // the modifiers? (Lgb)
257                 action = keyseq.addkey(keysym, key_modifier::none);
258
259                 if (lyxerr.debugging(Debug::KEY)) {
260                         lyxerr << "Removing modifiers...\n"
261                                << "Action now set to ["
262                                << action << "]" << endl;
263                 }
264                 if (action == LFUN_UNKNOWN_ACTION) {
265                         owner->message(_("Unknown function."));
266                         return;
267                 }
268         }
269
270         if (action == LFUN_SELFINSERT) {
271                 // This is very X dependent.
272                 char c = keysym->getISOEncoded();
273                 string argument;
274
275                 if (c != 0)
276                         argument = c;
277
278                 dispatch(LFUN_SELFINSERT, argument);
279                 lyxerr[Debug::KEY] << "SelfInsert arg[`"
280                                    << argument << "']" << endl;
281         } else {
282                 verboseDispatch(action, false);
283         }
284 }
285
286
287 FuncStatus LyXFunc::getStatus(int ac) const
288 {
289         kb_action action;
290         string argument;
291         action = lyxaction.retrieveActionArg(ac, argument);
292         return getStatus(action, argument);
293 }
294
295
296 FuncStatus LyXFunc::getStatus(kb_action action,
297                               string const & argument) const
298 {
299         FuncStatus flag;
300         Buffer * buf = owner->buffer();
301
302         if (action == LFUN_NOACTION) {
303                 setStatusMessage(N_("Nothing to do"));
304                 return flag.disabled(true);
305         }
306
307         if (action == LFUN_UNKNOWN_ACTION) {
308                 setStatusMessage(N_("Unknown action"));
309                 return flag.unknown(true);
310         }
311
312         // the default error message if we disable the command
313         setStatusMessage(N_("Command disabled"));
314
315         // Check whether we need a buffer
316         if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
317                 // Yes we need a buffer, do we have one?
318                 if (buf) {
319                         // yes
320                         // Can we use a readonly buffer?
321                         if (buf->isReadonly() &&
322                             !lyxaction.funcHasFlag(action,
323                                                    LyXAction::ReadOnly)) {
324                                 // no
325                                 setStatusMessage(N_("Document is read-only"));
326                                 flag.disabled(true);
327                         }
328                 } else {
329                         // no
330                         setStatusMessage(N_("Command not allowed with"
331                                            "out any document open"));
332                         return flag.disabled(true);
333                 }
334         }
335
336         UpdatableInset * tli = owner->view()->theLockingInset();
337
338         // I would really like to avoid having this switch and rather try to
339         // encode this in the function itself.
340         bool disable = false;
341         switch (action) {
342         case LFUN_MENUPRINT:
343                 disable = !Exporter::IsExportable(buf, "dvi")
344                         || lyxrc.print_command == "none";
345                 break;
346         case LFUN_EXPORT:
347                 disable = argument == "fax" &&
348                         !Exporter::IsExportable(buf, argument);
349                 break;
350         case LFUN_UNDO:
351                 disable = buf->undostack.empty();
352                 break;
353         case LFUN_REDO:
354                 disable = buf->redostack.empty();
355                 break;
356         case LFUN_SPELLCHECK:
357                 disable = lyxrc.isp_command == "none";
358                 break;
359 #ifndef HAVE_LIBAIKSAURUS
360         case LFUN_THESAURUS_ENTRY:
361                 disable = true;
362                 break;
363 #endif
364         case LFUN_RUNCHKTEX:
365                 disable = lyxrc.chktex_command == "none";
366                 break;
367         case LFUN_BUILDPROG:
368                 disable = !Exporter::IsExportable(buf, "program");
369                 break;
370
371         case LFUN_LAYOUT_TABULAR:
372                 disable = !tli
373                         || (tli->lyxCode() != Inset::TABULAR_CODE
374                             && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
375                 break;
376
377         case LFUN_LAYOUT:
378         case LFUN_LAYOUT_PARAGRAPH: {
379                 Inset * inset = TEXT(false)->cursor.par()->inInset();
380                 disable = inset && inset->forceDefaultParagraphs(inset);
381                 break;
382         }
383
384         case LFUN_TABULAR_FEATURE:
385                 disable = true;
386                 if (tli) {
387                         FuncStatus ret;
388                         //ret.disabled(true);
389                         if (tli->lyxCode() == Inset::TABULAR_CODE) {
390                                 ret = static_cast<InsetTabular *>(tli)
391                                         ->getStatus(argument);
392                         } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
393                                 ret = static_cast<InsetTabular *>
394                                         (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
395                                         ->getStatus(argument);
396                         }
397                         flag |= ret;
398                         disable = false;
399                 } else {
400                         static InsetTabular inset(*owner->buffer(), 1, 1);
401                         FuncStatus ret;
402
403                         disable = true;
404                         ret = inset.getStatus(argument);
405                         if (ret.onoff(true) || ret.onoff(false))
406                                 flag.setOnOff(false);
407                 }
408                 break;
409
410         case LFUN_VC_REGISTER:
411                 disable = buf->lyxvc.inUse();
412                 break;
413         case LFUN_VC_CHECKIN:
414                 disable = !buf->lyxvc.inUse() || buf->isReadonly();
415                 break;
416         case LFUN_VC_CHECKOUT:
417                 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
418                 break;
419         case LFUN_VC_REVERT:
420         case LFUN_VC_UNDO:
421         case LFUN_VC_HISTORY:
422                 disable = !buf->lyxvc.inUse();
423                 break;
424         case LFUN_BOOKMARK_GOTO:
425                 disable =  !owner->view()->
426                         isSavedPosition(strToUnsignedInt(argument));
427                 break;
428
429
430         case LFUN_INSET_TOGGLE: {
431                 LyXText * lt = owner->view()->getLyXText();
432                 disable = !(isEditableInset(lt->getInset())
433                             || (lt->inset_owner
434                                 && lt->inset_owner->owner()
435                                 && lt->inset_owner->owner()->isOpen()));
436                 break;
437         }
438         case LFUN_MATH_VALIGN:
439                 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
440                         char align = mathcursor->valign();
441                         if (align == '\0') {
442                                 disable = true;
443                                 break;
444                         }
445                         if (argument.empty()) {
446                                 flag.clear();
447                                 break;
448                         }
449                         if (!contains("tcb", argument[0])) {
450                                 disable = true;
451                                 break;
452                         }
453                         flag.setOnOff(argument[0] == align);
454                 } else
455                         disable = true;
456                 break;
457
458         case LFUN_MATH_HALIGN:
459                 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
460                         char align = mathcursor->halign();
461                         if (align == '\0') {
462                                 disable = true;
463                                 break;
464                         }
465                         if (argument.empty()) {
466                                 flag.clear();
467                                 break;
468                         }
469                         if (!contains("lcr", argument[0])) {
470                                 disable = true;
471                                 break;
472                         }
473                         flag.setOnOff(argument[0] == align);
474                 } else
475                         disable = true;
476                 break;
477
478         case LFUN_MATH_MUTATE:
479                 if (tli && (tli->lyxCode() == Inset::MATH_CODE))
480                         flag.setOnOff(mathcursor->formula()->hullType() == argument);
481                 else
482                         disable = true;
483                 break;
484
485         // we just need to be in math mode to enable that
486         case LFUN_MATH_SIZE:
487         case LFUN_MATH_SPACE:
488         case LFUN_MATH_LIMITS:
489         case LFUN_MATH_NONUMBER:
490         case LFUN_MATH_NUMBER:
491                 disable = !mathcursor;
492                 break;
493
494         // we need to be math mode and a math array for that
495         // Hack: halign produces non-zero result iff we are in a math array
496         case LFUN_MATH_ROW_INSERT:
497         case LFUN_MATH_ROW_DELETE:
498         case LFUN_MATH_COLUMN_INSERT:
499         case LFUN_MATH_COLUMN_DELETE:
500                 disable = !mathcursor || !mathcursor->halign() ||
501                         mathcursor->formula()->hullType() == "simple";
502                 break;
503
504         default:
505                 break;
506         }
507
508         // the functions which insert insets
509         Inset::Code code = Inset::NO_CODE;
510         switch (action) {
511         case LFUN_INSET_ERT:
512                 code = Inset::ERT_CODE;
513                 break;
514         case LFUN_INSET_GRAPHICS:
515                 code = Inset::GRAPHICS_CODE;
516                 break;
517         case LFUN_INSET_FOOTNOTE:
518                 code = Inset::FOOT_CODE;
519                 break;
520         case LFUN_DIALOG_TABULAR_INSERT:
521         case LFUN_INSET_TABULAR:
522                 code = Inset::TABULAR_CODE;
523                 break;
524         case LFUN_INSET_EXTERNAL:
525                 code = Inset::EXTERNAL_CODE;
526                 break;
527         case LFUN_INSET_MARGINAL:
528                 code = Inset::MARGIN_CODE;
529                 break;
530         case LFUN_INSET_MINIPAGE:
531                 code = Inset::MINIPAGE_CODE;
532                 break;
533         case LFUN_INSET_FLOAT:
534         case LFUN_INSET_WIDE_FLOAT:
535                 code = Inset::FLOAT_CODE;
536                 break;
537         case LFUN_FLOAT_LIST:
538                 code = Inset::FLOAT_LIST_CODE;
539                 break;
540 #if 0
541         case LFUN_INSET_LIST:
542                 code = Inset::LIST_CODE;
543                 break;
544         case LFUN_INSET_THEOREM:
545                 code = Inset::THEOREM_CODE;
546                 break;
547 #endif
548         case LFUN_INSET_CAPTION:
549                 code = Inset::CAPTION_CODE;
550                 break;
551         case LFUN_INSERT_NOTE:
552                 code = Inset::IGNORE_CODE;
553                 break;
554         case LFUN_INSERT_LABEL:
555                 code = Inset::LABEL_CODE;
556                 break;
557         case LFUN_REF_INSERT:
558                 code = Inset::REF_CODE;
559                 break;
560         case LFUN_CITATION_CREATE:
561         case LFUN_CITATION_INSERT:
562                 code = Inset::CITE_CODE;
563                 break;
564         case LFUN_INSERT_BIBTEX:
565                 code = Inset::BIBTEX_CODE;
566                 break;
567         case LFUN_INDEX_INSERT:
568                 code = Inset::INDEX_CODE;
569                 break;
570         case LFUN_INDEX_PRINT:
571                 code = Inset::INDEX_PRINT_CODE;
572                 break;
573         case LFUN_CHILD_INSERT:
574                 code = Inset::INCLUDE_CODE;
575                 break;
576         case LFUN_TOC_INSERT:
577                 code = Inset::TOC_CODE;
578                 break;
579         case LFUN_PARENTINSERT:
580                 code = Inset::PARENT_CODE;
581                 break;
582         case LFUN_HTMLURL:
583         case LFUN_URL:
584         case LFUN_INSERT_URL:
585                 code = Inset::URL_CODE;
586                 break;
587         case LFUN_QUOTE:
588                 // always allow this, since we will inset a raw quote
589                 // if an inset is not allowed.
590                 break;
591         case LFUN_HYPHENATION:
592         case LFUN_LIGATURE_BREAK:
593         case LFUN_HFILL:
594         case LFUN_MENU_SEPARATOR:
595         case LFUN_LDOTS:
596         case LFUN_END_OF_SENTENCE:
597                 code = Inset::SPECIALCHAR_CODE;
598                 break;
599         case LFUN_PROTECTEDSPACE:
600                 // slight hack: we know this is allowed in math mode
601                 if (!mathcursor)
602                         code = Inset::SPECIALCHAR_CODE;
603                 break;
604         default:
605                 break;
606         }
607         if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code)) {
608                 disable = true;
609         }
610
611         if (disable)
612                         flag.disabled(true);
613
614         // A few general toggles
615         switch (action) {
616         case LFUN_TOOLTIPS_TOGGLE:
617                 flag.setOnOff(owner->getDialogs()->tooltipsEnabled());
618                 break;
619
620         case LFUN_READ_ONLY_TOGGLE:
621                 flag.setOnOff(buf->isReadonly());
622                 break;
623         case LFUN_APPENDIX:
624                 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
625                 break;
626         case LFUN_SWITCHBUFFER:
627                 // toggle on the current buffer, but do not toggle off
628                 // the other ones (is that a good idea?)
629                 if (argument == buf->fileName())
630                         flag.setOnOff(true);
631                 break;
632         default:
633                 break;
634         }
635
636         // the font related toggles
637         if (!mathcursor) {
638                 LyXFont const & font = TEXT(false)->real_current_font;
639                 switch (action) {
640                 case LFUN_EMPH:
641                         flag.setOnOff(font.emph() == LyXFont::ON);
642                         break;
643                 case LFUN_NOUN:
644                         flag.setOnOff(font.noun() == LyXFont::ON);
645                         break;
646                 case LFUN_BOLD:
647                         flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
648                         break;
649                 case LFUN_SANS:
650                         flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
651                         break;
652                 case LFUN_ROMAN:
653                         flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
654                         break;
655                 case LFUN_CODE:
656                         flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
657                         break;
658                 default:
659                         break;
660                 }
661         }
662         else {
663                 string tc = mathcursor->getLastCode();
664                 switch (action) {
665                 case LFUN_BOLD:
666                         flag.setOnOff(tc == "mathbf");
667                         break;
668                 case LFUN_SANS:
669                         flag.setOnOff(tc == "mathsf");
670                         break;
671                 case LFUN_EMPH:
672                         flag.setOnOff(tc == "mathcal");
673                         break;
674                 case LFUN_ROMAN:
675                         flag.setOnOff(tc == "mathrm");
676                         break;
677                 case LFUN_CODE:
678                         flag.setOnOff(tc == "mathtt");
679                         break;
680                 case LFUN_NOUN:
681                         flag.setOnOff(tc == "mathbb");
682                         break;
683                 case LFUN_DEFAULT:
684                         flag.setOnOff(tc == "mathnormal");
685                         break;
686                 default:
687                         break;
688                 }
689         }
690
691         return flag;
692 }
693
694
695 // temporary dispatch method
696 void LyXFunc::miniDispatch(string const & s)
697 {
698         string s2(frontStrip(strip(s)));
699
700         if (!s2.empty()) {
701                 verboseDispatch(s2, true);
702         }
703 }
704
705
706 void LyXFunc::verboseDispatch(string const & s, bool show_sc)
707 {
708         int action = lyxaction.LookupFunc(frontStrip(s));
709
710         if (action == LFUN_UNKNOWN_ACTION) {
711                 string const msg = string(_("Unknown function ("))
712                         + s + ")";
713                 owner->message(msg);
714         } else {
715                 verboseDispatch(action, show_sc);
716         }
717 }
718
719
720 void LyXFunc::verboseDispatch(int ac, bool show_sc)
721 {
722         string argument;
723         kb_action action;
724
725         // get the real action and argument
726         action = lyxaction.retrieveActionArg(ac, argument);
727
728         verboseDispatch(action, argument, show_sc);
729 }
730
731
732
733 void LyXFunc::verboseDispatch(kb_action action,
734                               string const & argument, bool show_sc)
735 {
736         string res = dispatch(action, argument);
737
738         commandshortcut.erase();
739
740         if (show_sc && action != LFUN_SELFINSERT) {
741                 // Put name of command and list of shortcuts
742                 // for it in minibuffer
743                 string comname = lyxaction.getActionName(action);
744
745                 int pseudoaction = action;
746                 bool argsadded = false;
747
748                 if (!argument.empty()) {
749                         // the pseudoaction is useful for the bindings
750                         pseudoaction =
751                                 lyxaction.searchActionArg(action,
752                                                           argument);
753
754                         if (pseudoaction == LFUN_UNKNOWN_ACTION) {
755                                 pseudoaction = action;
756                         } else {
757                                 comname += " " + argument;
758                                 argsadded = true;
759                         }
760                 }
761
762                 string const shortcuts =
763                         toplevel_keymap->findbinding(pseudoaction);
764
765                 if (!shortcuts.empty()) {
766                         comname += ": " + shortcuts;
767                 } else if (!argsadded && !argument.empty()) {
768                         comname += " " + argument;
769                 }
770
771                 if (!comname.empty()) {
772                         comname = strip(comname);
773                         commandshortcut = "(" + comname + ')';
774                 }
775         }
776
777         if (res.empty()) {
778                 if (!commandshortcut.empty()) {
779                         owner->getMiniBuffer()->addSet(commandshortcut);
780                 }
781         } else {
782                 owner->getMiniBuffer()->addSet(' ' + commandshortcut);
783         }
784 }
785
786
787 string const LyXFunc::dispatch(kb_action action, string argument)
788 {
789         lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << action
790                               <<"] arg[" << argument << "]" << endl;
791
792         // we have not done anything wrong yet.
793         errorstat = false;
794         dispatch_buffer.erase();
795
796 #ifdef NEW_DISPATCHER
797         // We try do call the most specific dispatcher first:
798         //  1. the lockinginset's dispatch
799         //  2. the bufferview's dispatch
800         //  3. the lyxview's dispatch
801 #endif
802
803         selection_possible = false;
804
805         if (owner->view()->available())
806                 owner->view()->hideCursor();
807
808         // We cannot use this function here
809         if (getStatus(action, argument).disabled()) {
810                 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: "
811                        << lyxaction.getActionName(action)
812                        << " [" << action << "] is disabled at this location"
813                        << endl;
814                 setErrorMessage(getStatusMessage());
815                 goto exit_with_message;
816         }
817
818         if (owner->view()->available() && owner->view()->theLockingInset()) {
819                 UpdatableInset::RESULT result;
820                 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
821                                      (!keyseq.deleted())))
822                 {
823                         UpdatableInset * inset = owner->view()->theLockingInset();
824 #if 1
825                         int inset_x;
826                         int dummy_y;
827                         inset->getCursorPos(owner->view(), inset_x, dummy_y);
828 #endif
829                         if ((action == LFUN_UNKNOWN_ACTION)
830                             && argument.empty()) {
831                                 argument = keyseq.getLastKeyEncoded();
832                         }
833                         // Undo/Redo is a bit tricky for insets.
834                         if (action == LFUN_UNDO) {
835                                 owner->view()->menuUndo();
836                                 goto exit_with_message;
837                         } else if (action == LFUN_REDO) {
838                                 owner->view()->menuRedo();
839                                 goto exit_with_message;
840                         } else if (((result=inset->
841                                      // Hand-over to inset's own dispatch:
842                                      localDispatch(owner->view(), action, argument)) ==
843                                     UpdatableInset::DISPATCHED) ||
844                                    (result == UpdatableInset::DISPATCHED_NOUPDATE))
845                                 goto exit_with_message;
846                                         // If UNDISPATCHED, just soldier on
847                         else if (result == UpdatableInset::FINISHED) {
848                                 goto exit_with_message;
849                                 // We do not need special RTL handling here:
850                                 // FINISHED means that the cursor should be
851                                 // one position after the inset.
852                         } else if (result == UpdatableInset::FINISHED_RIGHT) {
853                                 TEXT()->cursorRight(owner->view());
854                                 moveCursorUpdate(true, false);
855                                 owner->showState();
856                                 goto exit_with_message;
857                         } else if (result == UpdatableInset::FINISHED_UP) {
858                                 if (TEXT()->cursor.irow()->previous()) {
859 #if 1
860                                         TEXT()->setCursorFromCoordinates(
861                                                 owner->view(), TEXT()->cursor.ix() + inset_x,
862                                                 TEXT()->cursor.iy() -
863                                                 TEXT()->cursor.irow()->baseline() - 1);
864                                         TEXT()->cursor.x_fix(TEXT()->cursor.x());
865 #else
866                                         TEXT()->cursorUp(owner->view());
867 #endif
868                                         moveCursorUpdate(true, false);
869                                         owner->showState();
870                                 } else {
871                                         owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
872                                 }
873                                 goto exit_with_message;
874                         } else if (result == UpdatableInset::FINISHED_DOWN) {
875                                 if (TEXT()->cursor.irow()->next()) {
876 #if 1
877                                         TEXT()->setCursorFromCoordinates(
878                                                 owner->view(), TEXT()->cursor.ix() + inset_x,
879                                                 TEXT()->cursor.iy() -
880                                                 TEXT()->cursor.irow()->baseline() +
881                                                 TEXT()->cursor.irow()->height() + 1);
882                                         TEXT()->cursor.x_fix(TEXT()->cursor.x());
883 #else
884                                         TEXT()->cursorDown(owner->view());
885 #endif
886                                 } else {
887                                         TEXT()->cursorRight(owner->view());
888                                 }
889                                 moveCursorUpdate(true, false);
890                                 owner->showState();
891                                 goto exit_with_message;
892                         }
893 #warning I am not sure this is still right, please have a look! (Jug 20020417)
894                         else { // result == UNDISPATCHED
895                                 //setMessage(N_("Text mode"));
896                                 switch (action) {
897                                 case LFUN_UNKNOWN_ACTION:
898                                 case LFUN_BREAKPARAGRAPH:
899                                 case LFUN_BREAKLINE:
900                                         TEXT()->cursorRight(owner->view());
901                                         owner->view()->setState();
902                                         owner->showState();
903                                         break;
904                                 case LFUN_RIGHT:
905                                         if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
906                                                 TEXT()->cursorRight(owner->view());
907                                                 moveCursorUpdate(true, false);
908                                                 owner->showState();
909                                         }
910                                         goto exit_with_message;
911                                 case LFUN_LEFT:
912                                         if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
913                                                 TEXT()->cursorRight(owner->view());
914                                                 moveCursorUpdate(true, false);
915                                                 owner->showState();
916                                         }
917                                         goto exit_with_message;
918                                 case LFUN_DOWN:
919                                         if (TEXT()->cursor.row()->next())
920                                                 TEXT()->cursorDown(owner->view());
921                                         else
922                                                 TEXT()->cursorRight(owner->view());
923                                         moveCursorUpdate(true, false);
924                                         owner->showState();
925                                         goto exit_with_message;
926                                 default:
927                                         break;
928                                 }
929                         }
930                 }
931         }
932
933         switch (action) {
934
935         case LFUN_ESCAPE:
936         {
937                 if (!owner->view()->available()) break;
938                 // this function should be used always [asierra060396]
939                 UpdatableInset * tli =
940                         owner->view()->theLockingInset();
941                 if (tli) {
942                         UpdatableInset * lock = tli->getLockingInset();
943
944                         if (tli == lock) {
945                                 owner->view()->unlockInset(tli);
946                                 TEXT()->cursorRight(owner->view());
947                                 moveCursorUpdate(true, false);
948                                 owner->showState();
949                         } else {
950                                 tli->unlockInsetInInset(owner->view(),
951                                                         lock,
952                                                         true);
953                         }
954                         finishUndo();
955                         // Tell the paragraph dialog that we changed paragraph
956                         owner->getDialogs()->updateParagraph();
957                 }
958         }
959         break;
960
961                 // --- Misc -------------------------------------------
962         case LFUN_WORDFINDFORWARD  :
963         case LFUN_WORDFINDBACKWARD : {
964                 static string last_search;
965                 string searched_string;
966
967                 if (!argument.empty()) {
968                         last_search = argument;
969                         searched_string = argument;
970                 } else {
971                         searched_string = last_search;
972                 }
973                 bool fw = (action == LFUN_WORDFINDBACKWARD);
974                 if (!searched_string.empty()) {
975                         lyxfind::LyXFind(owner->view(), searched_string, fw);
976                 }
977 //              owner->view()->showCursor();
978         }
979         break;
980
981         case LFUN_PREFIX:
982         {
983                 if (owner->view()->available() && !owner->view()->theLockingInset()) {
984                         owner->view()->update(TEXT(),
985                                               BufferView::SELECT|BufferView::FITCUR);
986                 }
987                 owner->message(keyseq.printOptions());
988         }
989         break;
990
991         // --- Misc -------------------------------------------
992         case LFUN_EXEC_COMMAND:
993         {
994                 vector<string> allCmds;
995                 transform(lyxaction.func_begin(), lyxaction.func_end(),
996                           back_inserter(allCmds), lyx::firster());
997                 static vector<string> hist;
998                 owner->getMiniBuffer()->prepareForInput(allCmds, hist);
999         }
1000         break;
1001
1002         case LFUN_CANCEL:                   // RVDK_PATCH_5
1003                 keyseq.reset();
1004                 meta_fake_bit = key_modifier::none;
1005                 if (owner->view()->available())
1006                         // cancel any selection
1007                         dispatch(LFUN_MARK_OFF);
1008                 setMessage(N_("Cancel"));
1009                 break;
1010
1011         case LFUN_META_FAKE:                                 // RVDK_PATCH_5
1012         {
1013                 meta_fake_bit = key_modifier::alt;
1014                 setMessage(keyseq.print());
1015         }
1016         break;
1017
1018         case LFUN_READ_ONLY_TOGGLE:
1019                 if (owner->buffer()->lyxvc.inUse()) {
1020                         owner->buffer()->lyxvc.toggleReadOnly();
1021                 } else {
1022                         owner->buffer()->setReadonly(
1023                                 !owner->buffer()->isReadonly());
1024                 }
1025                 break;
1026
1027         case LFUN_CENTER: // this is center and redraw.
1028                 owner->view()->center();
1029                 break;
1030
1031                 // --- Menus -----------------------------------------------
1032         case LFUN_MENUNEW:
1033                 menuNew(argument, false);
1034                 break;
1035
1036         case LFUN_MENUNEWTMPLT:
1037                 menuNew(argument, true);
1038                 break;
1039
1040         case LFUN_CLOSEBUFFER:
1041                 closeBuffer();
1042                 break;
1043
1044         case LFUN_MENUWRITE:
1045                 if (!owner->buffer()->isUnnamed()) {
1046                         ostringstream s1;
1047                         s1 << _("Saving document") << ' '
1048                            << MakeDisplayPath(owner->buffer()->fileName() + "...");
1049                         owner->message(s1.str().c_str());
1050                         MenuWrite(owner->view(), owner->buffer());
1051                 } else
1052                         WriteAs(owner->view(), owner->buffer());
1053                 break;
1054
1055         case LFUN_WRITEAS:
1056                 WriteAs(owner->view(), owner->buffer(), argument);
1057                 break;
1058
1059         case LFUN_MENURELOAD:
1060                 reloadBuffer();
1061                 break;
1062
1063         case LFUN_UPDATE:
1064                 Exporter::Export(owner->buffer(), argument, true);
1065                 break;
1066
1067         case LFUN_PREVIEW:
1068                 Exporter::Preview(owner->buffer(), argument);
1069                 break;
1070
1071         case LFUN_BUILDPROG:
1072                 Exporter::Export(owner->buffer(), "program", true);
1073                 break;
1074
1075         case LFUN_RUNCHKTEX:
1076                 MenuRunChktex(owner->buffer());
1077                 break;
1078
1079         case LFUN_MENUPRINT:
1080                 owner->getDialogs()->showPrint();
1081                 break;
1082
1083         case LFUN_EXPORT:
1084                 if (argument == "custom")
1085                         owner->getDialogs()->showSendto();
1086                 else
1087                         Exporter::Export(owner->buffer(), argument, false);
1088                 break;
1089
1090         case LFUN_IMPORT:
1091                 doImport(argument);
1092                 break;
1093
1094         case LFUN_QUIT:
1095                 QuitLyX();
1096                 break;
1097
1098         case LFUN_TOCVIEW:
1099 #if 0
1100         case LFUN_LOFVIEW:
1101         case LFUN_LOTVIEW:
1102         case LFUN_LOAVIEW:
1103 #endif
1104         {
1105                 InsetCommandParams p;
1106
1107 #if 0
1108                 if (action == LFUN_TOCVIEW)
1109 #endif
1110                         p.setCmdName("tableofcontents");
1111 #if 0
1112                 else if (action == LFUN_LOAVIEW)
1113                         p.setCmdName("listof{algorithm}{List of Algorithms}");
1114                 else if (action == LFUN_LOFVIEW)
1115                         p.setCmdName("listoffigures");
1116                 else
1117                         p.setCmdName("listoftables");
1118 #endif
1119                 owner->getDialogs()->createTOC(p.getAsString());
1120                 break;
1121         }
1122
1123         case LFUN_DIALOG_TABULAR_INSERT:
1124                 owner->getDialogs()->showTabularCreate();
1125                 break;
1126
1127         case LFUN_AUTOSAVE:
1128                 AutoSave(owner->view());
1129                 break;
1130
1131         case LFUN_UNDO:
1132                 owner->view()->menuUndo();
1133                 break;
1134
1135         case LFUN_REDO:
1136                 owner->view()->menuRedo();
1137                 break;
1138
1139         case LFUN_MENUSEARCH:
1140                 owner->getDialogs()->showSearch();
1141                 break;
1142
1143         case LFUN_REMOVEERRORS:
1144                 if (owner->view()->removeAutoInsets()) {
1145 #warning repaint() or update() or nothing ?
1146                         owner->view()->repaint();
1147                         owner->view()->fitCursor();
1148                 }
1149                 break;
1150
1151         case LFUN_DEPTH_MIN:
1152                 changeDepth(owner->view(), TEXT(false), -1);
1153                 break;
1154
1155         case LFUN_DEPTH_PLUS:
1156                 changeDepth(owner->view(), TEXT(false), 1);
1157                 break;
1158
1159         case LFUN_FREE:
1160                 owner->getDialogs()->setUserFreeFont();
1161                 break;
1162
1163         case LFUN_RECONFIGURE:
1164                 Reconfigure(owner->view());
1165                 break;
1166
1167 #if 0
1168         case LFUN_FLOATSOPERATE:
1169                 if (argument == "openfoot")
1170                         owner->view()->allFloats(1,0);
1171                 else if (argument == "closefoot")
1172                         owner->view()->allFloats(0,0);
1173                 else if (argument == "openfig")
1174                         owner->view()->allFloats(1,1);
1175                 else if (argument == "closefig")
1176                         owner->view()->allFloats(0,1);
1177                 break;
1178 #else
1179 #ifdef WITH_WARNINGS
1180 //#warning Find another implementation here (or another lyxfunc)!
1181 #endif
1182 #endif
1183         case LFUN_HELP_ABOUTLYX:
1184                 owner->getDialogs()->showAboutlyx();
1185                 break;
1186
1187         case LFUN_HELP_TEXINFO:
1188                 owner->getDialogs()->showTexinfo();
1189                 break;
1190
1191         case LFUN_HELP_OPEN:
1192         {
1193                 string const arg = argument;
1194                 if (arg.empty()) {
1195                         setErrorMessage(N_("Missing argument"));
1196                         break;
1197                 }
1198                 owner->prohibitInput();
1199                 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1200                 if (fname.empty()) {
1201                         lyxerr << "LyX: unable to find documentation file `"
1202                                << arg << "'. Bad installation?" << endl;
1203                         owner->allowInput();
1204                         break;
1205                 }
1206                 ostringstream str;
1207                 str << _("Opening help file") << ' '
1208                     << MakeDisplayPath(fname) << "...";
1209                 owner->message(str.str().c_str());
1210                 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1211                 owner->allowInput();
1212                 break;
1213         }
1214
1215                 // --- version control -------------------------------
1216         case LFUN_VC_REGISTER:
1217         {
1218                 if (!owner->buffer()->lyxvc.inUse())
1219                         owner->buffer()->lyxvc.registrer();
1220         }
1221         break;
1222
1223         case LFUN_VC_CHECKIN:
1224         {
1225                 if (owner->buffer()->lyxvc.inUse()
1226                     && !owner->buffer()->isReadonly())
1227                         owner->buffer()->lyxvc.checkIn();
1228         }
1229         break;
1230
1231         case LFUN_VC_CHECKOUT:
1232         {
1233                 if (owner->buffer()->lyxvc.inUse()
1234                     && owner->buffer()->isReadonly())
1235                         owner->buffer()->lyxvc.checkOut();
1236         }
1237         break;
1238
1239         case LFUN_VC_REVERT:
1240         {
1241                 owner->buffer()->lyxvc.revert();
1242         }
1243         break;
1244
1245         case LFUN_VC_UNDO:
1246         {
1247                 owner->buffer()->lyxvc.undoLast();
1248         }
1249         break;
1250
1251         case LFUN_VC_HISTORY:
1252         {
1253                 owner->getDialogs()->showVCLogFile();
1254                 break;
1255         }
1256
1257         // --- buffers ----------------------------------------
1258
1259         case LFUN_SWITCHBUFFER:
1260                 owner->view()->buffer(bufferlist.getBuffer(argument));
1261                 break;
1262
1263         case LFUN_FILE_NEW:
1264         {
1265                 // servercmd: argument must be <file>:<template>
1266                 Buffer * tmpbuf = NewLyxFile(argument);
1267                 if (tmpbuf)
1268                         owner->view()->buffer(tmpbuf);
1269         }
1270         break;
1271
1272         case LFUN_FILE_OPEN:
1273                 open(argument);
1274                 break;
1275
1276         case LFUN_LATEX_LOG:
1277                 owner->getDialogs()->showLogFile();
1278                 break;
1279
1280         case LFUN_LAYOUT_DOCUMENT:
1281                 owner->getDialogs()->showDocument();
1282                 break;
1283
1284         case LFUN_LAYOUT_PARAGRAPH:
1285                 owner->getDialogs()->showParagraph();
1286                 break;
1287
1288         case LFUN_LAYOUT_CHARACTER:
1289                 owner->getDialogs()->showCharacter();
1290                 break;
1291
1292         case LFUN_LAYOUT_TABULAR:
1293             if (owner->view()->theLockingInset()) {
1294                 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1295                     InsetTabular * inset = static_cast<InsetTabular *>
1296                         (owner->view()->theLockingInset());
1297                     inset->openLayoutDialog(owner->view());
1298                 } else if (owner->view()->theLockingInset()->
1299                            getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1300                     InsetTabular * inset = static_cast<InsetTabular *>(
1301                         owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1302                     inset->openLayoutDialog(owner->view());
1303                 }
1304             }
1305             break;
1306
1307         case LFUN_LAYOUT_PREAMBLE:
1308                 owner->getDialogs()->showPreamble();
1309                 break;
1310
1311         case LFUN_DROP_LAYOUTS_CHOICE:
1312                 owner->getToolbar()->openLayoutList();
1313                 break;
1314
1315         case LFUN_MENU_OPEN_BY_NAME:
1316                 owner->getMenubar()->openByName(argument);
1317                 break; // RVDK_PATCH_5
1318
1319         case LFUN_SPELLCHECK:
1320                 if (lyxrc.isp_command != "none")
1321                         owner->getDialogs()->showSpellchecker();
1322                 break;
1323
1324         // --- lyxserver commands ----------------------------
1325
1326
1327         case LFUN_GETNAME:
1328                 setMessage(owner->buffer()->fileName());
1329                 lyxerr[Debug::INFO] << "FNAME["
1330                                << owner->buffer()->fileName()
1331                                << "] " << endl;
1332                 break;
1333
1334         case LFUN_NOTIFY:
1335         {
1336                 dispatch_buffer = keyseq.print();
1337                 lyxserver->notifyClient(dispatch_buffer);
1338         }
1339         break;
1340
1341         case LFUN_GOTOFILEROW:
1342         {
1343                 string file_name;
1344                 int row;
1345                 istringstream istr(argument.c_str());
1346                 istr >> file_name >> row;
1347                 // Must replace extension of the file to be .lyx and get full path
1348                 string const s(ChangeExtension(file_name, ".lyx"));
1349
1350                 // Either change buffer or load the file
1351                 if (bufferlist.exists(s)) {
1352                         owner->view()->buffer(bufferlist.getBuffer(s));
1353                 } else {
1354                         owner->view()->buffer(bufferlist.loadLyXFile(s));
1355                 }
1356
1357                 owner->view()->setCursorFromRow(row);
1358
1359                 owner->view()->center();
1360                 // see BufferView_pimpl::center() 
1361                 owner->view()->updateScrollbar();
1362         }
1363         break;
1364
1365         case LFUN_GOTO_PARAGRAPH:
1366         {
1367                 istringstream istr(argument.c_str());
1368
1369                 int id;
1370                 istr >> id;
1371                 Paragraph * par = owner->buffer()->getParFromID(id);
1372                 if (par == 0) {
1373                         lyxerr[Debug::INFO] << "No matching paragraph found! ["
1374                                             << id << "]" << endl;
1375                         break;
1376                 } else {
1377                         lyxerr[Debug::INFO] << "Paragraph " << par->id()
1378                                             << " found." << endl;
1379                 }
1380
1381                 if (owner->view()->theLockingInset())
1382                         owner->view()->unlockInset(owner->view()->theLockingInset());
1383                 if (par->inInset()) {
1384                         par->inInset()->edit(owner->view());
1385                 }
1386                 // Set the cursor
1387                 owner->view()->getLyXText()->setCursor(owner->view(), par, 0);
1388                 owner->view()->setState();
1389                 owner->showState();
1390
1391                 owner->view()->center();
1392                 // see BufferView_pimpl::center() 
1393                 owner->view()->updateScrollbar();
1394         }
1395         break;
1396
1397         case LFUN_APROPOS:
1398         case LFUN_GETTIP:
1399         {
1400                 int const qa = lyxaction.LookupFunc(argument);
1401                 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1402         }
1403         break;
1404
1405         // --- toolbar ----------------------------------
1406         case LFUN_PUSH_TOOLBAR:
1407         {
1408                 int nth = strToInt(argument);
1409                 if (nth <= 0) {
1410                         setErrorMessage(N_("Push-toolbar needs argument > 0"));
1411                 } else {
1412                         owner->getToolbar()->push(nth);
1413                 }
1414         }
1415         break;
1416
1417         case LFUN_ADD_TO_TOOLBAR:
1418         {
1419                 if (lyxerr.debugging(Debug::GUI)) {
1420                         lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1421                                 "argument = `" << argument << '\'' << endl;
1422                 }
1423                 string tmp(argument);
1424                 //lyxerr <<string("Argument: ") + argument);
1425                 //lyxerr <<string("Tmp     : ") + tmp);
1426                 if (tmp.empty()) {
1427                         setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1428                 } else {
1429                         owner->getToolbar()->add(argument, false);
1430                         owner->getToolbar()->set();
1431                 }
1432         }
1433         break;
1434
1435         // --- insert characters ----------------------------------------
1436
1437         // ---  Mathed stuff. If we are here, there is no locked inset yet.
1438         case LFUN_MATH_EXTERN:
1439         case LFUN_MATH_NUMBER:
1440         case LFUN_MATH_NONUMBER:
1441         case LFUN_MATH_LIMITS:
1442         {
1443                 setErrorMessage(N_("This is only allowed in math mode!"));
1444         }
1445         break;
1446
1447         // passthrough hat and underscore outside mathed:
1448         case LFUN_SUBSCRIPT:
1449                 dispatch(LFUN_SELFINSERT, "_");
1450                 break;
1451         case LFUN_SUPERSCRIPT:
1452                 dispatch(LFUN_SELFINSERT, "^");
1453                 break;
1454
1455         case LFUN_MATH_PANEL:
1456                 owner->getDialogs()->showMathPanel();
1457                 break;
1458
1459         case LFUN_CITATION_CREATE:
1460         {
1461                 InsetCommandParams p("cite");
1462
1463                 if (!argument.empty()) {
1464                         // This should be set at source, ie when typing
1465                         // "citation-insert foo" in the minibuffer.
1466                         // Question: would pybibliographer also need to be
1467                         // changed. Suspect so. Leave as-is therefore.
1468                         if (contains(argument, "|")) {
1469                                 p.setContents(token(argument, '|', 0));
1470                                 p.setOptions( token(argument, '|', 1));
1471                         } else {
1472                                 p.setContents(argument);
1473                         }
1474                         dispatch(LFUN_CITATION_INSERT, p.getAsString());
1475                 } else
1476                         owner->getDialogs()->createCitation(p.getAsString());
1477         }
1478         break;
1479
1480         case LFUN_CHILDOPEN:
1481         {
1482                 string const filename =
1483                         MakeAbsPath(argument,
1484                                     owner->buffer()->filePath());
1485                 setMessage(N_("Opening child document ") +
1486                            MakeDisplayPath(filename) + "...");
1487                 owner->view()->savePosition(0);
1488                 if (bufferlist.exists(filename))
1489                         owner->view()->buffer(bufferlist.getBuffer(filename));
1490                 else
1491                         owner->view()->buffer(bufferlist.loadLyXFile(filename));
1492         }
1493         break;
1494
1495         case LFUN_TOGGLECURSORFOLLOW:
1496                 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1497                 break;
1498
1499         case LFUN_KMAP_OFF:             // keymap off
1500                 owner->getIntl()->KeyMapOn(false);
1501                 break;
1502
1503         case LFUN_KMAP_PRIM:    // primary keymap
1504                 owner->getIntl()->KeyMapPrim();
1505                 break;
1506
1507         case LFUN_KMAP_SEC:             // secondary keymap
1508                 owner->getIntl()->KeyMapSec();
1509                 break;
1510
1511         case LFUN_KMAP_TOGGLE:  // toggle keymap
1512                 owner->getIntl()->ToggleKeyMap();
1513                 break;
1514
1515         case LFUN_SEQUENCE:
1516         {
1517                 // argument contains ';'-terminated commands
1518                 while (!argument.empty()) {
1519                         string first;
1520                         argument = split(argument, first, ';');
1521                         verboseDispatch(first, false);
1522                 }
1523         }
1524         break;
1525
1526         case LFUN_DIALOG_PREFERENCES:
1527                 owner->getDialogs()->showPreferences();
1528                 break;
1529
1530         case LFUN_SAVEPREFERENCES:
1531         {
1532                 Path p(user_lyxdir);
1533                 lyxrc.write("preferences");
1534         }
1535         break;
1536
1537         case LFUN_SCREEN_FONT_UPDATE:
1538         {
1539                 // handle the screen font changes.
1540                 //
1541                 lyxrc.set_font_norm_type();
1542                 lyx_gui::update_fonts();
1543                 // Of course we should only do the resize and the textcache.clear
1544                 // if values really changed...but not very important right now. (Lgb)
1545                 // All visible buffers will need resize
1546                 owner->view()->resize();
1547                 owner->view()->repaint();
1548                 // We also need to empty the textcache so that
1549                 // the buffer will be formatted correctly after
1550                 // a zoom change.
1551                 textcache.clear();
1552         }
1553         break;
1554
1555         case LFUN_SET_COLOR:
1556         {
1557                 string lyx_name;
1558                 string const x11_name = split(argument, lyx_name, ' ');
1559                 if (lyx_name.empty() || x11_name.empty()) {
1560                         setErrorMessage(N_("Syntax: set-color <lyx_name>"
1561                                                 " <x11_name>"));
1562                         break;
1563                         }
1564
1565                 bool const graphicsbg_changed =
1566                         (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1567                          x11_name != lcolor.getX11Name(LColor::graphicsbg));
1568
1569                 if (!lcolor.setColor(lyx_name, x11_name)) {
1570                         static string const err1 (N_("Set-color \""));
1571                         static string const err2 (
1572                                 N_("\" failed - color is undefined "
1573                                    "or may not be redefined"));
1574                         setErrorMessage(_(err1) + lyx_name + _(err2));
1575                         break;
1576                 }
1577
1578                 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1579
1580                 if (graphicsbg_changed) {
1581 #ifdef WITH_WARNINGS
1582 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1583 #endif
1584 #if 0
1585                         grfx::GCache & gc = grfx::GCache::get();
1586                         gc.changeDisplay(true);
1587 #endif
1588                 }
1589
1590                 owner->view()->repaint();
1591                 break;
1592         }
1593
1594         case LFUN_MESSAGE:
1595                 owner->message(argument);
1596                 break;
1597
1598         case LFUN_MESSAGE_PUSH:
1599                 owner->messagePush(argument);
1600                 break;
1601
1602         case LFUN_MESSAGE_POP:
1603                 owner->messagePop();
1604                 break;
1605
1606         case LFUN_FORKS_SHOW:
1607                 owner->getDialogs()->showForks();
1608                 break;
1609
1610         case LFUN_FORKS_KILL:
1611         {
1612                 if (!isStrInt(argument))
1613                         break;
1614
1615                 pid_t const pid = strToInt(argument);
1616                 ForkedcallsController & fcc = ForkedcallsController::get();
1617                 fcc.kill(pid);
1618                 break;
1619         }
1620
1621         case LFUN_TOOLTIPS_TOGGLE:
1622                 owner->getDialogs()->toggleTooltips();
1623                 break;
1624
1625         default:
1626                 // Then if it was none of the above
1627                 // Trying the BufferView::pimpl dispatch:
1628                 if (!owner->view()->Dispatch(action, argument))
1629                         lyxerr << "A truly unknown func ["
1630                                << lyxaction.getActionName(action) << "]!"
1631                                << endl;
1632                 break;
1633         } // end of switch
1634
1635 exit_with_message:
1636
1637         string const res = getMessage();
1638
1639         if (!res.empty())
1640                 owner->message(_(res));
1641         owner->updateMenubar();
1642         owner->updateToolbar();
1643
1644         return res;
1645 }
1646
1647
1648 void LyXFunc::setupLocalKeymap()
1649 {
1650         keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1651         cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1652 }
1653
1654
1655 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1656 {
1657         string initpath = lyxrc.document_path;
1658         string filename(name);
1659
1660         if (owner->view()->available()) {
1661                 string const trypath = owner->buffer()->filePath();
1662                 // If directory is writeable, use this as default.
1663                 if (IsDirWriteable(trypath))
1664                         initpath = trypath;
1665         }
1666
1667         static int newfile_number;
1668
1669         if (filename.empty()) {
1670                 filename = AddName(lyxrc.document_path,
1671                             "newfile" + tostr(++newfile_number) + ".lyx");
1672                 FileInfo fi(filename);
1673                 while (bufferlist.exists(filename) || fi.readable()) {
1674                         ++newfile_number;
1675                         filename = AddName(lyxrc.document_path,
1676                                     "newfile" + tostr(newfile_number) +
1677                                     ".lyx");
1678                         fi.newFile(filename);
1679                 }
1680         }
1681
1682         // The template stuff
1683         string templname;
1684         if (fromTemplate) {
1685                 FileDialog fileDlg(owner, _("Select template file"),
1686                         LFUN_SELECT_FILE_SYNC,
1687                         make_pair(string(_("Documents|#o#O")),
1688                                   string(lyxrc.document_path)),
1689                         make_pair(string(_("Templates|#T#t")),
1690                                   string(lyxrc.template_path)));
1691
1692                 FileDialog::Result result =
1693                         fileDlg.Select(lyxrc.template_path,
1694                                        _("*.lyx|LyX Documents (*.lyx)"));
1695
1696                 if (result.first == FileDialog::Later)
1697                         return;
1698
1699                 string const fname = result.second;
1700
1701                 if (fname.empty())
1702                         return;
1703                 templname = fname;
1704         }
1705
1706         owner->view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1707 }
1708
1709
1710 void LyXFunc::open(string const & fname)
1711 {
1712         string initpath = lyxrc.document_path;
1713
1714         if (owner->view()->available()) {
1715                 string const trypath = owner->buffer()->filePath();
1716                 // If directory is writeable, use this as default.
1717                 if (IsDirWriteable(trypath))
1718                         initpath = trypath;
1719         }
1720
1721         string filename;
1722
1723         if (fname.empty()) {
1724                 FileDialog fileDlg(owner, _("Select document to open"),
1725                         LFUN_FILE_OPEN,
1726                         make_pair(string(_("Documents|#o#O")),
1727                                   string(lyxrc.document_path)),
1728                         make_pair(string(_("Examples|#E#e")),
1729                                   string(AddPath(system_lyxdir, "examples"))));
1730
1731                 FileDialog::Result result =
1732                         fileDlg.Select(initpath,
1733                                        "*.lyx|LyX Documents (*.lyx)");
1734
1735                 if (result.first == FileDialog::Later)
1736                         return;
1737
1738                 filename = result.second;
1739
1740                 // check selected filename
1741                 if (filename.empty()) {
1742                         owner->message(_("Canceled."));
1743                         return;
1744                 }
1745         } else
1746                 filename = fname;
1747
1748         // get absolute path of file and add ".lyx" to the filename if
1749         // necessary
1750         string const fullpath = FileSearch(string(), filename, "lyx");
1751         if (fullpath.empty()) {
1752                 Alert::alert(_("Error"), _("Could not find file"), filename);
1753                 return;
1754         }
1755
1756         filename = fullpath;
1757
1758         // loads document
1759         string const disp_fn(MakeDisplayPath(filename));
1760
1761         ostringstream str;
1762         str << _("Opening document") << ' ' << disp_fn << "...";
1763
1764         owner->message(str.str().c_str());
1765
1766         Buffer * openbuf = bufferlist.loadLyXFile(filename);
1767         if (openbuf) {
1768                 owner->view()->buffer(openbuf);
1769                 ostringstream str;
1770                 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1771                 owner->message(str.str().c_str());
1772         } else {
1773                 ostringstream str;
1774                 str << _("Could not open document") << ' ' << disp_fn;
1775                 owner->message(str.str().c_str());
1776         }
1777 }
1778
1779
1780 void LyXFunc::doImport(string const & argument)
1781 {
1782         string format;
1783         string filename = split(argument, format, ' ');
1784
1785         lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1786                             << " file: " << filename << endl;
1787
1788         // need user interaction
1789         if (filename.empty()) {
1790                 string initpath = lyxrc.document_path;
1791
1792                 if (owner->view()->available()) {
1793                         string const trypath = owner->buffer()->filePath();
1794                         // If directory is writeable, use this as default.
1795                         if (IsDirWriteable(trypath))
1796                                 initpath = trypath;
1797                 }
1798
1799                 string const text = _("Select ") + formats.prettyName(format)
1800                         + _(" file to import");
1801
1802                 FileDialog fileDlg(owner, text,
1803                         LFUN_IMPORT,
1804                         make_pair(string(_("Documents|#o#O")),
1805                                   string(lyxrc.document_path)),
1806                         make_pair(string(_("Examples|#E#e")),
1807                                   string(AddPath(system_lyxdir, "examples"))));
1808
1809                 string const extension = "*." + formats.extension(format)
1810                         + "| " + formats.prettyName(format)
1811                         + " (*." + formats.extension(format) + ")";
1812
1813                 FileDialog::Result result = fileDlg.Select(initpath,
1814                                                            extension);
1815
1816                 if (result.first == FileDialog::Later)
1817                         return;
1818
1819                 filename = result.second;
1820
1821                 // check selected filename
1822                 if (filename.empty())
1823                         owner->message(_("Canceled."));
1824         }
1825
1826         if (filename.empty())
1827                 return;
1828
1829         // get absolute path of file
1830         filename = MakeAbsPath(filename);
1831
1832         string const lyxfile = ChangeExtension(filename, ".lyx");
1833
1834         // Check if the document already is open
1835         if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1836                 switch (Alert::askConfirmation(_("Document is already open:"),
1837                                         MakeDisplayPath(lyxfile, 50),
1838                                         _("Do you want to close that document now?\n"
1839                                           "('No' will just switch to the open version)")))
1840                         {
1841                         case 1:
1842                                 // If close is canceled, we cancel here too.
1843                                 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1844                                         return;
1845                                 break;
1846                         case 2:
1847                                 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1848                                 return;
1849                         case 3:
1850                                 owner->message(_("Canceled."));
1851                                 return;
1852                         }
1853         }
1854
1855         // if the file exists already, and we didn't do
1856         // -i lyx thefile.lyx, warn
1857         if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1858                 if (!Alert::askQuestion(_("A document by the name"),
1859                         MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1860                         owner->message(_("Canceled"));
1861                         return;
1862                 }
1863         }
1864
1865         Importer::Import(owner, filename, format);
1866 }
1867
1868
1869 void LyXFunc::reloadBuffer()
1870 {
1871         string const fn = owner->buffer()->fileName();
1872         if (bufferlist.close(owner->buffer()))
1873                 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1874 }
1875
1876
1877 void LyXFunc::closeBuffer()
1878 {
1879         if (bufferlist.close(owner->buffer()) && !quitting) {
1880                 if (bufferlist.empty()) {
1881                         // need this otherwise SEGV may occur while trying to
1882                         // set variables that don't exist
1883                         // since there's no current buffer
1884                         owner->getDialogs()->hideBufferDependent();
1885                 } else {
1886                         owner->view()->buffer(bufferlist.first());
1887                 }
1888         }
1889 }
1890
1891
1892 // Each "owner" should have it's own message method. lyxview and
1893 // the minibuffer would use the minibuffer, but lyxserver would
1894 // send an ERROR signal to its client.  Alejandro 970603
1895 // This func is bit problematic when it comes to NLS, to make the
1896 // lyx servers client be language indepenent we must not translate
1897 // strings sent to this func.
1898 void LyXFunc::setErrorMessage(string const & m) const
1899 {
1900         dispatch_buffer = m;
1901         errorstat = true;
1902 }
1903
1904
1905 void LyXFunc::setMessage(string const & m) const
1906 {
1907         dispatch_buffer = m;
1908 }
1909
1910
1911 void LyXFunc::setStatusMessage(string const & m) const
1912 {
1913         status_buffer = m;
1914 }
1915
1916
1917 void LyXFunc::initMiniBuffer()
1918 {
1919         string text = _("Welcome to LyX!");
1920
1921         // When meta-fake key is pressed, show the key sequence so far + "M-".
1922         if (wasMetaKey()) {
1923                 text = keyseq.print();
1924                 text += "M-";
1925         }
1926
1927         // Else, when a non-complete key sequence is pressed,
1928         // show the available options.
1929         if (keyseq.length() > 0 && !keyseq.deleted()) {
1930                 text = keyseq.printOptions();
1931         }
1932
1933         // Else, show the buffer state.
1934         else if (owner->view()->available()) {
1935                 Buffer * tmpbuf = owner->buffer();
1936
1937                 string const nicename =
1938                         MakeDisplayPath(tmpbuf->fileName());
1939                 // Should we do this instead? (kindo like emacs)
1940                 // leaves more room for other information
1941                 text = "LyX: ";
1942                 text += nicename;
1943                 if (tmpbuf->lyxvc.inUse()) {
1944                         text += " [";
1945                         text += tmpbuf->lyxvc.versionString();
1946                         text += ' ';
1947                         text += tmpbuf->lyxvc.locker();
1948                         if (tmpbuf->isReadonly())
1949                                 text += " (RO)";
1950                         text += ']';
1951                 } else if (tmpbuf->isReadonly())
1952                         text += " [RO]";
1953                 if (!tmpbuf->isLyxClean())
1954                         text += _(" (Changed)");
1955         } else {
1956                 if (text != _("Welcome to LyX!")) // this is a hack
1957                         text = _("* No document open *");
1958         }
1959
1960         owner->message(text);
1961 }