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/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"
66 #include "mathed/math_cursor.h"
68 #include "frontends/Alert.h"
69 #include "frontends/Dialogs.h"
70 #include "frontends/FileDialog.h"
71 #include "frontends/lyx_gui.h"
72 #include "frontends/LyXKeySym.h"
73 #include "frontends/LyXView.h"
74 #include "frontends/Menubar.h"
75 #include "frontends/Toolbar.h"
77 #include "support/FileInfo.h"
78 #include "support/filetools.h"
79 #include "support/forkedcontr.h"
80 #include "support/path.h"
81 #include "support/path_defines.h"
82 #include "support/tostr.h"
83 #include "support/std_sstream.h"
84 #include "support/os.h"
86 using bv_funcs::changeDepth;
87 using bv_funcs::currentState;
88 using bv_funcs::DEC_DEPTH;
89 using bv_funcs::freefont2string;
90 using bv_funcs::INC_DEPTH;
92 using lyx::support::AddName;
93 using lyx::support::AddPath;
94 using lyx::support::bformat;
95 using lyx::support::ChangeExtension;
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;
118 using std::make_pair;
121 using std::istringstream;
124 extern BufferList bufferlist;
125 extern LyXServer * lyxserver;
126 extern bool selection_possible;
128 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
131 extern tex_accent_struct get_accent(kb_action action);
134 LyXFunc::LyXFunc(LyXView * lv)
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)
144 void LyXFunc::handleKeyFunc(kb_action action)
146 char c = encoded_last_key;
148 if (keyseq.length()) {
152 owner->getIntl().getTransManager()
153 .deadkey(c, get_accent(action).accent, view()->getLyXText());
154 // Need to clear, in case the minibuffer calls these
157 // copied verbatim from do_accent_char
159 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
163 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
164 key_modifier::state state)
168 if (lyxerr.debugging(Debug::KEY)) {
169 lyxerr << "KeySym is "
170 << keysym->getSymbolName()
174 // Do nothing if we have nothing (JMarc)
175 if (!keysym->isOK()) {
176 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
181 if (keysym->isModifier()) {
182 lyxerr[Debug::KEY] << "isModifier true" << endl;
186 Encoding const * encoding = view()->getEncoding();
188 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
190 // Do a one-deep top-level lookup for
191 // cancel and meta-fake keys. RVDK_PATCH_5
192 cancel_meta_seq.reset();
194 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
195 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
197 // When not cancel or meta-fake, do the normal lookup.
198 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
199 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
200 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
201 // remove Caps Lock and Mod2 as a modifiers
202 func = keyseq.addkey(keysym, (state | meta_fake_bit));
203 lyxerr[Debug::KEY] << "action now set to ["
204 << func.action << ']' << endl;
207 // Dont remove this unless you know what you are doing.
208 meta_fake_bit = key_modifier::none;
210 // can this happen now ?
211 if (func.action == LFUN_NOACTION) {
212 func = FuncRequest(LFUN_PREFIX);
215 if (lyxerr.debugging(Debug::KEY)) {
216 lyxerr << "Key [action="
217 << func.action << "]["
218 << keyseq.print() << ']'
222 // already here we know if it any point in going further
223 // why not return already here if action == -1 and
224 // num_bytes == 0? (Lgb)
226 if (keyseq.length() > 1) {
227 owner->message(keyseq.print());
231 // Maybe user can only reach the key via holding down shift.
232 // Let's see. But only if shift is the only modifier
233 if (func.action == LFUN_UNKNOWN_ACTION &&
234 state == key_modifier::shift) {
235 lyxerr[Debug::KEY] << "Trying without shift" << endl;
236 func = keyseq.addkey(keysym, key_modifier::none);
237 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
240 if (func.action == LFUN_UNKNOWN_ACTION) {
241 // Hmm, we didn't match any of the keysequences. See
242 // if it's normal insertable text not already covered
244 if (keysym->isText() && keyseq.length() == 1) {
245 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
246 func = FuncRequest(LFUN_SELFINSERT);
248 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
249 owner->message(_("Unknown function."));
254 if (func.action == LFUN_SELFINSERT) {
255 if (encoded_last_key != 0) {
257 arg += encoded_last_key;
259 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
261 lyxerr[Debug::KEY] << "SelfInsert arg[`"
262 << argument << "']" << endl;
270 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
273 Buffer * buf = owner->buffer();
275 if (ev.action == LFUN_NOACTION) {
276 setStatusMessage(N_("Nothing to do"));
282 case LFUN_UNKNOWN_ACTION:
283 #ifndef HAVE_LIBAIKSAURUS
284 case LFUN_THESAURUS_ENTRY:
290 flag |= lyx_gui::getStatus(ev);
293 if (flag.unknown()) {
294 setStatusMessage(N_("Unknown action"));
298 // the default error message if we disable the command
299 setStatusMessage(N_("Command disabled"));
301 // Check whether we need a buffer
302 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
303 // Yes we need a buffer, do we have one?
306 // Can we use a readonly buffer?
307 if (buf->isReadonly() &&
308 !lyxaction.funcHasFlag(ev.action,
309 LyXAction::ReadOnly)) {
311 setStatusMessage(N_("Document is read-only"));
316 setStatusMessage(N_("Command not allowed with"
317 "out any document open"));
323 UpdatableInset * tli = view()->cursor().innerInset();
324 InsetTabular * tab = view()->cursor().innerInsetTabular();
326 // I would really like to avoid having this switch and rather try to
327 // encode this in the function itself.
328 bool disable = false;
331 disable = ev.argument != "custom"
332 && !Exporter::IsExportable(*buf, ev.argument);
335 disable = buf->undostack().empty();
338 disable = buf->redostack().empty();
342 if (tab && tab->hasSelection())
345 disable = !mathcursor && !view()->getLyXText()->selection.set();
349 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
353 disable = !Exporter::IsExportable(*buf, "program");
356 case LFUN_LAYOUT_TABULAR:
357 disable = !view()->cursor().innerInsetTabular();
361 disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH);
364 case LFUN_DEPTH_PLUS:
365 disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH);
369 case LFUN_LAYOUT_PARAGRAPH: {
370 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
371 disable = inset && inset->forceDefaultParagraphs(inset);
375 case LFUN_INSET_OPTARG:
376 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
379 case LFUN_TABULAR_FEATURE:
382 // FIXME: check temporarily disabled
384 char align = mathcursor->valign();
389 if (ev.argument.empty()) {
393 if (!contains("tcb", ev.argument[0])) {
397 flag.setOnOff(ev.argument[0] == align);
401 char align = mathcursor->halign();
406 if (ev.argument.empty()) {
410 if (!contains("lcr", ev.argument[0])) {
414 flag.setOnOff(ev.argument[0] == align);
416 disable = !mathcursor->halign();
422 //ret.disabled(true);
423 InsetTabular * tab = view()->cursor().innerInsetTabular();
425 ret = tab->getStatus(ev.argument);
432 static InsetTabular inset(*buf, 1, 1);
434 FuncStatus ret = inset.getStatus(ev.argument);
435 if (ret.onoff(true) || ret.onoff(false))
436 flag.setOnOff(false);
441 case LFUN_VC_REGISTER:
442 disable = buf->lyxvc().inUse();
444 case LFUN_VC_CHECKIN:
445 disable = !buf->lyxvc().inUse() || buf->isReadonly();
447 case LFUN_VC_CHECKOUT:
448 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
452 disable = !buf->lyxvc().inUse();
454 case LFUN_MENURELOAD:
455 disable = buf->isUnnamed() || buf->isClean();
457 case LFUN_BOOKMARK_GOTO:
459 isSavedPosition(strToUnsignedInt(ev.argument));
462 case LFUN_MERGE_CHANGES:
463 case LFUN_ACCEPT_CHANGE:
464 case LFUN_REJECT_CHANGE:
465 case LFUN_ACCEPT_ALL_CHANGES:
466 case LFUN_REJECT_ALL_CHANGES:
467 disable = !buf->params().tracking_changes;
470 case LFUN_INSET_SETTINGS: {
472 UpdatableInset * inset = view()->cursor().innerInset();
477 // jump back to owner if an InsetText, so
478 // we get back to the InsetTabular or whatever
479 if (inset->lyxCode() == InsetOld::TEXT_CODE)
480 inset = inset->owner();
482 InsetOld::Code code = inset->lyxCode();
484 case InsetOld::TABULAR_CODE:
485 disable = ev.argument != "tabular";
487 case InsetOld::ERT_CODE:
488 disable = ev.argument != "ert";
490 case InsetOld::FLOAT_CODE:
491 disable = ev.argument != "float";
493 case InsetOld::WRAP_CODE:
494 disable = ev.argument != "wrap";
496 case InsetOld::NOTE_CODE:
497 disable = ev.argument != "note";
499 case InsetOld::BRANCH_CODE:
500 disable = ev.argument != "branch";
502 case InsetOld::BOX_CODE:
503 disable = ev.argument != "box";
511 case LFUN_MATH_MUTATE:
513 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
514 flag.setOnOff(false);
519 // we just need to be in math mode to enable that
521 case LFUN_MATH_SPACE:
522 case LFUN_MATH_LIMITS:
523 case LFUN_MATH_NONUMBER:
524 case LFUN_MATH_NUMBER:
525 case LFUN_MATH_EXTERN:
526 disable = !mathcursor;
529 case LFUN_DIALOG_SHOW: {
530 string const name = ev.getArg(0);
532 disable = !(name == "aboutlyx" ||
535 name == "preferences" ||
540 if (name == "print") {
541 disable = !Exporter::IsExportable(*buf, "dvi") ||
542 lyxrc.print_command == "none";
543 } else if (name == "character") {
544 UpdatableInset * tli = view()->cursor().innerInset();
545 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
546 } else if (name == "vclog") {
547 disable = !buf->lyxvc().inUse();
548 } else if (name == "latexlog") {
549 disable = !IsFileReadable(buf->getLogName().second);
558 // the functions which insert insets
559 InsetOld::Code code = InsetOld::NO_CODE;
561 case LFUN_DIALOG_SHOW_NEW_INSET:
562 if (ev.argument == "bibitem")
563 code = InsetOld::BIBITEM_CODE;
564 else if (ev.argument == "bibtex")
565 code = InsetOld::BIBTEX_CODE;
566 else if (ev.argument == "box")
567 code = InsetOld::BOX_CODE;
568 else if (ev.argument == "branch")
569 code = InsetOld::BRANCH_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 == "note")
587 code = InsetOld::NOTE_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_FLOAT:
613 case LFUN_INSET_WIDE_FLOAT:
614 code = InsetOld::FLOAT_CODE;
616 case LFUN_INSET_WRAP:
617 code = InsetOld::WRAP_CODE;
619 case LFUN_FLOAT_LIST:
620 code = InsetOld::FLOAT_LIST_CODE;
623 case LFUN_INSET_LIST:
624 code = InsetOld::LIST_CODE;
626 case LFUN_INSET_THEOREM:
627 code = InsetOld::THEOREM_CODE;
630 case LFUN_INSET_CAPTION:
631 code = InsetOld::CAPTION_CODE;
633 case LFUN_INSERT_NOTE:
634 code = InsetOld::NOTE_CODE;
636 case LFUN_INSERT_CHARSTYLE:
637 code = InsetOld::CHARSTYLE_CODE;
638 if (buf->params().getLyXTextClass().charstyles().empty())
641 case LFUN_INSERT_BOX:
642 code = InsetOld::BOX_CODE;
644 case LFUN_INSERT_BRANCH:
645 code = InsetOld::BRANCH_CODE;
646 if (buf->params().branchlist().empty())
649 case LFUN_INSERT_LABEL:
650 code = InsetOld::LABEL_CODE;
652 case LFUN_INSET_OPTARG:
653 code = InsetOld::OPTARG_CODE;
655 case LFUN_ENVIRONMENT_INSERT:
656 code = InsetOld::BOX_CODE;
658 case LFUN_INDEX_INSERT:
659 code = InsetOld::INDEX_CODE;
661 case LFUN_INDEX_PRINT:
662 code = InsetOld::INDEX_PRINT_CODE;
664 case LFUN_TOC_INSERT:
665 code = InsetOld::TOC_CODE;
669 code = InsetOld::URL_CODE;
672 // always allow this, since we will inset a raw quote
673 // if an inset is not allowed.
675 case LFUN_HYPHENATION:
676 case LFUN_LIGATURE_BREAK:
678 case LFUN_MENU_SEPARATOR:
680 case LFUN_END_OF_SENTENCE:
681 code = InsetOld::SPECIALCHAR_CODE;
683 case LFUN_SPACE_INSERT:
684 // slight hack: we know this is allowed in math mode
686 code = InsetOld::SPACE_CODE;
688 case LFUN_INSET_DIALOG_SHOW: {
689 InsetOld * inset = view()->getLyXText()->getInset();
692 code = inset->lyxCode();
693 if (!(code == InsetOld::INCLUDE_CODE
694 || code == InsetOld::BIBTEX_CODE
695 || code == InsetOld::FLOAT_LIST_CODE
696 || code == InsetOld::TOC_CODE))
704 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
710 // A few general toggles
712 case LFUN_TOOLTIPS_TOGGLE:
713 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
716 case LFUN_READ_ONLY_TOGGLE:
717 flag.setOnOff(buf->isReadonly());
720 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
722 case LFUN_SWITCHBUFFER:
723 // toggle on the current buffer, but do not toggle off
724 // the other ones (is that a good idea?)
725 if (ev.argument == buf->fileName())
728 case LFUN_TRACK_CHANGES:
729 flag.setOnOff(buf->params().tracking_changes);
736 // the font related toggles
738 LyXFont const & font = view()->getLyXText()->real_current_font;
741 flag.setOnOff(font.emph() == LyXFont::ON);
744 flag.setOnOff(font.noun() == LyXFont::ON);
747 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
750 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
753 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
756 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
762 string tc = mathcursor->getLastCode();
765 flag.setOnOff(tc == "mathbf");
768 flag.setOnOff(tc == "mathsf");
771 flag.setOnOff(tc == "mathcal");
774 flag.setOnOff(tc == "mathrm");
777 flag.setOnOff(tc == "mathtt");
780 flag.setOnOff(tc == "mathbb");
783 flag.setOnOff(tc == "mathnormal");
791 // this one is difficult to get right. As a half-baked
792 // solution, we consider only the first action of the sequence
793 if (ev.action == LFUN_SEQUENCE) {
794 // argument contains ';'-terminated commands
795 #warning LyXAction arguments not handled here.
796 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
805 bool ensureBufferClean(BufferView * bv)
807 Buffer & buf = *bv->buffer();
811 string const file = MakeDisplayPath(buf.fileName(), 30);
812 string text = bformat(_("The document %1$s has unsaved "
813 "changes.\n\nDo you want to save "
814 "the document?"), file);
815 int const ret = Alert::prompt(_("Save changed document?"),
816 text, 0, 1, _("&Save"),
820 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
822 return buf.isClean();
828 void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
830 string argument = func.argument;
831 kb_action action = func.action;
833 //lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << func << endl;
834 lyxerr << "LyXFunc::dispatch: cmd: " << func << endl;
836 // we have not done anything wrong yet.
838 dispatch_buffer.erase();
840 #ifdef NEW_DISPATCHER
841 // We try do call the most specific dispatcher first:
842 // 1. the lockinginset's dispatch
843 // 2. the bufferview's dispatch
844 // 3. the lyxview's dispatch
847 selection_possible = false;
849 // We cannot use this function here
850 if (getStatus(func).disabled()) {
851 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
852 << lyxaction.getActionName(action)
853 << " [" << action << "] is disabled at this location"
855 setErrorMessage(getStatusMessage());
859 if (view()->available())
860 view()->hideCursor();
865 if (!view()->available())
867 view()->cursor().pop();
868 // Tell the paragraph dialog that we changed paragraph
869 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
873 case LFUN_WORDFINDFORWARD:
874 case LFUN_WORDFINDBACKWARD: {
875 static string last_search;
876 string searched_string;
878 if (!argument.empty()) {
879 last_search = argument;
880 searched_string = argument;
882 searched_string = last_search;
884 bool fw = (action == LFUN_WORDFINDFORWARD);
885 if (!searched_string.empty())
886 lyx::find::find(view(), searched_string,
892 owner->message(keyseq.printOptions());
895 case LFUN_EXEC_COMMAND:
896 owner->focus_command_buffer();
901 meta_fake_bit = key_modifier::none;
902 if (view()->available())
903 // cancel any selection
904 dispatch(FuncRequest(LFUN_MARK_OFF));
905 setMessage(N_("Cancel"));
909 meta_fake_bit = key_modifier::alt;
910 setMessage(keyseq.print());
913 case LFUN_READ_ONLY_TOGGLE:
914 if (owner->buffer()->lyxvc().inUse())
915 owner->buffer()->lyxvc().toggleReadOnly();
917 owner->buffer()->setReadonly(
918 !owner->buffer()->isReadonly());
921 case LFUN_CENTER: // this is center and redraw.
925 // --- Menus -----------------------------------------------
927 menuNew(argument, false);
930 case LFUN_MENUNEWTMPLT:
931 menuNew(argument, true);
934 case LFUN_CLOSEBUFFER:
939 if (!owner->buffer()->isUnnamed()) {
940 string const str = bformat(_("Saving document %1$s..."),
941 MakeDisplayPath(owner->buffer()->fileName()));
943 MenuWrite(owner->buffer());
944 owner->message(str + _(" done."));
946 WriteAs(owner->buffer());
950 WriteAs(owner->buffer(), argument);
953 case LFUN_MENURELOAD: {
954 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
955 string text = bformat(_("Any changes will be lost. Are you sure "
956 "you want to revert to the saved version of the document %1$s?"), file);
957 int const ret = Alert::prompt(_("Revert to saved document?"),
958 text, 0, 1, _("&Revert"), _("&Cancel"));
966 Exporter::Export(owner->buffer(), argument, true);
967 view()->showErrorList(BufferFormat(*owner->buffer()));
971 Exporter::Preview(owner->buffer(), argument);
972 view()->showErrorList(BufferFormat(*owner->buffer()));
976 Exporter::Export(owner->buffer(), "program", true);
977 view()->showErrorList(_("Build"));
981 owner->buffer()->runChktex();
982 view()->showErrorList(_("ChkTeX"));
986 if (argument == "custom")
987 owner->getDialogs().showSendto();
989 Exporter::Export(owner->buffer(), argument, false);
990 view()->showErrorList(BufferFormat(*owner->buffer()));
1002 case LFUN_TOCVIEW: {
1003 InsetCommandParams p("tableofcontents");
1004 string const data = InsetCommandMailer::params2string("toc", p);
1005 owner->getDialogs().show("toc", data, 0);
1021 case LFUN_RECONFIGURE:
1022 Reconfigure(view());
1025 case LFUN_HELP_OPEN: {
1026 string const arg = argument;
1028 setErrorMessage(N_("Missing argument"));
1031 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1032 if (fname.empty()) {
1033 lyxerr << "LyX: unable to find documentation file `"
1034 << arg << "'. Bad installation?" << endl;
1037 owner->message(bformat(_("Opening help file %1$s..."),
1038 MakeDisplayPath(fname)));
1039 view()->loadLyXFile(fname, false);
1043 // --- version control -------------------------------
1044 case LFUN_VC_REGISTER:
1045 if (!ensureBufferClean(view()))
1047 if (!owner->buffer()->lyxvc().inUse()) {
1048 owner->buffer()->lyxvc().registrer();
1053 case LFUN_VC_CHECKIN:
1054 if (!ensureBufferClean(view()))
1056 if (owner->buffer()->lyxvc().inUse()
1057 && !owner->buffer()->isReadonly()) {
1058 owner->buffer()->lyxvc().checkIn();
1063 case LFUN_VC_CHECKOUT:
1064 if (!ensureBufferClean(view()))
1066 if (owner->buffer()->lyxvc().inUse()
1067 && owner->buffer()->isReadonly()) {
1068 owner->buffer()->lyxvc().checkOut();
1073 case LFUN_VC_REVERT:
1074 owner->buffer()->lyxvc().revert();
1079 owner->buffer()->lyxvc().undoLast();
1083 // --- buffers ----------------------------------------
1084 case LFUN_SWITCHBUFFER:
1085 view()->buffer(bufferlist.getBuffer(argument));
1089 NewFile(view(), argument);
1092 case LFUN_FILE_OPEN:
1096 case LFUN_LAYOUT_TABULAR:
1097 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1098 tab->openLayoutDialog(view());
1101 case LFUN_DROP_LAYOUTS_CHOICE:
1102 owner->getToolbar().openLayoutList();
1105 case LFUN_MENU_OPEN_BY_NAME:
1106 owner->getMenubar().openByName(argument);
1109 // --- lyxserver commands ----------------------------
1111 setMessage(owner->buffer()->fileName());
1112 lyxerr[Debug::INFO] << "FNAME["
1113 << owner->buffer()->fileName()
1118 dispatch_buffer = keyseq.print();
1119 lyxserver->notifyClient(dispatch_buffer);
1122 case LFUN_GOTOFILEROW: {
1125 istringstream is(argument);
1126 is >> file_name >> row;
1127 if (prefixIs(file_name, getTmpDir())) {
1128 // Needed by inverse dvi search. If it is a file
1129 // in tmpdir, call the apropriated function
1130 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1132 // Must replace extension of the file to be .lyx
1133 // and get full path
1134 string const s = ChangeExtension(file_name, ".lyx");
1135 // Either change buffer or load the file
1136 if (bufferlist.exists(s)) {
1137 view()->buffer(bufferlist.getBuffer(s));
1139 view()->loadLyXFile(s);
1143 view()->setCursorFromRow(row);
1146 // see BufferView_pimpl::center()
1147 view()->updateScrollbar();
1151 case LFUN_GOTO_PARAGRAPH: {
1152 istringstream is(argument);
1155 ParIterator par = owner->buffer()->getParFromID(id);
1156 if (par == owner->buffer()->par_iterator_end()) {
1157 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1158 << id << ']' << endl;
1161 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1162 << " found." << endl;
1165 par.lockPath(view());
1166 LyXText * lt = par.text(*view()->buffer());
1169 lt->setCursor(par.pit(), 0);
1170 view()->switchKeyMap();
1171 owner->view_state_changed();
1174 // see BufferView_pimpl::center()
1175 view()->updateScrollbar();
1179 // --- Mathed stuff. If we are here, there is no locked inset yet.
1180 case LFUN_MATH_EXTERN:
1181 case LFUN_MATH_NUMBER:
1182 case LFUN_MATH_NONUMBER:
1183 case LFUN_MATH_LIMITS:
1184 setErrorMessage(N_("This is only allowed in math mode!"));
1187 case LFUN_DIALOG_SHOW: {
1188 string const name = func.getArg(0);
1189 string data = trim(func.argument.substr(name.size()));
1191 if (name == "character") {
1192 data = freefont2string();
1194 owner->getDialogs().show("character", data);
1196 else if (name == "document")
1197 owner->getDialogs().showDocument();
1198 else if (name == "findreplace")
1199 owner->getDialogs().showSearch();
1200 else if (name == "forks")
1201 owner->getDialogs().showForks();
1202 else if (name == "preamble")
1203 owner->getDialogs().showPreamble();
1204 else if (name == "preferences")
1205 owner->getDialogs().showPreferences();
1206 else if (name == "print")
1207 owner->getDialogs().showPrint();
1208 else if (name == "spellchecker")
1209 owner->getDialogs().showSpellchecker();
1211 else if (name == "latexlog") {
1212 pair<Buffer::LogType, string> const logfile =
1213 owner->buffer()->getLogName();
1214 switch (logfile.first) {
1215 case Buffer::latexlog:
1218 case Buffer::buildlog:
1222 data += logfile.second;
1223 owner->getDialogs().show("log", data);
1225 else if (name == "vclog") {
1226 string const data = "vc " +
1227 owner->buffer()->lyxvc().getLogFile();
1228 owner->getDialogs().show("log", data);
1231 owner->getDialogs().show(name, data);
1235 case LFUN_DIALOG_SHOW_NEW_INSET: {
1236 string const name = func.getArg(0);
1237 string data = trim(func.argument.substr(name.size()));
1238 if (name == "bibitem" ||
1240 name == "include" ||
1246 InsetCommandParams p(name);
1247 data = InsetCommandMailer::params2string(name, p);
1248 } else if (name == "box") {
1249 // \c data == "Boxed" || "Frameless" etc
1250 InsetBoxParams p(data);
1251 data = InsetBoxMailer::params2string(p);
1252 } else if (name == "branch") {
1253 InsetBranchParams p;
1254 data = InsetBranchMailer::params2string(p);
1255 } else if (name == "citation") {
1256 InsetCommandParams p("cite");
1257 data = InsetCommandMailer::params2string(name, p);
1258 } else if (name == "ert") {
1259 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1260 } else if (name == "external") {
1261 InsetExternalParams p;
1262 Buffer const & buffer = *owner->buffer();
1263 data = InsetExternalMailer::params2string(p, buffer);
1264 } else if (name == "float") {
1266 data = InsetFloatMailer::params2string(p);
1267 } else if (name == "graphics") {
1268 InsetGraphicsParams p;
1269 Buffer const & buffer = *owner->buffer();
1270 data = InsetGraphicsMailer::params2string(p, buffer);
1271 } else if (name == "note") {
1273 data = InsetNoteMailer::params2string(p);
1274 } else if (name == "vspace") {
1276 data = InsetVSpaceMailer::params2string(space);
1277 } else if (name == "wrap") {
1279 data = InsetWrapMailer::params2string(p);
1281 owner->getDialogs().show(name, data, 0);
1285 case LFUN_DIALOG_SHOW_NEXT_INSET:
1288 case LFUN_INSET_DIALOG_SHOW: {
1289 InsetOld * inset = view()->getLyXText()->getInset();
1291 inset->dispatch(FuncRequest(view(), LFUN_INSET_DIALOG_SHOW));
1295 case LFUN_DIALOG_UPDATE: {
1296 string const & name = argument;
1297 // Can only update a dialog connected to an existing inset
1298 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1300 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1302 inset->dispatch(fr);
1303 } else if (name == "paragraph") {
1304 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1309 case LFUN_DIALOG_HIDE:
1310 Dialogs::hide(argument, 0);
1313 case LFUN_DIALOG_DISCONNECT_INSET:
1314 owner->getDialogs().disconnect(argument);
1317 case LFUN_CHILDOPEN: {
1318 string const filename =
1319 MakeAbsPath(argument, owner->buffer()->filePath());
1320 setMessage(N_("Opening child document ") +
1321 MakeDisplayPath(filename) + "...");
1322 view()->savePosition(0);
1323 string const parentfilename = owner->buffer()->fileName();
1324 if (bufferlist.exists(filename))
1325 view()->buffer(bufferlist.getBuffer(filename));
1327 view()->loadLyXFile(filename);
1328 // Set the parent name of the child document.
1329 // This makes insertion of citations and references in the child work,
1330 // when the target is in the parent or another child document.
1331 owner->buffer()->setParentName(parentfilename);
1335 case LFUN_TOGGLECURSORFOLLOW:
1336 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1340 owner->getIntl().KeyMapOn(false);
1343 case LFUN_KMAP_PRIM:
1344 owner->getIntl().KeyMapPrim();
1348 owner->getIntl().KeyMapSec();
1351 case LFUN_KMAP_TOGGLE:
1352 owner->getIntl().ToggleKeyMap();
1358 argument = split(argument, countstr, ' ');
1359 istringstream is(countstr);
1362 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1363 for (int i = 0; i < count; ++i)
1364 dispatch(lyxaction.lookupFunc(argument));
1369 // argument contains ';'-terminated commands
1370 while (!argument.empty()) {
1372 argument = split(argument, first, ';');
1373 dispatch(lyxaction.lookupFunc(first));
1377 case LFUN_SAVEPREFERENCES: {
1378 Path p(user_lyxdir());
1379 lyxrc.write("preferences");
1383 case LFUN_SCREEN_FONT_UPDATE:
1384 // handle the screen font changes.
1385 lyxrc.set_font_norm_type();
1386 lyx_gui::update_fonts();
1387 // All visible buffers will need resize
1391 case LFUN_SET_COLOR: {
1393 string const x11_name = split(argument, lyx_name, ' ');
1394 if (lyx_name.empty() || x11_name.empty()) {
1395 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1400 bool const graphicsbg_changed =
1401 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1402 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1404 LColor::color col = lcolor.getFromLyXName(lyx_name);
1405 if (!lcolor.setColor(col, x11_name)) {
1407 bformat(_("Set-color \"%1$s\" failed "
1408 "- color is undefined or "
1409 "may not be redefined"), lyx_name));
1413 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1415 if (graphicsbg_changed) {
1416 #ifdef WITH_WARNINGS
1417 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1420 lyx::graphics::GCache::get().changeDisplay(true);
1427 owner->message(argument);
1430 case LFUN_FORKS_KILL:
1431 if (isStrInt(argument))
1432 ForkedcallsController::get().kill(strToInt(argument));
1435 case LFUN_TOOLTIPS_TOGGLE:
1436 owner->getDialogs().toggleTooltips();
1439 case LFUN_EXTERNAL_EDIT:
1440 InsetExternal().dispatch(FuncRequest(view(), action, argument));
1444 DispatchResult result =
1445 view()->cursor().dispatch(FuncRequest(func, view()));
1446 if (result.dispatched())
1447 lyxerr << "dispatched by Cursor::dispatch()" << endl;
1449 lyxerr << "### NOT DISPATCHED BY Cursor::dispatch() ###" << endl;
1455 view()->owner()->updateLayoutChoice();
1457 if (view()->available()) {
1458 view()->fitCursor();
1460 view()->cursor().updatePos();
1461 // if we executed a mutating lfun, mark the buffer as dirty
1462 if (!getStatus(func).disabled()
1463 && !lyxaction.funcHasFlag(func.action, LyXAction::NoBuffer)
1464 && !lyxaction.funcHasFlag(func.action, LyXAction::ReadOnly))
1465 view()->buffer()->markDirty();
1468 sendDispatchMessage(getMessage(), func, verbose);
1472 void LyXFunc::sendDispatchMessage(string const & msg,
1473 FuncRequest const & func, bool verbose)
1475 owner->updateMenubar();
1476 owner->updateToolbar();
1478 if (func.action == LFUN_SELFINSERT || !verbose) {
1479 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1481 owner->message(msg);
1485 string dispatch_msg = msg;
1486 if (!dispatch_msg.empty())
1487 dispatch_msg += ' ';
1489 string comname = lyxaction.getActionName(func.action);
1491 bool argsadded = false;
1493 if (!func.argument.empty()) {
1494 if (func.action != LFUN_UNKNOWN_ACTION) {
1495 comname += ' ' + func.argument;
1500 string const shortcuts = toplevel_keymap->findbinding(func);
1502 if (!shortcuts.empty()) {
1503 comname += ": " + shortcuts;
1504 } else if (!argsadded && !func.argument.empty()) {
1505 comname += ' ' + func.argument;
1508 if (!comname.empty()) {
1509 comname = rtrim(comname);
1510 dispatch_msg += '(' + comname + ')';
1513 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1514 if (!dispatch_msg.empty())
1515 owner->message(dispatch_msg);
1519 void LyXFunc::setupLocalKeymap()
1521 keyseq.stdmap = toplevel_keymap.get();
1522 keyseq.curmap = toplevel_keymap.get();
1523 cancel_meta_seq.stdmap = toplevel_keymap.get();
1524 cancel_meta_seq.curmap = toplevel_keymap.get();
1528 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1530 string initpath = lyxrc.document_path;
1531 string filename(name);
1533 if (view()->available()) {
1534 string const trypath = owner->buffer()->filePath();
1535 // If directory is writeable, use this as default.
1536 if (IsDirWriteable(trypath))
1540 static int newfile_number;
1542 if (filename.empty()) {
1543 filename = AddName(lyxrc.document_path,
1544 "newfile" + tostr(++newfile_number) + ".lyx");
1545 FileInfo fi(filename);
1546 while (bufferlist.exists(filename) || fi.readable()) {
1548 filename = AddName(lyxrc.document_path,
1549 "newfile" + tostr(newfile_number) +
1551 fi.newFile(filename);
1555 // The template stuff
1558 FileDialog fileDlg(_("Select template file"),
1559 LFUN_SELECT_FILE_SYNC,
1560 make_pair(string(_("Documents|#o#O")),
1561 string(lyxrc.document_path)),
1562 make_pair(string(_("Templates|#T#t")),
1563 string(lyxrc.template_path)));
1565 FileDialog::Result result =
1566 fileDlg.open(lyxrc.template_path,
1567 _("*.lyx| LyX Documents (*.lyx)"));
1569 if (result.first == FileDialog::Later)
1571 if (result.second.empty())
1573 templname = result.second;
1576 view()->newFile(filename, templname, !name.empty());
1580 void LyXFunc::open(string const & fname)
1582 string initpath = lyxrc.document_path;
1584 if (view()->available()) {
1585 string const trypath = owner->buffer()->filePath();
1586 // If directory is writeable, use this as default.
1587 if (IsDirWriteable(trypath))
1593 if (fname.empty()) {
1594 FileDialog fileDlg(_("Select document to open"),
1596 make_pair(string(_("Documents|#o#O")),
1597 string(lyxrc.document_path)),
1598 make_pair(string(_("Examples|#E#e")),
1599 string(AddPath(system_lyxdir(), "examples"))));
1601 FileDialog::Result result =
1602 fileDlg.open(initpath,
1603 _("*.lyx| LyX Documents (*.lyx)"));
1605 if (result.first == FileDialog::Later)
1608 filename = result.second;
1610 // check selected filename
1611 if (filename.empty()) {
1612 owner->message(_("Canceled."));
1618 // get absolute path of file and add ".lyx" to the filename if
1620 string const fullpath = FileSearch(string(), filename, "lyx");
1621 if (!fullpath.empty()) {
1622 filename = fullpath;
1625 string const disp_fn(MakeDisplayPath(filename));
1627 // if the file doesn't exist, let the user create one
1628 FileInfo const f(filename, true);
1630 // the user specifically chose this name. Believe them.
1631 view()->newFile(filename, "", true);
1635 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1638 if (view()->loadLyXFile(filename)) {
1639 str2 = bformat(_("Document %1$s opened."), disp_fn);
1641 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1643 owner->message(str2);
1647 void LyXFunc::doImport(string const & argument)
1650 string filename = split(argument, format, ' ');
1652 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1653 << " file: " << filename << endl;
1655 // need user interaction
1656 if (filename.empty()) {
1657 string initpath = lyxrc.document_path;
1659 if (view()->available()) {
1660 string const trypath = owner->buffer()->filePath();
1661 // If directory is writeable, use this as default.
1662 if (IsDirWriteable(trypath))
1666 string const text = bformat(_("Select %1$s file to import"),
1667 formats.prettyName(format));
1669 FileDialog fileDlg(text,
1671 make_pair(string(_("Documents|#o#O")),
1672 string(lyxrc.document_path)),
1673 make_pair(string(_("Examples|#E#e")),
1674 string(AddPath(system_lyxdir(), "examples"))));
1676 string const extension = "*." + formats.extension(format)
1677 + "| " + formats.prettyName(format)
1678 + " (*." + formats.extension(format) + ')';
1680 FileDialog::Result result = fileDlg.open(initpath,
1683 if (result.first == FileDialog::Later)
1686 filename = result.second;
1688 // check selected filename
1689 if (filename.empty())
1690 owner->message(_("Canceled."));
1693 if (filename.empty())
1696 // get absolute path of file
1697 filename = MakeAbsPath(filename);
1699 string const lyxfile = ChangeExtension(filename, ".lyx");
1701 // Check if the document already is open
1702 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1703 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1704 owner->message(_("Canceled."));
1709 // if the file exists already, and we didn't do
1710 // -i lyx thefile.lyx, warn
1711 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1712 string const file = MakeDisplayPath(lyxfile, 30);
1714 string text = bformat(_("The document %1$s already exists.\n\n"
1715 "Do you want to over-write that document?"), file);
1716 int const ret = Alert::prompt(_("Over-write document?"),
1717 text, 0, 1, _("&Over-write"), _("&Cancel"));
1720 owner->message(_("Canceled."));
1725 Importer::Import(owner, filename, format);
1729 void LyXFunc::closeBuffer()
1731 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1732 if (bufferlist.empty()) {
1733 // need this otherwise SEGV may occur while
1734 // trying to set variables that don't exist
1735 // since there's no current buffer
1736 owner->getDialogs().hideBufferDependent();
1738 view()->buffer(bufferlist.first());
1744 // Each "owner" should have it's own message method. lyxview and
1745 // the minibuffer would use the minibuffer, but lyxserver would
1746 // send an ERROR signal to its client. Alejandro 970603
1747 // This func is bit problematic when it comes to NLS, to make the
1748 // lyx servers client be language indepenent we must not translate
1749 // strings sent to this func.
1750 void LyXFunc::setErrorMessage(string const & m) const
1752 dispatch_buffer = m;
1757 void LyXFunc::setMessage(string const & m) const
1759 dispatch_buffer = m;
1763 void LyXFunc::setStatusMessage(string const & m) const
1769 string const LyXFunc::view_status_message()
1771 // When meta-fake key is pressed, show the key sequence so far + "M-".
1773 return keyseq.print() + "M-";
1776 // Else, when a non-complete key sequence is pressed,
1777 // show the available options.
1778 if (keyseq.length() > 0 && !keyseq.deleted()) {
1779 return keyseq.printOptions();
1782 if (!view()->available())
1783 return _("Welcome to LyX!");
1785 return currentState(view());
1789 BufferView * LyXFunc::view() const
1791 BOOST_ASSERT(owner);
1792 return owner->view().get();
1796 bool LyXFunc::wasMetaKey() const
1798 return (meta_fake_bit != key_modifier::none);