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 "frontends/Alert.h"
67 #include "frontends/Dialogs.h"
68 #include "frontends/FileDialog.h"
69 #include "frontends/lyx_gui.h"
70 #include "frontends/LyXKeySym.h"
71 #include "frontends/LyXView.h"
72 #include "frontends/Menubar.h"
73 #include "frontends/Toolbar.h"
75 #include "support/FileInfo.h"
76 #include "support/filetools.h"
77 #include "support/forkedcontr.h"
78 #include "support/globbing.h"
79 #include "support/path.h"
80 #include "support/path_defines.h"
81 #include "support/tostr.h"
82 #include "support/std_sstream.h"
83 #include "support/os.h"
85 using bv_funcs::changeDepth;
86 using bv_funcs::currentState;
87 using bv_funcs::DEC_DEPTH;
88 using bv_funcs::freefont2string;
89 using bv_funcs::INC_DEPTH;
91 using lyx::support::AddName;
92 using lyx::support::AddPath;
93 using lyx::support::bformat;
94 using lyx::support::ChangeExtension;
95 using lyx::support::FileFilterList;
96 using lyx::support::FileInfo;
97 using lyx::support::FileSearch;
98 using lyx::support::ForkedcallsController;
99 using lyx::support::i18nLibFileSearch;
100 using lyx::support::IsDirWriteable;
101 using lyx::support::IsFileReadable;
102 using lyx::support::isStrInt;
103 using lyx::support::MakeAbsPath;
104 using lyx::support::MakeDisplayPath;
105 using lyx::support::Path;
106 using lyx::support::rtrim;
107 using lyx::support::split;
108 using lyx::support::strToInt;
109 using lyx::support::strToUnsignedInt;
110 using lyx::support::system_lyxdir;
111 using lyx::support::token;
112 using lyx::support::trim;
113 using lyx::support::user_lyxdir;
114 using lyx::support::prefixIs;
115 using lyx::support::os::getTmpDir;
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
158 view()->cursor().resetAnchor();
163 void LyXFunc::processKeySym(LyXKeySymPtr keysym, key_modifier::state state)
167 if (lyxerr.debugging(Debug::KEY)) {
168 lyxerr << "KeySym is "
169 << keysym->getSymbolName()
173 // Do nothing if we have nothing (JMarc)
174 if (!keysym->isOK()) {
175 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
180 if (keysym->isModifier()) {
181 lyxerr[Debug::KEY] << "isModifier true" << endl;
185 Encoding const * encoding = view()->getEncoding();
187 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
189 // Do a one-deep top-level lookup for
190 // cancel and meta-fake keys. RVDK_PATCH_5
191 cancel_meta_seq.reset();
193 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
194 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
196 // When not cancel or meta-fake, do the normal lookup.
197 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
198 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
199 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
200 // remove Caps Lock and Mod2 as a modifiers
201 func = keyseq.addkey(keysym, (state | meta_fake_bit));
202 lyxerr[Debug::KEY] << "action now set to ["
203 << func.action << ']' << endl;
206 // Dont remove this unless you know what you are doing.
207 meta_fake_bit = key_modifier::none;
209 // can this happen now ?
210 if (func.action == LFUN_NOACTION) {
211 func = FuncRequest(LFUN_PREFIX);
214 if (lyxerr.debugging(Debug::KEY)) {
215 lyxerr << "Key [action="
216 << func.action << "]["
217 << keyseq.print() << ']'
221 // already here we know if it any point in going further
222 // why not return already here if action == -1 and
223 // num_bytes == 0? (Lgb)
225 if (keyseq.length() > 1) {
226 owner->message(keyseq.print());
230 // Maybe user can only reach the key via holding down shift.
231 // Let's see. But only if shift is the only modifier
232 if (func.action == LFUN_UNKNOWN_ACTION &&
233 state == key_modifier::shift) {
234 lyxerr[Debug::KEY] << "Trying without shift" << endl;
235 func = keyseq.addkey(keysym, key_modifier::none);
236 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
239 if (func.action == LFUN_UNKNOWN_ACTION) {
240 // Hmm, we didn't match any of the keysequences. See
241 // if it's normal insertable text not already covered
243 if (keysym->isText() && keyseq.length() == 1) {
244 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
245 func = FuncRequest(LFUN_SELFINSERT);
247 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
248 owner->message(_("Unknown function."));
253 if (func.action == LFUN_SELFINSERT) {
254 if (encoded_last_key != 0) {
256 arg += encoded_last_key;
257 dispatch(FuncRequest(LFUN_SELFINSERT, arg));
258 lyxerr[Debug::KEY] << "SelfInsert arg[`"
259 << argument << "']" << endl;
267 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
270 Buffer * buf = owner->buffer();
272 if (ev.action == LFUN_NOACTION) {
273 setStatusMessage(N_("Nothing to do"));
279 case LFUN_UNKNOWN_ACTION:
280 #ifndef HAVE_LIBAIKSAURUS
281 case LFUN_THESAURUS_ENTRY:
287 flag |= lyx_gui::getStatus(ev);
290 if (flag.unknown()) {
291 setStatusMessage(N_("Unknown action"));
295 // the default error message if we disable the command
296 setStatusMessage(N_("Command disabled"));
298 // Check whether we need a buffer
299 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
300 // Yes we need a buffer, do we have one?
303 // Can we use a readonly buffer?
304 if (buf->isReadonly() &&
305 !lyxaction.funcHasFlag(ev.action,
306 LyXAction::ReadOnly)) {
308 setStatusMessage(N_("Document is read-only"));
313 setStatusMessage(N_("Command not allowed with"
314 "out any document open"));
320 UpdatableInset * tli = view()->cursor().inset()
321 ? view()->cursor().inset()->asUpdatableInset() : 0;
322 InsetTabular * tab = view()->cursor().innerInsetTabular();
324 // I would really like to avoid having this switch and rather try to
325 // encode this in the function itself.
326 bool disable = false;
329 disable = ev.argument != "custom"
330 && !Exporter::IsExportable(*buf, ev.argument);
333 disable = buf->undostack().empty();
336 disable = buf->redostack().empty();
340 if (tab && tab->hasSelection())
343 disable = !view()->cursor().inMathed() && !view()->cursor().selection();
347 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
351 disable = !Exporter::IsExportable(*buf, "program");
354 case LFUN_LAYOUT_TABULAR:
355 disable = !view()->cursor().innerInsetTabular();
359 disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH);
362 case LFUN_DEPTH_PLUS:
363 disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH);
367 case LFUN_LAYOUT_PARAGRAPH: {
368 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
369 disable = inset && inset->forceDefaultParagraphs(inset);
373 case LFUN_INSET_OPTARG:
374 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
377 case LFUN_TABULAR_FEATURE:
379 if (view()->cursor().inMathed()) {
380 // FIXME: check temporarily disabled
382 char align = mathcursor::valign();
387 if (ev.argument.empty()) {
391 if (!contains("tcb", ev.argument[0])) {
395 flag.setOnOff(ev.argument[0] == align);
399 char align = mathcursor::halign();
404 if (ev.argument.empty()) {
408 if (!contains("lcr", ev.argument[0])) {
412 flag.setOnOff(ev.argument[0] == align);
414 disable = !mathcursor::halign();
420 //ret.disabled(true);
421 InsetTabular * tab = view()->cursor().innerInsetTabular();
423 ret = tab->getStatus(ev.argument);
430 static InsetTabular inset(*buf, 1, 1);
432 FuncStatus ret = inset.getStatus(ev.argument);
433 if (ret.onoff(true) || ret.onoff(false))
434 flag.setOnOff(false);
439 case LFUN_VC_REGISTER:
440 disable = buf->lyxvc().inUse();
442 case LFUN_VC_CHECKIN:
443 disable = !buf->lyxvc().inUse() || buf->isReadonly();
445 case LFUN_VC_CHECKOUT:
446 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
450 disable = !buf->lyxvc().inUse();
452 case LFUN_MENURELOAD:
453 disable = buf->isUnnamed() || buf->isClean();
455 case LFUN_BOOKMARK_GOTO:
457 isSavedPosition(strToUnsignedInt(ev.argument));
460 case LFUN_MERGE_CHANGES:
461 case LFUN_ACCEPT_CHANGE:
462 case LFUN_REJECT_CHANGE:
463 case LFUN_ACCEPT_ALL_CHANGES:
464 case LFUN_REJECT_ALL_CHANGES:
465 disable = !buf->params().tracking_changes;
468 case LFUN_INSET_SETTINGS: {
470 UpdatableInset * inset = view()->cursor().inset()
471 ? view()->cursor().inset()->asUpdatableInset() : 0;
476 // jump back to owner if an InsetText, so
477 // we get back to the InsetTabular or whatever
478 if (inset->lyxCode() == InsetOld::TEXT_CODE)
479 inset = inset->owner();
481 InsetOld::Code code = inset->lyxCode();
483 case InsetOld::TABULAR_CODE:
484 disable = ev.argument != "tabular";
486 case InsetOld::ERT_CODE:
487 disable = ev.argument != "ert";
489 case InsetOld::FLOAT_CODE:
490 disable = ev.argument != "float";
492 case InsetOld::WRAP_CODE:
493 disable = ev.argument != "wrap";
495 case InsetOld::NOTE_CODE:
496 disable = ev.argument != "note";
498 case InsetOld::BRANCH_CODE:
499 disable = ev.argument != "branch";
501 case InsetOld::BOX_CODE:
502 disable = ev.argument != "box";
510 case LFUN_MATH_MUTATE:
511 if (view()->cursor().inMathed())
512 //flag.setOnOff(mathcursor::formula()->hullType() == ev.argument);
513 flag.setOnOff(false);
518 // we just need to be in math mode to enable that
520 case LFUN_MATH_SPACE:
521 case LFUN_MATH_LIMITS:
522 case LFUN_MATH_NONUMBER:
523 case LFUN_MATH_NUMBER:
524 case LFUN_MATH_EXTERN:
525 disable = !view()->cursor().inMathed();
528 case LFUN_DIALOG_SHOW: {
529 string const name = ev.getArg(0);
531 disable = !(name == "aboutlyx" ||
534 name == "preferences" ||
539 if (name == "print") {
540 disable = !Exporter::IsExportable(*buf, "dvi") ||
541 lyxrc.print_command == "none";
542 } else if (name == "character") {
543 InsetBase * inset = view()->cursor().inset();
544 disable = inset && inset->lyxCode() == InsetOld::ERT_CODE;
545 } else if (name == "vclog") {
546 disable = !buf->lyxvc().inUse();
547 } else if (name == "latexlog") {
548 disable = !IsFileReadable(buf->getLogName().second);
557 // the functions which insert insets
558 InsetOld::Code code = InsetOld::NO_CODE;
560 case LFUN_DIALOG_SHOW_NEW_INSET:
561 if (ev.argument == "bibitem")
562 code = InsetOld::BIBITEM_CODE;
563 else if (ev.argument == "bibtex")
564 code = InsetOld::BIBTEX_CODE;
565 else if (ev.argument == "box")
566 code = InsetOld::BOX_CODE;
567 else if (ev.argument == "branch")
568 code = InsetOld::BRANCH_CODE;
569 else if (ev.argument == "citation")
570 code = InsetOld::CITE_CODE;
571 else if (ev.argument == "ert")
572 code = InsetOld::ERT_CODE;
573 else if (ev.argument == "external")
574 code = InsetOld::EXTERNAL_CODE;
575 else if (ev.argument == "float")
576 code = InsetOld::FLOAT_CODE;
577 else if (ev.argument == "graphics")
578 code = InsetOld::GRAPHICS_CODE;
579 else if (ev.argument == "include")
580 code = InsetOld::INCLUDE_CODE;
581 else if (ev.argument == "index")
582 code = InsetOld::INDEX_CODE;
583 else if (ev.argument == "label")
584 code = InsetOld::LABEL_CODE;
585 else if (ev.argument == "note")
586 code = InsetOld::NOTE_CODE;
587 else if (ev.argument == "ref")
588 code = InsetOld::REF_CODE;
589 else if (ev.argument == "toc")
590 code = InsetOld::TOC_CODE;
591 else if (ev.argument == "url")
592 code = InsetOld::URL_CODE;
593 else if (ev.argument == "vspace")
594 code = InsetOld::VSPACE_CODE;
595 else if (ev.argument == "wrap")
596 code = InsetOld::WRAP_CODE;
600 code = InsetOld::ERT_CODE;
602 case LFUN_INSET_FOOTNOTE:
603 code = InsetOld::FOOT_CODE;
605 case LFUN_TABULAR_INSERT:
606 code = InsetOld::TABULAR_CODE;
608 case LFUN_INSET_MARGINAL:
609 code = InsetOld::MARGIN_CODE;
611 case LFUN_INSET_FLOAT:
612 case LFUN_INSET_WIDE_FLOAT:
613 code = InsetOld::FLOAT_CODE;
615 case LFUN_INSET_WRAP:
616 code = InsetOld::WRAP_CODE;
618 case LFUN_FLOAT_LIST:
619 code = InsetOld::FLOAT_LIST_CODE;
622 case LFUN_INSET_LIST:
623 code = InsetOld::LIST_CODE;
625 case LFUN_INSET_THEOREM:
626 code = InsetOld::THEOREM_CODE;
629 case LFUN_INSET_CAPTION:
630 code = InsetOld::CAPTION_CODE;
632 case LFUN_INSERT_NOTE:
633 code = InsetOld::NOTE_CODE;
635 case LFUN_INSERT_CHARSTYLE:
636 code = InsetOld::CHARSTYLE_CODE;
637 if (buf->params().getLyXTextClass().charstyles().empty())
640 case LFUN_INSERT_BOX:
641 code = InsetOld::BOX_CODE;
643 case LFUN_INSERT_BRANCH:
644 code = InsetOld::BRANCH_CODE;
645 if (buf->params().branchlist().empty())
648 case LFUN_INSERT_LABEL:
649 code = InsetOld::LABEL_CODE;
651 case LFUN_INSET_OPTARG:
652 code = InsetOld::OPTARG_CODE;
654 case LFUN_ENVIRONMENT_INSERT:
655 code = InsetOld::BOX_CODE;
657 case LFUN_INDEX_INSERT:
658 code = InsetOld::INDEX_CODE;
660 case LFUN_INDEX_PRINT:
661 code = InsetOld::INDEX_PRINT_CODE;
663 case LFUN_TOC_INSERT:
664 code = InsetOld::TOC_CODE;
668 code = InsetOld::URL_CODE;
671 // always allow this, since we will inset a raw quote
672 // if an inset is not allowed.
674 case LFUN_HYPHENATION:
675 case LFUN_LIGATURE_BREAK:
677 case LFUN_MENU_SEPARATOR:
679 case LFUN_END_OF_SENTENCE:
680 code = InsetOld::SPECIALCHAR_CODE;
682 case LFUN_SPACE_INSERT:
683 // slight hack: we know this is allowed in math mode
684 if (!view()->cursor().inMathed())
685 code = InsetOld::SPACE_CODE;
687 case LFUN_INSET_DIALOG_SHOW: {
688 InsetBase * inset = view()->cursor().nextInset();
691 code = inset->lyxCode();
692 if (!(code == InsetOld::INCLUDE_CODE
693 || code == InsetOld::BIBTEX_CODE
694 || code == InsetOld::FLOAT_LIST_CODE
695 || code == InsetOld::TOC_CODE))
703 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
709 // A few general toggles
711 case LFUN_TOOLTIPS_TOGGLE:
712 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
715 case LFUN_READ_ONLY_TOGGLE:
716 flag.setOnOff(buf->isReadonly());
719 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
721 case LFUN_SWITCHBUFFER:
722 // toggle on the current buffer, but do not toggle off
723 // the other ones (is that a good idea?)
724 if (ev.argument == buf->fileName())
727 case LFUN_TRACK_CHANGES:
728 flag.setOnOff(buf->params().tracking_changes);
735 // the font related toggles
736 if (!view()->cursor().inMathed()) {
737 LyXFont const & font = view()->getLyXText()->real_current_font;
740 flag.setOnOff(font.emph() == LyXFont::ON);
743 flag.setOnOff(font.noun() == LyXFont::ON);
746 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
749 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
752 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
755 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
761 string tc = mathcursor::getLastCode();
764 flag.setOnOff(tc == "mathbf");
767 flag.setOnOff(tc == "mathsf");
770 flag.setOnOff(tc == "mathcal");
773 flag.setOnOff(tc == "mathrm");
776 flag.setOnOff(tc == "mathtt");
779 flag.setOnOff(tc == "mathbb");
782 flag.setOnOff(tc == "mathnormal");
790 // this one is difficult to get right. As a half-baked
791 // solution, we consider only the first action of the sequence
792 if (ev.action == LFUN_SEQUENCE) {
793 // argument contains ';'-terminated commands
794 #warning LyXAction arguments not handled here.
795 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
804 bool ensureBufferClean(BufferView * bv)
806 Buffer & buf = *bv->buffer();
810 string const file = MakeDisplayPath(buf.fileName(), 30);
811 string text = bformat(_("The document %1$s has unsaved "
812 "changes.\n\nDo you want to save "
813 "the document?"), file);
814 int const ret = Alert::prompt(_("Save changed document?"),
815 text, 0, 1, _("&Save"),
819 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
821 return buf.isClean();
827 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
829 string argument = cmd.argument;
830 kb_action action = cmd.action;
832 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
833 //lyxerr << "*** LyXFunc::dispatch: cmd: " << cmd << endl;
835 // we have not done anything wrong yet.
837 dispatch_buffer.erase();
838 selection_possible = false;
840 // We cannot use this function here
841 if (getStatus(cmd).disabled()) {
842 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
843 << lyxaction.getActionName(action)
844 << " [" << action << "] is disabled at this location"
846 setErrorMessage(getStatusMessage());
850 if (view()->available())
851 view()->hideCursor();
856 if (!view()->available())
858 view()->cursor().pop();
859 // Tell the paragraph dialog that we changed paragraph
860 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
864 case LFUN_WORDFINDFORWARD:
865 case LFUN_WORDFINDBACKWARD: {
866 static string last_search;
867 string searched_string;
869 if (!argument.empty()) {
870 last_search = argument;
871 searched_string = argument;
873 searched_string = last_search;
875 if (searched_string.empty())
878 bool const fw = action == LFUN_WORDFINDFORWARD;
880 lyx::find::find2string(searched_string, true, false, fw);
881 view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
886 owner->message(keyseq.printOptions());
889 case LFUN_EXEC_COMMAND:
890 owner->focus_command_buffer();
895 meta_fake_bit = key_modifier::none;
896 if (view()->available())
897 // cancel any selection
898 dispatch(FuncRequest(LFUN_MARK_OFF));
899 setMessage(N_("Cancel"));
903 meta_fake_bit = key_modifier::alt;
904 setMessage(keyseq.print());
907 case LFUN_READ_ONLY_TOGGLE:
908 if (owner->buffer()->lyxvc().inUse())
909 owner->buffer()->lyxvc().toggleReadOnly();
911 owner->buffer()->setReadonly(
912 !owner->buffer()->isReadonly());
915 case LFUN_CENTER: // this is center and redraw.
919 // --- Menus -----------------------------------------------
921 menuNew(argument, false);
924 case LFUN_MENUNEWTMPLT:
925 menuNew(argument, true);
928 case LFUN_CLOSEBUFFER:
933 if (!owner->buffer()->isUnnamed()) {
934 string const str = bformat(_("Saving document %1$s..."),
935 MakeDisplayPath(owner->buffer()->fileName()));
937 MenuWrite(owner->buffer());
938 owner->message(str + _(" done."));
940 WriteAs(owner->buffer());
944 WriteAs(owner->buffer(), argument);
947 case LFUN_MENURELOAD: {
948 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
949 string text = bformat(_("Any changes will be lost. Are you sure "
950 "you want to revert to the saved version of the document %1$s?"), file);
951 int const ret = Alert::prompt(_("Revert to saved document?"),
952 text, 0, 1, _("&Revert"), _("&Cancel"));
960 Exporter::Export(owner->buffer(), argument, true);
961 view()->showErrorList(BufferFormat(*owner->buffer()));
965 Exporter::Preview(owner->buffer(), argument);
966 view()->showErrorList(BufferFormat(*owner->buffer()));
970 Exporter::Export(owner->buffer(), "program", true);
971 view()->showErrorList(_("Build"));
975 owner->buffer()->runChktex();
976 view()->showErrorList(_("ChkTeX"));
980 if (argument == "custom")
981 owner->getDialogs().showSendto();
983 Exporter::Export(owner->buffer(), argument, false);
984 view()->showErrorList(BufferFormat(*owner->buffer()));
997 InsetCommandParams p("tableofcontents");
998 string const data = InsetCommandMailer::params2string("toc", p);
999 owner->getDialogs().show("toc", data, 0);
1015 case LFUN_RECONFIGURE:
1016 Reconfigure(view());
1019 case LFUN_HELP_OPEN: {
1020 string const arg = argument;
1022 setErrorMessage(N_("Missing argument"));
1025 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1026 if (fname.empty()) {
1027 lyxerr << "LyX: unable to find documentation file `"
1028 << arg << "'. Bad installation?" << endl;
1031 owner->message(bformat(_("Opening help file %1$s..."),
1032 MakeDisplayPath(fname)));
1033 view()->loadLyXFile(fname, false);
1037 // --- version control -------------------------------
1038 case LFUN_VC_REGISTER:
1039 if (!ensureBufferClean(view()))
1041 if (!owner->buffer()->lyxvc().inUse()) {
1042 owner->buffer()->lyxvc().registrer();
1047 case LFUN_VC_CHECKIN:
1048 if (!ensureBufferClean(view()))
1050 if (owner->buffer()->lyxvc().inUse()
1051 && !owner->buffer()->isReadonly()) {
1052 owner->buffer()->lyxvc().checkIn();
1057 case LFUN_VC_CHECKOUT:
1058 if (!ensureBufferClean(view()))
1060 if (owner->buffer()->lyxvc().inUse()
1061 && owner->buffer()->isReadonly()) {
1062 owner->buffer()->lyxvc().checkOut();
1067 case LFUN_VC_REVERT:
1068 owner->buffer()->lyxvc().revert();
1073 owner->buffer()->lyxvc().undoLast();
1077 // --- buffers ----------------------------------------
1078 case LFUN_SWITCHBUFFER:
1079 view()->buffer(bufferlist.getBuffer(argument));
1083 NewFile(view(), argument);
1086 case LFUN_FILE_OPEN:
1090 case LFUN_LAYOUT_TABULAR:
1091 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1092 tab->openLayoutDialog(view());
1095 case LFUN_DROP_LAYOUTS_CHOICE:
1096 owner->getToolbar().openLayoutList();
1099 case LFUN_MENU_OPEN_BY_NAME:
1100 owner->getMenubar().openByName(argument);
1103 // --- lyxserver commands ----------------------------
1105 setMessage(owner->buffer()->fileName());
1106 lyxerr[Debug::INFO] << "FNAME["
1107 << owner->buffer()->fileName()
1112 dispatch_buffer = keyseq.print();
1113 lyxserver->notifyClient(dispatch_buffer);
1116 case LFUN_GOTOFILEROW: {
1119 istringstream is(argument);
1120 is >> file_name >> row;
1121 if (prefixIs(file_name, getTmpDir())) {
1122 // Needed by inverse dvi search. If it is a file
1123 // in tmpdir, call the apropriated function
1124 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1126 // Must replace extension of the file to be .lyx
1127 // and get full path
1128 string const s = ChangeExtension(file_name, ".lyx");
1129 // Either change buffer or load the file
1130 if (bufferlist.exists(s)) {
1131 view()->buffer(bufferlist.getBuffer(s));
1133 view()->loadLyXFile(s);
1137 view()->setCursorFromRow(row);
1140 // see BufferView_pimpl::center()
1141 view()->updateScrollbar();
1145 case LFUN_GOTO_PARAGRAPH: {
1146 istringstream is(argument);
1149 ParIterator par = owner->buffer()->getParFromID(id);
1150 if (par == owner->buffer()->par_iterator_end()) {
1151 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1152 << id << ']' << endl;
1155 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1156 << " found." << endl;
1160 view()->setCursor(par, 0);
1162 view()->switchKeyMap();
1163 owner->view_state_changed();
1166 // see BufferView_pimpl::center()
1167 view()->updateScrollbar();
1171 // --- Mathed stuff. If we are here, there is no locked inset yet.
1172 case LFUN_MATH_EXTERN:
1173 case LFUN_MATH_NUMBER:
1174 case LFUN_MATH_NONUMBER:
1175 case LFUN_MATH_LIMITS:
1176 setErrorMessage(N_("This is only allowed in math mode!"));
1179 case LFUN_DIALOG_SHOW: {
1180 string const name = cmd.getArg(0);
1181 string data = trim(cmd.argument.substr(name.size()));
1183 if (name == "character") {
1184 data = freefont2string();
1186 owner->getDialogs().show("character", data);
1188 else if (name == "document")
1189 owner->getDialogs().showDocument();
1190 else if (name == "forks")
1191 owner->getDialogs().showForks();
1192 else if (name == "preamble")
1193 owner->getDialogs().showPreamble();
1194 else if (name == "preferences")
1195 owner->getDialogs().showPreferences();
1196 else if (name == "print")
1197 owner->getDialogs().showPrint();
1198 else if (name == "spellchecker")
1199 owner->getDialogs().showSpellchecker();
1201 else if (name == "latexlog") {
1202 pair<Buffer::LogType, string> const logfile =
1203 owner->buffer()->getLogName();
1204 switch (logfile.first) {
1205 case Buffer::latexlog:
1208 case Buffer::buildlog:
1212 data += logfile.second;
1213 owner->getDialogs().show("log", data);
1215 else if (name == "vclog") {
1216 string const data = "vc " +
1217 owner->buffer()->lyxvc().getLogFile();
1218 owner->getDialogs().show("log", data);
1221 owner->getDialogs().show(name, data);
1225 case LFUN_DIALOG_SHOW_NEW_INSET: {
1226 string const name = cmd.getArg(0);
1227 string data = trim(cmd.argument.substr(name.size()));
1228 if (name == "bibitem" ||
1230 name == "include" ||
1236 InsetCommandParams p(name);
1237 data = InsetCommandMailer::params2string(name, p);
1238 } else if (name == "box") {
1239 // \c data == "Boxed" || "Frameless" etc
1240 InsetBoxParams p(data);
1241 data = InsetBoxMailer::params2string(p);
1242 } else if (name == "branch") {
1243 InsetBranchParams p;
1244 data = InsetBranchMailer::params2string(p);
1245 } else if (name == "citation") {
1246 InsetCommandParams p("cite");
1247 data = InsetCommandMailer::params2string(name, p);
1248 } else if (name == "ert") {
1249 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1250 } else if (name == "external") {
1251 InsetExternalParams p;
1252 Buffer const & buffer = *owner->buffer();
1253 data = InsetExternalMailer::params2string(p, buffer);
1254 } else if (name == "float") {
1256 data = InsetFloatMailer::params2string(p);
1257 } else if (name == "graphics") {
1258 InsetGraphicsParams p;
1259 Buffer const & buffer = *owner->buffer();
1260 data = InsetGraphicsMailer::params2string(p, buffer);
1261 } else if (name == "note") {
1263 data = InsetNoteMailer::params2string(p);
1264 } else if (name == "vspace") {
1266 data = InsetVSpaceMailer::params2string(space);
1267 } else if (name == "wrap") {
1269 data = InsetWrapMailer::params2string(p);
1271 owner->getDialogs().show(name, data, 0);
1275 case LFUN_DIALOG_SHOW_NEXT_INSET:
1278 case LFUN_INSET_DIALOG_SHOW: {
1279 InsetBase * inset = view()->cursor().nextInset();
1281 inset->dispatch(view()->cursor(), FuncRequest(LFUN_INSET_DIALOG_SHOW));
1285 case LFUN_DIALOG_UPDATE: {
1286 string const & name = argument;
1287 // Can only update a dialog connected to an existing inset
1288 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1290 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
1291 inset->dispatch(view()->cursor(), fr);
1292 } else if (name == "paragraph") {
1293 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1298 case LFUN_DIALOG_HIDE:
1299 Dialogs::hide(argument, 0);
1302 case LFUN_DIALOG_DISCONNECT_INSET:
1303 owner->getDialogs().disconnect(argument);
1306 case LFUN_CHILDOPEN: {
1307 string const filename =
1308 MakeAbsPath(argument, owner->buffer()->filePath());
1309 setMessage(N_("Opening child document ") +
1310 MakeDisplayPath(filename) + "...");
1311 view()->savePosition(0);
1312 string const parentfilename = owner->buffer()->fileName();
1313 if (bufferlist.exists(filename))
1314 view()->buffer(bufferlist.getBuffer(filename));
1316 view()->loadLyXFile(filename);
1317 // Set the parent name of the child document.
1318 // This makes insertion of citations and references in the child work,
1319 // when the target is in the parent or another child document.
1320 owner->buffer()->setParentName(parentfilename);
1324 case LFUN_TOGGLECURSORFOLLOW:
1325 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1329 owner->getIntl().KeyMapOn(false);
1332 case LFUN_KMAP_PRIM:
1333 owner->getIntl().KeyMapPrim();
1337 owner->getIntl().KeyMapSec();
1340 case LFUN_KMAP_TOGGLE:
1341 owner->getIntl().ToggleKeyMap();
1347 argument = split(argument, countstr, ' ');
1348 istringstream is(countstr);
1351 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1352 for (int i = 0; i < count; ++i)
1353 dispatch(lyxaction.lookupFunc(argument));
1358 // argument contains ';'-terminated commands
1359 while (!argument.empty()) {
1361 argument = split(argument, first, ';');
1362 dispatch(lyxaction.lookupFunc(first));
1366 case LFUN_SAVEPREFERENCES: {
1367 Path p(user_lyxdir());
1368 lyxrc.write("preferences");
1372 case LFUN_SCREEN_FONT_UPDATE:
1373 // handle the screen font changes.
1374 lyxrc.set_font_norm_type();
1375 lyx_gui::update_fonts();
1376 // All visible buffers will need resize
1380 case LFUN_SET_COLOR: {
1382 string const x11_name = split(argument, lyx_name, ' ');
1383 if (lyx_name.empty() || x11_name.empty()) {
1384 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1389 bool const graphicsbg_changed =
1390 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1391 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1393 LColor::color col = lcolor.getFromLyXName(lyx_name);
1394 if (!lcolor.setColor(col, x11_name)) {
1396 bformat(_("Set-color \"%1$s\" failed "
1397 "- color is undefined or "
1398 "may not be redefined"), lyx_name));
1402 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1404 if (graphicsbg_changed) {
1405 #ifdef WITH_WARNINGS
1406 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1409 lyx::graphics::GCache::get().changeDisplay(true);
1416 owner->message(argument);
1419 case LFUN_FORKS_KILL:
1420 if (isStrInt(argument))
1421 ForkedcallsController::get().kill(strToInt(argument));
1424 case LFUN_TOOLTIPS_TOGGLE:
1425 owner->getDialogs().toggleTooltips();
1428 case LFUN_EXTERNAL_EDIT:
1429 InsetExternal().dispatch(view()->cursor(), FuncRequest(action, argument));
1433 view()->cursor().dispatch(cmd);
1438 if (!view()->cursor().inMathed())
1439 view()->owner()->updateLayoutChoice();
1441 if (view()->available()) {
1442 view()->fitCursor();
1444 view()->cursor().updatePos();
1445 // if we executed a mutating lfun, mark the buffer as dirty
1446 if (!getStatus(cmd).disabled()
1447 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1448 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1449 view()->buffer()->markDirty();
1452 if (!view()->cursor().inMathed())
1453 sendDispatchMessage(getMessage(), cmd, verbose);
1457 void LyXFunc::sendDispatchMessage(string const & msg,
1458 FuncRequest const & cmd, bool verbose)
1460 owner->updateMenubar();
1461 owner->updateToolbar();
1463 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1464 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1466 owner->message(msg);
1470 string dispatch_msg = msg;
1471 if (!dispatch_msg.empty())
1472 dispatch_msg += ' ';
1474 string comname = lyxaction.getActionName(cmd.action);
1476 bool argsadded = false;
1478 if (!cmd.argument.empty()) {
1479 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1480 comname += ' ' + cmd.argument;
1485 string const shortcuts = toplevel_keymap->findbinding(cmd);
1487 if (!shortcuts.empty()) {
1488 comname += ": " + shortcuts;
1489 } else if (!argsadded && !cmd.argument.empty()) {
1490 comname += ' ' + cmd.argument;
1493 if (!comname.empty()) {
1494 comname = rtrim(comname);
1495 dispatch_msg += '(' + comname + ')';
1498 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1499 if (!dispatch_msg.empty())
1500 owner->message(dispatch_msg);
1504 void LyXFunc::setupLocalKeymap()
1506 keyseq.stdmap = toplevel_keymap.get();
1507 keyseq.curmap = toplevel_keymap.get();
1508 cancel_meta_seq.stdmap = toplevel_keymap.get();
1509 cancel_meta_seq.curmap = toplevel_keymap.get();
1513 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1515 string initpath = lyxrc.document_path;
1516 string filename(name);
1518 if (view()->available()) {
1519 string const trypath = owner->buffer()->filePath();
1520 // If directory is writeable, use this as default.
1521 if (IsDirWriteable(trypath))
1525 static int newfile_number;
1527 if (filename.empty()) {
1528 filename = AddName(lyxrc.document_path,
1529 "newfile" + tostr(++newfile_number) + ".lyx");
1530 FileInfo fi(filename);
1531 while (bufferlist.exists(filename) || fi.readable()) {
1533 filename = AddName(lyxrc.document_path,
1534 "newfile" + tostr(newfile_number) +
1536 fi.newFile(filename);
1540 // The template stuff
1543 FileDialog fileDlg(_("Select template file"),
1544 LFUN_SELECT_FILE_SYNC,
1545 make_pair(string(_("Documents|#o#O")),
1546 string(lyxrc.document_path)),
1547 make_pair(string(_("Templates|#T#t")),
1548 string(lyxrc.template_path)));
1550 FileDialog::Result result =
1551 fileDlg.open(lyxrc.template_path,
1552 FileFilterList(_("LyX Documents (*.lyx)")),
1555 if (result.first == FileDialog::Later)
1557 if (result.second.empty())
1559 templname = result.second;
1562 view()->newFile(filename, templname, !name.empty());
1566 void LyXFunc::open(string const & fname)
1568 string initpath = lyxrc.document_path;
1570 if (view()->available()) {
1571 string const trypath = owner->buffer()->filePath();
1572 // If directory is writeable, use this as default.
1573 if (IsDirWriteable(trypath))
1579 if (fname.empty()) {
1580 FileDialog fileDlg(_("Select document to open"),
1582 make_pair(string(_("Documents|#o#O")),
1583 string(lyxrc.document_path)),
1584 make_pair(string(_("Examples|#E#e")),
1585 string(AddPath(system_lyxdir(), "examples"))));
1587 FileDialog::Result result =
1588 fileDlg.open(initpath,
1589 FileFilterList(_("LyX Documents (*.lyx)")),
1592 if (result.first == FileDialog::Later)
1595 filename = result.second;
1597 // check selected filename
1598 if (filename.empty()) {
1599 owner->message(_("Canceled."));
1605 // get absolute path of file and add ".lyx" to the filename if
1607 string const fullpath = FileSearch(string(), filename, "lyx");
1608 if (!fullpath.empty()) {
1609 filename = fullpath;
1612 string const disp_fn(MakeDisplayPath(filename));
1614 // if the file doesn't exist, let the user create one
1615 FileInfo const f(filename, true);
1617 // the user specifically chose this name. Believe them.
1618 view()->newFile(filename, "", true);
1622 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1625 if (view()->loadLyXFile(filename)) {
1626 str2 = bformat(_("Document %1$s opened."), disp_fn);
1628 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1630 owner->message(str2);
1634 void LyXFunc::doImport(string const & argument)
1637 string filename = split(argument, format, ' ');
1639 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1640 << " file: " << filename << endl;
1642 // need user interaction
1643 if (filename.empty()) {
1644 string initpath = lyxrc.document_path;
1646 if (view()->available()) {
1647 string const trypath = owner->buffer()->filePath();
1648 // If directory is writeable, use this as default.
1649 if (IsDirWriteable(trypath))
1653 string const text = bformat(_("Select %1$s file to import"),
1654 formats.prettyName(format));
1656 FileDialog fileDlg(text,
1658 make_pair(string(_("Documents|#o#O")),
1659 string(lyxrc.document_path)),
1660 make_pair(string(_("Examples|#E#e")),
1661 string(AddPath(system_lyxdir(), "examples"))));
1663 string const filter = formats.prettyName(format)
1664 + " (*." + formats.extension(format) + ')';
1666 FileDialog::Result result =
1667 fileDlg.open(initpath,
1668 FileFilterList(filter),
1671 if (result.first == FileDialog::Later)
1674 filename = result.second;
1676 // check selected filename
1677 if (filename.empty())
1678 owner->message(_("Canceled."));
1681 if (filename.empty())
1684 // get absolute path of file
1685 filename = MakeAbsPath(filename);
1687 string const lyxfile = ChangeExtension(filename, ".lyx");
1689 // Check if the document already is open
1690 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1691 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1692 owner->message(_("Canceled."));
1697 // if the file exists already, and we didn't do
1698 // -i lyx thefile.lyx, warn
1699 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1700 string const file = MakeDisplayPath(lyxfile, 30);
1702 string text = bformat(_("The document %1$s already exists.\n\n"
1703 "Do you want to over-write that document?"), file);
1704 int const ret = Alert::prompt(_("Over-write document?"),
1705 text, 0, 1, _("&Over-write"), _("&Cancel"));
1708 owner->message(_("Canceled."));
1713 Importer::Import(owner, filename, format);
1717 void LyXFunc::closeBuffer()
1719 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1720 if (bufferlist.empty()) {
1721 // need this otherwise SEGV may occur while
1722 // trying to set variables that don't exist
1723 // since there's no current buffer
1724 owner->getDialogs().hideBufferDependent();
1726 view()->buffer(bufferlist.first());
1732 // Each "owner" should have it's own message method. lyxview and
1733 // the minibuffer would use the minibuffer, but lyxserver would
1734 // send an ERROR signal to its client. Alejandro 970603
1735 // This function is bit problematic when it comes to NLS, to make the
1736 // lyx servers client be language indepenent we must not translate
1737 // strings sent to this func.
1738 void LyXFunc::setErrorMessage(string const & m) const
1740 dispatch_buffer = m;
1745 void LyXFunc::setMessage(string const & m) const
1747 dispatch_buffer = m;
1751 void LyXFunc::setStatusMessage(string const & m) const
1757 string const LyXFunc::view_status_message()
1759 // When meta-fake key is pressed, show the key sequence so far + "M-".
1761 return keyseq.print() + "M-";
1764 // Else, when a non-complete key sequence is pressed,
1765 // show the available options.
1766 if (keyseq.length() > 0 && !keyseq.deleted()) {
1767 return keyseq.printOptions();
1770 if (!view()->available())
1771 return _("Welcome to LyX!");
1773 return currentState(view());
1777 BufferView * LyXFunc::view() const
1779 BOOST_ASSERT(owner);
1780 return owner->view().get();
1784 bool LyXFunc::wasMetaKey() const
1786 return (meta_fake_bit != key_modifier::none);