1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
17 #include "bufferlist.h"
19 #include "buffer_funcs.h"
20 #include "BufferView.h"
21 #include "lyxserver.h"
25 #include "LyXAction.h"
31 #include "trans_mgr.h"
34 #include "bufferview_funcs.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/lyx_gui.h"
38 #include "FloatList.h"
42 #include "TextCache.h"
44 #include "undo_funcs.h"
45 #include "ParagraphParameters.h"
47 #include "insets/insetcommand.h"
48 #include "insets/insetexternal.h"
49 #include "insets/insettabular.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/math_cursor.h"
53 #include "mathed/math_inset.h"
55 #include "frontends/FileDialog.h"
56 #include "frontends/Dialogs.h"
57 #include "frontends/Toolbar.h"
58 #include "frontends/Menubar.h"
59 #include "frontends/Alert.h"
61 #include "graphics/GraphicsCache.h"
63 #include "support/lyxalgo.h"
64 #include "support/LAssert.h"
65 #include "support/filetools.h"
66 #include "support/FileInfo.h"
67 #include "support/forkedcontr.h"
68 #include "support/lstrings.h"
69 #include "support/tostr.h"
70 #include "support/path.h"
71 #include "support/lyxfunctional.h"
87 using std::back_inserter;
88 using namespace bv_funcs;
90 extern BufferList bufferlist;
91 extern LyXServer * lyxserver;
92 extern bool selection_possible;
94 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
97 extern tex_accent_struct get_accent(kb_action action);
99 extern void ShowLatexLog();
102 LyXFunc::LyXFunc(LyXView * o)
105 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
106 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
107 meta_fake_bit(key_modifier::none)
113 LyXText * LyXFunc::TEXT(bool flag = true) const
117 return view()->getLyXText();
122 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
124 if (selecting || TEXT(flag)->selection.mark()) {
125 TEXT(flag)->setSelection();
126 if (!TEXT(flag)->isInInset())
127 view()->toggleToggle();
129 view()->update(TEXT(flag), BufferView::SELECT);
131 view()->switchKeyMap();
135 void LyXFunc::handleKeyFunc(kb_action action)
137 char c = encoded_last_key;
139 if (keyseq.length()) {
143 owner->getIntl().getTransManager()
144 .deadkey(c, get_accent(action).accent, TEXT(false));
145 // Need to clear, in case the minibuffer calls these
148 // copied verbatim from do_accent_char
149 view()->update(TEXT(false), BufferView::SELECT);
150 TEXT(false)->selection.cursor = TEXT(false)->cursor;
154 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
155 key_modifier::state state)
159 if (lyxerr.debugging(Debug::KEY)) {
160 lyxerr << "KeySym is "
161 << keysym->getSymbolName()
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 int action = cancel_meta_seq.addkey(keysym, state);
186 lyxerr[Debug::KEY] << "action first set to [" << 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 ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
192 // remove Caps Lock and Mod2 as a modifiers
193 action = keyseq.addkey(keysym, (state | meta_fake_bit));
194 lyxerr[Debug::KEY] << "action now set to ["
195 << 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 (action == LFUN_NOACTION) {
203 action = LFUN_PREFIX;
206 if (lyxerr.debugging(Debug::KEY)) {
207 lyxerr << "Key [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 (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
225 lyxerr[Debug::KEY] << "Trying without shift" << endl;
226 action = keyseq.addkey(keysym, key_modifier::none);
227 lyxerr[Debug::KEY] << "Action now " << action << endl;
230 if (action == LFUN_UNKNOWN_ACTION) {
231 // Hmm, we didn't match any of the keysequences. See
232 // if it's normal insertable text not already covered
234 if (keysym->isText() && keyseq.length() == 1) {
235 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
236 action = LFUN_SELFINSERT;
238 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
239 owner->message(_("Unknown function."));
244 if (action == LFUN_SELFINSERT) {
245 if (encoded_last_key != 0) {
247 arg += encoded_last_key;
249 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
251 lyxerr[Debug::KEY] << "SelfInsert arg[`"
252 << argument << "']" << endl;
260 FuncStatus LyXFunc::getStatus(int ac) const
262 return getStatus(lyxaction.retrieveActionArg(ac));
266 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
269 Buffer * buf = owner->buffer();
271 if (ev.action == LFUN_NOACTION) {
272 setStatusMessage(N_("Nothing to do"));
273 return flag.disabled(true);
277 case LFUN_UNKNOWN_ACTION:
278 #ifndef HAVE_LIBAIKSAURUS
279 case LFUN_THESAURUS_ENTRY:
284 flag |= lyx_gui::getStatus(ev);
287 if (flag.unknown()) {
288 setStatusMessage(N_("Unknown action"));
292 // the default error message if we disable the command
293 setStatusMessage(N_("Command disabled"));
295 // Check whether we need a buffer
296 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
297 // Yes we need a buffer, do we have one?
300 // Can we use a readonly buffer?
301 if (buf->isReadonly() &&
302 !lyxaction.funcHasFlag(ev.action,
303 LyXAction::ReadOnly)) {
305 setStatusMessage(N_("Document is read-only"));
310 setStatusMessage(N_("Command not allowed with"
311 "out any document open"));
312 return flag.disabled(true);
316 UpdatableInset * tli = view()->theLockingInset();
318 // I would really like to avoid having this switch and rather try to
319 // encode this in the function itself.
320 bool disable = false;
323 disable = ev.argument != "custom"
324 && !Exporter::IsExportable(buf, ev.argument);
327 disable = buf->undostack.empty();
330 disable = buf->redostack.empty();
335 UpdatableInset * in = tli;
336 if (in->lyxCode() != Inset::TABULAR_CODE) {
337 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
339 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
344 disable = !mathcursor && !view()->getLyXText()->selection.set();
347 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
350 disable = !Exporter::IsExportable(buf, "program");
353 case LFUN_LAYOUT_TABULAR:
355 || (tli->lyxCode() != Inset::TABULAR_CODE
356 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
360 disable = !changeDepth(view(), TEXT(false), DEC_DEPTH, true);
363 case LFUN_DEPTH_PLUS:
364 disable = !changeDepth(view(), TEXT(false), INC_DEPTH, true);
368 case LFUN_LAYOUT_PARAGRAPH: {
369 Inset * inset = TEXT(false)->cursor.par()->inInset();
370 disable = inset && inset->forceDefaultParagraphs(inset);
374 case LFUN_INSET_OPTARG:
375 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
378 case LFUN_TABULAR_FEATURE:
381 // FIXME: check temporarily disabled
383 char align = mathcursor->valign();
388 if (ev.argument.empty()) {
392 if (!contains("tcb", ev.argument[0])) {
396 flag.setOnOff(ev.argument[0] == align);
400 char align = mathcursor->halign();
405 if (ev.argument.empty()) {
409 if (!contains("lcr", ev.argument[0])) {
413 flag.setOnOff(ev.argument[0] == align);
416 disable = !mathcursor->halign();
422 //ret.disabled(true);
423 if (tli->lyxCode() == Inset::TABULAR_CODE) {
424 ret = static_cast<InsetTabular *>(tli)
425 ->getStatus(ev.argument);
428 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
429 ret = static_cast<InsetTabular *>
430 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
431 ->getStatus(ev.argument);
438 static InsetTabular inset(*owner->buffer(), 1, 1);
442 ret = inset.getStatus(ev.argument);
443 if (ret.onoff(true) || ret.onoff(false))
444 flag.setOnOff(false);
448 case LFUN_VC_REGISTER:
449 disable = buf->lyxvc.inUse();
451 case LFUN_VC_CHECKIN:
452 disable = !buf->lyxvc.inUse() || buf->isReadonly();
454 case LFUN_VC_CHECKOUT:
455 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
459 disable = !buf->lyxvc.inUse();
461 case LFUN_MENURELOAD:
462 disable = buf->isUnnamed() || buf->isClean();
464 case LFUN_BOOKMARK_GOTO:
466 isSavedPosition(strToUnsignedInt(ev.argument));
468 case LFUN_MERGE_CHANGES:
469 case LFUN_ACCEPT_CHANGE:
470 case LFUN_REJECT_CHANGE:
471 case LFUN_ACCEPT_ALL_CHANGES:
472 case LFUN_REJECT_ALL_CHANGES:
473 disable = !buf->params.tracking_changes;
475 case LFUN_INSET_TOGGLE: {
476 LyXText * lt = view()->getLyXText();
477 disable = !(isEditableInset(lt->getInset())
479 && lt->inset_owner->owner()
480 && lt->inset_owner->owner()->isOpen()));
484 case LFUN_INSET_SETTINGS: {
486 UpdatableInset * inset = view()->theLockingInset();
491 // get the innermost inset
492 inset = inset->getLockingInset();
494 // jump back to owner if an InsetText, so
495 // we get back to the InsetTabular or whatever
496 if (inset->lyxCode() == Inset::TEXT_CODE)
497 inset = inset->owner();
499 Inset::Code code = inset->lyxCode();
501 case Inset::TABULAR_CODE:
502 disable = ev.argument != "tabular";
504 case Inset::ERT_CODE:
505 disable = ev.argument != "ert";
507 case Inset::FLOAT_CODE:
508 disable = ev.argument != "float";
510 case Inset::MINIPAGE_CODE:
511 disable = ev.argument != "minipage";
513 case Inset::WRAP_CODE:
514 disable = ev.argument != "wrap";
522 case LFUN_MATH_MUTATE:
524 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
525 flag.setOnOff(false);
530 // we just need to be in math mode to enable that
532 case LFUN_MATH_SPACE:
533 case LFUN_MATH_LIMITS:
534 case LFUN_MATH_NONUMBER:
535 case LFUN_MATH_NUMBER:
536 case LFUN_MATH_EXTERN:
537 disable = !mathcursor;
540 case LFUN_DIALOG_SHOW: {
541 string const name = ev.getArg(0);
543 disable = !(name == "aboutlyx" ||
546 name == "preferences" ||
551 if (name == "print") {
552 disable = !Exporter::IsExportable(buf, "dvi") ||
553 lyxrc.print_command == "none";
554 } else if (name == "character") {
555 UpdatableInset * tli = view()->theLockingInset();
556 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
557 } else if (name == "vclog") {
558 disable = !buf->lyxvc.inUse();
559 } else if (name == "latexlog") {
560 disable = !IsFileReadable(buf->getLogName().second);
569 // the functions which insert insets
570 Inset::Code code = Inset::NO_CODE;
572 case LFUN_DIALOG_SHOW_NEW_INSET:
573 if (ev.argument == "bibitem")
574 code = Inset::BIBITEM_CODE;
575 else if (ev.argument == "bibtex")
576 code = Inset::BIBTEX_CODE;
577 else if (ev.argument == "citation")
578 code = Inset::CITE_CODE;
579 else if (ev.argument == "ert")
580 code = Inset::ERT_CODE;
581 else if (ev.argument == "external")
582 code = Inset::EXTERNAL_CODE;
583 else if (ev.argument == "float")
584 code = Inset::FLOAT_CODE;
585 else if (ev.argument == "graphics")
586 code = Inset::GRAPHICS_CODE;
587 else if (ev.argument == "include")
588 code = Inset::INCLUDE_CODE;
589 else if (ev.argument == "index")
590 code = Inset::INDEX_CODE;
591 else if (ev.argument == "label")
592 code = Inset::LABEL_CODE;
593 else if (ev.argument == "minipage")
594 code = Inset::MINIPAGE_CODE;
595 else if (ev.argument == "ref")
596 code = Inset::REF_CODE;
597 else if (ev.argument == "toc")
598 code = Inset::TOC_CODE;
599 else if (ev.argument == "url")
600 code = Inset::URL_CODE;
601 else if (ev.argument == "wrap")
602 code = Inset::WRAP_CODE;
606 code = Inset::ERT_CODE;
608 case LFUN_INSET_FOOTNOTE:
609 code = Inset::FOOT_CODE;
611 case LFUN_TABULAR_INSERT:
612 code = Inset::TABULAR_CODE;
614 case LFUN_INSET_MARGINAL:
615 code = Inset::MARGIN_CODE;
617 case LFUN_INSET_MINIPAGE:
618 code = Inset::MINIPAGE_CODE;
620 case LFUN_INSET_FLOAT:
621 case LFUN_INSET_WIDE_FLOAT:
622 code = Inset::FLOAT_CODE;
624 case LFUN_INSET_WRAP:
625 code = Inset::WRAP_CODE;
627 case LFUN_FLOAT_LIST:
628 code = Inset::FLOAT_LIST_CODE;
631 case LFUN_INSET_LIST:
632 code = Inset::LIST_CODE;
634 case LFUN_INSET_THEOREM:
635 code = Inset::THEOREM_CODE;
638 case LFUN_INSET_CAPTION:
639 code = Inset::CAPTION_CODE;
641 case LFUN_INSERT_NOTE:
642 code = Inset::NOTE_CODE;
644 case LFUN_INSERT_LABEL:
645 code = Inset::LABEL_CODE;
647 case LFUN_INSET_OPTARG:
648 code = Inset::OPTARG_CODE;
650 case LFUN_ENVIRONMENT_INSERT:
651 code = Inset::MINIPAGE_CODE;
653 case LFUN_INDEX_INSERT:
654 code = Inset::INDEX_CODE;
656 case LFUN_INDEX_PRINT:
657 code = Inset::INDEX_PRINT_CODE;
659 case LFUN_TOC_INSERT:
660 code = Inset::TOC_CODE;
664 code = Inset::URL_CODE;
667 // always allow this, since we will inset a raw quote
668 // if an inset is not allowed.
670 case LFUN_HYPHENATION:
671 case LFUN_LIGATURE_BREAK:
673 case LFUN_MENU_SEPARATOR:
675 case LFUN_END_OF_SENTENCE:
676 code = Inset::SPECIALCHAR_CODE;
678 case LFUN_SPACE_INSERT:
679 // slight hack: we know this is allowed in math mode
681 code = Inset::SPACE_CODE;
686 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
692 // A few general toggles
694 case LFUN_TOOLTIPS_TOGGLE:
695 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
698 case LFUN_READ_ONLY_TOGGLE:
699 flag.setOnOff(buf->isReadonly());
702 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
704 case LFUN_SWITCHBUFFER:
705 // toggle on the current buffer, but do not toggle off
706 // the other ones (is that a good idea?)
707 if (ev.argument == buf->fileName())
710 case LFUN_TRACK_CHANGES:
711 flag.setOnOff(buf->params.tracking_changes);
717 // the font related toggles
719 LyXFont const & font = TEXT(false)->real_current_font;
722 flag.setOnOff(font.emph() == LyXFont::ON);
725 flag.setOnOff(font.noun() == LyXFont::ON);
728 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
731 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
734 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
737 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
743 string tc = mathcursor->getLastCode();
746 flag.setOnOff(tc == "mathbf");
749 flag.setOnOff(tc == "mathsf");
752 flag.setOnOff(tc == "mathcal");
755 flag.setOnOff(tc == "mathrm");
758 flag.setOnOff(tc == "mathtt");
761 flag.setOnOff(tc == "mathbb");
764 flag.setOnOff(tc == "mathnormal");
771 // this one is difficult to get right. As a half-baked
772 // solution, we consider only the first action of the sequence
773 if (ev.action == LFUN_SEQUENCE) {
774 // argument contains ';'-terminated commands
775 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
782 void LyXFunc::dispatch(string const & s, bool verbose)
784 int const action = lyxaction.LookupFunc(s);
786 if (action == LFUN_UNKNOWN_ACTION) {
787 owner->message(bformat(_("Unknown function (%1$s)"), s));
791 dispatch(action, verbose);
795 void LyXFunc::dispatch(int ac, bool verbose)
797 dispatch(lyxaction.retrieveActionArg(ac), verbose);
802 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
804 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
805 <<"] arg[" << ev.argument << ']' << endl;
807 // we have not done anything wrong yet.
809 dispatch_buffer.erase();
811 #ifdef NEW_DISPATCHER
812 // We try do call the most specific dispatcher first:
813 // 1. the lockinginset's dispatch
814 // 2. the bufferview's dispatch
815 // 3. the lyxview's dispatch
818 selection_possible = false;
820 string argument = ev.argument;
821 kb_action action = ev.action;
823 // We cannot use this function here
824 if (getStatus(ev).disabled()) {
825 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
826 << lyxaction.getActionName(action)
827 << " [" << action << "] is disabled at this location"
829 setErrorMessage(getStatusMessage());
830 goto exit_with_message;
833 if (view()->available())
834 view()->hideCursor();
836 if (view()->available() && view()->theLockingInset()) {
837 Inset::RESULT result;
838 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
839 (!keyseq.deleted())))
841 UpdatableInset * inset = view()->theLockingInset();
845 inset->getCursorPos(view(), inset_x, dummy_y);
847 if ((action == LFUN_UNKNOWN_ACTION)
848 && argument.empty()) {
849 argument = encoded_last_key;
852 // the insets can't try to handle this,
853 // a table cell in the dummy position will
854 // lock its insettext, the insettext will
855 // pass it the bufferview, and succeed,
856 // so it will stay not locked. Not good
857 // if we've just done LFUN_ESCAPE (which
858 // injects an LFUN_PARAGRAPH_UPDATE)
859 if (action == LFUN_PARAGRAPH_UPDATE) {
860 view()->dispatch(ev);
861 goto exit_with_message;
864 // Undo/Redo is a bit tricky for insets.
865 if (action == LFUN_UNDO) {
867 goto exit_with_message;
868 } else if (action == LFUN_REDO) {
870 goto exit_with_message;
871 } else if (((result=inset->
872 // Hand-over to inset's own dispatch:
873 localDispatch(FuncRequest(view(), action, argument))) ==
875 (result == DISPATCHED_NOUPDATE))
876 goto exit_with_message;
877 // If UNDISPATCHED, just soldier on
878 else if (result == FINISHED) {
879 owner->clearMessage();
880 goto exit_with_message;
881 // We do not need special RTL handling here:
882 // FINISHED means that the cursor should be
883 // one position after the inset.
884 } else if (result == FINISHED_RIGHT) {
885 TEXT()->cursorRight(view());
886 moveCursorUpdate(true, false);
887 owner->clearMessage();
888 goto exit_with_message;
889 } else if (result == FINISHED_UP) {
890 if (TEXT()->cursor.irow() != TEXT()->rows().begin()) {
892 TEXT()->setCursorFromCoordinates(
893 TEXT()->cursor.ix() + inset_x,
894 TEXT()->cursor.iy() -
895 TEXT()->cursor.irow()->baseline() - 1);
896 TEXT()->cursor.x_fix(TEXT()->cursor.x());
898 TEXT()->cursorUp(view());
900 moveCursorUpdate(true, false);
902 view()->update(TEXT(), BufferView::SELECT);
904 owner->clearMessage();
905 goto exit_with_message;
906 } else if (result == FINISHED_DOWN) {
907 if (boost::next(TEXT()->cursor.irow()) != TEXT()->rows().end()) {
909 TEXT()->setCursorFromCoordinates(
910 TEXT()->cursor.ix() + inset_x,
911 TEXT()->cursor.iy() -
912 TEXT()->cursor.irow()->baseline() +
913 TEXT()->cursor.irow()->height() + 1);
914 TEXT()->cursor.x_fix(TEXT()->cursor.x());
916 TEXT()->cursorDown(view());
919 TEXT()->cursorRight(view());
921 moveCursorUpdate(true, false);
922 owner->clearMessage();
923 goto exit_with_message;
925 #warning I am not sure this is still right, please have a look! (Jug 20020417)
926 else { // result == UNDISPATCHED
927 //setMessage(N_("Text mode"));
929 case LFUN_UNKNOWN_ACTION:
930 case LFUN_BREAKPARAGRAPH:
932 TEXT()->cursorRight(view());
933 view()->switchKeyMap();
934 owner->view_state_changed();
937 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
938 TEXT()->cursorRight(view());
939 moveCursorUpdate(true, false);
940 owner->view_state_changed();
942 goto exit_with_message;
944 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
945 TEXT()->cursorRight(view());
946 moveCursorUpdate(true, false);
947 owner->view_state_changed();
949 goto exit_with_message;
951 if (boost::next(TEXT()->cursor.row()) != TEXT()->rows().end())
952 TEXT()->cursorDown(view());
954 TEXT()->cursorRight(view());
955 moveCursorUpdate(true, false);
956 owner->view_state_changed();
957 goto exit_with_message;
969 if (!view()->available()) break;
970 // this function should be used always [asierra060396]
971 UpdatableInset * tli =
972 view()->theLockingInset();
974 UpdatableInset * lock = tli->getLockingInset();
977 view()->unlockInset(tli);
978 TEXT()->cursorRight(view());
979 moveCursorUpdate(true, false);
980 owner->view_state_changed();
982 tli->unlockInsetInInset(view(),
987 // Tell the paragraph dialog that we changed paragraph
988 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
993 // --- Misc -------------------------------------------
994 case LFUN_WORDFINDFORWARD :
995 case LFUN_WORDFINDBACKWARD : {
996 static string last_search;
997 string searched_string;
999 if (!argument.empty()) {
1000 last_search = argument;
1001 searched_string = argument;
1003 searched_string = last_search;
1005 bool fw = (action == LFUN_WORDFINDFORWARD);
1006 if (!searched_string.empty()) {
1007 lyxfind::LyXFind(view(), searched_string, fw);
1014 if (view()->available() && !view()->theLockingInset()) {
1015 view()->update(TEXT(), BufferView::SELECT);
1017 owner->message(keyseq.printOptions());
1021 // --- Misc -------------------------------------------
1022 case LFUN_EXEC_COMMAND:
1023 owner->focus_command_buffer();
1026 case LFUN_CANCEL: // RVDK_PATCH_5
1028 meta_fake_bit = key_modifier::none;
1029 if (view()->available())
1030 // cancel any selection
1031 dispatch(LFUN_MARK_OFF);
1032 setMessage(N_("Cancel"));
1035 case LFUN_META_FAKE: // RVDK_PATCH_5
1037 meta_fake_bit = key_modifier::alt;
1038 setMessage(keyseq.print());
1042 case LFUN_READ_ONLY_TOGGLE:
1043 if (owner->buffer()->lyxvc.inUse()) {
1044 owner->buffer()->lyxvc.toggleReadOnly();
1046 owner->buffer()->setReadonly(
1047 !owner->buffer()->isReadonly());
1051 case LFUN_CENTER: // this is center and redraw.
1055 // --- Menus -----------------------------------------------
1057 menuNew(argument, false);
1060 case LFUN_MENUNEWTMPLT:
1061 menuNew(argument, true);
1064 case LFUN_CLOSEBUFFER:
1068 case LFUN_MENUWRITE:
1069 if (!owner->buffer()->isUnnamed()) {
1070 string const str = bformat(_("Saving document %1$s..."),
1071 MakeDisplayPath(owner->buffer()->fileName()));
1072 owner->message(str);
1073 MenuWrite(owner->buffer());
1074 owner->message(str + _(" done."));
1076 WriteAs(owner->buffer());
1080 WriteAs(owner->buffer(), argument);
1083 case LFUN_MENURELOAD: {
1084 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1085 string text = bformat(_("Any changes will be lost. Are you sure "
1086 "you want to revert to the saved version of the document %1$s?"), file);
1087 int const ret = Alert::prompt(_("Revert to saved document?"),
1088 text, 0, 1, _("&Revert"), _("&Cancel"));
1096 Exporter::Export(owner->buffer(), argument, true);
1100 Exporter::Preview(owner->buffer(), argument);
1103 case LFUN_BUILDPROG:
1104 Exporter::Export(owner->buffer(), "program", true);
1107 case LFUN_RUNCHKTEX:
1108 owner->buffer()->runChktex();
1112 if (argument == "custom")
1113 owner->getDialogs().showSendto();
1115 Exporter::Export(owner->buffer(), argument, false);
1128 InsetCommandParams p("tableofcontents");
1129 string const data = InsetCommandMailer::params2string("toc", p);
1130 owner->getDialogs().show("toc", data, 0);
1146 case LFUN_DEPTH_MIN:
1147 changeDepth(view(), TEXT(false), DEC_DEPTH, false);
1148 owner->view_state_changed();
1151 case LFUN_DEPTH_PLUS:
1152 changeDepth(view(), TEXT(false), INC_DEPTH, false);
1153 owner->view_state_changed();
1156 case LFUN_FREEFONT_APPLY:
1157 apply_freefont(view());
1160 case LFUN_FREEFONT_UPDATE:
1161 update_and_apply_freefont(view(), argument);
1164 case LFUN_RECONFIGURE:
1165 Reconfigure(view());
1169 case LFUN_FLOATSOPERATE:
1170 if (argument == "openfoot")
1171 view()->allFloats(1,0);
1172 else if (argument == "closefoot")
1173 view()->allFloats(0,0);
1174 else if (argument == "openfig")
1175 view()->allFloats(1,1);
1176 else if (argument == "closefig")
1177 view()->allFloats(0,1);
1180 #ifdef WITH_WARNINGS
1181 //#warning Find another implementation here (or another lyxfunc)!
1184 case LFUN_HELP_OPEN:
1186 string const arg = argument;
1188 setErrorMessage(N_("Missing argument"));
1191 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1192 if (fname.empty()) {
1193 lyxerr << "LyX: unable to find documentation file `"
1194 << arg << "'. Bad installation?" << endl;
1197 owner->message(bformat(_("Opening help file %1$s..."),
1198 MakeDisplayPath(fname)));
1199 view()->loadLyXFile(fname, false);
1203 // --- version control -------------------------------
1204 case LFUN_VC_REGISTER:
1206 if (!owner->buffer()->lyxvc.inUse())
1207 owner->buffer()->lyxvc.registrer();
1211 case LFUN_VC_CHECKIN:
1213 if (owner->buffer()->lyxvc.inUse()
1214 && !owner->buffer()->isReadonly())
1215 owner->buffer()->lyxvc.checkIn();
1219 case LFUN_VC_CHECKOUT:
1221 if (owner->buffer()->lyxvc.inUse()
1222 && owner->buffer()->isReadonly())
1223 owner->buffer()->lyxvc.checkOut();
1227 case LFUN_VC_REVERT:
1229 owner->buffer()->lyxvc.revert();
1235 owner->buffer()->lyxvc.undoLast();
1239 // --- buffers ----------------------------------------
1241 case LFUN_SWITCHBUFFER:
1242 view()->buffer(bufferlist.getBuffer(argument));
1247 // servercmd: argument must be <file>:<template>
1248 Buffer * tmpbuf = NewFile(argument);
1250 view()->buffer(tmpbuf);
1254 case LFUN_FILE_OPEN:
1258 case LFUN_LAYOUT_TABULAR:
1259 if (view()->theLockingInset()) {
1260 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1261 InsetTabular * inset = static_cast<InsetTabular *>
1262 (view()->theLockingInset());
1263 inset->openLayoutDialog(view());
1264 } else if (view()->theLockingInset()->
1265 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1266 InsetTabular * inset = static_cast<InsetTabular *>(
1267 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1268 inset->openLayoutDialog(view());
1273 case LFUN_DROP_LAYOUTS_CHOICE:
1274 owner->getToolbar().openLayoutList();
1277 case LFUN_MENU_OPEN_BY_NAME:
1278 owner->getMenubar().openByName(argument);
1279 break; // RVDK_PATCH_5
1281 // --- lyxserver commands ----------------------------
1285 setMessage(owner->buffer()->fileName());
1286 lyxerr[Debug::INFO] << "FNAME["
1287 << owner->buffer()->fileName()
1293 dispatch_buffer = keyseq.print();
1294 lyxserver->notifyClient(dispatch_buffer);
1298 case LFUN_GOTOFILEROW:
1302 istringstream istr(argument.c_str());
1303 istr >> file_name >> row;
1304 // Must replace extension of the file to be .lyx and get full path
1305 string const s(ChangeExtension(file_name, ".lyx"));
1307 // Either change buffer or load the file
1308 if (bufferlist.exists(s)) {
1309 view()->buffer(bufferlist.getBuffer(s));
1311 view()->loadLyXFile(s);
1314 view()->setCursorFromRow(row);
1317 // see BufferView_pimpl::center()
1318 view()->updateScrollbar();
1322 case LFUN_GOTO_PARAGRAPH:
1324 istringstream istr(argument.c_str());
1328 ParIterator par = owner->buffer()->getParFromID(id);
1329 if (par == owner->buffer()->par_iterator_end()) {
1330 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1331 << id << ']' << endl;
1334 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1335 << " found." << endl;
1338 if (view()->theLockingInset())
1339 view()->unlockInset(view()->theLockingInset());
1340 if (par->inInset()) {
1341 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1342 par->inInset()->localDispatch(cmd);
1345 view()->getLyXText()->setCursor(par.pit(), 0);
1346 view()->switchKeyMap();
1347 owner->view_state_changed();
1350 // see BufferView_pimpl::center()
1351 view()->updateScrollbar();
1355 // --- insert characters ----------------------------------------
1357 // --- Mathed stuff. If we are here, there is no locked inset yet.
1358 case LFUN_MATH_EXTERN:
1359 case LFUN_MATH_NUMBER:
1360 case LFUN_MATH_NONUMBER:
1361 case LFUN_MATH_LIMITS:
1363 setErrorMessage(N_("This is only allowed in math mode!"));
1367 // passthrough hat and underscore outside mathed:
1368 case LFUN_SUBSCRIPT:
1369 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1371 case LFUN_SUPERSCRIPT:
1372 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1375 case LFUN_DIALOG_SHOW: {
1376 string const name = ev.getArg(0);
1377 string data = trim(ev.argument.substr(name.size()));
1379 if (name == "character") {
1380 data = freefont2string();
1382 owner->getDialogs().show("character", data);
1383 } else if (name == "document")
1384 owner->getDialogs().showDocument();
1385 else if (name == "findreplace")
1386 owner->getDialogs().showSearch();
1387 else if (name == "forks")
1388 owner->getDialogs().showForks();
1389 else if (name == "preamble")
1390 owner->getDialogs().showPreamble();
1391 else if (name == "preferences")
1392 owner->getDialogs().showPreferences();
1393 else if (name == "print")
1394 owner->getDialogs().showPrint();
1395 else if (name == "spellchecker")
1396 owner->getDialogs().showSpellchecker();
1398 owner->getDialogs().show(name, data);
1402 case LFUN_DIALOG_SHOW_NEW_INSET: {
1403 string const & name = argument;
1405 if (name == "bibitem" ||
1407 name == "include" ||
1412 InsetCommandParams p(name);
1413 data = InsetCommandMailer::params2string(name, p);
1414 } else if (name == "citation") {
1415 InsetCommandParams p("cite");
1416 data = InsetCommandMailer::params2string(name, p);
1418 owner->getDialogs().show(name, data, 0);
1422 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1426 case LFUN_DIALOG_UPDATE: {
1427 string const & name = argument;
1428 // Can only update a dialog connected to an existing inset
1429 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1431 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1433 inset->localDispatch(fr);
1434 } else if (name == "paragraph") {
1435 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1440 case LFUN_DIALOG_HIDE:
1441 Dialogs::hide(argument, 0);
1444 case LFUN_DIALOG_DISCONNECT_INSET:
1445 owner->getDialogs().disconnect(argument);
1448 case LFUN_CHILDOPEN:
1450 string const filename =
1451 MakeAbsPath(argument,
1452 owner->buffer()->filePath());
1453 setMessage(N_("Opening child document ") +
1454 MakeDisplayPath(filename) + "...");
1455 view()->savePosition(0);
1456 if (bufferlist.exists(filename))
1457 view()->buffer(bufferlist.getBuffer(filename));
1459 view()->loadLyXFile(filename);
1463 case LFUN_TOGGLECURSORFOLLOW:
1464 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1468 owner->getIntl().KeyMapOn(false);
1471 case LFUN_KMAP_PRIM:
1472 owner->getIntl().KeyMapPrim();
1476 owner->getIntl().KeyMapSec();
1479 case LFUN_KMAP_TOGGLE:
1480 owner->getIntl().ToggleKeyMap();
1485 // argument contains ';'-terminated commands
1486 while (!argument.empty()) {
1488 argument = split(argument, first, ';');
1494 case LFUN_SAVEPREFERENCES:
1496 Path p(user_lyxdir);
1497 lyxrc.write("preferences");
1501 case LFUN_SCREEN_FONT_UPDATE:
1503 // handle the screen font changes.
1504 lyxrc.set_font_norm_type();
1505 lyx_gui::update_fonts();
1506 // We also need to empty the textcache so that
1507 // the buffer will be formatted correctly after
1510 // Of course we should only do the resize and the textcache.clear
1511 // if values really changed...but not very important right now. (Lgb)
1512 // All visible buffers will need resize
1518 case LFUN_SET_COLOR:
1521 string const x11_name = split(argument, lyx_name, ' ');
1522 if (lyx_name.empty() || x11_name.empty()) {
1523 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1528 bool const graphicsbg_changed =
1529 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1530 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1532 if (!lcolor.setColor(lyx_name, x11_name)) {
1534 bformat(_("Set-color \"%1$s\" failed "
1535 "- color is undefined or "
1536 "may not be redefined"), lyx_name));
1540 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1542 if (graphicsbg_changed) {
1543 #ifdef WITH_WARNINGS
1544 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1547 grfx::GCache & gc = grfx::GCache::get();
1548 gc.changeDisplay(true);
1557 owner->message(argument);
1560 case LFUN_FORKS_KILL:
1562 if (!isStrInt(argument))
1565 pid_t const pid = strToInt(argument);
1566 ForkedcallsController & fcc = ForkedcallsController::get();
1571 case LFUN_TOOLTIPS_TOGGLE:
1572 owner->getDialogs().toggleTooltips();
1575 case LFUN_EXTERNAL_EDIT: {
1577 .localDispatch(FuncRequest(view(), action, argument));
1582 // Then if it was none of the above
1583 // Trying the BufferView::pimpl dispatch:
1584 if (!view()->dispatch(ev))
1585 lyxerr << "A truly unknown func ["
1586 << lyxaction.getActionName(ev.action) << "]!"
1593 view()->owner()->updateLayoutChoice();
1595 if (view()->available()) {
1596 view()->fitCursor();
1598 // If we executed a mutating lfun, mark the buffer as dirty
1599 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1600 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1601 view()->buffer()->markDirty();
1604 sendDispatchMessage(getMessage(), ev, verbose);
1608 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1610 owner->updateMenubar();
1611 owner->updateToolbar();
1613 if (ev.action == LFUN_SELFINSERT || !verbose) {
1614 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1616 owner->message(msg);
1620 string dispatch_msg = msg;
1621 if (!dispatch_msg.empty())
1622 dispatch_msg += ' ';
1624 string comname = lyxaction.getActionName(ev.action);
1626 int pseudoaction = ev.action;
1627 bool argsadded = false;
1629 if (!ev.argument.empty()) {
1630 // the pseudoaction is useful for the bindings
1631 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1633 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1634 pseudoaction = ev.action;
1636 comname += ' ' + ev.argument;
1641 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1643 if (!shortcuts.empty()) {
1644 comname += ": " + shortcuts;
1645 } else if (!argsadded && !ev.argument.empty()) {
1646 comname += ' ' + ev.argument;
1649 if (!comname.empty()) {
1650 comname = rtrim(comname);
1651 dispatch_msg += '(' + comname + ')';
1654 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1655 if (!dispatch_msg.empty())
1656 owner->message(dispatch_msg);
1660 void LyXFunc::setupLocalKeymap()
1662 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1663 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1667 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1669 string initpath = lyxrc.document_path;
1670 string filename(name);
1672 if (view()->available()) {
1673 string const trypath = owner->buffer()->filePath();
1674 // If directory is writeable, use this as default.
1675 if (IsDirWriteable(trypath))
1679 static int newfile_number;
1681 if (filename.empty()) {
1682 filename = AddName(lyxrc.document_path,
1683 "newfile" + tostr(++newfile_number) + ".lyx");
1684 FileInfo fi(filename);
1685 while (bufferlist.exists(filename) || fi.readable()) {
1687 filename = AddName(lyxrc.document_path,
1688 "newfile" + tostr(newfile_number) +
1690 fi.newFile(filename);
1694 // The template stuff
1697 FileDialog fileDlg(_("Select template file"),
1698 LFUN_SELECT_FILE_SYNC,
1699 make_pair(string(_("Documents|#o#O")),
1700 string(lyxrc.document_path)),
1701 make_pair(string(_("Templates|#T#t")),
1702 string(lyxrc.template_path)));
1704 FileDialog::Result result =
1705 fileDlg.open(lyxrc.template_path,
1706 _("*.lyx| LyX Documents (*.lyx)"));
1708 if (result.first == FileDialog::Later)
1711 string const fname = result.second;
1718 view()->buffer(newFile(filename, templname, !name.empty()));
1722 void LyXFunc::open(string const & fname)
1724 string initpath = lyxrc.document_path;
1726 if (view()->available()) {
1727 string const trypath = owner->buffer()->filePath();
1728 // If directory is writeable, use this as default.
1729 if (IsDirWriteable(trypath))
1735 if (fname.empty()) {
1736 FileDialog fileDlg(_("Select document to open"),
1738 make_pair(string(_("Documents|#o#O")),
1739 string(lyxrc.document_path)),
1740 make_pair(string(_("Examples|#E#e")),
1741 string(AddPath(system_lyxdir, "examples"))));
1743 FileDialog::Result result =
1744 fileDlg.open(initpath,
1745 _("*.lyx| LyX Documents (*.lyx)"));
1747 if (result.first == FileDialog::Later)
1750 filename = result.second;
1752 // check selected filename
1753 if (filename.empty()) {
1754 owner->message(_("Canceled."));
1760 // get absolute path of file and add ".lyx" to the filename if
1762 string const fullpath = FileSearch(string(), filename, "lyx");
1763 if (!fullpath.empty()) {
1764 filename = fullpath;
1767 string const disp_fn(MakeDisplayPath(filename));
1769 // if the file doesn't exist, let the user create one
1770 FileInfo const f(filename, true);
1772 // the user specifically chose this name. Believe them.
1773 Buffer * buffer = newFile(filename, "", true);
1774 view()->buffer(buffer);
1778 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1781 if (view()->loadLyXFile(filename)) {
1782 str2 = bformat(_("Document %1$s opened."), disp_fn);
1784 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1786 owner->message(str2);
1790 void LyXFunc::doImport(string const & argument)
1793 string filename = split(argument, format, ' ');
1795 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1796 << " file: " << filename << endl;
1798 // need user interaction
1799 if (filename.empty()) {
1800 string initpath = lyxrc.document_path;
1802 if (view()->available()) {
1803 string const trypath = owner->buffer()->filePath();
1804 // If directory is writeable, use this as default.
1805 if (IsDirWriteable(trypath))
1809 string const text = bformat(_("Select %1$s file to import"),
1810 formats.prettyName(format));
1812 FileDialog fileDlg(text,
1814 make_pair(string(_("Documents|#o#O")),
1815 string(lyxrc.document_path)),
1816 make_pair(string(_("Examples|#E#e")),
1817 string(AddPath(system_lyxdir, "examples"))));
1819 string const extension = "*." + formats.extension(format)
1820 + "| " + formats.prettyName(format)
1821 + " (*." + formats.extension(format) + ')';
1823 FileDialog::Result result = fileDlg.open(initpath,
1826 if (result.first == FileDialog::Later)
1829 filename = result.second;
1831 // check selected filename
1832 if (filename.empty())
1833 owner->message(_("Canceled."));
1836 if (filename.empty())
1839 // get absolute path of file
1840 filename = MakeAbsPath(filename);
1842 string const lyxfile = ChangeExtension(filename, ".lyx");
1844 // Check if the document already is open
1845 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1846 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1847 owner->message(_("Canceled."));
1852 // if the file exists already, and we didn't do
1853 // -i lyx thefile.lyx, warn
1854 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1855 string const file = MakeDisplayPath(lyxfile, 30);
1857 string text = bformat(_("The document %1$s already exists.\n\n"
1858 "Do you want to over-write that document?"), file);
1859 int const ret = Alert::prompt(_("Over-write document?"),
1860 text, 0, 1, _("&Over-write"), _("&Cancel"));
1863 owner->message(_("Canceled."));
1868 Importer::Import(owner, filename, format);
1872 void LyXFunc::closeBuffer()
1874 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1875 if (bufferlist.empty()) {
1876 // need this otherwise SEGV may occur while trying to
1877 // set variables that don't exist
1878 // since there's no current buffer
1879 owner->getDialogs().hideBufferDependent();
1881 view()->buffer(bufferlist.first());
1887 // Each "owner" should have it's own message method. lyxview and
1888 // the minibuffer would use the minibuffer, but lyxserver would
1889 // send an ERROR signal to its client. Alejandro 970603
1890 // This func is bit problematic when it comes to NLS, to make the
1891 // lyx servers client be language indepenent we must not translate
1892 // strings sent to this func.
1893 void LyXFunc::setErrorMessage(string const & m) const
1895 dispatch_buffer = m;
1900 void LyXFunc::setMessage(string const & m) const
1902 dispatch_buffer = m;
1906 void LyXFunc::setStatusMessage(string const & m) const
1912 string const LyXFunc::view_status_message()
1914 // When meta-fake key is pressed, show the key sequence so far + "M-".
1916 return keyseq.print() + "M-";
1919 // Else, when a non-complete key sequence is pressed,
1920 // show the available options.
1921 if (keyseq.length() > 0 && !keyseq.deleted()) {
1922 return keyseq.printOptions();
1925 if (!view()->available())
1926 return _("Welcome to LyX!");
1928 return currentState(view());
1932 BufferView * LyXFunc::view() const
1935 return owner->view().get();