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::DEC_DEPTH;
86 using bv_funcs::freefont2string;
87 using bv_funcs::INC_DEPTH;
89 using lyx::support::AddName;
90 using lyx::support::AddPath;
91 using lyx::support::bformat;
92 using lyx::support::ChangeExtension;
93 using lyx::support::FileFilterList;
94 using lyx::support::FileInfo;
95 using lyx::support::FileSearch;
96 using lyx::support::ForkedcallsController;
97 using lyx::support::i18nLibFileSearch;
98 using lyx::support::IsDirWriteable;
99 using lyx::support::IsFileReadable;
100 using lyx::support::isStrInt;
101 using lyx::support::MakeAbsPath;
102 using lyx::support::MakeDisplayPath;
103 using lyx::support::Path;
104 using lyx::support::rtrim;
105 using lyx::support::split;
106 using lyx::support::strToInt;
107 using lyx::support::strToUnsignedInt;
108 using lyx::support::system_lyxdir;
109 using lyx::support::token;
110 using lyx::support::trim;
111 using lyx::support::user_lyxdir;
112 using lyx::support::prefixIs;
113 using lyx::support::os::getTmpDir;
116 using std::make_pair;
119 using std::istringstream;
122 extern BufferList bufferlist;
123 extern LyXServer * lyxserver;
124 extern bool selection_possible;
126 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
129 extern tex_accent_struct get_accent(kb_action action);
132 LyXFunc::LyXFunc(LyXView * lv)
135 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
136 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
137 meta_fake_bit(key_modifier::none)
142 void LyXFunc::handleKeyFunc(kb_action action)
144 char c = encoded_last_key;
146 if (keyseq.length()) {
150 owner->getIntl().getTransManager()
151 .deadkey(c, get_accent(action).accent, view()->getLyXText());
152 // Need to clear, in case the minibuffer calls these
155 // copied verbatim from do_accent_char
156 view()->cursor().resetAnchor();
161 void LyXFunc::processKeySym(LyXKeySymPtr keysym, key_modifier::state state)
163 lyxerr[Debug::KEY] << "KeySym is " << keysym->getSymbolName() << endl;
165 // Do nothing if we have nothing (JMarc)
166 if (!keysym->isOK()) {
167 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
172 if (keysym->isModifier()) {
173 lyxerr[Debug::KEY] << "isModifier true" << endl;
177 Encoding const * encoding = view()->getEncoding();
179 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
181 // Do a one-deep top-level lookup for
182 // cancel and meta-fake keys. RVDK_PATCH_5
183 cancel_meta_seq.reset();
185 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
186 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
188 // When not cancel or meta-fake, do the normal lookup.
189 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
190 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
191 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
192 // remove Caps Lock and Mod2 as a modifiers
193 func = keyseq.addkey(keysym, (state | meta_fake_bit));
194 lyxerr[Debug::KEY] << "action now set to ["
195 << func.action << ']' << endl;
198 // Dont remove this unless you know what you are doing.
199 meta_fake_bit = key_modifier::none;
201 // can this happen now ?
202 if (func.action == LFUN_NOACTION) {
203 func = FuncRequest(LFUN_PREFIX);
206 if (lyxerr.debugging(Debug::KEY)) {
207 lyxerr << "Key [action="
208 << func.action << "]["
209 << keyseq.print() << ']'
213 // already here we know if it any point in going further
214 // why not return already here if action == -1 and
215 // num_bytes == 0? (Lgb)
217 if (keyseq.length() > 1) {
218 owner->message(keyseq.print());
222 // Maybe user can only reach the key via holding down shift.
223 // Let's see. But only if shift is the only modifier
224 if (func.action == LFUN_UNKNOWN_ACTION &&
225 state == key_modifier::shift) {
226 lyxerr[Debug::KEY] << "Trying without shift" << endl;
227 func = keyseq.addkey(keysym, key_modifier::none);
228 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
231 if (func.action == LFUN_UNKNOWN_ACTION) {
232 // Hmm, we didn't match any of the keysequences. See
233 // if it's normal insertable text not already covered
235 if (keysym->isText() && keyseq.length() == 1) {
236 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
237 func = FuncRequest(LFUN_SELFINSERT);
239 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
240 owner->message(_("Unknown function."));
245 if (func.action == LFUN_SELFINSERT) {
246 if (encoded_last_key != 0) {
247 string arg(1, encoded_last_key);
248 dispatch(FuncRequest(LFUN_SELFINSERT, arg));
250 << "SelfInsert arg[`" << arg << "']" << endl;
258 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
261 Buffer * buf = owner->buffer();
262 LCursor & cur = view()->cursor();
264 if (cmd.action == LFUN_NOACTION) {
265 setStatusMessage(N_("Nothing to do"));
270 switch (cmd.action) {
271 case LFUN_UNKNOWN_ACTION:
272 #ifndef HAVE_LIBAIKSAURUS
273 case LFUN_THESAURUS_ENTRY:
279 flag |= lyx_gui::getStatus(cmd);
282 if (flag.unknown()) {
283 setStatusMessage(N_("Unknown action"));
287 // the default error message if we disable the command
288 setStatusMessage(N_("Command disabled"));
290 // Check whether we need a buffer
291 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
292 // Yes we need a buffer, do we have one?
295 // Can we use a readonly buffer?
296 if (buf->isReadonly() &&
297 !lyxaction.funcHasFlag(cmd.action,
298 LyXAction::ReadOnly)) {
300 setStatusMessage(N_("Document is read-only"));
305 setStatusMessage(N_("Command not allowed with"
306 "out any document open"));
312 UpdatableInset * tli = cur.inset() ? cur.inset()->asUpdatableInset() : 0;
314 // I would really like to avoid having this switch and rather try to
315 // encode this in the function itself.
316 bool disable = false;
317 switch (cmd.action) {
319 disable = cmd.argument != "custom"
320 && !Exporter::IsExportable(*buf, cmd.argument);
323 disable = buf->undostack().empty();
326 disable = buf->redostack().empty();
330 disable = !cur.selection();
334 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
338 disable = !Exporter::IsExportable(*buf, "program");
341 case LFUN_LAYOUT_TABULAR:
342 disable = !cur.innerInsetTabular();
346 disable = !changeDepthAllowed(cur, view()->getLyXText(), DEC_DEPTH);
349 case LFUN_DEPTH_PLUS:
350 disable = !changeDepthAllowed(cur, view()->getLyXText(), INC_DEPTH);
354 case LFUN_LAYOUT_PARAGRAPH:
355 disable = cur.inset()
356 && cur.inset()->forceDefaultParagraphs(cur.inset());
359 case LFUN_INSET_OPTARG:
360 disable = cur.inMathed()
361 || cur.paragraph().layout()->optionalargs == 0;
364 case LFUN_TABULAR_FEATURE:
366 if (cur.inMathed()) {
367 // FIXME: check temporarily disabled
369 char align = mathcursor::valign();
374 if (cmd.argument.empty()) {
378 if (!contains("tcb", cmd.argument[0])) {
382 flag.setOnOff(cmd.argument[0] == align);
386 char align = mathcursor::halign();
391 if (cmd.argument.empty()) {
395 if (!contains("lcr", cmd.argument[0])) {
399 flag.setOnOff(cmd.argument[0] == align);
401 disable = !mathcursor::halign();
407 //ret.disabled(true);
408 InsetTabular * tab = cur.innerInsetTabular();
410 ret = tab->getStatus(cmd.argument);
417 static InsetTabular inset(*buf, 1, 1);
419 FuncStatus ret = inset.getStatus(cmd.argument);
420 if (ret.onoff(true) || ret.onoff(false))
421 flag.setOnOff(false);
426 case LFUN_VC_REGISTER:
427 disable = buf->lyxvc().inUse();
429 case LFUN_VC_CHECKIN:
430 disable = !buf->lyxvc().inUse() || buf->isReadonly();
432 case LFUN_VC_CHECKOUT:
433 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
437 disable = !buf->lyxvc().inUse();
439 case LFUN_MENURELOAD:
440 disable = buf->isUnnamed() || buf->isClean();
442 case LFUN_BOOKMARK_GOTO:
444 isSavedPosition(strToUnsignedInt(cmd.argument));
447 case LFUN_MERGE_CHANGES:
448 case LFUN_ACCEPT_CHANGE:
449 case LFUN_REJECT_CHANGE:
450 case LFUN_ACCEPT_ALL_CHANGES:
451 case LFUN_REJECT_ALL_CHANGES:
452 disable = !buf->params().tracking_changes;
455 case LFUN_INSET_SETTINGS: {
459 UpdatableInset * inset = cur.inset()->asUpdatableInset();
463 // jump back to owner if an InsetText, so
464 // we get back to the InsetTabular or whatever
465 if (inset->lyxCode() == InsetOld::TEXT_CODE)
466 inset = inset->owner();
468 InsetOld::Code code = inset->lyxCode();
470 case InsetOld::TABULAR_CODE:
471 disable = cmd.argument != "tabular";
473 case InsetOld::ERT_CODE:
474 disable = cmd.argument != "ert";
476 case InsetOld::FLOAT_CODE:
477 disable = cmd.argument != "float";
479 case InsetOld::WRAP_CODE:
480 disable = cmd.argument != "wrap";
482 case InsetOld::NOTE_CODE:
483 disable = cmd.argument != "note";
485 case InsetOld::BRANCH_CODE:
486 disable = cmd.argument != "branch";
488 case InsetOld::BOX_CODE:
489 disable = cmd.argument != "box";
497 case LFUN_MATH_MUTATE:
499 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
500 flag.setOnOff(false);
505 // we just need to be in math mode to enable that
507 case LFUN_MATH_SPACE:
508 case LFUN_MATH_LIMITS:
509 case LFUN_MATH_NONUMBER:
510 case LFUN_MATH_NUMBER:
511 case LFUN_MATH_EXTERN:
512 disable = cur.inTexted();
515 case LFUN_DIALOG_SHOW: {
516 string const name = cmd.getArg(0);
518 disable = !(name == "aboutlyx" ||
521 name == "preferences" ||
526 if (name == "print") {
527 disable = !Exporter::IsExportable(*buf, "dvi") ||
528 lyxrc.print_command == "none";
529 } else if (name == "character") {
530 InsetBase * inset = cur.inset();
531 disable = inset && inset->lyxCode() == InsetOld::ERT_CODE;
532 } else if (name == "vclog") {
533 disable = !buf->lyxvc().inUse();
534 } else if (name == "latexlog") {
535 disable = !IsFileReadable(buf->getLogName().second);
544 // the functions which insert insets
545 InsetOld::Code code = InsetOld::NO_CODE;
546 switch (cmd.action) {
547 case LFUN_DIALOG_SHOW_NEW_INSET:
548 if (cmd.argument == "bibitem")
549 code = InsetOld::BIBITEM_CODE;
550 else if (cmd.argument == "bibtex")
551 code = InsetOld::BIBTEX_CODE;
552 else if (cmd.argument == "box")
553 code = InsetOld::BOX_CODE;
554 else if (cmd.argument == "branch")
555 code = InsetOld::BRANCH_CODE;
556 else if (cmd.argument == "citation")
557 code = InsetOld::CITE_CODE;
558 else if (cmd.argument == "ert")
559 code = InsetOld::ERT_CODE;
560 else if (cmd.argument == "external")
561 code = InsetOld::EXTERNAL_CODE;
562 else if (cmd.argument == "float")
563 code = InsetOld::FLOAT_CODE;
564 else if (cmd.argument == "graphics")
565 code = InsetOld::GRAPHICS_CODE;
566 else if (cmd.argument == "include")
567 code = InsetOld::INCLUDE_CODE;
568 else if (cmd.argument == "index")
569 code = InsetOld::INDEX_CODE;
570 else if (cmd.argument == "label")
571 code = InsetOld::LABEL_CODE;
572 else if (cmd.argument == "note")
573 code = InsetOld::NOTE_CODE;
574 else if (cmd.argument == "ref")
575 code = InsetOld::REF_CODE;
576 else if (cmd.argument == "toc")
577 code = InsetOld::TOC_CODE;
578 else if (cmd.argument == "url")
579 code = InsetOld::URL_CODE;
580 else if (cmd.argument == "vspace")
581 code = InsetOld::VSPACE_CODE;
582 else if (cmd.argument == "wrap")
583 code = InsetOld::WRAP_CODE;
587 code = InsetOld::ERT_CODE;
589 case LFUN_INSET_FOOTNOTE:
590 code = InsetOld::FOOT_CODE;
592 case LFUN_TABULAR_INSERT:
593 code = InsetOld::TABULAR_CODE;
595 case LFUN_INSET_MARGINAL:
596 code = InsetOld::MARGIN_CODE;
598 case LFUN_INSET_FLOAT:
599 case LFUN_INSET_WIDE_FLOAT:
600 code = InsetOld::FLOAT_CODE;
602 case LFUN_INSET_WRAP:
603 code = InsetOld::WRAP_CODE;
605 case LFUN_FLOAT_LIST:
606 code = InsetOld::FLOAT_LIST_CODE;
609 case LFUN_INSET_LIST:
610 code = InsetOld::LIST_CODE;
612 case LFUN_INSET_THEOREM:
613 code = InsetOld::THEOREM_CODE;
616 case LFUN_INSET_CAPTION:
617 code = InsetOld::CAPTION_CODE;
619 case LFUN_INSERT_NOTE:
620 code = InsetOld::NOTE_CODE;
622 case LFUN_INSERT_CHARSTYLE:
623 code = InsetOld::CHARSTYLE_CODE;
624 if (buf->params().getLyXTextClass().charstyles().empty())
627 case LFUN_INSERT_BOX:
628 code = InsetOld::BOX_CODE;
630 case LFUN_INSERT_BRANCH:
631 code = InsetOld::BRANCH_CODE;
632 if (buf->params().branchlist().empty())
635 case LFUN_INSERT_LABEL:
636 code = InsetOld::LABEL_CODE;
638 case LFUN_INSET_OPTARG:
639 code = InsetOld::OPTARG_CODE;
641 case LFUN_ENVIRONMENT_INSERT:
642 code = InsetOld::BOX_CODE;
644 case LFUN_INDEX_INSERT:
645 code = InsetOld::INDEX_CODE;
647 case LFUN_INDEX_PRINT:
648 code = InsetOld::INDEX_PRINT_CODE;
650 case LFUN_TOC_INSERT:
651 code = InsetOld::TOC_CODE;
655 code = InsetOld::URL_CODE;
658 // always allow this, since we will inset a raw quote
659 // if an inset is not allowed.
661 case LFUN_HYPHENATION:
662 case LFUN_LIGATURE_BREAK:
664 case LFUN_MENU_SEPARATOR:
666 case LFUN_END_OF_SENTENCE:
667 code = InsetOld::SPECIALCHAR_CODE;
669 case LFUN_SPACE_INSERT:
670 // slight hack: we know this is allowed in math mode
672 code = InsetOld::SPACE_CODE;
674 case LFUN_INSET_DIALOG_SHOW: {
675 InsetBase * inset = cur.nextInset();
678 code = inset->lyxCode();
679 if (!(code == InsetOld::INCLUDE_CODE
680 || code == InsetOld::BIBTEX_CODE
681 || code == InsetOld::FLOAT_LIST_CODE
682 || code == InsetOld::TOC_CODE))
690 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
696 // A few general toggles
697 switch (cmd.action) {
698 case LFUN_TOOLTIPS_TOGGLE:
699 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
702 case LFUN_READ_ONLY_TOGGLE:
703 flag.setOnOff(buf->isReadonly());
706 flag.setOnOff(cur.inTexted()
707 && cur.paragraph().params().startOfAppendix());
709 case LFUN_SWITCHBUFFER:
710 // toggle on the current buffer, but do not toggle off
711 // the other ones (is that a good idea?)
712 if (cmd.argument == buf->fileName())
715 case LFUN_TRACK_CHANGES:
716 flag.setOnOff(buf->params().tracking_changes);
723 // the font related toggles
724 if (cur.inTexted()) {
725 LyXFont const & font = cur.text()->real_current_font;
726 switch (cmd.action) {
728 flag.setOnOff(font.emph() == LyXFont::ON);
731 flag.setOnOff(font.noun() == LyXFont::ON);
734 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
737 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
740 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
743 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
749 string tc = mathcursor::getLastCode();
750 switch (cmd.action) {
752 flag.setOnOff(tc == "mathbf");
755 flag.setOnOff(tc == "mathsf");
758 flag.setOnOff(tc == "mathcal");
761 flag.setOnOff(tc == "mathrm");
764 flag.setOnOff(tc == "mathtt");
767 flag.setOnOff(tc == "mathbb");
770 flag.setOnOff(tc == "mathnormal");
778 // this one is difficult to get right. As a half-baked
779 // solution, we consider only the first action of the sequence
780 if (cmd.action == LFUN_SEQUENCE) {
781 // argument contains ';'-terminated commands
782 #warning LyXAction arguments not handled here.
783 flag = getStatus(FuncRequest(
784 lyxaction.lookupFunc(token(cmd.argument, ';', 0))));
793 bool ensureBufferClean(BufferView * bv)
795 Buffer & buf = *bv->buffer();
799 string const file = MakeDisplayPath(buf.fileName(), 30);
800 string text = bformat(_("The document %1$s has unsaved "
801 "changes.\n\nDo you want to save "
802 "the document?"), file);
803 int const ret = Alert::prompt(_("Save changed document?"),
804 text, 0, 1, _("&Save"),
808 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
810 return buf.isClean();
816 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
818 string argument = cmd.argument;
819 kb_action action = cmd.action;
821 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
822 lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
824 // we have not done anything wrong yet.
826 dispatch_buffer.erase();
827 selection_possible = false;
829 // We cannot use this function here
830 if (getStatus(cmd).disabled()) {
831 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
832 << lyxaction.getActionName(action)
833 << " [" << action << "] is disabled at this location"
835 setErrorMessage(getStatusMessage());
839 if (view()->available())
840 view()->hideCursor();
844 case LFUN_WORDFINDFORWARD:
845 case LFUN_WORDFINDBACKWARD: {
846 static string last_search;
847 string searched_string;
849 if (!argument.empty()) {
850 last_search = argument;
851 searched_string = argument;
853 searched_string = last_search;
855 if (searched_string.empty())
858 bool const fw = action == LFUN_WORDFINDFORWARD;
860 lyx::find::find2string(searched_string, true, false, fw);
861 view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
866 owner->message(keyseq.printOptions());
869 case LFUN_EXEC_COMMAND:
870 owner->focus_command_buffer();
875 meta_fake_bit = key_modifier::none;
876 if (view()->available())
877 // cancel any selection
878 dispatch(FuncRequest(LFUN_MARK_OFF));
879 setMessage(N_("Cancel"));
883 meta_fake_bit = key_modifier::alt;
884 setMessage(keyseq.print());
887 case LFUN_READ_ONLY_TOGGLE:
888 if (owner->buffer()->lyxvc().inUse())
889 owner->buffer()->lyxvc().toggleReadOnly();
891 owner->buffer()->setReadonly(
892 !owner->buffer()->isReadonly());
895 // --- Menus -----------------------------------------------
897 menuNew(argument, false);
900 case LFUN_MENUNEWTMPLT:
901 menuNew(argument, true);
904 case LFUN_CLOSEBUFFER:
909 if (!owner->buffer()->isUnnamed()) {
910 string const str = bformat(_("Saving document %1$s..."),
911 MakeDisplayPath(owner->buffer()->fileName()));
913 MenuWrite(owner->buffer());
914 owner->message(str + _(" done."));
916 WriteAs(owner->buffer());
920 WriteAs(owner->buffer(), argument);
923 case LFUN_MENURELOAD: {
924 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
925 string text = bformat(_("Any changes will be lost. Are you sure "
926 "you want to revert to the saved version of the document %1$s?"), file);
927 int const ret = Alert::prompt(_("Revert to saved document?"),
928 text, 0, 1, _("&Revert"), _("&Cancel"));
936 Exporter::Export(owner->buffer(), argument, true);
937 view()->showErrorList(BufferFormat(*owner->buffer()));
941 Exporter::Preview(owner->buffer(), argument);
942 view()->showErrorList(BufferFormat(*owner->buffer()));
946 Exporter::Export(owner->buffer(), "program", true);
947 view()->showErrorList(_("Build"));
951 owner->buffer()->runChktex();
952 view()->showErrorList(_("ChkTeX"));
956 if (argument == "custom")
957 owner->getDialogs().showSendto();
959 Exporter::Export(owner->buffer(), argument, false);
960 view()->showErrorList(BufferFormat(*owner->buffer()));
973 InsetCommandParams p("tableofcontents");
974 string const data = InsetCommandMailer::params2string("toc", p);
975 owner->getDialogs().show("toc", data, 0);
983 case LFUN_RECONFIGURE:
987 case LFUN_HELP_OPEN: {
988 string const arg = argument;
990 setErrorMessage(N_("Missing argument"));
993 string const fname = i18nLibFileSearch("doc", arg, "lyx");
995 lyxerr << "LyX: unable to find documentation file `"
996 << arg << "'. Bad installation?" << endl;
999 owner->message(bformat(_("Opening help file %1$s..."),
1000 MakeDisplayPath(fname)));
1001 view()->loadLyXFile(fname, false);
1005 // --- version control -------------------------------
1006 case LFUN_VC_REGISTER:
1007 if (!ensureBufferClean(view()))
1009 if (!owner->buffer()->lyxvc().inUse()) {
1010 owner->buffer()->lyxvc().registrer();
1015 case LFUN_VC_CHECKIN:
1016 if (!ensureBufferClean(view()))
1018 if (owner->buffer()->lyxvc().inUse()
1019 && !owner->buffer()->isReadonly()) {
1020 owner->buffer()->lyxvc().checkIn();
1025 case LFUN_VC_CHECKOUT:
1026 if (!ensureBufferClean(view()))
1028 if (owner->buffer()->lyxvc().inUse()
1029 && owner->buffer()->isReadonly()) {
1030 owner->buffer()->lyxvc().checkOut();
1035 case LFUN_VC_REVERT:
1036 owner->buffer()->lyxvc().revert();
1041 owner->buffer()->lyxvc().undoLast();
1045 // --- buffers ----------------------------------------
1046 case LFUN_SWITCHBUFFER:
1047 view()->buffer(bufferlist.getBuffer(argument));
1051 NewFile(view(), argument);
1054 case LFUN_FILE_OPEN:
1058 case LFUN_DROP_LAYOUTS_CHOICE:
1059 owner->getToolbar().openLayoutList();
1062 case LFUN_MENU_OPEN_BY_NAME:
1063 owner->getMenubar().openByName(argument);
1066 // --- lyxserver commands ----------------------------
1068 setMessage(owner->buffer()->fileName());
1069 lyxerr[Debug::INFO] << "FNAME["
1070 << owner->buffer()->fileName()
1075 dispatch_buffer = keyseq.print();
1076 lyxserver->notifyClient(dispatch_buffer);
1079 case LFUN_GOTOFILEROW: {
1082 istringstream is(argument);
1083 is >> file_name >> row;
1084 if (prefixIs(file_name, getTmpDir())) {
1085 // Needed by inverse dvi search. If it is a file
1086 // in tmpdir, call the apropriated function
1087 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1089 // Must replace extension of the file to be .lyx
1090 // and get full path
1091 string const s = ChangeExtension(file_name, ".lyx");
1092 // Either change buffer or load the file
1093 if (bufferlist.exists(s)) {
1094 view()->buffer(bufferlist.getBuffer(s));
1096 view()->loadLyXFile(s);
1100 view()->setCursorFromRow(row);
1103 // see BufferView_pimpl::center()
1104 view()->updateScrollbar();
1108 case LFUN_GOTO_PARAGRAPH: {
1109 istringstream is(argument);
1112 ParIterator par = owner->buffer()->getParFromID(id);
1113 if (par == owner->buffer()->par_iterator_end()) {
1114 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1115 << id << ']' << endl;
1118 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1119 << " found." << endl;
1123 view()->setCursor(par, 0);
1125 view()->switchKeyMap();
1126 owner->view_state_changed();
1129 // see BufferView_pimpl::center()
1130 view()->updateScrollbar();
1134 case LFUN_DIALOG_SHOW: {
1135 string const name = cmd.getArg(0);
1136 string data = trim(cmd.argument.substr(name.size()));
1138 if (name == "character") {
1139 data = freefont2string();
1141 owner->getDialogs().show("character", data);
1143 else if (name == "document")
1144 owner->getDialogs().showDocument();
1145 else if (name == "forks")
1146 owner->getDialogs().showForks();
1147 else if (name == "preamble")
1148 owner->getDialogs().showPreamble();
1149 else if (name == "preferences")
1150 owner->getDialogs().showPreferences();
1151 else if (name == "print")
1152 owner->getDialogs().showPrint();
1153 else if (name == "spellchecker")
1154 owner->getDialogs().showSpellchecker();
1156 else if (name == "latexlog") {
1157 pair<Buffer::LogType, string> const logfile =
1158 owner->buffer()->getLogName();
1159 switch (logfile.first) {
1160 case Buffer::latexlog:
1163 case Buffer::buildlog:
1167 data += logfile.second;
1168 owner->getDialogs().show("log", data);
1170 else if (name == "vclog") {
1171 string const data = "vc " +
1172 owner->buffer()->lyxvc().getLogFile();
1173 owner->getDialogs().show("log", data);
1176 owner->getDialogs().show(name, data);
1180 case LFUN_DIALOG_SHOW_NEW_INSET: {
1181 string const name = cmd.getArg(0);
1182 string data = trim(cmd.argument.substr(name.size()));
1183 if (name == "bibitem" ||
1185 name == "include" ||
1191 InsetCommandParams p(name);
1192 data = InsetCommandMailer::params2string(name, p);
1193 } else if (name == "box") {
1194 // \c data == "Boxed" || "Frameless" etc
1195 InsetBoxParams p(data);
1196 data = InsetBoxMailer::params2string(p);
1197 } else if (name == "branch") {
1198 InsetBranchParams p;
1199 data = InsetBranchMailer::params2string(p);
1200 } else if (name == "citation") {
1201 InsetCommandParams p("cite");
1202 data = InsetCommandMailer::params2string(name, p);
1203 } else if (name == "ert") {
1204 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1205 } else if (name == "external") {
1206 InsetExternalParams p;
1207 Buffer const & buffer = *owner->buffer();
1208 data = InsetExternalMailer::params2string(p, buffer);
1209 } else if (name == "float") {
1211 data = InsetFloatMailer::params2string(p);
1212 } else if (name == "graphics") {
1213 InsetGraphicsParams p;
1214 Buffer const & buffer = *owner->buffer();
1215 data = InsetGraphicsMailer::params2string(p, buffer);
1216 } else if (name == "note") {
1218 data = InsetNoteMailer::params2string(p);
1219 } else if (name == "vspace") {
1221 data = InsetVSpaceMailer::params2string(space);
1222 } else if (name == "wrap") {
1224 data = InsetWrapMailer::params2string(p);
1226 owner->getDialogs().show(name, data, 0);
1230 case LFUN_DIALOG_SHOW_NEXT_INSET:
1233 case LFUN_INSET_DIALOG_SHOW: {
1234 InsetBase * inset = view()->cursor().nextInset();
1236 inset->dispatch(view()->cursor(), FuncRequest(LFUN_INSET_DIALOG_SHOW));
1240 case LFUN_DIALOG_UPDATE: {
1241 string const & name = argument;
1242 // Can only update a dialog connected to an existing inset
1243 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1245 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
1246 inset->dispatch(view()->cursor(), fr);
1247 } else if (name == "paragraph") {
1248 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1253 case LFUN_DIALOG_HIDE:
1254 Dialogs::hide(argument, 0);
1257 case LFUN_DIALOG_DISCONNECT_INSET:
1258 owner->getDialogs().disconnect(argument);
1261 case LFUN_CHILDOPEN: {
1262 string const filename =
1263 MakeAbsPath(argument, owner->buffer()->filePath());
1264 setMessage(N_("Opening child document ") +
1265 MakeDisplayPath(filename) + "...");
1266 view()->savePosition(0);
1267 string const parentfilename = owner->buffer()->fileName();
1268 if (bufferlist.exists(filename))
1269 view()->buffer(bufferlist.getBuffer(filename));
1271 view()->loadLyXFile(filename);
1272 // Set the parent name of the child document.
1273 // This makes insertion of citations and references in the child work,
1274 // when the target is in the parent or another child document.
1275 owner->buffer()->setParentName(parentfilename);
1279 case LFUN_TOGGLECURSORFOLLOW:
1280 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1284 owner->getIntl().KeyMapOn(false);
1287 case LFUN_KMAP_PRIM:
1288 owner->getIntl().KeyMapPrim();
1292 owner->getIntl().KeyMapSec();
1295 case LFUN_KMAP_TOGGLE:
1296 owner->getIntl().ToggleKeyMap();
1302 argument = split(argument, countstr, ' ');
1303 istringstream is(countstr);
1306 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1307 for (int i = 0; i < count; ++i)
1308 dispatch(lyxaction.lookupFunc(argument));
1313 // argument contains ';'-terminated commands
1314 while (!argument.empty()) {
1316 argument = split(argument, first, ';');
1317 dispatch(lyxaction.lookupFunc(first));
1321 case LFUN_SAVEPREFERENCES: {
1322 Path p(user_lyxdir());
1323 lyxrc.write("preferences");
1327 case LFUN_SCREEN_FONT_UPDATE:
1328 // handle the screen font changes.
1329 lyxrc.set_font_norm_type();
1330 lyx_gui::update_fonts();
1331 // All visible buffers will need resize
1335 case LFUN_SET_COLOR: {
1337 string const x11_name = split(argument, lyx_name, ' ');
1338 if (lyx_name.empty() || x11_name.empty()) {
1339 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1344 bool const graphicsbg_changed =
1345 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1346 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1348 LColor::color col = lcolor.getFromLyXName(lyx_name);
1349 if (!lcolor.setColor(col, x11_name)) {
1351 bformat(_("Set-color \"%1$s\" failed "
1352 "- color is undefined or "
1353 "may not be redefined"), lyx_name));
1357 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1359 if (graphicsbg_changed) {
1360 #ifdef WITH_WARNINGS
1361 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1364 lyx::graphics::GCache::get().changeDisplay(true);
1371 owner->message(argument);
1374 case LFUN_FORKS_KILL:
1375 if (isStrInt(argument))
1376 ForkedcallsController::get().kill(strToInt(argument));
1379 case LFUN_TOOLTIPS_TOGGLE:
1380 owner->getDialogs().toggleTooltips();
1383 case LFUN_EXTERNAL_EDIT:
1384 InsetExternal().dispatch(view()->cursor(), FuncRequest(action, argument));
1388 DispatchResult res = view()->cursor().dispatch(cmd);
1389 if (!res.dispatched());
1390 view()->dispatch(cmd);
1395 if (view()->cursor().inTexted())
1396 view()->owner()->updateLayoutChoice();
1398 if (view()->available()) {
1399 view()->fitCursor();
1401 view()->cursor().updatePos();
1402 // if we executed a mutating lfun, mark the buffer as dirty
1403 if (!getStatus(cmd).disabled()
1404 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1405 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1406 view()->buffer()->markDirty();
1409 if (view()->cursor().inTexted())
1410 sendDispatchMessage(getMessage(), cmd, verbose);
1414 void LyXFunc::sendDispatchMessage(string const & msg,
1415 FuncRequest const & cmd, bool verbose)
1417 owner->updateMenubar();
1418 owner->updateToolbar();
1420 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1421 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1423 owner->message(msg);
1427 string dispatch_msg = msg;
1428 if (!dispatch_msg.empty())
1429 dispatch_msg += ' ';
1431 string comname = lyxaction.getActionName(cmd.action);
1433 bool argsadded = false;
1435 if (!cmd.argument.empty()) {
1436 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1437 comname += ' ' + cmd.argument;
1442 string const shortcuts = toplevel_keymap->findbinding(cmd);
1444 if (!shortcuts.empty()) {
1445 comname += ": " + shortcuts;
1446 } else if (!argsadded && !cmd.argument.empty()) {
1447 comname += ' ' + cmd.argument;
1450 if (!comname.empty()) {
1451 comname = rtrim(comname);
1452 dispatch_msg += '(' + comname + ')';
1455 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1456 if (!dispatch_msg.empty())
1457 owner->message(dispatch_msg);
1461 void LyXFunc::setupLocalKeymap()
1463 keyseq.stdmap = toplevel_keymap.get();
1464 keyseq.curmap = toplevel_keymap.get();
1465 cancel_meta_seq.stdmap = toplevel_keymap.get();
1466 cancel_meta_seq.curmap = toplevel_keymap.get();
1470 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1472 string initpath = lyxrc.document_path;
1473 string filename(name);
1475 if (view()->available()) {
1476 string const trypath = owner->buffer()->filePath();
1477 // If directory is writeable, use this as default.
1478 if (IsDirWriteable(trypath))
1482 static int newfile_number;
1484 if (filename.empty()) {
1485 filename = AddName(lyxrc.document_path,
1486 "newfile" + tostr(++newfile_number) + ".lyx");
1487 FileInfo fi(filename);
1488 while (bufferlist.exists(filename) || fi.readable()) {
1490 filename = AddName(lyxrc.document_path,
1491 "newfile" + tostr(newfile_number) +
1493 fi.newFile(filename);
1497 // The template stuff
1500 FileDialog fileDlg(_("Select template file"),
1501 LFUN_SELECT_FILE_SYNC,
1502 make_pair(string(_("Documents|#o#O")),
1503 string(lyxrc.document_path)),
1504 make_pair(string(_("Templates|#T#t")),
1505 string(lyxrc.template_path)));
1507 FileDialog::Result result =
1508 fileDlg.open(lyxrc.template_path,
1509 FileFilterList(_("LyX Documents (*.lyx)")),
1512 if (result.first == FileDialog::Later)
1514 if (result.second.empty())
1516 templname = result.second;
1519 view()->newFile(filename, templname, !name.empty());
1523 void LyXFunc::open(string const & fname)
1525 string initpath = lyxrc.document_path;
1527 if (view()->available()) {
1528 string const trypath = owner->buffer()->filePath();
1529 // If directory is writeable, use this as default.
1530 if (IsDirWriteable(trypath))
1536 if (fname.empty()) {
1537 FileDialog fileDlg(_("Select document to open"),
1539 make_pair(string(_("Documents|#o#O")),
1540 string(lyxrc.document_path)),
1541 make_pair(string(_("Examples|#E#e")),
1542 string(AddPath(system_lyxdir(), "examples"))));
1544 FileDialog::Result result =
1545 fileDlg.open(initpath,
1546 FileFilterList(_("LyX Documents (*.lyx)")),
1549 if (result.first == FileDialog::Later)
1552 filename = result.second;
1554 // check selected filename
1555 if (filename.empty()) {
1556 owner->message(_("Canceled."));
1562 // get absolute path of file and add ".lyx" to the filename if
1564 string const fullpath = FileSearch(string(), filename, "lyx");
1565 if (!fullpath.empty()) {
1566 filename = fullpath;
1569 string const disp_fn(MakeDisplayPath(filename));
1571 // if the file doesn't exist, let the user create one
1572 FileInfo const f(filename, true);
1574 // the user specifically chose this name. Believe them.
1575 view()->newFile(filename, "", true);
1579 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1582 if (view()->loadLyXFile(filename)) {
1583 str2 = bformat(_("Document %1$s opened."), disp_fn);
1585 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1587 owner->message(str2);
1591 void LyXFunc::doImport(string const & argument)
1594 string filename = split(argument, format, ' ');
1596 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1597 << " file: " << filename << endl;
1599 // need user interaction
1600 if (filename.empty()) {
1601 string initpath = lyxrc.document_path;
1603 if (view()->available()) {
1604 string const trypath = owner->buffer()->filePath();
1605 // If directory is writeable, use this as default.
1606 if (IsDirWriteable(trypath))
1610 string const text = bformat(_("Select %1$s file to import"),
1611 formats.prettyName(format));
1613 FileDialog fileDlg(text,
1615 make_pair(string(_("Documents|#o#O")),
1616 string(lyxrc.document_path)),
1617 make_pair(string(_("Examples|#E#e")),
1618 string(AddPath(system_lyxdir(), "examples"))));
1620 string const filter = formats.prettyName(format)
1621 + " (*." + formats.extension(format) + ')';
1623 FileDialog::Result result =
1624 fileDlg.open(initpath,
1625 FileFilterList(filter),
1628 if (result.first == FileDialog::Later)
1631 filename = result.second;
1633 // check selected filename
1634 if (filename.empty())
1635 owner->message(_("Canceled."));
1638 if (filename.empty())
1641 // get absolute path of file
1642 filename = MakeAbsPath(filename);
1644 string const lyxfile = ChangeExtension(filename, ".lyx");
1646 // Check if the document already is open
1647 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1648 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1649 owner->message(_("Canceled."));
1654 // if the file exists already, and we didn't do
1655 // -i lyx thefile.lyx, warn
1656 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1657 string const file = MakeDisplayPath(lyxfile, 30);
1659 string text = bformat(_("The document %1$s already exists.\n\n"
1660 "Do you want to over-write that document?"), file);
1661 int const ret = Alert::prompt(_("Over-write document?"),
1662 text, 0, 1, _("&Over-write"), _("&Cancel"));
1665 owner->message(_("Canceled."));
1670 Importer::Import(owner, filename, format);
1674 void LyXFunc::closeBuffer()
1676 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1677 if (bufferlist.empty()) {
1678 // need this otherwise SEGV may occur while
1679 // trying to set variables that don't exist
1680 // since there's no current buffer
1681 owner->getDialogs().hideBufferDependent();
1683 view()->buffer(bufferlist.first());
1689 // Each "owner" should have it's own message method. lyxview and
1690 // the minibuffer would use the minibuffer, but lyxserver would
1691 // send an ERROR signal to its client. Alejandro 970603
1692 // This function is bit problematic when it comes to NLS, to make the
1693 // lyx servers client be language indepenent we must not translate
1694 // strings sent to this func.
1695 void LyXFunc::setErrorMessage(string const & m) const
1697 dispatch_buffer = m;
1702 void LyXFunc::setMessage(string const & m) const
1704 dispatch_buffer = m;
1708 void LyXFunc::setStatusMessage(string const & m) const
1714 string const LyXFunc::viewStatusMessage()
1716 // When meta-fake key is pressed, show the key sequence so far + "M-".
1718 return keyseq.print() + "M-";
1720 // Else, when a non-complete key sequence is pressed,
1721 // show the available options.
1722 if (keyseq.length() > 0 && !keyseq.deleted())
1723 return keyseq.printOptions();
1725 if (!view()->available())
1726 return _("Welcome to LyX!");
1728 return view()->cursor().currentState();
1732 BufferView * LyXFunc::view() const
1734 BOOST_ASSERT(owner);
1735 return owner->view().get();
1739 bool LyXFunc::wasMetaKey() const
1741 return (meta_fake_bit != key_modifier::none);