3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "BranchList.h"
26 #include "buffer_funcs.h"
27 #include "bufferlist.h"
28 #include "bufferparams.h"
29 #include "BufferView.h"
32 #include "dispatchresult.h"
36 #include "funcrequest.h"
40 #include "iterators.h"
44 #include "LyXAction.h"
48 #include "lyxserver.h"
50 #include "paragraph.h"
51 #include "ParagraphParameters.h"
54 #include "insets/insetcommand.h"
55 #include "insets/insetexternal.h"
56 #include "insets/insettabular.h"
57 #include "insets/insetvspace.h"
59 #include "mathed/math_cursor.h"
61 #include "frontends/Alert.h"
62 #include "frontends/Dialogs.h"
63 #include "frontends/FileDialog.h"
64 #include "frontends/lyx_gui.h"
65 #include "frontends/LyXKeySym.h"
66 #include "frontends/LyXView.h"
67 #include "frontends/Menubar.h"
68 #include "frontends/Toolbar.h"
70 #include "support/FileInfo.h"
71 #include "support/filetools.h"
72 #include "support/forkedcontr.h"
73 #include "support/path.h"
74 #include "support/path_defines.h"
75 #include "support/tostr.h"
76 #include "support/std_sstream.h"
77 #include "support/os.h"
79 using bv_funcs::changeDepth;
80 using bv_funcs::currentState;
81 using bv_funcs::DEC_DEPTH;
82 using bv_funcs::freefont2string;
83 using bv_funcs::INC_DEPTH;
85 using lyx::support::AddName;
86 using lyx::support::AddPath;
87 using lyx::support::bformat;
88 using lyx::support::ChangeExtension;
89 using lyx::support::FileInfo;
90 using lyx::support::FileSearch;
91 using lyx::support::ForkedcallsController;
92 using lyx::support::i18nLibFileSearch;
93 using lyx::support::IsDirWriteable;
94 using lyx::support::IsFileReadable;
95 using lyx::support::isStrInt;
96 using lyx::support::MakeAbsPath;
97 using lyx::support::MakeDisplayPath;
98 using lyx::support::Path;
99 using lyx::support::rtrim;
100 using lyx::support::split;
101 using lyx::support::strToInt;
102 using lyx::support::strToUnsignedInt;
103 using lyx::support::system_lyxdir;
104 using lyx::support::token;
105 using lyx::support::trim;
106 using lyx::support::user_lyxdir;
107 using lyx::support::prefixIs;
108 using lyx::support::os::getTmpDir;
111 using std::make_pair;
114 using std::istringstream;
117 extern BufferList bufferlist;
118 extern LyXServer * lyxserver;
119 extern bool selection_possible;
121 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
124 extern tex_accent_struct get_accent(kb_action action);
127 LyXFunc::LyXFunc(LyXView * lv)
130 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
131 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
132 meta_fake_bit(key_modifier::none)
137 void LyXFunc::handleKeyFunc(kb_action action)
139 char c = encoded_last_key;
141 if (keyseq.length()) {
145 owner->getIntl().getTransManager()
146 .deadkey(c, get_accent(action).accent, view()->getLyXText());
147 // Need to clear, in case the minibuffer calls these
150 // copied verbatim from do_accent_char
152 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
156 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
157 key_modifier::state state)
161 if (lyxerr.debugging(Debug::KEY)) {
162 lyxerr << "KeySym is "
163 << keysym->getSymbolName()
167 // Do nothing if we have nothing (JMarc)
168 if (!keysym->isOK()) {
169 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
174 if (keysym->isModifier()) {
175 lyxerr[Debug::KEY] << "isModifier true" << endl;
179 Encoding const * encoding = view()->getEncoding();
181 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
183 // Do a one-deep top-level lookup for
184 // cancel and meta-fake keys. RVDK_PATCH_5
185 cancel_meta_seq.reset();
187 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
188 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
190 // When not cancel or meta-fake, do the normal lookup.
191 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
192 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
193 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
194 // remove Caps Lock and Mod2 as a modifiers
195 func = keyseq.addkey(keysym, (state | meta_fake_bit));
196 lyxerr[Debug::KEY] << "action now set to ["
197 << func.action << ']' << endl;
200 // Dont remove this unless you know what you are doing.
201 meta_fake_bit = key_modifier::none;
203 // can this happen now ?
204 if (func.action == LFUN_NOACTION) {
205 func = FuncRequest(LFUN_PREFIX);
208 if (lyxerr.debugging(Debug::KEY)) {
209 lyxerr << "Key [action="
210 << func.action << "]["
211 << keyseq.print() << ']'
215 // already here we know if it any point in going further
216 // why not return already here if action == -1 and
217 // num_bytes == 0? (Lgb)
219 if (keyseq.length() > 1) {
220 owner->message(keyseq.print());
224 // Maybe user can only reach the key via holding down shift.
225 // Let's see. But only if shift is the only modifier
226 if (func.action == LFUN_UNKNOWN_ACTION &&
227 state == key_modifier::shift) {
228 lyxerr[Debug::KEY] << "Trying without shift" << endl;
229 func = keyseq.addkey(keysym, key_modifier::none);
230 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
233 if (func.action == LFUN_UNKNOWN_ACTION) {
234 // Hmm, we didn't match any of the keysequences. See
235 // if it's normal insertable text not already covered
237 if (keysym->isText() && keyseq.length() == 1) {
238 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
239 func = FuncRequest(LFUN_SELFINSERT);
241 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
242 owner->message(_("Unknown function."));
247 if (func.action == LFUN_SELFINSERT) {
248 if (encoded_last_key != 0) {
250 arg += encoded_last_key;
252 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
254 lyxerr[Debug::KEY] << "SelfInsert arg[`"
255 << argument << "']" << endl;
263 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
266 Buffer * buf = owner->buffer();
268 if (ev.action == LFUN_NOACTION) {
269 setStatusMessage(N_("Nothing to do"));
275 case LFUN_UNKNOWN_ACTION:
276 #ifndef HAVE_LIBAIKSAURUS
277 case LFUN_THESAURUS_ENTRY:
283 flag |= lyx_gui::getStatus(ev);
286 if (flag.unknown()) {
287 setStatusMessage(N_("Unknown action"));
291 // the default error message if we disable the command
292 setStatusMessage(N_("Command disabled"));
294 // Check whether we need a buffer
295 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
296 // Yes we need a buffer, do we have one?
299 // Can we use a readonly buffer?
300 if (buf->isReadonly() &&
301 !lyxaction.funcHasFlag(ev.action,
302 LyXAction::ReadOnly)) {
304 setStatusMessage(N_("Document is read-only"));
309 setStatusMessage(N_("Command not allowed with"
310 "out any document open"));
316 UpdatableInset * tli = view()->cursor().innerInset();
317 InsetTabular * tab = view()->cursor().innerInsetTabular();
319 // I would really like to avoid having this switch and rather try to
320 // encode this in the function itself.
321 bool disable = false;
324 disable = ev.argument != "custom"
325 && !Exporter::IsExportable(*buf, ev.argument);
328 disable = buf->undostack().empty();
331 disable = buf->redostack().empty();
335 if (tab && tab->hasSelection())
338 disable = !mathcursor && !view()->getLyXText()->selection.set();
342 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
346 disable = !Exporter::IsExportable(*buf, "program");
349 case LFUN_LAYOUT_TABULAR:
350 disable = !view()->cursor().innerInsetTabular();
354 disable = !changeDepthAllowed(view(), view()->getLyXText(), DEC_DEPTH);
357 case LFUN_DEPTH_PLUS:
358 disable = !changeDepthAllowed(view(), view()->getLyXText(), INC_DEPTH);
362 case LFUN_LAYOUT_PARAGRAPH: {
363 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
364 disable = inset && inset->forceDefaultParagraphs(inset);
368 case LFUN_INSET_OPTARG:
369 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
372 case LFUN_TABULAR_FEATURE:
375 // FIXME: check temporarily disabled
377 char align = mathcursor->valign();
382 if (ev.argument.empty()) {
386 if (!contains("tcb", ev.argument[0])) {
390 flag.setOnOff(ev.argument[0] == align);
394 char align = mathcursor->halign();
399 if (ev.argument.empty()) {
403 if (!contains("lcr", ev.argument[0])) {
407 flag.setOnOff(ev.argument[0] == align);
409 disable = !mathcursor->halign();
415 //ret.disabled(true);
416 InsetTabular * tab = view()->cursor().innerInsetTabular();
418 ret = tab->getStatus(ev.argument);
425 static InsetTabular inset(*buf, 1, 1);
427 FuncStatus ret = inset.getStatus(ev.argument);
428 if (ret.onoff(true) || ret.onoff(false))
429 flag.setOnOff(false);
434 case LFUN_VC_REGISTER:
435 disable = buf->lyxvc().inUse();
437 case LFUN_VC_CHECKIN:
438 disable = !buf->lyxvc().inUse() || buf->isReadonly();
440 case LFUN_VC_CHECKOUT:
441 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
445 disable = !buf->lyxvc().inUse();
447 case LFUN_MENURELOAD:
448 disable = buf->isUnnamed() || buf->isClean();
450 case LFUN_BOOKMARK_GOTO:
452 isSavedPosition(strToUnsignedInt(ev.argument));
455 case LFUN_MERGE_CHANGES:
456 case LFUN_ACCEPT_CHANGE:
457 case LFUN_REJECT_CHANGE:
458 case LFUN_ACCEPT_ALL_CHANGES:
459 case LFUN_REJECT_ALL_CHANGES:
460 disable = !buf->params().tracking_changes;
463 case LFUN_INSET_SETTINGS: {
465 UpdatableInset * inset = view()->cursor().innerInset();
470 // jump back to owner if an InsetText, so
471 // we get back to the InsetTabular or whatever
472 if (inset->lyxCode() == InsetOld::TEXT_CODE)
473 inset = inset->owner();
475 InsetOld::Code code = inset->lyxCode();
477 case InsetOld::TABULAR_CODE:
478 disable = ev.argument != "tabular";
480 case InsetOld::ERT_CODE:
481 disable = ev.argument != "ert";
483 case InsetOld::FLOAT_CODE:
484 disable = ev.argument != "float";
486 case InsetOld::MINIPAGE_CODE:
487 disable = ev.argument != "minipage";
489 case InsetOld::WRAP_CODE:
490 disable = ev.argument != "wrap";
492 case InsetOld::NOTE_CODE:
493 disable = ev.argument != "note";
495 case InsetOld::BRANCH_CODE:
496 disable = ev.argument != "branch";
498 case InsetOld::BOX_CODE:
499 disable = ev.argument != "box";
507 case LFUN_MATH_MUTATE:
509 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
510 flag.setOnOff(false);
515 // we just need to be in math mode to enable that
517 case LFUN_MATH_SPACE:
518 case LFUN_MATH_LIMITS:
519 case LFUN_MATH_NONUMBER:
520 case LFUN_MATH_NUMBER:
521 case LFUN_MATH_EXTERN:
522 disable = !mathcursor;
525 case LFUN_DIALOG_SHOW: {
526 string const name = ev.getArg(0);
528 disable = !(name == "aboutlyx" ||
531 name == "preferences" ||
536 if (name == "print") {
537 disable = !Exporter::IsExportable(*buf, "dvi") ||
538 lyxrc.print_command == "none";
539 } else if (name == "character") {
540 UpdatableInset * tli = view()->cursor().innerInset();
541 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
542 } else if (name == "vclog") {
543 disable = !buf->lyxvc().inUse();
544 } else if (name == "latexlog") {
545 disable = !IsFileReadable(buf->getLogName().second);
554 // the functions which insert insets
555 InsetOld::Code code = InsetOld::NO_CODE;
557 case LFUN_DIALOG_SHOW_NEW_INSET:
558 if (ev.argument == "bibitem")
559 code = InsetOld::BIBITEM_CODE;
560 else if (ev.argument == "bibtex")
561 code = InsetOld::BIBTEX_CODE;
562 else if (ev.argument == "citation")
563 code = InsetOld::CITE_CODE;
564 else if (ev.argument == "ert")
565 code = InsetOld::ERT_CODE;
566 else if (ev.argument == "external")
567 code = InsetOld::EXTERNAL_CODE;
568 else if (ev.argument == "float")
569 code = InsetOld::FLOAT_CODE;
570 else if (ev.argument == "graphics")
571 code = InsetOld::GRAPHICS_CODE;
572 else if (ev.argument == "include")
573 code = InsetOld::INCLUDE_CODE;
574 else if (ev.argument == "index")
575 code = InsetOld::INDEX_CODE;
576 else if (ev.argument == "label")
577 code = InsetOld::LABEL_CODE;
578 else if (ev.argument == "minipage")
579 code = InsetOld::MINIPAGE_CODE;
580 else if (ev.argument == "ref")
581 code = InsetOld::REF_CODE;
582 else if (ev.argument == "toc")
583 code = InsetOld::TOC_CODE;
584 else if (ev.argument == "url")
585 code = InsetOld::URL_CODE;
586 else if (ev.argument == "vspace")
587 code = InsetOld::VSPACE_CODE;
588 else if (ev.argument == "wrap")
589 code = InsetOld::WRAP_CODE;
593 code = InsetOld::ERT_CODE;
595 case LFUN_INSET_FOOTNOTE:
596 code = InsetOld::FOOT_CODE;
598 case LFUN_TABULAR_INSERT:
599 code = InsetOld::TABULAR_CODE;
601 case LFUN_INSET_MARGINAL:
602 code = InsetOld::MARGIN_CODE;
604 case LFUN_INSET_MINIPAGE:
605 code = InsetOld::MINIPAGE_CODE;
607 case LFUN_INSET_FLOAT:
608 case LFUN_INSET_WIDE_FLOAT:
609 code = InsetOld::FLOAT_CODE;
611 case LFUN_INSET_WRAP:
612 code = InsetOld::WRAP_CODE;
614 case LFUN_FLOAT_LIST:
615 code = InsetOld::FLOAT_LIST_CODE;
618 case LFUN_INSET_LIST:
619 code = InsetOld::LIST_CODE;
621 case LFUN_INSET_THEOREM:
622 code = InsetOld::THEOREM_CODE;
625 case LFUN_INSET_CAPTION:
626 code = InsetOld::CAPTION_CODE;
628 case LFUN_INSERT_NOTE:
629 code = InsetOld::NOTE_CODE;
631 case LFUN_INSERT_CHARSTYLE:
632 code = InsetOld::CHARSTYLE_CODE;
633 if (buf->params().getLyXTextClass().charstyles().empty())
636 case LFUN_INSERT_BOX:
637 code = InsetOld::BOX_CODE;
639 case LFUN_INSERT_BRANCH:
640 code = InsetOld::BRANCH_CODE;
641 if (buf->params().branchlist().empty())
644 case LFUN_INSERT_LABEL:
645 code = InsetOld::LABEL_CODE;
647 case LFUN_INSET_OPTARG:
648 code = InsetOld::OPTARG_CODE;
650 case LFUN_ENVIRONMENT_INSERT:
651 code = InsetOld::MINIPAGE_CODE;
653 case LFUN_INDEX_INSERT:
654 code = InsetOld::INDEX_CODE;
656 case LFUN_INDEX_PRINT:
657 code = InsetOld::INDEX_PRINT_CODE;
659 case LFUN_TOC_INSERT:
660 code = InsetOld::TOC_CODE;
664 code = InsetOld::URL_CODE;
667 // always allow this, since we will inset a raw quote
668 // if an inset is not allowed.
670 case LFUN_HYPHENATION:
671 case LFUN_LIGATURE_BREAK:
673 case LFUN_MENU_SEPARATOR:
675 case LFUN_END_OF_SENTENCE:
676 code = InsetOld::SPECIALCHAR_CODE;
678 case LFUN_SPACE_INSERT:
679 // slight hack: we know this is allowed in math mode
681 code = InsetOld::SPACE_CODE;
683 case LFUN_INSET_DIALOG_SHOW: {
684 InsetOld * inset = view()->getLyXText()->getInset();
687 code = inset->lyxCode();
688 if (!(code == InsetOld::INCLUDE_CODE
689 || code == InsetOld::BIBTEX_CODE
690 || code == InsetOld::FLOAT_LIST_CODE
691 || code == InsetOld::TOC_CODE))
699 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
705 // A few general toggles
707 case LFUN_TOOLTIPS_TOGGLE:
708 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
711 case LFUN_READ_ONLY_TOGGLE:
712 flag.setOnOff(buf->isReadonly());
715 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
717 case LFUN_SWITCHBUFFER:
718 // toggle on the current buffer, but do not toggle off
719 // the other ones (is that a good idea?)
720 if (ev.argument == buf->fileName())
723 case LFUN_TRACK_CHANGES:
724 flag.setOnOff(buf->params().tracking_changes);
731 // the font related toggles
733 LyXFont const & font = view()->getLyXText()->real_current_font;
736 flag.setOnOff(font.emph() == LyXFont::ON);
739 flag.setOnOff(font.noun() == LyXFont::ON);
742 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
745 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
748 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
751 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
757 string tc = mathcursor->getLastCode();
760 flag.setOnOff(tc == "mathbf");
763 flag.setOnOff(tc == "mathsf");
766 flag.setOnOff(tc == "mathcal");
769 flag.setOnOff(tc == "mathrm");
772 flag.setOnOff(tc == "mathtt");
775 flag.setOnOff(tc == "mathbb");
778 flag.setOnOff(tc == "mathnormal");
786 // this one is difficult to get right. As a half-baked
787 // solution, we consider only the first action of the sequence
788 if (ev.action == LFUN_SEQUENCE) {
789 // argument contains ';'-terminated commands
790 #warning LyXAction arguments not handled here.
791 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
800 bool ensureBufferClean(BufferView * bv)
802 Buffer & buf = *bv->buffer();
806 string const file = MakeDisplayPath(buf.fileName(), 30);
807 string text = bformat(_("The document %1$s has unsaved "
808 "changes.\n\nDo you want to save "
809 "the document?"), file);
810 int const ret = Alert::prompt(_("Save changed document?"),
811 text, 0, 1, _("&Save"),
815 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
817 return buf.isClean();
823 void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
825 string argument = func.argument;
826 kb_action action = func.action;
828 //lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << func << endl;
829 lyxerr << "LyXFunc::dispatch: cmd: " << func << endl;
831 // we have not done anything wrong yet.
833 dispatch_buffer.erase();
835 #ifdef NEW_DISPATCHER
836 // We try do call the most specific dispatcher first:
837 // 1. the lockinginset's dispatch
838 // 2. the bufferview's dispatch
839 // 3. the lyxview's dispatch
842 selection_possible = false;
844 // We cannot use this function here
845 if (getStatus(func).disabled()) {
846 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
847 << lyxaction.getActionName(action)
848 << " [" << action << "] is disabled at this location"
850 setErrorMessage(getStatusMessage());
854 if (view()->available())
855 view()->hideCursor();
860 if (!view()->available())
862 view()->cursor().pop();
863 // Tell the paragraph dialog that we changed paragraph
864 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
868 case LFUN_WORDFINDFORWARD:
869 case LFUN_WORDFINDBACKWARD: {
870 static string last_search;
871 string searched_string;
873 if (!argument.empty()) {
874 last_search = argument;
875 searched_string = argument;
877 searched_string = last_search;
879 bool fw = (action == LFUN_WORDFINDFORWARD);
880 if (!searched_string.empty())
881 lyx::find::find(view(), searched_string,
887 owner->message(keyseq.printOptions());
890 case LFUN_EXEC_COMMAND:
891 owner->focus_command_buffer();
896 meta_fake_bit = key_modifier::none;
897 if (view()->available())
898 // cancel any selection
899 dispatch(FuncRequest(LFUN_MARK_OFF));
900 setMessage(N_("Cancel"));
904 meta_fake_bit = key_modifier::alt;
905 setMessage(keyseq.print());
908 case LFUN_READ_ONLY_TOGGLE:
909 if (owner->buffer()->lyxvc().inUse())
910 owner->buffer()->lyxvc().toggleReadOnly();
912 owner->buffer()->setReadonly(
913 !owner->buffer()->isReadonly());
916 case LFUN_CENTER: // this is center and redraw.
920 // --- Menus -----------------------------------------------
922 menuNew(argument, false);
925 case LFUN_MENUNEWTMPLT:
926 menuNew(argument, true);
929 case LFUN_CLOSEBUFFER:
934 if (!owner->buffer()->isUnnamed()) {
935 string const str = bformat(_("Saving document %1$s..."),
936 MakeDisplayPath(owner->buffer()->fileName()));
938 MenuWrite(owner->buffer());
939 owner->message(str + _(" done."));
941 WriteAs(owner->buffer());
945 WriteAs(owner->buffer(), argument);
948 case LFUN_MENURELOAD: {
949 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
950 string text = bformat(_("Any changes will be lost. Are you sure "
951 "you want to revert to the saved version of the document %1$s?"), file);
952 int const ret = Alert::prompt(_("Revert to saved document?"),
953 text, 0, 1, _("&Revert"), _("&Cancel"));
961 Exporter::Export(owner->buffer(), argument, true);
962 view()->showErrorList(BufferFormat(*owner->buffer()));
966 Exporter::Preview(owner->buffer(), argument);
967 view()->showErrorList(BufferFormat(*owner->buffer()));
971 Exporter::Export(owner->buffer(), "program", true);
972 view()->showErrorList(_("Build"));
976 owner->buffer()->runChktex();
977 view()->showErrorList(_("ChkTeX"));
981 if (argument == "custom")
982 owner->getDialogs().showSendto();
984 Exporter::Export(owner->buffer(), argument, false);
985 view()->showErrorList(BufferFormat(*owner->buffer()));
998 InsetCommandParams p("tableofcontents");
999 string const data = InsetCommandMailer::params2string("toc", p);
1000 owner->getDialogs().show("toc", data, 0);
1016 case LFUN_RECONFIGURE:
1017 Reconfigure(view());
1020 case LFUN_HELP_OPEN: {
1021 string const arg = argument;
1023 setErrorMessage(N_("Missing argument"));
1026 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1027 if (fname.empty()) {
1028 lyxerr << "LyX: unable to find documentation file `"
1029 << arg << "'. Bad installation?" << endl;
1032 owner->message(bformat(_("Opening help file %1$s..."),
1033 MakeDisplayPath(fname)));
1034 view()->loadLyXFile(fname, false);
1038 // --- version control -------------------------------
1039 case LFUN_VC_REGISTER:
1040 if (!ensureBufferClean(view()))
1042 if (!owner->buffer()->lyxvc().inUse()) {
1043 owner->buffer()->lyxvc().registrer();
1048 case LFUN_VC_CHECKIN:
1049 if (!ensureBufferClean(view()))
1051 if (owner->buffer()->lyxvc().inUse()
1052 && !owner->buffer()->isReadonly()) {
1053 owner->buffer()->lyxvc().checkIn();
1058 case LFUN_VC_CHECKOUT:
1059 if (!ensureBufferClean(view()))
1061 if (owner->buffer()->lyxvc().inUse()
1062 && owner->buffer()->isReadonly()) {
1063 owner->buffer()->lyxvc().checkOut();
1068 case LFUN_VC_REVERT:
1069 owner->buffer()->lyxvc().revert();
1074 owner->buffer()->lyxvc().undoLast();
1078 // --- buffers ----------------------------------------
1079 case LFUN_SWITCHBUFFER:
1080 view()->buffer(bufferlist.getBuffer(argument));
1084 NewFile(view(), argument);
1087 case LFUN_FILE_OPEN:
1091 case LFUN_LAYOUT_TABULAR:
1092 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1093 tab->openLayoutDialog(view());
1096 case LFUN_DROP_LAYOUTS_CHOICE:
1097 owner->getToolbar().openLayoutList();
1100 case LFUN_MENU_OPEN_BY_NAME:
1101 owner->getMenubar().openByName(argument);
1104 // --- lyxserver commands ----------------------------
1106 setMessage(owner->buffer()->fileName());
1107 lyxerr[Debug::INFO] << "FNAME["
1108 << owner->buffer()->fileName()
1113 dispatch_buffer = keyseq.print();
1114 lyxserver->notifyClient(dispatch_buffer);
1117 case LFUN_GOTOFILEROW: {
1120 istringstream is(argument);
1121 is >> file_name >> row;
1122 if (prefixIs(file_name, getTmpDir())) {
1123 // Needed by inverse dvi search. If it is a file
1124 // in tmpdir, call the apropriated function
1125 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1127 // Must replace extension of the file to be .lyx
1128 // and get full path
1129 string const s = ChangeExtension(file_name, ".lyx");
1130 // Either change buffer or load the file
1131 if (bufferlist.exists(s)) {
1132 view()->buffer(bufferlist.getBuffer(s));
1134 view()->loadLyXFile(s);
1138 view()->setCursorFromRow(row);
1141 // see BufferView_pimpl::center()
1142 view()->updateScrollbar();
1146 case LFUN_GOTO_PARAGRAPH: {
1147 istringstream is(argument);
1150 ParIterator par = owner->buffer()->getParFromID(id);
1151 if (par == owner->buffer()->par_iterator_end()) {
1152 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1153 << id << ']' << endl;
1156 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1157 << " found." << endl;
1160 par.lockPath(view());
1161 LyXText * lt = par.text(*view()->buffer());
1164 lt->setCursor(par.pit(), 0);
1165 view()->switchKeyMap();
1166 owner->view_state_changed();
1169 // see BufferView_pimpl::center()
1170 view()->updateScrollbar();
1174 // --- Mathed stuff. If we are here, there is no locked inset yet.
1175 case LFUN_MATH_EXTERN:
1176 case LFUN_MATH_NUMBER:
1177 case LFUN_MATH_NONUMBER:
1178 case LFUN_MATH_LIMITS:
1179 setErrorMessage(N_("This is only allowed in math mode!"));
1182 // passthrough hat and underscore outside mathed:
1183 case LFUN_SUBSCRIPT:
1184 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1187 case LFUN_SUPERSCRIPT:
1188 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1191 case LFUN_DIALOG_SHOW: {
1192 string const name = func.getArg(0);
1193 string data = trim(func.argument.substr(name.size()));
1195 if (name == "character") {
1196 data = freefont2string();
1198 owner->getDialogs().show("character", data);
1200 else if (name == "document")
1201 owner->getDialogs().showDocument();
1202 else if (name == "findreplace")
1203 owner->getDialogs().showSearch();
1204 else if (name == "forks")
1205 owner->getDialogs().showForks();
1206 else if (name == "preamble")
1207 owner->getDialogs().showPreamble();
1208 else if (name == "preferences")
1209 owner->getDialogs().showPreferences();
1210 else if (name == "print")
1211 owner->getDialogs().showPrint();
1212 else if (name == "spellchecker")
1213 owner->getDialogs().showSpellchecker();
1215 else if (name == "latexlog") {
1216 pair<Buffer::LogType, string> const logfile =
1217 owner->buffer()->getLogName();
1218 switch (logfile.first) {
1219 case Buffer::latexlog:
1222 case Buffer::buildlog:
1226 data += logfile.second;
1227 owner->getDialogs().show("log", data);
1229 else if (name == "vclog") {
1230 string const data = "vc " +
1231 owner->buffer()->lyxvc().getLogFile();
1232 owner->getDialogs().show("log", data);
1235 owner->getDialogs().show(name, data);
1239 case LFUN_DIALOG_SHOW_NEW_INSET: {
1240 string const & name = argument;
1242 if (name == "bibitem" ||
1244 name == "include" ||
1249 InsetCommandParams p(name);
1250 data = InsetCommandMailer::params2string(name, p);
1251 } else if (name == "citation") {
1252 InsetCommandParams p("cite");
1253 data = InsetCommandMailer::params2string(name, p);
1254 } else if (name == "vspace") {
1256 data = InsetVSpaceMailer::params2string(space);
1258 owner->getDialogs().show(name, data, 0);
1262 case LFUN_DIALOG_SHOW_NEXT_INSET:
1265 case LFUN_INSET_DIALOG_SHOW: {
1266 InsetOld * inset = view()->getLyXText()->getInset();
1268 inset->dispatch(FuncRequest(view(), LFUN_INSET_DIALOG_SHOW));
1272 case LFUN_DIALOG_UPDATE: {
1273 string const & name = argument;
1274 // Can only update a dialog connected to an existing inset
1275 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1277 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1279 inset->dispatch(fr);
1280 } else if (name == "paragraph") {
1281 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1286 case LFUN_DIALOG_HIDE:
1287 Dialogs::hide(argument, 0);
1290 case LFUN_DIALOG_DISCONNECT_INSET:
1291 owner->getDialogs().disconnect(argument);
1294 case LFUN_CHILDOPEN: {
1295 string const filename =
1296 MakeAbsPath(argument, owner->buffer()->filePath());
1297 setMessage(N_("Opening child document ") +
1298 MakeDisplayPath(filename) + "...");
1299 view()->savePosition(0);
1300 string const parentfilename = owner->buffer()->fileName();
1301 if (bufferlist.exists(filename))
1302 view()->buffer(bufferlist.getBuffer(filename));
1304 view()->loadLyXFile(filename);
1305 // Set the parent name of the child document.
1306 // This makes insertion of citations and references in the child work,
1307 // when the target is in the parent or another child document.
1308 owner->buffer()->setParentName(parentfilename);
1312 case LFUN_TOGGLECURSORFOLLOW:
1313 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1317 owner->getIntl().KeyMapOn(false);
1320 case LFUN_KMAP_PRIM:
1321 owner->getIntl().KeyMapPrim();
1325 owner->getIntl().KeyMapSec();
1328 case LFUN_KMAP_TOGGLE:
1329 owner->getIntl().ToggleKeyMap();
1335 argument = split(argument, countstr, ' ');
1336 istringstream is(countstr);
1339 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1340 for (int i = 0; i < count; ++i)
1341 dispatch(lyxaction.lookupFunc(argument));
1346 // argument contains ';'-terminated commands
1347 while (!argument.empty()) {
1349 argument = split(argument, first, ';');
1350 dispatch(lyxaction.lookupFunc(first));
1354 case LFUN_SAVEPREFERENCES: {
1355 Path p(user_lyxdir());
1356 lyxrc.write("preferences");
1360 case LFUN_SCREEN_FONT_UPDATE:
1361 // handle the screen font changes.
1362 lyxrc.set_font_norm_type();
1363 lyx_gui::update_fonts();
1364 // All visible buffers will need resize
1368 case LFUN_SET_COLOR: {
1370 string const x11_name = split(argument, lyx_name, ' ');
1371 if (lyx_name.empty() || x11_name.empty()) {
1372 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1377 bool const graphicsbg_changed =
1378 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1379 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1381 if (!lcolor.setColor(lyx_name, x11_name)) {
1383 bformat(_("Set-color \"%1$s\" failed "
1384 "- color is undefined or "
1385 "may not be redefined"), lyx_name));
1389 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1391 if (graphicsbg_changed) {
1392 #ifdef WITH_WARNINGS
1393 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1396 lyx::graphics::GCache::get().changeDisplay(true);
1403 owner->message(argument);
1406 case LFUN_FORKS_KILL:
1407 if (isStrInt(argument))
1408 ForkedcallsController::get().kill(strToInt(argument));
1411 case LFUN_TOOLTIPS_TOGGLE:
1412 owner->getDialogs().toggleTooltips();
1415 case LFUN_EXTERNAL_EDIT:
1416 InsetExternal().dispatch(FuncRequest(view(), action, argument));
1420 DispatchResult result =
1421 view()->cursor().dispatch(FuncRequest(func, view()));
1422 if (result.dispatched())
1423 lyxerr << "dispatched by Cursor::dispatch()" << endl;
1425 lyxerr << "### NOT DISPATCHED BY Cursor::dispatch() ###" << endl;
1431 view()->owner()->updateLayoutChoice();
1433 if (view()->available()) {
1434 view()->fitCursor();
1436 view()->cursor().updatePos();
1437 // if we executed a mutating lfun, mark the buffer as dirty
1438 if (!getStatus(func).disabled()
1439 && !lyxaction.funcHasFlag(func.action, LyXAction::NoBuffer)
1440 && !lyxaction.funcHasFlag(func.action, LyXAction::ReadOnly))
1441 view()->buffer()->markDirty();
1444 sendDispatchMessage(getMessage(), func, verbose);
1448 void LyXFunc::sendDispatchMessage(string const & msg,
1449 FuncRequest const & func, bool verbose)
1451 owner->updateMenubar();
1452 owner->updateToolbar();
1454 if (func.action == LFUN_SELFINSERT || !verbose) {
1455 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1457 owner->message(msg);
1461 string dispatch_msg = msg;
1462 if (!dispatch_msg.empty())
1463 dispatch_msg += ' ';
1465 string comname = lyxaction.getActionName(func.action);
1467 bool argsadded = false;
1469 if (!func.argument.empty()) {
1470 if (func.action != LFUN_UNKNOWN_ACTION) {
1471 comname += ' ' + func.argument;
1476 string const shortcuts = toplevel_keymap->findbinding(func);
1478 if (!shortcuts.empty()) {
1479 comname += ": " + shortcuts;
1480 } else if (!argsadded && !func.argument.empty()) {
1481 comname += ' ' + func.argument;
1484 if (!comname.empty()) {
1485 comname = rtrim(comname);
1486 dispatch_msg += '(' + comname + ')';
1489 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1490 if (!dispatch_msg.empty())
1491 owner->message(dispatch_msg);
1495 void LyXFunc::setupLocalKeymap()
1497 keyseq.stdmap = toplevel_keymap.get();
1498 keyseq.curmap = toplevel_keymap.get();
1499 cancel_meta_seq.stdmap = toplevel_keymap.get();
1500 cancel_meta_seq.curmap = toplevel_keymap.get();
1504 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1506 string initpath = lyxrc.document_path;
1507 string filename(name);
1509 if (view()->available()) {
1510 string const trypath = owner->buffer()->filePath();
1511 // If directory is writeable, use this as default.
1512 if (IsDirWriteable(trypath))
1516 static int newfile_number;
1518 if (filename.empty()) {
1519 filename = AddName(lyxrc.document_path,
1520 "newfile" + tostr(++newfile_number) + ".lyx");
1521 FileInfo fi(filename);
1522 while (bufferlist.exists(filename) || fi.readable()) {
1524 filename = AddName(lyxrc.document_path,
1525 "newfile" + tostr(newfile_number) +
1527 fi.newFile(filename);
1531 // The template stuff
1534 FileDialog fileDlg(_("Select template file"),
1535 LFUN_SELECT_FILE_SYNC,
1536 make_pair(string(_("Documents|#o#O")),
1537 string(lyxrc.document_path)),
1538 make_pair(string(_("Templates|#T#t")),
1539 string(lyxrc.template_path)));
1541 FileDialog::Result result =
1542 fileDlg.open(lyxrc.template_path,
1543 _("*.lyx| LyX Documents (*.lyx)"));
1545 if (result.first == FileDialog::Later)
1547 if (result.second.empty())
1549 templname = result.second;
1552 view()->newFile(filename, templname, !name.empty());
1556 void LyXFunc::open(string const & fname)
1558 string initpath = lyxrc.document_path;
1560 if (view()->available()) {
1561 string const trypath = owner->buffer()->filePath();
1562 // If directory is writeable, use this as default.
1563 if (IsDirWriteable(trypath))
1569 if (fname.empty()) {
1570 FileDialog fileDlg(_("Select document to open"),
1572 make_pair(string(_("Documents|#o#O")),
1573 string(lyxrc.document_path)),
1574 make_pair(string(_("Examples|#E#e")),
1575 string(AddPath(system_lyxdir(), "examples"))));
1577 FileDialog::Result result =
1578 fileDlg.open(initpath,
1579 _("*.lyx| LyX Documents (*.lyx)"));
1581 if (result.first == FileDialog::Later)
1584 filename = result.second;
1586 // check selected filename
1587 if (filename.empty()) {
1588 owner->message(_("Canceled."));
1594 // get absolute path of file and add ".lyx" to the filename if
1596 string const fullpath = FileSearch(string(), filename, "lyx");
1597 if (!fullpath.empty()) {
1598 filename = fullpath;
1601 string const disp_fn(MakeDisplayPath(filename));
1603 // if the file doesn't exist, let the user create one
1604 FileInfo const f(filename, true);
1606 // the user specifically chose this name. Believe them.
1607 view()->newFile(filename, "", true);
1611 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1614 if (view()->loadLyXFile(filename)) {
1615 str2 = bformat(_("Document %1$s opened."), disp_fn);
1617 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1619 owner->message(str2);
1623 void LyXFunc::doImport(string const & argument)
1626 string filename = split(argument, format, ' ');
1628 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1629 << " file: " << filename << endl;
1631 // need user interaction
1632 if (filename.empty()) {
1633 string initpath = lyxrc.document_path;
1635 if (view()->available()) {
1636 string const trypath = owner->buffer()->filePath();
1637 // If directory is writeable, use this as default.
1638 if (IsDirWriteable(trypath))
1642 string const text = bformat(_("Select %1$s file to import"),
1643 formats.prettyName(format));
1645 FileDialog fileDlg(text,
1647 make_pair(string(_("Documents|#o#O")),
1648 string(lyxrc.document_path)),
1649 make_pair(string(_("Examples|#E#e")),
1650 string(AddPath(system_lyxdir(), "examples"))));
1652 string const extension = "*." + formats.extension(format)
1653 + "| " + formats.prettyName(format)
1654 + " (*." + formats.extension(format) + ')';
1656 FileDialog::Result result = fileDlg.open(initpath,
1659 if (result.first == FileDialog::Later)
1662 filename = result.second;
1664 // check selected filename
1665 if (filename.empty())
1666 owner->message(_("Canceled."));
1669 if (filename.empty())
1672 // get absolute path of file
1673 filename = MakeAbsPath(filename);
1675 string const lyxfile = ChangeExtension(filename, ".lyx");
1677 // Check if the document already is open
1678 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1679 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1680 owner->message(_("Canceled."));
1685 // if the file exists already, and we didn't do
1686 // -i lyx thefile.lyx, warn
1687 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1688 string const file = MakeDisplayPath(lyxfile, 30);
1690 string text = bformat(_("The document %1$s already exists.\n\n"
1691 "Do you want to over-write that document?"), file);
1692 int const ret = Alert::prompt(_("Over-write document?"),
1693 text, 0, 1, _("&Over-write"), _("&Cancel"));
1696 owner->message(_("Canceled."));
1701 Importer::Import(owner, filename, format);
1705 void LyXFunc::closeBuffer()
1707 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1708 if (bufferlist.empty()) {
1709 // need this otherwise SEGV may occur while
1710 // trying to set variables that don't exist
1711 // since there's no current buffer
1712 owner->getDialogs().hideBufferDependent();
1714 view()->buffer(bufferlist.first());
1720 // Each "owner" should have it's own message method. lyxview and
1721 // the minibuffer would use the minibuffer, but lyxserver would
1722 // send an ERROR signal to its client. Alejandro 970603
1723 // This func is bit problematic when it comes to NLS, to make the
1724 // lyx servers client be language indepenent we must not translate
1725 // strings sent to this func.
1726 void LyXFunc::setErrorMessage(string const & m) const
1728 dispatch_buffer = m;
1733 void LyXFunc::setMessage(string const & m) const
1735 dispatch_buffer = m;
1739 void LyXFunc::setStatusMessage(string const & m) const
1745 string const LyXFunc::view_status_message()
1747 // When meta-fake key is pressed, show the key sequence so far + "M-".
1749 return keyseq.print() + "M-";
1752 // Else, when a non-complete key sequence is pressed,
1753 // show the available options.
1754 if (keyseq.length() > 0 && !keyseq.deleted()) {
1755 return keyseq.printOptions();
1758 if (!view()->available())
1759 return _("Welcome to LyX!");
1761 return currentState(view());
1765 BufferView * LyXFunc::view() const
1767 BOOST_ASSERT(owner);
1768 return owner->view().get();
1772 bool LyXFunc::wasMetaKey() const
1774 return (meta_fake_bit != key_modifier::none);