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_TOGGLE: {
463 LyXText * lt = view()->getLyXText();
464 disable = !(isEditableInset(lt->getInset())
466 && lt->inset_owner->owner()
467 && lt->inset_owner->owner()->isOpen()));
471 case LFUN_INSET_SETTINGS: {
473 UpdatableInset * inset = view()->cursor().innerInset();
478 // jump back to owner if an InsetText, so
479 // we get back to the InsetTabular or whatever
480 if (inset->lyxCode() == InsetOld::TEXT_CODE)
481 inset = inset->owner();
483 InsetOld::Code code = inset->lyxCode();
485 case InsetOld::TABULAR_CODE:
486 disable = ev.argument != "tabular";
488 case InsetOld::ERT_CODE:
489 disable = ev.argument != "ert";
491 case InsetOld::FLOAT_CODE:
492 disable = ev.argument != "float";
494 case InsetOld::MINIPAGE_CODE:
495 disable = ev.argument != "minipage";
497 case InsetOld::WRAP_CODE:
498 disable = ev.argument != "wrap";
500 case InsetOld::NOTE_CODE:
501 disable = ev.argument != "note";
503 case InsetOld::BRANCH_CODE:
504 disable = ev.argument != "branch";
506 case InsetOld::BOX_CODE:
507 disable = ev.argument != "box";
515 case LFUN_MATH_MUTATE:
517 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
518 flag.setOnOff(false);
523 // we just need to be in math mode to enable that
525 case LFUN_MATH_SPACE:
526 case LFUN_MATH_LIMITS:
527 case LFUN_MATH_NONUMBER:
528 case LFUN_MATH_NUMBER:
529 case LFUN_MATH_EXTERN:
530 disable = !mathcursor;
533 case LFUN_DIALOG_SHOW: {
534 string const name = ev.getArg(0);
536 disable = !(name == "aboutlyx" ||
539 name == "preferences" ||
544 if (name == "print") {
545 disable = !Exporter::IsExportable(*buf, "dvi") ||
546 lyxrc.print_command == "none";
547 } else if (name == "character") {
548 UpdatableInset * tli = view()->cursor().innerInset();
549 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
550 } else if (name == "vclog") {
551 disable = !buf->lyxvc().inUse();
552 } else if (name == "latexlog") {
553 disable = !IsFileReadable(buf->getLogName().second);
562 // the functions which insert insets
563 InsetOld::Code code = InsetOld::NO_CODE;
565 case LFUN_DIALOG_SHOW_NEW_INSET:
566 if (ev.argument == "bibitem")
567 code = InsetOld::BIBITEM_CODE;
568 else if (ev.argument == "bibtex")
569 code = InsetOld::BIBTEX_CODE;
570 else if (ev.argument == "citation")
571 code = InsetOld::CITE_CODE;
572 else if (ev.argument == "ert")
573 code = InsetOld::ERT_CODE;
574 else if (ev.argument == "external")
575 code = InsetOld::EXTERNAL_CODE;
576 else if (ev.argument == "float")
577 code = InsetOld::FLOAT_CODE;
578 else if (ev.argument == "graphics")
579 code = InsetOld::GRAPHICS_CODE;
580 else if (ev.argument == "include")
581 code = InsetOld::INCLUDE_CODE;
582 else if (ev.argument == "index")
583 code = InsetOld::INDEX_CODE;
584 else if (ev.argument == "label")
585 code = InsetOld::LABEL_CODE;
586 else if (ev.argument == "minipage")
587 code = InsetOld::MINIPAGE_CODE;
588 else if (ev.argument == "ref")
589 code = InsetOld::REF_CODE;
590 else if (ev.argument == "toc")
591 code = InsetOld::TOC_CODE;
592 else if (ev.argument == "url")
593 code = InsetOld::URL_CODE;
594 else if (ev.argument == "vspace")
595 code = InsetOld::VSPACE_CODE;
596 else if (ev.argument == "wrap")
597 code = InsetOld::WRAP_CODE;
601 code = InsetOld::ERT_CODE;
603 case LFUN_INSET_FOOTNOTE:
604 code = InsetOld::FOOT_CODE;
606 case LFUN_TABULAR_INSERT:
607 code = InsetOld::TABULAR_CODE;
609 case LFUN_INSET_MARGINAL:
610 code = InsetOld::MARGIN_CODE;
612 case LFUN_INSET_MINIPAGE:
613 code = InsetOld::MINIPAGE_CODE;
615 case LFUN_INSET_FLOAT:
616 case LFUN_INSET_WIDE_FLOAT:
617 code = InsetOld::FLOAT_CODE;
619 case LFUN_INSET_WRAP:
620 code = InsetOld::WRAP_CODE;
622 case LFUN_FLOAT_LIST:
623 code = InsetOld::FLOAT_LIST_CODE;
626 case LFUN_INSET_LIST:
627 code = InsetOld::LIST_CODE;
629 case LFUN_INSET_THEOREM:
630 code = InsetOld::THEOREM_CODE;
633 case LFUN_INSET_CAPTION:
634 code = InsetOld::CAPTION_CODE;
636 case LFUN_INSERT_NOTE:
637 code = InsetOld::NOTE_CODE;
639 case LFUN_INSERT_CHARSTYLE:
640 code = InsetOld::CHARSTYLE_CODE;
641 if (buf->params().getLyXTextClass().charstyles().empty())
644 case LFUN_INSERT_BOX:
645 code = InsetOld::BOX_CODE;
647 case LFUN_INSERT_BRANCH:
648 code = InsetOld::BRANCH_CODE;
649 if (buf->params().branchlist().empty())
652 case LFUN_INSERT_LABEL:
653 code = InsetOld::LABEL_CODE;
655 case LFUN_INSET_OPTARG:
656 code = InsetOld::OPTARG_CODE;
658 case LFUN_ENVIRONMENT_INSERT:
659 code = InsetOld::MINIPAGE_CODE;
661 case LFUN_INDEX_INSERT:
662 code = InsetOld::INDEX_CODE;
664 case LFUN_INDEX_PRINT:
665 code = InsetOld::INDEX_PRINT_CODE;
667 case LFUN_TOC_INSERT:
668 code = InsetOld::TOC_CODE;
672 code = InsetOld::URL_CODE;
675 // always allow this, since we will inset a raw quote
676 // if an inset is not allowed.
678 case LFUN_HYPHENATION:
679 case LFUN_LIGATURE_BREAK:
681 case LFUN_MENU_SEPARATOR:
683 case LFUN_END_OF_SENTENCE:
684 code = InsetOld::SPECIALCHAR_CODE;
686 case LFUN_SPACE_INSERT:
687 // slight hack: we know this is allowed in math mode
689 code = InsetOld::SPACE_CODE;
691 case LFUN_INSET_DIALOG_SHOW: {
692 InsetOld * inset = view()->getLyXText()->getInset();
695 code = inset->lyxCode();
696 if (!(code == InsetOld::INCLUDE_CODE
697 || code == InsetOld::BIBTEX_CODE
698 || code == InsetOld::FLOAT_LIST_CODE
699 || code == InsetOld::TOC_CODE))
707 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
713 // A few general toggles
715 case LFUN_TOOLTIPS_TOGGLE:
716 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
719 case LFUN_READ_ONLY_TOGGLE:
720 flag.setOnOff(buf->isReadonly());
723 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
725 case LFUN_SWITCHBUFFER:
726 // toggle on the current buffer, but do not toggle off
727 // the other ones (is that a good idea?)
728 if (ev.argument == buf->fileName())
731 case LFUN_TRACK_CHANGES:
732 flag.setOnOff(buf->params().tracking_changes);
739 // the font related toggles
741 LyXFont const & font = view()->getLyXText()->real_current_font;
744 flag.setOnOff(font.emph() == LyXFont::ON);
747 flag.setOnOff(font.noun() == LyXFont::ON);
750 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
753 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
756 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
759 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
765 string tc = mathcursor->getLastCode();
768 flag.setOnOff(tc == "mathbf");
771 flag.setOnOff(tc == "mathsf");
774 flag.setOnOff(tc == "mathcal");
777 flag.setOnOff(tc == "mathrm");
780 flag.setOnOff(tc == "mathtt");
783 flag.setOnOff(tc == "mathbb");
786 flag.setOnOff(tc == "mathnormal");
794 // this one is difficult to get right. As a half-baked
795 // solution, we consider only the first action of the sequence
796 if (ev.action == LFUN_SEQUENCE) {
797 // argument contains ';'-terminated commands
798 #warning LyXAction arguments not handled here.
799 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
808 bool ensureBufferClean(BufferView * bv)
810 Buffer & buf = *bv->buffer();
814 string const file = MakeDisplayPath(buf.fileName(), 30);
815 string text = bformat(_("The document %1$s has unsaved "
816 "changes.\n\nDo you want to save "
817 "the document?"), file);
818 int const ret = Alert::prompt(_("Save changed document?"),
819 text, 0, 1, _("&Save"),
823 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
825 return buf.isClean();
831 void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
833 string argument = func.argument;
834 kb_action action = func.action;
836 //lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << func << endl;
837 lyxerr << "LyXFunc::dispatch: cmd: " << func << endl;
839 // we have not done anything wrong yet.
841 dispatch_buffer.erase();
843 #ifdef NEW_DISPATCHER
844 // We try do call the most specific dispatcher first:
845 // 1. the lockinginset's dispatch
846 // 2. the bufferview's dispatch
847 // 3. the lyxview's dispatch
850 selection_possible = false;
852 // We cannot use this function here
853 if (getStatus(func).disabled()) {
854 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
855 << lyxaction.getActionName(action)
856 << " [" << action << "] is disabled at this location"
858 setErrorMessage(getStatusMessage());
862 if (view()->available())
863 view()->hideCursor();
868 if (!view()->available())
870 view()->cursor().pop();
871 // Tell the paragraph dialog that we changed paragraph
872 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
876 case LFUN_WORDFINDFORWARD:
877 case LFUN_WORDFINDBACKWARD: {
878 static string last_search;
879 string searched_string;
881 if (!argument.empty()) {
882 last_search = argument;
883 searched_string = argument;
885 searched_string = last_search;
887 bool fw = (action == LFUN_WORDFINDFORWARD);
888 if (!searched_string.empty())
889 lyx::find::find(view(), searched_string,
895 owner->message(keyseq.printOptions());
898 case LFUN_EXEC_COMMAND:
899 owner->focus_command_buffer();
904 meta_fake_bit = key_modifier::none;
905 if (view()->available())
906 // cancel any selection
907 dispatch(FuncRequest(LFUN_MARK_OFF));
908 setMessage(N_("Cancel"));
912 meta_fake_bit = key_modifier::alt;
913 setMessage(keyseq.print());
916 case LFUN_READ_ONLY_TOGGLE:
917 if (owner->buffer()->lyxvc().inUse())
918 owner->buffer()->lyxvc().toggleReadOnly();
920 owner->buffer()->setReadonly(
921 !owner->buffer()->isReadonly());
924 case LFUN_CENTER: // this is center and redraw.
928 // --- Menus -----------------------------------------------
930 menuNew(argument, false);
933 case LFUN_MENUNEWTMPLT:
934 menuNew(argument, true);
937 case LFUN_CLOSEBUFFER:
942 if (!owner->buffer()->isUnnamed()) {
943 string const str = bformat(_("Saving document %1$s..."),
944 MakeDisplayPath(owner->buffer()->fileName()));
946 MenuWrite(owner->buffer());
947 owner->message(str + _(" done."));
949 WriteAs(owner->buffer());
953 WriteAs(owner->buffer(), argument);
956 case LFUN_MENURELOAD: {
957 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
958 string text = bformat(_("Any changes will be lost. Are you sure "
959 "you want to revert to the saved version of the document %1$s?"), file);
960 int const ret = Alert::prompt(_("Revert to saved document?"),
961 text, 0, 1, _("&Revert"), _("&Cancel"));
969 Exporter::Export(owner->buffer(), argument, true);
970 view()->showErrorList(BufferFormat(*owner->buffer()));
974 Exporter::Preview(owner->buffer(), argument);
975 view()->showErrorList(BufferFormat(*owner->buffer()));
979 Exporter::Export(owner->buffer(), "program", true);
980 view()->showErrorList(_("Build"));
984 owner->buffer()->runChktex();
985 view()->showErrorList(_("ChkTeX"));
989 if (argument == "custom")
990 owner->getDialogs().showSendto();
992 Exporter::Export(owner->buffer(), argument, false);
993 view()->showErrorList(BufferFormat(*owner->buffer()));
1005 case LFUN_TOCVIEW: {
1006 InsetCommandParams p("tableofcontents");
1007 string const data = InsetCommandMailer::params2string("toc", p);
1008 owner->getDialogs().show("toc", data, 0);
1024 case LFUN_RECONFIGURE:
1025 Reconfigure(view());
1028 case LFUN_HELP_OPEN: {
1029 string const arg = argument;
1031 setErrorMessage(N_("Missing argument"));
1034 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1035 if (fname.empty()) {
1036 lyxerr << "LyX: unable to find documentation file `"
1037 << arg << "'. Bad installation?" << endl;
1040 owner->message(bformat(_("Opening help file %1$s..."),
1041 MakeDisplayPath(fname)));
1042 view()->loadLyXFile(fname, false);
1046 // --- version control -------------------------------
1047 case LFUN_VC_REGISTER:
1048 if (!ensureBufferClean(view()))
1050 if (!owner->buffer()->lyxvc().inUse()) {
1051 owner->buffer()->lyxvc().registrer();
1056 case LFUN_VC_CHECKIN:
1057 if (!ensureBufferClean(view()))
1059 if (owner->buffer()->lyxvc().inUse()
1060 && !owner->buffer()->isReadonly()) {
1061 owner->buffer()->lyxvc().checkIn();
1066 case LFUN_VC_CHECKOUT:
1067 if (!ensureBufferClean(view()))
1069 if (owner->buffer()->lyxvc().inUse()
1070 && owner->buffer()->isReadonly()) {
1071 owner->buffer()->lyxvc().checkOut();
1076 case LFUN_VC_REVERT:
1077 owner->buffer()->lyxvc().revert();
1082 owner->buffer()->lyxvc().undoLast();
1086 // --- buffers ----------------------------------------
1087 case LFUN_SWITCHBUFFER:
1088 view()->buffer(bufferlist.getBuffer(argument));
1092 NewFile(view(), argument);
1095 case LFUN_FILE_OPEN:
1099 case LFUN_LAYOUT_TABULAR:
1100 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1101 tab->openLayoutDialog(view());
1104 case LFUN_DROP_LAYOUTS_CHOICE:
1105 owner->getToolbar().openLayoutList();
1108 case LFUN_MENU_OPEN_BY_NAME:
1109 owner->getMenubar().openByName(argument);
1112 // --- lyxserver commands ----------------------------
1114 setMessage(owner->buffer()->fileName());
1115 lyxerr[Debug::INFO] << "FNAME["
1116 << owner->buffer()->fileName()
1121 dispatch_buffer = keyseq.print();
1122 lyxserver->notifyClient(dispatch_buffer);
1125 case LFUN_GOTOFILEROW: {
1128 istringstream is(argument);
1129 is >> file_name >> row;
1130 if (prefixIs(file_name, getTmpDir())) {
1131 // Needed by inverse dvi search. If it is a file
1132 // in tmpdir, call the apropriated function
1133 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1135 // Must replace extension of the file to be .lyx
1136 // and get full path
1137 string const s = ChangeExtension(file_name, ".lyx");
1138 // Either change buffer or load the file
1139 if (bufferlist.exists(s)) {
1140 view()->buffer(bufferlist.getBuffer(s));
1142 view()->loadLyXFile(s);
1146 view()->setCursorFromRow(row);
1149 // see BufferView_pimpl::center()
1150 view()->updateScrollbar();
1154 case LFUN_GOTO_PARAGRAPH: {
1155 istringstream is(argument);
1158 ParIterator par = owner->buffer()->getParFromID(id);
1159 if (par == owner->buffer()->par_iterator_end()) {
1160 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1161 << id << ']' << endl;
1164 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1165 << " found." << endl;
1168 par.lockPath(view());
1169 LyXText * lt = par.text(view());
1172 lt->setCursor(par.pit(), 0);
1173 view()->switchKeyMap();
1174 owner->view_state_changed();
1177 // see BufferView_pimpl::center()
1178 view()->updateScrollbar();
1182 // --- Mathed stuff. If we are here, there is no locked inset yet.
1183 case LFUN_MATH_EXTERN:
1184 case LFUN_MATH_NUMBER:
1185 case LFUN_MATH_NONUMBER:
1186 case LFUN_MATH_LIMITS:
1187 setErrorMessage(N_("This is only allowed in math mode!"));
1190 // passthrough hat and underscore outside mathed:
1191 case LFUN_SUBSCRIPT:
1192 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1195 case LFUN_SUPERSCRIPT:
1196 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1199 case LFUN_DIALOG_SHOW: {
1200 string const name = func.getArg(0);
1201 string data = trim(func.argument.substr(name.size()));
1203 if (name == "character") {
1204 data = freefont2string();
1206 owner->getDialogs().show("character", data);
1207 } else if (name == "document")
1208 owner->getDialogs().showDocument();
1209 else if (name == "findreplace")
1210 owner->getDialogs().showSearch();
1211 else if (name == "forks")
1212 owner->getDialogs().showForks();
1213 else if (name == "preamble")
1214 owner->getDialogs().showPreamble();
1215 else if (name == "preferences")
1216 owner->getDialogs().showPreferences();
1217 else if (name == "print")
1218 owner->getDialogs().showPrint();
1219 else if (name == "spellchecker")
1220 owner->getDialogs().showSpellchecker();
1222 owner->getDialogs().show(name, data);
1226 case LFUN_DIALOG_SHOW_NEW_INSET: {
1227 string const & name = argument;
1229 if (name == "bibitem" ||
1231 name == "include" ||
1236 InsetCommandParams p(name);
1237 data = InsetCommandMailer::params2string(name, p);
1238 } else if (name == "citation") {
1239 InsetCommandParams p("cite");
1240 data = InsetCommandMailer::params2string(name, p);
1241 } else if (name == "vspace") {
1243 data = InsetVSpaceMailer::params2string(space);
1245 owner->getDialogs().show(name, data, 0);
1249 case LFUN_DIALOG_SHOW_NEXT_INSET:
1252 case LFUN_INSET_DIALOG_SHOW: {
1253 InsetOld * inset = view()->getLyXText()->getInset();
1255 inset->dispatch(FuncRequest(view(), LFUN_INSET_DIALOG_SHOW));
1259 case LFUN_DIALOG_UPDATE: {
1260 string const & name = argument;
1261 // Can only update a dialog connected to an existing inset
1262 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1264 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1266 inset->dispatch(fr);
1267 } else if (name == "paragraph") {
1268 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1273 case LFUN_DIALOG_HIDE:
1274 Dialogs::hide(argument, 0);
1277 case LFUN_DIALOG_DISCONNECT_INSET:
1278 owner->getDialogs().disconnect(argument);
1281 case LFUN_CHILDOPEN: {
1282 string const filename =
1283 MakeAbsPath(argument, owner->buffer()->filePath());
1284 setMessage(N_("Opening child document ") +
1285 MakeDisplayPath(filename) + "...");
1286 view()->savePosition(0);
1287 string const parentfilename = owner->buffer()->fileName();
1288 if (bufferlist.exists(filename))
1289 view()->buffer(bufferlist.getBuffer(filename));
1291 view()->loadLyXFile(filename);
1292 // Set the parent name of the child document.
1293 // This makes insertion of citations and references in the child work,
1294 // when the target is in the parent or another child document.
1295 owner->buffer()->setParentName(parentfilename);
1299 case LFUN_TOGGLECURSORFOLLOW:
1300 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1304 owner->getIntl().KeyMapOn(false);
1307 case LFUN_KMAP_PRIM:
1308 owner->getIntl().KeyMapPrim();
1312 owner->getIntl().KeyMapSec();
1315 case LFUN_KMAP_TOGGLE:
1316 owner->getIntl().ToggleKeyMap();
1322 argument = split(argument, countstr, ' ');
1323 istringstream is(countstr);
1326 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1327 for (int i = 0; i < count; ++i)
1328 dispatch(lyxaction.lookupFunc(argument));
1333 // argument contains ';'-terminated commands
1334 while (!argument.empty()) {
1336 argument = split(argument, first, ';');
1337 dispatch(lyxaction.lookupFunc(first));
1341 case LFUN_SAVEPREFERENCES: {
1342 Path p(user_lyxdir());
1343 lyxrc.write("preferences");
1347 case LFUN_SCREEN_FONT_UPDATE:
1348 // handle the screen font changes.
1349 lyxrc.set_font_norm_type();
1350 lyx_gui::update_fonts();
1351 // All visible buffers will need resize
1355 case LFUN_SET_COLOR: {
1357 string const x11_name = split(argument, lyx_name, ' ');
1358 if (lyx_name.empty() || x11_name.empty()) {
1359 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1364 bool const graphicsbg_changed =
1365 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1366 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1368 if (!lcolor.setColor(lyx_name, x11_name)) {
1370 bformat(_("Set-color \"%1$s\" failed "
1371 "- color is undefined or "
1372 "may not be redefined"), lyx_name));
1376 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1378 if (graphicsbg_changed) {
1379 #ifdef WITH_WARNINGS
1380 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1383 lyx::graphics::GCache::get().changeDisplay(true);
1390 owner->message(argument);
1393 case LFUN_FORKS_KILL:
1394 if (isStrInt(argument))
1395 ForkedcallsController::get().kill(strToInt(argument));
1398 case LFUN_TOOLTIPS_TOGGLE:
1399 owner->getDialogs().toggleTooltips();
1402 case LFUN_EXTERNAL_EDIT:
1403 InsetExternal().dispatch(FuncRequest(view(), action, argument));
1407 DispatchResult result =
1408 view()->cursor().dispatch(FuncRequest(func, view()));
1409 if (result.dispatched())
1410 lyxerr << "dispatched by Cursor::dispatch()" << endl;
1412 lyxerr << "### NOT DISPATCHED BY Cursor::dispatch() ###" << endl;
1418 view()->owner()->updateLayoutChoice();
1420 if (view()->available()) {
1421 view()->fitCursor();
1423 view()->cursor().updatePos();
1424 // if we executed a mutating lfun, mark the buffer as dirty
1425 if (!getStatus(func).disabled()
1426 && !lyxaction.funcHasFlag(func.action, LyXAction::NoBuffer)
1427 && !lyxaction.funcHasFlag(func.action, LyXAction::ReadOnly))
1428 view()->buffer()->markDirty();
1431 sendDispatchMessage(getMessage(), func, verbose);
1435 void LyXFunc::sendDispatchMessage(string const & msg,
1436 FuncRequest const & func, bool verbose)
1438 owner->updateMenubar();
1439 owner->updateToolbar();
1441 if (func.action == LFUN_SELFINSERT || !verbose) {
1442 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1444 owner->message(msg);
1448 string dispatch_msg = msg;
1449 if (!dispatch_msg.empty())
1450 dispatch_msg += ' ';
1452 string comname = lyxaction.getActionName(func.action);
1454 bool argsadded = false;
1456 if (!func.argument.empty()) {
1457 if (func.action != LFUN_UNKNOWN_ACTION) {
1458 comname += ' ' + func.argument;
1463 string const shortcuts = toplevel_keymap->findbinding(func);
1465 if (!shortcuts.empty()) {
1466 comname += ": " + shortcuts;
1467 } else if (!argsadded && !func.argument.empty()) {
1468 comname += ' ' + func.argument;
1471 if (!comname.empty()) {
1472 comname = rtrim(comname);
1473 dispatch_msg += '(' + comname + ')';
1476 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1477 if (!dispatch_msg.empty())
1478 owner->message(dispatch_msg);
1482 void LyXFunc::setupLocalKeymap()
1484 keyseq.stdmap = toplevel_keymap.get();
1485 keyseq.curmap = toplevel_keymap.get();
1486 cancel_meta_seq.stdmap = toplevel_keymap.get();
1487 cancel_meta_seq.curmap = toplevel_keymap.get();
1491 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1493 string initpath = lyxrc.document_path;
1494 string filename(name);
1496 if (view()->available()) {
1497 string const trypath = owner->buffer()->filePath();
1498 // If directory is writeable, use this as default.
1499 if (IsDirWriteable(trypath))
1503 static int newfile_number;
1505 if (filename.empty()) {
1506 filename = AddName(lyxrc.document_path,
1507 "newfile" + tostr(++newfile_number) + ".lyx");
1508 FileInfo fi(filename);
1509 while (bufferlist.exists(filename) || fi.readable()) {
1511 filename = AddName(lyxrc.document_path,
1512 "newfile" + tostr(newfile_number) +
1514 fi.newFile(filename);
1518 // The template stuff
1521 FileDialog fileDlg(_("Select template file"),
1522 LFUN_SELECT_FILE_SYNC,
1523 make_pair(string(_("Documents|#o#O")),
1524 string(lyxrc.document_path)),
1525 make_pair(string(_("Templates|#T#t")),
1526 string(lyxrc.template_path)));
1528 FileDialog::Result result =
1529 fileDlg.open(lyxrc.template_path,
1530 _("*.lyx| LyX Documents (*.lyx)"));
1532 if (result.first == FileDialog::Later)
1534 if (result.second.empty())
1536 templname = result.second;
1539 view()->newFile(filename, templname, !name.empty());
1543 void LyXFunc::open(string const & fname)
1545 string initpath = lyxrc.document_path;
1547 if (view()->available()) {
1548 string const trypath = owner->buffer()->filePath();
1549 // If directory is writeable, use this as default.
1550 if (IsDirWriteable(trypath))
1556 if (fname.empty()) {
1557 FileDialog fileDlg(_("Select document to open"),
1559 make_pair(string(_("Documents|#o#O")),
1560 string(lyxrc.document_path)),
1561 make_pair(string(_("Examples|#E#e")),
1562 string(AddPath(system_lyxdir(), "examples"))));
1564 FileDialog::Result result =
1565 fileDlg.open(initpath,
1566 _("*.lyx| LyX Documents (*.lyx)"));
1568 if (result.first == FileDialog::Later)
1571 filename = result.second;
1573 // check selected filename
1574 if (filename.empty()) {
1575 owner->message(_("Canceled."));
1581 // get absolute path of file and add ".lyx" to the filename if
1583 string const fullpath = FileSearch(string(), filename, "lyx");
1584 if (!fullpath.empty()) {
1585 filename = fullpath;
1588 string const disp_fn(MakeDisplayPath(filename));
1590 // if the file doesn't exist, let the user create one
1591 FileInfo const f(filename, true);
1593 // the user specifically chose this name. Believe them.
1594 view()->newFile(filename, "", true);
1598 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1601 if (view()->loadLyXFile(filename)) {
1602 str2 = bformat(_("Document %1$s opened."), disp_fn);
1604 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1606 owner->message(str2);
1610 void LyXFunc::doImport(string const & argument)
1613 string filename = split(argument, format, ' ');
1615 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1616 << " file: " << filename << endl;
1618 // need user interaction
1619 if (filename.empty()) {
1620 string initpath = lyxrc.document_path;
1622 if (view()->available()) {
1623 string const trypath = owner->buffer()->filePath();
1624 // If directory is writeable, use this as default.
1625 if (IsDirWriteable(trypath))
1629 string const text = bformat(_("Select %1$s file to import"),
1630 formats.prettyName(format));
1632 FileDialog fileDlg(text,
1634 make_pair(string(_("Documents|#o#O")),
1635 string(lyxrc.document_path)),
1636 make_pair(string(_("Examples|#E#e")),
1637 string(AddPath(system_lyxdir(), "examples"))));
1639 string const extension = "*." + formats.extension(format)
1640 + "| " + formats.prettyName(format)
1641 + " (*." + formats.extension(format) + ')';
1643 FileDialog::Result result = fileDlg.open(initpath,
1646 if (result.first == FileDialog::Later)
1649 filename = result.second;
1651 // check selected filename
1652 if (filename.empty())
1653 owner->message(_("Canceled."));
1656 if (filename.empty())
1659 // get absolute path of file
1660 filename = MakeAbsPath(filename);
1662 string const lyxfile = ChangeExtension(filename, ".lyx");
1664 // Check if the document already is open
1665 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1666 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1667 owner->message(_("Canceled."));
1672 // if the file exists already, and we didn't do
1673 // -i lyx thefile.lyx, warn
1674 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1675 string const file = MakeDisplayPath(lyxfile, 30);
1677 string text = bformat(_("The document %1$s already exists.\n\n"
1678 "Do you want to over-write that document?"), file);
1679 int const ret = Alert::prompt(_("Over-write document?"),
1680 text, 0, 1, _("&Over-write"), _("&Cancel"));
1683 owner->message(_("Canceled."));
1688 Importer::Import(owner, filename, format);
1692 void LyXFunc::closeBuffer()
1694 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1695 if (bufferlist.empty()) {
1696 // need this otherwise SEGV may occur while
1697 // trying to set variables that don't exist
1698 // since there's no current buffer
1699 owner->getDialogs().hideBufferDependent();
1701 view()->buffer(bufferlist.first());
1707 // Each "owner" should have it's own message method. lyxview and
1708 // the minibuffer would use the minibuffer, but lyxserver would
1709 // send an ERROR signal to its client. Alejandro 970603
1710 // This func is bit problematic when it comes to NLS, to make the
1711 // lyx servers client be language indepenent we must not translate
1712 // strings sent to this func.
1713 void LyXFunc::setErrorMessage(string const & m) const
1715 dispatch_buffer = m;
1720 void LyXFunc::setMessage(string const & m) const
1722 dispatch_buffer = m;
1726 void LyXFunc::setStatusMessage(string const & m) const
1732 string const LyXFunc::view_status_message()
1734 // When meta-fake key is pressed, show the key sequence so far + "M-".
1736 return keyseq.print() + "M-";
1739 // Else, when a non-complete key sequence is pressed,
1740 // show the available options.
1741 if (keyseq.length() > 0 && !keyseq.deleted()) {
1742 return keyseq.printOptions();
1745 if (!view()->available())
1746 return _("Welcome to LyX!");
1748 return currentState(view());
1752 BufferView * LyXFunc::view() const
1754 BOOST_ASSERT(owner);
1755 return owner->view().get();
1759 bool LyXFunc::wasMetaKey() const
1761 return (meta_fake_bit != key_modifier::none);