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"
58 #include "mathed/math_cursor.h"
60 #include "frontends/Alert.h"
61 #include "frontends/Dialogs.h"
62 #include "frontends/FileDialog.h"
63 #include "frontends/lyx_gui.h"
64 #include "frontends/LyXKeySym.h"
65 #include "frontends/LyXView.h"
66 #include "frontends/Menubar.h"
67 #include "frontends/Toolbar.h"
69 #include "support/FileInfo.h"
70 #include "support/filetools.h"
71 #include "support/forkedcontr.h"
72 #include "support/path.h"
73 #include "support/path_defines.h"
74 #include "support/tostr.h"
75 #include "support/std_sstream.h"
76 #include "support/os.h"
78 using bv_funcs::changeDepth;
79 using bv_funcs::currentState;
80 using bv_funcs::DEC_DEPTH;
81 using bv_funcs::freefont2string;
82 using bv_funcs::INC_DEPTH;
84 using lyx::support::AddName;
85 using lyx::support::AddPath;
86 using lyx::support::bformat;
87 using lyx::support::ChangeExtension;
88 using lyx::support::FileInfo;
89 using lyx::support::FileSearch;
90 using lyx::support::ForkedcallsController;
91 using lyx::support::i18nLibFileSearch;
92 using lyx::support::IsDirWriteable;
93 using lyx::support::IsFileReadable;
94 using lyx::support::isStrInt;
95 using lyx::support::MakeAbsPath;
96 using lyx::support::MakeDisplayPath;
97 using lyx::support::Path;
98 using lyx::support::rtrim;
99 using lyx::support::split;
100 using lyx::support::strToInt;
101 using lyx::support::strToUnsignedInt;
102 using lyx::support::system_lyxdir;
103 using lyx::support::token;
104 using lyx::support::trim;
105 using lyx::support::user_lyxdir;
106 using lyx::support::prefixIs;
107 using lyx::support::os::getTmpDir;
110 using std::make_pair;
112 using std::istringstream;
115 extern BufferList bufferlist;
116 extern LyXServer * lyxserver;
117 extern bool selection_possible;
119 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
122 extern tex_accent_struct get_accent(kb_action action);
124 extern void ShowLatexLog();
127 LyXFunc::LyXFunc(LyXView * o)
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::moveCursorUpdate()
139 LyXText * lt = view()->text;
140 if (lt->selection.mark())
143 view()->switchKeyMap();
147 void LyXFunc::handleKeyFunc(kb_action action)
149 char c = encoded_last_key;
151 if (keyseq.length()) {
155 owner->getIntl().getTransManager()
156 .deadkey(c, get_accent(action).accent, view()->getLyXText());
157 // Need to clear, in case the minibuffer calls these
160 // copied verbatim from do_accent_char
162 view()->getLyXText()->selection.cursor = view()->getLyXText()->cursor;
166 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
167 key_modifier::state state)
171 if (lyxerr.debugging(Debug::KEY)) {
172 lyxerr << "KeySym is "
173 << keysym->getSymbolName()
177 // Do nothing if we have nothing (JMarc)
178 if (!keysym->isOK()) {
179 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
184 if (keysym->isModifier()) {
185 lyxerr[Debug::KEY] << "isModifier true" << endl;
189 Encoding const * encoding = view()->getEncoding();
191 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
193 // Do a one-deep top-level lookup for
194 // cancel and meta-fake keys. RVDK_PATCH_5
195 cancel_meta_seq.reset();
197 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
198 lyxerr[Debug::KEY] << "action first set to [" << func.action << ']' << endl;
200 // When not cancel or meta-fake, do the normal lookup.
201 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
202 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
203 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_FAKE)) {
204 // remove Caps Lock and Mod2 as a modifiers
205 func = keyseq.addkey(keysym, (state | meta_fake_bit));
206 lyxerr[Debug::KEY] << "action now set to ["
207 << func.action << ']' << endl;
210 // Dont remove this unless you know what you are doing.
211 meta_fake_bit = key_modifier::none;
213 // can this happen now ?
214 if (func.action == LFUN_NOACTION) {
215 func = FuncRequest(LFUN_PREFIX);
218 if (lyxerr.debugging(Debug::KEY)) {
219 lyxerr << "Key [action="
220 << func.action << "]["
221 << keyseq.print() << ']'
225 // already here we know if it any point in going further
226 // why not return already here if action == -1 and
227 // num_bytes == 0? (Lgb)
229 if (keyseq.length() > 1) {
230 owner->message(keyseq.print());
234 // Maybe user can only reach the key via holding down shift.
235 // Let's see. But only if shift is the only modifier
236 if (func.action == LFUN_UNKNOWN_ACTION &&
237 state == key_modifier::shift) {
238 lyxerr[Debug::KEY] << "Trying without shift" << endl;
239 func = keyseq.addkey(keysym, key_modifier::none);
240 lyxerr[Debug::KEY] << "Action now " << func.action << endl;
243 if (func.action == LFUN_UNKNOWN_ACTION) {
244 // Hmm, we didn't match any of the keysequences. See
245 // if it's normal insertable text not already covered
247 if (keysym->isText() && keyseq.length() == 1) {
248 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
249 func = FuncRequest(LFUN_SELFINSERT);
251 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
252 owner->message(_("Unknown function."));
257 if (func.action == LFUN_SELFINSERT) {
258 if (encoded_last_key != 0) {
260 arg += encoded_last_key;
262 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
264 lyxerr[Debug::KEY] << "SelfInsert arg[`"
265 << argument << "']" << endl;
273 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
276 Buffer * buf = owner->buffer();
278 if (ev.action == LFUN_NOACTION) {
279 setStatusMessage(N_("Nothing to do"));
285 case LFUN_UNKNOWN_ACTION:
286 #ifndef HAVE_LIBAIKSAURUS
287 case LFUN_THESAURUS_ENTRY:
293 flag |= lyx_gui::getStatus(ev);
296 if (flag.unknown()) {
297 setStatusMessage(N_("Unknown action"));
301 // the default error message if we disable the command
302 setStatusMessage(N_("Command disabled"));
304 // Check whether we need a buffer
305 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
306 // Yes we need a buffer, do we have one?
309 // Can we use a readonly buffer?
310 if (buf->isReadonly() &&
311 !lyxaction.funcHasFlag(ev.action,
312 LyXAction::ReadOnly)) {
314 setStatusMessage(N_("Document is read-only"));
319 setStatusMessage(N_("Command not allowed with"
320 "out any document open"));
326 UpdatableInset * tli = view()->cursor().innerInset();
327 InsetTabular * tab = view()->cursor().innerInsetTabular();
329 // I would really like to avoid having this switch and rather try to
330 // encode this in the function itself.
331 bool disable = false;
334 disable = ev.argument != "custom"
335 && !Exporter::IsExportable(*buf, ev.argument);
338 disable = buf->undostack().empty();
341 disable = buf->redostack().empty();
345 if (tab && tab->hasSelection())
348 disable = !mathcursor && !view()->getLyXText()->selection.set();
352 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
356 disable = !Exporter::IsExportable(*buf, "program");
359 case LFUN_LAYOUT_TABULAR:
360 disable = !view()->cursor().innerInsetTabular();
364 disable = !changeDepth(view(), view()->getLyXText(), DEC_DEPTH, true);
367 case LFUN_DEPTH_PLUS:
368 disable = !changeDepth(view(), view()->getLyXText(), INC_DEPTH, true);
372 case LFUN_LAYOUT_PARAGRAPH: {
373 InsetOld * inset = view()->getLyXText()->cursorPar()->inInset();
374 disable = inset && inset->forceDefaultParagraphs(inset);
378 case LFUN_INSET_OPTARG:
379 disable = (view()->getLyXText()->cursorPar()->layout()->optionalargs == 0);
382 case LFUN_TABULAR_FEATURE:
385 // FIXME: check temporarily disabled
387 char align = mathcursor->valign();
392 if (ev.argument.empty()) {
396 if (!contains("tcb", ev.argument[0])) {
400 flag.setOnOff(ev.argument[0] == align);
404 char align = mathcursor->halign();
409 if (ev.argument.empty()) {
413 if (!contains("lcr", ev.argument[0])) {
417 flag.setOnOff(ev.argument[0] == align);
419 disable = !mathcursor->halign();
425 //ret.disabled(true);
426 InsetTabular * tab = view()->cursor().innerInsetTabular();
428 ret = tab->getStatus(ev.argument);
435 static InsetTabular inset(*buf, 1, 1);
437 FuncStatus ret = inset.getStatus(ev.argument);
438 if (ret.onoff(true) || ret.onoff(false))
439 flag.setOnOff(false);
444 case LFUN_VC_REGISTER:
445 disable = buf->lyxvc().inUse();
447 case LFUN_VC_CHECKIN:
448 disable = !buf->lyxvc().inUse() || buf->isReadonly();
450 case LFUN_VC_CHECKOUT:
451 disable = !buf->lyxvc().inUse() || !buf->isReadonly();
455 disable = !buf->lyxvc().inUse();
457 case LFUN_MENURELOAD:
458 disable = buf->isUnnamed() || buf->isClean();
460 case LFUN_BOOKMARK_GOTO:
462 isSavedPosition(strToUnsignedInt(ev.argument));
465 case LFUN_MERGE_CHANGES:
466 case LFUN_ACCEPT_CHANGE:
467 case LFUN_REJECT_CHANGE:
468 case LFUN_ACCEPT_ALL_CHANGES:
469 case LFUN_REJECT_ALL_CHANGES:
470 disable = !buf->params().tracking_changes;
473 case LFUN_INSET_TOGGLE: {
474 LyXText * lt = view()->getLyXText();
475 disable = !(isEditableInset(lt->getInset())
477 && lt->inset_owner->owner()
478 && lt->inset_owner->owner()->isOpen()));
482 case LFUN_INSET_SETTINGS: {
484 UpdatableInset * inset = view()->cursor().innerInset();
489 // jump back to owner if an InsetText, so
490 // we get back to the InsetTabular or whatever
491 if (inset->lyxCode() == InsetOld::TEXT_CODE)
492 inset = inset->owner();
494 InsetOld::Code code = inset->lyxCode();
496 case InsetOld::TABULAR_CODE:
497 disable = ev.argument != "tabular";
499 case InsetOld::ERT_CODE:
500 disable = ev.argument != "ert";
502 case InsetOld::FLOAT_CODE:
503 disable = ev.argument != "float";
505 case InsetOld::MINIPAGE_CODE:
506 disable = ev.argument != "minipage";
508 case InsetOld::WRAP_CODE:
509 disable = ev.argument != "wrap";
511 case InsetOld::NOTE_CODE:
512 disable = ev.argument != "note";
514 case InsetOld::BRANCH_CODE:
515 disable = ev.argument != "branch";
517 case InsetOld::BOX_CODE:
518 disable = ev.argument != "box";
526 case LFUN_MATH_MUTATE:
528 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
529 flag.setOnOff(false);
534 // we just need to be in math mode to enable that
536 case LFUN_MATH_SPACE:
537 case LFUN_MATH_LIMITS:
538 case LFUN_MATH_NONUMBER:
539 case LFUN_MATH_NUMBER:
540 case LFUN_MATH_EXTERN:
541 disable = !mathcursor;
544 case LFUN_DIALOG_SHOW: {
545 string const name = ev.getArg(0);
547 disable = !(name == "aboutlyx" ||
550 name == "preferences" ||
555 if (name == "print") {
556 disable = !Exporter::IsExportable(*buf, "dvi") ||
557 lyxrc.print_command == "none";
558 } else if (name == "character") {
559 UpdatableInset * tli = view()->cursor().innerInset();
560 disable = tli && tli->lyxCode() == InsetOld::ERT_CODE;
561 } else if (name == "vclog") {
562 disable = !buf->lyxvc().inUse();
563 } else if (name == "latexlog") {
564 disable = !IsFileReadable(buf->getLogName().second);
573 // the functions which insert insets
574 InsetOld::Code code = InsetOld::NO_CODE;
576 case LFUN_DIALOG_SHOW_NEW_INSET:
577 if (ev.argument == "bibitem")
578 code = InsetOld::BIBITEM_CODE;
579 else if (ev.argument == "bibtex")
580 code = InsetOld::BIBTEX_CODE;
581 else if (ev.argument == "citation")
582 code = InsetOld::CITE_CODE;
583 else if (ev.argument == "ert")
584 code = InsetOld::ERT_CODE;
585 else if (ev.argument == "external")
586 code = InsetOld::EXTERNAL_CODE;
587 else if (ev.argument == "float")
588 code = InsetOld::FLOAT_CODE;
589 else if (ev.argument == "graphics")
590 code = InsetOld::GRAPHICS_CODE;
591 else if (ev.argument == "include")
592 code = InsetOld::INCLUDE_CODE;
593 else if (ev.argument == "index")
594 code = InsetOld::INDEX_CODE;
595 else if (ev.argument == "label")
596 code = InsetOld::LABEL_CODE;
597 else if (ev.argument == "minipage")
598 code = InsetOld::MINIPAGE_CODE;
599 else if (ev.argument == "ref")
600 code = InsetOld::REF_CODE;
601 else if (ev.argument == "toc")
602 code = InsetOld::TOC_CODE;
603 else if (ev.argument == "url")
604 code = InsetOld::URL_CODE;
605 else if (ev.argument == "wrap")
606 code = InsetOld::WRAP_CODE;
610 code = InsetOld::ERT_CODE;
612 case LFUN_INSET_FOOTNOTE:
613 code = InsetOld::FOOT_CODE;
615 case LFUN_TABULAR_INSERT:
616 code = InsetOld::TABULAR_CODE;
618 case LFUN_INSET_MARGINAL:
619 code = InsetOld::MARGIN_CODE;
621 case LFUN_INSET_MINIPAGE:
622 code = InsetOld::MINIPAGE_CODE;
624 case LFUN_INSET_FLOAT:
625 case LFUN_INSET_WIDE_FLOAT:
626 code = InsetOld::FLOAT_CODE;
628 case LFUN_INSET_WRAP:
629 code = InsetOld::WRAP_CODE;
631 case LFUN_FLOAT_LIST:
632 code = InsetOld::FLOAT_LIST_CODE;
635 case LFUN_INSET_LIST:
636 code = InsetOld::LIST_CODE;
638 case LFUN_INSET_THEOREM:
639 code = InsetOld::THEOREM_CODE;
642 case LFUN_INSET_CAPTION:
643 code = InsetOld::CAPTION_CODE;
645 case LFUN_INSERT_NOTE:
646 code = InsetOld::NOTE_CODE;
648 case LFUN_INSERT_BOX:
649 code = InsetOld::BOX_CODE;
651 case LFUN_INSERT_BRANCH:
652 code = InsetOld::BRANCH_CODE;
653 if (buf->params().branchlist().empty())
656 case LFUN_INSERT_LABEL:
657 code = InsetOld::LABEL_CODE;
659 case LFUN_INSET_OPTARG:
660 code = InsetOld::OPTARG_CODE;
662 case LFUN_ENVIRONMENT_INSERT:
663 code = InsetOld::MINIPAGE_CODE;
665 case LFUN_INDEX_INSERT:
666 code = InsetOld::INDEX_CODE;
668 case LFUN_INDEX_PRINT:
669 code = InsetOld::INDEX_PRINT_CODE;
671 case LFUN_TOC_INSERT:
672 code = InsetOld::TOC_CODE;
676 code = InsetOld::URL_CODE;
679 // always allow this, since we will inset a raw quote
680 // if an inset is not allowed.
682 case LFUN_HYPHENATION:
683 case LFUN_LIGATURE_BREAK:
685 case LFUN_MENU_SEPARATOR:
687 case LFUN_END_OF_SENTENCE:
688 code = InsetOld::SPECIALCHAR_CODE;
690 case LFUN_SPACE_INSERT:
691 // slight hack: we know this is allowed in math mode
693 code = InsetOld::SPACE_CODE;
695 case LFUN_INSET_DIALOG_SHOW: {
696 InsetOld * inset = view()->getLyXText()->getInset();
699 code = inset->lyxCode();
700 if (!(code == InsetOld::INCLUDE_CODE
701 || code == InsetOld::BIBTEX_CODE
702 || code == InsetOld::FLOAT_LIST_CODE
703 || code == InsetOld::TOC_CODE))
711 if (code != InsetOld::NO_CODE && tli && !tli->insetAllowed(code))
717 // A few general toggles
719 case LFUN_TOOLTIPS_TOGGLE:
720 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
723 case LFUN_READ_ONLY_TOGGLE:
724 flag.setOnOff(buf->isReadonly());
727 flag.setOnOff(view()->getLyXText()->cursorPar()->params().startOfAppendix());
729 case LFUN_SWITCHBUFFER:
730 // toggle on the current buffer, but do not toggle off
731 // the other ones (is that a good idea?)
732 if (ev.argument == buf->fileName())
735 case LFUN_TRACK_CHANGES:
736 flag.setOnOff(buf->params().tracking_changes);
743 // the font related toggles
745 LyXFont const & font = view()->getLyXText()->real_current_font;
748 flag.setOnOff(font.emph() == LyXFont::ON);
751 flag.setOnOff(font.noun() == LyXFont::ON);
754 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
757 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
760 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
763 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
769 string tc = mathcursor->getLastCode();
772 flag.setOnOff(tc == "mathbf");
775 flag.setOnOff(tc == "mathsf");
778 flag.setOnOff(tc == "mathcal");
781 flag.setOnOff(tc == "mathrm");
784 flag.setOnOff(tc == "mathtt");
787 flag.setOnOff(tc == "mathbb");
790 flag.setOnOff(tc == "mathnormal");
798 // this one is difficult to get right. As a half-baked
799 // solution, we consider only the first action of the sequence
800 if (ev.action == LFUN_SEQUENCE) {
801 // argument contains ';'-terminated commands
802 #warning LyXAction arguments not handled here.
803 flag = getStatus(FuncRequest(lyxaction.lookupFunc(token(ev.argument, ';', 0))));
812 bool ensureBufferClean(BufferView * bv)
814 Buffer & buf = *bv->buffer();
818 string const file = MakeDisplayPath(buf.fileName(), 30);
819 string text = bformat(_("The document %1$s has unsaved "
820 "changes.\n\nDo you want to save "
821 "the document?"), file);
822 int const ret = Alert::prompt(_("Save changed document?"),
823 text, 0, 1, _("&Save"),
827 bv->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
829 return buf.isClean();
835 void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
837 string argument = func.argument;
838 kb_action action = func.action;
840 //lyxerr[Debug::ACTION] << "LyXFunc::dispatch: cmd: " << func << endl;
841 lyxerr << "LyXFunc::dispatch: cmd: " << func << endl;
843 // we have not done anything wrong yet.
845 dispatch_buffer.erase();
847 #ifdef NEW_DISPATCHER
848 // We try do call the most specific dispatcher first:
849 // 1. the lockinginset's dispatch
850 // 2. the bufferview's dispatch
851 // 3. the lyxview's dispatch
854 selection_possible = false;
856 // We cannot use this function here
857 if (getStatus(func).disabled()) {
858 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
859 << lyxaction.getActionName(action)
860 << " [" << action << "] is disabled at this location"
862 setErrorMessage(getStatusMessage());
863 goto exit_with_message;
866 if (view()->available())
867 view()->hideCursor();
872 if (!view()->available())
874 view()->cursor().pop();
875 // Tell the paragraph dialog that we changed paragraph
876 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
880 case LFUN_WORDFINDFORWARD:
881 case LFUN_WORDFINDBACKWARD: {
882 static string last_search;
883 string searched_string;
885 if (!argument.empty()) {
886 last_search = argument;
887 searched_string = argument;
889 searched_string = last_search;
891 bool fw = (action == LFUN_WORDFINDFORWARD);
892 if (!searched_string.empty())
893 lyx::find::find(view(), searched_string,
899 if (view()->available())
901 owner->message(keyseq.printOptions());
904 // --- Misc -------------------------------------------
905 case LFUN_EXEC_COMMAND:
906 owner->focus_command_buffer();
911 meta_fake_bit = key_modifier::none;
912 if (view()->available())
913 // cancel any selection
914 dispatch(FuncRequest(LFUN_MARK_OFF));
915 setMessage(N_("Cancel"));
919 meta_fake_bit = key_modifier::alt;
920 setMessage(keyseq.print());
923 case LFUN_READ_ONLY_TOGGLE:
924 if (owner->buffer()->lyxvc().inUse())
925 owner->buffer()->lyxvc().toggleReadOnly();
927 owner->buffer()->setReadonly(
928 !owner->buffer()->isReadonly());
931 case LFUN_CENTER: // this is center and redraw.
935 // --- Menus -----------------------------------------------
937 menuNew(argument, false);
940 case LFUN_MENUNEWTMPLT:
941 menuNew(argument, true);
944 case LFUN_CLOSEBUFFER:
949 if (!owner->buffer()->isUnnamed()) {
950 string const str = bformat(_("Saving document %1$s..."),
951 MakeDisplayPath(owner->buffer()->fileName()));
953 MenuWrite(owner->buffer());
954 owner->message(str + _(" done."));
956 WriteAs(owner->buffer());
960 WriteAs(owner->buffer(), argument);
963 case LFUN_MENURELOAD: {
964 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
965 string text = bformat(_("Any changes will be lost. Are you sure "
966 "you want to revert to the saved version of the document %1$s?"), file);
967 int const ret = Alert::prompt(_("Revert to saved document?"),
968 text, 0, 1, _("&Revert"), _("&Cancel"));
976 Exporter::Export(owner->buffer(), argument, true);
977 view()->showErrorList(BufferFormat(*owner->buffer()));
981 Exporter::Preview(owner->buffer(), argument);
982 view()->showErrorList(BufferFormat(*owner->buffer()));
986 Exporter::Export(owner->buffer(), "program", true);
987 view()->showErrorList(_("Build"));
991 owner->buffer()->runChktex();
992 view()->showErrorList(_("ChkTeX"));
996 if (argument == "custom")
997 owner->getDialogs().showSendto();
999 Exporter::Export(owner->buffer(), argument, false);
1000 view()->showErrorList(BufferFormat(*owner->buffer()));
1012 case LFUN_TOCVIEW: {
1013 InsetCommandParams p("tableofcontents");
1014 string const data = InsetCommandMailer::params2string("toc", p);
1015 owner->getDialogs().show("toc", data, 0);
1031 case LFUN_RECONFIGURE:
1032 Reconfigure(view());
1035 case LFUN_HELP_OPEN: {
1036 string const arg = argument;
1038 setErrorMessage(N_("Missing argument"));
1041 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1042 if (fname.empty()) {
1043 lyxerr << "LyX: unable to find documentation file `"
1044 << arg << "'. Bad installation?" << endl;
1047 owner->message(bformat(_("Opening help file %1$s..."),
1048 MakeDisplayPath(fname)));
1049 view()->loadLyXFile(fname, false);
1053 // --- version control -------------------------------
1054 case LFUN_VC_REGISTER:
1055 if (!ensureBufferClean(view()))
1057 if (!owner->buffer()->lyxvc().inUse()) {
1058 owner->buffer()->lyxvc().registrer();
1063 case LFUN_VC_CHECKIN:
1064 if (!ensureBufferClean(view()))
1066 if (owner->buffer()->lyxvc().inUse()
1067 && !owner->buffer()->isReadonly()) {
1068 owner->buffer()->lyxvc().checkIn();
1073 case LFUN_VC_CHECKOUT:
1074 if (!ensureBufferClean(view()))
1076 if (owner->buffer()->lyxvc().inUse()
1077 && owner->buffer()->isReadonly()) {
1078 owner->buffer()->lyxvc().checkOut();
1083 case LFUN_VC_REVERT:
1084 owner->buffer()->lyxvc().revert();
1089 owner->buffer()->lyxvc().undoLast();
1093 // --- buffers ----------------------------------------
1094 case LFUN_SWITCHBUFFER:
1095 view()->buffer(bufferlist.getBuffer(argument));
1099 NewFile(view(), argument);
1102 case LFUN_FILE_OPEN:
1106 case LFUN_LAYOUT_TABULAR:
1107 if (InsetTabular * tab = view()->cursor().innerInsetTabular())
1108 tab->openLayoutDialog(view());
1111 case LFUN_DROP_LAYOUTS_CHOICE:
1112 owner->getToolbar().openLayoutList();
1115 case LFUN_MENU_OPEN_BY_NAME:
1116 owner->getMenubar().openByName(argument);
1119 // --- lyxserver commands ----------------------------
1121 setMessage(owner->buffer()->fileName());
1122 lyxerr[Debug::INFO] << "FNAME["
1123 << owner->buffer()->fileName()
1128 dispatch_buffer = keyseq.print();
1129 lyxserver->notifyClient(dispatch_buffer);
1132 case LFUN_GOTOFILEROW: {
1135 istringstream is(argument);
1136 is >> file_name >> row;
1137 if (prefixIs(file_name, getTmpDir())) {
1138 // Needed by inverse dvi search. If it is a file
1139 // in tmpdir, call the apropriated function
1140 view()->buffer(bufferlist.getBufferFromTmp(file_name));
1142 // Must replace extension of the file to be .lyx
1143 // and get full path
1144 string const s = ChangeExtension(file_name, ".lyx");
1145 // Either change buffer or load the file
1146 if (bufferlist.exists(s)) {
1147 view()->buffer(bufferlist.getBuffer(s));
1149 view()->loadLyXFile(s);
1153 view()->setCursorFromRow(row);
1156 // see BufferView_pimpl::center()
1157 view()->updateScrollbar();
1161 case LFUN_GOTO_PARAGRAPH: {
1162 istringstream is(argument);
1165 ParIterator par = owner->buffer()->getParFromID(id);
1166 if (par == owner->buffer()->par_iterator_end()) {
1167 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1168 << id << ']' << endl;
1171 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1172 << " found." << endl;
1175 par.lockPath(view());
1176 LyXText * lt = par.text(view());
1179 lt->setCursor(par.pit(), 0);
1180 view()->switchKeyMap();
1181 owner->view_state_changed();
1184 // see BufferView_pimpl::center()
1185 view()->updateScrollbar();
1189 // --- Mathed stuff. If we are here, there is no locked inset yet.
1190 case LFUN_MATH_EXTERN:
1191 case LFUN_MATH_NUMBER:
1192 case LFUN_MATH_NONUMBER:
1193 case LFUN_MATH_LIMITS:
1194 setErrorMessage(N_("This is only allowed in math mode!"));
1197 // passthrough hat and underscore outside mathed:
1198 case LFUN_SUBSCRIPT:
1199 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1202 case LFUN_SUPERSCRIPT:
1203 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1206 case LFUN_DIALOG_SHOW: {
1207 string const name = func.getArg(0);
1208 string data = trim(func.argument.substr(name.size()));
1210 if (name == "character") {
1211 data = freefont2string();
1213 owner->getDialogs().show("character", data);
1214 } else if (name == "document")
1215 owner->getDialogs().showDocument();
1216 else if (name == "findreplace")
1217 owner->getDialogs().showSearch();
1218 else if (name == "forks")
1219 owner->getDialogs().showForks();
1220 else if (name == "preamble")
1221 owner->getDialogs().showPreamble();
1222 else if (name == "preferences")
1223 owner->getDialogs().showPreferences();
1224 else if (name == "print")
1225 owner->getDialogs().showPrint();
1226 else if (name == "spellchecker")
1227 owner->getDialogs().showSpellchecker();
1229 owner->getDialogs().show(name, data);
1233 case LFUN_DIALOG_SHOW_NEW_INSET: {
1234 string const & name = argument;
1236 if (name == "bibitem" ||
1238 name == "include" ||
1243 InsetCommandParams p(name);
1244 data = InsetCommandMailer::params2string(name, p);
1245 } else if (name == "citation") {
1246 InsetCommandParams p("cite");
1247 data = InsetCommandMailer::params2string(name, p);
1249 owner->getDialogs().show(name, data, 0);
1253 case LFUN_DIALOG_SHOW_NEXT_INSET:
1256 case LFUN_INSET_DIALOG_SHOW: {
1257 InsetOld * inset = view()->getLyXText()->getInset();
1259 inset->dispatch(FuncRequest(view(), LFUN_INSET_DIALOG_SHOW));
1263 case LFUN_DIALOG_UPDATE: {
1264 string const & name = argument;
1265 // Can only update a dialog connected to an existing inset
1266 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1268 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1270 inset->dispatch(fr);
1271 } else if (name == "paragraph") {
1272 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1277 case LFUN_DIALOG_HIDE:
1278 Dialogs::hide(argument, 0);
1281 case LFUN_DIALOG_DISCONNECT_INSET:
1282 owner->getDialogs().disconnect(argument);
1285 case LFUN_CHILDOPEN: {
1286 string const filename =
1287 MakeAbsPath(argument, owner->buffer()->filePath());
1288 setMessage(N_("Opening child document ") +
1289 MakeDisplayPath(filename) + "...");
1290 view()->savePosition(0);
1291 if (bufferlist.exists(filename))
1292 view()->buffer(bufferlist.getBuffer(filename));
1294 view()->loadLyXFile(filename);
1298 case LFUN_TOGGLECURSORFOLLOW:
1299 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1303 owner->getIntl().KeyMapOn(false);
1306 case LFUN_KMAP_PRIM:
1307 owner->getIntl().KeyMapPrim();
1311 owner->getIntl().KeyMapSec();
1314 case LFUN_KMAP_TOGGLE:
1315 owner->getIntl().ToggleKeyMap();
1321 argument = split(argument, countstr, ' ');
1322 istringstream is(countstr);
1325 lyxerr << "repeat: count: " << count << " cmd: " << argument << endl;
1326 for (int i = 0; i < count; ++i)
1327 dispatch(lyxaction.lookupFunc(argument));
1332 // argument contains ';'-terminated commands
1333 while (!argument.empty()) {
1335 argument = split(argument, first, ';');
1336 dispatch(lyxaction.lookupFunc(first));
1340 case LFUN_SAVEPREFERENCES: {
1341 Path p(user_lyxdir());
1342 lyxrc.write("preferences");
1346 case LFUN_SCREEN_FONT_UPDATE:
1347 // handle the screen font changes.
1348 lyxrc.set_font_norm_type();
1349 lyx_gui::update_fonts();
1350 // All visible buffers will need resize
1355 case LFUN_SET_COLOR: {
1357 string const x11_name = split(argument, lyx_name, ' ');
1358 if (lyx_name.empty() || x11_name.empty()) {
1359 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1364 bool const graphicsbg_changed =
1365 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1366 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1368 if (!lcolor.setColor(lyx_name, x11_name)) {
1370 bformat(_("Set-color \"%1$s\" failed "
1371 "- color is undefined or "
1372 "may not be redefined"), lyx_name));
1376 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1378 if (graphicsbg_changed) {
1379 #ifdef WITH_WARNINGS
1380 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1383 lyx::graphics::GCache::get().changeDisplay(true);
1392 owner->message(argument);
1395 case LFUN_FORKS_KILL:
1396 if (isStrInt(argument)) {
1397 pid_t const pid = strToInt(argument);
1398 ForkedcallsController::get().kill(pid);
1402 case LFUN_TOOLTIPS_TOGGLE:
1403 owner->getDialogs().toggleTooltips();
1406 case LFUN_EXTERNAL_EDIT:
1407 InsetExternal().dispatch(FuncRequest(view(), action, argument));
1411 DispatchResult result =
1412 view()->cursor().dispatch(FuncRequest(func, view()));
1413 if (result.dispatched()) {
1414 if (result.update())
1416 lyxerr << "dispatched by Cursor::dispatch()" << endl;
1418 lyxerr << "### NOT DISPATCHED BY Cursor::dispatch() ###" << endl;
1426 view()->owner()->updateLayoutChoice();
1428 if (view()->available()) {
1429 if (view()->fitCursor()) {
1430 //lyxerr << "LyXFunc->fitCursor->update" << endl;
1434 // If we executed a mutating lfun, mark the buffer as dirty
1435 if (!getStatus(func).disabled()
1436 && !lyxaction.funcHasFlag(func.action, LyXAction::NoBuffer)
1437 && !lyxaction.funcHasFlag(func.action, LyXAction::ReadOnly))
1438 view()->buffer()->markDirty();
1441 sendDispatchMessage(getMessage(), func, verbose);
1445 void LyXFunc::sendDispatchMessage(string const & msg,
1446 FuncRequest const & func, bool verbose)
1448 owner->updateMenubar();
1449 owner->updateToolbar();
1451 if (func.action == LFUN_SELFINSERT || !verbose) {
1452 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1454 owner->message(msg);
1458 string dispatch_msg = msg;
1459 if (!dispatch_msg.empty())
1460 dispatch_msg += ' ';
1462 string comname = lyxaction.getActionName(func.action);
1464 bool argsadded = false;
1466 if (!func.argument.empty()) {
1467 if (func.action != LFUN_UNKNOWN_ACTION) {
1468 comname += ' ' + func.argument;
1473 string const shortcuts = toplevel_keymap->findbinding(func);
1475 if (!shortcuts.empty()) {
1476 comname += ": " + shortcuts;
1477 } else if (!argsadded && !func.argument.empty()) {
1478 comname += ' ' + func.argument;
1481 if (!comname.empty()) {
1482 comname = rtrim(comname);
1483 dispatch_msg += '(' + comname + ')';
1486 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1487 if (!dispatch_msg.empty())
1488 owner->message(dispatch_msg);
1492 void LyXFunc::setupLocalKeymap()
1494 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1495 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1499 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1501 string initpath = lyxrc.document_path;
1502 string filename(name);
1504 if (view()->available()) {
1505 string const trypath = owner->buffer()->filePath();
1506 // If directory is writeable, use this as default.
1507 if (IsDirWriteable(trypath))
1511 static int newfile_number;
1513 if (filename.empty()) {
1514 filename = AddName(lyxrc.document_path,
1515 "newfile" + tostr(++newfile_number) + ".lyx");
1516 FileInfo fi(filename);
1517 while (bufferlist.exists(filename) || fi.readable()) {
1519 filename = AddName(lyxrc.document_path,
1520 "newfile" + tostr(newfile_number) +
1522 fi.newFile(filename);
1526 // The template stuff
1529 FileDialog fileDlg(_("Select template file"),
1530 LFUN_SELECT_FILE_SYNC,
1531 make_pair(string(_("Documents|#o#O")),
1532 string(lyxrc.document_path)),
1533 make_pair(string(_("Templates|#T#t")),
1534 string(lyxrc.template_path)));
1536 FileDialog::Result result =
1537 fileDlg.open(lyxrc.template_path,
1538 _("*.lyx| LyX Documents (*.lyx)"));
1540 if (result.first == FileDialog::Later)
1543 string const fname = result.second;
1550 view()->newFile(filename, templname, !name.empty());
1554 void LyXFunc::open(string const & fname)
1556 string initpath = lyxrc.document_path;
1558 if (view()->available()) {
1559 string const trypath = owner->buffer()->filePath();
1560 // If directory is writeable, use this as default.
1561 if (IsDirWriteable(trypath))
1567 if (fname.empty()) {
1568 FileDialog fileDlg(_("Select document to open"),
1570 make_pair(string(_("Documents|#o#O")),
1571 string(lyxrc.document_path)),
1572 make_pair(string(_("Examples|#E#e")),
1573 string(AddPath(system_lyxdir(), "examples"))));
1575 FileDialog::Result result =
1576 fileDlg.open(initpath,
1577 _("*.lyx| LyX Documents (*.lyx)"));
1579 if (result.first == FileDialog::Later)
1582 filename = result.second;
1584 // check selected filename
1585 if (filename.empty()) {
1586 owner->message(_("Canceled."));
1592 // get absolute path of file and add ".lyx" to the filename if
1594 string const fullpath = FileSearch(string(), filename, "lyx");
1595 if (!fullpath.empty()) {
1596 filename = fullpath;
1599 string const disp_fn(MakeDisplayPath(filename));
1601 // if the file doesn't exist, let the user create one
1602 FileInfo const f(filename, true);
1604 // the user specifically chose this name. Believe them.
1605 view()->newFile(filename, "", true);
1609 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1612 if (view()->loadLyXFile(filename)) {
1613 str2 = bformat(_("Document %1$s opened."), disp_fn);
1615 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1617 owner->message(str2);
1621 void LyXFunc::doImport(string const & argument)
1624 string filename = split(argument, format, ' ');
1626 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1627 << " file: " << filename << endl;
1629 // need user interaction
1630 if (filename.empty()) {
1631 string initpath = lyxrc.document_path;
1633 if (view()->available()) {
1634 string const trypath = owner->buffer()->filePath();
1635 // If directory is writeable, use this as default.
1636 if (IsDirWriteable(trypath))
1640 string const text = bformat(_("Select %1$s file to import"),
1641 formats.prettyName(format));
1643 FileDialog fileDlg(text,
1645 make_pair(string(_("Documents|#o#O")),
1646 string(lyxrc.document_path)),
1647 make_pair(string(_("Examples|#E#e")),
1648 string(AddPath(system_lyxdir(), "examples"))));
1650 string const extension = "*." + formats.extension(format)
1651 + "| " + formats.prettyName(format)
1652 + " (*." + formats.extension(format) + ')';
1654 FileDialog::Result result = fileDlg.open(initpath,
1657 if (result.first == FileDialog::Later)
1660 filename = result.second;
1662 // check selected filename
1663 if (filename.empty())
1664 owner->message(_("Canceled."));
1667 if (filename.empty())
1670 // get absolute path of file
1671 filename = MakeAbsPath(filename);
1673 string const lyxfile = ChangeExtension(filename, ".lyx");
1675 // Check if the document already is open
1676 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1677 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1678 owner->message(_("Canceled."));
1683 // if the file exists already, and we didn't do
1684 // -i lyx thefile.lyx, warn
1685 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1686 string const file = MakeDisplayPath(lyxfile, 30);
1688 string text = bformat(_("The document %1$s already exists.\n\n"
1689 "Do you want to over-write that document?"), file);
1690 int const ret = Alert::prompt(_("Over-write document?"),
1691 text, 0, 1, _("&Over-write"), _("&Cancel"));
1694 owner->message(_("Canceled."));
1699 Importer::Import(owner, filename, format);
1703 void LyXFunc::closeBuffer()
1705 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1706 if (bufferlist.empty()) {
1707 // need this otherwise SEGV may occur while
1708 // trying to set variables that don't exist
1709 // since there's no current buffer
1710 owner->getDialogs().hideBufferDependent();
1712 view()->buffer(bufferlist.first());
1718 // Each "owner" should have it's own message method. lyxview and
1719 // the minibuffer would use the minibuffer, but lyxserver would
1720 // send an ERROR signal to its client. Alejandro 970603
1721 // This func is bit problematic when it comes to NLS, to make the
1722 // lyx servers client be language indepenent we must not translate
1723 // strings sent to this func.
1724 void LyXFunc::setErrorMessage(string const & m) const
1726 dispatch_buffer = m;
1731 void LyXFunc::setMessage(string const & m) const
1733 dispatch_buffer = m;
1737 void LyXFunc::setStatusMessage(string const & m) const
1743 string const LyXFunc::view_status_message()
1745 // When meta-fake key is pressed, show the key sequence so far + "M-".
1747 return keyseq.print() + "M-";
1750 // Else, when a non-complete key sequence is pressed,
1751 // show the available options.
1752 if (keyseq.length() > 0 && !keyseq.deleted()) {
1753 return keyseq.printOptions();
1756 if (!view()->available())
1757 return _("Welcome to LyX!");
1759 return currentState(view());
1763 BufferView * LyXFunc::view() const
1765 BOOST_ASSERT(owner);
1766 return owner->view().get();
1770 bool LyXFunc::wasMetaKey() const
1772 return (meta_fake_bit != key_modifier::none);