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/tostr.h"
67 #include "support/path.h"
68 #include "support/lyxfunctional.h"
84 using std::back_inserter;
85 using namespace bv_funcs;
87 extern BufferList bufferlist;
88 extern LyXServer * lyxserver;
89 extern bool selection_possible;
91 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
94 extern tex_accent_struct get_accent(kb_action action);
96 extern void ShowLatexLog();
99 LyXFunc::LyXFunc(LyXView * o)
102 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
103 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
104 meta_fake_bit(key_modifier::none)
110 LyXText * LyXFunc::TEXT(bool flag = true) const
114 return view()->getLyXText();
119 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
121 if (selecting || TEXT(flag)->selection.mark()) {
122 TEXT(flag)->setSelection();
123 if (!TEXT(flag)->isInInset())
124 view()->toggleToggle();
126 view()->update(TEXT(flag), BufferView::SELECT);
128 view()->switchKeyMap();
132 void LyXFunc::handleKeyFunc(kb_action action)
134 char c = encoded_last_key;
136 if (keyseq.length()) {
140 owner->getIntl().getTransManager()
141 .deadkey(c, get_accent(action).accent, TEXT(false));
142 // Need to clear, in case the minibuffer calls these
145 // copied verbatim from do_accent_char
146 view()->update(TEXT(false), BufferView::SELECT);
147 TEXT(false)->selection.cursor = TEXT(false)->cursor;
151 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
152 key_modifier::state state)
156 if (lyxerr.debugging(Debug::KEY)) {
157 lyxerr << "KeySym is "
158 << keysym->getSymbolName()
162 // Do nothing if we have nothing (JMarc)
163 if (!keysym->isOK()) {
164 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
169 if (keysym->isModifier()) {
170 lyxerr[Debug::KEY] << "isModifier true" << endl;
174 Encoding const * encoding = view()->getEncoding();
176 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
178 // Do a one-deep top-level lookup for
179 // cancel and meta-fake keys. RVDK_PATCH_5
180 cancel_meta_seq.reset();
182 int action = cancel_meta_seq.addkey(keysym, state);
183 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
185 // When not cancel or meta-fake, do the normal lookup.
186 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
187 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
188 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
189 // remove Caps Lock and Mod2 as a modifiers
190 action = keyseq.addkey(keysym, (state | meta_fake_bit));
191 lyxerr[Debug::KEY] << "action now set to ["
192 << action << ']' << endl;
195 // Dont remove this unless you know what you are doing.
196 meta_fake_bit = key_modifier::none;
198 // can this happen now ?
199 if (action == LFUN_NOACTION) {
200 action = LFUN_PREFIX;
203 if (lyxerr.debugging(Debug::KEY)) {
204 lyxerr << "Key [action="
206 << keyseq.print() << ']'
210 // already here we know if it any point in going further
211 // why not return already here if action == -1 and
212 // num_bytes == 0? (Lgb)
214 if (keyseq.length() > 1) {
215 owner->message(keyseq.print());
219 // Maybe user can only reach the key via holding down shift.
220 // Let's see. But only if shift is the only modifier
221 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
222 lyxerr[Debug::KEY] << "Trying without shift" << endl;
223 action = keyseq.addkey(keysym, key_modifier::none);
224 lyxerr[Debug::KEY] << "Action now " << action << endl;
227 if (action == LFUN_UNKNOWN_ACTION) {
228 // Hmm, we didn't match any of the keysequences. See
229 // if it's normal insertable text not already covered
231 if (keysym->isText() && keyseq.length() == 1) {
232 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
233 action = LFUN_SELFINSERT;
235 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
236 owner->message(_("Unknown function."));
241 if (action == LFUN_SELFINSERT) {
242 if (encoded_last_key != 0) {
244 arg += encoded_last_key;
246 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
248 lyxerr[Debug::KEY] << "SelfInsert arg[`"
249 << argument << "']" << endl;
257 FuncStatus LyXFunc::getStatus(int ac) const
259 return getStatus(lyxaction.retrieveActionArg(ac));
263 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
266 Buffer * buf = owner->buffer();
268 if (ev.action == LFUN_NOACTION) {
269 setStatusMessage(N_("Nothing to do"));
270 return flag.disabled(true);
274 case LFUN_UNKNOWN_ACTION:
275 #ifndef HAVE_LIBAIKSAURUS
276 case LFUN_THESAURUS_ENTRY:
281 flag |= lyx_gui::getStatus(ev);
284 if (flag.unknown()) {
285 setStatusMessage(N_("Unknown action"));
289 // the default error message if we disable the command
290 setStatusMessage(N_("Command disabled"));
292 // Check whether we need a buffer
293 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
294 // Yes we need a buffer, do we have one?
297 // Can we use a readonly buffer?
298 if (buf->isReadonly() &&
299 !lyxaction.funcHasFlag(ev.action,
300 LyXAction::ReadOnly)) {
302 setStatusMessage(N_("Document is read-only"));
307 setStatusMessage(N_("Command not allowed with"
308 "out any document open"));
309 return flag.disabled(true);
313 UpdatableInset * tli = view()->theLockingInset();
315 // I would really like to avoid having this switch and rather try to
316 // encode this in the function itself.
317 bool disable = false;
320 disable = !Exporter::IsExportable(buf, "dvi")
321 || lyxrc.print_command == "none";
324 disable = ev.argument != "custom"
325 && !Exporter::IsExportable(buf, ev.argument);
328 disable = buf->undostack.empty();
331 disable = buf->redostack.empty();
336 UpdatableInset * in = tli;
337 if (in->lyxCode() != Inset::TABULAR_CODE) {
338 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
340 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
345 disable = !mathcursor && !view()->getLyXText()->selection.set();
348 disable = !buf->isLatex() || lyxrc.chktex_command == "none";
351 disable = !Exporter::IsExportable(buf, "program");
354 case LFUN_LAYOUT_CHARACTER:
355 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
358 case LFUN_LAYOUT_TABULAR:
360 || (tli->lyxCode() != Inset::TABULAR_CODE
361 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
365 disable = !changeDepth(view(), TEXT(false), DEC_DEPTH, true);
368 case LFUN_DEPTH_PLUS:
369 disable = !changeDepth(view(), TEXT(false), INC_DEPTH, true);
373 case LFUN_LAYOUT_PARAGRAPH: {
374 Inset * inset = TEXT(false)->cursor.par()->inInset();
375 disable = inset && inset->forceDefaultParagraphs(inset);
379 case LFUN_INSET_OPTARG:
380 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
383 case LFUN_TABULAR_FEATURE:
386 // FIXME: check temporarily disabled
388 char align = mathcursor->valign();
393 if (ev.argument.empty()) {
397 if (!contains("tcb", ev.argument[0])) {
401 flag.setOnOff(ev.argument[0] == align);
405 char align = mathcursor->halign();
410 if (ev.argument.empty()) {
414 if (!contains("lcr", ev.argument[0])) {
418 flag.setOnOff(ev.argument[0] == align);
421 disable = !mathcursor->halign();
427 //ret.disabled(true);
428 if (tli->lyxCode() == Inset::TABULAR_CODE) {
429 ret = static_cast<InsetTabular *>(tli)
430 ->getStatus(ev.argument);
433 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
434 ret = static_cast<InsetTabular *>
435 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
436 ->getStatus(ev.argument);
443 static InsetTabular inset(*owner->buffer(), 1, 1);
447 ret = inset.getStatus(ev.argument);
448 if (ret.onoff(true) || ret.onoff(false))
449 flag.setOnOff(false);
453 case LFUN_VC_REGISTER:
454 disable = buf->lyxvc.inUse();
456 case LFUN_VC_CHECKIN:
457 disable = !buf->lyxvc.inUse() || buf->isReadonly();
459 case LFUN_VC_CHECKOUT:
460 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
464 case LFUN_VC_HISTORY:
465 disable = !buf->lyxvc.inUse();
467 case LFUN_MENURELOAD:
468 disable = buf->isUnnamed() || buf->isClean();
470 case LFUN_BOOKMARK_GOTO:
472 isSavedPosition(strToUnsignedInt(ev.argument));
474 case LFUN_MERGE_CHANGES:
475 case LFUN_ACCEPT_CHANGE:
476 case LFUN_REJECT_CHANGE:
477 case LFUN_ACCEPT_ALL_CHANGES:
478 case LFUN_REJECT_ALL_CHANGES:
479 disable = !buf->params.tracking_changes;
481 case LFUN_INSET_TOGGLE: {
482 LyXText * lt = view()->getLyXText();
483 disable = !(isEditableInset(lt->getInset())
485 && lt->inset_owner->owner()
486 && lt->inset_owner->owner()->isOpen()));
490 case LFUN_INSET_SETTINGS: {
492 UpdatableInset * inset = view()->theLockingInset();
497 // get the innermost inset
498 inset = inset->getLockingInset();
500 // jump back to owner if an InsetText, so
501 // we get back to the InsetTabular or whatever
502 if (inset->lyxCode() == Inset::TEXT_CODE)
503 inset = static_cast<UpdatableInset*>(inset->owner());
505 Inset::Code code = inset->lyxCode();
507 case Inset::TABULAR_CODE:
508 disable = ev.argument != "tabular";
510 case Inset::ERT_CODE:
511 disable = ev.argument != "ert";
513 case Inset::FLOAT_CODE:
514 disable = ev.argument != "float";
516 case Inset::MINIPAGE_CODE:
517 disable = ev.argument != "minipage";
519 case Inset::WRAP_CODE:
520 disable = ev.argument != "wrap";
529 disable = !IsFileReadable(buf->getLogName().second);
532 case LFUN_MATH_MUTATE:
534 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
535 flag.setOnOff(false);
540 // we just need to be in math mode to enable that
542 case LFUN_MATH_SPACE:
543 case LFUN_MATH_LIMITS:
544 case LFUN_MATH_NONUMBER:
545 case LFUN_MATH_NUMBER:
546 case LFUN_MATH_EXTERN:
547 disable = !mathcursor;
554 // the functions which insert insets
555 Inset::Code code = Inset::NO_CODE;
557 case LFUN_DIALOG_SHOW_NEW_INSET:
558 if (ev.argument == "bibitem")
559 code = Inset::BIBITEM_CODE;
560 else if (ev.argument == "bibtex")
561 code = Inset::BIBTEX_CODE;
562 else if (ev.argument == "citation")
563 code = Inset::CITE_CODE;
564 else if (ev.argument == "ert")
565 code = Inset::ERT_CODE;
566 else if (ev.argument == "external")
567 code = Inset::EXTERNAL_CODE;
568 else if (ev.argument == "float")
569 code = Inset::FLOAT_CODE;
570 else if (ev.argument == "graphics")
571 code = Inset::GRAPHICS_CODE;
572 else if (ev.argument == "include")
573 code = Inset::INCLUDE_CODE;
574 else if (ev.argument == "index")
575 code = Inset::INDEX_CODE;
576 else if (ev.argument == "label")
577 code = Inset::LABEL_CODE;
578 else if (ev.argument == "minipage")
579 code = Inset::MINIPAGE_CODE;
580 else if (ev.argument == "ref")
581 code = Inset::REF_CODE;
582 else if (ev.argument == "toc")
583 code = Inset::TOC_CODE;
584 else if (ev.argument == "url")
585 code = Inset::URL_CODE;
586 else if (ev.argument == "wrap")
587 code = Inset::WRAP_CODE;
591 code = Inset::ERT_CODE;
593 case LFUN_INSET_FOOTNOTE:
594 code = Inset::FOOT_CODE;
596 case LFUN_TABULAR_INSERT:
597 code = Inset::TABULAR_CODE;
599 case LFUN_INSET_MARGINAL:
600 code = Inset::MARGIN_CODE;
602 case LFUN_INSET_MINIPAGE:
603 code = Inset::MINIPAGE_CODE;
605 case LFUN_INSET_FLOAT:
606 case LFUN_INSET_WIDE_FLOAT:
607 code = Inset::FLOAT_CODE;
609 case LFUN_INSET_WRAP:
610 code = Inset::WRAP_CODE;
612 case LFUN_FLOAT_LIST:
613 code = Inset::FLOAT_LIST_CODE;
616 case LFUN_INSET_LIST:
617 code = Inset::LIST_CODE;
619 case LFUN_INSET_THEOREM:
620 code = Inset::THEOREM_CODE;
623 case LFUN_INSET_CAPTION:
624 code = Inset::CAPTION_CODE;
626 case LFUN_INSERT_NOTE:
627 code = Inset::NOTE_CODE;
629 case LFUN_INSERT_LABEL:
630 code = Inset::LABEL_CODE;
632 case LFUN_INSET_OPTARG:
633 code = Inset::OPTARG_CODE;
635 case LFUN_ENVIRONMENT_INSERT:
636 code = Inset::MINIPAGE_CODE;
638 case LFUN_INDEX_INSERT:
639 code = Inset::INDEX_CODE;
641 case LFUN_INDEX_PRINT:
642 code = Inset::INDEX_PRINT_CODE;
644 case LFUN_TOC_INSERT:
645 code = Inset::TOC_CODE;
647 case LFUN_PARENTINSERT:
648 code = Inset::PARENT_CODE;
652 code = Inset::URL_CODE;
655 // always allow this, since we will inset a raw quote
656 // if an inset is not allowed.
658 case LFUN_HYPHENATION:
659 case LFUN_LIGATURE_BREAK:
661 case LFUN_MENU_SEPARATOR:
663 case LFUN_END_OF_SENTENCE:
664 code = Inset::SPECIALCHAR_CODE;
666 case LFUN_PROTECTEDSPACE:
667 // slight hack: we know this is allowed in math mode
669 code = Inset::SPECIALCHAR_CODE;
674 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
680 // A few general toggles
682 case LFUN_TOOLTIPS_TOGGLE:
683 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
686 case LFUN_READ_ONLY_TOGGLE:
687 flag.setOnOff(buf->isReadonly());
690 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
692 case LFUN_SWITCHBUFFER:
693 // toggle on the current buffer, but do not toggle off
694 // the other ones (is that a good idea?)
695 if (ev.argument == buf->fileName())
698 case LFUN_TRACK_CHANGES:
699 flag.setOnOff(buf->params.tracking_changes);
705 // the font related toggles
707 LyXFont const & font = TEXT(false)->real_current_font;
710 flag.setOnOff(font.emph() == LyXFont::ON);
713 flag.setOnOff(font.noun() == LyXFont::ON);
716 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
719 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
722 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
725 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
731 string tc = mathcursor->getLastCode();
734 flag.setOnOff(tc == "mathbf");
737 flag.setOnOff(tc == "mathsf");
740 flag.setOnOff(tc == "mathcal");
743 flag.setOnOff(tc == "mathrm");
746 flag.setOnOff(tc == "mathtt");
749 flag.setOnOff(tc == "mathbb");
752 flag.setOnOff(tc == "mathnormal");
759 // this one is difficult to get right. As a half-baked
760 // solution, we consider only the first action of the sequence
761 if (ev.action == LFUN_SEQUENCE) {
762 // argument contains ';'-terminated commands
763 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
770 void LyXFunc::dispatch(string const & s, bool verbose)
772 int const action = lyxaction.LookupFunc(s);
774 if (action == LFUN_UNKNOWN_ACTION) {
775 owner->message(bformat(_("Unknown function (%1$s)"), s));
779 dispatch(action, verbose);
783 void LyXFunc::dispatch(int ac, bool verbose)
785 dispatch(lyxaction.retrieveActionArg(ac), verbose);
790 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
792 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
793 <<"] arg[" << ev.argument << ']' << endl;
795 // we have not done anything wrong yet.
797 dispatch_buffer.erase();
799 #ifdef NEW_DISPATCHER
800 // We try do call the most specific dispatcher first:
801 // 1. the lockinginset's dispatch
802 // 2. the bufferview's dispatch
803 // 3. the lyxview's dispatch
806 selection_possible = false;
808 string argument = ev.argument;
809 kb_action action = ev.action;
811 // We cannot use this function here
812 if (getStatus(ev).disabled()) {
813 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
814 << lyxaction.getActionName(action)
815 << " [" << action << "] is disabled at this location"
817 setErrorMessage(getStatusMessage());
818 goto exit_with_message;
821 if (view()->available())
822 view()->hideCursor();
824 if (view()->available() && view()->theLockingInset()) {
825 Inset::RESULT result;
826 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
827 (!keyseq.deleted())))
829 UpdatableInset * inset = view()->theLockingInset();
833 inset->getCursorPos(view(), inset_x, dummy_y);
835 if ((action == LFUN_UNKNOWN_ACTION)
836 && argument.empty()) {
837 argument = encoded_last_key;
839 // Undo/Redo is a bit tricky for insets.
840 if (action == LFUN_UNDO) {
842 goto exit_with_message;
843 } else if (action == LFUN_REDO) {
845 goto exit_with_message;
846 } else if (((result=inset->
847 // Hand-over to inset's own dispatch:
848 localDispatch(FuncRequest(view(), action, argument))) ==
850 (result == DISPATCHED_NOUPDATE))
851 goto exit_with_message;
852 // If UNDISPATCHED, just soldier on
853 else if (result == FINISHED) {
854 owner->clearMessage();
855 goto exit_with_message;
856 // We do not need special RTL handling here:
857 // FINISHED means that the cursor should be
858 // one position after the inset.
859 } else if (result == FINISHED_RIGHT) {
860 TEXT()->cursorRight(view());
861 moveCursorUpdate(true, false);
862 owner->clearMessage();
863 goto exit_with_message;
864 } else if (result == FINISHED_UP) {
865 if (TEXT()->cursor.irow() != TEXT()->rows().begin()) {
867 TEXT()->setCursorFromCoordinates(
868 TEXT()->cursor.ix() + inset_x,
869 TEXT()->cursor.iy() -
870 TEXT()->cursor.irow()->baseline() - 1);
871 TEXT()->cursor.x_fix(TEXT()->cursor.x());
873 TEXT()->cursorUp(view());
875 moveCursorUpdate(true, false);
877 view()->update(TEXT(), BufferView::SELECT);
879 owner->clearMessage();
880 goto exit_with_message;
881 } else if (result == FINISHED_DOWN) {
882 if (boost::next(TEXT()->cursor.irow()) != TEXT()->rows().end()) {
884 TEXT()->setCursorFromCoordinates(
885 TEXT()->cursor.ix() + inset_x,
886 TEXT()->cursor.iy() -
887 TEXT()->cursor.irow()->baseline() +
888 TEXT()->cursor.irow()->height() + 1);
889 TEXT()->cursor.x_fix(TEXT()->cursor.x());
891 TEXT()->cursorDown(view());
894 TEXT()->cursorRight(view());
896 moveCursorUpdate(true, false);
897 owner->clearMessage();
898 goto exit_with_message;
900 #warning I am not sure this is still right, please have a look! (Jug 20020417)
901 else { // result == UNDISPATCHED
902 //setMessage(N_("Text mode"));
904 case LFUN_UNKNOWN_ACTION:
905 case LFUN_BREAKPARAGRAPH:
907 TEXT()->cursorRight(view());
908 view()->switchKeyMap();
909 owner->view_state_changed();
912 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
913 TEXT()->cursorRight(view());
914 moveCursorUpdate(true, false);
915 owner->view_state_changed();
917 goto exit_with_message;
919 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
920 TEXT()->cursorRight(view());
921 moveCursorUpdate(true, false);
922 owner->view_state_changed();
924 goto exit_with_message;
926 if (boost::next(TEXT()->cursor.row()) != TEXT()->rows().end())
927 TEXT()->cursorDown(view());
929 TEXT()->cursorRight(view());
930 moveCursorUpdate(true, false);
931 owner->view_state_changed();
932 goto exit_with_message;
944 if (!view()->available()) break;
945 // this function should be used always [asierra060396]
946 UpdatableInset * tli =
947 view()->theLockingInset();
949 UpdatableInset * lock = tli->getLockingInset();
952 view()->unlockInset(tli);
953 TEXT()->cursorRight(view());
954 moveCursorUpdate(true, false);
955 owner->view_state_changed();
957 tli->unlockInsetInInset(view(),
962 // Tell the paragraph dialog that we changed paragraph
963 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
968 // --- Misc -------------------------------------------
969 case LFUN_WORDFINDFORWARD :
970 case LFUN_WORDFINDBACKWARD : {
971 static string last_search;
972 string searched_string;
974 if (!argument.empty()) {
975 last_search = argument;
976 searched_string = argument;
978 searched_string = last_search;
980 bool fw = (action == LFUN_WORDFINDFORWARD);
981 if (!searched_string.empty()) {
982 lyxfind::LyXFind(view(), searched_string, fw);
989 if (view()->available() && !view()->theLockingInset()) {
990 view()->update(TEXT(), BufferView::SELECT);
992 owner->message(keyseq.printOptions());
996 // --- Misc -------------------------------------------
997 case LFUN_EXEC_COMMAND:
998 owner->focus_command_buffer();
1001 case LFUN_CANCEL: // RVDK_PATCH_5
1003 meta_fake_bit = key_modifier::none;
1004 if (view()->available())
1005 // cancel any selection
1006 dispatch(LFUN_MARK_OFF);
1007 setMessage(N_("Cancel"));
1010 case LFUN_META_FAKE: // RVDK_PATCH_5
1012 meta_fake_bit = key_modifier::alt;
1013 setMessage(keyseq.print());
1017 case LFUN_READ_ONLY_TOGGLE:
1018 if (owner->buffer()->lyxvc.inUse()) {
1019 owner->buffer()->lyxvc.toggleReadOnly();
1021 owner->buffer()->setReadonly(
1022 !owner->buffer()->isReadonly());
1026 case LFUN_CENTER: // this is center and redraw.
1030 // --- Menus -----------------------------------------------
1032 menuNew(argument, false);
1035 case LFUN_MENUNEWTMPLT:
1036 menuNew(argument, true);
1039 case LFUN_CLOSEBUFFER:
1043 case LFUN_MENUWRITE:
1044 if (!owner->buffer()->isUnnamed()) {
1045 string const str = bformat(_("Saving document %1$s..."),
1046 MakeDisplayPath(owner->buffer()->fileName()));
1047 owner->message(str);
1048 MenuWrite(view(), owner->buffer());
1049 owner->message(str + _(" done."));
1051 WriteAs(view(), owner->buffer());
1055 WriteAs(view(), owner->buffer(), argument);
1058 case LFUN_MENURELOAD: {
1059 string const file = MakeDisplayPath(view()->buffer()->fileName(), 20);
1060 string text = bformat(_("Any changes will be lost. Are you sure "
1061 "you want to revert to the saved version of the document %1$s?"), file);
1062 int const ret = Alert::prompt(_("Revert to saved document?"),
1063 text, 0, 1, _("&Revert"), _("&Cancel"));
1071 Exporter::Export(owner->buffer(), argument, true);
1075 Exporter::Preview(owner->buffer(), argument);
1078 case LFUN_BUILDPROG:
1079 Exporter::Export(owner->buffer(), "program", true);
1082 case LFUN_RUNCHKTEX:
1083 owner->buffer()->runChktex();
1086 case LFUN_MENUPRINT:
1087 owner->getDialogs().showPrint();
1091 if (argument == "custom")
1092 owner->getDialogs().showSendto();
1094 Exporter::Export(owner->buffer(), argument, false);
1107 InsetCommandParams p("tableofcontents");
1108 string const data = InsetCommandMailer::params2string("toc", p);
1109 owner->getDialogs().show("toc", data, 0);
1125 case LFUN_MENUSEARCH:
1126 owner->getDialogs().showSearch();
1129 case LFUN_REMOVEERRORS:
1130 if (view()->removeAutoInsets()) {
1131 #warning repaint() or update() or nothing ?
1133 view()->fitCursor();
1137 case LFUN_DEPTH_MIN:
1138 changeDepth(view(), TEXT(false), DEC_DEPTH, false);
1139 owner->view_state_changed();
1142 case LFUN_DEPTH_PLUS:
1143 changeDepth(view(), TEXT(false), INC_DEPTH, false);
1144 owner->view_state_changed();
1147 case LFUN_FREEFONT_APPLY:
1148 apply_freefont(view());
1151 case LFUN_FREEFONT_UPDATE:
1152 update_and_apply_freefont(view(), argument);
1155 case LFUN_RECONFIGURE:
1156 Reconfigure(view());
1160 case LFUN_FLOATSOPERATE:
1161 if (argument == "openfoot")
1162 view()->allFloats(1,0);
1163 else if (argument == "closefoot")
1164 view()->allFloats(0,0);
1165 else if (argument == "openfig")
1166 view()->allFloats(1,1);
1167 else if (argument == "closefig")
1168 view()->allFloats(0,1);
1171 #ifdef WITH_WARNINGS
1172 //#warning Find another implementation here (or another lyxfunc)!
1175 case LFUN_HELP_ABOUTLYX:
1176 owner->getDialogs().show("about");
1179 case LFUN_HELP_TEXINFO:
1180 owner->getDialogs().showTexinfo();
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 case LFUN_VC_HISTORY:
1240 owner->getDialogs().show("vclog");
1244 // --- buffers ----------------------------------------
1246 case LFUN_SWITCHBUFFER:
1247 view()->buffer(bufferlist.getBuffer(argument));
1252 // servercmd: argument must be <file>:<template>
1253 Buffer * tmpbuf = NewFile(argument);
1255 view()->buffer(tmpbuf);
1259 case LFUN_FILE_OPEN:
1263 case LFUN_LATEX_LOG:
1264 owner->getDialogs().show("log");
1267 case LFUN_LAYOUT_DOCUMENT:
1268 owner->getDialogs().showDocument();
1271 case LFUN_LAYOUT_CHARACTER: {
1272 string data = freefont2string();
1274 owner->getDialogs().show("character", data);
1278 case LFUN_LAYOUT_TABULAR:
1279 if (view()->theLockingInset()) {
1280 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1281 InsetTabular * inset = static_cast<InsetTabular *>
1282 (view()->theLockingInset());
1283 inset->openLayoutDialog(view());
1284 } else if (view()->theLockingInset()->
1285 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1286 InsetTabular * inset = static_cast<InsetTabular *>(
1287 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1288 inset->openLayoutDialog(view());
1293 case LFUN_LAYOUT_PREAMBLE:
1294 owner->getDialogs().showPreamble();
1297 case LFUN_DROP_LAYOUTS_CHOICE:
1298 owner->getToolbar().openLayoutList();
1301 case LFUN_MENU_OPEN_BY_NAME:
1302 owner->getMenubar().openByName(argument);
1303 break; // RVDK_PATCH_5
1305 case LFUN_SPELLCHECK:
1306 owner->getDialogs().showSpellchecker();
1309 // --- lyxserver commands ----------------------------
1313 setMessage(owner->buffer()->fileName());
1314 lyxerr[Debug::INFO] << "FNAME["
1315 << owner->buffer()->fileName()
1321 dispatch_buffer = keyseq.print();
1322 lyxserver->notifyClient(dispatch_buffer);
1326 case LFUN_GOTOFILEROW:
1330 istringstream istr(argument.c_str());
1331 istr >> file_name >> row;
1332 // Must replace extension of the file to be .lyx and get full path
1333 string const s(ChangeExtension(file_name, ".lyx"));
1335 // Either change buffer or load the file
1336 if (bufferlist.exists(s)) {
1337 view()->buffer(bufferlist.getBuffer(s));
1339 view()->buffer(bufferlist.loadLyXFile(s));
1342 view()->setCursorFromRow(row);
1345 // see BufferView_pimpl::center()
1346 view()->updateScrollbar();
1350 case LFUN_GOTO_PARAGRAPH:
1352 istringstream istr(argument.c_str());
1356 Paragraph * par = &*owner->buffer()->getParFromID(id);
1358 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1359 << id << ']' << endl;
1362 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1363 << " found." << endl;
1366 if (view()->theLockingInset())
1367 view()->unlockInset(view()->theLockingInset());
1368 if (par->inInset()) {
1369 FuncRequest cmd(view(), LFUN_INSET_EDIT, "left");
1370 par->inInset()->localDispatch(cmd);
1373 view()->getLyXText()->setCursor(par, 0);
1374 view()->switchKeyMap();
1375 owner->view_state_changed();
1378 // see BufferView_pimpl::center()
1379 view()->updateScrollbar();
1383 // --- insert characters ----------------------------------------
1385 // --- Mathed stuff. If we are here, there is no locked inset yet.
1386 case LFUN_MATH_EXTERN:
1387 case LFUN_MATH_NUMBER:
1388 case LFUN_MATH_NONUMBER:
1389 case LFUN_MATH_LIMITS:
1391 setErrorMessage(N_("This is only allowed in math mode!"));
1395 // passthrough hat and underscore outside mathed:
1396 case LFUN_SUBSCRIPT:
1397 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1399 case LFUN_SUPERSCRIPT:
1400 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1403 case LFUN_MATH_PANEL:
1404 owner->getDialogs().showMathPanel();
1407 case LFUN_DIALOG_SHOW_NEW_INSET: {
1408 string const & name = argument;
1410 if (name == "bibitem" ||
1412 name == "include" ||
1417 InsetCommandParams p(name);
1418 data = InsetCommandMailer::params2string(name, p);
1419 } else if (name == "citation") {
1420 InsetCommandParams p("cite");
1421 data = InsetCommandMailer::params2string(name, p);
1423 owner->getDialogs().show(name, data, 0);
1427 case LFUN_DIALOG_SHOW_NEXT_INSET: {
1431 case LFUN_DIALOG_UPDATE: {
1432 string const & name = argument;
1433 // Can only update a dialog connected to an existing inset
1434 InsetBase * inset = owner->getDialogs().getOpenInset(name);
1436 FuncRequest fr(view(), LFUN_INSET_DIALOG_UPDATE,
1438 inset->localDispatch(fr);
1439 } else if (name == "paragraph") {
1440 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
1445 case LFUN_DIALOG_HIDE:
1446 Dialogs::hide(argument, 0);
1449 case LFUN_DIALOG_DISCONNECT_INSET:
1450 owner->getDialogs().disconnect(argument);
1453 case LFUN_CHILDOPEN:
1455 string const filename =
1456 MakeAbsPath(argument,
1457 owner->buffer()->filePath());
1458 setMessage(N_("Opening child document ") +
1459 MakeDisplayPath(filename) + "...");
1460 view()->savePosition(0);
1461 if (bufferlist.exists(filename))
1462 view()->buffer(bufferlist.getBuffer(filename));
1464 view()->buffer(bufferlist.loadLyXFile(filename));
1468 case LFUN_TOGGLECURSORFOLLOW:
1469 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1473 owner->getIntl().KeyMapOn(false);
1476 case LFUN_KMAP_PRIM:
1477 owner->getIntl().KeyMapPrim();
1481 owner->getIntl().KeyMapSec();
1484 case LFUN_KMAP_TOGGLE:
1485 owner->getIntl().ToggleKeyMap();
1490 // argument contains ';'-terminated commands
1491 while (!argument.empty()) {
1493 argument = split(argument, first, ';');
1499 case LFUN_DIALOG_PREFERENCES:
1500 owner->getDialogs().showPreferences();
1503 case LFUN_SAVEPREFERENCES:
1505 Path p(user_lyxdir);
1506 lyxrc.write("preferences");
1510 case LFUN_SCREEN_FONT_UPDATE:
1512 // handle the screen font changes.
1513 lyxrc.set_font_norm_type();
1514 lyx_gui::update_fonts();
1515 // We also need to empty the textcache so that
1516 // the buffer will be formatted correctly after
1519 // Of course we should only do the resize and the textcache.clear
1520 // if values really changed...but not very important right now. (Lgb)
1521 // All visible buffers will need resize
1527 case LFUN_SET_COLOR:
1530 string const x11_name = split(argument, lyx_name, ' ');
1531 if (lyx_name.empty() || x11_name.empty()) {
1532 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1537 bool const graphicsbg_changed =
1538 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1539 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1541 if (!lcolor.setColor(lyx_name, x11_name)) {
1543 bformat(_("Set-color \"%1$s\" failed "
1544 "- color is undefined or "
1545 "may not be redefined"), lyx_name));
1549 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1551 if (graphicsbg_changed) {
1552 #ifdef WITH_WARNINGS
1553 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1556 grfx::GCache & gc = grfx::GCache::get();
1557 gc.changeDisplay(true);
1566 owner->message(argument);
1569 case LFUN_FORKS_SHOW:
1570 owner->getDialogs().showForks();
1573 case LFUN_FORKS_KILL:
1575 if (!isStrInt(argument))
1578 pid_t const pid = strToInt(argument);
1579 ForkedcallsController & fcc = ForkedcallsController::get();
1584 case LFUN_TOOLTIPS_TOGGLE:
1585 owner->getDialogs().toggleTooltips();
1589 // Then if it was none of the above
1590 // Trying the BufferView::pimpl dispatch:
1591 if (!view()->dispatch(ev))
1592 lyxerr << "A truly unknown func ["
1593 << lyxaction.getActionName(ev.action) << "]!"
1600 view()->owner()->updateLayoutChoice();
1602 if (view()->available()) {
1603 view()->fitCursor();
1605 // If we executed a mutating lfun, mark the buffer as dirty
1606 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)
1607 && !lyxaction.funcHasFlag(ev.action, LyXAction::ReadOnly))
1608 view()->buffer()->markDirty();
1611 sendDispatchMessage(getMessage(), ev, verbose);
1615 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1617 owner->updateMenubar();
1618 owner->updateToolbar();
1620 if (ev.action == LFUN_SELFINSERT || !verbose) {
1621 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1623 owner->message(msg);
1627 string dispatch_msg = msg;
1628 if (!dispatch_msg.empty())
1629 dispatch_msg += ' ';
1631 string comname = lyxaction.getActionName(ev.action);
1633 int pseudoaction = ev.action;
1634 bool argsadded = false;
1636 if (!ev.argument.empty()) {
1637 // the pseudoaction is useful for the bindings
1638 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1640 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1641 pseudoaction = ev.action;
1643 comname += ' ' + ev.argument;
1648 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1650 if (!shortcuts.empty()) {
1651 comname += ": " + shortcuts;
1652 } else if (!argsadded && !ev.argument.empty()) {
1653 comname += ' ' + ev.argument;
1656 if (!comname.empty()) {
1657 comname = rtrim(comname);
1658 dispatch_msg += '(' + comname + ')';
1661 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1662 if (!dispatch_msg.empty())
1663 owner->message(dispatch_msg);
1667 void LyXFunc::setupLocalKeymap()
1669 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1670 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1674 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1676 string initpath = lyxrc.document_path;
1677 string filename(name);
1679 if (view()->available()) {
1680 string const trypath = owner->buffer()->filePath();
1681 // If directory is writeable, use this as default.
1682 if (IsDirWriteable(trypath))
1686 static int newfile_number;
1688 if (filename.empty()) {
1689 filename = AddName(lyxrc.document_path,
1690 "newfile" + tostr(++newfile_number) + ".lyx");
1691 FileInfo fi(filename);
1692 while (bufferlist.exists(filename) || fi.readable()) {
1694 filename = AddName(lyxrc.document_path,
1695 "newfile" + tostr(newfile_number) +
1697 fi.newFile(filename);
1701 // The template stuff
1704 FileDialog fileDlg(_("Select template file"),
1705 LFUN_SELECT_FILE_SYNC,
1706 make_pair(string(_("Documents|#o#O")),
1707 string(lyxrc.document_path)),
1708 make_pair(string(_("Templates|#T#t")),
1709 string(lyxrc.template_path)));
1711 FileDialog::Result result =
1712 fileDlg.open(lyxrc.template_path,
1713 _("*.lyx| LyX Documents (*.lyx)"));
1715 if (result.first == FileDialog::Later)
1718 string const fname = result.second;
1725 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1729 void LyXFunc::open(string const & fname)
1731 string initpath = lyxrc.document_path;
1733 if (view()->available()) {
1734 string const trypath = owner->buffer()->filePath();
1735 // If directory is writeable, use this as default.
1736 if (IsDirWriteable(trypath))
1742 if (fname.empty()) {
1743 FileDialog fileDlg(_("Select document to open"),
1745 make_pair(string(_("Documents|#o#O")),
1746 string(lyxrc.document_path)),
1747 make_pair(string(_("Examples|#E#e")),
1748 string(AddPath(system_lyxdir, "examples"))));
1750 FileDialog::Result result =
1751 fileDlg.open(initpath,
1752 _("*.lyx| LyX Documents (*.lyx)"));
1754 if (result.first == FileDialog::Later)
1757 filename = result.second;
1759 // check selected filename
1760 if (filename.empty()) {
1761 owner->message(_("Canceled."));
1767 // get absolute path of file and add ".lyx" to the filename if
1769 string const fullpath = FileSearch(string(), filename, "lyx");
1770 if (!fullpath.empty()) {
1771 filename = fullpath;
1774 string const disp_fn(MakeDisplayPath(filename));
1776 // if the file doesn't exist, let the user create one
1777 FileInfo const f(filename, true);
1779 // the user specifically chose this name. Believe them.
1780 Buffer * buffer = bufferlist.newFile(filename, "", true);
1781 view()->buffer(buffer);
1785 owner->message(bformat(_("Opening document %1$s..."), disp_fn));
1787 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1790 view()->buffer(openbuf);
1791 str2 = bformat(_("Document %1$s opened."), disp_fn);
1793 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1795 owner->message(str2);
1799 void LyXFunc::doImport(string const & argument)
1802 string filename = split(argument, format, ' ');
1804 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1805 << " file: " << filename << endl;
1807 // need user interaction
1808 if (filename.empty()) {
1809 string initpath = lyxrc.document_path;
1811 if (view()->available()) {
1812 string const trypath = owner->buffer()->filePath();
1813 // If directory is writeable, use this as default.
1814 if (IsDirWriteable(trypath))
1818 string const text = bformat(_("Select %1$s file to import"),
1819 formats.prettyName(format));
1821 FileDialog fileDlg(text,
1823 make_pair(string(_("Documents|#o#O")),
1824 string(lyxrc.document_path)),
1825 make_pair(string(_("Examples|#E#e")),
1826 string(AddPath(system_lyxdir, "examples"))));
1828 string const extension = "*." + formats.extension(format)
1829 + "| " + formats.prettyName(format)
1830 + " (*." + formats.extension(format) + ')';
1832 FileDialog::Result result = fileDlg.open(initpath,
1835 if (result.first == FileDialog::Later)
1838 filename = result.second;
1840 // check selected filename
1841 if (filename.empty())
1842 owner->message(_("Canceled."));
1845 if (filename.empty())
1848 // get absolute path of file
1849 filename = MakeAbsPath(filename);
1851 string const lyxfile = ChangeExtension(filename, ".lyx");
1853 // Check if the document already is open
1854 if (lyx_gui::use_gui && bufferlist.exists(lyxfile)) {
1855 if (!bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
1856 owner->message(_("Canceled."));
1861 // if the file exists already, and we didn't do
1862 // -i lyx thefile.lyx, warn
1863 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1864 string const file = MakeDisplayPath(lyxfile, 30);
1866 string text = bformat(_("The document %1$s already exists.\n\n"
1867 "Do you want to over-write that document?"), file);
1868 int const ret = Alert::prompt(_("Over-write document?"),
1869 text, 0, 1, _("&Over-write"), _("&Cancel"));
1872 owner->message(_("Canceled."));
1877 Importer::Import(owner, filename, format);
1881 void LyXFunc::closeBuffer()
1883 if (bufferlist.close(owner->buffer(), true) && !quitting) {
1884 if (bufferlist.empty()) {
1885 // need this otherwise SEGV may occur while trying to
1886 // set variables that don't exist
1887 // since there's no current buffer
1888 owner->getDialogs().hideBufferDependent();
1890 view()->buffer(bufferlist.first());
1896 // Each "owner" should have it's own message method. lyxview and
1897 // the minibuffer would use the minibuffer, but lyxserver would
1898 // send an ERROR signal to its client. Alejandro 970603
1899 // This func is bit problematic when it comes to NLS, to make the
1900 // lyx servers client be language indepenent we must not translate
1901 // strings sent to this func.
1902 void LyXFunc::setErrorMessage(string const & m) const
1904 dispatch_buffer = m;
1909 void LyXFunc::setMessage(string const & m) const
1911 dispatch_buffer = m;
1915 void LyXFunc::setStatusMessage(string const & m) const
1921 string const LyXFunc::view_status_message()
1923 // When meta-fake key is pressed, show the key sequence so far + "M-".
1925 return keyseq.print() + "M-";
1928 // Else, when a non-complete key sequence is pressed,
1929 // show the available options.
1930 if (keyseq.length() > 0 && !keyseq.deleted()) {
1931 return keyseq.printOptions();
1934 if (!view()->available())
1935 return _("Welcome to LyX!");
1937 return currentState(view());
1941 BufferView * LyXFunc::view() const
1944 return owner->view().get();