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