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"
35 #include "bufferview_funcs.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/lyx_gui.h"
39 #include "FloatList.h"
40 #include "converter.h"
43 #include "TextCache.h"
45 #include "undo_funcs.h"
46 #include "ParagraphParameters.h"
48 #include "insets/insetcommand.h"
49 #include "insets/insettabular.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/math_cursor.h"
53 #include "mathed/math_inset.h"
55 #include "frontends/FileDialog.h"
56 #include "frontends/Dialogs.h"
57 #include "frontends/Toolbar.h"
58 #include "frontends/Menubar.h"
59 #include "frontends/Alert.h"
61 #include "graphics/GraphicsCache.h"
63 #include "support/lyxalgo.h"
64 #include "support/LAssert.h"
65 #include "support/filetools.h"
66 #include "support/FileInfo.h"
67 #include "support/forkedcontr.h"
68 #include "support/lstrings.h"
69 #include "support/path.h"
70 #include "support/lyxfunctional.h"
86 using std::back_inserter;
88 extern BufferList bufferlist;
89 extern LyXServer * lyxserver;
90 extern bool selection_possible;
92 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
95 extern tex_accent_struct get_accent(kb_action action);
97 extern void ShowLatexLog();
100 LyXFunc::LyXFunc(LyXView * o)
102 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
103 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
105 meta_fake_bit = key_modifier::none;
106 lyx_dead_action = LFUN_NOACTION;
107 lyx_calling_dead_action = LFUN_NOACTION;
112 LyXText * LyXFunc::TEXT(bool flag = true) const
116 return view()->getLyXText();
121 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
123 if (selecting || TEXT(flag)->selection.mark()) {
124 TEXT(flag)->setSelection(view());
125 if (TEXT(flag)->bv_owner)
126 view()->toggleToggle();
128 view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
129 view()->showCursor();
131 view()->switchKeyMap();
135 void LyXFunc::handleKeyFunc(kb_action action)
137 char c = keyseq.getLastKeyEncoded();
139 if (keyseq.length() > 1) {
143 owner->getIntl().getTransManager()
144 .deadkey(c, get_accent(action).accent, TEXT(false));
145 // Need to clear, in case the minibuffer calls these
148 // copied verbatim from do_accent_char
149 view()->update(TEXT(false),
150 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
151 TEXT(false)->selection.cursor = TEXT(false)->cursor;
155 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
156 key_modifier::state state)
160 if (lyxerr.debugging(Debug::KEY)) {
161 lyxerr << "KeySym is "
162 << keysym->getSymbolName()
166 // Do nothing if we have nothing (JMarc)
167 if (!keysym->isOK()) {
168 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
173 if (keysym->isModifier()) {
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());
217 if (action == LFUN_UNKNOWN_ACTION) {
218 // It is unknown, but what if we remove all
219 // the modifiers? (Lgb)
220 action = keyseq.addkey(keysym, key_modifier::none);
222 lyxerr[Debug::KEY] << "Removing modifiers...\n"
223 << "Action now set to ["
224 << action << "]" << endl;
226 if (action == LFUN_UNKNOWN_ACTION) {
227 owner->message(_("Unknown function."));
232 if (action == LFUN_SELFINSERT) {
233 char c = keysym->getISOEncoded();
239 dispatch(FuncRequest(view(), LFUN_SELFINSERT, argument));
240 lyxerr[Debug::KEY] << "SelfInsert arg[`"
241 << argument << "']" << endl;
248 FuncStatus LyXFunc::getStatus(int ac) const
250 return getStatus(lyxaction.retrieveActionArg(ac));
254 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
257 Buffer * buf = owner->buffer();
259 if (ev.action == LFUN_NOACTION) {
260 setStatusMessage(N_("Nothing to do"));
261 return flag.disabled(true);
264 if (ev.action == LFUN_UNKNOWN_ACTION) {
265 setStatusMessage(N_("Unknown action"));
266 return flag.unknown(true);
269 // the default error message if we disable the command
270 setStatusMessage(N_("Command disabled"));
272 // Check whether we need a buffer
273 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
274 // Yes we need a buffer, do we have one?
277 // Can we use a readonly buffer?
278 if (buf->isReadonly() &&
279 !lyxaction.funcHasFlag(ev.action,
280 LyXAction::ReadOnly)) {
282 setStatusMessage(N_("Document is read-only"));
287 setStatusMessage(N_("Command not allowed with"
288 "out any document open"));
289 return flag.disabled(true);
293 UpdatableInset * tli = view()->theLockingInset();
295 // I would really like to avoid having this switch and rather try to
296 // encode this in the function itself.
297 bool disable = false;
300 disable = !Exporter::IsExportable(buf, "dvi")
301 || lyxrc.print_command == "none";
304 disable = ev.argument == "fax" &&
305 !Exporter::IsExportable(buf, ev.argument);
308 disable = buf->undostack.empty();
311 disable = buf->redostack.empty();
315 disable = !mathcursor && !view()->getLyXText()->selection.set();
317 #ifndef HAVE_LIBAIKSAURUS
318 case LFUN_THESAURUS_ENTRY:
323 disable = lyxrc.chktex_command == "none";
326 disable = !Exporter::IsExportable(buf, "program");
329 case LFUN_LAYOUT_CHARACTER:
330 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
333 case LFUN_LAYOUT_TABULAR:
335 || (tli->lyxCode() != Inset::TABULAR_CODE
336 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
340 case LFUN_LAYOUT_PARAGRAPH: {
341 Inset * inset = TEXT(false)->cursor.par()->inInset();
342 disable = inset && inset->forceDefaultParagraphs(inset);
346 case LFUN_INSET_OPTARG:
347 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
350 case LFUN_TABULAR_FEATURE:
353 // FIXME: check temporarily disabled
355 char align = mathcursor->valign();
360 if (ev.argument.empty()) {
364 if (!contains("tcb", ev.argument[0])) {
368 flag.setOnOff(ev.argument[0] == align);
372 char align = mathcursor->halign();
377 if (ev.argument.empty()) {
381 if (!contains("lcr", ev.argument[0])) {
385 flag.setOnOff(ev.argument[0] == align);
388 disable = !mathcursor->halign();
394 //ret.disabled(true);
395 if (tli->lyxCode() == Inset::TABULAR_CODE) {
396 ret = static_cast<InsetTabular *>(tli)
397 ->getStatus(ev.argument);
398 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
399 ret = static_cast<InsetTabular *>
400 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
401 ->getStatus(ev.argument);
406 static InsetTabular inset(*owner->buffer(), 1, 1);
410 ret = inset.getStatus(ev.argument);
411 if (ret.onoff(true) || ret.onoff(false))
412 flag.setOnOff(false);
416 case LFUN_VC_REGISTER:
417 disable = buf->lyxvc.inUse();
419 case LFUN_VC_CHECKIN:
420 disable = !buf->lyxvc.inUse() || buf->isReadonly();
422 case LFUN_VC_CHECKOUT:
423 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
427 case LFUN_VC_HISTORY:
428 disable = !buf->lyxvc.inUse();
430 case LFUN_BOOKMARK_GOTO:
432 isSavedPosition(strToUnsignedInt(ev.argument));
434 case LFUN_INSET_TOGGLE: {
435 LyXText * lt = view()->getLyXText();
436 disable = !(isEditableInset(lt->getInset())
438 && lt->inset_owner->owner()
439 && lt->inset_owner->owner()->isOpen()));
443 disable = !IsFileReadable(buf->getLogName().second);
446 case LFUN_MATH_MUTATE:
448 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
449 flag.setOnOff(false);
454 // we just need to be in math mode to enable that
456 case LFUN_MATH_SPACE:
457 case LFUN_MATH_LIMITS:
458 case LFUN_MATH_NONUMBER:
459 case LFUN_MATH_NUMBER:
460 case LFUN_MATH_EXTERN:
461 disable = !mathcursor;
468 // the functions which insert insets
469 Inset::Code code = Inset::NO_CODE;
472 code = Inset::ERT_CODE;
474 case LFUN_INSET_GRAPHICS:
475 code = Inset::GRAPHICS_CODE;
477 case LFUN_INSET_FOOTNOTE:
478 code = Inset::FOOT_CODE;
480 case LFUN_TABULAR_INSERT:
481 code = Inset::TABULAR_CODE;
483 case LFUN_INSET_EXTERNAL:
484 code = Inset::EXTERNAL_CODE;
486 case LFUN_INSET_MARGINAL:
487 code = Inset::MARGIN_CODE;
489 case LFUN_INSET_MINIPAGE:
490 code = Inset::MINIPAGE_CODE;
492 case LFUN_INSET_FLOAT:
493 case LFUN_INSET_WIDE_FLOAT:
494 code = Inset::FLOAT_CODE;
496 case LFUN_INSET_WRAP:
497 code == Inset::WRAP_CODE;
499 case LFUN_FLOAT_LIST:
500 code = Inset::FLOAT_LIST_CODE;
503 case LFUN_INSET_LIST:
504 code = Inset::LIST_CODE;
506 case LFUN_INSET_THEOREM:
507 code = Inset::THEOREM_CODE;
510 case LFUN_INSET_CAPTION:
511 code = Inset::CAPTION_CODE;
513 case LFUN_INSERT_NOTE:
514 code = Inset::NOTE_CODE;
516 case LFUN_INSERT_LABEL:
517 code = Inset::LABEL_CODE;
519 case LFUN_INSET_OPTARG:
520 code = Inset::OPTARG_CODE;
522 case LFUN_REF_INSERT:
523 code = Inset::REF_CODE;
525 case LFUN_CITATION_CREATE:
526 case LFUN_CITATION_INSERT:
527 code = Inset::CITE_CODE;
529 case LFUN_INSERT_BIBTEX:
530 code = Inset::BIBTEX_CODE;
532 case LFUN_INDEX_INSERT:
533 code = Inset::INDEX_CODE;
535 case LFUN_INDEX_PRINT:
536 code = Inset::INDEX_PRINT_CODE;
538 case LFUN_CHILD_INSERT:
539 code = Inset::INCLUDE_CODE;
541 case LFUN_TOC_INSERT:
542 code = Inset::TOC_CODE;
544 case LFUN_PARENTINSERT:
545 code = Inset::PARENT_CODE;
549 case LFUN_INSERT_URL:
550 code = Inset::URL_CODE;
553 // always allow this, since we will inset a raw quote
554 // if an inset is not allowed.
556 case LFUN_HYPHENATION:
557 case LFUN_LIGATURE_BREAK:
559 case LFUN_MENU_SEPARATOR:
561 case LFUN_END_OF_SENTENCE:
562 code = Inset::SPECIALCHAR_CODE;
564 case LFUN_PROTECTEDSPACE:
565 // slight hack: we know this is allowed in math mode
567 code = Inset::SPECIALCHAR_CODE;
572 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
578 // A few general toggles
580 case LFUN_TOOLTIPS_TOGGLE:
581 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
584 case LFUN_READ_ONLY_TOGGLE:
585 flag.setOnOff(buf->isReadonly());
588 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
590 case LFUN_SWITCHBUFFER:
591 // toggle on the current buffer, but do not toggle off
592 // the other ones (is that a good idea?)
593 if (ev.argument == buf->fileName())
600 // the font related toggles
602 LyXFont const & font = TEXT(false)->real_current_font;
605 flag.setOnOff(font.emph() == LyXFont::ON);
608 flag.setOnOff(font.noun() == LyXFont::ON);
611 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
614 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
617 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
620 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
626 string tc = mathcursor->getLastCode();
629 flag.setOnOff(tc == "mathbf");
632 flag.setOnOff(tc == "mathsf");
635 flag.setOnOff(tc == "mathcal");
638 flag.setOnOff(tc == "mathrm");
641 flag.setOnOff(tc == "mathtt");
644 flag.setOnOff(tc == "mathbb");
647 flag.setOnOff(tc == "mathnormal");
654 // this one is difficult to get right. As a half-baked
655 // solution, we consider only the first action of the sequence
656 if (ev.action == LFUN_SEQUENCE) {
657 // argument contains ';'-terminated commands
658 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
665 void LyXFunc::dispatch(string const & s, bool verbose)
667 int const action = lyxaction.LookupFunc(s);
669 if (action == LFUN_UNKNOWN_ACTION) {
670 string const msg = string(_("Unknown function ("))
676 dispatch(action, verbose);
680 void LyXFunc::dispatch(int ac, bool verbose)
682 dispatch(lyxaction.retrieveActionArg(ac), verbose);
687 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
689 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
690 <<"] arg[" << ev.argument << "]" << endl;
692 // we have not done anything wrong yet.
694 dispatch_buffer.erase();
696 #ifdef NEW_DISPATCHER
697 // We try do call the most specific dispatcher first:
698 // 1. the lockinginset's dispatch
699 // 2. the bufferview's dispatch
700 // 3. the lyxview's dispatch
703 selection_possible = false;
705 if (view()->available())
706 view()->hideCursor();
708 string argument = ev.argument;
709 kb_action action = ev.action;
711 // We cannot use this function here
712 if (getStatus(ev).disabled()) {
713 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
714 << lyxaction.getActionName(action)
715 << " [" << action << "] is disabled at this location"
717 setErrorMessage(getStatusMessage());
718 goto exit_with_message;
721 if (view()->available() && view()->theLockingInset()) {
722 Inset::RESULT result;
723 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
724 (!keyseq.deleted())))
726 UpdatableInset * inset = view()->theLockingInset();
730 inset->getCursorPos(view(), inset_x, dummy_y);
732 if ((action == LFUN_UNKNOWN_ACTION)
733 && argument.empty()) {
734 argument = keyseq.getLastKeyEncoded();
736 // Undo/Redo is a bit tricky for insets.
737 if (action == LFUN_UNDO) {
739 goto exit_with_message;
740 } else if (action == LFUN_REDO) {
742 goto exit_with_message;
743 } else if (((result=inset->
744 // Hand-over to inset's own dispatch:
745 localDispatch(FuncRequest(view(), action, argument))) ==
746 UpdatableInset::DISPATCHED) ||
747 (result == UpdatableInset::DISPATCHED_NOUPDATE))
748 goto exit_with_message;
749 // If UNDISPATCHED, just soldier on
750 else if (result == UpdatableInset::FINISHED) {
751 goto exit_with_message;
752 // We do not need special RTL handling here:
753 // FINISHED means that the cursor should be
754 // one position after the inset.
755 } else if (result == UpdatableInset::FINISHED_RIGHT) {
756 TEXT()->cursorRight(view());
757 moveCursorUpdate(true, false);
758 owner->view_state_changed();
759 goto exit_with_message;
760 } else if (result == UpdatableInset::FINISHED_UP) {
761 if (TEXT()->cursor.irow()->previous()) {
763 TEXT()->setCursorFromCoordinates(
764 view(), TEXT()->cursor.ix() + inset_x,
765 TEXT()->cursor.iy() -
766 TEXT()->cursor.irow()->baseline() - 1);
767 TEXT()->cursor.x_fix(TEXT()->cursor.x());
769 TEXT()->cursorUp(view());
771 moveCursorUpdate(true, false);
772 owner->view_state_changed();
774 view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
776 goto exit_with_message;
777 } else if (result == UpdatableInset::FINISHED_DOWN) {
778 if (TEXT()->cursor.irow()->next()) {
780 TEXT()->setCursorFromCoordinates(
781 view(), TEXT()->cursor.ix() + inset_x,
782 TEXT()->cursor.iy() -
783 TEXT()->cursor.irow()->baseline() +
784 TEXT()->cursor.irow()->height() + 1);
785 TEXT()->cursor.x_fix(TEXT()->cursor.x());
787 TEXT()->cursorDown(view());
790 TEXT()->cursorRight(view());
792 moveCursorUpdate(true, false);
793 owner->view_state_changed();
794 goto exit_with_message;
796 #warning I am not sure this is still right, please have a look! (Jug 20020417)
797 else { // result == UNDISPATCHED
798 //setMessage(N_("Text mode"));
800 case LFUN_UNKNOWN_ACTION:
801 case LFUN_BREAKPARAGRAPH:
803 TEXT()->cursorRight(view());
804 view()->switchKeyMap();
805 owner->view_state_changed();
808 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
809 TEXT()->cursorRight(view());
810 moveCursorUpdate(true, false);
811 owner->view_state_changed();
813 goto exit_with_message;
815 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
816 TEXT()->cursorRight(view());
817 moveCursorUpdate(true, false);
818 owner->view_state_changed();
820 goto exit_with_message;
822 if (TEXT()->cursor.row()->next())
823 TEXT()->cursorDown(view());
825 TEXT()->cursorRight(view());
826 moveCursorUpdate(true, false);
827 owner->view_state_changed();
828 goto exit_with_message;
840 if (!view()->available()) break;
841 // this function should be used always [asierra060396]
842 UpdatableInset * tli =
843 view()->theLockingInset();
845 UpdatableInset * lock = tli->getLockingInset();
848 view()->unlockInset(tli);
849 TEXT()->cursorRight(view());
850 moveCursorUpdate(true, false);
851 owner->view_state_changed();
853 tli->unlockInsetInInset(view(),
858 // Tell the paragraph dialog that we changed paragraph
859 owner->getDialogs().updateParagraph();
864 // --- Misc -------------------------------------------
865 case LFUN_WORDFINDFORWARD :
866 case LFUN_WORDFINDBACKWARD : {
867 static string last_search;
868 string searched_string;
870 if (!argument.empty()) {
871 last_search = argument;
872 searched_string = argument;
874 searched_string = last_search;
876 bool fw = (action == LFUN_WORDFINDBACKWARD);
877 if (!searched_string.empty()) {
878 lyxfind::LyXFind(view(), searched_string, fw);
880 // view()->showCursor();
886 if (view()->available() && !view()->theLockingInset()) {
887 view()->update(TEXT(),
888 BufferView::SELECT|BufferView::FITCUR);
890 owner->message(keyseq.printOptions());
894 // --- Misc -------------------------------------------
895 case LFUN_EXEC_COMMAND:
896 owner->focus_command_buffer();
899 case LFUN_CANCEL: // RVDK_PATCH_5
901 meta_fake_bit = key_modifier::none;
902 if (view()->available())
903 // cancel any selection
904 dispatch(LFUN_MARK_OFF);
905 setMessage(N_("Cancel"));
908 case LFUN_META_FAKE: // RVDK_PATCH_5
910 meta_fake_bit = key_modifier::alt;
911 setMessage(keyseq.print());
915 case LFUN_READ_ONLY_TOGGLE:
916 if (owner->buffer()->lyxvc.inUse()) {
917 owner->buffer()->lyxvc.toggleReadOnly();
919 owner->buffer()->setReadonly(
920 !owner->buffer()->isReadonly());
924 case LFUN_CENTER: // this is center and redraw.
928 // --- Menus -----------------------------------------------
930 menuNew(argument, false);
933 case LFUN_MENUNEWTMPLT:
934 menuNew(argument, true);
937 case LFUN_CLOSEBUFFER:
942 if (!owner->buffer()->isUnnamed()) {
944 s1 << _("Saving document") << ' '
945 << MakeDisplayPath(owner->buffer()->fileName() + "...");
946 owner->message(s1.str().c_str());
947 MenuWrite(view(), owner->buffer());
949 owner->message(s1.str().c_str());
951 WriteAs(view(), owner->buffer());
955 WriteAs(view(), owner->buffer(), argument);
958 case LFUN_MENURELOAD:
963 Exporter::Export(owner->buffer(), argument, true);
967 Exporter::Preview(owner->buffer(), argument);
971 Exporter::Export(owner->buffer(), "program", true);
975 MenuRunChktex(owner->buffer());
979 owner->getDialogs().showPrint();
983 if (argument == "custom")
984 owner->getDialogs().showSendto();
986 Exporter::Export(owner->buffer(), argument, false);
1004 InsetCommandParams p;
1007 if (action == LFUN_TOCVIEW)
1009 p.setCmdName("tableofcontents");
1011 else if (action == LFUN_LOAVIEW)
1012 p.setCmdName("listof{algorithm}{List of Algorithms}");
1013 else if (action == LFUN_LOFVIEW)
1014 p.setCmdName("listoffigures");
1016 p.setCmdName("listoftables");
1018 owner->getDialogs().createTOC(p.getAsString());
1034 case LFUN_MENUSEARCH:
1035 owner->getDialogs().showSearch();
1038 case LFUN_REMOVEERRORS:
1039 if (view()->removeAutoInsets()) {
1040 #warning repaint() or update() or nothing ?
1042 view()->fitCursor();
1046 case LFUN_DEPTH_MIN:
1047 changeDepth(view(), TEXT(false), -1);
1050 case LFUN_DEPTH_PLUS:
1051 changeDepth(view(), TEXT(false), 1);
1055 owner->getDialogs().setUserFreeFont();
1058 case LFUN_RECONFIGURE:
1059 Reconfigure(view());
1063 case LFUN_FLOATSOPERATE:
1064 if (argument == "openfoot")
1065 view()->allFloats(1,0);
1066 else if (argument == "closefoot")
1067 view()->allFloats(0,0);
1068 else if (argument == "openfig")
1069 view()->allFloats(1,1);
1070 else if (argument == "closefig")
1071 view()->allFloats(0,1);
1074 #ifdef WITH_WARNINGS
1075 //#warning Find another implementation here (or another lyxfunc)!
1078 case LFUN_HELP_ABOUTLYX:
1079 owner->getDialogs().showAboutlyx();
1082 case LFUN_HELP_TEXINFO:
1083 owner->getDialogs().showTexinfo();
1086 case LFUN_HELP_OPEN:
1088 string const arg = argument;
1090 setErrorMessage(N_("Missing argument"));
1093 owner->prohibitInput();
1094 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1095 if (fname.empty()) {
1096 lyxerr << "LyX: unable to find documentation file `"
1097 << arg << "'. Bad installation?" << endl;
1098 owner->allowInput();
1102 str << _("Opening help file") << ' '
1103 << MakeDisplayPath(fname) << "...";
1104 owner->message(str.str().c_str());
1105 view()->buffer(bufferlist.loadLyXFile(fname, false));
1106 owner->allowInput();
1110 // --- version control -------------------------------
1111 case LFUN_VC_REGISTER:
1113 if (!owner->buffer()->lyxvc.inUse())
1114 owner->buffer()->lyxvc.registrer();
1118 case LFUN_VC_CHECKIN:
1120 if (owner->buffer()->lyxvc.inUse()
1121 && !owner->buffer()->isReadonly())
1122 owner->buffer()->lyxvc.checkIn();
1126 case LFUN_VC_CHECKOUT:
1128 if (owner->buffer()->lyxvc.inUse()
1129 && owner->buffer()->isReadonly())
1130 owner->buffer()->lyxvc.checkOut();
1134 case LFUN_VC_REVERT:
1136 owner->buffer()->lyxvc.revert();
1142 owner->buffer()->lyxvc.undoLast();
1146 case LFUN_VC_HISTORY:
1148 owner->getDialogs().showVCLogFile();
1152 // --- buffers ----------------------------------------
1154 case LFUN_SWITCHBUFFER:
1155 view()->buffer(bufferlist.getBuffer(argument));
1160 // servercmd: argument must be <file>:<template>
1161 Buffer * tmpbuf = NewFile(argument);
1163 view()->buffer(tmpbuf);
1167 case LFUN_FILE_OPEN:
1171 case LFUN_LATEX_LOG:
1172 owner->getDialogs().showLogFile();
1175 case LFUN_LAYOUT_DOCUMENT:
1176 owner->getDialogs().showDocument();
1179 case LFUN_LAYOUT_PARAGRAPH:
1180 owner->getDialogs().showParagraph();
1183 case LFUN_LAYOUT_CHARACTER:
1184 owner->getDialogs().showCharacter();
1187 case LFUN_LAYOUT_TABULAR:
1188 if (view()->theLockingInset()) {
1189 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1190 InsetTabular * inset = static_cast<InsetTabular *>
1191 (view()->theLockingInset());
1192 inset->openLayoutDialog(view());
1193 } else if (view()->theLockingInset()->
1194 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1195 InsetTabular * inset = static_cast<InsetTabular *>(
1196 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1197 inset->openLayoutDialog(view());
1202 case LFUN_LAYOUT_PREAMBLE:
1203 owner->getDialogs().showPreamble();
1206 case LFUN_DROP_LAYOUTS_CHOICE:
1207 owner->getToolbar().openLayoutList();
1210 case LFUN_MENU_OPEN_BY_NAME:
1211 owner->getMenubar().openByName(argument);
1212 break; // RVDK_PATCH_5
1214 case LFUN_SPELLCHECK:
1215 owner->getDialogs().showSpellchecker();
1218 // --- lyxserver commands ----------------------------
1222 setMessage(owner->buffer()->fileName());
1223 lyxerr[Debug::INFO] << "FNAME["
1224 << owner->buffer()->fileName()
1230 dispatch_buffer = keyseq.print();
1231 lyxserver->notifyClient(dispatch_buffer);
1235 case LFUN_GOTOFILEROW:
1239 istringstream istr(argument.c_str());
1240 istr >> file_name >> row;
1241 // Must replace extension of the file to be .lyx and get full path
1242 string const s(ChangeExtension(file_name, ".lyx"));
1244 // Either change buffer or load the file
1245 if (bufferlist.exists(s)) {
1246 view()->buffer(bufferlist.getBuffer(s));
1248 view()->buffer(bufferlist.loadLyXFile(s));
1251 view()->setCursorFromRow(row);
1254 // see BufferView_pimpl::center()
1255 view()->updateScrollbar();
1259 case LFUN_GOTO_PARAGRAPH:
1261 istringstream istr(argument.c_str());
1265 Paragraph * par = owner->buffer()->getParFromID(id);
1267 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1268 << id << "]" << endl;
1271 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1272 << " found." << endl;
1275 if (view()->theLockingInset())
1276 view()->unlockInset(view()->theLockingInset());
1277 if (par->inInset()) {
1278 par->inInset()->edit(view());
1281 view()->getLyXText()->setCursor(view(), par, 0);
1282 view()->switchKeyMap();
1283 owner->view_state_changed();
1286 // see BufferView_pimpl::center()
1287 view()->updateScrollbar();
1294 int const qa = lyxaction.LookupFunc(argument);
1295 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1299 // --- insert characters ----------------------------------------
1301 // --- Mathed stuff. If we are here, there is no locked inset yet.
1302 case LFUN_MATH_EXTERN:
1303 case LFUN_MATH_NUMBER:
1304 case LFUN_MATH_NONUMBER:
1305 case LFUN_MATH_LIMITS:
1307 setErrorMessage(N_("This is only allowed in math mode!"));
1311 // passthrough hat and underscore outside mathed:
1312 case LFUN_SUBSCRIPT:
1313 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1315 case LFUN_SUPERSCRIPT:
1316 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1319 case LFUN_MATH_PANEL:
1320 owner->getDialogs().showMathPanel();
1323 case LFUN_CITATION_CREATE:
1325 InsetCommandParams p("cite");
1327 if (!argument.empty()) {
1328 // This should be set at source, ie when typing
1329 // "citation-insert foo" in the minibuffer.
1330 // Question: would pybibliographer also need to be
1331 // changed. Suspect so. Leave as-is therefore.
1332 if (contains(argument, "|")) {
1333 p.setContents(token(argument, '|', 0));
1334 p.setOptions(token(argument, '|', 1));
1336 p.setContents(argument);
1338 dispatch(FuncRequest(view(), LFUN_CITATION_INSERT, p.getAsString()));
1340 owner->getDialogs().createCitation(p.getAsString());
1344 case LFUN_CHILDOPEN:
1346 string const filename =
1347 MakeAbsPath(argument,
1348 owner->buffer()->filePath());
1349 setMessage(N_("Opening child document ") +
1350 MakeDisplayPath(filename) + "...");
1351 view()->savePosition(0);
1352 if (bufferlist.exists(filename))
1353 view()->buffer(bufferlist.getBuffer(filename));
1355 view()->buffer(bufferlist.loadLyXFile(filename));
1359 case LFUN_TOGGLECURSORFOLLOW:
1360 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1364 owner->getIntl().KeyMapOn(false);
1367 case LFUN_KMAP_PRIM:
1368 owner->getIntl().KeyMapPrim();
1372 owner->getIntl().KeyMapSec();
1375 case LFUN_KMAP_TOGGLE:
1376 owner->getIntl().ToggleKeyMap();
1381 // argument contains ';'-terminated commands
1382 while (!argument.empty()) {
1384 argument = split(argument, first, ';');
1390 case LFUN_DIALOG_PREFERENCES:
1391 owner->getDialogs().showPreferences();
1394 case LFUN_SAVEPREFERENCES:
1396 Path p(user_lyxdir);
1397 lyxrc.write("preferences");
1401 case LFUN_SCREEN_FONT_UPDATE:
1403 // handle the screen font changes.
1404 lyxrc.set_font_norm_type();
1405 lyx_gui::update_fonts();
1406 // We also need to empty the textcache so that
1407 // the buffer will be formatted correctly after
1410 // Of course we should only do the resize and the textcache.clear
1411 // if values really changed...but not very important right now. (Lgb)
1412 // All visible buffers will need resize
1418 case LFUN_SET_COLOR:
1421 string const x11_name = split(argument, lyx_name, ' ');
1422 if (lyx_name.empty() || x11_name.empty()) {
1423 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1428 bool const graphicsbg_changed =
1429 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1430 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1432 if (!lcolor.setColor(lyx_name, x11_name)) {
1433 static string const err1 (N_("Set-color \""));
1434 static string const err2 (
1435 N_("\" failed - color is undefined "
1436 "or may not be redefined"));
1437 setErrorMessage(_(err1) + lyx_name + _(err2));
1441 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1443 if (graphicsbg_changed) {
1444 #ifdef WITH_WARNINGS
1445 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1448 grfx::GCache & gc = grfx::GCache::get();
1449 gc.changeDisplay(true);
1458 owner->message(argument);
1461 case LFUN_FORKS_SHOW:
1462 owner->getDialogs().showForks();
1465 case LFUN_FORKS_KILL:
1467 if (!isStrInt(argument))
1470 pid_t const pid = strToInt(argument);
1471 ForkedcallsController & fcc = ForkedcallsController::get();
1476 case LFUN_TOOLTIPS_TOGGLE:
1477 owner->getDialogs().toggleTooltips();
1481 // Then if it was none of the above
1482 // Trying the BufferView::pimpl dispatch:
1483 if (!view()->dispatch(ev))
1484 lyxerr << "A truly unknown func ["
1485 << lyxaction.getActionName(ev.action) << "]!"
1491 sendDispatchMessage(getMessage(), ev, verbose);
1495 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1497 owner->updateMenubar();
1498 owner->updateToolbar();
1500 if (ev.action == LFUN_SELFINSERT || !verbose) {
1501 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1503 owner->message(msg);
1507 string dispatch_msg = msg;
1508 if (!dispatch_msg.empty())
1509 dispatch_msg += " ";
1511 string comname = lyxaction.getActionName(ev.action);
1513 int pseudoaction = ev.action;
1514 bool argsadded = false;
1516 if (!ev.argument.empty()) {
1517 // the pseudoaction is useful for the bindings
1518 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1520 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1521 pseudoaction = ev.action;
1523 comname += " " + ev.argument;
1528 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1530 if (!shortcuts.empty()) {
1531 comname += ": " + shortcuts;
1532 } else if (!argsadded && !ev.argument.empty()) {
1533 comname += " " + ev.argument;
1536 if (!comname.empty()) {
1537 comname = rtrim(comname);
1538 dispatch_msg += "(" + comname + ')';
1541 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1542 if (!dispatch_msg.empty())
1543 owner->message(dispatch_msg);
1547 void LyXFunc::setupLocalKeymap()
1549 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1550 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1554 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1556 string initpath = lyxrc.document_path;
1557 string filename(name);
1559 if (view()->available()) {
1560 string const trypath = owner->buffer()->filePath();
1561 // If directory is writeable, use this as default.
1562 if (IsDirWriteable(trypath))
1566 static int newfile_number;
1568 if (filename.empty()) {
1569 filename = AddName(lyxrc.document_path,
1570 "newfile" + tostr(++newfile_number) + ".lyx");
1571 FileInfo fi(filename);
1572 while (bufferlist.exists(filename) || fi.readable()) {
1574 filename = AddName(lyxrc.document_path,
1575 "newfile" + tostr(newfile_number) +
1577 fi.newFile(filename);
1581 // The template stuff
1584 FileDialog fileDlg(owner, _("Select template file"),
1585 LFUN_SELECT_FILE_SYNC,
1586 make_pair(string(_("Documents|#o#O")),
1587 string(lyxrc.document_path)),
1588 make_pair(string(_("Templates|#T#t")),
1589 string(lyxrc.template_path)));
1591 FileDialog::Result result =
1592 fileDlg.Select(lyxrc.template_path,
1593 _("*.lyx|LyX Documents (*.lyx)"));
1595 if (result.first == FileDialog::Later)
1598 string const fname = result.second;
1605 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1609 void LyXFunc::open(string const & fname)
1611 string initpath = lyxrc.document_path;
1613 if (view()->available()) {
1614 string const trypath = owner->buffer()->filePath();
1615 // If directory is writeable, use this as default.
1616 if (IsDirWriteable(trypath))
1622 if (fname.empty()) {
1623 FileDialog fileDlg(owner, _("Select document to open"),
1625 make_pair(string(_("Documents|#o#O")),
1626 string(lyxrc.document_path)),
1627 make_pair(string(_("Examples|#E#e")),
1628 string(AddPath(system_lyxdir, "examples"))));
1630 FileDialog::Result result =
1631 fileDlg.Select(initpath,
1632 "*.lyx|LyX Documents (*.lyx)");
1634 if (result.first == FileDialog::Later)
1637 filename = result.second;
1639 // check selected filename
1640 if (filename.empty()) {
1641 owner->message(_("Canceled."));
1647 // get absolute path of file and add ".lyx" to the filename if
1649 string const fullpath = FileSearch(string(), filename, "lyx");
1650 if (!fullpath.empty()) {
1651 filename = fullpath;
1654 string const disp_fn(MakeDisplayPath(filename));
1656 // if the file doesn't exist, let the user create one
1657 FileInfo const f(filename, true);
1659 if (!Alert::askQuestion(_("No such file"), disp_fn,
1660 _("Start a new document with this filename ?"))) {
1661 owner->message(_("Canceled"));
1664 // the user specifically chose this name. Believe them.
1665 Buffer * buffer = bufferlist.newFile(filename, "", true);
1666 view()->buffer(buffer);
1671 str << _("Opening document") << ' ' << disp_fn << "...";
1673 owner->message(str.str().c_str());
1675 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1677 view()->buffer(openbuf);
1679 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1680 owner->message(str.str().c_str());
1683 str << _("Could not open document") << ' ' << disp_fn;
1684 owner->message(str.str().c_str());
1689 void LyXFunc::doImport(string const & argument)
1692 string filename = split(argument, format, ' ');
1694 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1695 << " file: " << filename << endl;
1697 // need user interaction
1698 if (filename.empty()) {
1699 string initpath = lyxrc.document_path;
1701 if (view()->available()) {
1702 string const trypath = owner->buffer()->filePath();
1703 // If directory is writeable, use this as default.
1704 if (IsDirWriteable(trypath))
1708 string const text = _("Select ") + formats.prettyName(format)
1709 + _(" file to import");
1711 FileDialog fileDlg(owner, text,
1713 make_pair(string(_("Documents|#o#O")),
1714 string(lyxrc.document_path)),
1715 make_pair(string(_("Examples|#E#e")),
1716 string(AddPath(system_lyxdir, "examples"))));
1718 string const extension = "*." + formats.extension(format)
1719 + "| " + formats.prettyName(format)
1720 + " (*." + formats.extension(format) + ")";
1722 FileDialog::Result result = fileDlg.Select(initpath,
1725 if (result.first == FileDialog::Later)
1728 filename = result.second;
1730 // check selected filename
1731 if (filename.empty())
1732 owner->message(_("Canceled."));
1735 if (filename.empty())
1738 // get absolute path of file
1739 filename = MakeAbsPath(filename);
1741 string const lyxfile = ChangeExtension(filename, ".lyx");
1743 // Check if the document already is open
1744 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1745 switch (Alert::askConfirmation(_("Document is already open:"),
1746 MakeDisplayPath(lyxfile, 50),
1747 _("Do you want to close that document now?\n"
1748 "('No' will just switch to the open version)")))
1751 // If close is canceled, we cancel here too.
1752 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1756 view()->buffer(bufferlist.getBuffer(lyxfile));
1759 owner->message(_("Canceled."));
1764 // if the file exists already, and we didn't do
1765 // -i lyx thefile.lyx, warn
1766 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1767 if (!Alert::askQuestion(_("A document by the name"),
1768 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1769 owner->message(_("Canceled"));
1774 Importer::Import(owner, filename, format);
1778 void LyXFunc::reloadBuffer()
1780 string const fn = owner->buffer()->fileName();
1781 if (bufferlist.close(owner->buffer()))
1782 view()->buffer(bufferlist.loadLyXFile(fn));
1786 void LyXFunc::closeBuffer()
1788 if (bufferlist.close(owner->buffer()) && !quitting) {
1789 if (bufferlist.empty()) {
1790 // need this otherwise SEGV may occur while trying to
1791 // set variables that don't exist
1792 // since there's no current buffer
1793 owner->getDialogs().hideBufferDependent();
1795 view()->buffer(bufferlist.first());
1801 // Each "owner" should have it's own message method. lyxview and
1802 // the minibuffer would use the minibuffer, but lyxserver would
1803 // send an ERROR signal to its client. Alejandro 970603
1804 // This func is bit problematic when it comes to NLS, to make the
1805 // lyx servers client be language indepenent we must not translate
1806 // strings sent to this func.
1807 void LyXFunc::setErrorMessage(string const & m) const
1809 dispatch_buffer = m;
1814 void LyXFunc::setMessage(string const & m) const
1816 dispatch_buffer = m;
1820 void LyXFunc::setStatusMessage(string const & m) const
1826 string const LyXFunc::view_status_message()
1828 // When meta-fake key is pressed, show the key sequence so far + "M-".
1830 return keyseq.print() + "M-";
1833 // Else, when a non-complete key sequence is pressed,
1834 // show the available options.
1835 if (keyseq.length() > 0 && !keyseq.deleted()) {
1836 return keyseq.printOptions();
1839 if (!view()->available())
1840 return _("Welcome to LyX!");
1842 return currentState(view());
1846 BufferView * LyXFunc::view() const
1849 return owner->view().get();