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