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/inseturl.h"
49 #include "insets/insetlatexaccent.h"
50 #include "insets/insettoc.h"
51 #include "insets/insetref.h"
52 #include "insets/insetparent.h"
53 #include "insets/insetindex.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insetbib.h"
56 #include "insets/insetcite.h"
57 #include "insets/insettext.h"
58 #include "insets/insetert.h"
59 #include "insets/insetexternal.h"
60 #include "insets/insetgraphics.h"
61 #include "insets/insetfoot.h"
62 #include "insets/insetmarginal.h"
63 #include "insets/insetminipage.h"
64 #include "insets/insetfloat.h"
66 #include "insets/insetlist.h"
67 #include "insets/insettheorem.h"
69 #include "insets/insettabular.h"
70 #include "insets/insetcaption.h"
72 #include "mathed/formulamacro.h"
73 #include "mathed/math_cursor.h"
74 #include "mathed/math_inset.h"
76 #include "frontends/FileDialog.h"
77 #include "frontends/Dialogs.h"
78 #include "frontends/Toolbar.h"
79 #include "frontends/Menubar.h"
80 #include "frontends/Alert.h"
82 #include "graphics/GraphicsCache.h"
84 #include "support/lyxalgo.h"
85 #include "support/LAssert.h"
86 #include "support/filetools.h"
87 #include "support/FileInfo.h"
88 #include "support/forkedcontr.h"
89 #include "support/lstrings.h"
90 #include "support/path.h"
91 #include "support/lyxfunctional.h"
102 using std::make_pair;
106 using std::transform;
107 using std::back_inserter;
109 extern BufferList bufferlist;
110 extern LyXServer * lyxserver;
111 extern bool selection_possible;
113 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
115 extern void show_symbols_form(LyXFunc *);
117 extern LyXAction lyxaction;
119 extern tex_accent_struct get_accent(kb_action action);
121 extern void ShowLatexLog();
124 /* === globals =========================================================== */
127 LyXFunc::LyXFunc(LyXView * o)
129 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
130 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
132 meta_fake_bit = key_modifier::none;
133 lyx_dead_action = LFUN_NOACTION;
134 lyx_calling_dead_action = LFUN_NOACTION;
139 LyXText * LyXFunc::TEXT(bool flag = true) const
142 return owner->view()->text;
143 return owner->view()->getLyXText();
148 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
150 if (selecting || TEXT(flag)->selection.mark()) {
151 TEXT(flag)->setSelection(owner->view().get());
152 if (TEXT(flag)->bv_owner)
153 owner->view()->toggleToggle();
155 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
156 owner->view()->showCursor();
158 owner->view()->switchKeyMap();
162 void LyXFunc::handleKeyFunc(kb_action action)
164 char c = keyseq.getLastKeyEncoded();
166 if (keyseq.length() > 1) {
170 owner->getIntl()->getTransManager()
171 .deadkey(c, get_accent(action).accent, TEXT(false));
172 // Need to clear, in case the minibuffer calls these
175 // copied verbatim from do_accent_char
176 owner->view()->update(TEXT(false),
177 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
178 TEXT(false)->selection.cursor = TEXT(false)->cursor;
182 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
183 key_modifier::state state)
187 if (lyxerr.debugging(Debug::KEY)) {
188 lyxerr << "KeySym is "
189 << keysym->getSymbolName()
192 // Do nothing if we have nothing (JMarc)
193 if (!keysym->isOK()) {
194 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
199 if (keysym->isModifier()) {
203 // Do a one-deep top-level lookup for
204 // cancel and meta-fake keys. RVDK_PATCH_5
205 cancel_meta_seq.reset();
207 int action = cancel_meta_seq.addkey(keysym, state);
208 if (lyxerr.debugging(Debug::KEY)) {
209 lyxerr << "action first set to [" << action << "]" << endl;
212 // When not cancel or meta-fake, do the normal lookup.
213 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
214 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
215 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
216 // remove Caps Lock and Mod2 as a modifiers
217 action = keyseq.addkey(keysym, (state | meta_fake_bit));
218 lyxerr[Debug::KEY] << "action now set to ["
219 << action << "]" << endl;
222 // Dont remove this unless you know what you are doing.
223 meta_fake_bit = key_modifier::none;
225 // can this happen now ?
226 if (action == LFUN_NOACTION) {
227 action = LFUN_PREFIX;
230 if (lyxerr.debugging(Debug::KEY)) {
231 lyxerr << "Key [action="
233 << keyseq.print() << "]"
237 // already here we know if it any point in going further
238 // why not return already here if action == -1 and
239 // num_bytes == 0? (Lgb)
241 if (keyseq.length() > 1) {
242 owner->message(keyseq.print());
245 if (action == LFUN_UNKNOWN_ACTION) {
246 // It is unknown, but what if we remove all
247 // the modifiers? (Lgb)
248 action = keyseq.addkey(keysym, key_modifier::none);
250 lyxerr[Debug::KEY] << "Removing modifiers...\n"
251 << "Action now set to ["
252 << action << "]" << endl;
254 if (action == LFUN_UNKNOWN_ACTION) {
255 owner->message(_("Unknown function."));
260 if (action == LFUN_SELFINSERT) {
261 char c = keysym->getISOEncoded();
267 dispatch(FuncRequest(LFUN_SELFINSERT, argument));
268 lyxerr[Debug::KEY] << "SelfInsert arg[`"
269 << argument << "']" << endl;
276 FuncStatus LyXFunc::getStatus(int ac) const
279 kb_action action = lyxaction.retrieveActionArg(ac, argument);
280 return getStatus(FuncRequest(action, argument));
284 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
287 Buffer * buf = owner->buffer();
289 if (ev.action == LFUN_NOACTION) {
290 setStatusMessage(N_("Nothing to do"));
291 return flag.disabled(true);
294 if (ev.action == LFUN_UNKNOWN_ACTION) {
295 setStatusMessage(N_("Unknown action"));
296 return flag.unknown(true);
299 // the default error message if we disable the command
300 setStatusMessage(N_("Command disabled"));
302 // Check whether we need a buffer
303 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
304 // Yes we need a buffer, do we have one?
307 // Can we use a readonly buffer?
308 if (buf->isReadonly() &&
309 !lyxaction.funcHasFlag(ev.action,
310 LyXAction::ReadOnly)) {
312 setStatusMessage(N_("Document is read-only"));
317 setStatusMessage(N_("Command not allowed with"
318 "out any document open"));
319 return flag.disabled(true);
323 UpdatableInset * tli = owner->view()->theLockingInset();
325 // I would really like to avoid having this switch and rather try to
326 // encode this in the function itself.
327 bool disable = false;
330 disable = !Exporter::IsExportable(buf, "dvi")
331 || lyxrc.print_command == "none";
334 disable = ev.argument == "fax" &&
335 !Exporter::IsExportable(buf, ev.argument);
338 disable = buf->undostack.empty();
341 disable = buf->redostack.empty();
343 #ifndef HAVE_LIBAIKSAURUS
344 case LFUN_THESAURUS_ENTRY:
349 disable = lyxrc.chktex_command == "none";
352 disable = !Exporter::IsExportable(buf, "program");
355 case LFUN_LAYOUT_CHARACTER:
356 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
359 case LFUN_LAYOUT_TABULAR:
361 || (tli->lyxCode() != Inset::TABULAR_CODE
362 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
366 case LFUN_LAYOUT_PARAGRAPH: {
367 Inset * inset = TEXT(false)->cursor.par()->inInset();
368 disable = inset && inset->forceDefaultParagraphs(inset);
372 case LFUN_TABULAR_FEATURE:
376 //ret.disabled(true);
377 if (tli->lyxCode() == Inset::TABULAR_CODE) {
378 ret = static_cast<InsetTabular *>(tli)
379 ->getStatus(ev.argument);
380 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
381 ret = static_cast<InsetTabular *>
382 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
383 ->getStatus(ev.argument);
388 static InsetTabular inset(*owner->buffer(), 1, 1);
392 ret = inset.getStatus(ev.argument);
393 if (ret.onoff(true) || ret.onoff(false))
394 flag.setOnOff(false);
398 case LFUN_VC_REGISTER:
399 disable = buf->lyxvc.inUse();
401 case LFUN_VC_CHECKIN:
402 disable = !buf->lyxvc.inUse() || buf->isReadonly();
404 case LFUN_VC_CHECKOUT:
405 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
409 case LFUN_VC_HISTORY:
410 disable = !buf->lyxvc.inUse();
412 case LFUN_BOOKMARK_GOTO:
413 disable = !owner->view()->
414 isSavedPosition(strToUnsignedInt(ev.argument));
416 case LFUN_INSET_TOGGLE: {
417 LyXText * lt = owner->view()->getLyXText();
418 disable = !(isEditableInset(lt->getInset())
420 && lt->inset_owner->owner()
421 && lt->inset_owner->owner()->isOpen()));
425 disable = !IsFileReadable(buf->getLogName().second);
427 case LFUN_MATH_VALIGN:
428 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
429 char align = mathcursor->valign();
434 if (ev.argument.empty()) {
438 if (!contains("tcb", ev.argument[0])) {
442 flag.setOnOff(ev.argument[0] == align);
447 case LFUN_MATH_HALIGN:
448 if (mathcursor && mathcursor->formula()->hullType() != "simple") {
449 char align = mathcursor->halign();
454 if (ev.argument.empty()) {
458 if (!contains("lcr", ev.argument[0])) {
462 flag.setOnOff(ev.argument[0] == align);
467 case LFUN_MATH_MUTATE:
468 if (tli && (tli->lyxCode() == Inset::MATH_CODE))
469 flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
474 // we just need to be in math mode to enable that
476 case LFUN_MATH_SPACE:
477 case LFUN_MATH_LIMITS:
478 case LFUN_MATH_NONUMBER:
479 case LFUN_MATH_NUMBER:
480 disable = !mathcursor;
483 // we need to be math mode and a math array for that
484 // Hack: halign produces non-zero result iff we are in a math array
485 case LFUN_MATH_ROW_INSERT:
486 case LFUN_MATH_ROW_DELETE:
487 case LFUN_MATH_COLUMN_INSERT:
488 case LFUN_MATH_COLUMN_DELETE:
489 disable = !mathcursor || !mathcursor->halign() ||
490 mathcursor->formula()->hullType() == "simple";
497 // the functions which insert insets
498 Inset::Code code = Inset::NO_CODE;
501 code = Inset::ERT_CODE;
503 case LFUN_INSET_GRAPHICS:
504 code = Inset::GRAPHICS_CODE;
506 case LFUN_INSET_FOOTNOTE:
507 code = Inset::FOOT_CODE;
509 case LFUN_TABULAR_INSERT:
510 code = Inset::TABULAR_CODE;
512 case LFUN_INSET_EXTERNAL:
513 code = Inset::EXTERNAL_CODE;
515 case LFUN_INSET_MARGINAL:
516 code = Inset::MARGIN_CODE;
518 case LFUN_INSET_MINIPAGE:
519 code = Inset::MINIPAGE_CODE;
521 case LFUN_INSET_FLOAT:
522 case LFUN_INSET_WIDE_FLOAT:
523 code = Inset::FLOAT_CODE;
525 case LFUN_FLOAT_LIST:
526 code = Inset::FLOAT_LIST_CODE;
529 case LFUN_INSET_LIST:
530 code = Inset::LIST_CODE;
532 case LFUN_INSET_THEOREM:
533 code = Inset::THEOREM_CODE;
536 case LFUN_INSET_CAPTION:
537 code = Inset::CAPTION_CODE;
539 case LFUN_INSERT_NOTE:
540 code = Inset::NOTE_CODE;
542 case LFUN_INSERT_LABEL:
543 code = Inset::LABEL_CODE;
545 case LFUN_REF_INSERT:
546 code = Inset::REF_CODE;
548 case LFUN_CITATION_CREATE:
549 case LFUN_CITATION_INSERT:
550 code = Inset::CITE_CODE;
552 case LFUN_INSERT_BIBTEX:
553 code = Inset::BIBTEX_CODE;
555 case LFUN_INDEX_INSERT:
556 code = Inset::INDEX_CODE;
558 case LFUN_INDEX_PRINT:
559 code = Inset::INDEX_PRINT_CODE;
561 case LFUN_CHILD_INSERT:
562 code = Inset::INCLUDE_CODE;
564 case LFUN_TOC_INSERT:
565 code = Inset::TOC_CODE;
567 case LFUN_PARENTINSERT:
568 code = Inset::PARENT_CODE;
572 case LFUN_INSERT_URL:
573 code = Inset::URL_CODE;
576 // always allow this, since we will inset a raw quote
577 // if an inset is not allowed.
579 case LFUN_HYPHENATION:
580 case LFUN_LIGATURE_BREAK:
582 case LFUN_MENU_SEPARATOR:
584 case LFUN_END_OF_SENTENCE:
585 code = Inset::SPECIALCHAR_CODE;
587 case LFUN_PROTECTEDSPACE:
588 // slight hack: we know this is allowed in math mode
590 code = Inset::SPECIALCHAR_CODE;
595 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code)) {
602 // A few general toggles
604 case LFUN_TOOLTIPS_TOGGLE:
605 flag.setOnOff(owner->getDialogs()->tooltipsEnabled());
608 case LFUN_READ_ONLY_TOGGLE:
609 flag.setOnOff(buf->isReadonly());
612 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
614 case LFUN_SWITCHBUFFER:
615 // toggle on the current buffer, but do not toggle off
616 // the other ones (is that a good idea?)
617 if (ev.argument == buf->fileName())
624 // the font related toggles
626 LyXFont const & font = TEXT(false)->real_current_font;
629 flag.setOnOff(font.emph() == LyXFont::ON);
632 flag.setOnOff(font.noun() == LyXFont::ON);
635 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
638 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
641 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
644 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
650 string tc = mathcursor->getLastCode();
653 flag.setOnOff(tc == "mathbf");
656 flag.setOnOff(tc == "mathsf");
659 flag.setOnOff(tc == "mathcal");
662 flag.setOnOff(tc == "mathrm");
665 flag.setOnOff(tc == "mathtt");
668 flag.setOnOff(tc == "mathbb");
671 flag.setOnOff(tc == "mathnormal");
678 // this one is difficult to get right. As a half-baked
679 // solution, we consider only the first action of the sequence
680 if (ev.action == LFUN_SEQUENCE) {
681 // argument contains ';'-terminated commands
682 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
689 void LyXFunc::dispatch(string const & s, bool verbose)
691 int const action = lyxaction.LookupFunc(s);
693 if (action == LFUN_UNKNOWN_ACTION) {
694 string const msg = string(_("Unknown function ("))
700 dispatch(action, verbose);
704 void LyXFunc::dispatch(int ac, bool verbose)
707 kb_action const action = lyxaction.retrieveActionArg(ac, argument);
708 dispatch(FuncRequest(action, argument), verbose);
713 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
715 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
716 <<"] arg[" << ev.argument << "]" << endl;
718 // we have not done anything wrong yet.
720 dispatch_buffer.erase();
722 #ifdef NEW_DISPATCHER
723 // We try do call the most specific dispatcher first:
724 // 1. the lockinginset's dispatch
725 // 2. the bufferview's dispatch
726 // 3. the lyxview's dispatch
729 selection_possible = false;
731 if (owner->view()->available())
732 owner->view()->hideCursor();
734 string argument = ev.argument;
735 kb_action action = ev.action;
737 // We cannot use this function here
738 if (getStatus(ev).disabled()) {
739 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
740 << lyxaction.getActionName(action)
741 << " [" << action << "] is disabled at this location"
743 setErrorMessage(getStatusMessage());
744 goto exit_with_message;
747 if (owner->view()->available() && owner->view()->theLockingInset()) {
748 UpdatableInset::RESULT result;
749 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
750 (!keyseq.deleted())))
752 UpdatableInset * inset = owner->view()->theLockingInset();
756 inset->getCursorPos(owner->view().get(), inset_x, dummy_y);
758 if ((action == LFUN_UNKNOWN_ACTION)
759 && argument.empty()) {
760 argument = keyseq.getLastKeyEncoded();
762 // Undo/Redo is a bit tricky for insets.
763 if (action == LFUN_UNDO) {
764 owner->view()->menuUndo();
765 goto exit_with_message;
766 } else if (action == LFUN_REDO) {
767 owner->view()->menuRedo();
768 goto exit_with_message;
769 } else if (((result=inset->
770 // Hand-over to inset's own dispatch:
771 localDispatch(owner->view().get(), FuncRequest(action, argument))) ==
772 UpdatableInset::DISPATCHED) ||
773 (result == UpdatableInset::DISPATCHED_NOUPDATE))
774 goto exit_with_message;
775 // If UNDISPATCHED, just soldier on
776 else if (result == UpdatableInset::FINISHED) {
777 goto exit_with_message;
778 // We do not need special RTL handling here:
779 // FINISHED means that the cursor should be
780 // one position after the inset.
781 } else if (result == UpdatableInset::FINISHED_RIGHT) {
782 TEXT()->cursorRight(owner->view().get());
783 moveCursorUpdate(true, false);
784 owner->view_state_changed();
785 goto exit_with_message;
786 } else if (result == UpdatableInset::FINISHED_UP) {
787 if (TEXT()->cursor.irow()->previous()) {
789 TEXT()->setCursorFromCoordinates(
790 owner->view().get(), TEXT()->cursor.ix() + inset_x,
791 TEXT()->cursor.iy() -
792 TEXT()->cursor.irow()->baseline() - 1);
793 TEXT()->cursor.x_fix(TEXT()->cursor.x());
795 TEXT()->cursorUp(owner->view().get());
797 moveCursorUpdate(true, false);
798 owner->view_state_changed();
800 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
802 goto exit_with_message;
803 } else if (result == UpdatableInset::FINISHED_DOWN) {
804 if (TEXT()->cursor.irow()->next()) {
806 TEXT()->setCursorFromCoordinates(
807 owner->view().get(), TEXT()->cursor.ix() + inset_x,
808 TEXT()->cursor.iy() -
809 TEXT()->cursor.irow()->baseline() +
810 TEXT()->cursor.irow()->height() + 1);
811 TEXT()->cursor.x_fix(TEXT()->cursor.x());
813 TEXT()->cursorDown(owner->view().get());
816 TEXT()->cursorRight(owner->view().get());
818 moveCursorUpdate(true, false);
819 owner->view_state_changed();
820 goto exit_with_message;
822 #warning I am not sure this is still right, please have a look! (Jug 20020417)
823 else { // result == UNDISPATCHED
824 //setMessage(N_("Text mode"));
826 case LFUN_UNKNOWN_ACTION:
827 case LFUN_BREAKPARAGRAPH:
829 TEXT()->cursorRight(owner->view().get());
830 owner->view()->switchKeyMap();
831 owner->view_state_changed();
834 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
835 TEXT()->cursorRight(owner->view().get());
836 moveCursorUpdate(true, false);
837 owner->view_state_changed();
839 goto exit_with_message;
841 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
842 TEXT()->cursorRight(owner->view().get());
843 moveCursorUpdate(true, false);
844 owner->view_state_changed();
846 goto exit_with_message;
848 if (TEXT()->cursor.row()->next())
849 TEXT()->cursorDown(owner->view().get());
851 TEXT()->cursorRight(owner->view().get());
852 moveCursorUpdate(true, false);
853 owner->view_state_changed();
854 goto exit_with_message;
866 if (!owner->view()->available()) break;
867 // this function should be used always [asierra060396]
868 UpdatableInset * tli =
869 owner->view()->theLockingInset();
871 UpdatableInset * lock = tli->getLockingInset();
874 owner->view()->unlockInset(tli);
875 TEXT()->cursorRight(owner->view().get());
876 moveCursorUpdate(true, false);
877 owner->view_state_changed();
879 tli->unlockInsetInInset(owner->view().get(),
884 // Tell the paragraph dialog that we changed paragraph
885 owner->getDialogs()->updateParagraph();
890 // --- Misc -------------------------------------------
891 case LFUN_WORDFINDFORWARD :
892 case LFUN_WORDFINDBACKWARD : {
893 static string last_search;
894 string searched_string;
896 if (!argument.empty()) {
897 last_search = argument;
898 searched_string = argument;
900 searched_string = last_search;
902 bool fw = (action == LFUN_WORDFINDBACKWARD);
903 if (!searched_string.empty()) {
904 lyxfind::LyXFind(owner->view().get(), searched_string, fw);
906 // owner->view()->showCursor();
912 if (owner->view()->available() && !owner->view()->theLockingInset()) {
913 owner->view()->update(TEXT(),
914 BufferView::SELECT|BufferView::FITCUR);
916 owner->message(keyseq.printOptions());
920 // --- Misc -------------------------------------------
921 case LFUN_EXEC_COMMAND:
922 owner->focus_command_buffer();
925 case LFUN_CANCEL: // RVDK_PATCH_5
927 meta_fake_bit = key_modifier::none;
928 if (owner->view()->available())
929 // cancel any selection
930 dispatch(LFUN_MARK_OFF);
931 setMessage(N_("Cancel"));
934 case LFUN_META_FAKE: // RVDK_PATCH_5
936 meta_fake_bit = key_modifier::alt;
937 setMessage(keyseq.print());
941 case LFUN_READ_ONLY_TOGGLE:
942 if (owner->buffer()->lyxvc.inUse()) {
943 owner->buffer()->lyxvc.toggleReadOnly();
945 owner->buffer()->setReadonly(
946 !owner->buffer()->isReadonly());
950 case LFUN_CENTER: // this is center and redraw.
951 owner->view()->center();
954 // --- Menus -----------------------------------------------
956 menuNew(argument, false);
959 case LFUN_MENUNEWTMPLT:
960 menuNew(argument, true);
963 case LFUN_CLOSEBUFFER:
968 if (!owner->buffer()->isUnnamed()) {
970 s1 << _("Saving document") << ' '
971 << MakeDisplayPath(owner->buffer()->fileName() + "...");
972 owner->message(s1.str().c_str());
973 MenuWrite(owner->view().get(), owner->buffer());
975 owner->message(s1.str().c_str());
977 WriteAs(owner->view().get(), owner->buffer());
981 WriteAs(owner->view().get(), owner->buffer(), argument);
984 case LFUN_MENURELOAD:
989 Exporter::Export(owner->buffer(), argument, true);
993 Exporter::Preview(owner->buffer(), argument);
997 Exporter::Export(owner->buffer(), "program", true);
1000 case LFUN_RUNCHKTEX:
1001 MenuRunChktex(owner->buffer());
1004 case LFUN_MENUPRINT:
1005 owner->getDialogs()->showPrint();
1009 if (argument == "custom")
1010 owner->getDialogs()->showSendto();
1012 Exporter::Export(owner->buffer(), argument, false);
1030 InsetCommandParams p;
1033 if (action == LFUN_TOCVIEW)
1035 p.setCmdName("tableofcontents");
1037 else if (action == LFUN_LOAVIEW)
1038 p.setCmdName("listof{algorithm}{List of Algorithms}");
1039 else if (action == LFUN_LOFVIEW)
1040 p.setCmdName("listoffigures");
1042 p.setCmdName("listoftables");
1044 owner->getDialogs()->createTOC(p.getAsString());
1049 AutoSave(owner->view().get());
1053 owner->view()->menuUndo();
1057 owner->view()->menuRedo();
1060 case LFUN_MENUSEARCH:
1061 owner->getDialogs()->showSearch();
1064 case LFUN_REMOVEERRORS:
1065 if (owner->view()->removeAutoInsets()) {
1066 #warning repaint() or update() or nothing ?
1067 owner->view()->repaint();
1068 owner->view()->fitCursor();
1072 case LFUN_DEPTH_MIN:
1073 changeDepth(owner->view().get(), TEXT(false), -1);
1076 case LFUN_DEPTH_PLUS:
1077 changeDepth(owner->view().get(), TEXT(false), 1);
1081 owner->getDialogs()->setUserFreeFont();
1084 case LFUN_RECONFIGURE:
1085 Reconfigure(owner->view().get());
1089 case LFUN_FLOATSOPERATE:
1090 if (argument == "openfoot")
1091 owner->view()->allFloats(1,0);
1092 else if (argument == "closefoot")
1093 owner->view()->allFloats(0,0);
1094 else if (argument == "openfig")
1095 owner->view()->allFloats(1,1);
1096 else if (argument == "closefig")
1097 owner->view()->allFloats(0,1);
1100 #ifdef WITH_WARNINGS
1101 //#warning Find another implementation here (or another lyxfunc)!
1104 case LFUN_HELP_ABOUTLYX:
1105 owner->getDialogs()->showAboutlyx();
1108 case LFUN_HELP_TEXINFO:
1109 owner->getDialogs()->showTexinfo();
1112 case LFUN_HELP_OPEN:
1114 string const arg = argument;
1116 setErrorMessage(N_("Missing argument"));
1119 owner->prohibitInput();
1120 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1121 if (fname.empty()) {
1122 lyxerr << "LyX: unable to find documentation file `"
1123 << arg << "'. Bad installation?" << endl;
1124 owner->allowInput();
1128 str << _("Opening help file") << ' '
1129 << MakeDisplayPath(fname) << "...";
1130 owner->message(str.str().c_str());
1131 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1132 owner->allowInput();
1136 // --- version control -------------------------------
1137 case LFUN_VC_REGISTER:
1139 if (!owner->buffer()->lyxvc.inUse())
1140 owner->buffer()->lyxvc.registrer();
1144 case LFUN_VC_CHECKIN:
1146 if (owner->buffer()->lyxvc.inUse()
1147 && !owner->buffer()->isReadonly())
1148 owner->buffer()->lyxvc.checkIn();
1152 case LFUN_VC_CHECKOUT:
1154 if (owner->buffer()->lyxvc.inUse()
1155 && owner->buffer()->isReadonly())
1156 owner->buffer()->lyxvc.checkOut();
1160 case LFUN_VC_REVERT:
1162 owner->buffer()->lyxvc.revert();
1168 owner->buffer()->lyxvc.undoLast();
1172 case LFUN_VC_HISTORY:
1174 owner->getDialogs()->showVCLogFile();
1178 // --- buffers ----------------------------------------
1180 case LFUN_SWITCHBUFFER:
1181 owner->view()->buffer(bufferlist.getBuffer(argument));
1186 // servercmd: argument must be <file>:<template>
1187 Buffer * tmpbuf = NewFile(argument);
1189 owner->view()->buffer(tmpbuf);
1193 case LFUN_FILE_OPEN:
1197 case LFUN_LATEX_LOG:
1198 owner->getDialogs()->showLogFile();
1201 case LFUN_LAYOUT_DOCUMENT:
1202 owner->getDialogs()->showDocument();
1205 case LFUN_LAYOUT_PARAGRAPH:
1206 owner->getDialogs()->showParagraph();
1209 case LFUN_LAYOUT_CHARACTER:
1210 owner->getDialogs()->showCharacter();
1213 case LFUN_LAYOUT_TABULAR:
1214 if (owner->view()->theLockingInset()) {
1215 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1216 InsetTabular * inset = static_cast<InsetTabular *>
1217 (owner->view()->theLockingInset());
1218 inset->openLayoutDialog(owner->view().get());
1219 } else if (owner->view()->theLockingInset()->
1220 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1221 InsetTabular * inset = static_cast<InsetTabular *>(
1222 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1223 inset->openLayoutDialog(owner->view().get());
1228 case LFUN_LAYOUT_PREAMBLE:
1229 owner->getDialogs()->showPreamble();
1232 case LFUN_DROP_LAYOUTS_CHOICE:
1233 owner->getToolbar()->openLayoutList();
1236 case LFUN_MENU_OPEN_BY_NAME:
1237 owner->getMenubar()->openByName(argument);
1238 break; // RVDK_PATCH_5
1240 case LFUN_SPELLCHECK:
1241 owner->getDialogs()->showSpellchecker();
1244 // --- lyxserver commands ----------------------------
1248 setMessage(owner->buffer()->fileName());
1249 lyxerr[Debug::INFO] << "FNAME["
1250 << owner->buffer()->fileName()
1256 dispatch_buffer = keyseq.print();
1257 lyxserver->notifyClient(dispatch_buffer);
1261 case LFUN_GOTOFILEROW:
1265 istringstream istr(argument.c_str());
1266 istr >> file_name >> row;
1267 // Must replace extension of the file to be .lyx and get full path
1268 string const s(ChangeExtension(file_name, ".lyx"));
1270 // Either change buffer or load the file
1271 if (bufferlist.exists(s)) {
1272 owner->view()->buffer(bufferlist.getBuffer(s));
1274 owner->view()->buffer(bufferlist.loadLyXFile(s));
1277 owner->view()->setCursorFromRow(row);
1279 owner->view()->center();
1280 // see BufferView_pimpl::center()
1281 owner->view()->updateScrollbar();
1285 case LFUN_GOTO_PARAGRAPH:
1287 istringstream istr(argument.c_str());
1291 Paragraph * par = owner->buffer()->getParFromID(id);
1293 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1294 << id << "]" << endl;
1297 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1298 << " found." << endl;
1301 if (owner->view()->theLockingInset())
1302 owner->view()->unlockInset(owner->view()->theLockingInset());
1303 if (par->inInset()) {
1304 par->inInset()->edit(owner->view().get());
1307 owner->view()->getLyXText()->setCursor(owner->view().get(), par, 0);
1308 owner->view()->switchKeyMap();
1309 owner->view_state_changed();
1311 owner->view()->center();
1312 // see BufferView_pimpl::center()
1313 owner->view()->updateScrollbar();
1320 int const qa = lyxaction.LookupFunc(argument);
1321 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1325 // --- insert characters ----------------------------------------
1327 // --- Mathed stuff. If we are here, there is no locked inset yet.
1328 case LFUN_MATH_EXTERN:
1329 case LFUN_MATH_NUMBER:
1330 case LFUN_MATH_NONUMBER:
1331 case LFUN_MATH_LIMITS:
1333 setErrorMessage(N_("This is only allowed in math mode!"));
1337 // passthrough hat and underscore outside mathed:
1338 case LFUN_SUBSCRIPT:
1339 dispatch(FuncRequest(LFUN_SELFINSERT, "_"));
1341 case LFUN_SUPERSCRIPT:
1342 dispatch(FuncRequest(LFUN_SELFINSERT, "^"));
1345 case LFUN_MATH_PANEL:
1346 owner->getDialogs()->showMathPanel();
1349 case LFUN_CITATION_CREATE:
1351 InsetCommandParams p("cite");
1353 if (!argument.empty()) {
1354 // This should be set at source, ie when typing
1355 // "citation-insert foo" in the minibuffer.
1356 // Question: would pybibliographer also need to be
1357 // changed. Suspect so. Leave as-is therefore.
1358 if (contains(argument, "|")) {
1359 p.setContents(token(argument, '|', 0));
1360 p.setOptions( token(argument, '|', 1));
1362 p.setContents(argument);
1364 dispatch(FuncRequest(LFUN_CITATION_INSERT, p.getAsString()));
1366 owner->getDialogs()->createCitation(p.getAsString());
1370 case LFUN_CHILDOPEN:
1372 string const filename =
1373 MakeAbsPath(argument,
1374 owner->buffer()->filePath());
1375 setMessage(N_("Opening child document ") +
1376 MakeDisplayPath(filename) + "...");
1377 owner->view()->savePosition(0);
1378 if (bufferlist.exists(filename))
1379 owner->view()->buffer(bufferlist.getBuffer(filename));
1381 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1385 case LFUN_TOGGLECURSORFOLLOW:
1386 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1389 case LFUN_KMAP_OFF: // keymap off
1390 owner->getIntl()->KeyMapOn(false);
1393 case LFUN_KMAP_PRIM: // primary keymap
1394 owner->getIntl()->KeyMapPrim();
1397 case LFUN_KMAP_SEC: // secondary keymap
1398 owner->getIntl()->KeyMapSec();
1401 case LFUN_KMAP_TOGGLE: // toggle keymap
1402 owner->getIntl()->ToggleKeyMap();
1407 // argument contains ';'-terminated commands
1408 while (!argument.empty()) {
1410 argument = split(argument, first, ';');
1416 case LFUN_DIALOG_PREFERENCES:
1417 owner->getDialogs()->showPreferences();
1420 case LFUN_SAVEPREFERENCES:
1422 Path p(user_lyxdir);
1423 lyxrc.write("preferences");
1427 case LFUN_SCREEN_FONT_UPDATE:
1429 // handle the screen font changes.
1430 lyxrc.set_font_norm_type();
1431 lyx_gui::update_fonts();
1432 // We also need to empty the textcache so that
1433 // the buffer will be formatted correctly after
1436 // Of course we should only do the resize and the textcache.clear
1437 // if values really changed...but not very important right now. (Lgb)
1438 // All visible buffers will need resize
1439 owner->view()->resize();
1440 owner->view()->repaint();
1444 case LFUN_SET_COLOR:
1447 string const x11_name = split(argument, lyx_name, ' ');
1448 if (lyx_name.empty() || x11_name.empty()) {
1449 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1454 bool const graphicsbg_changed =
1455 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1456 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1458 if (!lcolor.setColor(lyx_name, x11_name)) {
1459 static string const err1 (N_("Set-color \""));
1460 static string const err2 (
1461 N_("\" failed - color is undefined "
1462 "or may not be redefined"));
1463 setErrorMessage(_(err1) + lyx_name + _(err2));
1467 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1469 if (graphicsbg_changed) {
1470 #ifdef WITH_WARNINGS
1471 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1474 grfx::GCache & gc = grfx::GCache::get();
1475 gc.changeDisplay(true);
1479 owner->view()->repaint();
1484 owner->message(argument);
1487 case LFUN_FORKS_SHOW:
1488 owner->getDialogs()->showForks();
1491 case LFUN_FORKS_KILL:
1493 if (!isStrInt(argument))
1496 pid_t const pid = strToInt(argument);
1497 ForkedcallsController & fcc = ForkedcallsController::get();
1502 case LFUN_TOOLTIPS_TOGGLE:
1503 owner->getDialogs()->toggleTooltips();
1507 // Then if it was none of the above
1508 // Trying the BufferView::pimpl dispatch:
1509 if (!owner->view()->dispatch(ev))
1510 lyxerr << "A truly unknown func ["
1511 << lyxaction.getActionName(ev.action) << "]!"
1517 sendDispatchMessage(getMessage(), ev, verbose);
1521 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1523 owner->updateMenubar();
1524 owner->updateToolbar();
1526 if (ev.action == LFUN_SELFINSERT || !verbose) {
1527 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1529 owner->message(msg);
1533 string dispatch_msg = msg;
1534 if (!dispatch_msg.empty())
1535 dispatch_msg += " ";
1537 string comname = lyxaction.getActionName(ev.action);
1539 int pseudoaction = ev.action;
1540 bool argsadded = false;
1542 if (!ev.argument.empty()) {
1543 // the pseudoaction is useful for the bindings
1544 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1546 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1547 pseudoaction = ev.action;
1549 comname += " " + ev.argument;
1554 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1556 if (!shortcuts.empty()) {
1557 comname += ": " + shortcuts;
1558 } else if (!argsadded && !ev.argument.empty()) {
1559 comname += " " + ev.argument;
1562 if (!comname.empty()) {
1563 comname = rtrim(comname);
1564 dispatch_msg += "(" + comname + ')';
1567 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1568 if (!dispatch_msg.empty())
1569 owner->message(dispatch_msg);
1573 void LyXFunc::setupLocalKeymap()
1575 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1576 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1580 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1582 string initpath = lyxrc.document_path;
1583 string filename(name);
1585 if (owner->view()->available()) {
1586 string const trypath = owner->buffer()->filePath();
1587 // If directory is writeable, use this as default.
1588 if (IsDirWriteable(trypath))
1592 static int newfile_number;
1594 if (filename.empty()) {
1595 filename = AddName(lyxrc.document_path,
1596 "newfile" + tostr(++newfile_number) + ".lyx");
1597 FileInfo fi(filename);
1598 while (bufferlist.exists(filename) || fi.readable()) {
1600 filename = AddName(lyxrc.document_path,
1601 "newfile" + tostr(newfile_number) +
1603 fi.newFile(filename);
1607 // The template stuff
1610 FileDialog fileDlg(owner, _("Select template file"),
1611 LFUN_SELECT_FILE_SYNC,
1612 make_pair(string(_("Documents|#o#O")),
1613 string(lyxrc.document_path)),
1614 make_pair(string(_("Templates|#T#t")),
1615 string(lyxrc.template_path)));
1617 FileDialog::Result result =
1618 fileDlg.Select(lyxrc.template_path,
1619 _("*.lyx|LyX Documents (*.lyx)"));
1621 if (result.first == FileDialog::Later)
1624 string const fname = result.second;
1631 owner->view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1635 void LyXFunc::open(string const & fname)
1637 string initpath = lyxrc.document_path;
1639 if (owner->view()->available()) {
1640 string const trypath = owner->buffer()->filePath();
1641 // If directory is writeable, use this as default.
1642 if (IsDirWriteable(trypath))
1648 if (fname.empty()) {
1649 FileDialog fileDlg(owner, _("Select document to open"),
1651 make_pair(string(_("Documents|#o#O")),
1652 string(lyxrc.document_path)),
1653 make_pair(string(_("Examples|#E#e")),
1654 string(AddPath(system_lyxdir, "examples"))));
1656 FileDialog::Result result =
1657 fileDlg.Select(initpath,
1658 "*.lyx|LyX Documents (*.lyx)");
1660 if (result.first == FileDialog::Later)
1663 filename = result.second;
1665 // check selected filename
1666 if (filename.empty()) {
1667 owner->message(_("Canceled."));
1673 // get absolute path of file and add ".lyx" to the filename if
1675 string const fullpath = FileSearch(string(), filename, "lyx");
1676 if (!fullpath.empty()) {
1677 filename = fullpath;
1680 string const disp_fn(MakeDisplayPath(filename));
1682 // if the file doesn't exist, let the user create one
1683 FileInfo const f(filename, true);
1685 if (!Alert::askQuestion(_("No such file"), disp_fn,
1686 _("Start a new document with this filename ?"))) {
1687 owner->message(_("Canceled"));
1690 // the user specifically chose this name. Believe them.
1691 Buffer * buffer = bufferlist.newFile(filename, "", true);
1692 owner->view()->buffer(buffer);
1697 str << _("Opening document") << ' ' << disp_fn << "...";
1699 owner->message(str.str().c_str());
1701 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1703 owner->view()->buffer(openbuf);
1705 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1706 owner->message(str.str().c_str());
1709 str << _("Could not open document") << ' ' << disp_fn;
1710 owner->message(str.str().c_str());
1715 void LyXFunc::doImport(string const & argument)
1718 string filename = split(argument, format, ' ');
1720 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1721 << " file: " << filename << endl;
1723 // need user interaction
1724 if (filename.empty()) {
1725 string initpath = lyxrc.document_path;
1727 if (owner->view()->available()) {
1728 string const trypath = owner->buffer()->filePath();
1729 // If directory is writeable, use this as default.
1730 if (IsDirWriteable(trypath))
1734 string const text = _("Select ") + formats.prettyName(format)
1735 + _(" file to import");
1737 FileDialog fileDlg(owner, text,
1739 make_pair(string(_("Documents|#o#O")),
1740 string(lyxrc.document_path)),
1741 make_pair(string(_("Examples|#E#e")),
1742 string(AddPath(system_lyxdir, "examples"))));
1744 string const extension = "*." + formats.extension(format)
1745 + "| " + formats.prettyName(format)
1746 + " (*." + formats.extension(format) + ")";
1748 FileDialog::Result result = fileDlg.Select(initpath,
1751 if (result.first == FileDialog::Later)
1754 filename = result.second;
1756 // check selected filename
1757 if (filename.empty())
1758 owner->message(_("Canceled."));
1761 if (filename.empty())
1764 // get absolute path of file
1765 filename = MakeAbsPath(filename);
1767 string const lyxfile = ChangeExtension(filename, ".lyx");
1769 // Check if the document already is open
1770 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1771 switch (Alert::askConfirmation(_("Document is already open:"),
1772 MakeDisplayPath(lyxfile, 50),
1773 _("Do you want to close that document now?\n"
1774 "('No' will just switch to the open version)")))
1777 // If close is canceled, we cancel here too.
1778 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1782 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1785 owner->message(_("Canceled."));
1790 // if the file exists already, and we didn't do
1791 // -i lyx thefile.lyx, warn
1792 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1793 if (!Alert::askQuestion(_("A document by the name"),
1794 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1795 owner->message(_("Canceled"));
1800 Importer::Import(owner, filename, format);
1804 void LyXFunc::reloadBuffer()
1806 string const fn = owner->buffer()->fileName();
1807 if (bufferlist.close(owner->buffer()))
1808 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1812 void LyXFunc::closeBuffer()
1814 if (bufferlist.close(owner->buffer()) && !quitting) {
1815 if (bufferlist.empty()) {
1816 // need this otherwise SEGV may occur while trying to
1817 // set variables that don't exist
1818 // since there's no current buffer
1819 owner->getDialogs()->hideBufferDependent();
1821 owner->view()->buffer(bufferlist.first());
1827 // Each "owner" should have it's own message method. lyxview and
1828 // the minibuffer would use the minibuffer, but lyxserver would
1829 // send an ERROR signal to its client. Alejandro 970603
1830 // This func is bit problematic when it comes to NLS, to make the
1831 // lyx servers client be language indepenent we must not translate
1832 // strings sent to this func.
1833 void LyXFunc::setErrorMessage(string const & m) const
1835 dispatch_buffer = m;
1840 void LyXFunc::setMessage(string const & m) const
1842 dispatch_buffer = m;
1846 void LyXFunc::setStatusMessage(string const & m) const
1852 string const LyXFunc::view_status_message()
1854 // When meta-fake key is pressed, show the key sequence so far + "M-".
1856 return keyseq.print() + "M-";
1859 // Else, when a non-complete key sequence is pressed,
1860 // show the available options.
1861 if (keyseq.length() > 0 && !keyseq.deleted()) {
1862 return keyseq.printOptions();
1865 if (!owner->view()->available())
1866 return _("Welcome to LyX!");
1868 return currentState(owner->view().get());