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"
18 #include "BufferView.h"
19 #include "lyxserver.h"
23 #include "LyXAction.h"
29 #include "trans_mgr.h"
32 #include "bufferview_funcs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/lyx_gui.h"
36 #include "FloatList.h"
40 #include "TextCache.h"
42 #include "undo_funcs.h"
43 #include "ParagraphParameters.h"
45 #include "insets/insetcommand.h"
46 #include "insets/insettabular.h"
48 #include "mathed/formulamacro.h"
49 #include "mathed/math_cursor.h"
50 #include "mathed/math_inset.h"
52 #include "frontends/FileDialog.h"
53 #include "frontends/Dialogs.h"
54 #include "frontends/Toolbar.h"
55 #include "frontends/Menubar.h"
56 #include "frontends/Alert.h"
58 #include "graphics/GraphicsCache.h"
60 #include "support/lyxalgo.h"
61 #include "support/LAssert.h"
62 #include "support/filetools.h"
63 #include "support/FileInfo.h"
64 #include "support/forkedcontr.h"
65 #include "support/lstrings.h"
66 #include "support/path.h"
67 #include "support/lyxfunctional.h"
83 using std::back_inserter;
84 using namespace bv_funcs;
86 extern BufferList bufferlist;
87 extern LyXServer * lyxserver;
88 extern bool selection_possible;
90 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
93 extern tex_accent_struct get_accent(kb_action action);
95 extern void ShowLatexLog();
98 LyXFunc::LyXFunc(LyXView * o)
101 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
102 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
103 meta_fake_bit(key_modifier::none)
109 LyXText * LyXFunc::TEXT(bool flag = true) const
113 return view()->getLyXText();
118 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
120 if (selecting || TEXT(flag)->selection.mark()) {
121 TEXT(flag)->setSelection();
122 if (!TEXT(flag)->isInInset())
123 view()->toggleToggle();
125 view()->update(TEXT(flag), BufferView::SELECT);
127 view()->switchKeyMap();
131 void LyXFunc::handleKeyFunc(kb_action action)
133 char c = encoded_last_key;
135 if (keyseq.length()) {
139 owner->getIntl().getTransManager()
140 .deadkey(c, get_accent(action).accent, TEXT(false));
141 // Need to clear, in case the minibuffer calls these
144 // copied verbatim from do_accent_char
145 view()->update(TEXT(false), BufferView::SELECT);
146 TEXT(false)->selection.cursor = TEXT(false)->cursor;
150 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
151 key_modifier::state state)
155 if (lyxerr.debugging(Debug::KEY)) {
156 lyxerr << "KeySym is "
157 << keysym->getSymbolName()
161 // Do nothing if we have nothing (JMarc)
162 if (!keysym->isOK()) {
163 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
168 if (keysym->isModifier()) {
169 lyxerr[Debug::KEY] << "isModifier true" << endl;
173 Encoding const * encoding = view()->getEncoding();
175 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
177 // Do a one-deep top-level lookup for
178 // cancel and meta-fake keys. RVDK_PATCH_5
179 cancel_meta_seq.reset();
181 int action = cancel_meta_seq.addkey(keysym, state);
182 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
184 // When not cancel or meta-fake, do the normal lookup.
185 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
186 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
187 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
188 // remove Caps Lock and Mod2 as a modifiers
189 action = keyseq.addkey(keysym, (state | meta_fake_bit));
190 lyxerr[Debug::KEY] << "action now set to ["
191 << action << ']' << endl;
194 // Dont remove this unless you know what you are doing.
195 meta_fake_bit = key_modifier::none;
197 // can this happen now ?
198 if (action == LFUN_NOACTION) {
199 action = LFUN_PREFIX;
202 if (lyxerr.debugging(Debug::KEY)) {
203 lyxerr << "Key [action="
205 << keyseq.print() << ']'
209 // already here we know if it any point in going further
210 // why not return already here if action == -1 and
211 // num_bytes == 0? (Lgb)
213 if (keyseq.length() > 1) {
214 owner->message(keyseq.print());
218 // Maybe user can only reach the key via holding down shift.
219 // Let's see. But only if shift is the only modifier
220 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
221 lyxerr[Debug::KEY] << "Trying without shift" << endl;
222 action = keyseq.addkey(keysym, key_modifier::none);
223 lyxerr[Debug::KEY] << "Action now " << action << endl;
226 if (action == LFUN_UNKNOWN_ACTION) {
227 // Hmm, we didn't match any of the keysequences. See
228 // if it's normal insertable text not already covered
230 if (keysym->isText() && keyseq.length() == 1) {
231 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
232 action = LFUN_SELFINSERT;
234 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
235 owner->message(_("Unknown function."));
240 if (action == LFUN_SELFINSERT) {
241 if (encoded_last_key != 0) {
243 arg += encoded_last_key;
245 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
247 lyxerr[Debug::KEY] << "SelfInsert arg[`"
248 << argument << "']" << endl;
256 FuncStatus LyXFunc::getStatus(int ac) const
258 return getStatus(lyxaction.retrieveActionArg(ac));
262 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
265 Buffer * buf = owner->buffer();
267 if (ev.action == LFUN_NOACTION) {
268 setStatusMessage(N_("Nothing to do"));
269 return flag.disabled(true);
273 case LFUN_UNKNOWN_ACTION:
274 #ifndef HAVE_LIBAIKSAURUS
275 case LFUN_THESAURUS_ENTRY:
280 flag |= lyx_gui::getStatus(ev);
283 if (flag.unknown()) {
284 setStatusMessage(N_("Unknown action"));
288 // the default error message if we disable the command
289 setStatusMessage(N_("Command disabled"));
291 // Check whether we need a buffer
292 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
293 // Yes we need a buffer, do we have one?
296 // Can we use a readonly buffer?
297 if (buf->isReadonly() &&
298 !lyxaction.funcHasFlag(ev.action,
299 LyXAction::ReadOnly)) {
301 setStatusMessage(N_("Document is read-only"));
306 setStatusMessage(N_("Command not allowed with"
307 "out any document open"));
308 return flag.disabled(true);
312 UpdatableInset * tli = view()->theLockingInset();
314 // I would really like to avoid having this switch and rather try to
315 // encode this in the function itself.
316 bool disable = false;
319 disable = !Exporter::IsExportable(buf, "dvi")
320 || lyxrc.print_command == "none";
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_CHARACTER:
354 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
357 case LFUN_LAYOUT_TABULAR:
359 || (tli->lyxCode() != Inset::TABULAR_CODE
360 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
364 disable = !changeDepth(view(), TEXT(false), DEC_DEPTH, true);
367 case LFUN_DEPTH_PLUS:
368 disable = !changeDepth(view(), TEXT(false), INC_DEPTH, true);
372 case LFUN_LAYOUT_PARAGRAPH: {
373 Inset * inset = TEXT(false)->cursor.par()->inInset();
374 disable = inset && inset->forceDefaultParagraphs(inset);
378 case LFUN_INSET_OPTARG:
379 disable = (TEXT(false)->cursor.par()->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);
420 disable = !mathcursor->halign();
426 //ret.disabled(true);
427 if (tli->lyxCode() == Inset::TABULAR_CODE) {
428 ret = static_cast<InsetTabular *>(tli)
429 ->getStatus(ev.argument);
432 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
433 ret = static_cast<InsetTabular *>
434 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
435 ->getStatus(ev.argument);
442 static InsetTabular inset(*owner->buffer(), 1, 1);
446 ret = inset.getStatus(ev.argument);
447 if (ret.onoff(true) || ret.onoff(false))
448 flag.setOnOff(false);
452 case LFUN_VC_REGISTER:
453 disable = buf->lyxvc.inUse();
455 case LFUN_VC_CHECKIN:
456 disable = !buf->lyxvc.inUse() || buf->isReadonly();
458 case LFUN_VC_CHECKOUT:
459 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
463 case LFUN_VC_HISTORY:
464 disable = !buf->lyxvc.inUse();
466 case LFUN_MENURELOAD:
467 disable = buf->isUnnamed() || buf->isClean();
469 case LFUN_BOOKMARK_GOTO:
471 isSavedPosition(strToUnsignedInt(ev.argument));
473 case LFUN_MERGE_CHANGES:
474 case LFUN_ACCEPT_CHANGE:
475 case LFUN_REJECT_CHANGE:
476 case LFUN_ACCEPT_ALL_CHANGES:
477 case LFUN_REJECT_ALL_CHANGES:
478 disable = !buf->params.tracking_changes;
480 case LFUN_INSET_TOGGLE: {
481 LyXText * lt = view()->getLyXText();
482 disable = !(isEditableInset(lt->getInset())
484 && lt->inset_owner->owner()
485 && lt->inset_owner->owner()->isOpen()));
489 case LFUN_INSET_SETTINGS: {
491 UpdatableInset * inset = view()->theLockingInset();
496 // get the innermost inset
497 inset = inset->getLockingInset();
499 // jump back to owner if an InsetText, so
500 // we get back to the InsetTabular or whatever
501 if (inset->lyxCode() == Inset::TEXT_CODE)
502 inset = static_cast<UpdatableInset*>(inset->owner());
504 Inset::Code code = inset->lyxCode();
506 case Inset::TABULAR_CODE:
507 disable = ev.argument != "tabular";
509 case Inset::ERT_CODE:
510 disable = ev.argument != "ert";
512 case Inset::FLOAT_CODE:
513 disable = ev.argument != "float";
515 case Inset::MINIPAGE_CODE:
516 disable = ev.argument != "minipage";
518 case Inset::WRAP_CODE:
519 disable = ev.argument != "wrap";
528 disable = !IsFileReadable(buf->getLogName().second);
531 case LFUN_MATH_MUTATE:
533 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
534 flag.setOnOff(false);
539 // we just need to be in math mode to enable that
541 case LFUN_MATH_SPACE:
542 case LFUN_MATH_LIMITS:
543 case LFUN_MATH_NONUMBER:
544 case LFUN_MATH_NUMBER:
545 case LFUN_MATH_EXTERN:
546 disable = !mathcursor;
553 // the functions which insert insets
554 Inset::Code code = Inset::NO_CODE;
556 case LFUN_DIALOG_SHOW_NEW_INSET:
557 if (ev.argument == "bibitem")
558 code = Inset::BIBITEM_CODE;
559 else if (ev.argument == "bibtex")
560 code = Inset::BIBTEX_CODE;
561 else if (ev.argument == "citation")
562 code = Inset::CITE_CODE;
563 else if (ev.argument == "ert")
564 code = Inset::ERT_CODE;
565 else if (ev.argument == "external")
566 code = Inset::EXTERNAL_CODE;
567 else if (ev.argument == "float")
568 code = Inset::FLOAT_CODE;
569 else if (ev.argument == "graphics")
570 code = Inset::GRAPHICS_CODE;
571 else if (ev.argument == "include")
572 code = Inset::INCLUDE_CODE;
573 else if (ev.argument == "index")
574 code = Inset::INDEX_CODE;
575 else if (ev.argument == "label")
576 code = Inset::LABEL_CODE;
577 else if (ev.argument == "minipage")
578 code = Inset::MINIPAGE_CODE;
579 else if (ev.argument == "ref")
580 code = Inset::REF_CODE;
581 else if (ev.argument == "toc")
582 code = Inset::TOC_CODE;
583 else if (ev.argument == "url")
584 code = Inset::URL_CODE;
585 else if (ev.argument == "wrap")
586 code = Inset::WRAP_CODE;
590 code = Inset::ERT_CODE;
592 case LFUN_INSET_FOOTNOTE:
593 code = Inset::FOOT_CODE;
595 case LFUN_TABULAR_INSERT:
596 code = Inset::TABULAR_CODE;
598 case LFUN_INSET_MARGINAL:
599 code = Inset::MARGIN_CODE;
601 case LFUN_INSET_MINIPAGE:
602 code = Inset::MINIPAGE_CODE;
604 case LFUN_INSET_FLOAT:
605 case LFUN_INSET_WIDE_FLOAT:
606 code = Inset::FLOAT_CODE;
608 case LFUN_INSET_WRAP:
609 code = Inset::WRAP_CODE;
611 case LFUN_FLOAT_LIST:
612 code = Inset::FLOAT_LIST_CODE;
615 case LFUN_INSET_LIST:
616 code = Inset::LIST_CODE;
618 case LFUN_INSET_THEOREM:
619 code = Inset::THEOREM_CODE;
622 case LFUN_INSET_CAPTION:
623 code = Inset::CAPTION_CODE;
625 case LFUN_INSERT_NOTE:
626 code = Inset::NOTE_CODE;
628 case LFUN_INSERT_LABEL:
629 code = Inset::LABEL_CODE;
631 case LFUN_INSET_OPTARG:
632 code = Inset::OPTARG_CODE;
634 case LFUN_ENVIRONMENT_INSERT:
635 code = Inset::MINIPAGE_CODE;
637 case LFUN_INDEX_INSERT:
638 code = Inset::INDEX_CODE;
640 case LFUN_INDEX_PRINT:
641 code = Inset::INDEX_PRINT_CODE;
643 case LFUN_TOC_INSERT:
644 code = Inset::TOC_CODE;
646 case LFUN_PARENTINSERT:
647 code = Inset::PARENT_CODE;
651 code = Inset::URL_CODE;
654 // always allow this, since we will inset a raw quote
655 // if an inset is not allowed.
657 case LFUN_HYPHENATION:
658 case LFUN_LIGATURE_BREAK:
660 case LFUN_MENU_SEPARATOR:
662 case LFUN_END_OF_SENTENCE:
663 code = Inset::SPECIALCHAR_CODE;
665 case LFUN_PROTECTEDSPACE:
666 // slight hack: we know this is allowed in math mode
668 code = Inset::SPECIALCHAR_CODE;
673 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
679 // A few general toggles
681 case LFUN_TOOLTIPS_TOGGLE:
682 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
685 case LFUN_READ_ONLY_TOGGLE:
686 flag.setOnOff(buf->isReadonly());
689 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
691 case LFUN_SWITCHBUFFER:
692 // toggle on the current buffer, but do not toggle off
693 // the other ones (is that a good idea?)
694 if (ev.argument == buf->fileName())
697 case LFUN_TRACK_CHANGES:
698 flag.setOnOff(buf->params.tracking_changes);
704 // the font related toggles
706 LyXFont const & font = TEXT(false)->real_current_font;
709 flag.setOnOff(font.emph() == LyXFont::ON);
712 flag.setOnOff(font.noun() == LyXFont::ON);
715 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
718 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
721 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
724 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
730 string tc = mathcursor->getLastCode();
733 flag.setOnOff(tc == "mathbf");
736 flag.setOnOff(tc == "mathsf");
739 flag.setOnOff(tc == "mathcal");
742 flag.setOnOff(tc == "mathrm");
745 flag.setOnOff(tc == "mathtt");
748 flag.setOnOff(tc == "mathbb");
751 flag.setOnOff(tc == "mathnormal");
758 // this one is difficult to get right. As a half-baked
759 // solution, we consider only the first action of the sequence
760 if (ev.action == LFUN_SEQUENCE) {
761 // argument contains ';'-terminated commands
762 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
769 void LyXFunc::dispatch(string const & s, bool verbose)
771 int const action = lyxaction.LookupFunc(s);
773 if (action == LFUN_UNKNOWN_ACTION) {
774 owner->message(bformat(_("Unknown function (%1$s)"), s));
778 dispatch(action, verbose);
782 void LyXFunc::dispatch(int ac, bool verbose)
784 dispatch(lyxaction.retrieveActionArg(ac), verbose);
789 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
791 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
792 <<"] arg[" << ev.argument << ']' << endl;
794 // we have not done anything wrong yet.
796 dispatch_buffer.erase();
798 #ifdef NEW_DISPATCHER
799 // We try do call the most specific dispatcher first:
800 // 1. the lockinginset's dispatch
801 // 2. the bufferview's dispatch
802 // 3. the lyxview's dispatch
805 selection_possible = false;
807 string argument = ev.argument;
808 kb_action action = ev.action;
810 // We cannot use this function here
811 if (getStatus(ev).disabled()) {
812 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
813 << lyxaction.getActionName(action)
814 << " [" << action << "] is disabled at this location"
816 setErrorMessage(getStatusMessage());
817 goto exit_with_message;
820 if (view()->available())
821 view()->hideCursor();
823 if (view()->available() && view()->theLockingInset()) {
824 Inset::RESULT result;
825 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
826 (!keyseq.deleted())))
828 UpdatableInset * inset = view()->theLockingInset();
832 inset->getCursorPos(view(), inset_x, dummy_y);
834 if ((action == LFUN_UNKNOWN_ACTION)
835 && argument.empty()) {
836 argument = encoded_last_key;
838 // Undo/Redo is a bit tricky for insets.
839 if (action == LFUN_UNDO) {
841 goto exit_with_message;
842 } else if (action == LFUN_REDO) {
844 goto exit_with_message;
845 } else if (((result=inset->
846 // Hand-over to inset's own dispatch:
847 localDispatch(FuncRequest(view(), action, argument))) ==
849 (result == DISPATCHED_NOUPDATE))
850 goto exit_with_message;
851 // If UNDISPATCHED, just soldier on
852 else if (result == FINISHED) {
853 owner->clearMessage();
854 goto exit_with_message;
855 // We do not need special RTL handling here:
856 // FINISHED means that the cursor should be
857 // one position after the inset.
858 } else if (result == FINISHED_RIGHT) {
859 TEXT()->cursorRight(view());
860 moveCursorUpdate(true, false);
861 owner->clearMessage();
862 goto exit_with_message;
863 } else if (result == FINISHED_UP) {
864 if (TEXT()->cursor.irow() != TEXT()->rows().begin()) {
866 TEXT()->setCursorFromCoordinates(
867 TEXT()->cursor.ix() + inset_x,
868 TEXT()->cursor.iy() -
869 TEXT()->cursor.irow()->baseline() - 1);
870 TEXT()->cursor.x_fix(TEXT()->cursor.x());
872 TEXT()->cursorUp(view());
874 moveCursorUpdate(true, false);
876 view()->update(TEXT(), BufferView::SELECT);
878 owner->clearMessage();
879 goto exit_with_message;
880 } else if (result == FINISHED_DOWN) {
881 if (boost::next(TEXT()->cursor.irow()) != TEXT()->rows().end()) {
883 TEXT()->setCursorFromCoordinates(
884 TEXT()->cursor.ix() + inset_x,
885 TEXT()->cursor.iy() -
886 TEXT()->cursor.irow()->baseline() +
887 TEXT()->cursor.irow()->height() + 1);
888 TEXT()->cursor.x_fix(TEXT()->cursor.x());
890 TEXT()->cursorDown(view());
893 TEXT()->cursorRight(view());
895 moveCursorUpdate(true, false);
896 owner->clearMessage();
897 goto exit_with_message;
899 #warning I am not sure this is still right, please have a look! (Jug 20020417)
900 else { // result == UNDISPATCHED
901 //setMessage(N_("Text mode"));
903 case LFUN_UNKNOWN_ACTION:
904 case LFUN_BREAKPARAGRAPH:
906 TEXT()->cursorRight(view());
907 view()->switchKeyMap();
908 owner->view_state_changed();
911 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
912 TEXT()->cursorRight(view());
913 moveCursorUpdate(true, false);
914 owner->view_state_changed();
916 goto exit_with_message;
918 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
919 TEXT()->cursorRight(view());
920 moveCursorUpdate(true, false);
921 owner->view_state_changed();
923 goto exit_with_message;
925 if (boost::next(TEXT()->cursor.row()) != TEXT()->rows().end())
926 TEXT()->cursorDown(view());
928 TEXT()->cursorRight(view());
929 moveCursorUpdate(true, false);
930 owner->view_state_changed();
931 goto exit_with_message;
943 if (!view()->available()) break;
944 // this function should be used always [asierra060396]
945 UpdatableInset * tli =
946 view()->theLockingInset();
948 UpdatableInset * lock = tli->getLockingInset();
951 view()->unlockInset(tli);
952 TEXT()->cursorRight(view());
953 moveCursorUpdate(true, false);
954 owner->view_state_changed();
956 tli->unlockInsetInInset(view(),
961 // Tell the paragraph dialog that we changed paragraph
962 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
967 // --- Misc -------------------------------------------
968 case LFUN_WORDFINDFORWARD :
969 case LFUN_WORDFINDBACKWARD : {
970 static string last_search;
971 string searched_string;
973 if (!argument.empty()) {
974 last_search = argument;
975 searched_string = argument;
977 searched_string = last_search;
979 bool fw = (action == LFUN_WORDFINDFORWARD);
980 if (!searched_string.empty()) {
981 lyxfind::LyXFind(view(), searched_string, fw);
988 if (view()->available() && !view()->theLockingInset()) {
989 view()->update(TEXT(), BufferView::SELECT);
991 owner->message(keyseq.printOptions());
995 // --- Misc -------------------------------------------
996 case LFUN_EXEC_COMMAND:
997 owner->focus_command_buffer();
1000 case LFUN_CANCEL: // RVDK_PATCH_5
1002 meta_fake_bit = key_modifier::none;
1003 if (view()->available())
1004 // cancel any selection
1005 dispatch(LFUN_MARK_OFF);
1006 setMessage(N_("Cancel"));
1009 case LFUN_META_FAKE: // RVDK_PATCH_5
1011 meta_fake_bit = key_modifier::alt;
1012 setMessage(keyseq.print());
1016 case LFUN_READ_ONLY_TOGGLE:
1017 if (owner->buffer()->lyxvc.inUse()) {
1018 owner->buffer()->lyxvc.toggleReadOnly();
1020 owner->buffer()->setReadonly(
1021 !owner->buffer()->isReadonly());
1025 case LFUN_CENTER: // this is center and redraw.
1029 // --- Menus -----------------------------------------------
1031 menuNew(argument, false);
1034 case LFUN_MENUNEWTMPLT:
1035 menuNew(argument, true);
1038 case LFUN_CLOSEBUFFER:
1042 case LFUN_MENUWRITE:
1043 if (!owner->buffer()->isUnnamed()) {
1044 string const str = bformat(_("Saving document %1$s..."),
1045 MakeDisplayPath(owner->buffer()->fileName()));
1046 owner->message(str);
1047 MenuWrite(view(), owner->buffer());
1048 owner->message(str + _(" done."));
1050 WriteAs(view(), owner->buffer());
1054 WriteAs(view(), owner->buffer(), argument);
1057 case LFUN_MENURELOAD: {
1058 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1059 string text = bformat(_("Any changes will be lost. Are you sure "
1060 "you want to revert to the saved version of the document %1$s?"), file);
1061 int const ret = Alert::prompt(_("Revert to saved document?"),
1062 text, 0, 1, _("&Revert"), _("&Cancel"));
1070 Exporter::Export(owner->buffer(), argument, true);
1074 Exporter::Preview(owner->buffer(), argument);
1077 case LFUN_BUILDPROG:
1078 Exporter::Export(owner->buffer(), "program", true);
1081 case LFUN_RUNCHKTEX:
1082 owner->buffer()->runChktex();
1085 case LFUN_MENUPRINT:
1086 owner->getDialogs().showPrint();
1090 if (argument == "custom")
1091 owner->getDialogs().showSendto();
1093 Exporter::Export(owner->buffer(), argument, false);
1106 InsetCommandParams p("tableofcontents");
1107 string const data = InsetCommandMailer::params2string("toc", p);
1108 owner->getDialogs().show("toc", data, 0);
1124 case LFUN_MENUSEARCH:
1125 owner->getDialogs().showSearch();
1128 case LFUN_REMOVEERRORS:
1129 if (view()->removeAutoInsets()) {
1130 #warning repaint() or update() or nothing ?
1132 view()->fitCursor();
1136 case LFUN_DEPTH_MIN:
1137 changeDepth(view(), TEXT(false), DEC_DEPTH, false);
1138 owner->view_state_changed();
1141 case LFUN_DEPTH_PLUS:
1142 changeDepth(view(), TEXT(false), INC_DEPTH, false);
1143 owner->view_state_changed();
1146 case LFUN_FREEFONT_APPLY:
1147 apply_freefont(view());
1150 case LFUN_FREEFONT_UPDATE:
1151 update_and_apply_freefont(view(), argument);
1154 case LFUN_RECONFIGURE:
1155 Reconfigure(view());
1159 case LFUN_FLOATSOPERATE:
1160 if (argument == "openfoot")
1161 view()->allFloats(1,0);
1162 else if (argument == "closefoot")
1163 view()->allFloats(0,0);
1164 else if (argument == "openfig")
1165 view()->allFloats(1,1);
1166 else if (argument == "closefig")
1167 view()->allFloats(0,1);
1170 #ifdef WITH_WARNINGS
1171 //#warning Find another implementation here (or another lyxfunc)!
1174 case LFUN_HELP_ABOUTLYX:
1175 owner->getDialogs().show("about");
1178 case LFUN_HELP_TEXINFO:
1179 owner->getDialogs().showTexinfo();
1182 case LFUN_HELP_OPEN:
1184 string const arg = argument;
1186 setErrorMessage(N_("Missing argument"));
1189 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1190 if (fname.empty()) {
1191 lyxerr << "LyX: unable to find documentation file `"
1192 << arg << "'. Bad installation?" << endl;
1195 owner->message(bformat(_("Opening help file %1$s..."),
1196 MakeDisplayPath(fname)));
1197 view()->buffer(bufferlist.loadLyXFile(fname, false));
1201 // --- version control -------------------------------
1202 case LFUN_VC_REGISTER:
1204 if (!owner->buffer()->lyxvc.inUse())
1205 owner->buffer()->lyxvc.registrer();
1209 case LFUN_VC_CHECKIN:
1211 if (owner->buffer()->lyxvc.inUse()
1212 && !owner->buffer()->isReadonly())
1213 owner->buffer()->lyxvc.checkIn();
1217 case LFUN_VC_CHECKOUT:
1219 if (owner->buffer()->lyxvc.inUse()
1220 && owner->buffer()->isReadonly())
1221 owner->buffer()->lyxvc.checkOut();
1225 case LFUN_VC_REVERT:
1227 owner->buffer()->lyxvc.revert();
1233 owner->buffer()->lyxvc.undoLast();
1237 case LFUN_VC_HISTORY:
1239 owner->getDialogs().show("vclog");
1243 // --- buffers ----------------------------------------
1245 case LFUN_SWITCHBUFFER:
1246 view()->buffer(bufferlist.getBuffer(argument));
1251 // servercmd: argument must be <file>:<template>
1252 Buffer * tmpbuf = NewFile(argument);
1254 view()->buffer(tmpbuf);
1258 case LFUN_FILE_OPEN:
1262 case LFUN_LATEX_LOG:
1263 owner->getDialogs().show("log");
1266 case LFUN_LAYOUT_DOCUMENT:
1267 owner->getDialogs().showDocument();
1270 case LFUN_LAYOUT_CHARACTER: {
1271 string data = freefont2string();
1273 owner->getDialogs().show("character", data);
1277 case LFUN_LAYOUT_TABULAR:
1278 if (view()->theLockingInset()) {
1279 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1280 InsetTabular * inset = static_cast<InsetTabular *>
1281 (view()->theLockingInset());
1282 inset->openLayoutDialog(view());
1283 } else if (view()->theLockingInset()->
1284 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1285 InsetTabular * inset = static_cast<InsetTabular *>(
1286 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1287 inset->openLayoutDialog(view());
1292 case LFUN_LAYOUT_PREAMBLE:
1293 owner->getDialogs().showPreamble();
1296 case LFUN_DROP_LAYOUTS_CHOICE:
1297 owner->getToolbar().openLayoutList();
1300 case LFUN_MENU_OPEN_BY_NAME:
1301 owner->getMenubar().openByName(argument);
1302 break; // RVDK_PATCH_5
1304 case LFUN_SPELLCHECK:
1305 owner->getDialogs().showSpellchecker();
1308 // --- lyxserver commands ----------------------------
1312 setMessage(owner->buffer()->fileName());
1313 lyxerr[Debug::INFO] << "FNAME["
1314 << owner->buffer()->fileName()
1320 dispatch_buffer = keyseq.print();
1321 lyxserver->notifyClient(dispatch_buffer);
1325 case LFUN_GOTOFILEROW:
1329 istringstream istr(argument.c_str());
1330 istr >> file_name >> row;
1331 // Must replace extension of the file to be .lyx and get full path
1332 string const s(ChangeExtension(file_name, ".lyx"));
1334 // Either change buffer or load the file
1335 if (bufferlist.exists(s)) {
1336 view()->buffer(bufferlist.getBuffer(s));
1338 view()->buffer(bufferlist.loadLyXFile(s));
1341 view()->setCursorFromRow(row);
1344 // see BufferView_pimpl::center()
1345 view()->updateScrollbar();
1349 case LFUN_GOTO_PARAGRAPH:
1351 istringstream istr(argument.c_str());
1355 Paragraph * par = &*owner->buffer()->getParFromID(id);
1357 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1358 << id << ']' << endl;
1361 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1362 << " found." << endl;
1365 if (view()->theLockingInset())
1366 view()->unlockInset(view()->theLockingInset());
1367 if (par->inInset()) {
1368 par->inInset()->edit(view());
1371 view()->getLyXText()->setCursor(par, 0);
1372 view()->switchKeyMap();
1373 owner->view_state_changed();
1376 // see BufferView_pimpl::center()
1377 view()->updateScrollbar();
1381 // --- insert characters ----------------------------------------
1383 // --- Mathed stuff. If we are here, there is no locked inset yet.
1384 case LFUN_MATH_EXTERN:
1385 case LFUN_MATH_NUMBER:
1386 case LFUN_MATH_NONUMBER:
1387 case LFUN_MATH_LIMITS:
1389 setErrorMessage(N_("This is only allowed in math mode!"));
1393 // passthrough hat and underscore outside mathed:
1394 case LFUN_SUBSCRIPT:
1395 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1397 case LFUN_SUPERSCRIPT:
1398 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1401 case LFUN_MATH_PANEL:
1402 owner->getDialogs().showMathPanel();
1405 case LFUN_DIALOG_SHOW_NEW_INSET: {
1406 string const & name = argument;
1408 if (name == "bibitem" ||
1410 name == "include" ||
1415 InsetCommandParams p(name);
1416 data = InsetCommandMailer::params2string(name, p);
1417 } else if (name == "citation") {
1418 InsetCommandParams p("cite");
1419 data = InsetCommandMailer::params2string(name, p);
1421 owner->getDialogs().show(name, data, 0);
1425 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1429 case LFUN_DIALOG_UPDATE: {
1430 string const & name = argument;
1431 // Can only update a dialog connected to an existing inset
1432 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1434 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1436 inset->localDispatch(fr);
1437 } else if (name == "paragraph") {
1438 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1443 case LFUN_DIALOG_HIDE:
1444 Dialogs::hide(argument, 0);
1447 case LFUN_DIALOG_DISCONNECT_INSET:
1448 owner->getDialogs().disconnect(argument);
1451 case LFUN_CHILDOPEN:
1453 string const filename =
1454 MakeAbsPath(argument,
1455 owner->buffer()->filePath());
1456 setMessage(N_("Opening child document ") +
1457 MakeDisplayPath(filename) + "...");
1458 view()->savePosition(0);
1459 if (bufferlist.exists(filename))
1460 view()->buffer(bufferlist.getBuffer(filename));
1462 view()->buffer(bufferlist.loadLyXFile(filename));
1466 case LFUN_TOGGLECURSORFOLLOW:
1467 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1471 owner->getIntl().KeyMapOn(false);
1474 case LFUN_KMAP_PRIM:
1475 owner->getIntl().KeyMapPrim();
1479 owner->getIntl().KeyMapSec();
1482 case LFUN_KMAP_TOGGLE:
1483 owner->getIntl().ToggleKeyMap();
1488 // argument contains ';'-terminated commands
1489 while (!argument.empty()) {
1491 argument = split(argument, first, ';');
1497 case LFUN_DIALOG_PREFERENCES:
1498 owner->getDialogs().showPreferences();
1501 case LFUN_SAVEPREFERENCES:
1503 Path p(user_lyxdir);
1504 lyxrc.write("preferences");
1508 case LFUN_SCREEN_FONT_UPDATE:
1510 // handle the screen font changes.
1511 lyxrc.set_font_norm_type();
1512 lyx_gui::update_fonts();
1513 // We also need to empty the textcache so that
1514 // the buffer will be formatted correctly after
1517 // Of course we should only do the resize and the textcache.clear
1518 // if values really changed...but not very important right now. (Lgb)
1519 // All visible buffers will need resize
1525 case LFUN_SET_COLOR:
1528 string const x11_name = split(argument, lyx_name, ' ');
1529 if (lyx_name.empty() || x11_name.empty()) {
1530 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1535 bool const graphicsbg_changed =
1536 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1537 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1539 if (!lcolor.setColor(lyx_name, x11_name)) {
1541 bformat(_("Set-color \"%1$s\" failed "
1542 "- color is undefined or "
1543 "may not be redefined"), lyx_name));
1547 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1549 if (graphicsbg_changed) {
1550 #ifdef WITH_WARNINGS
1551 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1554 grfx::GCache & gc = grfx::GCache::get();
1555 gc.changeDisplay(true);
1564 owner->message(argument);
1567 case LFUN_FORKS_SHOW:
1568 owner->getDialogs().showForks();
1571 case LFUN_FORKS_KILL:
1573 if (!isStrInt(argument))
1576 pid_t const pid = strToInt(argument);
1577 ForkedcallsController & fcc = ForkedcallsController::get();
1582 case LFUN_TOOLTIPS_TOGGLE:
1583 owner->getDialogs().toggleTooltips();
1587 // Then if it was none of the above
1588 // Trying the BufferView::pimpl dispatch:
1589 if (!view()->dispatch(ev))
1590 lyxerr << "A truly unknown func ["
1591 << lyxaction.getActionName(ev.action) << "]!"
1598 view()->owner()->updateLayoutChoice();
1600 if (view()->available()) {
1601 view()->fitCursor();
1603 // If we executed a mutating lfun, mark the buffer as dirty
1604 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1605 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1606 view()->buffer()->markDirty();
1609 sendDispatchMessage(getMessage(), ev, verbose);
1613 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1615 owner->updateMenubar();
1616 owner->updateToolbar();
1618 if (ev.action == LFUN_SELFINSERT || !verbose) {
1619 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1621 owner->message(msg);
1625 string dispatch_msg = msg;
1626 if (!dispatch_msg.empty())
1627 dispatch_msg += ' ';
1629 string comname = lyxaction.getActionName(ev.action);
1631 int pseudoaction = ev.action;
1632 bool argsadded = false;
1634 if (!ev.argument.empty()) {
1635 // the pseudoaction is useful for the bindings
1636 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1638 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1639 pseudoaction = ev.action;
1641 comname += ' ' + ev.argument;
1646 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1648 if (!shortcuts.empty()) {
1649 comname += ": " + shortcuts;
1650 } else if (!argsadded && !ev.argument.empty()) {
1651 comname += ' ' + ev.argument;
1654 if (!comname.empty()) {
1655 comname = rtrim(comname);
1656 dispatch_msg += '(' + comname + ')';
1659 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1660 if (!dispatch_msg.empty())
1661 owner->message(dispatch_msg);
1665 void LyXFunc::setupLocalKeymap()
1667 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1668 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1672 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1674 string initpath = lyxrc.document_path;
1675 string filename(name);
1677 if (view()->available()) {
1678 string const trypath = owner->buffer()->filePath();
1679 // If directory is writeable, use this as default.
1680 if (IsDirWriteable(trypath))
1684 static int newfile_number;
1686 if (filename.empty()) {
1687 filename = AddName(lyxrc.document_path,
1688 "newfile" + tostr(++newfile_number) + ".lyx");
1689 FileInfo fi(filename);
1690 while (bufferlist.exists(filename) || fi.readable()) {
1692 filename = AddName(lyxrc.document_path,
1693 "newfile" + tostr(newfile_number) +
1695 fi.newFile(filename);
1699 // The template stuff
1702 FileDialog fileDlg(_("Select template file"),
1703 LFUN_SELECT_FILE_SYNC,
1704 make_pair(string(_("Documents|#o#O")),
1705 string(lyxrc.document_path)),
1706 make_pair(string(_("Templates|#T#t")),
1707 string(lyxrc.template_path)));
1709 FileDialog::Result result =
1710 fileDlg.open(lyxrc.template_path,
1711 _("*.lyx| LyX Documents (*.lyx)"));
1713 if (result.first == FileDialog::Later)
1716 string const fname = result.second;
1723 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1727 void LyXFunc::open(string const & fname)
1729 string initpath = lyxrc.document_path;
1731 if (view()->available()) {
1732 string const trypath = owner->buffer()->filePath();
1733 // If directory is writeable, use this as default.
1734 if (IsDirWriteable(trypath))
1740 if (fname.empty()) {
1741 FileDialog fileDlg(_("Select document to open"),
1743 make_pair(string(_("Documents|#o#O")),
1744 string(lyxrc.document_path)),
1745 make_pair(string(_("Examples|#E#e")),
1746 string(AddPath(system_lyxdir, "examples"))));
1748 FileDialog::Result result =
1749 fileDlg.open(initpath,
1750 _("*.lyx| LyX Documents (*.lyx)"));
1752 if (result.first == FileDialog::Later)
1755 filename = result.second;
1757 // check selected filename
1758 if (filename.empty()) {
1759 owner->message(_("Canceled."));
1765 // get absolute path of file and add ".lyx" to the filename if
1767 string const fullpath = FileSearch(string(), filename, "lyx");
1768 if (!fullpath.empty()) {
1769 filename = fullpath;
1772 string const disp_fn(MakeDisplayPath(filename));
1774 // if the file doesn't exist, let the user create one
1775 FileInfo const f(filename, true);
1777 // the user specifically chose this name. Believe them.
1778 Buffer * buffer = bufferlist.newFile(filename, "", true);
1779 view()->buffer(buffer);
1783 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1785 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1788 view()->buffer(openbuf);
1789 str2 = bformat(_("Document %1$s opened."), disp_fn);
1791 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1793 owner->message(str2);
1797 void LyXFunc::doImport(string const & argument)
1800 string filename = split(argument, format, ' ');
1802 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1803 << " file: " << filename << endl;
1805 // need user interaction
1806 if (filename.empty()) {
1807 string initpath = lyxrc.document_path;
1809 if (view()->available()) {
1810 string const trypath = owner->buffer()->filePath();
1811 // If directory is writeable, use this as default.
1812 if (IsDirWriteable(trypath))
1816 string const text = bformat(_("Select %1$s file to import"),
1817 formats.prettyName(format));
1819 FileDialog fileDlg(text,
1821 make_pair(string(_("Documents|#o#O")),
1822 string(lyxrc.document_path)),
1823 make_pair(string(_("Examples|#E#e")),
1824 string(AddPath(system_lyxdir, "examples"))));
1826 string const extension = "*." + formats.extension(format)
1827 + "| " + formats.prettyName(format)
1828 + " (*." + formats.extension(format) + ')';
1830 FileDialog::Result result = fileDlg.open(initpath,
1833 if (result.first == FileDialog::Later)
1836 filename = result.second;
1838 // check selected filename
1839 if (filename.empty())
1840 owner->message(_("Canceled."));
1843 if (filename.empty())
1846 // get absolute path of file
1847 filename = MakeAbsPath(filename);
1849 string const lyxfile = ChangeExtension(filename, ".lyx");
1851 // Check if the document already is open
1852 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1853 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1854 owner->message(_("Canceled."));
1859 // if the file exists already, and we didn't do
1860 // -i lyx thefile.lyx, warn
1861 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1862 string const file = MakeDisplayPath(lyxfile, 30);
1864 string text = bformat(_("The document %1$s already exists.\n\n"
1865 "Do you want to over-write that document?"), file);
1866 int const ret = Alert::prompt(_("Over-write document?"),
1867 text, 0, 1, _("&Over-write"), _("&Cancel"));
1870 owner->message(_("Canceled."));
1875 Importer::Import(owner, filename, format);
1879 void LyXFunc::closeBuffer()
1881 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1882 if (bufferlist.empty()) {
1883 // need this otherwise SEGV may occur while trying to
1884 // set variables that don't exist
1885 // since there's no current buffer
1886 owner->getDialogs().hideBufferDependent();
1888 view()->buffer(bufferlist.first());
1894 // Each "owner" should have it's own message method. lyxview and
1895 // the minibuffer would use the minibuffer, but lyxserver would
1896 // send an ERROR signal to its client. Alejandro 970603
1897 // This func is bit problematic when it comes to NLS, to make the
1898 // lyx servers client be language indepenent we must not translate
1899 // strings sent to this func.
1900 void LyXFunc::setErrorMessage(string const & m) const
1902 dispatch_buffer = m;
1907 void LyXFunc::setMessage(string const & m) const
1909 dispatch_buffer = m;
1913 void LyXFunc::setStatusMessage(string const & m) const
1919 string const LyXFunc::view_status_message()
1921 // When meta-fake key is pressed, show the key sequence so far + "M-".
1923 return keyseq.print() + "M-";
1926 // Else, when a non-complete key sequence is pressed,
1927 // show the available options.
1928 if (keyseq.length() > 0 && !keyseq.deleted()) {
1929 return keyseq.printOptions();
1932 if (!view()->available())
1933 return _("Welcome to LyX!");
1935 return currentState(view());
1939 BufferView * LyXFunc::view() const
1942 return owner->view().get();