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 * ====================================================== */
14 #pragma implementation
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "lyxserver.h"
27 #include "LyXAction.h"
33 #include "trans_mgr.h"
36 #include "bufferview_funcs.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/lyx_gui.h"
40 #include "FloatList.h"
41 #include "converter.h"
44 #include "TextCache.h"
46 #include "undo_funcs.h"
47 #include "ParagraphParameters.h"
49 #include "insets/insetcommand.h"
50 #include "insets/insettabular.h"
52 #include "mathed/formulamacro.h"
53 #include "mathed/math_cursor.h"
54 #include "mathed/math_inset.h"
56 #include "frontends/FileDialog.h"
57 #include "frontends/Dialogs.h"
58 #include "frontends/Toolbar.h"
59 #include "frontends/Menubar.h"
60 #include "frontends/Alert.h"
62 #include "graphics/GraphicsCache.h"
64 #include "support/lyxalgo.h"
65 #include "support/LAssert.h"
66 #include "support/filetools.h"
67 #include "support/FileInfo.h"
68 #include "support/forkedcontr.h"
69 #include "support/lstrings.h"
70 #include "support/path.h"
71 #include "support/lyxfunctional.h"
73 #include "BoostFormat.h"
89 using std::back_inserter;
91 extern BufferList bufferlist;
92 extern LyXServer * lyxserver;
93 extern bool selection_possible;
95 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
98 extern tex_accent_struct get_accent(kb_action action);
100 extern void ShowLatexLog();
103 LyXFunc::LyXFunc(LyXView * o)
106 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
107 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
108 meta_fake_bit(key_modifier::none)
114 LyXText * LyXFunc::TEXT(bool flag = true) const
118 return view()->getLyXText();
123 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
125 if (selecting || TEXT(flag)->selection.mark()) {
126 TEXT(flag)->setSelection(view());
127 if (TEXT(flag)->bv_owner)
128 view()->toggleToggle();
130 view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
131 view()->showCursor();
133 view()->switchKeyMap();
137 void LyXFunc::handleKeyFunc(kb_action action)
139 char c = encoded_last_key;
141 if (keyseq.length()) {
145 owner->getIntl().getTransManager()
146 .deadkey(c, get_accent(action).accent, TEXT(false));
147 // Need to clear, in case the minibuffer calls these
150 // copied verbatim from do_accent_char
151 view()->update(TEXT(false),
152 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
153 TEXT(false)->selection.cursor = TEXT(false)->cursor;
157 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
158 key_modifier::state state)
162 if (lyxerr.debugging(Debug::KEY)) {
163 lyxerr << "KeySym is "
164 << keysym->getSymbolName()
168 // Do nothing if we have nothing (JMarc)
169 if (!keysym->isOK()) {
170 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
175 if (keysym->isModifier()) {
176 lyxerr[Debug::KEY] << "isModifier true" << endl;
180 Encoding const * encoding = view()->getEncoding();
182 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
184 // Do a one-deep top-level lookup for
185 // cancel and meta-fake keys. RVDK_PATCH_5
186 cancel_meta_seq.reset();
188 int action = cancel_meta_seq.addkey(keysym, state);
189 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
191 // When not cancel or meta-fake, do the normal lookup.
192 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
193 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
194 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
195 // remove Caps Lock and Mod2 as a modifiers
196 action = keyseq.addkey(keysym, (state | meta_fake_bit));
197 lyxerr[Debug::KEY] << "action now set to ["
198 << action << ']' << endl;
201 // Dont remove this unless you know what you are doing.
202 meta_fake_bit = key_modifier::none;
204 // can this happen now ?
205 if (action == LFUN_NOACTION) {
206 action = LFUN_PREFIX;
209 if (lyxerr.debugging(Debug::KEY)) {
210 lyxerr << "Key [action="
212 << keyseq.print() << ']'
216 // already here we know if it any point in going further
217 // why not return already here if action == -1 and
218 // num_bytes == 0? (Lgb)
220 if (keyseq.length() > 1) {
221 owner->message(keyseq.print());
225 // Maybe user can only reach the key via holding down shift.
226 // Let's see. But only if shift is the only modifier
227 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
228 lyxerr[Debug::KEY] << "Trying without shift" << endl;
229 action = keyseq.addkey(keysym, key_modifier::none);
230 lyxerr[Debug::KEY] << "Action now " << action << endl;
233 if (action == LFUN_UNKNOWN_ACTION) {
234 // Hmm, we didn't match any of the keysequences. See
235 // if it's normal insertable text not already covered
237 if (keysym->isText() && keyseq.length() == 1) {
238 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
239 action = LFUN_SELFINSERT;
241 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
242 owner->message(_("Unknown function."));
247 if (action == LFUN_SELFINSERT) {
248 if (encoded_last_key != 0) {
250 arg += encoded_last_key;
252 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
254 lyxerr[Debug::KEY] << "SelfInsert arg[`"
255 << argument << "']" << endl;
263 FuncStatus LyXFunc::getStatus(int ac) const
265 return getStatus(lyxaction.retrieveActionArg(ac));
269 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
272 Buffer * buf = owner->buffer();
274 if (ev.action == LFUN_NOACTION) {
275 setStatusMessage(N_("Nothing to do"));
276 return flag.disabled(true);
280 case LFUN_UNKNOWN_ACTION:
281 #ifndef HAVE_LIBAIKSAURUS
282 case LFUN_THESAURUS_ENTRY:
287 flag |= lyx_gui::getStatus(ev);
290 if (flag.unknown()) {
291 setStatusMessage(N_("Unknown action"));
295 // the default error message if we disable the command
296 setStatusMessage(N_("Command disabled"));
298 // Check whether we need a buffer
299 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
300 // Yes we need a buffer, do we have one?
303 // Can we use a readonly buffer?
304 if (buf->isReadonly() &&
305 !lyxaction.funcHasFlag(ev.action,
306 LyXAction::ReadOnly)) {
308 setStatusMessage(N_("Document is read-only"));
313 setStatusMessage(N_("Command not allowed with"
314 "out any document open"));
315 return flag.disabled(true);
319 UpdatableInset * tli = view()->theLockingInset();
321 // I would really like to avoid having this switch and rather try to
322 // encode this in the function itself.
323 bool disable = false;
326 disable = !Exporter::IsExportable(buf, "dvi")
327 || lyxrc.print_command == "none";
330 disable = ev.argument == "fax" &&
331 !Exporter::IsExportable(buf, ev.argument);
334 disable = buf->undostack.empty();
337 disable = buf->redostack.empty();
341 if (tli && tli->lyxCode() == Inset::TABULAR_CODE) {
342 InsetTabular * t(static_cast<InsetTabular*>(tli));
343 if (t->hasSelection()) {
348 disable = !mathcursor && !view()->getLyXText()->selection.set();
351 disable = lyxrc.chktex_command == "none";
354 disable = !Exporter::IsExportable(buf, "program");
357 case LFUN_LAYOUT_CHARACTER:
358 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
361 case LFUN_LAYOUT_TABULAR:
363 || (tli->lyxCode() != Inset::TABULAR_CODE
364 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
368 case LFUN_LAYOUT_PARAGRAPH: {
369 Inset * inset = TEXT(false)->cursor.par()->inInset();
370 disable = inset && inset->forceDefaultParagraphs(inset);
374 case LFUN_INSET_OPTARG:
375 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
378 case LFUN_TABULAR_FEATURE:
381 // FIXME: check temporarily disabled
383 char align = mathcursor->valign();
388 if (ev.argument.empty()) {
392 if (!contains("tcb", ev.argument[0])) {
396 flag.setOnOff(ev.argument[0] == align);
400 char align = mathcursor->halign();
405 if (ev.argument.empty()) {
409 if (!contains("lcr", ev.argument[0])) {
413 flag.setOnOff(ev.argument[0] == align);
416 disable = !mathcursor->halign();
422 //ret.disabled(true);
423 if (tli->lyxCode() == Inset::TABULAR_CODE) {
424 ret = static_cast<InsetTabular *>(tli)
425 ->getStatus(ev.argument);
426 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
427 ret = static_cast<InsetTabular *>
428 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
429 ->getStatus(ev.argument);
434 static InsetTabular inset(*owner->buffer(), 1, 1);
438 ret = inset.getStatus(ev.argument);
439 if (ret.onoff(true) || ret.onoff(false))
440 flag.setOnOff(false);
444 case LFUN_VC_REGISTER:
445 disable = buf->lyxvc.inUse();
447 case LFUN_VC_CHECKIN:
448 disable = !buf->lyxvc.inUse() || buf->isReadonly();
450 case LFUN_VC_CHECKOUT:
451 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
455 case LFUN_VC_HISTORY:
456 disable = !buf->lyxvc.inUse();
458 case LFUN_BOOKMARK_GOTO:
460 isSavedPosition(strToUnsignedInt(ev.argument));
462 case LFUN_INSET_TOGGLE: {
463 LyXText * lt = view()->getLyXText();
464 disable = !(isEditableInset(lt->getInset())
466 && lt->inset_owner->owner()
467 && lt->inset_owner->owner()->isOpen()));
471 disable = !IsFileReadable(buf->getLogName().second);
474 case LFUN_MATH_MUTATE:
476 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
477 flag.setOnOff(false);
482 // we just need to be in math mode to enable that
484 case LFUN_MATH_SPACE:
485 case LFUN_MATH_LIMITS:
486 case LFUN_MATH_NONUMBER:
487 case LFUN_MATH_NUMBER:
488 case LFUN_MATH_EXTERN:
489 disable = !mathcursor;
496 // the functions which insert insets
497 Inset::Code code = Inset::NO_CODE;
500 code = Inset::ERT_CODE;
502 case LFUN_INSET_GRAPHICS:
503 code = Inset::GRAPHICS_CODE;
505 case LFUN_INSET_FOOTNOTE:
506 code = Inset::FOOT_CODE;
508 case LFUN_TABULAR_INSERT:
509 code = Inset::TABULAR_CODE;
511 case LFUN_INSET_EXTERNAL:
512 code = Inset::EXTERNAL_CODE;
514 case LFUN_INSET_MARGINAL:
515 code = Inset::MARGIN_CODE;
517 case LFUN_INSET_MINIPAGE:
518 code = Inset::MINIPAGE_CODE;
520 case LFUN_INSET_FLOAT:
521 case LFUN_INSET_WIDE_FLOAT:
522 code = Inset::FLOAT_CODE;
524 case LFUN_INSET_WRAP:
525 code = Inset::WRAP_CODE;
527 case LFUN_FLOAT_LIST:
528 code = Inset::FLOAT_LIST_CODE;
531 case LFUN_INSET_LIST:
532 code = Inset::LIST_CODE;
534 case LFUN_INSET_THEOREM:
535 code = Inset::THEOREM_CODE;
538 case LFUN_INSET_CAPTION:
539 code = Inset::CAPTION_CODE;
541 case LFUN_INSERT_NOTE:
542 code = Inset::NOTE_CODE;
544 case LFUN_INSERT_LABEL:
545 code = Inset::LABEL_CODE;
547 case LFUN_INSET_OPTARG:
548 code = Inset::OPTARG_CODE;
550 case LFUN_REF_INSERT:
551 code = Inset::REF_CODE;
553 case LFUN_CITATION_CREATE:
554 case LFUN_CITATION_INSERT:
555 code = Inset::CITE_CODE;
557 case LFUN_INSERT_BIBTEX:
558 code = Inset::BIBTEX_CODE;
560 case LFUN_INDEX_INSERT:
561 code = Inset::INDEX_CODE;
563 case LFUN_INDEX_PRINT:
564 code = Inset::INDEX_PRINT_CODE;
566 case LFUN_CHILD_INSERT:
567 code = Inset::INCLUDE_CODE;
569 case LFUN_TOC_INSERT:
570 code = Inset::TOC_CODE;
572 case LFUN_PARENTINSERT:
573 code = Inset::PARENT_CODE;
577 case LFUN_INSERT_URL:
578 code = Inset::URL_CODE;
581 // always allow this, since we will inset a raw quote
582 // if an inset is not allowed.
584 case LFUN_HYPHENATION:
585 case LFUN_LIGATURE_BREAK:
587 case LFUN_MENU_SEPARATOR:
589 case LFUN_END_OF_SENTENCE:
590 code = Inset::SPECIALCHAR_CODE;
592 case LFUN_PROTECTEDSPACE:
593 // slight hack: we know this is allowed in math mode
595 code = Inset::SPECIALCHAR_CODE;
600 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
606 // A few general toggles
608 case LFUN_TOOLTIPS_TOGGLE:
609 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
612 case LFUN_READ_ONLY_TOGGLE:
613 flag.setOnOff(buf->isReadonly());
616 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
618 case LFUN_SWITCHBUFFER:
619 // toggle on the current buffer, but do not toggle off
620 // the other ones (is that a good idea?)
621 if (ev.argument == buf->fileName())
628 // the font related toggles
630 LyXFont const & font = TEXT(false)->real_current_font;
633 flag.setOnOff(font.emph() == LyXFont::ON);
636 flag.setOnOff(font.noun() == LyXFont::ON);
639 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
642 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
645 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
648 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
654 string tc = mathcursor->getLastCode();
657 flag.setOnOff(tc == "mathbf");
660 flag.setOnOff(tc == "mathsf");
663 flag.setOnOff(tc == "mathcal");
666 flag.setOnOff(tc == "mathrm");
669 flag.setOnOff(tc == "mathtt");
672 flag.setOnOff(tc == "mathbb");
675 flag.setOnOff(tc == "mathnormal");
682 // this one is difficult to get right. As a half-baked
683 // solution, we consider only the first action of the sequence
684 if (ev.action == LFUN_SEQUENCE) {
685 // argument contains ';'-terminated commands
686 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
693 void LyXFunc::dispatch(string const & s, bool verbose)
695 int const action = lyxaction.LookupFunc(s);
697 if (action == LFUN_UNKNOWN_ACTION) {
699 boost::format fmt(_("Unknown function (%1$s)"));
701 owner->message(fmt.str());
703 string const msg = string(_("Unknown function ("))
710 dispatch(action, verbose);
714 void LyXFunc::dispatch(int ac, bool verbose)
716 dispatch(lyxaction.retrieveActionArg(ac), verbose);
721 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
723 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
724 <<"] arg[" << ev.argument << ']' << endl;
726 // we have not done anything wrong yet.
728 dispatch_buffer.erase();
730 #ifdef NEW_DISPATCHER
731 // We try do call the most specific dispatcher first:
732 // 1. the lockinginset's dispatch
733 // 2. the bufferview's dispatch
734 // 3. the lyxview's dispatch
737 selection_possible = false;
739 if (view()->available())
740 view()->hideCursor();
742 string argument = ev.argument;
743 kb_action action = ev.action;
745 // We cannot use this function here
746 if (getStatus(ev).disabled()) {
747 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
748 << lyxaction.getActionName(action)
749 << " [" << action << "] is disabled at this location"
751 setErrorMessage(getStatusMessage());
752 goto exit_with_message;
755 if (view()->available() && view()->theLockingInset()) {
756 Inset::RESULT result;
757 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
758 (!keyseq.deleted())))
760 UpdatableInset * inset = view()->theLockingInset();
764 inset->getCursorPos(view(), inset_x, dummy_y);
766 if ((action == LFUN_UNKNOWN_ACTION)
767 && argument.empty()) {
768 argument = encoded_last_key;
770 // Undo/Redo is a bit tricky for insets.
771 if (action == LFUN_UNDO) {
773 goto exit_with_message;
774 } else if (action == LFUN_REDO) {
776 goto exit_with_message;
777 } else if (((result=inset->
778 // Hand-over to inset's own dispatch:
779 localDispatch(FuncRequest(view(), action, argument))) ==
780 UpdatableInset::DISPATCHED) ||
781 (result == UpdatableInset::DISPATCHED_NOUPDATE))
782 goto exit_with_message;
783 // If UNDISPATCHED, just soldier on
784 else if (result == UpdatableInset::FINISHED) {
785 goto exit_with_message;
786 // We do not need special RTL handling here:
787 // FINISHED means that the cursor should be
788 // one position after the inset.
789 } else if (result == UpdatableInset::FINISHED_RIGHT) {
790 TEXT()->cursorRight(view());
791 moveCursorUpdate(true, false);
792 owner->view_state_changed();
793 goto exit_with_message;
794 } else if (result == UpdatableInset::FINISHED_UP) {
795 if (TEXT()->cursor.irow()->previous()) {
797 TEXT()->setCursorFromCoordinates(
798 view(), TEXT()->cursor.ix() + inset_x,
799 TEXT()->cursor.iy() -
800 TEXT()->cursor.irow()->baseline() - 1);
801 TEXT()->cursor.x_fix(TEXT()->cursor.x());
803 TEXT()->cursorUp(view());
805 moveCursorUpdate(true, false);
806 owner->view_state_changed();
808 view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
810 goto exit_with_message;
811 } else if (result == UpdatableInset::FINISHED_DOWN) {
812 if (TEXT()->cursor.irow()->next()) {
814 TEXT()->setCursorFromCoordinates(
815 view(), TEXT()->cursor.ix() + inset_x,
816 TEXT()->cursor.iy() -
817 TEXT()->cursor.irow()->baseline() +
818 TEXT()->cursor.irow()->height() + 1);
819 TEXT()->cursor.x_fix(TEXT()->cursor.x());
821 TEXT()->cursorDown(view());
824 TEXT()->cursorRight(view());
826 moveCursorUpdate(true, false);
827 owner->view_state_changed();
828 goto exit_with_message;
830 #warning I am not sure this is still right, please have a look! (Jug 20020417)
831 else { // result == UNDISPATCHED
832 //setMessage(N_("Text mode"));
834 case LFUN_UNKNOWN_ACTION:
835 case LFUN_BREAKPARAGRAPH:
837 TEXT()->cursorRight(view());
838 view()->switchKeyMap();
839 owner->view_state_changed();
842 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
843 TEXT()->cursorRight(view());
844 moveCursorUpdate(true, false);
845 owner->view_state_changed();
847 goto exit_with_message;
849 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
850 TEXT()->cursorRight(view());
851 moveCursorUpdate(true, false);
852 owner->view_state_changed();
854 goto exit_with_message;
856 if (TEXT()->cursor.row()->next())
857 TEXT()->cursorDown(view());
859 TEXT()->cursorRight(view());
860 moveCursorUpdate(true, false);
861 owner->view_state_changed();
862 goto exit_with_message;
874 if (!view()->available()) break;
875 // this function should be used always [asierra060396]
876 UpdatableInset * tli =
877 view()->theLockingInset();
879 UpdatableInset * lock = tli->getLockingInset();
882 view()->unlockInset(tli);
883 TEXT()->cursorRight(view());
884 moveCursorUpdate(true, false);
885 owner->view_state_changed();
887 tli->unlockInsetInInset(view(),
892 // Tell the paragraph dialog that we changed paragraph
893 owner->getDialogs().updateParagraph();
898 // --- Misc -------------------------------------------
899 case LFUN_WORDFINDFORWARD :
900 case LFUN_WORDFINDBACKWARD : {
901 static string last_search;
902 string searched_string;
904 if (!argument.empty()) {
905 last_search = argument;
906 searched_string = argument;
908 searched_string = last_search;
910 bool fw = (action == LFUN_WORDFINDFORWARD);
911 if (!searched_string.empty()) {
912 lyxfind::LyXFind(view(), searched_string, fw);
914 // view()->showCursor();
920 if (view()->available() && !view()->theLockingInset()) {
921 view()->update(TEXT(),
922 BufferView::SELECT|BufferView::FITCUR);
924 owner->message(keyseq.printOptions());
928 // --- Misc -------------------------------------------
929 case LFUN_EXEC_COMMAND:
930 owner->focus_command_buffer();
933 case LFUN_CANCEL: // RVDK_PATCH_5
935 meta_fake_bit = key_modifier::none;
936 if (view()->available())
937 // cancel any selection
938 dispatch(LFUN_MARK_OFF);
939 setMessage(N_("Cancel"));
942 case LFUN_META_FAKE: // RVDK_PATCH_5
944 meta_fake_bit = key_modifier::alt;
945 setMessage(keyseq.print());
949 case LFUN_READ_ONLY_TOGGLE:
950 if (owner->buffer()->lyxvc.inUse()) {
951 owner->buffer()->lyxvc.toggleReadOnly();
953 owner->buffer()->setReadonly(
954 !owner->buffer()->isReadonly());
958 case LFUN_CENTER: // this is center and redraw.
962 // --- Menus -----------------------------------------------
964 menuNew(argument, false);
967 case LFUN_MENUNEWTMPLT:
968 menuNew(argument, true);
971 case LFUN_CLOSEBUFFER:
976 if (!owner->buffer()->isUnnamed()) {
979 s1 << boost::format(_("Saving document %1$s..."))
980 % MakeDisplayPath(owner->buffer()->fileName());
982 s1 << _("Saving document ")
983 << MakeDisplayPath(owner->buffer()->fileName())
986 owner->message(STRCONV(s1.str()));
987 MenuWrite(view(), owner->buffer());
989 owner->message(STRCONV(s1.str()));
991 WriteAs(view(), owner->buffer());
995 WriteAs(view(), owner->buffer(), argument);
998 case LFUN_MENURELOAD:
1003 Exporter::Export(owner->buffer(), argument, true);
1007 Exporter::Preview(owner->buffer(), argument);
1010 case LFUN_BUILDPROG:
1011 Exporter::Export(owner->buffer(), "program", true);
1014 case LFUN_RUNCHKTEX:
1015 MenuRunChktex(owner->buffer());
1018 case LFUN_MENUPRINT:
1019 owner->getDialogs().showPrint();
1023 if (argument == "custom")
1024 owner->getDialogs().showSendto();
1026 Exporter::Export(owner->buffer(), argument, false);
1044 InsetCommandParams p;
1047 if (action == LFUN_TOCVIEW)
1049 p.setCmdName("tableofcontents");
1051 else if (action == LFUN_LOAVIEW)
1052 p.setCmdName("listof{algorithm}{List of Algorithms}");
1053 else if (action == LFUN_LOFVIEW)
1054 p.setCmdName("listoffigures");
1056 p.setCmdName("listoftables");
1058 owner->getDialogs().createTOC(p.getAsString());
1074 case LFUN_MENUSEARCH:
1075 owner->getDialogs().showSearch();
1078 case LFUN_REMOVEERRORS:
1079 if (view()->removeAutoInsets()) {
1080 #warning repaint() or update() or nothing ?
1082 view()->fitCursor();
1086 case LFUN_DEPTH_MIN:
1087 changeDepth(view(), TEXT(false), -1);
1090 case LFUN_DEPTH_PLUS:
1091 changeDepth(view(), TEXT(false), 1);
1095 owner->getDialogs().setUserFreeFont();
1098 case LFUN_RECONFIGURE:
1099 Reconfigure(view());
1103 case LFUN_FLOATSOPERATE:
1104 if (argument == "openfoot")
1105 view()->allFloats(1,0);
1106 else if (argument == "closefoot")
1107 view()->allFloats(0,0);
1108 else if (argument == "openfig")
1109 view()->allFloats(1,1);
1110 else if (argument == "closefig")
1111 view()->allFloats(0,1);
1114 #ifdef WITH_WARNINGS
1115 //#warning Find another implementation here (or another lyxfunc)!
1118 case LFUN_HELP_ABOUTLYX:
1119 owner->getDialogs().showAboutlyx();
1122 case LFUN_HELP_TEXINFO:
1123 owner->getDialogs().showTexinfo();
1126 case LFUN_HELP_OPEN:
1128 string const arg = argument;
1130 setErrorMessage(N_("Missing argument"));
1133 owner->prohibitInput();
1134 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1135 if (fname.empty()) {
1136 lyxerr << "LyX: unable to find documentation file `"
1137 << arg << "'. Bad installation?" << endl;
1138 owner->allowInput();
1142 #if USE_BOOST_FORMAT
1143 str << boost::format(_("Opening help file %1$s..."))
1144 % MakeDisplayPath(fname);
1146 str << _("Opening help file ")
1147 << MakeDisplayPath(fname) << _("...");
1149 owner->message(STRCONV(str.str()));
1150 view()->buffer(bufferlist.loadLyXFile(fname, false));
1151 owner->allowInput();
1155 // --- version control -------------------------------
1156 case LFUN_VC_REGISTER:
1158 if (!owner->buffer()->lyxvc.inUse())
1159 owner->buffer()->lyxvc.registrer();
1163 case LFUN_VC_CHECKIN:
1165 if (owner->buffer()->lyxvc.inUse()
1166 && !owner->buffer()->isReadonly())
1167 owner->buffer()->lyxvc.checkIn();
1171 case LFUN_VC_CHECKOUT:
1173 if (owner->buffer()->lyxvc.inUse()
1174 && owner->buffer()->isReadonly())
1175 owner->buffer()->lyxvc.checkOut();
1179 case LFUN_VC_REVERT:
1181 owner->buffer()->lyxvc.revert();
1187 owner->buffer()->lyxvc.undoLast();
1191 case LFUN_VC_HISTORY:
1193 owner->getDialogs().showVCLogFile();
1197 // --- buffers ----------------------------------------
1199 case LFUN_SWITCHBUFFER:
1200 view()->buffer(bufferlist.getBuffer(argument));
1205 // servercmd: argument must be <file>:<template>
1206 Buffer * tmpbuf = NewFile(argument);
1208 view()->buffer(tmpbuf);
1212 case LFUN_FILE_OPEN:
1216 case LFUN_LATEX_LOG:
1217 owner->getDialogs().showLogFile();
1220 case LFUN_LAYOUT_DOCUMENT:
1221 owner->getDialogs().showDocument();
1224 case LFUN_LAYOUT_PARAGRAPH:
1225 owner->getDialogs().showParagraph();
1228 case LFUN_LAYOUT_CHARACTER:
1229 owner->getDialogs().showCharacter();
1232 case LFUN_LAYOUT_TABULAR:
1233 if (view()->theLockingInset()) {
1234 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1235 InsetTabular * inset = static_cast<InsetTabular *>
1236 (view()->theLockingInset());
1237 inset->openLayoutDialog(view());
1238 } else if (view()->theLockingInset()->
1239 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1240 InsetTabular * inset = static_cast<InsetTabular *>(
1241 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1242 inset->openLayoutDialog(view());
1247 case LFUN_LAYOUT_PREAMBLE:
1248 owner->getDialogs().showPreamble();
1251 case LFUN_DROP_LAYOUTS_CHOICE:
1252 owner->getToolbar().openLayoutList();
1255 case LFUN_MENU_OPEN_BY_NAME:
1256 owner->getMenubar().openByName(argument);
1257 break; // RVDK_PATCH_5
1259 case LFUN_SPELLCHECK:
1260 owner->getDialogs().showSpellchecker();
1263 // --- lyxserver commands ----------------------------
1267 setMessage(owner->buffer()->fileName());
1268 lyxerr[Debug::INFO] << "FNAME["
1269 << owner->buffer()->fileName()
1275 dispatch_buffer = keyseq.print();
1276 lyxserver->notifyClient(dispatch_buffer);
1280 case LFUN_GOTOFILEROW:
1284 istringstream istr(argument.c_str());
1285 istr >> file_name >> row;
1286 // Must replace extension of the file to be .lyx and get full path
1287 string const s(ChangeExtension(file_name, ".lyx"));
1289 // Either change buffer or load the file
1290 if (bufferlist.exists(s)) {
1291 view()->buffer(bufferlist.getBuffer(s));
1293 view()->buffer(bufferlist.loadLyXFile(s));
1296 view()->setCursorFromRow(row);
1299 // see BufferView_pimpl::center()
1300 view()->updateScrollbar();
1304 case LFUN_GOTO_PARAGRAPH:
1306 istringstream istr(argument.c_str());
1310 Paragraph * par = owner->buffer()->getParFromID(id);
1312 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1313 << id << ']' << endl;
1316 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1317 << " found." << endl;
1320 if (view()->theLockingInset())
1321 view()->unlockInset(view()->theLockingInset());
1322 if (par->inInset()) {
1323 par->inInset()->edit(view());
1326 view()->getLyXText()->setCursor(view(), par, 0);
1327 view()->switchKeyMap();
1328 owner->view_state_changed();
1331 // see BufferView_pimpl::center()
1332 view()->updateScrollbar();
1339 int const qa = lyxaction.LookupFunc(argument);
1340 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1344 // --- insert characters ----------------------------------------
1346 // --- Mathed stuff. If we are here, there is no locked inset yet.
1347 case LFUN_MATH_EXTERN:
1348 case LFUN_MATH_NUMBER:
1349 case LFUN_MATH_NONUMBER:
1350 case LFUN_MATH_LIMITS:
1352 setErrorMessage(N_("This is only allowed in math mode!"));
1356 // passthrough hat and underscore outside mathed:
1357 case LFUN_SUBSCRIPT:
1358 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1360 case LFUN_SUPERSCRIPT:
1361 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1364 case LFUN_MATH_PANEL:
1365 owner->getDialogs().showMathPanel();
1368 case LFUN_CITATION_CREATE:
1370 InsetCommandParams p("cite");
1372 if (!argument.empty()) {
1373 // This should be set at source, ie when typing
1374 // "citation-insert foo" in the minibuffer.
1375 // Question: would pybibliographer also need to be
1376 // changed. Suspect so. Leave as-is therefore.
1377 if (contains(argument, "|")) {
1378 p.setContents(token(argument, '|', 0));
1379 p.setOptions(token(argument, '|', 1));
1381 p.setContents(argument);
1383 dispatch(FuncRequest(view(), LFUN_CITATION_INSERT, p.getAsString()));
1385 owner->getDialogs().createCitation(p.getAsString());
1389 case LFUN_CHILDOPEN:
1391 string const filename =
1392 MakeAbsPath(argument,
1393 owner->buffer()->filePath());
1394 setMessage(N_("Opening child document ") +
1395 MakeDisplayPath(filename) + "...");
1396 view()->savePosition(0);
1397 if (bufferlist.exists(filename))
1398 view()->buffer(bufferlist.getBuffer(filename));
1400 view()->buffer(bufferlist.loadLyXFile(filename));
1404 case LFUN_TOGGLECURSORFOLLOW:
1405 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1409 owner->getIntl().KeyMapOn(false);
1412 case LFUN_KMAP_PRIM:
1413 owner->getIntl().KeyMapPrim();
1417 owner->getIntl().KeyMapSec();
1420 case LFUN_KMAP_TOGGLE:
1421 owner->getIntl().ToggleKeyMap();
1426 // argument contains ';'-terminated commands
1427 while (!argument.empty()) {
1429 argument = split(argument, first, ';');
1435 case LFUN_DIALOG_PREFERENCES:
1436 owner->getDialogs().showPreferences();
1439 case LFUN_SAVEPREFERENCES:
1441 Path p(user_lyxdir);
1442 lyxrc.write("preferences");
1446 case LFUN_SCREEN_FONT_UPDATE:
1448 // handle the screen font changes.
1449 lyxrc.set_font_norm_type();
1450 lyx_gui::update_fonts();
1451 // We also need to empty the textcache so that
1452 // the buffer will be formatted correctly after
1455 // Of course we should only do the resize and the textcache.clear
1456 // if values really changed...but not very important right now. (Lgb)
1457 // All visible buffers will need resize
1463 case LFUN_SET_COLOR:
1466 string const x11_name = split(argument, lyx_name, ' ');
1467 if (lyx_name.empty() || x11_name.empty()) {
1468 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1473 bool const graphicsbg_changed =
1474 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1475 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1477 if (!lcolor.setColor(lyx_name, x11_name)) {
1478 #if USE_BOOST_FORMAT
1482 _("Set-color \"%1$s\" failed "
1483 "- color is undefined or "
1484 "may not be redefined"))
1487 setErrorMessage(_("Set-color ") + lyx_name
1488 + _(" failed - color is undefined"
1489 " or may not be redefined"));
1495 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1497 if (graphicsbg_changed) {
1498 #ifdef WITH_WARNINGS
1499 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1502 grfx::GCache & gc = grfx::GCache::get();
1503 gc.changeDisplay(true);
1512 owner->message(argument);
1515 case LFUN_FORKS_SHOW:
1516 owner->getDialogs().showForks();
1519 case LFUN_FORKS_KILL:
1521 if (!isStrInt(argument))
1524 pid_t const pid = strToInt(argument);
1525 ForkedcallsController & fcc = ForkedcallsController::get();
1530 case LFUN_TOOLTIPS_TOGGLE:
1531 owner->getDialogs().toggleTooltips();
1535 // Then if it was none of the above
1536 // Trying the BufferView::pimpl dispatch:
1537 if (!view()->dispatch(ev))
1538 lyxerr << "A truly unknown func ["
1539 << lyxaction.getActionName(ev.action) << "]!"
1545 sendDispatchMessage(getMessage(), ev, verbose);
1549 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1551 owner->updateMenubar();
1552 owner->updateToolbar();
1554 if (ev.action == LFUN_SELFINSERT || !verbose) {
1555 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1557 owner->message(msg);
1561 string dispatch_msg = msg;
1562 if (!dispatch_msg.empty())
1563 dispatch_msg += ' ';
1565 string comname = lyxaction.getActionName(ev.action);
1567 int pseudoaction = ev.action;
1568 bool argsadded = false;
1570 if (!ev.argument.empty()) {
1571 // the pseudoaction is useful for the bindings
1572 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1574 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1575 pseudoaction = ev.action;
1577 comname += ' ' + ev.argument;
1582 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1584 if (!shortcuts.empty()) {
1585 comname += ": " + shortcuts;
1586 } else if (!argsadded && !ev.argument.empty()) {
1587 comname += ' ' + ev.argument;
1590 if (!comname.empty()) {
1591 comname = rtrim(comname);
1592 dispatch_msg += '(' + comname + ')';
1595 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1596 if (!dispatch_msg.empty())
1597 owner->message(dispatch_msg);
1601 void LyXFunc::setupLocalKeymap()
1603 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1604 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1608 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1610 string initpath = lyxrc.document_path;
1611 string filename(name);
1613 if (view()->available()) {
1614 string const trypath = owner->buffer()->filePath();
1615 // If directory is writeable, use this as default.
1616 if (IsDirWriteable(trypath))
1620 static int newfile_number;
1622 if (filename.empty()) {
1623 filename = AddName(lyxrc.document_path,
1624 "newfile" + tostr(++newfile_number) + ".lyx");
1625 FileInfo fi(filename);
1626 while (bufferlist.exists(filename) || fi.readable()) {
1628 filename = AddName(lyxrc.document_path,
1629 "newfile" + tostr(newfile_number) +
1631 fi.newFile(filename);
1635 // The template stuff
1638 FileDialog fileDlg(owner, _("Select template file"),
1639 LFUN_SELECT_FILE_SYNC,
1640 make_pair(string(_("Documents|#o#O")),
1641 string(lyxrc.document_path)),
1642 make_pair(string(_("Templates|#T#t")),
1643 string(lyxrc.template_path)));
1645 FileDialog::Result result =
1646 fileDlg.open(lyxrc.template_path,
1647 _("*.lyx|LyX Documents (*.lyx)"));
1649 if (result.first == FileDialog::Later)
1652 string const fname = result.second;
1659 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1663 void LyXFunc::open(string const & fname)
1665 string initpath = lyxrc.document_path;
1667 if (view()->available()) {
1668 string const trypath = owner->buffer()->filePath();
1669 // If directory is writeable, use this as default.
1670 if (IsDirWriteable(trypath))
1676 if (fname.empty()) {
1677 FileDialog fileDlg(owner, _("Select document to open"),
1679 make_pair(string(_("Documents|#o#O")),
1680 string(lyxrc.document_path)),
1681 make_pair(string(_("Examples|#E#e")),
1682 string(AddPath(system_lyxdir, "examples"))));
1684 FileDialog::Result result =
1685 fileDlg.open(initpath,
1686 "*.lyx|LyX Documents (*.lyx)");
1688 if (result.first == FileDialog::Later)
1691 filename = result.second;
1693 // check selected filename
1694 if (filename.empty()) {
1695 owner->message(_("Canceled."));
1701 // get absolute path of file and add ".lyx" to the filename if
1703 string const fullpath = FileSearch(string(), filename, "lyx");
1704 if (!fullpath.empty()) {
1705 filename = fullpath;
1708 string const disp_fn(MakeDisplayPath(filename));
1710 // if the file doesn't exist, let the user create one
1711 FileInfo const f(filename, true);
1713 if (!Alert::askQuestion(_("No such file"), disp_fn,
1714 _("Start a new document with this filename ?"))) {
1715 owner->message(_("Canceled."));
1718 // the user specifically chose this name. Believe them.
1719 Buffer * buffer = bufferlist.newFile(filename, "", true);
1720 view()->buffer(buffer);
1725 #if USE_BOOST_FORMAT
1726 str << boost::format(_("Opening document %1$s...")) % disp_fn;
1728 str << _("Opening document ") << disp_fn << _("...");
1731 owner->message(STRCONV(str.str()));
1733 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1736 view()->buffer(openbuf);
1737 #if USE_BOOST_FORMAT
1738 str2 << boost::format(_("Document %1$s opened.")) % disp_fn;
1740 str2 << _("Document ") << disp_fn << _(" opened.");
1743 #if USE_BOOST_FORMAT
1744 str2 << boost::format(_("Could not open document %1$s"))
1747 str2 << _("Could not open document ") << disp_fn;
1750 owner->message(STRCONV(str2.str()));
1754 void LyXFunc::doImport(string const & argument)
1757 string filename = split(argument, format, ' ');
1759 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1760 << " file: " << filename << endl;
1762 // need user interaction
1763 if (filename.empty()) {
1764 string initpath = lyxrc.document_path;
1766 if (view()->available()) {
1767 string const trypath = owner->buffer()->filePath();
1768 // If directory is writeable, use this as default.
1769 if (IsDirWriteable(trypath))
1773 #if USE_BOOST_FORMAT
1774 boost::format fmt(_("Select %1$s file to import"));
1775 fmt % formats.prettyName(format);
1776 string const text = fmt.str();
1778 string const text = _("Select ") + formats.prettyName(format)
1779 + _(" file to import");;
1782 FileDialog fileDlg(owner, text,
1784 make_pair(string(_("Documents|#o#O")),
1785 string(lyxrc.document_path)),
1786 make_pair(string(_("Examples|#E#e")),
1787 string(AddPath(system_lyxdir, "examples"))));
1789 string const extension = "*." + formats.extension(format)
1790 + "| " + formats.prettyName(format)
1791 + " (*." + formats.extension(format) + ')';
1793 FileDialog::Result result = fileDlg.open(initpath,
1796 if (result.first == FileDialog::Later)
1799 filename = result.second;
1801 // check selected filename
1802 if (filename.empty())
1803 owner->message(_("Canceled."));
1806 if (filename.empty())
1809 // get absolute path of file
1810 filename = MakeAbsPath(filename);
1812 string const lyxfile = ChangeExtension(filename, ".lyx");
1814 // Check if the document already is open
1815 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1816 switch (Alert::askConfirmation(_("Document is already open:"),
1817 MakeDisplayPath(lyxfile, 50),
1818 _("Do you want to close that document now?\n"
1819 "('No' will just switch to the open version)")))
1822 // If close is canceled, we cancel here too.
1823 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1827 view()->buffer(bufferlist.getBuffer(lyxfile));
1830 owner->message(_("Canceled."));
1835 // if the file exists already, and we didn't do
1836 // -i lyx thefile.lyx, warn
1837 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1838 if (!Alert::askQuestion(_("A document by the name"),
1839 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1840 owner->message(_("Canceled."));
1845 Importer::Import(owner, filename, format);
1849 void LyXFunc::reloadBuffer()
1851 string const fn = owner->buffer()->fileName();
1852 if (bufferlist.close(owner->buffer()))
1853 view()->buffer(bufferlist.loadLyXFile(fn));
1857 void LyXFunc::closeBuffer()
1859 if (bufferlist.close(owner->buffer()) && !quitting) {
1860 if (bufferlist.empty()) {
1861 // need this otherwise SEGV may occur while trying to
1862 // set variables that don't exist
1863 // since there's no current buffer
1864 owner->getDialogs().hideBufferDependent();
1866 view()->buffer(bufferlist.first());
1872 // Each "owner" should have it's own message method. lyxview and
1873 // the minibuffer would use the minibuffer, but lyxserver would
1874 // send an ERROR signal to its client. Alejandro 970603
1875 // This func is bit problematic when it comes to NLS, to make the
1876 // lyx servers client be language indepenent we must not translate
1877 // strings sent to this func.
1878 void LyXFunc::setErrorMessage(string const & m) const
1880 dispatch_buffer = m;
1885 void LyXFunc::setMessage(string const & m) const
1887 dispatch_buffer = m;
1891 void LyXFunc::setStatusMessage(string const & m) const
1897 string const LyXFunc::view_status_message()
1899 // When meta-fake key is pressed, show the key sequence so far + "M-".
1901 return keyseq.print() + "M-";
1904 // Else, when a non-complete key sequence is pressed,
1905 // show the available options.
1906 if (keyseq.length() > 0 && !keyseq.deleted()) {
1907 return keyseq.printOptions();
1910 if (!view()->available())
1911 return _("Welcome to LyX!");
1913 return currentState(view());
1917 BufferView * LyXFunc::view() const
1920 return owner->view().get();