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