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()->cursor().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 // I would really like to avoid having this switch and rather try to
313 // encode this in the function itself.
314 bool disable = false;
315 switch (cmd.action) {
317 disable = cmd.argument != "custom"
318 && !Exporter::IsExportable(*buf, cmd.argument);
321 disable = buf->undostack().empty();
324 disable = buf->redostack().empty();
328 disable = !cur.selection();
332 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
336 disable = !Exporter::IsExportable(*buf, "program");
339 case LFUN_LAYOUT_TABULAR:
340 disable = !cur.innerInsetOfType(InsetBase::TABULAR_CODE);
344 disable = !changeDepthAllowed(cur, view()->getLyXText(), DEC_DEPTH);
347 case LFUN_DEPTH_PLUS:
348 disable = !changeDepthAllowed(cur, view()->getLyXText(), INC_DEPTH);
352 case LFUN_LAYOUT_PARAGRAPH:
353 disable = cur.inset().forceDefaultParagraphs(&cur.inset());
356 case LFUN_INSET_OPTARG:
357 disable = cur.inMathed()
358 || cur.paragraph().layout()->optionalargs == 0;
361 case LFUN_TABULAR_FEATURE:
363 if (cur.inMathed()) {
364 // FIXME: check temporarily disabled
366 char align = mathcursor::valign();
371 if (cmd.argument.empty()) {
375 if (!contains("tcb", cmd.argument[0])) {
379 flag.setOnOff(cmd.argument[0] == align);
383 char const align = mathcursor::halign();
388 if (cmd.argument.empty()) {
392 if (!contains("lcr", cmd.argument[0])) {
396 flag.setOnOff(cmd.argument[0] == align);
398 disable = !mathcursor::halign();
402 if (!cur.empty() && cur.inset().asUpdatableInset()) {
404 //ret.disabled(true);
405 InsetTabular * tab = static_cast<InsetTabular *>
406 (cur.innerInsetOfType(InsetBase::TABULAR_CODE));
408 ret = tab->getStatus(cmd.argument);
415 static InsetTabular inset(*buf, 1, 1);
417 FuncStatus ret = inset.getStatus(cmd.argument);
418 if (ret.onoff(true) || ret.onoff(false))
419 flag.setOnOff(false);
424 case LFUN_VC_REGISTER:
425 disable = buf->lyxvc().inUse();
427 case LFUN_VC_CHECKIN:
428 disable = !buf->lyxvc().inUse() || buf->isReadonly();
430 case LFUN_VC_CHECKOUT:
431 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
435 disable = !buf->lyxvc().inUse();
437 case LFUN_MENURELOAD:
438 disable = buf->isUnnamed() || buf->isClean();
440 case LFUN_BOOKMARK_GOTO:
442 isSavedPosition(strToUnsignedInt(cmd.argument));
445 case LFUN_MERGE_CHANGES:
446 case LFUN_ACCEPT_CHANGE:
447 case LFUN_REJECT_CHANGE:
448 case LFUN_ACCEPT_ALL_CHANGES:
449 case LFUN_REJECT_ALL_CHANGES:
450 disable = !buf->params().tracking_changes;
453 case LFUN_INSET_SETTINGS: {
455 UpdatableInset * inset = cur.inset().asUpdatableInset();
459 // jump back to owner if an InsetText, so
460 // we get back to the InsetTabular or whatever
461 if (inset->lyxCode() == InsetOld::TEXT_CODE)
462 inset = inset->owner();
464 InsetOld::Code code = inset->lyxCode();
466 case InsetOld::TABULAR_CODE:
467 disable = cmd.argument != "tabular";
469 case InsetOld::ERT_CODE:
470 disable = cmd.argument != "ert";
472 case InsetOld::FLOAT_CODE:
473 disable = cmd.argument != "float";
475 case InsetOld::WRAP_CODE:
476 disable = cmd.argument != "wrap";
478 case InsetOld::NOTE_CODE:
479 disable = cmd.argument != "note";
481 case InsetOld::BRANCH_CODE:
482 disable = cmd.argument != "branch";
484 case InsetOld::BOX_CODE:
485 disable = cmd.argument != "box";
493 case LFUN_MATH_MUTATE:
495 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
496 flag.setOnOff(false);
501 // we just need to be in math mode to enable that
503 case LFUN_MATH_SPACE:
504 case LFUN_MATH_LIMITS:
505 case LFUN_MATH_NONUMBER:
506 case LFUN_MATH_NUMBER:
507 case LFUN_MATH_EXTERN:
508 disable = cur.inTexted();
511 case LFUN_DIALOG_SHOW: {
512 string const name = cmd.getArg(0);
514 disable = !(name == "aboutlyx" ||
516 name == "preferences" ||
518 else if (name == "print")
519 disable = !Exporter::IsExportable(*buf, "dvi") ||
520 lyxrc.print_command == "none";
521 else if (name == "character")
522 disable = cur.inset().lyxCode() == InsetOld::ERT_CODE;
523 else if (name == "vclog")
524 disable = !buf->lyxvc().inUse();
525 else if (name == "latexlog")
526 disable = !IsFileReadable(buf->getLogName().second);
534 // the functions which insert insets
535 InsetOld::Code code = InsetOld::NO_CODE;
536 switch (cmd.action) {
537 case LFUN_DIALOG_SHOW_NEW_INSET:
538 if (cmd.argument == "bibitem")
539 code = InsetOld::BIBITEM_CODE;
540 else if (cmd.argument == "bibtex")
541 code = InsetOld::BIBTEX_CODE;
542 else if (cmd.argument == "box")
543 code = InsetOld::BOX_CODE;
544 else if (cmd.argument == "branch")
545 code = InsetOld::BRANCH_CODE;
546 else if (cmd.argument == "citation")
547 code = InsetOld::CITE_CODE;
548 else if (cmd.argument == "ert")
549 code = InsetOld::ERT_CODE;
550 else if (cmd.argument == "external")
551 code = InsetOld::EXTERNAL_CODE;
552 else if (cmd.argument == "float")
553 code = InsetOld::FLOAT_CODE;
554 else if (cmd.argument == "graphics")
555 code = InsetOld::GRAPHICS_CODE;
556 else if (cmd.argument == "include")
557 code = InsetOld::INCLUDE_CODE;
558 else if (cmd.argument == "index")
559 code = InsetOld::INDEX_CODE;
560 else if (cmd.argument == "label")
561 code = InsetOld::LABEL_CODE;
562 else if (cmd.argument == "note")
563 code = InsetOld::NOTE_CODE;
564 else if (cmd.argument == "ref")
565 code = InsetOld::REF_CODE;
566 else if (cmd.argument == "toc")
567 code = InsetOld::TOC_CODE;
568 else if (cmd.argument == "url")
569 code = InsetOld::URL_CODE;
570 else if (cmd.argument == "vspace")
571 code = InsetOld::VSPACE_CODE;
572 else if (cmd.argument == "wrap")
573 code = InsetOld::WRAP_CODE;
577 code = InsetOld::ERT_CODE;
579 case LFUN_INSET_FOOTNOTE:
580 code = InsetOld::FOOT_CODE;
582 case LFUN_TABULAR_INSERT:
583 code = InsetOld::TABULAR_CODE;
585 case LFUN_INSET_MARGINAL:
586 code = InsetOld::MARGIN_CODE;
588 case LFUN_INSET_FLOAT:
589 case LFUN_INSET_WIDE_FLOAT:
590 code = InsetOld::FLOAT_CODE;
592 case LFUN_INSET_WRAP:
593 code = InsetOld::WRAP_CODE;
595 case LFUN_FLOAT_LIST:
596 code = InsetOld::FLOAT_LIST_CODE;
599 case LFUN_INSET_LIST:
600 code = InsetOld::LIST_CODE;
602 case LFUN_INSET_THEOREM:
603 code = InsetOld::THEOREM_CODE;
606 case LFUN_INSET_CAPTION:
607 code = InsetOld::CAPTION_CODE;
609 case LFUN_INSERT_NOTE:
610 code = InsetOld::NOTE_CODE;
612 case LFUN_INSERT_CHARSTYLE:
613 code = InsetOld::CHARSTYLE_CODE;
614 if (buf->params().getLyXTextClass().charstyles().empty())
617 case LFUN_INSERT_BOX:
618 code = InsetOld::BOX_CODE;
620 case LFUN_INSERT_BRANCH:
621 code = InsetOld::BRANCH_CODE;
622 if (buf->params().branchlist().empty())
625 case LFUN_INSERT_LABEL:
626 code = InsetOld::LABEL_CODE;
628 case LFUN_INSET_OPTARG:
629 code = InsetOld::OPTARG_CODE;
631 case LFUN_ENVIRONMENT_INSERT:
632 code = InsetOld::BOX_CODE;
634 case LFUN_INDEX_INSERT:
635 code = InsetOld::INDEX_CODE;
637 case LFUN_INDEX_PRINT:
638 code = InsetOld::INDEX_PRINT_CODE;
640 case LFUN_TOC_INSERT:
641 code = InsetOld::TOC_CODE;
645 code = InsetOld::URL_CODE;
648 // always allow this, since we will inset a raw quote
649 // if an inset is not allowed.
651 case LFUN_HYPHENATION:
652 case LFUN_LIGATURE_BREAK:
654 case LFUN_MENU_SEPARATOR:
656 case LFUN_END_OF_SENTENCE:
657 code = InsetOld::SPECIALCHAR_CODE;
659 case LFUN_SPACE_INSERT:
660 // slight hack: we know this is allowed in math mode
662 code = InsetOld::SPACE_CODE;
664 case LFUN_INSET_DIALOG_SHOW: {
665 InsetBase * inset = cur.nextInset();
668 code = inset->lyxCode();
669 if (!(code == InsetOld::INCLUDE_CODE
670 || code == InsetOld::BIBTEX_CODE
671 || code == InsetOld::FLOAT_LIST_CODE
672 || code == InsetOld::TOC_CODE))
681 if (code != InsetOld::NO_CODE
682 && (cur.empty() || !cur.inset().insetAllowed(code)))
688 // A few general toggles
689 switch (cmd.action) {
690 case LFUN_TOOLTIPS_TOGGLE:
691 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
694 case LFUN_READ_ONLY_TOGGLE:
695 flag.setOnOff(buf->isReadonly());
698 flag.setOnOff(cur.inTexted()
699 && cur.paragraph().params().startOfAppendix());
701 case LFUN_SWITCHBUFFER:
702 // toggle on the current buffer, but do not toggle off
703 // the other ones (is that a good idea?)
704 if (cmd.argument == buf->fileName())
707 case LFUN_TRACK_CHANGES:
708 flag.setOnOff(buf->params().tracking_changes);
715 // the font related toggles
716 if (cur.inTexted()) {
717 LyXFont const & font = cur.text()->real_current_font;
718 switch (cmd.action) {
720 flag.setOnOff(font.emph() == LyXFont::ON);
723 flag.setOnOff(font.noun() == LyXFont::ON);
726 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
729 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
732 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
735 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
741 string tc = mathcursor::getLastCode();
742 switch (cmd.action) {
744 flag.setOnOff(tc == "mathbf");
747 flag.setOnOff(tc == "mathsf");
750 flag.setOnOff(tc == "mathcal");
753 flag.setOnOff(tc == "mathrm");
756 flag.setOnOff(tc == "mathtt");
759 flag.setOnOff(tc == "mathbb");
762 flag.setOnOff(tc == "mathnormal");
770 // this one is difficult to get right. As a half-baked
771 // solution, we consider only the first action of the sequence
772 if (cmd.action == LFUN_SEQUENCE) {
773 // argument contains ';'-terminated commands
774 #warning LyXAction arguments not handled here.
775 flag = getStatus(FuncRequest(
776 lyxaction.lookupFunc(token(cmd.argument, ';', 0))));
785 bool ensureBufferClean(BufferView * bv)
787 Buffer & buf = *bv->buffer();
791 string const file = MakeDisplayPath(buf.fileName(), 30);
792 string text = bformat(_("The document %1$s has unsaved "
793 "changes.\n\nDo you want to save "
794 "the document?"), file);
795 int const ret = Alert::prompt(_("Save changed document?"),
796 text, 0, 1, _("&Save"),
800 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
802 return buf.isClean();
808 void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
810 string const argument = cmd.argument;
811 kb_action const action = cmd.action;
813 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << cmd << endl;
814 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
816 // we have not done anything wrong yet.
818 dispatch_buffer.erase();
819 selection_possible = false;
821 // We cannot use this function here
822 if (getStatus(cmd).disabled()) {
823 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
824 << lyxaction.getActionName(action)
825 << " [" << action << "] is disabled at this location"
827 setErrorMessage(getStatusMessage());
831 if (view()->available())
832 view()->hideCursor();
836 case LFUN_WORDFINDFORWARD:
837 case LFUN_WORDFINDBACKWARD: {
838 static string last_search;
839 string searched_string;
841 if (!argument.empty()) {
842 last_search = argument;
843 searched_string = argument;
845 searched_string = last_search;
847 if (searched_string.empty())
850 bool const fw = action == LFUN_WORDFINDFORWARD;
852 lyx::find::find2string(searched_string, true, false, fw);
853 view()->dispatch(FuncRequest(LFUN_WORD_FIND, data));
858 owner->message(keyseq.printOptions());
861 case LFUN_EXEC_COMMAND:
862 owner->focus_command_buffer();
867 meta_fake_bit = key_modifier::none;
868 if (view()->available())
869 // cancel any selection
870 dispatch(FuncRequest(LFUN_MARK_OFF));
871 setMessage(N_("Cancel"));
875 meta_fake_bit = key_modifier::alt;
876 setMessage(keyseq.print());
879 case LFUN_READ_ONLY_TOGGLE:
880 if (owner->buffer()->lyxvc().inUse())
881 owner->buffer()->lyxvc().toggleReadOnly();
883 owner->buffer()->setReadonly(
884 !owner->buffer()->isReadonly());
887 // --- Menus -----------------------------------------------
889 menuNew(argument, false);
892 case LFUN_MENUNEWTMPLT:
893 menuNew(argument, true);
896 case LFUN_CLOSEBUFFER:
901 if (!owner->buffer()->isUnnamed()) {
902 string const str = bformat(_("Saving document %1$s..."),
903 MakeDisplayPath(owner->buffer()->fileName()));
905 MenuWrite(owner->buffer());
906 owner->message(str + _(" done."));
908 WriteAs(owner->buffer());
912 WriteAs(owner->buffer(), argument);
915 case LFUN_MENURELOAD: {
916 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
917 string text = bformat(_("Any changes will be lost. Are you sure "
918 "you want to revert to the saved version of the document %1$s?"), file);
919 int const ret = Alert::prompt(_("Revert to saved document?"),
920 text, 0, 1, _("&Revert"), _("&Cancel"));
928 Exporter::Export(owner->buffer(), argument, true);
929 view()->showErrorList(BufferFormat(*owner->buffer()));
933 Exporter::Preview(owner->buffer(), argument);
934 view()->showErrorList(BufferFormat(*owner->buffer()));
938 Exporter::Export(owner->buffer(), "program", true);
939 view()->showErrorList(_("Build"));
943 owner->buffer()->runChktex();
944 view()->showErrorList(_("ChkTeX"));
948 if (argument == "custom")
949 owner->getDialogs().showSendto();
951 Exporter::Export(owner->buffer(), argument, false);
952 view()->showErrorList(BufferFormat(*owner->buffer()));
965 InsetCommandParams p("tableofcontents");
966 string const data = InsetCommandMailer::params2string("toc", p);
967 owner->getDialogs().show("toc", data, 0);
975 case LFUN_RECONFIGURE:
979 case LFUN_HELP_OPEN: {
980 string const arg = argument;
982 setErrorMessage(N_("Missing argument"));
985 string const fname = i18nLibFileSearch("doc", arg, "lyx");
987 lyxerr << "LyX: unable to find documentation file `"
988 << arg << "'. Bad installation?" << endl;
991 owner->message(bformat(_("Opening help file %1$s..."),
992 MakeDisplayPath(fname)));
993 view()->loadLyXFile(fname, false);
997 // --- version control -------------------------------
998 case LFUN_VC_REGISTER:
999 if (!ensureBufferClean(view()))
1001 if (!owner->buffer()->lyxvc().inUse()) {
1002 owner->buffer()->lyxvc().registrer();
1007 case LFUN_VC_CHECKIN:
1008 if (!ensureBufferClean(view()))
1010 if (owner->buffer()->lyxvc().inUse()
1011 && !owner->buffer()->isReadonly()) {
1012 owner->buffer()->lyxvc().checkIn();
1017 case LFUN_VC_CHECKOUT:
1018 if (!ensureBufferClean(view()))
1020 if (owner->buffer()->lyxvc().inUse()
1021 && owner->buffer()->isReadonly()) {
1022 owner->buffer()->lyxvc().checkOut();
1027 case LFUN_VC_REVERT:
1028 owner->buffer()->lyxvc().revert();
1033 owner->buffer()->lyxvc().undoLast();
1037 // --- buffers ----------------------------------------
1038 case LFUN_SWITCHBUFFER:
1039 view()->setBuffer(bufferlist.getBuffer(argument));
1043 NewFile(view(), argument);
1046 case LFUN_FILE_OPEN:
1050 case LFUN_DROP_LAYOUTS_CHOICE:
1051 owner->getToolbar().openLayoutList();
1054 case LFUN_MENU_OPEN_BY_NAME:
1055 owner->getMenubar().openByName(argument);
1058 // --- lyxserver commands ----------------------------
1060 setMessage(owner->buffer()->fileName());
1061 lyxerr[Debug::INFO] << "FNAME["
1062 << owner->buffer()->fileName()
1067 dispatch_buffer = keyseq.print();
1068 lyxserver->notifyClient(dispatch_buffer);
1071 case LFUN_GOTOFILEROW: {
1074 istringstream is(argument);
1075 is >> file_name >> row;
1076 if (prefixIs(file_name, getTmpDir())) {
1077 // Needed by inverse dvi search. If it is a file
1078 // in tmpdir, call the apropriated function
1079 view()->setBuffer(bufferlist.getBufferFromTmp(file_name));
1081 // Must replace extension of the file to be .lyx
1082 // and get full path
1083 string const s = ChangeExtension(file_name, ".lyx");
1084 // Either change buffer or load the file
1085 if (bufferlist.exists(s)) {
1086 view()->setBuffer(bufferlist.getBuffer(s));
1088 view()->loadLyXFile(s);
1092 view()->setCursorFromRow(row);
1095 // see BufferView_pimpl::center()
1096 view()->updateScrollbar();
1100 case LFUN_GOTO_PARAGRAPH: {
1101 istringstream is(argument);
1104 ParIterator par = owner->buffer()->getParFromID(id);
1105 if (par == owner->buffer()->par_iterator_end()) {
1106 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1107 << id << ']' << endl;
1110 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1111 << " found." << endl;
1115 view()->setCursor(par, 0);
1117 view()->switchKeyMap();
1118 owner->view_state_changed();
1121 // see BufferView_pimpl::center()
1122 view()->updateScrollbar();
1126 case LFUN_DIALOG_SHOW: {
1127 string const name = cmd.getArg(0);
1128 string data = trim(cmd.argument.substr(name.size()));
1130 if (name == "character") {
1131 data = freefont2string();
1133 owner->getDialogs().show("character", data);
1135 else if (name == "document")
1136 owner->getDialogs().showDocument();
1137 else if (name == "preamble")
1138 owner->getDialogs().showPreamble();
1139 else if (name == "preferences")
1140 owner->getDialogs().showPreferences();
1141 else if (name == "print")
1142 owner->getDialogs().showPrint();
1143 else if (name == "spellchecker")
1144 owner->getDialogs().showSpellchecker();
1146 else if (name == "latexlog") {
1147 pair<Buffer::LogType, string> const logfile =
1148 owner->buffer()->getLogName();
1149 switch (logfile.first) {
1150 case Buffer::latexlog:
1153 case Buffer::buildlog:
1157 data += logfile.second;
1158 owner->getDialogs().show("log", data);
1160 else if (name == "vclog") {
1161 string const data = "vc " +
1162 owner->buffer()->lyxvc().getLogFile();
1163 owner->getDialogs().show("log", data);
1166 owner->getDialogs().show(name, data);
1170 case LFUN_DIALOG_SHOW_NEW_INSET: {
1171 string const name = cmd.getArg(0);
1172 string data = trim(cmd.argument.substr(name.size()));
1173 if (name == "bibitem" ||
1175 name == "include" ||
1181 InsetCommandParams p(name);
1182 data = InsetCommandMailer::params2string(name, p);
1183 } else if (name == "box") {
1184 // \c data == "Boxed" || "Frameless" etc
1185 InsetBoxParams p(data);
1186 data = InsetBoxMailer::params2string(p);
1187 } else if (name == "branch") {
1188 InsetBranchParams p;
1189 data = InsetBranchMailer::params2string(p);
1190 } else if (name == "citation") {
1191 InsetCommandParams p("cite");
1192 data = InsetCommandMailer::params2string(name, p);
1193 } else if (name == "ert") {
1194 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1195 } else if (name == "external") {
1196 InsetExternalParams p;
1197 Buffer const & buffer = *owner->buffer();
1198 data = InsetExternalMailer::params2string(p, buffer);
1199 } else if (name == "float") {
1201 data = InsetFloatMailer::params2string(p);
1202 } else if (name == "graphics") {
1203 InsetGraphicsParams p;
1204 Buffer const & buffer = *owner->buffer();
1205 data = InsetGraphicsMailer::params2string(p, buffer);
1206 } else if (name == "note") {
1208 data = InsetNoteMailer::params2string(p);
1209 } else if (name == "vspace") {
1211 data = InsetVSpaceMailer::params2string(space);
1212 } else if (name == "wrap") {
1214 data = InsetWrapMailer::params2string(p);
1216 owner->getDialogs().show(name, data, 0);
1220 case LFUN_DIALOG_SHOW_NEXT_INSET:
1223 case LFUN_DIALOG_UPDATE: {
1224 string const & name = argument;
1225 // Can only update a dialog connected to an existing inset
1226 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1228 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument);
1229 inset->dispatch(view()->cursor(), fr);
1230 } else if (name == "paragraph") {
1231 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1236 case LFUN_DIALOG_HIDE:
1237 Dialogs::hide(argument, 0);
1240 case LFUN_DIALOG_DISCONNECT_INSET:
1241 owner->getDialogs().disconnect(argument);
1244 case LFUN_CHILDOPEN: {
1245 string const filename =
1246 MakeAbsPath(argument, owner->buffer()->filePath());
1247 setMessage(N_("Opening child document ") +
1248 MakeDisplayPath(filename) + "...");
1249 view()->savePosition(0);
1250 string const parentfilename = owner->buffer()->fileName();
1251 if (bufferlist.exists(filename))
1252 view()->setBuffer(bufferlist.getBuffer(filename));
1254 view()->loadLyXFile(filename);
1255 // Set the parent name of the child document.
1256 // This makes insertion of citations and references in the child work,
1257 // when the target is in the parent or another child document.
1258 owner->buffer()->setParentName(parentfilename);
1262 case LFUN_TOGGLECURSORFOLLOW:
1263 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1267 owner->getIntl().KeyMapOn(false);
1270 case LFUN_KMAP_PRIM:
1271 owner->getIntl().KeyMapPrim();
1275 owner->getIntl().KeyMapSec();
1278 case LFUN_KMAP_TOGGLE:
1279 owner->getIntl().ToggleKeyMap();
1285 string rest = split(argument, countstr, ' ');
1286 istringstream is(countstr);
1289 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1290 for (int i = 0; i < count; ++i)
1291 dispatch(lyxaction.lookupFunc(rest));
1296 // argument contains ';'-terminated commands
1297 while (!argument.empty()) {
1299 string rest = split(argument, first, ';');
1300 dispatch(lyxaction.lookupFunc(rest));
1304 case LFUN_SAVEPREFERENCES: {
1305 Path p(user_lyxdir());
1306 lyxrc.write("preferences");
1310 case LFUN_SCREEN_FONT_UPDATE:
1311 // handle the screen font changes.
1312 lyxrc.set_font_norm_type();
1313 lyx_gui::update_fonts();
1314 // All visible buffers will need resize
1318 case LFUN_SET_COLOR: {
1320 string const x11_name = split(argument, lyx_name, ' ');
1321 if (lyx_name.empty() || x11_name.empty()) {
1322 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1327 bool const graphicsbg_changed =
1328 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1329 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1331 LColor::color col = lcolor.getFromLyXName(lyx_name);
1332 if (!lcolor.setColor(col, x11_name)) {
1334 bformat(_("Set-color \"%1$s\" failed "
1335 "- color is undefined or "
1336 "may not be redefined"), lyx_name));
1340 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1342 if (graphicsbg_changed) {
1343 #ifdef WITH_WARNINGS
1344 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1347 lyx::graphics::GCache::get().changeDisplay(true);
1354 owner->message(argument);
1357 case LFUN_TOOLTIPS_TOGGLE:
1358 owner->getDialogs().toggleTooltips();
1361 case LFUN_EXTERNAL_EDIT: {
1362 FuncRequest fr(action, argument);
1363 InsetExternal().dispatch(view()->cursor(), fr);
1368 DispatchResult res = view()->cursor().dispatch(cmd);
1369 if (!res.dispatched());
1370 view()->dispatch(cmd);
1376 if (view()->available()) {
1377 view()->fitCursor();
1379 view()->cursor().updatePos();
1380 // if we executed a mutating lfun, mark the buffer as dirty
1381 if (!getStatus(cmd).disabled()
1382 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)
1383 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly))
1384 view()->buffer()->markDirty();
1387 if (view()->cursor().inTexted()) {
1388 view()->owner()->updateLayoutChoice();
1389 sendDispatchMessage(getMessage(), cmd, verbose);
1394 void LyXFunc::sendDispatchMessage(string const & msg,
1395 FuncRequest const & cmd, bool verbose)
1397 owner->updateMenubar();
1398 owner->updateToolbar();
1400 if (cmd.action == LFUN_SELFINSERT || !verbose) {
1401 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1403 owner->message(msg);
1407 string dispatch_msg = msg;
1408 if (!dispatch_msg.empty())
1409 dispatch_msg += ' ';
1411 string comname = lyxaction.getActionName(cmd.action);
1413 bool argsadded = false;
1415 if (!cmd.argument.empty()) {
1416 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1417 comname += ' ' + cmd.argument;
1422 string const shortcuts = toplevel_keymap->findbinding(cmd);
1424 if (!shortcuts.empty()) {
1425 comname += ": " + shortcuts;
1426 } else if (!argsadded && !cmd.argument.empty()) {
1427 comname += ' ' + cmd.argument;
1430 if (!comname.empty()) {
1431 comname = rtrim(comname);
1432 dispatch_msg += '(' + comname + ')';
1435 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1436 if (!dispatch_msg.empty())
1437 owner->message(dispatch_msg);
1441 void LyXFunc::setupLocalKeymap()
1443 keyseq.stdmap = toplevel_keymap.get();
1444 keyseq.curmap = toplevel_keymap.get();
1445 cancel_meta_seq.stdmap = toplevel_keymap.get();
1446 cancel_meta_seq.curmap = toplevel_keymap.get();
1450 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1452 string initpath = lyxrc.document_path;
1453 string filename(name);
1455 if (view()->available()) {
1456 string const trypath = owner->buffer()->filePath();
1457 // If directory is writeable, use this as default.
1458 if (IsDirWriteable(trypath))
1462 static int newfile_number;
1464 if (filename.empty()) {
1465 filename = AddName(lyxrc.document_path,
1466 "newfile" + tostr(++newfile_number) + ".lyx");
1467 FileInfo fi(filename);
1468 while (bufferlist.exists(filename) || fi.readable()) {
1470 filename = AddName(lyxrc.document_path,
1471 "newfile" + tostr(newfile_number) +
1473 fi.newFile(filename);
1477 // The template stuff
1480 FileDialog fileDlg(_("Select template file"),
1481 LFUN_SELECT_FILE_SYNC,
1482 make_pair(string(_("Documents|#o#O")),
1483 string(lyxrc.document_path)),
1484 make_pair(string(_("Templates|#T#t")),
1485 string(lyxrc.template_path)));
1487 FileDialog::Result result =
1488 fileDlg.open(lyxrc.template_path,
1489 FileFilterList(_("LyX Documents (*.lyx)")),
1492 if (result.first == FileDialog::Later)
1494 if (result.second.empty())
1496 templname = result.second;
1499 view()->newFile(filename, templname, !name.empty());
1503 void LyXFunc::open(string const & fname)
1505 string initpath = lyxrc.document_path;
1507 if (view()->available()) {
1508 string const trypath = owner->buffer()->filePath();
1509 // If directory is writeable, use this as default.
1510 if (IsDirWriteable(trypath))
1516 if (fname.empty()) {
1517 FileDialog fileDlg(_("Select document to open"),
1519 make_pair(string(_("Documents|#o#O")),
1520 string(lyxrc.document_path)),
1521 make_pair(string(_("Examples|#E#e")),
1522 string(AddPath(system_lyxdir(), "examples"))));
1524 FileDialog::Result result =
1525 fileDlg.open(initpath,
1526 FileFilterList(_("LyX Documents (*.lyx)")),
1529 if (result.first == FileDialog::Later)
1532 filename = result.second;
1534 // check selected filename
1535 if (filename.empty()) {
1536 owner->message(_("Canceled."));
1542 // get absolute path of file and add ".lyx" to the filename if
1544 string const fullpath = FileSearch(string(), filename, "lyx");
1545 if (!fullpath.empty()) {
1546 filename = fullpath;
1549 string const disp_fn(MakeDisplayPath(filename));
1551 // if the file doesn't exist, let the user create one
1552 FileInfo const f(filename, true);
1554 // the user specifically chose this name. Believe them.
1555 view()->newFile(filename, "", true);
1559 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1562 if (view()->loadLyXFile(filename)) {
1563 str2 = bformat(_("Document %1$s opened."), disp_fn);
1565 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1567 owner->message(str2);
1571 void LyXFunc::doImport(string const & argument)
1574 string filename = split(argument, format, ' ');
1576 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1577 << " file: " << filename << endl;
1579 // need user interaction
1580 if (filename.empty()) {
1581 string initpath = lyxrc.document_path;
1583 if (view()->available()) {
1584 string const trypath = owner->buffer()->filePath();
1585 // If directory is writeable, use this as default.
1586 if (IsDirWriteable(trypath))
1590 string const text = bformat(_("Select %1$s file to import"),
1591 formats.prettyName(format));
1593 FileDialog fileDlg(text,
1595 make_pair(string(_("Documents|#o#O")),
1596 string(lyxrc.document_path)),
1597 make_pair(string(_("Examples|#E#e")),
1598 string(AddPath(system_lyxdir(), "examples"))));
1600 string const filter = formats.prettyName(format)
1601 + " (*." + formats.extension(format) + ')';
1603 FileDialog::Result result =
1604 fileDlg.open(initpath,
1605 FileFilterList(filter),
1608 if (result.first == FileDialog::Later)
1611 filename = result.second;
1613 // check selected filename
1614 if (filename.empty())
1615 owner->message(_("Canceled."));
1618 if (filename.empty())
1621 // get absolute path of file
1622 filename = MakeAbsPath(filename);
1624 string const lyxfile = ChangeExtension(filename, ".lyx");
1626 // Check if the document already is open
1627 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1628 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1629 owner->message(_("Canceled."));
1634 // if the file exists already, and we didn't do
1635 // -i lyx thefile.lyx, warn
1636 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1637 string const file = MakeDisplayPath(lyxfile, 30);
1639 string text = bformat(_("The document %1$s already exists.\n\n"
1640 "Do you want to over-write that document?"), file);
1641 int const ret = Alert::prompt(_("Over-write document?"),
1642 text, 0, 1, _("&Over-write"), _("&Cancel"));
1645 owner->message(_("Canceled."));
1650 Importer::Import(owner, filename, format);
1654 void LyXFunc::closeBuffer()
1656 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1657 if (bufferlist.empty()) {
1658 // need this otherwise SEGV may occur while
1659 // trying to set variables that don't exist
1660 // since there's no current buffer
1661 owner->getDialogs().hideBufferDependent();
1663 view()->setBuffer(bufferlist.first());
1669 // Each "owner" should have it's own message method. lyxview and
1670 // the minibuffer would use the minibuffer, but lyxserver would
1671 // send an ERROR signal to its client. Alejandro 970603
1672 // This function is bit problematic when it comes to NLS, to make the
1673 // lyx servers client be language indepenent we must not translate
1674 // strings sent to this func.
1675 void LyXFunc::setErrorMessage(string const & m) const
1677 dispatch_buffer = m;
1682 void LyXFunc::setMessage(string const & m) const
1684 dispatch_buffer = m;
1688 void LyXFunc::setStatusMessage(string const & m) const
1694 string const LyXFunc::viewStatusMessage()
1696 // When meta-fake key is pressed, show the key sequence so far + "M-".
1698 return keyseq.print() + "M-";
1700 // Else, when a non-complete key sequence is pressed,
1701 // show the available options.
1702 if (keyseq.length() > 0 && !keyseq.deleted())
1703 return keyseq.printOptions();
1705 if (!view()->available())
1706 return _("Welcome to LyX!");
1708 return view()->cursor().currentState();
1712 BufferView * LyXFunc::view() const
1714 BOOST_ASSERT(owner);
1715 return owner->view().get();
1719 bool LyXFunc::wasMetaKey() const
1721 return (meta_fake_bit != key_modifier::none);