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 "BufferView.h"
20 #include "lyxserver.h"
24 #include "LyXAction.h"
30 #include "trans_mgr.h"
33 #include "bufferview_funcs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/lyx_gui.h"
37 #include "FloatList.h"
41 #include "TextCache.h"
43 #include "undo_funcs.h"
44 #include "ParagraphParameters.h"
46 #include "insets/insetcommand.h"
47 #include "insets/insetexternal.h"
48 #include "insets/insettabular.h"
50 #include "mathed/formulamacro.h"
51 #include "mathed/math_cursor.h"
52 #include "mathed/math_inset.h"
54 #include "frontends/FileDialog.h"
55 #include "frontends/Dialogs.h"
56 #include "frontends/Toolbar.h"
57 #include "frontends/Menubar.h"
58 #include "frontends/Alert.h"
60 #include "graphics/GraphicsCache.h"
62 #include "support/lyxalgo.h"
63 #include "support/LAssert.h"
64 #include "support/filetools.h"
65 #include "support/FileInfo.h"
66 #include "support/forkedcontr.h"
67 #include "support/lstrings.h"
68 #include "support/tostr.h"
69 #include "support/path.h"
70 #include "support/lyxfunctional.h"
86 using std::back_inserter;
87 using namespace bv_funcs;
89 extern BufferList bufferlist;
90 extern LyXServer * lyxserver;
91 extern bool selection_possible;
93 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
96 extern tex_accent_struct get_accent(kb_action action);
98 extern void ShowLatexLog();
101 LyXFunc::LyXFunc(LyXView * o)
104 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
105 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
106 meta_fake_bit(key_modifier::none)
112 LyXText * LyXFunc::TEXT(bool flag = true) const
116 return view()->getLyXText();
121 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
123 if (selecting || TEXT(flag)->selection.mark()) {
124 TEXT(flag)->setSelection();
125 if (!TEXT(flag)->isInInset())
126 view()->toggleToggle();
128 view()->update(TEXT(flag), BufferView::SELECT);
130 view()->switchKeyMap();
134 void LyXFunc::handleKeyFunc(kb_action action)
136 char c = encoded_last_key;
138 if (keyseq.length()) {
142 owner->getIntl().getTransManager()
143 .deadkey(c, get_accent(action).accent, TEXT(false));
144 // Need to clear, in case the minibuffer calls these
147 // copied verbatim from do_accent_char
148 view()->update(TEXT(false), BufferView::SELECT);
149 TEXT(false)->selection.cursor = TEXT(false)->cursor;
153 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
154 key_modifier::state state)
158 if (lyxerr.debugging(Debug::KEY)) {
159 lyxerr << "KeySym is "
160 << keysym->getSymbolName()
164 // Do nothing if we have nothing (JMarc)
165 if (!keysym->isOK()) {
166 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
171 if (keysym->isModifier()) {
172 lyxerr[Debug::KEY] << "isModifier true" << endl;
176 Encoding const * encoding = view()->getEncoding();
178 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
180 // Do a one-deep top-level lookup for
181 // cancel and meta-fake keys. RVDK_PATCH_5
182 cancel_meta_seq.reset();
184 int action = cancel_meta_seq.addkey(keysym, state);
185 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
187 // When not cancel or meta-fake, do the normal lookup.
188 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
189 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
190 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
191 // remove Caps Lock and Mod2 as a modifiers
192 action = keyseq.addkey(keysym, (state | meta_fake_bit));
193 lyxerr[Debug::KEY] << "action now set to ["
194 << action << ']' << endl;
197 // Dont remove this unless you know what you are doing.
198 meta_fake_bit = key_modifier::none;
200 // can this happen now ?
201 if (action == LFUN_NOACTION) {
202 action = LFUN_PREFIX;
205 if (lyxerr.debugging(Debug::KEY)) {
206 lyxerr << "Key [action="
208 << keyseq.print() << ']'
212 // already here we know if it any point in going further
213 // why not return already here if action == -1 and
214 // num_bytes == 0? (Lgb)
216 if (keyseq.length() > 1) {
217 owner->message(keyseq.print());
221 // Maybe user can only reach the key via holding down shift.
222 // Let's see. But only if shift is the only modifier
223 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
224 lyxerr[Debug::KEY] << "Trying without shift" << endl;
225 action = keyseq.addkey(keysym, key_modifier::none);
226 lyxerr[Debug::KEY] << "Action now " << action << endl;
229 if (action == LFUN_UNKNOWN_ACTION) {
230 // Hmm, we didn't match any of the keysequences. See
231 // if it's normal insertable text not already covered
233 if (keysym->isText() && keyseq.length() == 1) {
234 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
235 action = LFUN_SELFINSERT;
237 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
238 owner->message(_("Unknown function."));
243 if (action == LFUN_SELFINSERT) {
244 if (encoded_last_key != 0) {
246 arg += encoded_last_key;
248 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
250 lyxerr[Debug::KEY] << "SelfInsert arg[`"
251 << argument << "']" << endl;
259 FuncStatus LyXFunc::getStatus(int ac) const
261 return getStatus(lyxaction.retrieveActionArg(ac));
265 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
268 Buffer * buf = owner->buffer();
270 if (ev.action == LFUN_NOACTION) {
271 setStatusMessage(N_("Nothing to do"));
272 return flag.disabled(true);
276 case LFUN_UNKNOWN_ACTION:
277 #ifndef HAVE_LIBAIKSAURUS
278 case LFUN_THESAURUS_ENTRY:
283 flag |= lyx_gui::getStatus(ev);
286 if (flag.unknown()) {
287 setStatusMessage(N_("Unknown action"));
291 // the default error message if we disable the command
292 setStatusMessage(N_("Command disabled"));
294 // Check whether we need a buffer
295 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
296 // Yes we need a buffer, do we have one?
299 // Can we use a readonly buffer?
300 if (buf->isReadonly() &&
301 !lyxaction.funcHasFlag(ev.action,
302 LyXAction::ReadOnly)) {
304 setStatusMessage(N_("Document is read-only"));
309 setStatusMessage(N_("Command not allowed with"
310 "out any document open"));
311 return flag.disabled(true);
315 UpdatableInset * tli = view()->theLockingInset();
317 // I would really like to avoid having this switch and rather try to
318 // encode this in the function itself.
319 bool disable = false;
322 disable = ev.argument != "custom"
323 && !Exporter::IsExportable(buf, ev.argument);
326 disable = buf->undostack.empty();
329 disable = buf->redostack.empty();
334 UpdatableInset * in = tli;
335 if (in->lyxCode() != Inset::TABULAR_CODE) {
336 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
338 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
343 disable = !mathcursor && !view()->getLyXText()->selection.set();
346 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
349 disable = !Exporter::IsExportable(buf, "program");
352 case LFUN_LAYOUT_TABULAR:
354 || (tli->lyxCode() != Inset::TABULAR_CODE
355 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
359 disable = !changeDepth(view(), TEXT(false), DEC_DEPTH, true);
362 case LFUN_DEPTH_PLUS:
363 disable = !changeDepth(view(), TEXT(false), INC_DEPTH, true);
367 case LFUN_LAYOUT_PARAGRAPH: {
368 Inset * inset = TEXT(false)->cursor.par()->inInset();
369 disable = inset && inset->forceDefaultParagraphs(inset);
373 case LFUN_INSET_OPTARG:
374 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
377 case LFUN_TABULAR_FEATURE:
380 // FIXME: check temporarily disabled
382 char align = mathcursor->valign();
387 if (ev.argument.empty()) {
391 if (!contains("tcb", ev.argument[0])) {
395 flag.setOnOff(ev.argument[0] == align);
399 char align = mathcursor->halign();
404 if (ev.argument.empty()) {
408 if (!contains("lcr", ev.argument[0])) {
412 flag.setOnOff(ev.argument[0] == align);
415 disable = !mathcursor->halign();
421 //ret.disabled(true);
422 if (tli->lyxCode() == Inset::TABULAR_CODE) {
423 ret = static_cast<InsetTabular *>(tli)
424 ->getStatus(ev.argument);
427 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
428 ret = static_cast<InsetTabular *>
429 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
430 ->getStatus(ev.argument);
437 static InsetTabular inset(*owner->buffer(), 1, 1);
441 ret = inset.getStatus(ev.argument);
442 if (ret.onoff(true) || ret.onoff(false))
443 flag.setOnOff(false);
447 case LFUN_VC_REGISTER:
448 disable = buf->lyxvc.inUse();
450 case LFUN_VC_CHECKIN:
451 disable = !buf->lyxvc.inUse() || buf->isReadonly();
453 case LFUN_VC_CHECKOUT:
454 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
458 disable = !buf->lyxvc.inUse();
460 case LFUN_MENURELOAD:
461 disable = buf->isUnnamed() || buf->isClean();
463 case LFUN_BOOKMARK_GOTO:
465 isSavedPosition(strToUnsignedInt(ev.argument));
467 case LFUN_MERGE_CHANGES:
468 case LFUN_ACCEPT_CHANGE:
469 case LFUN_REJECT_CHANGE:
470 case LFUN_ACCEPT_ALL_CHANGES:
471 case LFUN_REJECT_ALL_CHANGES:
472 disable = !buf->params.tracking_changes;
474 case LFUN_INSET_TOGGLE: {
475 LyXText * lt = view()->getLyXText();
476 disable = !(isEditableInset(lt->getInset())
478 && lt->inset_owner->owner()
479 && lt->inset_owner->owner()->isOpen()));
483 case LFUN_INSET_SETTINGS: {
485 UpdatableInset * inset = view()->theLockingInset();
490 // get the innermost inset
491 inset = inset->getLockingInset();
493 // jump back to owner if an InsetText, so
494 // we get back to the InsetTabular or whatever
495 if (inset->lyxCode() == Inset::TEXT_CODE)
496 inset = inset->owner();
498 Inset::Code code = inset->lyxCode();
500 case Inset::TABULAR_CODE:
501 disable = ev.argument != "tabular";
503 case Inset::ERT_CODE:
504 disable = ev.argument != "ert";
506 case Inset::FLOAT_CODE:
507 disable = ev.argument != "float";
509 case Inset::MINIPAGE_CODE:
510 disable = ev.argument != "minipage";
512 case Inset::WRAP_CODE:
513 disable = ev.argument != "wrap";
521 case LFUN_MATH_MUTATE:
523 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
524 flag.setOnOff(false);
529 // we just need to be in math mode to enable that
531 case LFUN_MATH_SPACE:
532 case LFUN_MATH_LIMITS:
533 case LFUN_MATH_NONUMBER:
534 case LFUN_MATH_NUMBER:
535 case LFUN_MATH_EXTERN:
536 disable = !mathcursor;
539 case LFUN_DIALOG_SHOW: {
540 string const name = ev.getArg(0);
542 disable = !(name == "aboutlyx" ||
545 name == "preferences" ||
550 if (name == "print") {
551 disable = !Exporter::IsExportable(buf, "dvi") ||
552 lyxrc.print_command == "none";
553 } else if (name == "character") {
554 UpdatableInset * tli = view()->theLockingInset();
555 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
556 } else if (name == "vclog") {
557 disable = !buf->lyxvc.inUse();
558 } else if (name == "latexlog") {
559 disable = !IsFileReadable(buf->getLogName().second);
568 // the functions which insert insets
569 Inset::Code code = Inset::NO_CODE;
571 case LFUN_DIALOG_SHOW_NEW_INSET:
572 if (ev.argument == "bibitem")
573 code = Inset::BIBITEM_CODE;
574 else if (ev.argument == "bibtex")
575 code = Inset::BIBTEX_CODE;
576 else if (ev.argument == "citation")
577 code = Inset::CITE_CODE;
578 else if (ev.argument == "ert")
579 code = Inset::ERT_CODE;
580 else if (ev.argument == "external")
581 code = Inset::EXTERNAL_CODE;
582 else if (ev.argument == "float")
583 code = Inset::FLOAT_CODE;
584 else if (ev.argument == "graphics")
585 code = Inset::GRAPHICS_CODE;
586 else if (ev.argument == "include")
587 code = Inset::INCLUDE_CODE;
588 else if (ev.argument == "index")
589 code = Inset::INDEX_CODE;
590 else if (ev.argument == "label")
591 code = Inset::LABEL_CODE;
592 else if (ev.argument == "minipage")
593 code = Inset::MINIPAGE_CODE;
594 else if (ev.argument == "ref")
595 code = Inset::REF_CODE;
596 else if (ev.argument == "toc")
597 code = Inset::TOC_CODE;
598 else if (ev.argument == "url")
599 code = Inset::URL_CODE;
600 else if (ev.argument == "wrap")
601 code = Inset::WRAP_CODE;
605 code = Inset::ERT_CODE;
607 case LFUN_INSET_FOOTNOTE:
608 code = Inset::FOOT_CODE;
610 case LFUN_TABULAR_INSERT:
611 code = Inset::TABULAR_CODE;
613 case LFUN_INSET_MARGINAL:
614 code = Inset::MARGIN_CODE;
616 case LFUN_INSET_MINIPAGE:
617 code = Inset::MINIPAGE_CODE;
619 case LFUN_INSET_FLOAT:
620 case LFUN_INSET_WIDE_FLOAT:
621 code = Inset::FLOAT_CODE;
623 case LFUN_INSET_WRAP:
624 code = Inset::WRAP_CODE;
626 case LFUN_FLOAT_LIST:
627 code = Inset::FLOAT_LIST_CODE;
630 case LFUN_INSET_LIST:
631 code = Inset::LIST_CODE;
633 case LFUN_INSET_THEOREM:
634 code = Inset::THEOREM_CODE;
637 case LFUN_INSET_CAPTION:
638 code = Inset::CAPTION_CODE;
640 case LFUN_INSERT_NOTE:
641 code = Inset::NOTE_CODE;
643 case LFUN_INSERT_LABEL:
644 code = Inset::LABEL_CODE;
646 case LFUN_INSET_OPTARG:
647 code = Inset::OPTARG_CODE;
649 case LFUN_ENVIRONMENT_INSERT:
650 code = Inset::MINIPAGE_CODE;
652 case LFUN_INDEX_INSERT:
653 code = Inset::INDEX_CODE;
655 case LFUN_INDEX_PRINT:
656 code = Inset::INDEX_PRINT_CODE;
658 case LFUN_TOC_INSERT:
659 code = Inset::TOC_CODE;
663 code = Inset::URL_CODE;
666 // always allow this, since we will inset a raw quote
667 // if an inset is not allowed.
669 case LFUN_HYPHENATION:
670 case LFUN_LIGATURE_BREAK:
672 case LFUN_MENU_SEPARATOR:
674 case LFUN_END_OF_SENTENCE:
675 code = Inset::SPECIALCHAR_CODE;
677 case LFUN_SPACE_INSERT:
678 // slight hack: we know this is allowed in math mode
680 code = Inset::SPACE_CODE;
685 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
691 // A few general toggles
693 case LFUN_TOOLTIPS_TOGGLE:
694 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
697 case LFUN_READ_ONLY_TOGGLE:
698 flag.setOnOff(buf->isReadonly());
701 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
703 case LFUN_SWITCHBUFFER:
704 // toggle on the current buffer, but do not toggle off
705 // the other ones (is that a good idea?)
706 if (ev.argument == buf->fileName())
709 case LFUN_TRACK_CHANGES:
710 flag.setOnOff(buf->params.tracking_changes);
716 // the font related toggles
718 LyXFont const & font = TEXT(false)->real_current_font;
721 flag.setOnOff(font.emph() == LyXFont::ON);
724 flag.setOnOff(font.noun() == LyXFont::ON);
727 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
730 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
733 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
736 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
742 string tc = mathcursor->getLastCode();
745 flag.setOnOff(tc == "mathbf");
748 flag.setOnOff(tc == "mathsf");
751 flag.setOnOff(tc == "mathcal");
754 flag.setOnOff(tc == "mathrm");
757 flag.setOnOff(tc == "mathtt");
760 flag.setOnOff(tc == "mathbb");
763 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 (ev.action == LFUN_SEQUENCE) {
773 // argument contains ';'-terminated commands
774 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
781 void LyXFunc::dispatch(string const & s, bool verbose)
783 int const action = lyxaction.LookupFunc(s);
785 if (action == LFUN_UNKNOWN_ACTION) {
786 owner->message(bformat(_("Unknown function (%1$s)"), s));
790 dispatch(action, verbose);
794 void LyXFunc::dispatch(int ac, bool verbose)
796 dispatch(lyxaction.retrieveActionArg(ac), verbose);
801 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
803 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
804 <<"] arg[" << ev.argument << ']' << endl;
806 // we have not done anything wrong yet.
808 dispatch_buffer.erase();
810 #ifdef NEW_DISPATCHER
811 // We try do call the most specific dispatcher first:
812 // 1. the lockinginset's dispatch
813 // 2. the bufferview's dispatch
814 // 3. the lyxview's dispatch
817 selection_possible = false;
819 string argument = ev.argument;
820 kb_action action = ev.action;
822 // We cannot use this function here
823 if (getStatus(ev).disabled()) {
824 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
825 << lyxaction.getActionName(action)
826 << " [" << action << "] is disabled at this location"
828 setErrorMessage(getStatusMessage());
829 goto exit_with_message;
832 if (view()->available())
833 view()->hideCursor();
835 if (view()->available() && view()->theLockingInset()) {
836 Inset::RESULT result;
837 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
838 (!keyseq.deleted())))
840 UpdatableInset * inset = view()->theLockingInset();
844 inset->getCursorPos(view(), inset_x, dummy_y);
846 if ((action == LFUN_UNKNOWN_ACTION)
847 && argument.empty()) {
848 argument = encoded_last_key;
851 // the insets can't try to handle this,
852 // a table cell in the dummy position will
853 // lock its insettext, the insettext will
854 // pass it the bufferview, and succeed,
855 // so it will stay not locked. Not good
856 // if we've just done LFUN_ESCAPE (which
857 // injects an LFUN_PARAGRAPH_UPDATE)
858 if (action == LFUN_PARAGRAPH_UPDATE) {
859 view()->dispatch(ev);
860 goto exit_with_message;
863 // Undo/Redo is a bit tricky for insets.
864 if (action == LFUN_UNDO) {
866 goto exit_with_message;
867 } else if (action == LFUN_REDO) {
869 goto exit_with_message;
870 } else if (((result=inset->
871 // Hand-over to inset's own dispatch:
872 localDispatch(FuncRequest(view(), action, argument))) ==
874 (result == DISPATCHED_NOUPDATE))
875 goto exit_with_message;
876 // If UNDISPATCHED, just soldier on
877 else if (result == FINISHED) {
878 owner->clearMessage();
879 goto exit_with_message;
880 // We do not need special RTL handling here:
881 // FINISHED means that the cursor should be
882 // one position after the inset.
883 } else if (result == FINISHED_RIGHT) {
884 TEXT()->cursorRight(view());
885 moveCursorUpdate(true, false);
886 owner->clearMessage();
887 goto exit_with_message;
888 } else if (result == FINISHED_UP) {
889 if (TEXT()->cursor.irow() != TEXT()->rows().begin()) {
891 TEXT()->setCursorFromCoordinates(
892 TEXT()->cursor.ix() + inset_x,
893 TEXT()->cursor.iy() -
894 TEXT()->cursor.irow()->baseline() - 1);
895 TEXT()->cursor.x_fix(TEXT()->cursor.x());
897 TEXT()->cursorUp(view());
899 moveCursorUpdate(true, false);
901 view()->update(TEXT(), BufferView::SELECT);
903 owner->clearMessage();
904 goto exit_with_message;
905 } else if (result == FINISHED_DOWN) {
906 if (boost::next(TEXT()->cursor.irow()) != TEXT()->rows().end()) {
908 TEXT()->setCursorFromCoordinates(
909 TEXT()->cursor.ix() + inset_x,
910 TEXT()->cursor.iy() -
911 TEXT()->cursor.irow()->baseline() +
912 TEXT()->cursor.irow()->height() + 1);
913 TEXT()->cursor.x_fix(TEXT()->cursor.x());
915 TEXT()->cursorDown(view());
918 TEXT()->cursorRight(view());
920 moveCursorUpdate(true, false);
921 owner->clearMessage();
922 goto exit_with_message;
924 #warning I am not sure this is still right, please have a look! (Jug 20020417)
925 else { // result == UNDISPATCHED
926 //setMessage(N_("Text mode"));
928 case LFUN_UNKNOWN_ACTION:
929 case LFUN_BREAKPARAGRAPH:
931 TEXT()->cursorRight(view());
932 view()->switchKeyMap();
933 owner->view_state_changed();
936 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
937 TEXT()->cursorRight(view());
938 moveCursorUpdate(true, false);
939 owner->view_state_changed();
941 goto exit_with_message;
943 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
944 TEXT()->cursorRight(view());
945 moveCursorUpdate(true, false);
946 owner->view_state_changed();
948 goto exit_with_message;
950 if (boost::next(TEXT()->cursor.row()) != TEXT()->rows().end())
951 TEXT()->cursorDown(view());
953 TEXT()->cursorRight(view());
954 moveCursorUpdate(true, false);
955 owner->view_state_changed();
956 goto exit_with_message;
968 if (!view()->available()) break;
969 // this function should be used always [asierra060396]
970 UpdatableInset * tli =
971 view()->theLockingInset();
973 UpdatableInset * lock = tli->getLockingInset();
976 view()->unlockInset(tli);
977 TEXT()->cursorRight(view());
978 moveCursorUpdate(true, false);
979 owner->view_state_changed();
981 tli->unlockInsetInInset(view(),
986 // Tell the paragraph dialog that we changed paragraph
987 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
992 // --- Misc -------------------------------------------
993 case LFUN_WORDFINDFORWARD :
994 case LFUN_WORDFINDBACKWARD : {
995 static string last_search;
996 string searched_string;
998 if (!argument.empty()) {
999 last_search = argument;
1000 searched_string = argument;
1002 searched_string = last_search;
1004 bool fw = (action == LFUN_WORDFINDFORWARD);
1005 if (!searched_string.empty()) {
1006 lyxfind::LyXFind(view(), searched_string, fw);
1013 if (view()->available() && !view()->theLockingInset()) {
1014 view()->update(TEXT(), BufferView::SELECT);
1016 owner->message(keyseq.printOptions());
1020 // --- Misc -------------------------------------------
1021 case LFUN_EXEC_COMMAND:
1022 owner->focus_command_buffer();
1025 case LFUN_CANCEL: // RVDK_PATCH_5
1027 meta_fake_bit = key_modifier::none;
1028 if (view()->available())
1029 // cancel any selection
1030 dispatch(LFUN_MARK_OFF);
1031 setMessage(N_("Cancel"));
1034 case LFUN_META_FAKE: // RVDK_PATCH_5
1036 meta_fake_bit = key_modifier::alt;
1037 setMessage(keyseq.print());
1041 case LFUN_READ_ONLY_TOGGLE:
1042 if (owner->buffer()->lyxvc.inUse()) {
1043 owner->buffer()->lyxvc.toggleReadOnly();
1045 owner->buffer()->setReadonly(
1046 !owner->buffer()->isReadonly());
1050 case LFUN_CENTER: // this is center and redraw.
1054 // --- Menus -----------------------------------------------
1056 menuNew(argument, false);
1059 case LFUN_MENUNEWTMPLT:
1060 menuNew(argument, true);
1063 case LFUN_CLOSEBUFFER:
1067 case LFUN_MENUWRITE:
1068 if (!owner->buffer()->isUnnamed()) {
1069 string const str = bformat(_("Saving document %1$s..."),
1070 MakeDisplayPath(owner->buffer()->fileName()));
1071 owner->message(str);
1072 MenuWrite(owner->buffer());
1073 owner->message(str + _(" done."));
1075 WriteAs(owner->buffer());
1079 WriteAs(owner->buffer(), argument);
1082 case LFUN_MENURELOAD: {
1083 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1084 string text = bformat(_("Any changes will be lost. Are you sure "
1085 "you want to revert to the saved version of the document %1$s?"), file);
1086 int const ret = Alert::prompt(_("Revert to saved document?"),
1087 text, 0, 1, _("&Revert"), _("&Cancel"));
1095 Exporter::Export(owner->buffer(), argument, true);
1099 Exporter::Preview(owner->buffer(), argument);
1102 case LFUN_BUILDPROG:
1103 Exporter::Export(owner->buffer(), "program", true);
1106 case LFUN_RUNCHKTEX:
1107 owner->buffer()->runChktex();
1111 if (argument == "custom")
1112 owner->getDialogs().showSendto();
1114 Exporter::Export(owner->buffer(), argument, false);
1127 InsetCommandParams p("tableofcontents");
1128 string const data = InsetCommandMailer::params2string("toc", p);
1129 owner->getDialogs().show("toc", data, 0);
1145 case LFUN_DEPTH_MIN:
1146 changeDepth(view(), TEXT(false), DEC_DEPTH, false);
1147 owner->view_state_changed();
1150 case LFUN_DEPTH_PLUS:
1151 changeDepth(view(), TEXT(false), INC_DEPTH, false);
1152 owner->view_state_changed();
1155 case LFUN_FREEFONT_APPLY:
1156 apply_freefont(view());
1159 case LFUN_FREEFONT_UPDATE:
1160 update_and_apply_freefont(view(), argument);
1163 case LFUN_RECONFIGURE:
1164 Reconfigure(view());
1168 case LFUN_FLOATSOPERATE:
1169 if (argument == "openfoot")
1170 view()->allFloats(1,0);
1171 else if (argument == "closefoot")
1172 view()->allFloats(0,0);
1173 else if (argument == "openfig")
1174 view()->allFloats(1,1);
1175 else if (argument == "closefig")
1176 view()->allFloats(0,1);
1179 #ifdef WITH_WARNINGS
1180 //#warning Find another implementation here (or another lyxfunc)!
1183 case LFUN_HELP_OPEN:
1185 string const arg = argument;
1187 setErrorMessage(N_("Missing argument"));
1190 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1191 if (fname.empty()) {
1192 lyxerr << "LyX: unable to find documentation file `"
1193 << arg << "'. Bad installation?" << endl;
1196 owner->message(bformat(_("Opening help file %1$s..."),
1197 MakeDisplayPath(fname)));
1198 view()->buffer(bufferlist.loadLyXFile(fname, false));
1202 // --- version control -------------------------------
1203 case LFUN_VC_REGISTER:
1205 if (!owner->buffer()->lyxvc.inUse())
1206 owner->buffer()->lyxvc.registrer();
1210 case LFUN_VC_CHECKIN:
1212 if (owner->buffer()->lyxvc.inUse()
1213 && !owner->buffer()->isReadonly())
1214 owner->buffer()->lyxvc.checkIn();
1218 case LFUN_VC_CHECKOUT:
1220 if (owner->buffer()->lyxvc.inUse()
1221 && owner->buffer()->isReadonly())
1222 owner->buffer()->lyxvc.checkOut();
1226 case LFUN_VC_REVERT:
1228 owner->buffer()->lyxvc.revert();
1234 owner->buffer()->lyxvc.undoLast();
1238 // --- buffers ----------------------------------------
1240 case LFUN_SWITCHBUFFER:
1241 view()->buffer(bufferlist.getBuffer(argument));
1246 // servercmd: argument must be <file>:<template>
1247 Buffer * tmpbuf = NewFile(argument);
1249 view()->buffer(tmpbuf);
1253 case LFUN_FILE_OPEN:
1257 case LFUN_LAYOUT_TABULAR:
1258 if (view()->theLockingInset()) {
1259 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1260 InsetTabular * inset = static_cast<InsetTabular *>
1261 (view()->theLockingInset());
1262 inset->openLayoutDialog(view());
1263 } else if (view()->theLockingInset()->
1264 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1265 InsetTabular * inset = static_cast<InsetTabular *>(
1266 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1267 inset->openLayoutDialog(view());
1272 case LFUN_DROP_LAYOUTS_CHOICE:
1273 owner->getToolbar().openLayoutList();
1276 case LFUN_MENU_OPEN_BY_NAME:
1277 owner->getMenubar().openByName(argument);
1278 break; // RVDK_PATCH_5
1280 // --- lyxserver commands ----------------------------
1284 setMessage(owner->buffer()->fileName());
1285 lyxerr[Debug::INFO] << "FNAME["
1286 << owner->buffer()->fileName()
1292 dispatch_buffer = keyseq.print();
1293 lyxserver->notifyClient(dispatch_buffer);
1297 case LFUN_GOTOFILEROW:
1301 istringstream istr(argument.c_str());
1302 istr >> file_name >> row;
1303 // Must replace extension of the file to be .lyx and get full path
1304 string const s(ChangeExtension(file_name, ".lyx"));
1306 // Either change buffer or load the file
1307 if (bufferlist.exists(s)) {
1308 view()->buffer(bufferlist.getBuffer(s));
1310 view()->buffer(bufferlist.loadLyXFile(s));
1313 view()->setCursorFromRow(row);
1316 // see BufferView_pimpl::center()
1317 view()->updateScrollbar();
1321 case LFUN_GOTO_PARAGRAPH:
1323 istringstream istr(argument.c_str());
1327 ParIterator par = owner->buffer()->getParFromID(id);
1328 if (par == owner->buffer()->par_iterator_end()) {
1329 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1330 << id << ']' << endl;
1333 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1334 << " found." << endl;
1337 if (view()->theLockingInset())
1338 view()->unlockInset(view()->theLockingInset());
1339 if (par->inInset()) {
1340 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1341 par->inInset()->localDispatch(cmd);
1344 view()->getLyXText()->setCursor(par.pit(), 0);
1345 view()->switchKeyMap();
1346 owner->view_state_changed();
1349 // see BufferView_pimpl::center()
1350 view()->updateScrollbar();
1354 // --- insert characters ----------------------------------------
1356 // --- Mathed stuff. If we are here, there is no locked inset yet.
1357 case LFUN_MATH_EXTERN:
1358 case LFUN_MATH_NUMBER:
1359 case LFUN_MATH_NONUMBER:
1360 case LFUN_MATH_LIMITS:
1362 setErrorMessage(N_("This is only allowed in math mode!"));
1366 // passthrough hat and underscore outside mathed:
1367 case LFUN_SUBSCRIPT:
1368 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1370 case LFUN_SUPERSCRIPT:
1371 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1374 case LFUN_DIALOG_SHOW: {
1375 string const name = ev.getArg(0);
1376 string data = trim(ev.argument.substr(name.size()));
1378 if (name == "character") {
1379 data = freefont2string();
1381 owner->getDialogs().show("character", data);
1382 } else if (name == "document")
1383 owner->getDialogs().showDocument();
1384 else if (name == "findreplace")
1385 owner->getDialogs().showSearch();
1386 else if (name == "forks")
1387 owner->getDialogs().showForks();
1388 else if (name == "preamble")
1389 owner->getDialogs().showPreamble();
1390 else if (name == "preferences")
1391 owner->getDialogs().showPreferences();
1392 else if (name == "print")
1393 owner->getDialogs().showPrint();
1394 else if (name == "spellchecker")
1395 owner->getDialogs().showSpellchecker();
1397 owner->getDialogs().show(name, data);
1401 case LFUN_DIALOG_SHOW_NEW_INSET: {
1402 string const & name = argument;
1404 if (name == "bibitem" ||
1406 name == "include" ||
1411 InsetCommandParams p(name);
1412 data = InsetCommandMailer::params2string(name, p);
1413 } else if (name == "citation") {
1414 InsetCommandParams p("cite");
1415 data = InsetCommandMailer::params2string(name, p);
1417 owner->getDialogs().show(name, data, 0);
1421 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1425 case LFUN_DIALOG_UPDATE: {
1426 string const & name = argument;
1427 // Can only update a dialog connected to an existing inset
1428 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1430 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1432 inset->localDispatch(fr);
1433 } else if (name == "paragraph") {
1434 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1439 case LFUN_DIALOG_HIDE:
1440 Dialogs::hide(argument, 0);
1443 case LFUN_DIALOG_DISCONNECT_INSET:
1444 owner->getDialogs().disconnect(argument);
1447 case LFUN_CHILDOPEN:
1449 string const filename =
1450 MakeAbsPath(argument,
1451 owner->buffer()->filePath());
1452 setMessage(N_("Opening child document ") +
1453 MakeDisplayPath(filename) + "...");
1454 view()->savePosition(0);
1455 if (bufferlist.exists(filename))
1456 view()->buffer(bufferlist.getBuffer(filename));
1458 view()->buffer(bufferlist.loadLyXFile(filename));
1462 case LFUN_TOGGLECURSORFOLLOW:
1463 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1467 owner->getIntl().KeyMapOn(false);
1470 case LFUN_KMAP_PRIM:
1471 owner->getIntl().KeyMapPrim();
1475 owner->getIntl().KeyMapSec();
1478 case LFUN_KMAP_TOGGLE:
1479 owner->getIntl().ToggleKeyMap();
1484 // argument contains ';'-terminated commands
1485 while (!argument.empty()) {
1487 argument = split(argument, first, ';');
1493 case LFUN_SAVEPREFERENCES:
1495 Path p(user_lyxdir);
1496 lyxrc.write("preferences");
1500 case LFUN_SCREEN_FONT_UPDATE:
1502 // handle the screen font changes.
1503 lyxrc.set_font_norm_type();
1504 lyx_gui::update_fonts();
1505 // We also need to empty the textcache so that
1506 // the buffer will be formatted correctly after
1509 // Of course we should only do the resize and the textcache.clear
1510 // if values really changed...but not very important right now. (Lgb)
1511 // All visible buffers will need resize
1517 case LFUN_SET_COLOR:
1520 string const x11_name = split(argument, lyx_name, ' ');
1521 if (lyx_name.empty() || x11_name.empty()) {
1522 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1527 bool const graphicsbg_changed =
1528 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1529 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1531 if (!lcolor.setColor(lyx_name, x11_name)) {
1533 bformat(_("Set-color \"%1$s\" failed "
1534 "- color is undefined or "
1535 "may not be redefined"), lyx_name));
1539 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1541 if (graphicsbg_changed) {
1542 #ifdef WITH_WARNINGS
1543 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1546 grfx::GCache & gc = grfx::GCache::get();
1547 gc.changeDisplay(true);
1556 owner->message(argument);
1559 case LFUN_FORKS_KILL:
1561 if (!isStrInt(argument))
1564 pid_t const pid = strToInt(argument);
1565 ForkedcallsController & fcc = ForkedcallsController::get();
1570 case LFUN_TOOLTIPS_TOGGLE:
1571 owner->getDialogs().toggleTooltips();
1574 case LFUN_EXTERNAL_EDIT: {
1576 .localDispatch(FuncRequest(view(), action, argument));
1581 // Then if it was none of the above
1582 // Trying the BufferView::pimpl dispatch:
1583 if (!view()->dispatch(ev))
1584 lyxerr << "A truly unknown func ["
1585 << lyxaction.getActionName(ev.action) << "]!"
1592 view()->owner()->updateLayoutChoice();
1594 if (view()->available()) {
1595 view()->fitCursor();
1597 // If we executed a mutating lfun, mark the buffer as dirty
1598 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1599 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1600 view()->buffer()->markDirty();
1603 sendDispatchMessage(getMessage(), ev, verbose);
1607 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1609 owner->updateMenubar();
1610 owner->updateToolbar();
1612 if (ev.action == LFUN_SELFINSERT || !verbose) {
1613 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1615 owner->message(msg);
1619 string dispatch_msg = msg;
1620 if (!dispatch_msg.empty())
1621 dispatch_msg += ' ';
1623 string comname = lyxaction.getActionName(ev.action);
1625 int pseudoaction = ev.action;
1626 bool argsadded = false;
1628 if (!ev.argument.empty()) {
1629 // the pseudoaction is useful for the bindings
1630 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1632 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1633 pseudoaction = ev.action;
1635 comname += ' ' + ev.argument;
1640 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1642 if (!shortcuts.empty()) {
1643 comname += ": " + shortcuts;
1644 } else if (!argsadded && !ev.argument.empty()) {
1645 comname += ' ' + ev.argument;
1648 if (!comname.empty()) {
1649 comname = rtrim(comname);
1650 dispatch_msg += '(' + comname + ')';
1653 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1654 if (!dispatch_msg.empty())
1655 owner->message(dispatch_msg);
1659 void LyXFunc::setupLocalKeymap()
1661 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1662 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1666 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1668 string initpath = lyxrc.document_path;
1669 string filename(name);
1671 if (view()->available()) {
1672 string const trypath = owner->buffer()->filePath();
1673 // If directory is writeable, use this as default.
1674 if (IsDirWriteable(trypath))
1678 static int newfile_number;
1680 if (filename.empty()) {
1681 filename = AddName(lyxrc.document_path,
1682 "newfile" + tostr(++newfile_number) + ".lyx");
1683 FileInfo fi(filename);
1684 while (bufferlist.exists(filename) || fi.readable()) {
1686 filename = AddName(lyxrc.document_path,
1687 "newfile" + tostr(newfile_number) +
1689 fi.newFile(filename);
1693 // The template stuff
1696 FileDialog fileDlg(_("Select template file"),
1697 LFUN_SELECT_FILE_SYNC,
1698 make_pair(string(_("Documents|#o#O")),
1699 string(lyxrc.document_path)),
1700 make_pair(string(_("Templates|#T#t")),
1701 string(lyxrc.template_path)));
1703 FileDialog::Result result =
1704 fileDlg.open(lyxrc.template_path,
1705 _("*.lyx| LyX Documents (*.lyx)"));
1707 if (result.first == FileDialog::Later)
1710 string const fname = result.second;
1717 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1721 void LyXFunc::open(string const & fname)
1723 string initpath = lyxrc.document_path;
1725 if (view()->available()) {
1726 string const trypath = owner->buffer()->filePath();
1727 // If directory is writeable, use this as default.
1728 if (IsDirWriteable(trypath))
1734 if (fname.empty()) {
1735 FileDialog fileDlg(_("Select document to open"),
1737 make_pair(string(_("Documents|#o#O")),
1738 string(lyxrc.document_path)),
1739 make_pair(string(_("Examples|#E#e")),
1740 string(AddPath(system_lyxdir, "examples"))));
1742 FileDialog::Result result =
1743 fileDlg.open(initpath,
1744 _("*.lyx| LyX Documents (*.lyx)"));
1746 if (result.first == FileDialog::Later)
1749 filename = result.second;
1751 // check selected filename
1752 if (filename.empty()) {
1753 owner->message(_("Canceled."));
1759 // get absolute path of file and add ".lyx" to the filename if
1761 string const fullpath = FileSearch(string(), filename, "lyx");
1762 if (!fullpath.empty()) {
1763 filename = fullpath;
1766 string const disp_fn(MakeDisplayPath(filename));
1768 // if the file doesn't exist, let the user create one
1769 FileInfo const f(filename, true);
1771 // the user specifically chose this name. Believe them.
1772 Buffer * buffer = bufferlist.newFile(filename, "", true);
1773 view()->buffer(buffer);
1777 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1779 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1782 view()->buffer(openbuf);
1783 str2 = bformat(_("Document %1$s opened."), disp_fn);
1785 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1787 owner->message(str2);
1791 void LyXFunc::doImport(string const & argument)
1794 string filename = split(argument, format, ' ');
1796 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1797 << " file: " << filename << endl;
1799 // need user interaction
1800 if (filename.empty()) {
1801 string initpath = lyxrc.document_path;
1803 if (view()->available()) {
1804 string const trypath = owner->buffer()->filePath();
1805 // If directory is writeable, use this as default.
1806 if (IsDirWriteable(trypath))
1810 string const text = bformat(_("Select %1$s file to import"),
1811 formats.prettyName(format));
1813 FileDialog fileDlg(text,
1815 make_pair(string(_("Documents|#o#O")),
1816 string(lyxrc.document_path)),
1817 make_pair(string(_("Examples|#E#e")),
1818 string(AddPath(system_lyxdir, "examples"))));
1820 string const extension = "*." + formats.extension(format)
1821 + "| " + formats.prettyName(format)
1822 + " (*." + formats.extension(format) + ')';
1824 FileDialog::Result result = fileDlg.open(initpath,
1827 if (result.first == FileDialog::Later)
1830 filename = result.second;
1832 // check selected filename
1833 if (filename.empty())
1834 owner->message(_("Canceled."));
1837 if (filename.empty())
1840 // get absolute path of file
1841 filename = MakeAbsPath(filename);
1843 string const lyxfile = ChangeExtension(filename, ".lyx");
1845 // Check if the document already is open
1846 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1847 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1848 owner->message(_("Canceled."));
1853 // if the file exists already, and we didn't do
1854 // -i lyx thefile.lyx, warn
1855 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1856 string const file = MakeDisplayPath(lyxfile, 30);
1858 string text = bformat(_("The document %1$s already exists.\n\n"
1859 "Do you want to over-write that document?"), file);
1860 int const ret = Alert::prompt(_("Over-write document?"),
1861 text, 0, 1, _("&Over-write"), _("&Cancel"));
1864 owner->message(_("Canceled."));
1869 Importer::Import(owner, filename, format);
1873 void LyXFunc::closeBuffer()
1875 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1876 if (bufferlist.empty()) {
1877 // need this otherwise SEGV may occur while trying to
1878 // set variables that don't exist
1879 // since there's no current buffer
1880 owner->getDialogs().hideBufferDependent();
1882 view()->buffer(bufferlist.first());
1888 // Each "owner" should have it's own message method. lyxview and
1889 // the minibuffer would use the minibuffer, but lyxserver would
1890 // send an ERROR signal to its client. Alejandro 970603
1891 // This func is bit problematic when it comes to NLS, to make the
1892 // lyx servers client be language indepenent we must not translate
1893 // strings sent to this func.
1894 void LyXFunc::setErrorMessage(string const & m) const
1896 dispatch_buffer = m;
1901 void LyXFunc::setMessage(string const & m) const
1903 dispatch_buffer = m;
1907 void LyXFunc::setStatusMessage(string const & m) const
1913 string const LyXFunc::view_status_message()
1915 // When meta-fake key is pressed, show the key sequence so far + "M-".
1917 return keyseq.print() + "M-";
1920 // Else, when a non-complete key sequence is pressed,
1921 // show the available options.
1922 if (keyseq.length() > 0 && !keyseq.deleted()) {
1923 return keyseq.printOptions();
1926 if (!view()->available())
1927 return _("Welcome to LyX!");
1929 return currentState(view());
1933 BufferView * LyXFunc::view() const
1936 return owner->view().get();