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()->getLyXText()->getInset();
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();
839 #ifdef NEW_DISPATCHER
840 // We try do call the most specific dispatcher first:
841 // 1. the lockinginset's dispatch
842 // 2. the bufferview's dispatch
843 // 3. the lyxview's dispatch
846 selection_possible = false;
848 // We cannot use this function here
849 if (getStatus(cmd).disabled()) {
850 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
851 << lyxaction.getActionName(action)
852 << " [" << action << "] is disabled at this location"
854 setErrorMessage(getStatusMessage());
858 if (view()->available())
859 view()->hideCursor();
864 if (!view()->available())
866 view()->cursor().pop();
867 // Tell the paragraph dialog that we changed paragraph
868 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
872 case LFUN_WORDFINDFORWARD:
873 case LFUN_WORDFINDBACKWARD: {
874 static string last_search;
875 string searched_string;
877 if (!argument.empty()) {
878 last_search = argument;
879 searched_string = argument;
881 searched_string = last_search;
883 if (searched_string.empty())
886 bool const fw = action == LFUN_WORDFINDFORWARD;
888 lyx::find::find2string(searched_string, true, false, fw);
889 view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
894 owner->message(keyseq.printOptions());
897 case LFUN_EXEC_COMMAND:
898 owner->focus_command_buffer();
903 meta_fake_bit = key_modifier::none;
904 if (view()->available())
905 // cancel any selection
906 dispatch(FuncRequest(LFUN_MARK_OFF));
907 setMessage(N_("Cancel"));
911 meta_fake_bit = key_modifier::alt;
912 setMessage(keyseq.print());
915 case LFUN_READ_ONLY_TOGGLE:
916 if (owner->buffer()->lyxvc().inUse())
917 owner->buffer()->lyxvc().toggleReadOnly();
919 owner->buffer()->setReadonly(
920 !owner->buffer()->isReadonly());
923 case LFUN_CENTER: // this is center and redraw.
927 // --- Menus -----------------------------------------------
929 menuNew(argument, false);
932 case LFUN_MENUNEWTMPLT:
933 menuNew(argument, true);
936 case LFUN_CLOSEBUFFER:
941 if (!owner->buffer()->isUnnamed()) {
942 string const str = bformat(_("Saving document %1$s..."),
943 MakeDisplayPath(owner->buffer()->fileName()));
945 MenuWrite(owner->buffer());
946 owner->message(str + _(" done."));
948 WriteAs(owner->buffer());
952 WriteAs(owner->buffer(), argument);
955 case LFUN_MENURELOAD: {
956 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
957 string text = bformat(_("Any changes will be lost. Are you sure "
958 "you want to revert to the saved version of the document %1$s?"), file);
959 int const ret = Alert::prompt(_("Revert to saved document?"),
960 text, 0, 1, _("&Revert"), _("&Cancel"));
968 Exporter::Export(owner->buffer(), argument, true);
969 view()->showErrorList(BufferFormat(*owner->buffer()));
973 Exporter::Preview(owner->buffer(), argument);
974 view()->showErrorList(BufferFormat(*owner->buffer()));
978 Exporter::Export(owner->buffer(), "program", true);
979 view()->showErrorList(_("Build"));
983 owner->buffer()->runChktex();
984 view()->showErrorList(_("ChkTeX"));
988 if (argument == "custom")
989 owner->getDialogs().showSendto();
991 Exporter::Export(owner->buffer(), argument, false);
992 view()->showErrorList(BufferFormat(*owner->buffer()));
1004 case LFUN_TOCVIEW: {
1005 InsetCommandParams p("tableofcontents");
1006 string const data = InsetCommandMailer::params2string("toc", p);
1007 owner->getDialogs().show("toc", data, 0);
1023 case LFUN_RECONFIGURE:
1024 Reconfigure(view());
1027 case LFUN_HELP_OPEN: {
1028 string const arg = argument;
1030 setErrorMessage(N_("Missing argument"));
1033 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1034 if (fname.empty()) {
1035 lyxerr << "LyX: unable to find documentation file `"
1036 << arg << "'. Bad installation?" << endl;
1039 owner->message(bformat(_("Opening help file %1$s..."),
1040 MakeDisplayPath(fname)));
1041 view()->loadLyXFile(fname, false);
1045 // --- version control -------------------------------
1046 case LFUN_VC_REGISTER:
1047 if (!ensureBufferClean(view()))
1049 if (!owner->buffer()->lyxvc().inUse()) {
1050 owner->buffer()->lyxvc().registrer();
1055 case LFUN_VC_CHECKIN:
1056 if (!ensureBufferClean(view()))
1058 if (owner->buffer()->lyxvc().inUse()
1059 && !owner->buffer()->isReadonly()) {
1060 owner->buffer()->lyxvc().checkIn();
1065 case LFUN_VC_CHECKOUT:
1066 if (!ensureBufferClean(view()))
1068 if (owner->buffer()->lyxvc().inUse()
1069 && owner->buffer()->isReadonly()) {
1070 owner->buffer()->lyxvc().checkOut();
1075 case LFUN_VC_REVERT:
1076 owner->buffer()->lyxvc().revert();
1081 owner->buffer()->lyxvc().undoLast();
1085 // --- buffers ----------------------------------------
1086 case LFUN_SWITCHBUFFER:
1087 view()->buffer(bufferlist.getBuffer(argument));
1091 NewFile(view(), argument);
1094 case LFUN_FILE_OPEN:
1098 case LFUN_LAYOUT_TABULAR:
1099 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1100 tab->openLayoutDialog(view());
1103 case LFUN_DROP_LAYOUTS_CHOICE:
1104 owner->getToolbar().openLayoutList();
1107 case LFUN_MENU_OPEN_BY_NAME:
1108 owner->getMenubar().openByName(argument);
1111 // --- lyxserver commands ----------------------------
1113 setMessage(owner->buffer()->fileName());
1114 lyxerr[Debug::INFO] << "FNAME["
1115 << owner->buffer()->fileName()
1120 dispatch_buffer = keyseq.print();
1121 lyxserver->notifyClient(dispatch_buffer);
1124 case LFUN_GOTOFILEROW: {
1127 istringstream is(argument);
1128 is >> file_name >> row;
1129 if (prefixIs(file_name, getTmpDir())) {
1130 // Needed by inverse dvi search. If it is a file
1131 // in tmpdir, call the apropriated function
1132 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1134 // Must replace extension of the file to be .lyx
1135 // and get full path
1136 string const s = ChangeExtension(file_name, ".lyx");
1137 // Either change buffer or load the file
1138 if (bufferlist.exists(s)) {
1139 view()->buffer(bufferlist.getBuffer(s));
1141 view()->loadLyXFile(s);
1145 view()->setCursorFromRow(row);
1148 // see BufferView_pimpl::center()
1149 view()->updateScrollbar();
1153 case LFUN_GOTO_PARAGRAPH: {
1154 istringstream is(argument);
1157 ParIterator par = owner->buffer()->getParFromID(id);
1158 if (par == owner->buffer()->par_iterator_end()) {
1159 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1160 << id << ']' << endl;
1163 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1164 << " found." << endl;
1168 view()->setCursor(par, 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 = cmd.getArg(0);
1189 string data = trim(cmd.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 == "forks")
1199 owner->getDialogs().showForks();
1200 else if (name == "preamble")
1201 owner->getDialogs().showPreamble();
1202 else if (name == "preferences")
1203 owner->getDialogs().showPreferences();
1204 else if (name == "print")
1205 owner->getDialogs().showPrint();
1206 else if (name == "spellchecker")
1207 owner->getDialogs().showSpellchecker();
1209 else if (name == "latexlog") {
1210 pair<Buffer::LogType, string> const logfile =
1211 owner->buffer()->getLogName();
1212 switch (logfile.first) {
1213 case Buffer::latexlog:
1216 case Buffer::buildlog:
1220 data += logfile.second;
1221 owner->getDialogs().show("log", data);
1223 else if (name == "vclog") {
1224 string const data = "vc " +
1225 owner->buffer()->lyxvc().getLogFile();
1226 owner->getDialogs().show("log", data);
1229 owner->getDialogs().show(name, data);
1233 case LFUN_DIALOG_SHOW_NEW_INSET: {
1234 string const name = cmd.getArg(0);
1235 string data = trim(cmd.argument.substr(name.size()));
1236 if (name == "bibitem" ||
1238 name == "include" ||
1244 InsetCommandParams p(name);
1245 data = InsetCommandMailer::params2string(name, p);
1246 } else if (name == "box") {
1247 // \c data == "Boxed" || "Frameless" etc
1248 InsetBoxParams p(data);
1249 data = InsetBoxMailer::params2string(p);
1250 } else if (name == "branch") {
1251 InsetBranchParams p;
1252 data = InsetBranchMailer::params2string(p);
1253 } else if (name == "citation") {
1254 InsetCommandParams p("cite");
1255 data = InsetCommandMailer::params2string(name, p);
1256 } else if (name == "ert") {
1257 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1258 } else if (name == "external") {
1259 InsetExternalParams p;
1260 Buffer const & buffer = *owner->buffer();
1261 data = InsetExternalMailer::params2string(p, buffer);
1262 } else if (name == "float") {
1264 data = InsetFloatMailer::params2string(p);
1265 } else if (name == "graphics") {
1266 InsetGraphicsParams p;
1267 Buffer const & buffer = *owner->buffer();
1268 data = InsetGraphicsMailer::params2string(p, buffer);
1269 } else if (name == "note") {
1271 data = InsetNoteMailer::params2string(p);
1272 } else if (name == "vspace") {
1274 data = InsetVSpaceMailer::params2string(space);
1275 } else if (name == "wrap") {
1277 data = InsetWrapMailer::params2string(p);
1279 owner->getDialogs().show(name, data, 0);
1283 case LFUN_DIALOG_SHOW_NEXT_INSET:
1286 case LFUN_INSET_DIALOG_SHOW: {
1287 InsetBase * inset = view()->getLyXText()->getInset();
1289 inset->dispatch(view()->cursor(), FuncRequest(LFUN_INSET_DIALOG_SHOW));
1293 case LFUN_DIALOG_UPDATE: {
1294 string const & name = argument;
1295 // Can only update a dialog connected to an existing inset
1296 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1298 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
1299 inset->dispatch(view()->cursor(), fr);
1300 } else if (name == "paragraph") {
1301 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1306 case LFUN_DIALOG_HIDE:
1307 Dialogs::hide(argument, 0);
1310 case LFUN_DIALOG_DISCONNECT_INSET:
1311 owner->getDialogs().disconnect(argument);
1314 case LFUN_CHILDOPEN: {
1315 string const filename =
1316 MakeAbsPath(argument, owner->buffer()->filePath());
1317 setMessage(N_("Opening child document ") +
1318 MakeDisplayPath(filename) + "...");
1319 view()->savePosition(0);
1320 string const parentfilename = owner->buffer()->fileName();
1321 if (bufferlist.exists(filename))
1322 view()->buffer(bufferlist.getBuffer(filename));
1324 view()->loadLyXFile(filename);
1325 // Set the parent name of the child document.
1326 // This makes insertion of citations and references in the child work,
1327 // when the target is in the parent or another child document.
1328 owner->buffer()->setParentName(parentfilename);
1332 case LFUN_TOGGLECURSORFOLLOW:
1333 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1337 owner->getIntl().KeyMapOn(false);
1340 case LFUN_KMAP_PRIM:
1341 owner->getIntl().KeyMapPrim();
1345 owner->getIntl().KeyMapSec();
1348 case LFUN_KMAP_TOGGLE:
1349 owner->getIntl().ToggleKeyMap();
1355 argument = split(argument, countstr, ' ');
1356 istringstream is(countstr);
1359 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1360 for (int i = 0; i < count; ++i)
1361 dispatch(lyxaction.lookupFunc(argument));
1366 // argument contains ';'-terminated commands
1367 while (!argument.empty()) {
1369 argument = split(argument, first, ';');
1370 dispatch(lyxaction.lookupFunc(first));
1374 case LFUN_SAVEPREFERENCES: {
1375 Path p(user_lyxdir());
1376 lyxrc.write("preferences");
1380 case LFUN_SCREEN_FONT_UPDATE:
1381 // handle the screen font changes.
1382 lyxrc.set_font_norm_type();
1383 lyx_gui::update_fonts();
1384 // All visible buffers will need resize
1388 case LFUN_SET_COLOR: {
1390 string const x11_name = split(argument, lyx_name, ' ');
1391 if (lyx_name.empty() || x11_name.empty()) {
1392 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1397 bool const graphicsbg_changed =
1398 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1399 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1401 LColor::color col = lcolor.getFromLyXName(lyx_name);
1402 if (!lcolor.setColor(col, x11_name)) {
1404 bformat(_("Set-color \"%1$s\" failed "
1405 "- color is undefined or "
1406 "may not be redefined"), lyx_name));
1410 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1412 if (graphicsbg_changed) {
1413 #ifdef WITH_WARNINGS
1414 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1417 lyx::graphics::GCache::get().changeDisplay(true);
1424 owner->message(argument);
1427 case LFUN_FORKS_KILL:
1428 if (isStrInt(argument))
1429 ForkedcallsController::get().kill(strToInt(argument));
1432 case LFUN_TOOLTIPS_TOGGLE:
1433 owner->getDialogs().toggleTooltips();
1436 case LFUN_EXTERNAL_EDIT:
1437 InsetExternal().dispatch(view()->cursor(), FuncRequest(action, argument));
1441 view()->cursor().dispatch(cmd);
1446 view()->owner()->updateLayoutChoice();
1448 if (view()->available()) {
1449 view()->fitCursor();
1451 view()->cursor().updatePos();
1452 // if we executed a mutating lfun, mark the buffer as dirty
1453 if (!getStatus(cmd).disabled()
1454 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1455 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1456 view()->buffer()->markDirty();
1459 sendDispatchMessage(getMessage(), cmd, verbose);
1463 void LyXFunc::sendDispatchMessage(string const & msg,
1464 FuncRequest const & cmd, bool verbose)
1466 owner->updateMenubar();
1467 owner->updateToolbar();
1469 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1470 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1472 owner->message(msg);
1476 string dispatch_msg = msg;
1477 if (!dispatch_msg.empty())
1478 dispatch_msg += ' ';
1480 string comname = lyxaction.getActionName(cmd.action);
1482 bool argsadded = false;
1484 if (!cmd.argument.empty()) {
1485 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1486 comname += ' ' + cmd.argument;
1491 string const shortcuts = toplevel_keymap->findbinding(cmd);
1493 if (!shortcuts.empty()) {
1494 comname += ": " + shortcuts;
1495 } else if (!argsadded && !cmd.argument.empty()) {
1496 comname += ' ' + cmd.argument;
1499 if (!comname.empty()) {
1500 comname = rtrim(comname);
1501 dispatch_msg += '(' + comname + ')';
1504 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1505 if (!dispatch_msg.empty())
1506 owner->message(dispatch_msg);
1510 void LyXFunc::setupLocalKeymap()
1512 keyseq.stdmap = toplevel_keymap.get();
1513 keyseq.curmap = toplevel_keymap.get();
1514 cancel_meta_seq.stdmap = toplevel_keymap.get();
1515 cancel_meta_seq.curmap = toplevel_keymap.get();
1519 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1521 string initpath = lyxrc.document_path;
1522 string filename(name);
1524 if (view()->available()) {
1525 string const trypath = owner->buffer()->filePath();
1526 // If directory is writeable, use this as default.
1527 if (IsDirWriteable(trypath))
1531 static int newfile_number;
1533 if (filename.empty()) {
1534 filename = AddName(lyxrc.document_path,
1535 "newfile" + tostr(++newfile_number) + ".lyx");
1536 FileInfo fi(filename);
1537 while (bufferlist.exists(filename) || fi.readable()) {
1539 filename = AddName(lyxrc.document_path,
1540 "newfile" + tostr(newfile_number) +
1542 fi.newFile(filename);
1546 // The template stuff
1549 FileDialog fileDlg(_("Select template file"),
1550 LFUN_SELECT_FILE_SYNC,
1551 make_pair(string(_("Documents|#o#O")),
1552 string(lyxrc.document_path)),
1553 make_pair(string(_("Templates|#T#t")),
1554 string(lyxrc.template_path)));
1556 FileDialog::Result result =
1557 fileDlg.open(lyxrc.template_path,
1558 FileFilterList(_("LyX Documents (*.lyx)")),
1561 if (result.first == FileDialog::Later)
1563 if (result.second.empty())
1565 templname = result.second;
1568 view()->newFile(filename, templname, !name.empty());
1572 void LyXFunc::open(string const & fname)
1574 string initpath = lyxrc.document_path;
1576 if (view()->available()) {
1577 string const trypath = owner->buffer()->filePath();
1578 // If directory is writeable, use this as default.
1579 if (IsDirWriteable(trypath))
1585 if (fname.empty()) {
1586 FileDialog fileDlg(_("Select document to open"),
1588 make_pair(string(_("Documents|#o#O")),
1589 string(lyxrc.document_path)),
1590 make_pair(string(_("Examples|#E#e")),
1591 string(AddPath(system_lyxdir(), "examples"))));
1593 FileDialog::Result result =
1594 fileDlg.open(initpath,
1595 FileFilterList(_("LyX Documents (*.lyx)")),
1598 if (result.first == FileDialog::Later)
1601 filename = result.second;
1603 // check selected filename
1604 if (filename.empty()) {
1605 owner->message(_("Canceled."));
1611 // get absolute path of file and add ".lyx" to the filename if
1613 string const fullpath = FileSearch(string(), filename, "lyx");
1614 if (!fullpath.empty()) {
1615 filename = fullpath;
1618 string const disp_fn(MakeDisplayPath(filename));
1620 // if the file doesn't exist, let the user create one
1621 FileInfo const f(filename, true);
1623 // the user specifically chose this name. Believe them.
1624 view()->newFile(filename, "", true);
1628 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1631 if (view()->loadLyXFile(filename)) {
1632 str2 = bformat(_("Document %1$s opened."), disp_fn);
1634 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1636 owner->message(str2);
1640 void LyXFunc::doImport(string const & argument)
1643 string filename = split(argument, format, ' ');
1645 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1646 << " file: " << filename << endl;
1648 // need user interaction
1649 if (filename.empty()) {
1650 string initpath = lyxrc.document_path;
1652 if (view()->available()) {
1653 string const trypath = owner->buffer()->filePath();
1654 // If directory is writeable, use this as default.
1655 if (IsDirWriteable(trypath))
1659 string const text = bformat(_("Select %1$s file to import"),
1660 formats.prettyName(format));
1662 FileDialog fileDlg(text,
1664 make_pair(string(_("Documents|#o#O")),
1665 string(lyxrc.document_path)),
1666 make_pair(string(_("Examples|#E#e")),
1667 string(AddPath(system_lyxdir(), "examples"))));
1669 string const filter = formats.prettyName(format)
1670 + " (*." + formats.extension(format) + ')';
1672 FileDialog::Result result =
1673 fileDlg.open(initpath,
1674 FileFilterList(filter),
1677 if (result.first == FileDialog::Later)
1680 filename = result.second;
1682 // check selected filename
1683 if (filename.empty())
1684 owner->message(_("Canceled."));
1687 if (filename.empty())
1690 // get absolute path of file
1691 filename = MakeAbsPath(filename);
1693 string const lyxfile = ChangeExtension(filename, ".lyx");
1695 // Check if the document already is open
1696 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1697 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1698 owner->message(_("Canceled."));
1703 // if the file exists already, and we didn't do
1704 // -i lyx thefile.lyx, warn
1705 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1706 string const file = MakeDisplayPath(lyxfile, 30);
1708 string text = bformat(_("The document %1$s already exists.\n\n"
1709 "Do you want to over-write that document?"), file);
1710 int const ret = Alert::prompt(_("Over-write document?"),
1711 text, 0, 1, _("&Over-write"), _("&Cancel"));
1714 owner->message(_("Canceled."));
1719 Importer::Import(owner, filename, format);
1723 void LyXFunc::closeBuffer()
1725 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1726 if (bufferlist.empty()) {
1727 // need this otherwise SEGV may occur while
1728 // trying to set variables that don't exist
1729 // since there's no current buffer
1730 owner->getDialogs().hideBufferDependent();
1732 view()->buffer(bufferlist.first());
1738 // Each "owner" should have it's own message method. lyxview and
1739 // the minibuffer would use the minibuffer, but lyxserver would
1740 // send an ERROR signal to its client. Alejandro 970603
1741 // This function is bit problematic when it comes to NLS, to make the
1742 // lyx servers client be language indepenent we must not translate
1743 // strings sent to this func.
1744 void LyXFunc::setErrorMessage(string const & m) const
1746 dispatch_buffer = m;
1751 void LyXFunc::setMessage(string const & m) const
1753 dispatch_buffer = m;
1757 void LyXFunc::setStatusMessage(string const & m) const
1763 string const LyXFunc::view_status_message()
1765 // When meta-fake key is pressed, show the key sequence so far + "M-".
1767 return keyseq.print() + "M-";
1770 // Else, when a non-complete key sequence is pressed,
1771 // show the available options.
1772 if (keyseq.length() > 0 && !keyseq.deleted()) {
1773 return keyseq.printOptions();
1776 if (!view()->available())
1777 return _("Welcome to LyX!");
1779 return currentState(view());
1783 BufferView * LyXFunc::view() const
1785 BOOST_ASSERT(owner);
1786 return owner->view().get();
1790 bool LyXFunc::wasMetaKey() const
1792 return (meta_fake_bit != key_modifier::none);