3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "BranchList.h"
26 #include "buffer_funcs.h"
27 #include "bufferlist.h"
28 #include "bufferparams.h"
29 #include "BufferView.h"
32 #include "dispatchresult.h"
36 #include "funcrequest.h"
40 #include "iterators.h"
44 #include "LyXAction.h"
48 #include "lyxserver.h"
50 #include "paragraph.h"
51 #include "ParagraphParameters.h"
54 #include "insets/insetcommand.h"
55 #include "insets/insetexternal.h"
56 #include "insets/insettabular.h"
57 #include "insets/insetvspace.h"
59 #include "mathed/math_cursor.h"
61 #include "frontends/Alert.h"
62 #include "frontends/Dialogs.h"
63 #include "frontends/FileDialog.h"
64 #include "frontends/lyx_gui.h"
65 #include "frontends/LyXKeySym.h"
66 #include "frontends/LyXView.h"
67 #include "frontends/Menubar.h"
68 #include "frontends/Toolbar.h"
70 #include "support/FileInfo.h"
71 #include "support/filetools.h"
72 #include "support/forkedcontr.h"
73 #include "support/path.h"
74 #include "support/path_defines.h"
75 #include "support/tostr.h"
76 #include "support/std_sstream.h"
77 #include "support/os.h"
79 using bv_funcs::changeDepth;
80 using bv_funcs::currentState;
81 using bv_funcs::DEC_DEPTH;
82 using bv_funcs::freefont2string;
83 using bv_funcs::INC_DEPTH;
85 using lyx::support::AddName;
86 using lyx::support::AddPath;
87 using lyx::support::bformat;
88 using lyx::support::ChangeExtension;
89 using lyx::support::FileInfo;
90 using lyx::support::FileSearch;
91 using lyx::support::ForkedcallsController;
92 using lyx::support::i18nLibFileSearch;
93 using lyx::support::IsDirWriteable;
94 using lyx::support::IsFileReadable;
95 using lyx::support::isStrInt;
96 using lyx::support::MakeAbsPath;
97 using lyx::support::MakeDisplayPath;
98 using lyx::support::Path;
99 using lyx::support::rtrim;
100 using lyx::support::split;
101 using lyx::support::strToInt;
102 using lyx::support::strToUnsignedInt;
103 using lyx::support::system_lyxdir;
104 using lyx::support::token;
105 using lyx::support::trim;
106 using lyx::support::user_lyxdir;
107 using lyx::support::prefixIs;
108 using lyx::support::os::getTmpDir;
111 using std::make_pair;
113 using std::istringstream;
116 extern BufferList bufferlist;
117 extern LyXServer * lyxserver;
118 extern bool selection_possible;
120 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
123 extern tex_accent_struct get_accent(kb_action action);
126 LyXFunc::LyXFunc(LyXView * lv)
129 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
130 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
131 meta_fake_bit(key_modifier::none)
136 void LyXFunc::handleKeyFunc(kb_action action)
138 char c = encoded_last_key;
140 if (keyseq.length()) {
144 owner->getIntl().getTransManager()
145 .deadkey(c, get_accent(action).accent, view()->getLyXText());
146 // Need to clear, in case the minibuffer calls these
149 // copied verbatim from do_accent_char
151 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
155 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
156 key_modifier::state state)
160 if (lyxerr.debugging(Debug::KEY)) {
161 lyxerr << "KeySym is "
162 << keysym->getSymbolName()
166 // Do nothing if we have nothing (JMarc)
167 if (!keysym->isOK()) {
168 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
173 if (keysym->isModifier()) {
174 lyxerr[Debug::KEY] << "isModifier true" << endl;
178 Encoding const * encoding = view()->getEncoding();
180 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
182 // Do a one-deep top-level lookup for
183 // cancel and meta-fake keys. RVDK_PATCH_5
184 cancel_meta_seq.reset();
186 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
187 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
189 // When not cancel or meta-fake, do the normal lookup.
190 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
191 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
192 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
193 // remove Caps Lock and Mod2 as a modifiers
194 func = keyseq.addkey(keysym, (state | meta_fake_bit));
195 lyxerr[Debug::KEY] << "action now set to ["
196 << func.action << ']' << endl;
199 // Dont remove this unless you know what you are doing.
200 meta_fake_bit = key_modifier::none;
202 // can this happen now ?
203 if (func.action == LFUN_NOACTION) {
204 func = FuncRequest(LFUN_PREFIX);
207 if (lyxerr.debugging(Debug::KEY)) {
208 lyxerr << "Key [action="
209 << func.action << "]["
210 << keyseq.print() << ']'
214 // already here we know if it any point in going further
215 // why not return already here if action == -1 and
216 // num_bytes == 0? (Lgb)
218 if (keyseq.length() > 1) {
219 owner->message(keyseq.print());
223 // Maybe user can only reach the key via holding down shift.
224 // Let's see. But only if shift is the only modifier
225 if (func.action == LFUN_UNKNOWN_ACTION &&
226 state == key_modifier::shift) {
227 lyxerr[Debug::KEY] << "Trying without shift" << endl;
228 func = keyseq.addkey(keysym, key_modifier::none);
229 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
232 if (func.action == LFUN_UNKNOWN_ACTION) {
233 // Hmm, we didn't match any of the keysequences. See
234 // if it's normal insertable text not already covered
236 if (keysym->isText() && keyseq.length() == 1) {
237 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
238 func = FuncRequest(LFUN_SELFINSERT);
240 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
241 owner->message(_("Unknown function."));
246 if (func.action == LFUN_SELFINSERT) {
247 if (encoded_last_key != 0) {
249 arg += encoded_last_key;
251 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
253 lyxerr[Debug::KEY] << "SelfInsert arg[`"
254 << argument << "']" << endl;
262 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
265 Buffer * buf = owner->buffer();
267 if (ev.action == LFUN_NOACTION) {
268 setStatusMessage(N_("Nothing to do"));
274 case LFUN_UNKNOWN_ACTION:
275 #ifndef HAVE_LIBAIKSAURUS
276 case LFUN_THESAURUS_ENTRY:
282 flag |= lyx_gui::getStatus(ev);
285 if (flag.unknown()) {
286 setStatusMessage(N_("Unknown action"));
290 // the default error message if we disable the command
291 setStatusMessage(N_("Command disabled"));
293 // Check whether we need a buffer
294 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
295 // Yes we need a buffer, do we have one?
298 // Can we use a readonly buffer?
299 if (buf->isReadonly() &&
300 !lyxaction.funcHasFlag(ev.action,
301 LyXAction::ReadOnly)) {
303 setStatusMessage(N_("Document is read-only"));
308 setStatusMessage(N_("Command not allowed with"
309 "out any document open"));
315 UpdatableInset * tli = view()->cursor().innerInset();
316 InsetTabular * tab = view()->cursor().innerInsetTabular();
318 // I would really like to avoid having this switch and rather try to
319 // encode this in the function itself.
320 bool disable = false;
323 disable = ev.argument != "custom"
324 && !Exporter::IsExportable(*buf, ev.argument);
327 disable = buf->undostack().empty();
330 disable = buf->redostack().empty();
334 if (tab && tab->hasSelection())
337 disable = !mathcursor && !view()->getLyXText()->selection.set();
341 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
345 disable = !Exporter::IsExportable(*buf, "program");
348 case LFUN_LAYOUT_TABULAR:
349 disable = !view()->cursor().innerInsetTabular();
353 disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH);
356 case LFUN_DEPTH_PLUS:
357 disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH);
361 case LFUN_LAYOUT_PARAGRAPH: {
362 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
363 disable = inset && inset->forceDefaultParagraphs(inset);
367 case LFUN_INSET_OPTARG:
368 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
371 case LFUN_TABULAR_FEATURE:
374 // FIXME: check temporarily disabled
376 char align = mathcursor->valign();
381 if (ev.argument.empty()) {
385 if (!contains("tcb", ev.argument[0])) {
389 flag.setOnOff(ev.argument[0] == align);
393 char align = mathcursor->halign();
398 if (ev.argument.empty()) {
402 if (!contains("lcr", ev.argument[0])) {
406 flag.setOnOff(ev.argument[0] == align);
408 disable = !mathcursor->halign();
414 //ret.disabled(true);
415 InsetTabular * tab = view()->cursor().innerInsetTabular();
417 ret = tab->getStatus(ev.argument);
424 static InsetTabular inset(*buf, 1, 1);
426 FuncStatus ret = inset.getStatus(ev.argument);
427 if (ret.onoff(true) || ret.onoff(false))
428 flag.setOnOff(false);
433 case LFUN_VC_REGISTER:
434 disable = buf->lyxvc().inUse();
436 case LFUN_VC_CHECKIN:
437 disable = !buf->lyxvc().inUse() || buf->isReadonly();
439 case LFUN_VC_CHECKOUT:
440 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
444 disable = !buf->lyxvc().inUse();
446 case LFUN_MENURELOAD:
447 disable = buf->isUnnamed() || buf->isClean();
449 case LFUN_BOOKMARK_GOTO:
451 isSavedPosition(strToUnsignedInt(ev.argument));
454 case LFUN_MERGE_CHANGES:
455 case LFUN_ACCEPT_CHANGE:
456 case LFUN_REJECT_CHANGE:
457 case LFUN_ACCEPT_ALL_CHANGES:
458 case LFUN_REJECT_ALL_CHANGES:
459 disable = !buf->params().tracking_changes;
462 case LFUN_INSET_SETTINGS: {
464 UpdatableInset * inset = view()->cursor().innerInset();
469 // jump back to owner if an InsetText, so
470 // we get back to the InsetTabular or whatever
471 if (inset->lyxCode() == InsetOld::TEXT_CODE)
472 inset = inset->owner();
474 InsetOld::Code code = inset->lyxCode();
476 case InsetOld::TABULAR_CODE:
477 disable = ev.argument != "tabular";
479 case InsetOld::ERT_CODE:
480 disable = ev.argument != "ert";
482 case InsetOld::FLOAT_CODE:
483 disable = ev.argument != "float";
485 case InsetOld::MINIPAGE_CODE:
486 disable = ev.argument != "minipage";
488 case InsetOld::WRAP_CODE:
489 disable = ev.argument != "wrap";
491 case InsetOld::NOTE_CODE:
492 disable = ev.argument != "note";
494 case InsetOld::BRANCH_CODE:
495 disable = ev.argument != "branch";
497 case InsetOld::BOX_CODE:
498 disable = ev.argument != "box";
506 case LFUN_MATH_MUTATE:
508 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
509 flag.setOnOff(false);
514 // we just need to be in math mode to enable that
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 = !mathcursor;
524 case LFUN_DIALOG_SHOW: {
525 string const name = ev.getArg(0);
527 disable = !(name == "aboutlyx" ||
530 name == "preferences" ||
535 if (name == "print") {
536 disable = !Exporter::IsExportable(*buf, "dvi") ||
537 lyxrc.print_command == "none";
538 } else if (name == "character") {
539 UpdatableInset * tli = view()->cursor().innerInset();
540 disable = tli && tli->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);
553 // the functions which insert insets
554 InsetOld::Code code = InsetOld::NO_CODE;
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 == "citation")
562 code = InsetOld::CITE_CODE;
563 else if (ev.argument == "ert")
564 code = InsetOld::ERT_CODE;
565 else if (ev.argument == "external")
566 code = InsetOld::EXTERNAL_CODE;
567 else if (ev.argument == "float")
568 code = InsetOld::FLOAT_CODE;
569 else if (ev.argument == "graphics")
570 code = InsetOld::GRAPHICS_CODE;
571 else if (ev.argument == "include")
572 code = InsetOld::INCLUDE_CODE;
573 else if (ev.argument == "index")
574 code = InsetOld::INDEX_CODE;
575 else if (ev.argument == "label")
576 code = InsetOld::LABEL_CODE;
577 else if (ev.argument == "minipage")
578 code = InsetOld::MINIPAGE_CODE;
579 else if (ev.argument == "ref")
580 code = InsetOld::REF_CODE;
581 else if (ev.argument == "toc")
582 code = InsetOld::TOC_CODE;
583 else if (ev.argument == "url")
584 code = InsetOld::URL_CODE;
585 else if (ev.argument == "vspace")
586 code = InsetOld::VSPACE_CODE;
587 else if (ev.argument == "wrap")
588 code = InsetOld::WRAP_CODE;
592 code = InsetOld::ERT_CODE;
594 case LFUN_INSET_FOOTNOTE:
595 code = InsetOld::FOOT_CODE;
597 case LFUN_TABULAR_INSERT:
598 code = InsetOld::TABULAR_CODE;
600 case LFUN_INSET_MARGINAL:
601 code = InsetOld::MARGIN_CODE;
603 case LFUN_INSET_MINIPAGE:
604 code = InsetOld::MINIPAGE_CODE;
606 case LFUN_INSET_FLOAT:
607 case LFUN_INSET_WIDE_FLOAT:
608 code = InsetOld::FLOAT_CODE;
610 case LFUN_INSET_WRAP:
611 code = InsetOld::WRAP_CODE;
613 case LFUN_FLOAT_LIST:
614 code = InsetOld::FLOAT_LIST_CODE;
617 case LFUN_INSET_LIST:
618 code = InsetOld::LIST_CODE;
620 case LFUN_INSET_THEOREM:
621 code = InsetOld::THEOREM_CODE;
624 case LFUN_INSET_CAPTION:
625 code = InsetOld::CAPTION_CODE;
627 case LFUN_INSERT_NOTE:
628 code = InsetOld::NOTE_CODE;
630 case LFUN_INSERT_CHARSTYLE:
631 code = InsetOld::CHARSTYLE_CODE;
632 if (buf->params().getLyXTextClass().charstyles().empty())
635 case LFUN_INSERT_BOX:
636 code = InsetOld::BOX_CODE;
638 case LFUN_INSERT_BRANCH:
639 code = InsetOld::BRANCH_CODE;
640 if (buf->params().branchlist().empty())
643 case LFUN_INSERT_LABEL:
644 code = InsetOld::LABEL_CODE;
646 case LFUN_INSET_OPTARG:
647 code = InsetOld::OPTARG_CODE;
649 case LFUN_ENVIRONMENT_INSERT:
650 code = InsetOld::MINIPAGE_CODE;
652 case LFUN_INDEX_INSERT:
653 code = InsetOld::INDEX_CODE;
655 case LFUN_INDEX_PRINT:
656 code = InsetOld::INDEX_PRINT_CODE;
658 case LFUN_TOC_INSERT:
659 code = InsetOld::TOC_CODE;
663 code = InsetOld::URL_CODE;
666 // always allow this, since we will inset a raw quote
667 // if an inset is not allowed.
669 case LFUN_HYPHENATION:
670 case LFUN_LIGATURE_BREAK:
672 case LFUN_MENU_SEPARATOR:
674 case LFUN_END_OF_SENTENCE:
675 code = InsetOld::SPECIALCHAR_CODE;
677 case LFUN_SPACE_INSERT:
678 // slight hack: we know this is allowed in math mode
680 code = InsetOld::SPACE_CODE;
682 case LFUN_INSET_DIALOG_SHOW: {
683 InsetOld * inset = view()->getLyXText()->getInset();
686 code = inset->lyxCode();
687 if (!(code == InsetOld::INCLUDE_CODE
688 || code == InsetOld::BIBTEX_CODE
689 || code == InsetOld::FLOAT_LIST_CODE
690 || code == InsetOld::TOC_CODE))
698 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
704 // A few general toggles
706 case LFUN_TOOLTIPS_TOGGLE:
707 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
710 case LFUN_READ_ONLY_TOGGLE:
711 flag.setOnOff(buf->isReadonly());
714 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
716 case LFUN_SWITCHBUFFER:
717 // toggle on the current buffer, but do not toggle off
718 // the other ones (is that a good idea?)
719 if (ev.argument == buf->fileName())
722 case LFUN_TRACK_CHANGES:
723 flag.setOnOff(buf->params().tracking_changes);
730 // the font related toggles
732 LyXFont const & font = view()->getLyXText()->real_current_font;
735 flag.setOnOff(font.emph() == LyXFont::ON);
738 flag.setOnOff(font.noun() == LyXFont::ON);
741 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
744 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
747 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
750 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
756 string tc = mathcursor->getLastCode();
759 flag.setOnOff(tc == "mathbf");
762 flag.setOnOff(tc == "mathsf");
765 flag.setOnOff(tc == "mathcal");
768 flag.setOnOff(tc == "mathrm");
771 flag.setOnOff(tc == "mathtt");
774 flag.setOnOff(tc == "mathbb");
777 flag.setOnOff(tc == "mathnormal");
785 // this one is difficult to get right. As a half-baked
786 // solution, we consider only the first action of the sequence
787 if (ev.action == LFUN_SEQUENCE) {
788 // argument contains ';'-terminated commands
789 #warning LyXAction arguments not handled here.
790 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
799 bool ensureBufferClean(BufferView * bv)
801 Buffer & buf = *bv->buffer();
805 string const file = MakeDisplayPath(buf.fileName(), 30);
806 string text = bformat(_("The document %1$s has unsaved "
807 "changes.\n\nDo you want to save "
808 "the document?"), file);
809 int const ret = Alert::prompt(_("Save changed document?"),
810 text, 0, 1, _("&Save"),
814 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
816 return buf.isClean();
822 void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
824 string argument = func.argument;
825 kb_action action = func.action;
827 //lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << func << endl;
828 lyxerr << "LyXFunc::dispatch: cmd: " << func << endl;
830 // we have not done anything wrong yet.
832 dispatch_buffer.erase();
834 #ifdef NEW_DISPATCHER
835 // We try do call the most specific dispatcher first:
836 // 1. the lockinginset's dispatch
837 // 2. the bufferview's dispatch
838 // 3. the lyxview's dispatch
841 selection_possible = false;
843 // We cannot use this function here
844 if (getStatus(func).disabled()) {
845 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
846 << lyxaction.getActionName(action)
847 << " [" << action << "] is disabled at this location"
849 setErrorMessage(getStatusMessage());
853 if (view()->available())
854 view()->hideCursor();
859 if (!view()->available())
861 view()->cursor().pop();
862 // Tell the paragraph dialog that we changed paragraph
863 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
867 case LFUN_WORDFINDFORWARD:
868 case LFUN_WORDFINDBACKWARD: {
869 static string last_search;
870 string searched_string;
872 if (!argument.empty()) {
873 last_search = argument;
874 searched_string = argument;
876 searched_string = last_search;
878 bool fw = (action == LFUN_WORDFINDFORWARD);
879 if (!searched_string.empty())
880 lyx::find::find(view(), searched_string,
886 owner->message(keyseq.printOptions());
889 case LFUN_EXEC_COMMAND:
890 owner->focus_command_buffer();
895 meta_fake_bit = key_modifier::none;
896 if (view()->available())
897 // cancel any selection
898 dispatch(FuncRequest(LFUN_MARK_OFF));
899 setMessage(N_("Cancel"));
903 meta_fake_bit = key_modifier::alt;
904 setMessage(keyseq.print());
907 case LFUN_READ_ONLY_TOGGLE:
908 if (owner->buffer()->lyxvc().inUse())
909 owner->buffer()->lyxvc().toggleReadOnly();
911 owner->buffer()->setReadonly(
912 !owner->buffer()->isReadonly());
915 case LFUN_CENTER: // this is center and redraw.
919 // --- Menus -----------------------------------------------
921 menuNew(argument, false);
924 case LFUN_MENUNEWTMPLT:
925 menuNew(argument, true);
928 case LFUN_CLOSEBUFFER:
933 if (!owner->buffer()->isUnnamed()) {
934 string const str = bformat(_("Saving document %1$s..."),
935 MakeDisplayPath(owner->buffer()->fileName()));
937 MenuWrite(owner->buffer());
938 owner->message(str + _(" done."));
940 WriteAs(owner->buffer());
944 WriteAs(owner->buffer(), argument);
947 case LFUN_MENURELOAD: {
948 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
949 string text = bformat(_("Any changes will be lost. Are you sure "
950 "you want to revert to the saved version of the document %1$s?"), file);
951 int const ret = Alert::prompt(_("Revert to saved document?"),
952 text, 0, 1, _("&Revert"), _("&Cancel"));
960 Exporter::Export(owner->buffer(), argument, true);
961 view()->showErrorList(BufferFormat(*owner->buffer()));
965 Exporter::Preview(owner->buffer(), argument);
966 view()->showErrorList(BufferFormat(*owner->buffer()));
970 Exporter::Export(owner->buffer(), "program", true);
971 view()->showErrorList(_("Build"));
975 owner->buffer()->runChktex();
976 view()->showErrorList(_("ChkTeX"));
980 if (argument == "custom")
981 owner->getDialogs().showSendto();
983 Exporter::Export(owner->buffer(), argument, false);
984 view()->showErrorList(BufferFormat(*owner->buffer()));
997 InsetCommandParams p("tableofcontents");
998 string const data = InsetCommandMailer::params2string("toc", p);
999 owner->getDialogs().show("toc", data, 0);
1015 case LFUN_RECONFIGURE:
1016 Reconfigure(view());
1019 case LFUN_HELP_OPEN: {
1020 string const arg = argument;
1022 setErrorMessage(N_("Missing argument"));
1025 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1026 if (fname.empty()) {
1027 lyxerr << "LyX: unable to find documentation file `"
1028 << arg << "'. Bad installation?" << endl;
1031 owner->message(bformat(_("Opening help file %1$s..."),
1032 MakeDisplayPath(fname)));
1033 view()->loadLyXFile(fname, false);
1037 // --- version control -------------------------------
1038 case LFUN_VC_REGISTER:
1039 if (!ensureBufferClean(view()))
1041 if (!owner->buffer()->lyxvc().inUse()) {
1042 owner->buffer()->lyxvc().registrer();
1047 case LFUN_VC_CHECKIN:
1048 if (!ensureBufferClean(view()))
1050 if (owner->buffer()->lyxvc().inUse()
1051 && !owner->buffer()->isReadonly()) {
1052 owner->buffer()->lyxvc().checkIn();
1057 case LFUN_VC_CHECKOUT:
1058 if (!ensureBufferClean(view()))
1060 if (owner->buffer()->lyxvc().inUse()
1061 && owner->buffer()->isReadonly()) {
1062 owner->buffer()->lyxvc().checkOut();
1067 case LFUN_VC_REVERT:
1068 owner->buffer()->lyxvc().revert();
1073 owner->buffer()->lyxvc().undoLast();
1077 // --- buffers ----------------------------------------
1078 case LFUN_SWITCHBUFFER:
1079 view()->buffer(bufferlist.getBuffer(argument));
1083 NewFile(view(), argument);
1086 case LFUN_FILE_OPEN:
1090 case LFUN_LAYOUT_TABULAR:
1091 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1092 tab->openLayoutDialog(view());
1095 case LFUN_DROP_LAYOUTS_CHOICE:
1096 owner->getToolbar().openLayoutList();
1099 case LFUN_MENU_OPEN_BY_NAME:
1100 owner->getMenubar().openByName(argument);
1103 // --- lyxserver commands ----------------------------
1105 setMessage(owner->buffer()->fileName());
1106 lyxerr[Debug::INFO] << "FNAME["
1107 << owner->buffer()->fileName()
1112 dispatch_buffer = keyseq.print();
1113 lyxserver->notifyClient(dispatch_buffer);
1116 case LFUN_GOTOFILEROW: {
1119 istringstream is(argument);
1120 is >> file_name >> row;
1121 if (prefixIs(file_name, getTmpDir())) {
1122 // Needed by inverse dvi search. If it is a file
1123 // in tmpdir, call the apropriated function
1124 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1126 // Must replace extension of the file to be .lyx
1127 // and get full path
1128 string const s = ChangeExtension(file_name, ".lyx");
1129 // Either change buffer or load the file
1130 if (bufferlist.exists(s)) {
1131 view()->buffer(bufferlist.getBuffer(s));
1133 view()->loadLyXFile(s);
1137 view()->setCursorFromRow(row);
1140 // see BufferView_pimpl::center()
1141 view()->updateScrollbar();
1145 case LFUN_GOTO_PARAGRAPH: {
1146 istringstream is(argument);
1149 ParIterator par = owner->buffer()->getParFromID(id);
1150 if (par == owner->buffer()->par_iterator_end()) {
1151 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1152 << id << ']' << endl;
1155 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1156 << " found." << endl;
1159 par.lockPath(view());
1160 LyXText * lt = par.text(*view()->buffer());
1163 lt->setCursor(par.pit(), 0);
1164 view()->switchKeyMap();
1165 owner->view_state_changed();
1168 // see BufferView_pimpl::center()
1169 view()->updateScrollbar();
1173 // --- Mathed stuff. If we are here, there is no locked inset yet.
1174 case LFUN_MATH_EXTERN:
1175 case LFUN_MATH_NUMBER:
1176 case LFUN_MATH_NONUMBER:
1177 case LFUN_MATH_LIMITS:
1178 setErrorMessage(N_("This is only allowed in math mode!"));
1181 // passthrough hat and underscore outside mathed:
1182 case LFUN_SUBSCRIPT:
1183 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1186 case LFUN_SUPERSCRIPT:
1187 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1190 case LFUN_DIALOG_SHOW: {
1191 string const name = func.getArg(0);
1192 string data = trim(func.argument.substr(name.size()));
1194 if (name == "character") {
1195 data = freefont2string();
1197 owner->getDialogs().show("character", data);
1198 } else if (name == "document")
1199 owner->getDialogs().showDocument();
1200 else if (name == "findreplace")
1201 owner->getDialogs().showSearch();
1202 else if (name == "forks")
1203 owner->getDialogs().showForks();
1204 else if (name == "preamble")
1205 owner->getDialogs().showPreamble();
1206 else if (name == "preferences")
1207 owner->getDialogs().showPreferences();
1208 else if (name == "print")
1209 owner->getDialogs().showPrint();
1210 else if (name == "spellchecker")
1211 owner->getDialogs().showSpellchecker();
1213 owner->getDialogs().show(name, data);
1217 case LFUN_DIALOG_SHOW_NEW_INSET: {
1218 string const & name = argument;
1220 if (name == "bibitem" ||
1222 name == "include" ||
1227 InsetCommandParams p(name);
1228 data = InsetCommandMailer::params2string(name, p);
1229 } else if (name == "citation") {
1230 InsetCommandParams p("cite");
1231 data = InsetCommandMailer::params2string(name, p);
1232 } else if (name == "vspace") {
1234 data = InsetVSpaceMailer::params2string(space);
1236 owner->getDialogs().show(name, data, 0);
1240 case LFUN_DIALOG_SHOW_NEXT_INSET:
1243 case LFUN_INSET_DIALOG_SHOW: {
1244 InsetOld * inset = view()->getLyXText()->getInset();
1246 inset->dispatch(FuncRequest(view(), LFUN_INSET_DIALOG_SHOW));
1250 case LFUN_DIALOG_UPDATE: {
1251 string const & name = argument;
1252 // Can only update a dialog connected to an existing inset
1253 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1255 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1257 inset->dispatch(fr);
1258 } else if (name == "paragraph") {
1259 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1264 case LFUN_DIALOG_HIDE:
1265 Dialogs::hide(argument, 0);
1268 case LFUN_DIALOG_DISCONNECT_INSET:
1269 owner->getDialogs().disconnect(argument);
1272 case LFUN_CHILDOPEN: {
1273 string const filename =
1274 MakeAbsPath(argument, owner->buffer()->filePath());
1275 setMessage(N_("Opening child document ") +
1276 MakeDisplayPath(filename) + "...");
1277 view()->savePosition(0);
1278 string const parentfilename = owner->buffer()->fileName();
1279 if (bufferlist.exists(filename))
1280 view()->buffer(bufferlist.getBuffer(filename));
1282 view()->loadLyXFile(filename);
1283 // Set the parent name of the child document.
1284 // This makes insertion of citations and references in the child work,
1285 // when the target is in the parent or another child document.
1286 owner->buffer()->setParentName(parentfilename);
1290 case LFUN_TOGGLECURSORFOLLOW:
1291 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1295 owner->getIntl().KeyMapOn(false);
1298 case LFUN_KMAP_PRIM:
1299 owner->getIntl().KeyMapPrim();
1303 owner->getIntl().KeyMapSec();
1306 case LFUN_KMAP_TOGGLE:
1307 owner->getIntl().ToggleKeyMap();
1313 argument = split(argument, countstr, ' ');
1314 istringstream is(countstr);
1317 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1318 for (int i = 0; i < count; ++i)
1319 dispatch(lyxaction.lookupFunc(argument));
1324 // argument contains ';'-terminated commands
1325 while (!argument.empty()) {
1327 argument = split(argument, first, ';');
1328 dispatch(lyxaction.lookupFunc(first));
1332 case LFUN_SAVEPREFERENCES: {
1333 Path p(user_lyxdir());
1334 lyxrc.write("preferences");
1338 case LFUN_SCREEN_FONT_UPDATE:
1339 // handle the screen font changes.
1340 lyxrc.set_font_norm_type();
1341 lyx_gui::update_fonts();
1342 // All visible buffers will need resize
1346 case LFUN_SET_COLOR: {
1348 string const x11_name = split(argument, lyx_name, ' ');
1349 if (lyx_name.empty() || x11_name.empty()) {
1350 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1355 bool const graphicsbg_changed =
1356 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1357 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1359 if (!lcolor.setColor(lyx_name, x11_name)) {
1361 bformat(_("Set-color \"%1$s\" failed "
1362 "- color is undefined or "
1363 "may not be redefined"), lyx_name));
1367 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1369 if (graphicsbg_changed) {
1370 #ifdef WITH_WARNINGS
1371 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1374 lyx::graphics::GCache::get().changeDisplay(true);
1381 owner->message(argument);
1384 case LFUN_FORKS_KILL:
1385 if (isStrInt(argument))
1386 ForkedcallsController::get().kill(strToInt(argument));
1389 case LFUN_TOOLTIPS_TOGGLE:
1390 owner->getDialogs().toggleTooltips();
1393 case LFUN_EXTERNAL_EDIT:
1394 InsetExternal().dispatch(FuncRequest(view(), action, argument));
1398 DispatchResult result =
1399 view()->cursor().dispatch(FuncRequest(func, view()));
1400 if (result.dispatched())
1401 lyxerr << "dispatched by Cursor::dispatch()" << endl;
1403 lyxerr << "### NOT DISPATCHED BY Cursor::dispatch() ###" << endl;
1409 view()->owner()->updateLayoutChoice();
1411 if (view()->available()) {
1412 view()->fitCursor();
1414 view()->cursor().updatePos();
1415 // if we executed a mutating lfun, mark the buffer as dirty
1416 if (!getStatus(func).disabled()
1417 && !lyxaction.funcHasFlag(func.action, LyXAction::NoBuffer)
1418 && !lyxaction.funcHasFlag(func.action, LyXAction::ReadOnly))
1419 view()->buffer()->markDirty();
1422 sendDispatchMessage(getMessage(), func, verbose);
1426 void LyXFunc::sendDispatchMessage(string const & msg,
1427 FuncRequest const & func, bool verbose)
1429 owner->updateMenubar();
1430 owner->updateToolbar();
1432 if (func.action == LFUN_SELFINSERT || !verbose) {
1433 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1435 owner->message(msg);
1439 string dispatch_msg = msg;
1440 if (!dispatch_msg.empty())
1441 dispatch_msg += ' ';
1443 string comname = lyxaction.getActionName(func.action);
1445 bool argsadded = false;
1447 if (!func.argument.empty()) {
1448 if (func.action != LFUN_UNKNOWN_ACTION) {
1449 comname += ' ' + func.argument;
1454 string const shortcuts = toplevel_keymap->findbinding(func);
1456 if (!shortcuts.empty()) {
1457 comname += ": " + shortcuts;
1458 } else if (!argsadded && !func.argument.empty()) {
1459 comname += ' ' + func.argument;
1462 if (!comname.empty()) {
1463 comname = rtrim(comname);
1464 dispatch_msg += '(' + comname + ')';
1467 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1468 if (!dispatch_msg.empty())
1469 owner->message(dispatch_msg);
1473 void LyXFunc::setupLocalKeymap()
1475 keyseq.stdmap = toplevel_keymap.get();
1476 keyseq.curmap = toplevel_keymap.get();
1477 cancel_meta_seq.stdmap = toplevel_keymap.get();
1478 cancel_meta_seq.curmap = toplevel_keymap.get();
1482 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1484 string initpath = lyxrc.document_path;
1485 string filename(name);
1487 if (view()->available()) {
1488 string const trypath = owner->buffer()->filePath();
1489 // If directory is writeable, use this as default.
1490 if (IsDirWriteable(trypath))
1494 static int newfile_number;
1496 if (filename.empty()) {
1497 filename = AddName(lyxrc.document_path,
1498 "newfile" + tostr(++newfile_number) + ".lyx");
1499 FileInfo fi(filename);
1500 while (bufferlist.exists(filename) || fi.readable()) {
1502 filename = AddName(lyxrc.document_path,
1503 "newfile" + tostr(newfile_number) +
1505 fi.newFile(filename);
1509 // The template stuff
1512 FileDialog fileDlg(_("Select template file"),
1513 LFUN_SELECT_FILE_SYNC,
1514 make_pair(string(_("Documents|#o#O")),
1515 string(lyxrc.document_path)),
1516 make_pair(string(_("Templates|#T#t")),
1517 string(lyxrc.template_path)));
1519 FileDialog::Result result =
1520 fileDlg.open(lyxrc.template_path,
1521 _("*.lyx| LyX Documents (*.lyx)"));
1523 if (result.first == FileDialog::Later)
1525 if (result.second.empty())
1527 templname = result.second;
1530 view()->newFile(filename, templname, !name.empty());
1534 void LyXFunc::open(string const & fname)
1536 string initpath = lyxrc.document_path;
1538 if (view()->available()) {
1539 string const trypath = owner->buffer()->filePath();
1540 // If directory is writeable, use this as default.
1541 if (IsDirWriteable(trypath))
1547 if (fname.empty()) {
1548 FileDialog fileDlg(_("Select document to open"),
1550 make_pair(string(_("Documents|#o#O")),
1551 string(lyxrc.document_path)),
1552 make_pair(string(_("Examples|#E#e")),
1553 string(AddPath(system_lyxdir(), "examples"))));
1555 FileDialog::Result result =
1556 fileDlg.open(initpath,
1557 _("*.lyx| LyX Documents (*.lyx)"));
1559 if (result.first == FileDialog::Later)
1562 filename = result.second;
1564 // check selected filename
1565 if (filename.empty()) {
1566 owner->message(_("Canceled."));
1572 // get absolute path of file and add ".lyx" to the filename if
1574 string const fullpath = FileSearch(string(), filename, "lyx");
1575 if (!fullpath.empty()) {
1576 filename = fullpath;
1579 string const disp_fn(MakeDisplayPath(filename));
1581 // if the file doesn't exist, let the user create one
1582 FileInfo const f(filename, true);
1584 // the user specifically chose this name. Believe them.
1585 view()->newFile(filename, "", true);
1589 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1592 if (view()->loadLyXFile(filename)) {
1593 str2 = bformat(_("Document %1$s opened."), disp_fn);
1595 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1597 owner->message(str2);
1601 void LyXFunc::doImport(string const & argument)
1604 string filename = split(argument, format, ' ');
1606 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1607 << " file: " << filename << endl;
1609 // need user interaction
1610 if (filename.empty()) {
1611 string initpath = lyxrc.document_path;
1613 if (view()->available()) {
1614 string const trypath = owner->buffer()->filePath();
1615 // If directory is writeable, use this as default.
1616 if (IsDirWriteable(trypath))
1620 string const text = bformat(_("Select %1$s file to import"),
1621 formats.prettyName(format));
1623 FileDialog fileDlg(text,
1625 make_pair(string(_("Documents|#o#O")),
1626 string(lyxrc.document_path)),
1627 make_pair(string(_("Examples|#E#e")),
1628 string(AddPath(system_lyxdir(), "examples"))));
1630 string const extension = "*." + formats.extension(format)
1631 + "| " + formats.prettyName(format)
1632 + " (*." + formats.extension(format) + ')';
1634 FileDialog::Result result = fileDlg.open(initpath,
1637 if (result.first == FileDialog::Later)
1640 filename = result.second;
1642 // check selected filename
1643 if (filename.empty())
1644 owner->message(_("Canceled."));
1647 if (filename.empty())
1650 // get absolute path of file
1651 filename = MakeAbsPath(filename);
1653 string const lyxfile = ChangeExtension(filename, ".lyx");
1655 // Check if the document already is open
1656 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1657 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1658 owner->message(_("Canceled."));
1663 // if the file exists already, and we didn't do
1664 // -i lyx thefile.lyx, warn
1665 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1666 string const file = MakeDisplayPath(lyxfile, 30);
1668 string text = bformat(_("The document %1$s already exists.\n\n"
1669 "Do you want to over-write that document?"), file);
1670 int const ret = Alert::prompt(_("Over-write document?"),
1671 text, 0, 1, _("&Over-write"), _("&Cancel"));
1674 owner->message(_("Canceled."));
1679 Importer::Import(owner, filename, format);
1683 void LyXFunc::closeBuffer()
1685 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1686 if (bufferlist.empty()) {
1687 // need this otherwise SEGV may occur while
1688 // trying to set variables that don't exist
1689 // since there's no current buffer
1690 owner->getDialogs().hideBufferDependent();
1692 view()->buffer(bufferlist.first());
1698 // Each "owner" should have it's own message method. lyxview and
1699 // the minibuffer would use the minibuffer, but lyxserver would
1700 // send an ERROR signal to its client. Alejandro 970603
1701 // This func is bit problematic when it comes to NLS, to make the
1702 // lyx servers client be language indepenent we must not translate
1703 // strings sent to this func.
1704 void LyXFunc::setErrorMessage(string const & m) const
1706 dispatch_buffer = m;
1711 void LyXFunc::setMessage(string const & m) const
1713 dispatch_buffer = m;
1717 void LyXFunc::setStatusMessage(string const & m) const
1723 string const LyXFunc::view_status_message()
1725 // When meta-fake key is pressed, show the key sequence so far + "M-".
1727 return keyseq.print() + "M-";
1730 // Else, when a non-complete key sequence is pressed,
1731 // show the available options.
1732 if (keyseq.length() > 0 && !keyseq.deleted()) {
1733 return keyseq.printOptions();
1736 if (!view()->available())
1737 return _("Welcome to LyX!");
1739 return currentState(view());
1743 BufferView * LyXFunc::view() const
1745 BOOST_ASSERT(owner);
1746 return owner->view().get();
1750 bool LyXFunc::wasMetaKey() const
1752 return (meta_fake_bit != key_modifier::none);