]> git.lyx.org Git - lyx.git/blob - src/lyxfunc.C
more cursor dispatch
[lyx.git] / src / lyxfunc.C
1 /**
2  * \file lyxfunc.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Allan Rae
13  * \author Dekel Tsur
14  * \author Martin Vermeer
15  * \author Jürgen Vigna
16  *
17  * Full author contact details are available in file CREDITS.
18  */
19
20 #include <config.h>
21
22 #include "lyxfunc.h"
23
24 #include "BranchList.h"
25 #include "buffer.h"
26 #include "buffer_funcs.h"
27 #include "bufferlist.h"
28 #include "bufferparams.h"
29 #include "BufferView.h"
30 #include "cursor.h"
31 #include "debug.h"
32 #include "dispatchresult.h"
33 #include "encoding.h"
34 #include "exporter.h"
35 #include "format.h"
36 #include "funcrequest.h"
37 #include "gettext.h"
38 #include "importer.h"
39 #include "intl.h"
40 #include "iterators.h"
41 #include "kbmap.h"
42 #include "LColor.h"
43 #include "lyx_cb.h"
44 #include "LyXAction.h"
45 #include "lyxfind.h"
46 #include "lyxrc.h"
47 #include "lyxrow.h"
48 #include "lyxserver.h"
49 #include "lyxvc.h"
50 #include "paragraph.h"
51 #include "ParagraphParameters.h"
52 #include "undo.h"
53
54 #include "insets/insetbox.h"
55 #include "insets/insetbranch.h"
56 #include "insets/insetcommand.h"
57 #include "insets/insetert.h"
58 #include "insets/insetexternal.h"
59 #include "insets/insetfloat.h"
60 #include "insets/insetgraphics.h"
61 #include "insets/insetnote.h"
62 #include "insets/insettabular.h"
63 #include "insets/insetvspace.h"
64 #include "insets/insetwrap.h"
65
66 #include "frontends/Alert.h"
67 #include "frontends/Dialogs.h"
68 #include "frontends/FileDialog.h"
69 #include "frontends/lyx_gui.h"
70 #include "frontends/LyXKeySym.h"
71 #include "frontends/LyXView.h"
72 #include "frontends/Menubar.h"
73 #include "frontends/Toolbar.h"
74
75 #include "support/FileInfo.h"
76 #include "support/filetools.h"
77 #include "support/forkedcontr.h"
78 #include "support/globbing.h"
79 #include "support/path.h"
80 #include "support/path_defines.h"
81 #include "support/tostr.h"
82 #include "support/std_sstream.h"
83 #include "support/os.h"
84
85 using bv_funcs::changeDepth;
86 using bv_funcs::currentState;
87 using bv_funcs::DEC_DEPTH;
88 using bv_funcs::freefont2string;
89 using bv_funcs::INC_DEPTH;
90
91 using lyx::support::AddName;
92 using lyx::support::AddPath;
93 using lyx::support::bformat;
94 using lyx::support::ChangeExtension;
95 using lyx::support::FileFilterList;
96 using lyx::support::FileInfo;
97 using lyx::support::FileSearch;
98 using lyx::support::ForkedcallsController;
99 using lyx::support::i18nLibFileSearch;
100 using lyx::support::IsDirWriteable;
101 using lyx::support::IsFileReadable;
102 using lyx::support::isStrInt;
103 using lyx::support::MakeAbsPath;
104 using lyx::support::MakeDisplayPath;
105 using lyx::support::Path;
106 using lyx::support::rtrim;
107 using lyx::support::split;
108 using lyx::support::strToInt;
109 using lyx::support::strToUnsignedInt;
110 using lyx::support::system_lyxdir;
111 using lyx::support::token;
112 using lyx::support::trim;
113 using lyx::support::user_lyxdir;
114 using lyx::support::prefixIs;
115 using lyx::support::os::getTmpDir;
116
117 using std::endl;
118 using std::make_pair;
119 using std::pair;
120 using std::string;
121 using std::istringstream;
122
123
124 extern BufferList bufferlist;
125 extern LyXServer * lyxserver;
126 extern bool selection_possible;
127
128 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
129
130 // (alkis)
131 extern tex_accent_struct get_accent(kb_action action);
132
133
134 LyXFunc::LyXFunc(LyXView * lv)
135         : owner(lv),
136         encoded_last_key(0),
137         keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
138         cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
139         meta_fake_bit(key_modifier::none)
140 {
141 }
142
143
144 void LyXFunc::handleKeyFunc(kb_action action)
145 {
146         char c = encoded_last_key;
147
148         if (keyseq.length()) {
149                 c = 0;
150         }
151
152         owner->getIntl().getTransManager()
153                 .deadkey(c, get_accent(action).accent, view()->getLyXText());
154         // Need to clear, in case the minibuffer calls these
155         // actions
156         keyseq.clear();
157         // copied verbatim from do_accent_char
158         view()->cursor().resetAnchor();
159         view()->update();
160 }
161
162
163 void LyXFunc::processKeySym(LyXKeySymPtr keysym, key_modifier::state state)
164 {
165         lyxerr[Debug::KEY] << "KeySym is " << keysym->getSymbolName() << endl;
166
167         // Do nothing if we have nothing (JMarc)
168         if (!keysym->isOK()) {
169                 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
170                                    << endl;
171                 return;
172         }
173
174         if (keysym->isModifier()) {
175                 lyxerr[Debug::KEY] << "isModifier true" << endl;
176                 return;
177         }
178
179         Encoding const * encoding = view()->getEncoding();
180
181         encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
182
183         // Do a one-deep top-level lookup for
184         // cancel and meta-fake keys. RVDK_PATCH_5
185         cancel_meta_seq.reset();
186
187         FuncRequest func = cancel_meta_seq.addkey(keysym, state);
188         lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
189
190         // When not cancel or meta-fake, do the normal lookup.
191         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
192         // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
193         if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
194                 // remove Caps Lock and Mod2 as a modifiers
195                 func = keyseq.addkey(keysym, (state | meta_fake_bit));
196                 lyxerr[Debug::KEY] << "action now set to ["
197                         << func.action << ']' << endl;
198         }
199
200         // Dont remove this unless you know what you are doing.
201         meta_fake_bit = key_modifier::none;
202
203         // can this happen now ?
204         if (func.action == LFUN_NOACTION) {
205                 func = FuncRequest(LFUN_PREFIX);
206         }
207
208         if (lyxerr.debugging(Debug::KEY)) {
209                 lyxerr << "Key [action="
210                        << func.action << "]["
211                        << keyseq.print() << ']'
212                        << endl;
213         }
214
215         // already here we know if it any point in going further
216         // why not return already here if action == -1 and
217         // num_bytes == 0? (Lgb)
218
219         if (keyseq.length() > 1) {
220                 owner->message(keyseq.print());
221         }
222
223
224         // Maybe user can only reach the key via holding down shift.
225         // Let's see. But only if shift is the only modifier
226         if (func.action == LFUN_UNKNOWN_ACTION &&
227             state == key_modifier::shift) {
228                 lyxerr[Debug::KEY] << "Trying without shift" << endl;
229                 func = keyseq.addkey(keysym, key_modifier::none);
230                 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
231         }
232
233         if (func.action == LFUN_UNKNOWN_ACTION) {
234                 // Hmm, we didn't match any of the keysequences. See
235                 // if it's normal insertable text not already covered
236                 // by a binding
237                 if (keysym->isText() && keyseq.length() == 1) {
238                         lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
239                         func = FuncRequest(LFUN_SELFINSERT);
240                 } else {
241                         lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
242                         owner->message(_("Unknown function."));
243                         return;
244                 }
245         }
246
247         if (func.action == LFUN_SELFINSERT) {
248                 if (encoded_last_key != 0) {
249                         string arg;
250                         arg += encoded_last_key;
251                         lyxerr << "SelfInsert arg[`"
252                                    << arg << "']" << endl;
253                         dispatch(FuncRequest(LFUN_SELFINSERT, arg));
254                         lyxerr[Debug::KEY]
255                                 << "SelfInsert arg[`" << arg << "']" << endl;
256                 }
257         } else {
258                 dispatch(func);
259         }
260 }
261
262
263 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
264 {
265         FuncStatus flag;
266         Buffer * buf = owner->buffer();
267
268         if (ev.action == LFUN_NOACTION) {
269                 setStatusMessage(N_("Nothing to do"));
270                 flag.disabled(true);
271                 return flag;
272         }
273
274         switch (ev.action) {
275         case LFUN_UNKNOWN_ACTION:
276 #ifndef HAVE_LIBAIKSAURUS
277         case LFUN_THESAURUS_ENTRY:
278 #endif
279                 flag.unknown(true);
280                 flag.disabled(true);
281                 break;
282         default:
283                 flag |= lyx_gui::getStatus(ev);
284         }
285
286         if (flag.unknown()) {
287                 setStatusMessage(N_("Unknown action"));
288                 return flag;
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                         flag.disabled(true);
312                         return flag;
313                 }
314         }
315
316         UpdatableInset * tli = view()->cursor().inset()
317                 ? view()->cursor().inset()->asUpdatableInset() : 0;
318         InsetTabular * tab = view()->cursor().innerInsetTabular();
319
320         // I would really like to avoid having this switch and rather try to
321         // encode this in the function itself.
322         bool disable = false;
323         switch (ev.action) {
324         case LFUN_EXPORT:
325                 disable = ev.argument != "custom"
326                         && !Exporter::IsExportable(*buf, ev.argument);
327                 break;
328         case LFUN_UNDO:
329                 disable = buf->undostack().empty();
330                 break;
331         case LFUN_REDO:
332                 disable = buf->redostack().empty();
333                 break;
334         case LFUN_CUT:
335         case LFUN_COPY:
336                 if (tab && tab->hasSelection())
337                         disable = false;
338                 else
339                         disable = !view()->cursor().inMathed() && !view()->cursor().selection();
340                 break;
341
342         case LFUN_RUNCHKTEX:
343                 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
344                 break;
345
346         case LFUN_BUILDPROG:
347                 disable = !Exporter::IsExportable(*buf, "program");
348                 break;
349
350         case LFUN_LAYOUT_TABULAR:
351                 disable = !view()->cursor().innerInsetTabular();
352                 break;
353
354         case LFUN_DEPTH_MIN:
355                 disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH);
356                 break;
357
358         case LFUN_DEPTH_PLUS:
359                 disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH);
360                 break;
361
362         case LFUN_LAYOUT:
363         case LFUN_LAYOUT_PARAGRAPH: {
364                 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
365                 disable = inset && inset->forceDefaultParagraphs(inset);
366                 break;
367         }
368
369         case LFUN_INSET_OPTARG:
370                 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
371                 break;
372
373         case LFUN_TABULAR_FEATURE:
374 #if 0
375                 if (view()->cursor().inMathed()) {
376                         // FIXME: check temporarily disabled
377                         // valign code
378                         char align = mathcursor::valign();
379                         if (align == '\0') {
380                                 disable = true;
381                                 break;
382                         }
383                         if (ev.argument.empty()) {
384                                 flag.clear();
385                                 break;
386                         }
387                         if (!contains("tcb", ev.argument[0])) {
388                                 disable = true;
389                                 break;
390                         }
391                         flag.setOnOff(ev.argument[0] == align);
392                 } else {
393                         disable = true;
394
395                         char align = mathcursor::halign();
396                         if (align == '\0') {
397                                 disable = true;
398                                 break;
399                         }
400                         if (ev.argument.empty()) {
401                                 flag.clear();
402                                 break;
403                         }
404                         if (!contains("lcr", ev.argument[0])) {
405                                 disable = true;
406                                 break;
407                         }
408                         flag.setOnOff(ev.argument[0] == align);
409
410                         disable = !mathcursor::halign();
411                         break;
412                 }
413
414                 if (tli) {
415                         FuncStatus ret;
416                         //ret.disabled(true);
417                         InsetTabular * tab = view()->cursor().innerInsetTabular();
418                         if (tab) {
419                                 ret = tab->getStatus(ev.argument);
420                                 flag |= ret;
421                                 disable = false;
422                         } else {
423                                 disable = true;
424                         }
425                 } else {
426                         static InsetTabular inset(*buf, 1, 1);
427                         disable = true;
428                         FuncStatus ret = inset.getStatus(ev.argument);
429                         if (ret.onoff(true) || ret.onoff(false))
430                                 flag.setOnOff(false);
431                 }
432 #endif
433                 break;
434
435         case LFUN_VC_REGISTER:
436                 disable = buf->lyxvc().inUse();
437                 break;
438         case LFUN_VC_CHECKIN:
439                 disable = !buf->lyxvc().inUse() || buf->isReadonly();
440                 break;
441         case LFUN_VC_CHECKOUT:
442                 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
443                 break;
444         case LFUN_VC_REVERT:
445         case LFUN_VC_UNDO:
446                 disable = !buf->lyxvc().inUse();
447                 break;
448         case LFUN_MENURELOAD:
449                 disable = buf->isUnnamed() || buf->isClean();
450                 break;
451         case LFUN_BOOKMARK_GOTO:
452                 disable = !view()->
453                         isSavedPosition(strToUnsignedInt(ev.argument));
454                 break;
455
456         case LFUN_MERGE_CHANGES:
457         case LFUN_ACCEPT_CHANGE:
458         case LFUN_REJECT_CHANGE:
459         case LFUN_ACCEPT_ALL_CHANGES:
460         case LFUN_REJECT_ALL_CHANGES:
461                 disable = !buf->params().tracking_changes;
462                 break;
463
464         case LFUN_INSET_SETTINGS: {
465                 disable = true;
466                 UpdatableInset * inset = view()->cursor().inset()
467                         ? view()->cursor().inset()->asUpdatableInset() : 0;
468
469                 if (!inset)
470                         break;
471
472                 // jump back to owner if an InsetText, so
473                 // we get back to the InsetTabular or whatever
474                 if (inset->lyxCode() == InsetOld::TEXT_CODE)
475                         inset = inset->owner();
476
477                 InsetOld::Code code = inset->lyxCode();
478                 switch (code) {
479                         case InsetOld::TABULAR_CODE:
480                                 disable = ev.argument != "tabular";
481                                 break;
482                         case InsetOld::ERT_CODE:
483                                 disable = ev.argument != "ert";
484                                 break;
485                         case InsetOld::FLOAT_CODE:
486                                 disable = ev.argument != "float";
487                                 break;
488                         case InsetOld::WRAP_CODE:
489                                 disable = ev.argument != "wrap";
490                                 break;
491                         case InsetOld::NOTE_CODE:
492                                 disable = ev.argument != "note";
493                                 break;
494                         case InsetOld::BRANCH_CODE:
495                                 disable = ev.argument != "branch";
496                                 break;
497                         case InsetOld::BOX_CODE:
498                                 disable = ev.argument != "box";
499                                 break;
500                         default:
501                                 break;
502                 }
503                 break;
504         }
505
506         case LFUN_MATH_MUTATE:
507                 if (view()->cursor().inMathed())
508                         //flag.setOnOff(mathcursor::formula()->hullType() == ev.argument);
509                         flag.setOnOff(false);
510                 else
511                         disable = true;
512                 break;
513
514         // we just need to be in math mode to enable that
515         case LFUN_MATH_SIZE:
516         case LFUN_MATH_SPACE:
517         case LFUN_MATH_LIMITS:
518         case LFUN_MATH_NONUMBER:
519         case LFUN_MATH_NUMBER:
520         case LFUN_MATH_EXTERN:
521                 disable = !view()->cursor().inMathed();
522                 break;
523
524         case LFUN_DIALOG_SHOW: {
525                 string const name = ev.getArg(0);
526                 if (!buf) {
527                         disable = !(name == "aboutlyx" ||
528                                     name == "file" ||
529                                     name == "forks" ||
530                                     name == "preferences" ||
531                                     name == "texinfo");
532                         break;
533                 }
534
535                 if (name == "print") {
536                         disable = !Exporter::IsExportable(*buf, "dvi") ||
537                                 lyxrc.print_command == "none";
538                 } else if (name == "character") {
539                         InsetBase * inset = view()->cursor().inset();
540                         disable = inset && inset->lyxCode() == InsetOld::ERT_CODE;
541                 } else if (name == "vclog") {
542                         disable = !buf->lyxvc().inUse();
543                 } else if (name == "latexlog") {
544                         disable = !IsFileReadable(buf->getLogName().second);
545                 }
546                 break;
547         }
548
549         default:
550                 break;
551         }
552
553         // the functions which insert insets
554         InsetOld::Code code = InsetOld::NO_CODE;
555         switch (ev.action) {
556         case LFUN_DIALOG_SHOW_NEW_INSET:
557                 if (ev.argument == "bibitem")
558                         code = InsetOld::BIBITEM_CODE;
559                 else if (ev.argument == "bibtex")
560                         code = InsetOld::BIBTEX_CODE;
561                 else if (ev.argument == "box")
562                         code = InsetOld::BOX_CODE;
563                 else if (ev.argument == "branch")
564                         code = InsetOld::BRANCH_CODE;
565                 else if (ev.argument == "citation")
566                         code = InsetOld::CITE_CODE;
567                 else if (ev.argument == "ert")
568                         code = InsetOld::ERT_CODE;
569                 else if (ev.argument == "external")
570                         code = InsetOld::EXTERNAL_CODE;
571                 else if (ev.argument == "float")
572                         code = InsetOld::FLOAT_CODE;
573                 else if (ev.argument == "graphics")
574                         code = InsetOld::GRAPHICS_CODE;
575                 else if (ev.argument == "include")
576                         code = InsetOld::INCLUDE_CODE;
577                 else if (ev.argument == "index")
578                         code = InsetOld::INDEX_CODE;
579                 else if (ev.argument == "label")
580                         code = InsetOld::LABEL_CODE;
581                 else if (ev.argument == "note")
582                         code = InsetOld::NOTE_CODE;
583                 else if (ev.argument == "ref")
584                         code = InsetOld::REF_CODE;
585                 else if (ev.argument == "toc")
586                         code = InsetOld::TOC_CODE;
587                 else if (ev.argument == "url")
588                         code = InsetOld::URL_CODE;
589                 else if (ev.argument == "vspace")
590                         code = InsetOld::VSPACE_CODE;
591                 else if (ev.argument == "wrap")
592                         code = InsetOld::WRAP_CODE;
593                 break;
594
595         case LFUN_INSET_ERT:
596                 code = InsetOld::ERT_CODE;
597                 break;
598         case LFUN_INSET_FOOTNOTE:
599                 code = InsetOld::FOOT_CODE;
600                 break;
601         case LFUN_TABULAR_INSERT:
602                 code = InsetOld::TABULAR_CODE;
603                 break;
604         case LFUN_INSET_MARGINAL:
605                 code = InsetOld::MARGIN_CODE;
606                 break;
607         case LFUN_INSET_FLOAT:
608         case LFUN_INSET_WIDE_FLOAT:
609                 code = InsetOld::FLOAT_CODE;
610                 break;
611         case LFUN_INSET_WRAP:
612                 code = InsetOld::WRAP_CODE;
613                 break;
614         case LFUN_FLOAT_LIST:
615                 code = InsetOld::FLOAT_LIST_CODE;
616                 break;
617 #if 0
618         case LFUN_INSET_LIST:
619                 code = InsetOld::LIST_CODE;
620                 break;
621         case LFUN_INSET_THEOREM:
622                 code = InsetOld::THEOREM_CODE;
623                 break;
624 #endif
625         case LFUN_INSET_CAPTION:
626                 code = InsetOld::CAPTION_CODE;
627                 break;
628         case LFUN_INSERT_NOTE:
629                 code = InsetOld::NOTE_CODE;
630                 break;
631         case LFUN_INSERT_CHARSTYLE:
632                 code = InsetOld::CHARSTYLE_CODE;
633                 if (buf->params().getLyXTextClass().charstyles().empty())
634                         disable = true;
635                 break;
636         case LFUN_INSERT_BOX:
637                 code = InsetOld::BOX_CODE;
638                 break;
639         case LFUN_INSERT_BRANCH:
640                 code = InsetOld::BRANCH_CODE;
641                 if (buf->params().branchlist().empty())
642                         disable = true;
643                 break;
644         case LFUN_INSERT_LABEL:
645                 code = InsetOld::LABEL_CODE;
646                 break;
647         case LFUN_INSET_OPTARG:
648                 code = InsetOld::OPTARG_CODE;
649                 break;
650         case LFUN_ENVIRONMENT_INSERT:
651                 code = InsetOld::BOX_CODE;
652                 break;
653         case LFUN_INDEX_INSERT:
654                 code = InsetOld::INDEX_CODE;
655                 break;
656         case LFUN_INDEX_PRINT:
657                 code = InsetOld::INDEX_PRINT_CODE;
658                 break;
659         case LFUN_TOC_INSERT:
660                 code = InsetOld::TOC_CODE;
661                 break;
662         case LFUN_HTMLURL:
663         case LFUN_URL:
664                 code = InsetOld::URL_CODE;
665                 break;
666         case LFUN_QUOTE:
667                 // always allow this, since we will inset a raw quote
668                 // if an inset is not allowed.
669                 break;
670         case LFUN_HYPHENATION:
671         case LFUN_LIGATURE_BREAK:
672         case LFUN_HFILL:
673         case LFUN_MENU_SEPARATOR:
674         case LFUN_LDOTS:
675         case LFUN_END_OF_SENTENCE:
676                 code = InsetOld::SPECIALCHAR_CODE;
677                 break;
678         case LFUN_SPACE_INSERT:
679                 // slight hack: we know this is allowed in math mode
680                 if (!view()->cursor().inMathed())
681                         code = InsetOld::SPACE_CODE;
682                 break;
683         case LFUN_INSET_DIALOG_SHOW: {
684                 InsetBase * inset = view()->cursor().nextInset();
685                 disable = !inset;
686                 if (inset) {
687                         code = inset->lyxCode();
688                         if (!(code == InsetOld::INCLUDE_CODE
689                                 || code == InsetOld::BIBTEX_CODE
690                                 || code == InsetOld::FLOAT_LIST_CODE
691                                 || code == InsetOld::TOC_CODE))
692                                 disable = true;
693                 }
694                 break;
695         }
696         default:
697                 break;
698         }
699         if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
700                 disable = true;
701
702         if (disable)
703                 flag.disabled(true);
704
705         // A few general toggles
706         switch (ev.action) {
707         case LFUN_TOOLTIPS_TOGGLE:
708                 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
709                 break;
710
711         case LFUN_READ_ONLY_TOGGLE:
712                 flag.setOnOff(buf->isReadonly());
713                 break;
714         case LFUN_APPENDIX:
715                 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
716                 break;
717         case LFUN_SWITCHBUFFER:
718                 // toggle on the current buffer, but do not toggle off
719                 // the other ones (is that a good idea?)
720                 if (ev.argument == buf->fileName())
721                         flag.setOnOff(true);
722                 break;
723         case LFUN_TRACK_CHANGES:
724                 flag.setOnOff(buf->params().tracking_changes);
725                 break;
726         default:
727                 break;
728         }
729
730 #ifdef LOCK
731         // the font related toggles
732         if (!view()->cursor().inMathed()) {
733                 LyXFont const & font = view()->getLyXText()->real_current_font;
734                 switch (ev.action) {
735                 case LFUN_EMPH:
736                         flag.setOnOff(font.emph() == LyXFont::ON);
737                         break;
738                 case LFUN_NOUN:
739                         flag.setOnOff(font.noun() == LyXFont::ON);
740                         break;
741                 case LFUN_BOLD:
742                         flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
743                         break;
744                 case LFUN_SANS:
745                         flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
746                         break;
747                 case LFUN_ROMAN:
748                         flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
749                         break;
750                 case LFUN_CODE:
751                         flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
752                         break;
753                 default:
754                         break;
755                 }
756         } else {
757                 string tc = mathcursor::getLastCode();
758                 switch (ev.action) {
759                 case LFUN_BOLD:
760                         flag.setOnOff(tc == "mathbf");
761                         break;
762                 case LFUN_SANS:
763                         flag.setOnOff(tc == "mathsf");
764                         break;
765                 case LFUN_EMPH:
766                         flag.setOnOff(tc == "mathcal");
767                         break;
768                 case LFUN_ROMAN:
769                         flag.setOnOff(tc == "mathrm");
770                         break;
771                 case LFUN_CODE:
772                         flag.setOnOff(tc == "mathtt");
773                         break;
774                 case LFUN_NOUN:
775                         flag.setOnOff(tc == "mathbb");
776                         break;
777                 case LFUN_DEFAULT:
778                         flag.setOnOff(tc == "mathnormal");
779                         break;
780                 default:
781                         break;
782                 }
783         }
784 #endif
785
786         // this one is difficult to get right. As a half-baked
787         // solution, we consider only the first action of the sequence
788         if (ev.action == LFUN_SEQUENCE) {
789                 // argument contains ';'-terminated commands
790 #warning LyXAction arguments not handled here.
791                 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
792         }
793
794         return flag;
795 }
796
797
798 namespace {
799
800 bool ensureBufferClean(BufferView * bv)
801 {
802         Buffer & buf = *bv->buffer();
803         if (buf.isClean())
804                 return true;
805
806         string const file = MakeDisplayPath(buf.fileName(), 30);
807         string text = bformat(_("The document %1$s has unsaved "
808                                 "changes.\n\nDo you want to save "
809                                 "the document?"), file);
810         int const ret = Alert::prompt(_("Save changed document?"),
811                                       text, 0, 1, _("&Save"),
812                                       _("&Cancel"));
813
814         if (ret == 0)
815                 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
816
817         return buf.isClean();
818 }
819
820 } //namespace anon
821
822
823 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
824 {
825         string argument = cmd.argument;
826         kb_action action = cmd.action;
827
828         lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
829         //lyxerr << "*** LyXFunc::dispatch: cmd: " << cmd << endl;
830
831         // we have not done anything wrong yet.
832         errorstat = false;
833         dispatch_buffer.erase();
834         selection_possible = false;
835
836         // We cannot use this function here
837         if (getStatus(cmd).disabled()) {
838                 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
839                        << lyxaction.getActionName(action)
840                        << " [" << action << "] is disabled at this location"
841                        << endl;
842                 setErrorMessage(getStatusMessage());
843
844         } else {
845
846                 if (view()->available())
847                         view()->hideCursor();
848
849                 switch (action) {
850
851                 case LFUN_ESCAPE: {
852                         if (!view()->available())
853                                 break;
854                         view()->cursor().pop();
855                         // Tell the paragraph dialog that we changed paragraph
856                         dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
857                         break;
858                 }
859
860                 case LFUN_WORDFINDFORWARD:
861                 case LFUN_WORDFINDBACKWARD: {
862                         static string last_search;
863                         string searched_string;
864
865                         if (!argument.empty()) {
866                                 last_search = argument;
867                                 searched_string = argument;
868                         } else
869                                 searched_string = last_search;
870
871                         if (searched_string.empty())
872                                 break;
873
874                         bool const fw = action == LFUN_WORDFINDFORWARD;
875                         string const data =
876                                 lyx::find::find2string(searched_string, true, false, fw);
877                         view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
878                         break;
879                 }
880
881                 case LFUN_PREFIX:
882                         owner->message(keyseq.printOptions());
883                         break;
884
885                 case LFUN_EXEC_COMMAND:
886                         owner->focus_command_buffer();
887                         break;
888
889                 case LFUN_CANCEL:
890                         keyseq.reset();
891                         meta_fake_bit = key_modifier::none;
892                         if (view()->available())
893                                 // cancel any selection
894                                 dispatch(FuncRequest(LFUN_MARK_OFF));
895                         setMessage(N_("Cancel"));
896                         break;
897
898                 case LFUN_META_FAKE:
899                         meta_fake_bit = key_modifier::alt;
900                         setMessage(keyseq.print());
901                         break;
902
903                 case LFUN_READ_ONLY_TOGGLE:
904                         if (owner->buffer()->lyxvc().inUse())
905                                 owner->buffer()->lyxvc().toggleReadOnly();
906                         else
907                                 owner->buffer()->setReadonly(
908                                         !owner->buffer()->isReadonly());
909                         break;
910
911                 case LFUN_CENTER: // this is center and redraw.
912                         view()->center();
913                         break;
914
915                 // --- Menus -----------------------------------------------
916                 case LFUN_MENUNEW:
917                         menuNew(argument, false);
918                         break;
919
920                 case LFUN_MENUNEWTMPLT:
921                         menuNew(argument, true);
922                         break;
923
924                 case LFUN_CLOSEBUFFER:
925                         closeBuffer();
926                         break;
927
928                 case LFUN_MENUWRITE:
929                         if (!owner->buffer()->isUnnamed()) {
930                                 string const str = bformat(_("Saving document %1$s..."),
931                                          MakeDisplayPath(owner->buffer()->fileName()));
932                                 owner->message(str);
933                                 MenuWrite(owner->buffer());
934                                 owner->message(str + _(" done."));
935                         } else
936                                 WriteAs(owner->buffer());
937                         break;
938
939                 case LFUN_WRITEAS:
940                         WriteAs(owner->buffer(), argument);
941                         break;
942
943                 case LFUN_MENURELOAD: {
944                         string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
945                         string text = bformat(_("Any changes will be lost. Are you sure "
946                                 "you want to revert to the saved version of the document %1$s?"), file);
947                         int const ret = Alert::prompt(_("Revert to saved document?"),
948                                 text, 0, 1, _("&Revert"), _("&Cancel"));
949
950                         if (ret == 0)
951                                 view()->reload();
952                         break;
953                 }
954
955                 case LFUN_UPDATE:
956                         Exporter::Export(owner->buffer(), argument, true);
957                         view()->showErrorList(BufferFormat(*owner->buffer()));
958                         break;
959
960                 case LFUN_PREVIEW:
961                         Exporter::Preview(owner->buffer(), argument);
962                         view()->showErrorList(BufferFormat(*owner->buffer()));
963                         break;
964
965                 case LFUN_BUILDPROG:
966                         Exporter::Export(owner->buffer(), "program", true);
967                         view()->showErrorList(_("Build"));
968                         break;
969
970                 case LFUN_RUNCHKTEX:
971                         owner->buffer()->runChktex();
972                         view()->showErrorList(_("ChkTeX"));
973                         break;
974
975                 case LFUN_EXPORT:
976                         if (argument == "custom")
977                                 owner->getDialogs().showSendto();
978                         else {
979                                 Exporter::Export(owner->buffer(), argument, false);
980                                 view()->showErrorList(BufferFormat(*owner->buffer()));
981                         }
982                         break;
983
984                 case LFUN_IMPORT:
985                         doImport(argument);
986                         break;
987
988                 case LFUN_QUIT:
989                         QuitLyX();
990                         break;
991
992                 case LFUN_TOCVIEW: {
993                         InsetCommandParams p("tableofcontents");
994                         string const data = InsetCommandMailer::params2string("toc", p);
995                         owner->getDialogs().show("toc", data, 0);
996                         break;
997                 }
998
999                 case LFUN_AUTOSAVE:
1000                         AutoSave(view());
1001                         break;
1002
1003                 case LFUN_UNDO:
1004                         view()->undo();
1005                         break;
1006
1007                 case LFUN_REDO:
1008                         view()->redo();
1009                         break;
1010
1011                 case LFUN_RECONFIGURE:
1012                         Reconfigure(view());
1013                         break;
1014
1015                 case LFUN_HELP_OPEN: {
1016                         string const arg = argument;
1017                         if (arg.empty()) {
1018                                 setErrorMessage(N_("Missing argument"));
1019                                 break;
1020                         }
1021                         string const fname = i18nLibFileSearch("doc", arg, "lyx");
1022                         if (fname.empty()) {
1023                                 lyxerr << "LyX: unable to find documentation file `"
1024                                                          << arg << "'. Bad installation?" << endl;
1025                                 break;
1026                         }
1027                         owner->message(bformat(_("Opening help file %1$s..."),
1028                                 MakeDisplayPath(fname)));
1029                         view()->loadLyXFile(fname, false);
1030                         break;
1031                 }
1032
1033                 // --- version control -------------------------------
1034                 case LFUN_VC_REGISTER:
1035                         if (!ensureBufferClean(view()))
1036                                 break;
1037                         if (!owner->buffer()->lyxvc().inUse()) {
1038                                 owner->buffer()->lyxvc().registrer();
1039                                 view()->reload();
1040                         }
1041                         break;
1042
1043                 case LFUN_VC_CHECKIN:
1044                         if (!ensureBufferClean(view()))
1045                                 break;
1046                         if (owner->buffer()->lyxvc().inUse()
1047                                         && !owner->buffer()->isReadonly()) {
1048                                 owner->buffer()->lyxvc().checkIn();
1049                                 view()->reload();
1050                         }
1051                         break;
1052
1053                 case LFUN_VC_CHECKOUT:
1054                         if (!ensureBufferClean(view()))
1055                                 break;
1056                         if (owner->buffer()->lyxvc().inUse()
1057                                         && owner->buffer()->isReadonly()) {
1058                                 owner->buffer()->lyxvc().checkOut();
1059                                 view()->reload();
1060                         }
1061                         break;
1062
1063                 case LFUN_VC_REVERT:
1064                         owner->buffer()->lyxvc().revert();
1065                         view()->reload();
1066                         break;
1067
1068                 case LFUN_VC_UNDO:
1069                         owner->buffer()->lyxvc().undoLast();
1070                         view()->reload();
1071                         break;
1072
1073                 // --- buffers ----------------------------------------
1074                 case LFUN_SWITCHBUFFER:
1075                         view()->buffer(bufferlist.getBuffer(argument));
1076                         break;
1077
1078                 case LFUN_FILE_NEW:
1079                         NewFile(view(), argument);
1080                         break;
1081
1082                 case LFUN_FILE_OPEN:
1083                         open(argument);
1084                         break;
1085
1086                 case LFUN_LAYOUT_TABULAR:
1087                         if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1088                                 tab->openLayoutDialog(view());
1089                         break;
1090
1091                 case LFUN_DROP_LAYOUTS_CHOICE:
1092                         owner->getToolbar().openLayoutList();
1093                         break;
1094
1095                 case LFUN_MENU_OPEN_BY_NAME:
1096                         owner->getMenubar().openByName(argument);
1097                         break;
1098
1099                 // --- lyxserver commands ----------------------------
1100                 case LFUN_GETNAME:
1101                         setMessage(owner->buffer()->fileName());
1102                         lyxerr[Debug::INFO] << "FNAME["
1103                                                          << owner->buffer()->fileName()
1104                                                          << "] " << endl;
1105                         break;
1106
1107                 case LFUN_NOTIFY:
1108                         dispatch_buffer = keyseq.print();
1109                         lyxserver->notifyClient(dispatch_buffer);
1110                         break;
1111
1112                 case LFUN_GOTOFILEROW: {
1113                         string file_name;
1114                         int row;
1115                         istringstream is(argument);
1116                         is >> file_name >> row;
1117                         if (prefixIs(file_name, getTmpDir())) {
1118                                 // Needed by inverse dvi search. If it is a file
1119                                 // in tmpdir, call the apropriated function
1120                                 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1121                         } else {
1122                                 // Must replace extension of the file to be .lyx
1123                                 // and get full path
1124                                 string const s = ChangeExtension(file_name, ".lyx");
1125                                 // Either change buffer or load the file
1126                                 if (bufferlist.exists(s)) {
1127                                         view()->buffer(bufferlist.getBuffer(s));
1128                                 } else {
1129                                         view()->loadLyXFile(s);
1130                                 }
1131                         }
1132
1133                         view()->setCursorFromRow(row);
1134
1135                         view()->center();
1136                         // see BufferView_pimpl::center()
1137                         view()->updateScrollbar();
1138                         break;
1139                 }
1140
1141                 case LFUN_GOTO_PARAGRAPH: {
1142                         istringstream is(argument);
1143                         int id;
1144                         is >> id;
1145                         ParIterator par = owner->buffer()->getParFromID(id);
1146                         if (par == owner->buffer()->par_iterator_end()) {
1147                                 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1148                                                                 << id << ']' << endl;
1149                                 break;
1150                         } else {
1151                                 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1152                                                                 << " found." << endl;
1153                         }
1154
1155                         // Set the cursor
1156                         view()->setCursor(par, 0);
1157
1158                         view()->switchKeyMap();
1159                         owner->view_state_changed();
1160
1161                         view()->center();
1162                         // see BufferView_pimpl::center()
1163                         view()->updateScrollbar();
1164                         break;
1165                 }
1166
1167                 // ---  Mathed stuff. If we are here, there is no locked inset yet.
1168                 case LFUN_MATH_EXTERN:
1169                 case LFUN_MATH_NUMBER:
1170                 case LFUN_MATH_NONUMBER:
1171                 case LFUN_MATH_LIMITS:
1172                         setErrorMessage(N_("This is only allowed in math mode!"));
1173                         break;
1174
1175                 case LFUN_DIALOG_SHOW: {
1176                         string const name = cmd.getArg(0);
1177                         string data = trim(cmd.argument.substr(name.size()));
1178
1179                         if (name == "character") {
1180                                 data = freefont2string();
1181                                 if (!data.empty())
1182                                         owner->getDialogs().show("character", data);
1183                         }
1184                         else if (name == "document")
1185                                 owner->getDialogs().showDocument();
1186                         else if (name == "forks")
1187                                 owner->getDialogs().showForks();
1188                         else if (name == "preamble")
1189                                 owner->getDialogs().showPreamble();
1190                         else if (name == "preferences")
1191                                 owner->getDialogs().showPreferences();
1192                         else if (name == "print")
1193                                 owner->getDialogs().showPrint();
1194                         else if (name == "spellchecker")
1195                                 owner->getDialogs().showSpellchecker();
1196
1197                         else if (name == "latexlog") {
1198                                 pair<Buffer::LogType, string> const logfile =
1199                                         owner->buffer()->getLogName();
1200                                 switch (logfile.first) {
1201                                 case Buffer::latexlog:
1202                                         data = "latex ";
1203                                         break;
1204                                 case Buffer::buildlog:
1205                                         data = "literate ";
1206                                         break;
1207                                 }
1208                                 data += logfile.second;
1209                                 owner->getDialogs().show("log", data);
1210                         }
1211                         else if (name == "vclog") {
1212                                 string const data = "vc " +
1213                                         owner->buffer()->lyxvc().getLogFile();
1214                                 owner->getDialogs().show("log", data);
1215                         }
1216                         else
1217                                 owner->getDialogs().show(name, data);
1218                         break;
1219                 }
1220
1221                 case LFUN_DIALOG_SHOW_NEW_INSET: {
1222                         string const name = cmd.getArg(0);
1223                         string data = trim(cmd.argument.substr(name.size()));
1224                         if (name == "bibitem" ||
1225                             name == "bibtex" ||
1226                             name == "include" ||
1227                             name == "index" ||
1228                             name == "label" ||
1229                             name == "ref" ||
1230                             name == "toc" ||
1231                             name == "url") {
1232                                 InsetCommandParams p(name);
1233                                 data = InsetCommandMailer::params2string(name, p);
1234                         } else if (name == "box") {
1235                                 // \c data == "Boxed" || "Frameless" etc
1236                                 InsetBoxParams p(data);
1237                                 data = InsetBoxMailer::params2string(p);
1238                         } else if (name == "branch") {
1239                                 InsetBranchParams p;
1240                                 data = InsetBranchMailer::params2string(p);
1241                         } else if (name == "citation") {
1242                                 InsetCommandParams p("cite");
1243                                 data = InsetCommandMailer::params2string(name, p);
1244                         } else if (name == "ert") {
1245                                 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1246                         } else if (name == "external") {
1247                                 InsetExternalParams p;
1248                                 Buffer const & buffer = *owner->buffer();
1249                                 data = InsetExternalMailer::params2string(p, buffer);
1250                         } else if (name == "float") {
1251                                 InsetFloatParams p;
1252                                 data = InsetFloatMailer::params2string(p);
1253                         } else if (name == "graphics") {
1254                                 InsetGraphicsParams p;
1255                                 Buffer const & buffer = *owner->buffer();
1256                                 data = InsetGraphicsMailer::params2string(p, buffer);
1257                         } else if (name == "note") {
1258                                 InsetNoteParams p;
1259                                 data = InsetNoteMailer::params2string(p);
1260                         } else if (name == "vspace") {
1261                                 VSpace space;
1262                                 data = InsetVSpaceMailer::params2string(space);
1263                         } else if (name == "wrap") {
1264                                 InsetWrapParams p;
1265                                 data = InsetWrapMailer::params2string(p);
1266                         }
1267                         owner->getDialogs().show(name, data, 0);
1268                         break;
1269                 }
1270
1271                 case LFUN_DIALOG_SHOW_NEXT_INSET:
1272                         break;
1273
1274                 case LFUN_INSET_DIALOG_SHOW: {
1275                         InsetBase * inset = view()->cursor().nextInset();
1276                         if (inset)
1277                                 inset->dispatch(view()->cursor(), FuncRequest(LFUN_INSET_DIALOG_SHOW));
1278                         break;
1279                 }
1280
1281                 case LFUN_DIALOG_UPDATE: {
1282                         string const & name = argument;
1283                         // Can only update a dialog connected to an existing inset
1284                         InsetBase * inset = owner->getDialogs().getOpenInset(name);
1285                         if (inset) {
1286                                 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
1287                                 inset->dispatch(view()->cursor(), fr);
1288                         } else if (name == "paragraph") {
1289                                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1290                         }
1291                         break;
1292                 }
1293
1294                 case LFUN_DIALOG_HIDE:
1295                         Dialogs::hide(argument, 0);
1296                         break;
1297
1298                 case LFUN_DIALOG_DISCONNECT_INSET:
1299                         owner->getDialogs().disconnect(argument);
1300                         break;
1301
1302                 case LFUN_CHILDOPEN: {
1303                         string const filename =
1304                                 MakeAbsPath(argument, owner->buffer()->filePath());
1305                         setMessage(N_("Opening child document ") +
1306                                          MakeDisplayPath(filename) + "...");
1307                         view()->savePosition(0);
1308                         string const parentfilename = owner->buffer()->fileName();
1309                         if (bufferlist.exists(filename))
1310                                 view()->buffer(bufferlist.getBuffer(filename));
1311                         else
1312                                 view()->loadLyXFile(filename);
1313                         // Set the parent name of the child document.
1314                         // This makes insertion of citations and references in the child work,
1315                         // when the target is in the parent or another child document.
1316                         owner->buffer()->setParentName(parentfilename);
1317                         break;
1318                 }
1319
1320                 case LFUN_TOGGLECURSORFOLLOW:
1321                         lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1322                         break;
1323
1324                 case LFUN_KMAP_OFF:
1325                         owner->getIntl().KeyMapOn(false);
1326                         break;
1327
1328                 case LFUN_KMAP_PRIM:
1329                         owner->getIntl().KeyMapPrim();
1330                         break;
1331
1332                 case LFUN_KMAP_SEC:
1333                         owner->getIntl().KeyMapSec();
1334                         break;
1335
1336                 case LFUN_KMAP_TOGGLE:
1337                         owner->getIntl().ToggleKeyMap();
1338                         break;
1339
1340                 case LFUN_REPEAT: {
1341                         // repeat command
1342                         string countstr;
1343                         argument = split(argument, countstr, ' ');
1344                         istringstream is(countstr);
1345                         int count = 0;
1346                         is >> count;
1347                         lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1348                         for (int i = 0; i < count; ++i)
1349                                 dispatch(lyxaction.lookupFunc(argument));
1350                         break;
1351                 }
1352
1353                 case LFUN_SEQUENCE:
1354                         // argument contains ';'-terminated commands
1355                         while (!argument.empty()) {
1356                                 string first;
1357                                 argument = split(argument, first, ';');
1358                                 dispatch(lyxaction.lookupFunc(first));
1359                         }
1360                         break;
1361
1362                 case LFUN_SAVEPREFERENCES: {
1363                         Path p(user_lyxdir());
1364                         lyxrc.write("preferences");
1365                         break;
1366                 }
1367
1368                 case LFUN_SCREEN_FONT_UPDATE:
1369                         // handle the screen font changes.
1370                         lyxrc.set_font_norm_type();
1371                         lyx_gui::update_fonts();
1372                         // All visible buffers will need resize
1373                         view()->resize();
1374                         break;
1375
1376                 case LFUN_SET_COLOR: {
1377                         string lyx_name;
1378                         string const x11_name = split(argument, lyx_name, ' ');
1379                         if (lyx_name.empty() || x11_name.empty()) {
1380                                 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1381                                                         " <x11_name>"));
1382                                 break;
1383                         }
1384
1385                         bool const graphicsbg_changed =
1386                                 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1387                                  x11_name != lcolor.getX11Name(LColor::graphicsbg));
1388
1389                         LColor::color col = lcolor.getFromLyXName(lyx_name);
1390                         if (!lcolor.setColor(col, x11_name)) {
1391                                 setErrorMessage(
1392                                         bformat(_("Set-color \"%1$s\" failed "
1393                                                                 "- color is undefined or "
1394                                                                 "may not be redefined"), lyx_name));
1395                                 break;
1396                         }
1397
1398                         lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1399
1400                         if (graphicsbg_changed) {
1401 #ifdef WITH_WARNINGS
1402 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1403 #endif
1404 #if 0
1405                                 lyx::graphics::GCache::get().changeDisplay(true);
1406 #endif
1407                         }
1408                         break;
1409                 }
1410
1411                 case LFUN_MESSAGE:
1412                         owner->message(argument);
1413                         break;
1414
1415                 case LFUN_FORKS_KILL:
1416                         if (isStrInt(argument))
1417                                 ForkedcallsController::get().kill(strToInt(argument));
1418                         break;
1419
1420                 case LFUN_TOOLTIPS_TOGGLE:
1421                         owner->getDialogs().toggleTooltips();
1422                         break;
1423
1424                 case LFUN_EXTERNAL_EDIT:
1425                         InsetExternal().dispatch(view()->cursor(), FuncRequest(action, argument));
1426                         break;
1427
1428                 default:
1429                         DispatchResult res = view()->cursor().dispatch(cmd);
1430                         if (!res.dispatched());
1431                                 view()->dispatch(cmd);
1432                         break;
1433                 }
1434         }
1435
1436         if (!view()->cursor().inMathed())
1437                 view()->owner()->updateLayoutChoice();
1438
1439         if (view()->available()) {
1440                 view()->fitCursor();
1441                 view()->update();
1442                 view()->cursor().updatePos();
1443                 // if we executed a mutating lfun, mark the buffer as dirty
1444                 if (!getStatus(cmd).disabled()
1445                     && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1446                     && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1447                         view()->buffer()->markDirty();
1448         }
1449
1450         if (!view()->cursor().inMathed())
1451                 sendDispatchMessage(getMessage(), cmd, verbose);
1452 }
1453
1454
1455 void LyXFunc::sendDispatchMessage(string const & msg,
1456                                   FuncRequest const & cmd, bool verbose)
1457 {
1458         owner->updateMenubar();
1459         owner->updateToolbar();
1460
1461         if (cmd.action == LFUN_SELFINSERT || !verbose) {
1462                 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1463                 if (!msg.empty())
1464                         owner->message(msg);
1465                 return;
1466         }
1467
1468         string dispatch_msg = msg;
1469         if (!dispatch_msg.empty())
1470                 dispatch_msg += ' ';
1471
1472         string comname = lyxaction.getActionName(cmd.action);
1473
1474         bool argsadded = false;
1475
1476         if (!cmd.argument.empty()) {
1477                 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1478                         comname += ' ' + cmd.argument;
1479                         argsadded = true;
1480                 }
1481         }
1482
1483         string const shortcuts = toplevel_keymap->findbinding(cmd);
1484
1485         if (!shortcuts.empty()) {
1486                 comname += ": " + shortcuts;
1487         } else if (!argsadded && !cmd.argument.empty()) {
1488                 comname += ' ' + cmd.argument;
1489         }
1490
1491         if (!comname.empty()) {
1492                 comname = rtrim(comname);
1493                 dispatch_msg += '(' + comname + ')';
1494         }
1495
1496         lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1497         if (!dispatch_msg.empty())
1498                 owner->message(dispatch_msg);
1499 }
1500
1501
1502 void LyXFunc::setupLocalKeymap()
1503 {
1504         keyseq.stdmap = toplevel_keymap.get();
1505         keyseq.curmap = toplevel_keymap.get();
1506         cancel_meta_seq.stdmap = toplevel_keymap.get();
1507         cancel_meta_seq.curmap = toplevel_keymap.get();
1508 }
1509
1510
1511 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1512 {
1513         string initpath = lyxrc.document_path;
1514         string filename(name);
1515
1516         if (view()->available()) {
1517                 string const trypath = owner->buffer()->filePath();
1518                 // If directory is writeable, use this as default.
1519                 if (IsDirWriteable(trypath))
1520                         initpath = trypath;
1521         }
1522
1523         static int newfile_number;
1524
1525         if (filename.empty()) {
1526                 filename = AddName(lyxrc.document_path,
1527                             "newfile" + tostr(++newfile_number) + ".lyx");
1528                 FileInfo fi(filename);
1529                 while (bufferlist.exists(filename) || fi.readable()) {
1530                         ++newfile_number;
1531                         filename = AddName(lyxrc.document_path,
1532                                     "newfile" + tostr(newfile_number) +
1533                                     ".lyx");
1534                         fi.newFile(filename);
1535                 }
1536         }
1537
1538         // The template stuff
1539         string templname;
1540         if (fromTemplate) {
1541                 FileDialog fileDlg(_("Select template file"),
1542                         LFUN_SELECT_FILE_SYNC,
1543                         make_pair(string(_("Documents|#o#O")),
1544                                   string(lyxrc.document_path)),
1545                         make_pair(string(_("Templates|#T#t")),
1546                                   string(lyxrc.template_path)));
1547
1548                 FileDialog::Result result =
1549                         fileDlg.open(lyxrc.template_path,
1550                                      FileFilterList(_("LyX Documents (*.lyx)")),
1551                                      string());
1552
1553                 if (result.first == FileDialog::Later)
1554                         return;
1555                 if (result.second.empty())
1556                         return;
1557                 templname = result.second;
1558         }
1559
1560         view()->newFile(filename, templname, !name.empty());
1561 }
1562
1563
1564 void LyXFunc::open(string const & fname)
1565 {
1566         string initpath = lyxrc.document_path;
1567
1568         if (view()->available()) {
1569                 string const trypath = owner->buffer()->filePath();
1570                 // If directory is writeable, use this as default.
1571                 if (IsDirWriteable(trypath))
1572                         initpath = trypath;
1573         }
1574
1575         string filename;
1576
1577         if (fname.empty()) {
1578                 FileDialog fileDlg(_("Select document to open"),
1579                         LFUN_FILE_OPEN,
1580                         make_pair(string(_("Documents|#o#O")),
1581                                   string(lyxrc.document_path)),
1582                         make_pair(string(_("Examples|#E#e")),
1583                                   string(AddPath(system_lyxdir(), "examples"))));
1584
1585                 FileDialog::Result result =
1586                         fileDlg.open(initpath,
1587                                      FileFilterList(_("LyX Documents (*.lyx)")),
1588                                      string());
1589
1590                 if (result.first == FileDialog::Later)
1591                         return;
1592
1593                 filename = result.second;
1594
1595                 // check selected filename
1596                 if (filename.empty()) {
1597                         owner->message(_("Canceled."));
1598                         return;
1599                 }
1600         } else
1601                 filename = fname;
1602
1603         // get absolute path of file and add ".lyx" to the filename if
1604         // necessary
1605         string const fullpath = FileSearch(string(), filename, "lyx");
1606         if (!fullpath.empty()) {
1607                 filename = fullpath;
1608         }
1609
1610         string const disp_fn(MakeDisplayPath(filename));
1611
1612         // if the file doesn't exist, let the user create one
1613         FileInfo const f(filename, true);
1614         if (!f.exist()) {
1615                 // the user specifically chose this name. Believe them.
1616                 view()->newFile(filename, "", true);
1617                 return;
1618         }
1619
1620         owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1621
1622         string str2;
1623         if (view()->loadLyXFile(filename)) {
1624                 str2 = bformat(_("Document %1$s opened."), disp_fn);
1625         } else {
1626                 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1627         }
1628         owner->message(str2);
1629 }
1630
1631
1632 void LyXFunc::doImport(string const & argument)
1633 {
1634         string format;
1635         string filename = split(argument, format, ' ');
1636
1637         lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1638                             << " file: " << filename << endl;
1639
1640         // need user interaction
1641         if (filename.empty()) {
1642                 string initpath = lyxrc.document_path;
1643
1644                 if (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 const text = bformat(_("Select %1$s file to import"),
1652                         formats.prettyName(format));
1653
1654                 FileDialog fileDlg(text,
1655                         LFUN_IMPORT,
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                 string const filter = formats.prettyName(format)
1662                         + " (*." + formats.extension(format) + ')';
1663
1664                 FileDialog::Result result =
1665                         fileDlg.open(initpath,
1666                                      FileFilterList(filter),
1667                                      string());
1668
1669                 if (result.first == FileDialog::Later)
1670                         return;
1671
1672                 filename = result.second;
1673
1674                 // check selected filename
1675                 if (filename.empty())
1676                         owner->message(_("Canceled."));
1677         }
1678
1679         if (filename.empty())
1680                 return;
1681
1682         // get absolute path of file
1683         filename = MakeAbsPath(filename);
1684
1685         string const lyxfile = ChangeExtension(filename, ".lyx");
1686
1687         // Check if the document already is open
1688         if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1689                 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1690                         owner->message(_("Canceled."));
1691                         return;
1692                 }
1693         }
1694
1695         // if the file exists already, and we didn't do
1696         // -i lyx thefile.lyx, warn
1697         if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1698                 string const file = MakeDisplayPath(lyxfile, 30);
1699
1700                 string text = bformat(_("The document %1$s already exists.\n\n"
1701                         "Do you want to over-write that document?"), file);
1702                 int const ret = Alert::prompt(_("Over-write document?"),
1703                         text, 0, 1, _("&Over-write"), _("&Cancel"));
1704
1705                 if (ret == 1) {
1706                         owner->message(_("Canceled."));
1707                         return;
1708                 }
1709         }
1710
1711         Importer::Import(owner, filename, format);
1712 }
1713
1714
1715 void LyXFunc::closeBuffer()
1716 {
1717         if (bufferlist.close(owner->buffer(), true) && !quitting) {
1718                 if (bufferlist.empty()) {
1719                         // need this otherwise SEGV may occur while
1720                         // trying to set variables that don't exist
1721                         // since there's no current buffer
1722                         owner->getDialogs().hideBufferDependent();
1723                 } else {
1724                         view()->buffer(bufferlist.first());
1725                 }
1726         }
1727 }
1728
1729
1730 // Each "owner" should have it's own message method. lyxview and
1731 // the minibuffer would use the minibuffer, but lyxserver would
1732 // send an ERROR signal to its client.  Alejandro 970603
1733 // This function is bit problematic when it comes to NLS, to make the
1734 // lyx servers client be language indepenent we must not translate
1735 // strings sent to this func.
1736 void LyXFunc::setErrorMessage(string const & m) const
1737 {
1738         dispatch_buffer = m;
1739         errorstat = true;
1740 }
1741
1742
1743 void LyXFunc::setMessage(string const & m) const
1744 {
1745         dispatch_buffer = m;
1746 }
1747
1748
1749 void LyXFunc::setStatusMessage(string const & m) const
1750 {
1751         status_buffer = m;
1752 }
1753
1754
1755 string const LyXFunc::view_status_message()
1756 {
1757         // When meta-fake key is pressed, show the key sequence so far + "M-".
1758         if (wasMetaKey()) {
1759                 return keyseq.print() + "M-";
1760         }
1761
1762         // Else, when a non-complete key sequence is pressed,
1763         // show the available options.
1764         if (keyseq.length() > 0 && !keyseq.deleted()) {
1765                 return keyseq.printOptions();
1766         }
1767
1768         if (!view()->available())
1769                 return _("Welcome to LyX!");
1770
1771         return currentState(view());
1772 }
1773
1774
1775 BufferView * LyXFunc::view() const
1776 {
1777         BOOST_ASSERT(owner);
1778         return owner->view().get();
1779 }
1780
1781
1782 bool LyXFunc::wasMetaKey() const
1783 {
1784         return (meta_fake_bit != key_modifier::none);
1785 }