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/globbing.h"
81 #include "support/path.h"
82 #include "support/path_defines.h"
83 #include "support/tostr.h"
84 #include "support/std_sstream.h"
85 #include "support/os.h"
87 using bv_funcs::changeDepth;
88 using bv_funcs::currentState;
89 using bv_funcs::DEC_DEPTH;
90 using bv_funcs::freefont2string;
91 using bv_funcs::INC_DEPTH;
93 using lyx::support::AddName;
94 using lyx::support::AddPath;
95 using lyx::support::bformat;
96 using lyx::support::ChangeExtension;
97 using lyx::support::FileFilterList;
98 using lyx::support::FileInfo;
99 using lyx::support::FileSearch;
100 using lyx::support::ForkedcallsController;
101 using lyx::support::i18nLibFileSearch;
102 using lyx::support::IsDirWriteable;
103 using lyx::support::IsFileReadable;
104 using lyx::support::isStrInt;
105 using lyx::support::MakeAbsPath;
106 using lyx::support::MakeDisplayPath;
107 using lyx::support::Path;
108 using lyx::support::rtrim;
109 using lyx::support::split;
110 using lyx::support::strToInt;
111 using lyx::support::strToUnsignedInt;
112 using lyx::support::system_lyxdir;
113 using lyx::support::token;
114 using lyx::support::trim;
115 using lyx::support::user_lyxdir;
116 using lyx::support::prefixIs;
117 using lyx::support::os::getTmpDir;
120 using std::make_pair;
123 using std::istringstream;
126 extern BufferList bufferlist;
127 extern LyXServer * lyxserver;
128 extern bool selection_possible;
130 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
133 extern tex_accent_struct get_accent(kb_action action);
136 LyXFunc::LyXFunc(LyXView * lv)
139 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
140 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
141 meta_fake_bit(key_modifier::none)
146 void LyXFunc::handleKeyFunc(kb_action action)
148 char c = encoded_last_key;
150 if (keyseq.length()) {
154 owner->getIntl().getTransManager()
155 .deadkey(c, get_accent(action).accent, view()->getLyXText());
156 // Need to clear, in case the minibuffer calls these
159 // copied verbatim from do_accent_char
160 view()->cursor().resetAnchor();
165 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
166 key_modifier::state state)
170 if (lyxerr.debugging(Debug::KEY)) {
171 lyxerr << "KeySym is "
172 << keysym->getSymbolName()
176 // Do nothing if we have nothing (JMarc)
177 if (!keysym->isOK()) {
178 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
183 if (keysym->isModifier()) {
184 lyxerr[Debug::KEY] << "isModifier true" << endl;
188 Encoding const * encoding = view()->getEncoding();
190 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
192 // Do a one-deep top-level lookup for
193 // cancel and meta-fake keys. RVDK_PATCH_5
194 cancel_meta_seq.reset();
196 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
197 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
199 // When not cancel or meta-fake, do the normal lookup.
200 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
201 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
202 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
203 // remove Caps Lock and Mod2 as a modifiers
204 func = keyseq.addkey(keysym, (state | meta_fake_bit));
205 lyxerr[Debug::KEY] << "action now set to ["
206 << func.action << ']' << endl;
209 // Dont remove this unless you know what you are doing.
210 meta_fake_bit = key_modifier::none;
212 // can this happen now ?
213 if (func.action == LFUN_NOACTION) {
214 func = FuncRequest(LFUN_PREFIX);
217 if (lyxerr.debugging(Debug::KEY)) {
218 lyxerr << "Key [action="
219 << func.action << "]["
220 << keyseq.print() << ']'
224 // already here we know if it any point in going further
225 // why not return already here if action == -1 and
226 // num_bytes == 0? (Lgb)
228 if (keyseq.length() > 1) {
229 owner->message(keyseq.print());
233 // Maybe user can only reach the key via holding down shift.
234 // Let's see. But only if shift is the only modifier
235 if (func.action == LFUN_UNKNOWN_ACTION &&
236 state == key_modifier::shift) {
237 lyxerr[Debug::KEY] << "Trying without shift" << endl;
238 func = keyseq.addkey(keysym, key_modifier::none);
239 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
242 if (func.action == LFUN_UNKNOWN_ACTION) {
243 // Hmm, we didn't match any of the keysequences. See
244 // if it's normal insertable text not already covered
246 if (keysym->isText() && keyseq.length() == 1) {
247 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
248 func = FuncRequest(LFUN_SELFINSERT);
250 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
251 owner->message(_("Unknown function."));
256 if (func.action == LFUN_SELFINSERT) {
257 if (encoded_last_key != 0) {
259 arg += encoded_last_key;
260 dispatch(FuncRequest(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().inset()
324 ? view()->cursor().inset()->asUpdatableInset() : 0;
325 InsetTabular * tab = view()->cursor().innerInsetTabular();
327 // I would really like to avoid having this switch and rather try to
328 // encode this in the function itself.
329 bool disable = false;
332 disable = ev.argument != "custom"
333 && !Exporter::IsExportable(*buf, ev.argument);
336 disable = buf->undostack().empty();
339 disable = buf->redostack().empty();
343 if (tab && tab->hasSelection())
346 disable = !inMathed() && !view()->cursor().selection();
350 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
354 disable = !Exporter::IsExportable(*buf, "program");
357 case LFUN_LAYOUT_TABULAR:
358 disable = !view()->cursor().innerInsetTabular();
362 disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH);
365 case LFUN_DEPTH_PLUS:
366 disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH);
370 case LFUN_LAYOUT_PARAGRAPH: {
371 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
372 disable = inset && inset->forceDefaultParagraphs(inset);
376 case LFUN_INSET_OPTARG:
377 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
380 case LFUN_TABULAR_FEATURE:
383 // FIXME: check temporarily disabled
385 char align = mathcursor::valign();
390 if (ev.argument.empty()) {
394 if (!contains("tcb", ev.argument[0])) {
398 flag.setOnOff(ev.argument[0] == align);
402 char align = mathcursor::halign();
407 if (ev.argument.empty()) {
411 if (!contains("lcr", ev.argument[0])) {
415 flag.setOnOff(ev.argument[0] == align);
417 disable = !mathcursor::halign();
423 //ret.disabled(true);
424 InsetTabular * tab = view()->cursor().innerInsetTabular();
426 ret = tab->getStatus(ev.argument);
433 static InsetTabular inset(*buf, 1, 1);
435 FuncStatus ret = inset.getStatus(ev.argument);
436 if (ret.onoff(true) || ret.onoff(false))
437 flag.setOnOff(false);
442 case LFUN_VC_REGISTER:
443 disable = buf->lyxvc().inUse();
445 case LFUN_VC_CHECKIN:
446 disable = !buf->lyxvc().inUse() || buf->isReadonly();
448 case LFUN_VC_CHECKOUT:
449 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
453 disable = !buf->lyxvc().inUse();
455 case LFUN_MENURELOAD:
456 disable = buf->isUnnamed() || buf->isClean();
458 case LFUN_BOOKMARK_GOTO:
460 isSavedPosition(strToUnsignedInt(ev.argument));
463 case LFUN_MERGE_CHANGES:
464 case LFUN_ACCEPT_CHANGE:
465 case LFUN_REJECT_CHANGE:
466 case LFUN_ACCEPT_ALL_CHANGES:
467 case LFUN_REJECT_ALL_CHANGES:
468 disable = !buf->params().tracking_changes;
471 case LFUN_INSET_SETTINGS: {
473 UpdatableInset * inset = view()->cursor().inset()
474 ? view()->cursor().inset()->asUpdatableInset() : 0;
479 // jump back to owner if an InsetText, so
480 // we get back to the InsetTabular or whatever
481 if (inset->lyxCode() == InsetOld::TEXT_CODE)
482 inset = inset->owner();
484 InsetOld::Code code = inset->lyxCode();
486 case InsetOld::TABULAR_CODE:
487 disable = ev.argument != "tabular";
489 case InsetOld::ERT_CODE:
490 disable = ev.argument != "ert";
492 case InsetOld::FLOAT_CODE:
493 disable = ev.argument != "float";
495 case InsetOld::WRAP_CODE:
496 disable = ev.argument != "wrap";
498 case InsetOld::NOTE_CODE:
499 disable = ev.argument != "note";
501 case InsetOld::BRANCH_CODE:
502 disable = ev.argument != "branch";
504 case InsetOld::BOX_CODE:
505 disable = ev.argument != "box";
513 case LFUN_MATH_MUTATE:
515 //flag.setOnOff(mathcursor::formula()->hullType() == ev.argument);
516 flag.setOnOff(false);
521 // we just need to be in math mode to enable that
523 case LFUN_MATH_SPACE:
524 case LFUN_MATH_LIMITS:
525 case LFUN_MATH_NONUMBER:
526 case LFUN_MATH_NUMBER:
527 case LFUN_MATH_EXTERN:
528 disable = !inMathed();
531 case LFUN_DIALOG_SHOW: {
532 string const name = ev.getArg(0);
534 disable = !(name == "aboutlyx" ||
537 name == "preferences" ||
542 if (name == "print") {
543 disable = !Exporter::IsExportable(*buf, "dvi") ||
544 lyxrc.print_command == "none";
545 } else if (name == "character") {
546 InsetBase * inset = view()->cursor().inset();
547 disable = inset && inset->lyxCode() == InsetOld::ERT_CODE;
548 } else if (name == "vclog") {
549 disable = !buf->lyxvc().inUse();
550 } else if (name == "latexlog") {
551 disable = !IsFileReadable(buf->getLogName().second);
560 // the functions which insert insets
561 InsetOld::Code code = InsetOld::NO_CODE;
563 case LFUN_DIALOG_SHOW_NEW_INSET:
564 if (ev.argument == "bibitem")
565 code = InsetOld::BIBITEM_CODE;
566 else if (ev.argument == "bibtex")
567 code = InsetOld::BIBTEX_CODE;
568 else if (ev.argument == "box")
569 code = InsetOld::BOX_CODE;
570 else if (ev.argument == "branch")
571 code = InsetOld::BRANCH_CODE;
572 else if (ev.argument == "citation")
573 code = InsetOld::CITE_CODE;
574 else if (ev.argument == "ert")
575 code = InsetOld::ERT_CODE;
576 else if (ev.argument == "external")
577 code = InsetOld::EXTERNAL_CODE;
578 else if (ev.argument == "float")
579 code = InsetOld::FLOAT_CODE;
580 else if (ev.argument == "graphics")
581 code = InsetOld::GRAPHICS_CODE;
582 else if (ev.argument == "include")
583 code = InsetOld::INCLUDE_CODE;
584 else if (ev.argument == "index")
585 code = InsetOld::INDEX_CODE;
586 else if (ev.argument == "label")
587 code = InsetOld::LABEL_CODE;
588 else if (ev.argument == "note")
589 code = InsetOld::NOTE_CODE;
590 else if (ev.argument == "ref")
591 code = InsetOld::REF_CODE;
592 else if (ev.argument == "toc")
593 code = InsetOld::TOC_CODE;
594 else if (ev.argument == "url")
595 code = InsetOld::URL_CODE;
596 else if (ev.argument == "vspace")
597 code = InsetOld::VSPACE_CODE;
598 else if (ev.argument == "wrap")
599 code = InsetOld::WRAP_CODE;
603 code = InsetOld::ERT_CODE;
605 case LFUN_INSET_FOOTNOTE:
606 code = InsetOld::FOOT_CODE;
608 case LFUN_TABULAR_INSERT:
609 code = InsetOld::TABULAR_CODE;
611 case LFUN_INSET_MARGINAL:
612 code = InsetOld::MARGIN_CODE;
614 case LFUN_INSET_FLOAT:
615 case LFUN_INSET_WIDE_FLOAT:
616 code = InsetOld::FLOAT_CODE;
618 case LFUN_INSET_WRAP:
619 code = InsetOld::WRAP_CODE;
621 case LFUN_FLOAT_LIST:
622 code = InsetOld::FLOAT_LIST_CODE;
625 case LFUN_INSET_LIST:
626 code = InsetOld::LIST_CODE;
628 case LFUN_INSET_THEOREM:
629 code = InsetOld::THEOREM_CODE;
632 case LFUN_INSET_CAPTION:
633 code = InsetOld::CAPTION_CODE;
635 case LFUN_INSERT_NOTE:
636 code = InsetOld::NOTE_CODE;
638 case LFUN_INSERT_CHARSTYLE:
639 code = InsetOld::CHARSTYLE_CODE;
640 if (buf->params().getLyXTextClass().charstyles().empty())
643 case LFUN_INSERT_BOX:
644 code = InsetOld::BOX_CODE;
646 case LFUN_INSERT_BRANCH:
647 code = InsetOld::BRANCH_CODE;
648 if (buf->params().branchlist().empty())
651 case LFUN_INSERT_LABEL:
652 code = InsetOld::LABEL_CODE;
654 case LFUN_INSET_OPTARG:
655 code = InsetOld::OPTARG_CODE;
657 case LFUN_ENVIRONMENT_INSERT:
658 code = InsetOld::BOX_CODE;
660 case LFUN_INDEX_INSERT:
661 code = InsetOld::INDEX_CODE;
663 case LFUN_INDEX_PRINT:
664 code = InsetOld::INDEX_PRINT_CODE;
666 case LFUN_TOC_INSERT:
667 code = InsetOld::TOC_CODE;
671 code = InsetOld::URL_CODE;
674 // always allow this, since we will inset a raw quote
675 // if an inset is not allowed.
677 case LFUN_HYPHENATION:
678 case LFUN_LIGATURE_BREAK:
680 case LFUN_MENU_SEPARATOR:
682 case LFUN_END_OF_SENTENCE:
683 code = InsetOld::SPECIALCHAR_CODE;
685 case LFUN_SPACE_INSERT:
686 // slight hack: we know this is allowed in math mode
688 code = InsetOld::SPACE_CODE;
690 case LFUN_INSET_DIALOG_SHOW: {
691 InsetOld * inset = view()->getLyXText()->getInset();
694 code = inset->lyxCode();
695 if (!(code == InsetOld::INCLUDE_CODE
696 || code == InsetOld::BIBTEX_CODE
697 || code == InsetOld::FLOAT_LIST_CODE
698 || code == InsetOld::TOC_CODE))
706 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
712 // A few general toggles
714 case LFUN_TOOLTIPS_TOGGLE:
715 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
718 case LFUN_READ_ONLY_TOGGLE:
719 flag.setOnOff(buf->isReadonly());
722 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
724 case LFUN_SWITCHBUFFER:
725 // toggle on the current buffer, but do not toggle off
726 // the other ones (is that a good idea?)
727 if (ev.argument == buf->fileName())
730 case LFUN_TRACK_CHANGES:
731 flag.setOnOff(buf->params().tracking_changes);
738 // the font related toggles
740 LyXFont const & font = view()->getLyXText()->real_current_font;
743 flag.setOnOff(font.emph() == LyXFont::ON);
746 flag.setOnOff(font.noun() == LyXFont::ON);
749 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
752 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
755 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
758 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
764 string tc = mathcursor::getLastCode();
767 flag.setOnOff(tc == "mathbf");
770 flag.setOnOff(tc == "mathsf");
773 flag.setOnOff(tc == "mathcal");
776 flag.setOnOff(tc == "mathrm");
779 flag.setOnOff(tc == "mathtt");
782 flag.setOnOff(tc == "mathbb");
785 flag.setOnOff(tc == "mathnormal");
793 // this one is difficult to get right. As a half-baked
794 // solution, we consider only the first action of the sequence
795 if (ev.action == LFUN_SEQUENCE) {
796 // argument contains ';'-terminated commands
797 #warning LyXAction arguments not handled here.
798 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
807 bool ensureBufferClean(BufferView * bv)
809 Buffer & buf = *bv->buffer();
813 string const file = MakeDisplayPath(buf.fileName(), 30);
814 string text = bformat(_("The document %1$s has unsaved "
815 "changes.\n\nDo you want to save "
816 "the document?"), file);
817 int const ret = Alert::prompt(_("Save changed document?"),
818 text, 0, 1, _("&Save"),
822 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
824 return buf.isClean();
830 void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
832 string argument = func.argument;
833 kb_action action = func.action;
835 //lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << func << endl;
836 lyxerr << "LyXFunc::dispatch: cmd: " << func << endl;
838 // we have not done anything wrong yet.
840 dispatch_buffer.erase();
842 #ifdef NEW_DISPATCHER
843 // We try do call the most specific dispatcher first:
844 // 1. the lockinginset's dispatch
845 // 2. the bufferview's dispatch
846 // 3. the lyxview's dispatch
849 selection_possible = false;
851 // We cannot use this function here
852 if (getStatus(func).disabled()) {
853 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
854 << lyxaction.getActionName(action)
855 << " [" << action << "] is disabled at this location"
857 setErrorMessage(getStatusMessage());
861 if (view()->available())
862 view()->hideCursor();
867 if (!view()->available())
869 view()->cursor().pop();
870 // Tell the paragraph dialog that we changed paragraph
871 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
875 case LFUN_WORDFINDFORWARD:
876 case LFUN_WORDFINDBACKWARD: {
877 static string last_search;
878 string searched_string;
880 if (!argument.empty()) {
881 last_search = argument;
882 searched_string = argument;
884 searched_string = last_search;
886 if (searched_string.empty())
889 bool const fw = action == LFUN_WORDFINDFORWARD;
891 lyx::find::find2string(searched_string, true, false, fw);
892 view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
897 owner->message(keyseq.printOptions());
900 case LFUN_EXEC_COMMAND:
901 owner->focus_command_buffer();
906 meta_fake_bit = key_modifier::none;
907 if (view()->available())
908 // cancel any selection
909 dispatch(FuncRequest(LFUN_MARK_OFF));
910 setMessage(N_("Cancel"));
914 meta_fake_bit = key_modifier::alt;
915 setMessage(keyseq.print());
918 case LFUN_READ_ONLY_TOGGLE:
919 if (owner->buffer()->lyxvc().inUse())
920 owner->buffer()->lyxvc().toggleReadOnly();
922 owner->buffer()->setReadonly(
923 !owner->buffer()->isReadonly());
926 case LFUN_CENTER: // this is center and redraw.
930 // --- Menus -----------------------------------------------
932 menuNew(argument, false);
935 case LFUN_MENUNEWTMPLT:
936 menuNew(argument, true);
939 case LFUN_CLOSEBUFFER:
944 if (!owner->buffer()->isUnnamed()) {
945 string const str = bformat(_("Saving document %1$s..."),
946 MakeDisplayPath(owner->buffer()->fileName()));
948 MenuWrite(owner->buffer());
949 owner->message(str + _(" done."));
951 WriteAs(owner->buffer());
955 WriteAs(owner->buffer(), argument);
958 case LFUN_MENURELOAD: {
959 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
960 string text = bformat(_("Any changes will be lost. Are you sure "
961 "you want to revert to the saved version of the document %1$s?"), file);
962 int const ret = Alert::prompt(_("Revert to saved document?"),
963 text, 0, 1, _("&Revert"), _("&Cancel"));
971 Exporter::Export(owner->buffer(), argument, true);
972 view()->showErrorList(BufferFormat(*owner->buffer()));
976 Exporter::Preview(owner->buffer(), argument);
977 view()->showErrorList(BufferFormat(*owner->buffer()));
981 Exporter::Export(owner->buffer(), "program", true);
982 view()->showErrorList(_("Build"));
986 owner->buffer()->runChktex();
987 view()->showErrorList(_("ChkTeX"));
991 if (argument == "custom")
992 owner->getDialogs().showSendto();
994 Exporter::Export(owner->buffer(), argument, false);
995 view()->showErrorList(BufferFormat(*owner->buffer()));
1007 case LFUN_TOCVIEW: {
1008 InsetCommandParams p("tableofcontents");
1009 string const data = InsetCommandMailer::params2string("toc", p);
1010 owner->getDialogs().show("toc", data, 0);
1026 case LFUN_RECONFIGURE:
1027 Reconfigure(view());
1030 case LFUN_HELP_OPEN: {
1031 string const arg = argument;
1033 setErrorMessage(N_("Missing argument"));
1036 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1037 if (fname.empty()) {
1038 lyxerr << "LyX: unable to find documentation file `"
1039 << arg << "'. Bad installation?" << endl;
1042 owner->message(bformat(_("Opening help file %1$s..."),
1043 MakeDisplayPath(fname)));
1044 view()->loadLyXFile(fname, false);
1048 // --- version control -------------------------------
1049 case LFUN_VC_REGISTER:
1050 if (!ensureBufferClean(view()))
1052 if (!owner->buffer()->lyxvc().inUse()) {
1053 owner->buffer()->lyxvc().registrer();
1058 case LFUN_VC_CHECKIN:
1059 if (!ensureBufferClean(view()))
1061 if (owner->buffer()->lyxvc().inUse()
1062 && !owner->buffer()->isReadonly()) {
1063 owner->buffer()->lyxvc().checkIn();
1068 case LFUN_VC_CHECKOUT:
1069 if (!ensureBufferClean(view()))
1071 if (owner->buffer()->lyxvc().inUse()
1072 && owner->buffer()->isReadonly()) {
1073 owner->buffer()->lyxvc().checkOut();
1078 case LFUN_VC_REVERT:
1079 owner->buffer()->lyxvc().revert();
1084 owner->buffer()->lyxvc().undoLast();
1088 // --- buffers ----------------------------------------
1089 case LFUN_SWITCHBUFFER:
1090 view()->buffer(bufferlist.getBuffer(argument));
1094 NewFile(view(), argument);
1097 case LFUN_FILE_OPEN:
1101 case LFUN_LAYOUT_TABULAR:
1102 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1103 tab->openLayoutDialog(view());
1106 case LFUN_DROP_LAYOUTS_CHOICE:
1107 owner->getToolbar().openLayoutList();
1110 case LFUN_MENU_OPEN_BY_NAME:
1111 owner->getMenubar().openByName(argument);
1114 // --- lyxserver commands ----------------------------
1116 setMessage(owner->buffer()->fileName());
1117 lyxerr[Debug::INFO] << "FNAME["
1118 << owner->buffer()->fileName()
1123 dispatch_buffer = keyseq.print();
1124 lyxserver->notifyClient(dispatch_buffer);
1127 case LFUN_GOTOFILEROW: {
1130 istringstream is(argument);
1131 is >> file_name >> row;
1132 if (prefixIs(file_name, getTmpDir())) {
1133 // Needed by inverse dvi search. If it is a file
1134 // in tmpdir, call the apropriated function
1135 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1137 // Must replace extension of the file to be .lyx
1138 // and get full path
1139 string const s = ChangeExtension(file_name, ".lyx");
1140 // Either change buffer or load the file
1141 if (bufferlist.exists(s)) {
1142 view()->buffer(bufferlist.getBuffer(s));
1144 view()->loadLyXFile(s);
1148 view()->setCursorFromRow(row);
1151 // see BufferView_pimpl::center()
1152 view()->updateScrollbar();
1156 case LFUN_GOTO_PARAGRAPH: {
1157 istringstream is(argument);
1160 ParIterator par = owner->buffer()->getParFromID(id);
1161 if (par == owner->buffer()->par_iterator_end()) {
1162 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1163 << id << ']' << endl;
1166 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1167 << " found." << endl;
1170 par.lockPath(view());
1171 LyXText * lt = par.text(*view()->buffer());
1174 lt->setCursor(par.pit(), 0);
1175 view()->switchKeyMap();
1176 owner->view_state_changed();
1179 // see BufferView_pimpl::center()
1180 view()->updateScrollbar();
1184 // --- Mathed stuff. If we are here, there is no locked inset yet.
1185 case LFUN_MATH_EXTERN:
1186 case LFUN_MATH_NUMBER:
1187 case LFUN_MATH_NONUMBER:
1188 case LFUN_MATH_LIMITS:
1189 setErrorMessage(N_("This is only allowed in math mode!"));
1192 case LFUN_DIALOG_SHOW: {
1193 string const name = func.getArg(0);
1194 string data = trim(func.argument.substr(name.size()));
1196 if (name == "character") {
1197 data = freefont2string();
1199 owner->getDialogs().show("character", data);
1201 else if (name == "document")
1202 owner->getDialogs().showDocument();
1203 else if (name == "findreplace")
1204 owner->getDialogs().showSearch();
1205 else if (name == "forks")
1206 owner->getDialogs().showForks();
1207 else if (name == "preamble")
1208 owner->getDialogs().showPreamble();
1209 else if (name == "preferences")
1210 owner->getDialogs().showPreferences();
1211 else if (name == "print")
1212 owner->getDialogs().showPrint();
1213 else if (name == "spellchecker")
1214 owner->getDialogs().showSpellchecker();
1216 else if (name == "latexlog") {
1217 pair<Buffer::LogType, string> const logfile =
1218 owner->buffer()->getLogName();
1219 switch (logfile.first) {
1220 case Buffer::latexlog:
1223 case Buffer::buildlog:
1227 data += logfile.second;
1228 owner->getDialogs().show("log", data);
1230 else if (name == "vclog") {
1231 string const data = "vc " +
1232 owner->buffer()->lyxvc().getLogFile();
1233 owner->getDialogs().show("log", data);
1236 owner->getDialogs().show(name, data);
1240 case LFUN_DIALOG_SHOW_NEW_INSET: {
1241 string const name = func.getArg(0);
1242 string data = trim(func.argument.substr(name.size()));
1243 if (name == "bibitem" ||
1245 name == "include" ||
1251 InsetCommandParams p(name);
1252 data = InsetCommandMailer::params2string(name, p);
1253 } else if (name == "box") {
1254 // \c data == "Boxed" || "Frameless" etc
1255 InsetBoxParams p(data);
1256 data = InsetBoxMailer::params2string(p);
1257 } else if (name == "branch") {
1258 InsetBranchParams p;
1259 data = InsetBranchMailer::params2string(p);
1260 } else if (name == "citation") {
1261 InsetCommandParams p("cite");
1262 data = InsetCommandMailer::params2string(name, p);
1263 } else if (name == "ert") {
1264 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1265 } else if (name == "external") {
1266 InsetExternalParams p;
1267 Buffer const & buffer = *owner->buffer();
1268 data = InsetExternalMailer::params2string(p, buffer);
1269 } else if (name == "float") {
1271 data = InsetFloatMailer::params2string(p);
1272 } else if (name == "graphics") {
1273 InsetGraphicsParams p;
1274 Buffer const & buffer = *owner->buffer();
1275 data = InsetGraphicsMailer::params2string(p, buffer);
1276 } else if (name == "note") {
1278 data = InsetNoteMailer::params2string(p);
1279 } else if (name == "vspace") {
1281 data = InsetVSpaceMailer::params2string(space);
1282 } else if (name == "wrap") {
1284 data = InsetWrapMailer::params2string(p);
1286 owner->getDialogs().show(name, data, 0);
1290 case LFUN_DIALOG_SHOW_NEXT_INSET:
1293 case LFUN_INSET_DIALOG_SHOW: {
1294 InsetOld * inset = view()->getLyXText()->getInset();
1296 inset->dispatch(view()->cursor(), FuncRequest(LFUN_INSET_DIALOG_SHOW));
1300 case LFUN_DIALOG_UPDATE: {
1301 string const & name = argument;
1302 // Can only update a dialog connected to an existing inset
1303 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1305 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, func.argument);
1306 inset->dispatch(view()->cursor(), fr);
1307 } else if (name == "paragraph") {
1308 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1313 case LFUN_DIALOG_HIDE:
1314 Dialogs::hide(argument, 0);
1317 case LFUN_DIALOG_DISCONNECT_INSET:
1318 owner->getDialogs().disconnect(argument);
1321 case LFUN_CHILDOPEN: {
1322 string const filename =
1323 MakeAbsPath(argument, owner->buffer()->filePath());
1324 setMessage(N_("Opening child document ") +
1325 MakeDisplayPath(filename) + "...");
1326 view()->savePosition(0);
1327 string const parentfilename = owner->buffer()->fileName();
1328 if (bufferlist.exists(filename))
1329 view()->buffer(bufferlist.getBuffer(filename));
1331 view()->loadLyXFile(filename);
1332 // Set the parent name of the child document.
1333 // This makes insertion of citations and references in the child work,
1334 // when the target is in the parent or another child document.
1335 owner->buffer()->setParentName(parentfilename);
1339 case LFUN_TOGGLECURSORFOLLOW:
1340 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1344 owner->getIntl().KeyMapOn(false);
1347 case LFUN_KMAP_PRIM:
1348 owner->getIntl().KeyMapPrim();
1352 owner->getIntl().KeyMapSec();
1355 case LFUN_KMAP_TOGGLE:
1356 owner->getIntl().ToggleKeyMap();
1362 argument = split(argument, countstr, ' ');
1363 istringstream is(countstr);
1366 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1367 for (int i = 0; i < count; ++i)
1368 dispatch(lyxaction.lookupFunc(argument));
1373 // argument contains ';'-terminated commands
1374 while (!argument.empty()) {
1376 argument = split(argument, first, ';');
1377 dispatch(lyxaction.lookupFunc(first));
1381 case LFUN_SAVEPREFERENCES: {
1382 Path p(user_lyxdir());
1383 lyxrc.write("preferences");
1387 case LFUN_SCREEN_FONT_UPDATE:
1388 // handle the screen font changes.
1389 lyxrc.set_font_norm_type();
1390 lyx_gui::update_fonts();
1391 // All visible buffers will need resize
1395 case LFUN_SET_COLOR: {
1397 string const x11_name = split(argument, lyx_name, ' ');
1398 if (lyx_name.empty() || x11_name.empty()) {
1399 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1404 bool const graphicsbg_changed =
1405 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1406 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1408 LColor::color col = lcolor.getFromLyXName(lyx_name);
1409 if (!lcolor.setColor(col, x11_name)) {
1411 bformat(_("Set-color \"%1$s\" failed "
1412 "- color is undefined or "
1413 "may not be redefined"), lyx_name));
1417 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1419 if (graphicsbg_changed) {
1420 #ifdef WITH_WARNINGS
1421 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1424 lyx::graphics::GCache::get().changeDisplay(true);
1431 owner->message(argument);
1434 case LFUN_FORKS_KILL:
1435 if (isStrInt(argument))
1436 ForkedcallsController::get().kill(strToInt(argument));
1439 case LFUN_TOOLTIPS_TOGGLE:
1440 owner->getDialogs().toggleTooltips();
1443 case LFUN_EXTERNAL_EDIT:
1444 InsetExternal().dispatch(view()->cursor(), FuncRequest(action, argument));
1448 view()->cursor().dispatch(FuncRequest(func));
1453 view()->owner()->updateLayoutChoice();
1455 if (view()->available()) {
1456 view()->fitCursor();
1458 view()->cursor().updatePos();
1459 // if we executed a mutating lfun, mark the buffer as dirty
1460 if (!getStatus(func).disabled()
1461 && !lyxaction.funcHasFlag(func.action, LyXAction::NoBuffer)
1462 && !lyxaction.funcHasFlag(func.action, LyXAction::ReadOnly))
1463 view()->buffer()->markDirty();
1466 sendDispatchMessage(getMessage(), func, verbose);
1470 void LyXFunc::sendDispatchMessage(string const & msg,
1471 FuncRequest const & func, bool verbose)
1473 owner->updateMenubar();
1474 owner->updateToolbar();
1476 if (func.action == LFUN_SELFINSERT || !verbose) {
1477 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1479 owner->message(msg);
1483 string dispatch_msg = msg;
1484 if (!dispatch_msg.empty())
1485 dispatch_msg += ' ';
1487 string comname = lyxaction.getActionName(func.action);
1489 bool argsadded = false;
1491 if (!func.argument.empty()) {
1492 if (func.action != LFUN_UNKNOWN_ACTION) {
1493 comname += ' ' + func.argument;
1498 string const shortcuts = toplevel_keymap->findbinding(func);
1500 if (!shortcuts.empty()) {
1501 comname += ": " + shortcuts;
1502 } else if (!argsadded && !func.argument.empty()) {
1503 comname += ' ' + func.argument;
1506 if (!comname.empty()) {
1507 comname = rtrim(comname);
1508 dispatch_msg += '(' + comname + ')';
1511 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1512 if (!dispatch_msg.empty())
1513 owner->message(dispatch_msg);
1517 void LyXFunc::setupLocalKeymap()
1519 keyseq.stdmap = toplevel_keymap.get();
1520 keyseq.curmap = toplevel_keymap.get();
1521 cancel_meta_seq.stdmap = toplevel_keymap.get();
1522 cancel_meta_seq.curmap = toplevel_keymap.get();
1526 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1528 string initpath = lyxrc.document_path;
1529 string filename(name);
1531 if (view()->available()) {
1532 string const trypath = owner->buffer()->filePath();
1533 // If directory is writeable, use this as default.
1534 if (IsDirWriteable(trypath))
1538 static int newfile_number;
1540 if (filename.empty()) {
1541 filename = AddName(lyxrc.document_path,
1542 "newfile" + tostr(++newfile_number) + ".lyx");
1543 FileInfo fi(filename);
1544 while (bufferlist.exists(filename) || fi.readable()) {
1546 filename = AddName(lyxrc.document_path,
1547 "newfile" + tostr(newfile_number) +
1549 fi.newFile(filename);
1553 // The template stuff
1556 FileDialog fileDlg(_("Select template file"),
1557 LFUN_SELECT_FILE_SYNC,
1558 make_pair(string(_("Documents|#o#O")),
1559 string(lyxrc.document_path)),
1560 make_pair(string(_("Templates|#T#t")),
1561 string(lyxrc.template_path)));
1563 FileDialog::Result result =
1564 fileDlg.open(lyxrc.template_path,
1565 FileFilterList(_("LyX Documents (*.lyx)")),
1568 if (result.first == FileDialog::Later)
1570 if (result.second.empty())
1572 templname = result.second;
1575 view()->newFile(filename, templname, !name.empty());
1579 void LyXFunc::open(string const & fname)
1581 string initpath = lyxrc.document_path;
1583 if (view()->available()) {
1584 string const trypath = owner->buffer()->filePath();
1585 // If directory is writeable, use this as default.
1586 if (IsDirWriteable(trypath))
1592 if (fname.empty()) {
1593 FileDialog fileDlg(_("Select document to open"),
1595 make_pair(string(_("Documents|#o#O")),
1596 string(lyxrc.document_path)),
1597 make_pair(string(_("Examples|#E#e")),
1598 string(AddPath(system_lyxdir(), "examples"))));
1600 FileDialog::Result result =
1601 fileDlg.open(initpath,
1602 FileFilterList(_("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 filter = formats.prettyName(format)
1677 + " (*." + formats.extension(format) + ')';
1679 FileDialog::Result result =
1680 fileDlg.open(initpath,
1681 FileFilterList(filter),
1684 if (result.first == FileDialog::Later)
1687 filename = result.second;
1689 // check selected filename
1690 if (filename.empty())
1691 owner->message(_("Canceled."));
1694 if (filename.empty())
1697 // get absolute path of file
1698 filename = MakeAbsPath(filename);
1700 string const lyxfile = ChangeExtension(filename, ".lyx");
1702 // Check if the document already is open
1703 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1704 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1705 owner->message(_("Canceled."));
1710 // if the file exists already, and we didn't do
1711 // -i lyx thefile.lyx, warn
1712 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1713 string const file = MakeDisplayPath(lyxfile, 30);
1715 string text = bformat(_("The document %1$s already exists.\n\n"
1716 "Do you want to over-write that document?"), file);
1717 int const ret = Alert::prompt(_("Over-write document?"),
1718 text, 0, 1, _("&Over-write"), _("&Cancel"));
1721 owner->message(_("Canceled."));
1726 Importer::Import(owner, filename, format);
1730 void LyXFunc::closeBuffer()
1732 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1733 if (bufferlist.empty()) {
1734 // need this otherwise SEGV may occur while
1735 // trying to set variables that don't exist
1736 // since there's no current buffer
1737 owner->getDialogs().hideBufferDependent();
1739 view()->buffer(bufferlist.first());
1745 // Each "owner" should have it's own message method. lyxview and
1746 // the minibuffer would use the minibuffer, but lyxserver would
1747 // send an ERROR signal to its client. Alejandro 970603
1748 // This func is bit problematic when it comes to NLS, to make the
1749 // lyx servers client be language indepenent we must not translate
1750 // strings sent to this func.
1751 void LyXFunc::setErrorMessage(string const & m) const
1753 dispatch_buffer = m;
1758 void LyXFunc::setMessage(string const & m) const
1760 dispatch_buffer = m;
1764 void LyXFunc::setStatusMessage(string const & m) const
1770 string const LyXFunc::view_status_message()
1772 // When meta-fake key is pressed, show the key sequence so far + "M-".
1774 return keyseq.print() + "M-";
1777 // Else, when a non-complete key sequence is pressed,
1778 // show the available options.
1779 if (keyseq.length() > 0 && !keyseq.deleted()) {
1780 return keyseq.printOptions();
1783 if (!view()->available())
1784 return _("Welcome to LyX!");
1786 return currentState(view());
1790 BufferView * LyXFunc::view() const
1792 BOOST_ASSERT(owner);
1793 return owner->view().get();
1797 bool LyXFunc::wasMetaKey() const
1799 return (meta_fake_bit != key_modifier::none);