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"
93 #include <boost/tuple/tuple.hpp>
104 using std::make_pair;
108 using std::transform;
109 using std::back_inserter;
111 extern BufferList bufferlist;
112 extern LyXServer * lyxserver;
113 extern bool selection_possible;
115 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
118 extern tex_accent_struct get_accent(kb_action action);
120 extern void ShowLatexLog();
123 /* === globals =========================================================== */
126 LyXFunc::LyXFunc(LyXView * o)
128 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
129 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get())
131 meta_fake_bit = key_modifier::none;
132 lyx_dead_action = LFUN_NOACTION;
133 lyx_calling_dead_action = LFUN_NOACTION;
138 LyXText * LyXFunc::TEXT(bool flag = true) const
141 return owner->view()->text;
142 return owner->view()->getLyXText();
147 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
149 if (selecting || TEXT(flag)->selection.mark()) {
150 TEXT(flag)->setSelection(owner->view().get());
151 if (TEXT(flag)->bv_owner)
152 owner->view()->toggleToggle();
154 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
155 owner->view()->showCursor();
157 owner->view()->switchKeyMap();
161 void LyXFunc::handleKeyFunc(kb_action action)
163 char c = keyseq.getLastKeyEncoded();
165 if (keyseq.length() > 1) {
169 owner->getIntl()->getTransManager()
170 .deadkey(c, get_accent(action).accent, TEXT(false));
171 // Need to clear, in case the minibuffer calls these
174 // copied verbatim from do_accent_char
175 owner->view()->update(TEXT(false),
176 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
177 TEXT(false)->selection.cursor = TEXT(false)->cursor;
181 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
182 key_modifier::state state)
186 if (lyxerr.debugging(Debug::KEY)) {
187 lyxerr << "KeySym is "
188 << keysym->getSymbolName()
191 // Do nothing if we have nothing (JMarc)
192 if (!keysym->isOK()) {
193 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
198 if (keysym->isModifier()) {
202 // Do a one-deep top-level lookup for
203 // cancel and meta-fake keys. RVDK_PATCH_5
204 cancel_meta_seq.reset();
206 int action = cancel_meta_seq.addkey(keysym, state);
207 if (lyxerr.debugging(Debug::KEY)) {
208 lyxerr << "action first set to [" << action << "]" << endl;
211 // When not cancel or meta-fake, do the normal lookup.
212 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
213 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
214 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
215 // remove Caps Lock and Mod2 as a modifiers
216 action = keyseq.addkey(keysym, (state | meta_fake_bit));
217 lyxerr[Debug::KEY] << "action now set to ["
218 << action << "]" << endl;
221 // Dont remove this unless you know what you are doing.
222 meta_fake_bit = key_modifier::none;
224 // can this happen now ?
225 if (action == LFUN_NOACTION) {
226 action = LFUN_PREFIX;
229 if (lyxerr.debugging(Debug::KEY)) {
230 lyxerr << "Key [action="
232 << keyseq.print() << "]"
236 // already here we know if it any point in going further
237 // why not return already here if action == -1 and
238 // num_bytes == 0? (Lgb)
240 if (keyseq.length() > 1) {
241 owner->message(keyseq.print());
244 if (action == LFUN_UNKNOWN_ACTION) {
245 // It is unknown, but what if we remove all
246 // the modifiers? (Lgb)
247 action = keyseq.addkey(keysym, key_modifier::none);
249 lyxerr[Debug::KEY] << "Removing modifiers...\n"
250 << "Action now set to ["
251 << action << "]" << endl;
253 if (action == LFUN_UNKNOWN_ACTION) {
254 owner->message(_("Unknown function."));
259 if (action == LFUN_SELFINSERT) {
260 char c = keysym->getISOEncoded();
266 dispatch(FuncRequest(LFUN_SELFINSERT, argument));
267 lyxerr[Debug::KEY] << "SelfInsert arg[`"
268 << argument << "']" << endl;
275 FuncStatus LyXFunc::getStatus(int ac) const
279 boost::tie(action, arg) = lyxaction.retrieveActionArg(ac);
280 return getStatus(FuncRequest(action, arg));
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)
708 boost::tie(action, arg) = lyxaction.retrieveActionArg(ac);
709 dispatch(FuncRequest(action, arg), verbose);
714 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
716 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
717 <<"] arg[" << ev.argument << "]" << endl;
719 // we have not done anything wrong yet.
721 dispatch_buffer.erase();
723 #ifdef NEW_DISPATCHER
724 // We try do call the most specific dispatcher first:
725 // 1. the lockinginset's dispatch
726 // 2. the bufferview's dispatch
727 // 3. the lyxview's dispatch
730 selection_possible = false;
732 if (owner->view()->available())
733 owner->view()->hideCursor();
735 string argument = ev.argument;
736 kb_action action = ev.action;
738 // We cannot use this function here
739 if (getStatus(ev).disabled()) {
740 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
741 << lyxaction.getActionName(action)
742 << " [" << action << "] is disabled at this location"
744 setErrorMessage(getStatusMessage());
745 goto exit_with_message;
748 if (owner->view()->available() && owner->view()->theLockingInset()) {
749 UpdatableInset::RESULT result;
750 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
751 (!keyseq.deleted())))
753 UpdatableInset * inset = owner->view()->theLockingInset();
757 inset->getCursorPos(owner->view().get(), inset_x, dummy_y);
759 if ((action == LFUN_UNKNOWN_ACTION)
760 && argument.empty()) {
761 argument = keyseq.getLastKeyEncoded();
763 // Undo/Redo is a bit tricky for insets.
764 if (action == LFUN_UNDO) {
765 owner->view()->menuUndo();
766 goto exit_with_message;
767 } else if (action == LFUN_REDO) {
768 owner->view()->menuRedo();
769 goto exit_with_message;
770 } else if (((result=inset->
771 // Hand-over to inset's own dispatch:
772 localDispatch(owner->view().get(), FuncRequest(action, argument))) ==
773 UpdatableInset::DISPATCHED) ||
774 (result == UpdatableInset::DISPATCHED_NOUPDATE))
775 goto exit_with_message;
776 // If UNDISPATCHED, just soldier on
777 else if (result == UpdatableInset::FINISHED) {
778 goto exit_with_message;
779 // We do not need special RTL handling here:
780 // FINISHED means that the cursor should be
781 // one position after the inset.
782 } else if (result == UpdatableInset::FINISHED_RIGHT) {
783 TEXT()->cursorRight(owner->view().get());
784 moveCursorUpdate(true, false);
785 owner->view_state_changed();
786 goto exit_with_message;
787 } else if (result == UpdatableInset::FINISHED_UP) {
788 if (TEXT()->cursor.irow()->previous()) {
790 TEXT()->setCursorFromCoordinates(
791 owner->view().get(), TEXT()->cursor.ix() + inset_x,
792 TEXT()->cursor.iy() -
793 TEXT()->cursor.irow()->baseline() - 1);
794 TEXT()->cursor.x_fix(TEXT()->cursor.x());
796 TEXT()->cursorUp(owner->view().get());
798 moveCursorUpdate(true, false);
799 owner->view_state_changed();
801 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
803 goto exit_with_message;
804 } else if (result == UpdatableInset::FINISHED_DOWN) {
805 if (TEXT()->cursor.irow()->next()) {
807 TEXT()->setCursorFromCoordinates(
808 owner->view().get(), TEXT()->cursor.ix() + inset_x,
809 TEXT()->cursor.iy() -
810 TEXT()->cursor.irow()->baseline() +
811 TEXT()->cursor.irow()->height() + 1);
812 TEXT()->cursor.x_fix(TEXT()->cursor.x());
814 TEXT()->cursorDown(owner->view().get());
817 TEXT()->cursorRight(owner->view().get());
819 moveCursorUpdate(true, false);
820 owner->view_state_changed();
821 goto exit_with_message;
823 #warning I am not sure this is still right, please have a look! (Jug 20020417)
824 else { // result == UNDISPATCHED
825 //setMessage(N_("Text mode"));
827 case LFUN_UNKNOWN_ACTION:
828 case LFUN_BREAKPARAGRAPH:
830 TEXT()->cursorRight(owner->view().get());
831 owner->view()->switchKeyMap();
832 owner->view_state_changed();
835 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
836 TEXT()->cursorRight(owner->view().get());
837 moveCursorUpdate(true, false);
838 owner->view_state_changed();
840 goto exit_with_message;
842 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
843 TEXT()->cursorRight(owner->view().get());
844 moveCursorUpdate(true, false);
845 owner->view_state_changed();
847 goto exit_with_message;
849 if (TEXT()->cursor.row()->next())
850 TEXT()->cursorDown(owner->view().get());
852 TEXT()->cursorRight(owner->view().get());
853 moveCursorUpdate(true, false);
854 owner->view_state_changed();
855 goto exit_with_message;
867 if (!owner->view()->available()) break;
868 // this function should be used always [asierra060396]
869 UpdatableInset * tli =
870 owner->view()->theLockingInset();
872 UpdatableInset * lock = tli->getLockingInset();
875 owner->view()->unlockInset(tli);
876 TEXT()->cursorRight(owner->view().get());
877 moveCursorUpdate(true, false);
878 owner->view_state_changed();
880 tli->unlockInsetInInset(owner->view().get(),
885 // Tell the paragraph dialog that we changed paragraph
886 owner->getDialogs()->updateParagraph();
891 // --- Misc -------------------------------------------
892 case LFUN_WORDFINDFORWARD :
893 case LFUN_WORDFINDBACKWARD : {
894 static string last_search;
895 string searched_string;
897 if (!argument.empty()) {
898 last_search = argument;
899 searched_string = argument;
901 searched_string = last_search;
903 bool fw = (action == LFUN_WORDFINDBACKWARD);
904 if (!searched_string.empty()) {
905 lyxfind::LyXFind(owner->view().get(), searched_string, fw);
907 // owner->view()->showCursor();
913 if (owner->view()->available() && !owner->view()->theLockingInset()) {
914 owner->view()->update(TEXT(),
915 BufferView::SELECT|BufferView::FITCUR);
917 owner->message(keyseq.printOptions());
921 // --- Misc -------------------------------------------
922 case LFUN_EXEC_COMMAND:
923 owner->focus_command_buffer();
926 case LFUN_CANCEL: // RVDK_PATCH_5
928 meta_fake_bit = key_modifier::none;
929 if (owner->view()->available())
930 // cancel any selection
931 dispatch(LFUN_MARK_OFF);
932 setMessage(N_("Cancel"));
935 case LFUN_META_FAKE: // RVDK_PATCH_5
937 meta_fake_bit = key_modifier::alt;
938 setMessage(keyseq.print());
942 case LFUN_READ_ONLY_TOGGLE:
943 if (owner->buffer()->lyxvc.inUse()) {
944 owner->buffer()->lyxvc.toggleReadOnly();
946 owner->buffer()->setReadonly(
947 !owner->buffer()->isReadonly());
951 case LFUN_CENTER: // this is center and redraw.
952 owner->view()->center();
955 // --- Menus -----------------------------------------------
957 menuNew(argument, false);
960 case LFUN_MENUNEWTMPLT:
961 menuNew(argument, true);
964 case LFUN_CLOSEBUFFER:
969 if (!owner->buffer()->isUnnamed()) {
971 s1 << _("Saving document") << ' '
972 << MakeDisplayPath(owner->buffer()->fileName() + "...");
973 owner->message(s1.str().c_str());
974 MenuWrite(owner->view().get(), owner->buffer());
976 owner->message(s1.str().c_str());
978 WriteAs(owner->view().get(), owner->buffer());
982 WriteAs(owner->view().get(), owner->buffer(), argument);
985 case LFUN_MENURELOAD:
990 Exporter::Export(owner->buffer(), argument, true);
994 Exporter::Preview(owner->buffer(), argument);
998 Exporter::Export(owner->buffer(), "program", true);
1001 case LFUN_RUNCHKTEX:
1002 MenuRunChktex(owner->buffer());
1005 case LFUN_MENUPRINT:
1006 owner->getDialogs()->showPrint();
1010 if (argument == "custom")
1011 owner->getDialogs()->showSendto();
1013 Exporter::Export(owner->buffer(), argument, false);
1031 InsetCommandParams p;
1034 if (action == LFUN_TOCVIEW)
1036 p.setCmdName("tableofcontents");
1038 else if (action == LFUN_LOAVIEW)
1039 p.setCmdName("listof{algorithm}{List of Algorithms}");
1040 else if (action == LFUN_LOFVIEW)
1041 p.setCmdName("listoffigures");
1043 p.setCmdName("listoftables");
1045 owner->getDialogs()->createTOC(p.getAsString());
1050 AutoSave(owner->view().get());
1054 owner->view()->menuUndo();
1058 owner->view()->menuRedo();
1061 case LFUN_MENUSEARCH:
1062 owner->getDialogs()->showSearch();
1065 case LFUN_REMOVEERRORS:
1066 if (owner->view()->removeAutoInsets()) {
1067 #warning repaint() or update() or nothing ?
1068 owner->view()->repaint();
1069 owner->view()->fitCursor();
1073 case LFUN_DEPTH_MIN:
1074 changeDepth(owner->view().get(), TEXT(false), -1);
1077 case LFUN_DEPTH_PLUS:
1078 changeDepth(owner->view().get(), TEXT(false), 1);
1082 owner->getDialogs()->setUserFreeFont();
1085 case LFUN_RECONFIGURE:
1086 Reconfigure(owner->view().get());
1090 case LFUN_FLOATSOPERATE:
1091 if (argument == "openfoot")
1092 owner->view()->allFloats(1,0);
1093 else if (argument == "closefoot")
1094 owner->view()->allFloats(0,0);
1095 else if (argument == "openfig")
1096 owner->view()->allFloats(1,1);
1097 else if (argument == "closefig")
1098 owner->view()->allFloats(0,1);
1101 #ifdef WITH_WARNINGS
1102 //#warning Find another implementation here (or another lyxfunc)!
1105 case LFUN_HELP_ABOUTLYX:
1106 owner->getDialogs()->showAboutlyx();
1109 case LFUN_HELP_TEXINFO:
1110 owner->getDialogs()->showTexinfo();
1113 case LFUN_HELP_OPEN:
1115 string const arg = argument;
1117 setErrorMessage(N_("Missing argument"));
1120 owner->prohibitInput();
1121 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1122 if (fname.empty()) {
1123 lyxerr << "LyX: unable to find documentation file `"
1124 << arg << "'. Bad installation?" << endl;
1125 owner->allowInput();
1129 str << _("Opening help file") << ' '
1130 << MakeDisplayPath(fname) << "...";
1131 owner->message(str.str().c_str());
1132 owner->view()->buffer(bufferlist.loadLyXFile(fname, false));
1133 owner->allowInput();
1137 // --- version control -------------------------------
1138 case LFUN_VC_REGISTER:
1140 if (!owner->buffer()->lyxvc.inUse())
1141 owner->buffer()->lyxvc.registrer();
1145 case LFUN_VC_CHECKIN:
1147 if (owner->buffer()->lyxvc.inUse()
1148 && !owner->buffer()->isReadonly())
1149 owner->buffer()->lyxvc.checkIn();
1153 case LFUN_VC_CHECKOUT:
1155 if (owner->buffer()->lyxvc.inUse()
1156 && owner->buffer()->isReadonly())
1157 owner->buffer()->lyxvc.checkOut();
1161 case LFUN_VC_REVERT:
1163 owner->buffer()->lyxvc.revert();
1169 owner->buffer()->lyxvc.undoLast();
1173 case LFUN_VC_HISTORY:
1175 owner->getDialogs()->showVCLogFile();
1179 // --- buffers ----------------------------------------
1181 case LFUN_SWITCHBUFFER:
1182 owner->view()->buffer(bufferlist.getBuffer(argument));
1187 // servercmd: argument must be <file>:<template>
1188 Buffer * tmpbuf = NewFile(argument);
1190 owner->view()->buffer(tmpbuf);
1194 case LFUN_FILE_OPEN:
1198 case LFUN_LATEX_LOG:
1199 owner->getDialogs()->showLogFile();
1202 case LFUN_LAYOUT_DOCUMENT:
1203 owner->getDialogs()->showDocument();
1206 case LFUN_LAYOUT_PARAGRAPH:
1207 owner->getDialogs()->showParagraph();
1210 case LFUN_LAYOUT_CHARACTER:
1211 owner->getDialogs()->showCharacter();
1214 case LFUN_LAYOUT_TABULAR:
1215 if (owner->view()->theLockingInset()) {
1216 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1217 InsetTabular * inset = static_cast<InsetTabular *>
1218 (owner->view()->theLockingInset());
1219 inset->openLayoutDialog(owner->view().get());
1220 } else if (owner->view()->theLockingInset()->
1221 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1222 InsetTabular * inset = static_cast<InsetTabular *>(
1223 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1224 inset->openLayoutDialog(owner->view().get());
1229 case LFUN_LAYOUT_PREAMBLE:
1230 owner->getDialogs()->showPreamble();
1233 case LFUN_DROP_LAYOUTS_CHOICE:
1234 owner->getToolbar()->openLayoutList();
1237 case LFUN_MENU_OPEN_BY_NAME:
1238 owner->getMenubar()->openByName(argument);
1239 break; // RVDK_PATCH_5
1241 case LFUN_SPELLCHECK:
1242 owner->getDialogs()->showSpellchecker();
1245 // --- lyxserver commands ----------------------------
1249 setMessage(owner->buffer()->fileName());
1250 lyxerr[Debug::INFO] << "FNAME["
1251 << owner->buffer()->fileName()
1257 dispatch_buffer = keyseq.print();
1258 lyxserver->notifyClient(dispatch_buffer);
1262 case LFUN_GOTOFILEROW:
1266 istringstream istr(argument.c_str());
1267 istr >> file_name >> row;
1268 // Must replace extension of the file to be .lyx and get full path
1269 string const s(ChangeExtension(file_name, ".lyx"));
1271 // Either change buffer or load the file
1272 if (bufferlist.exists(s)) {
1273 owner->view()->buffer(bufferlist.getBuffer(s));
1275 owner->view()->buffer(bufferlist.loadLyXFile(s));
1278 owner->view()->setCursorFromRow(row);
1280 owner->view()->center();
1281 // see BufferView_pimpl::center()
1282 owner->view()->updateScrollbar();
1286 case LFUN_GOTO_PARAGRAPH:
1288 istringstream istr(argument.c_str());
1292 Paragraph * par = owner->buffer()->getParFromID(id);
1294 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1295 << id << "]" << endl;
1298 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1299 << " found." << endl;
1302 if (owner->view()->theLockingInset())
1303 owner->view()->unlockInset(owner->view()->theLockingInset());
1304 if (par->inInset()) {
1305 par->inInset()->edit(owner->view().get());
1308 owner->view()->getLyXText()->setCursor(owner->view().get(), par, 0);
1309 owner->view()->switchKeyMap();
1310 owner->view_state_changed();
1312 owner->view()->center();
1313 // see BufferView_pimpl::center()
1314 owner->view()->updateScrollbar();
1321 int const qa = lyxaction.LookupFunc(argument);
1322 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1326 // --- insert characters ----------------------------------------
1328 // --- Mathed stuff. If we are here, there is no locked inset yet.
1329 case LFUN_MATH_EXTERN:
1330 case LFUN_MATH_NUMBER:
1331 case LFUN_MATH_NONUMBER:
1332 case LFUN_MATH_LIMITS:
1334 setErrorMessage(N_("This is only allowed in math mode!"));
1338 // passthrough hat and underscore outside mathed:
1339 case LFUN_SUBSCRIPT:
1340 dispatch(FuncRequest(LFUN_SELFINSERT, "_"));
1342 case LFUN_SUPERSCRIPT:
1343 dispatch(FuncRequest(LFUN_SELFINSERT, "^"));
1346 case LFUN_MATH_PANEL:
1347 owner->getDialogs()->showMathPanel();
1350 case LFUN_CITATION_CREATE:
1352 InsetCommandParams p("cite");
1354 if (!argument.empty()) {
1355 // This should be set at source, ie when typing
1356 // "citation-insert foo" in the minibuffer.
1357 // Question: would pybibliographer also need to be
1358 // changed. Suspect so. Leave as-is therefore.
1359 if (contains(argument, "|")) {
1360 p.setContents(token(argument, '|', 0));
1361 p.setOptions( token(argument, '|', 1));
1363 p.setContents(argument);
1365 dispatch(FuncRequest(LFUN_CITATION_INSERT, p.getAsString()));
1367 owner->getDialogs()->createCitation(p.getAsString());
1371 case LFUN_CHILDOPEN:
1373 string const filename =
1374 MakeAbsPath(argument,
1375 owner->buffer()->filePath());
1376 setMessage(N_("Opening child document ") +
1377 MakeDisplayPath(filename) + "...");
1378 owner->view()->savePosition(0);
1379 if (bufferlist.exists(filename))
1380 owner->view()->buffer(bufferlist.getBuffer(filename));
1382 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1386 case LFUN_TOGGLECURSORFOLLOW:
1387 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1390 case LFUN_KMAP_OFF: // keymap off
1391 owner->getIntl()->KeyMapOn(false);
1394 case LFUN_KMAP_PRIM: // primary keymap
1395 owner->getIntl()->KeyMapPrim();
1398 case LFUN_KMAP_SEC: // secondary keymap
1399 owner->getIntl()->KeyMapSec();
1402 case LFUN_KMAP_TOGGLE: // toggle keymap
1403 owner->getIntl()->ToggleKeyMap();
1408 // argument contains ';'-terminated commands
1409 while (!argument.empty()) {
1411 argument = split(argument, first, ';');
1417 case LFUN_DIALOG_PREFERENCES:
1418 owner->getDialogs()->showPreferences();
1421 case LFUN_SAVEPREFERENCES:
1423 Path p(user_lyxdir);
1424 lyxrc.write("preferences");
1428 case LFUN_SCREEN_FONT_UPDATE:
1430 // handle the screen font changes.
1431 lyxrc.set_font_norm_type();
1432 lyx_gui::update_fonts();
1433 // We also need to empty the textcache so that
1434 // the buffer will be formatted correctly after
1437 // Of course we should only do the resize and the textcache.clear
1438 // if values really changed...but not very important right now. (Lgb)
1439 // All visible buffers will need resize
1440 owner->view()->resize();
1441 owner->view()->repaint();
1445 case LFUN_SET_COLOR:
1448 string const x11_name = split(argument, lyx_name, ' ');
1449 if (lyx_name.empty() || x11_name.empty()) {
1450 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1455 bool const graphicsbg_changed =
1456 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1457 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1459 if (!lcolor.setColor(lyx_name, x11_name)) {
1460 static string const err1 (N_("Set-color \""));
1461 static string const err2 (
1462 N_("\" failed - color is undefined "
1463 "or may not be redefined"));
1464 setErrorMessage(_(err1) + lyx_name + _(err2));
1468 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1470 if (graphicsbg_changed) {
1471 #ifdef WITH_WARNINGS
1472 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1475 grfx::GCache & gc = grfx::GCache::get();
1476 gc.changeDisplay(true);
1480 owner->view()->repaint();
1485 owner->message(argument);
1488 case LFUN_FORKS_SHOW:
1489 owner->getDialogs()->showForks();
1492 case LFUN_FORKS_KILL:
1494 if (!isStrInt(argument))
1497 pid_t const pid = strToInt(argument);
1498 ForkedcallsController & fcc = ForkedcallsController::get();
1503 case LFUN_TOOLTIPS_TOGGLE:
1504 owner->getDialogs()->toggleTooltips();
1508 // Then if it was none of the above
1509 // Trying the BufferView::pimpl dispatch:
1510 if (!owner->view()->dispatch(ev))
1511 lyxerr << "A truly unknown func ["
1512 << lyxaction.getActionName(ev.action) << "]!"
1518 sendDispatchMessage(getMessage(), ev, verbose);
1522 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1524 owner->updateMenubar();
1525 owner->updateToolbar();
1527 if (ev.action == LFUN_SELFINSERT || !verbose) {
1528 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1530 owner->message(msg);
1534 string dispatch_msg = msg;
1535 if (!dispatch_msg.empty())
1536 dispatch_msg += " ";
1538 string comname = lyxaction.getActionName(ev.action);
1540 int pseudoaction = ev.action;
1541 bool argsadded = false;
1543 if (!ev.argument.empty()) {
1544 // the pseudoaction is useful for the bindings
1545 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1547 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1548 pseudoaction = ev.action;
1550 comname += " " + ev.argument;
1555 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1557 if (!shortcuts.empty()) {
1558 comname += ": " + shortcuts;
1559 } else if (!argsadded && !ev.argument.empty()) {
1560 comname += " " + ev.argument;
1563 if (!comname.empty()) {
1564 comname = rtrim(comname);
1565 dispatch_msg += "(" + comname + ')';
1568 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1569 if (!dispatch_msg.empty())
1570 owner->message(dispatch_msg);
1574 void LyXFunc::setupLocalKeymap()
1576 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1577 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1581 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1583 string initpath = lyxrc.document_path;
1584 string filename(name);
1586 if (owner->view()->available()) {
1587 string const trypath = owner->buffer()->filePath();
1588 // If directory is writeable, use this as default.
1589 if (IsDirWriteable(trypath))
1593 static int newfile_number;
1595 if (filename.empty()) {
1596 filename = AddName(lyxrc.document_path,
1597 "newfile" + tostr(++newfile_number) + ".lyx");
1598 FileInfo fi(filename);
1599 while (bufferlist.exists(filename) || fi.readable()) {
1601 filename = AddName(lyxrc.document_path,
1602 "newfile" + tostr(newfile_number) +
1604 fi.newFile(filename);
1608 // The template stuff
1611 FileDialog fileDlg(owner, _("Select template file"),
1612 LFUN_SELECT_FILE_SYNC,
1613 make_pair(string(_("Documents|#o#O")),
1614 string(lyxrc.document_path)),
1615 make_pair(string(_("Templates|#T#t")),
1616 string(lyxrc.template_path)));
1618 FileDialog::Result result =
1619 fileDlg.Select(lyxrc.template_path,
1620 _("*.lyx|LyX Documents (*.lyx)"));
1622 if (result.first == FileDialog::Later)
1625 string const fname = result.second;
1632 owner->view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1636 void LyXFunc::open(string const & fname)
1638 string initpath = lyxrc.document_path;
1640 if (owner->view()->available()) {
1641 string const trypath = owner->buffer()->filePath();
1642 // If directory is writeable, use this as default.
1643 if (IsDirWriteable(trypath))
1649 if (fname.empty()) {
1650 FileDialog fileDlg(owner, _("Select document to open"),
1652 make_pair(string(_("Documents|#o#O")),
1653 string(lyxrc.document_path)),
1654 make_pair(string(_("Examples|#E#e")),
1655 string(AddPath(system_lyxdir, "examples"))));
1657 FileDialog::Result result =
1658 fileDlg.Select(initpath,
1659 "*.lyx|LyX Documents (*.lyx)");
1661 if (result.first == FileDialog::Later)
1664 filename = result.second;
1666 // check selected filename
1667 if (filename.empty()) {
1668 owner->message(_("Canceled."));
1674 // get absolute path of file and add ".lyx" to the filename if
1676 string const fullpath = FileSearch(string(), filename, "lyx");
1677 if (!fullpath.empty()) {
1678 filename = fullpath;
1681 string const disp_fn(MakeDisplayPath(filename));
1683 // if the file doesn't exist, let the user create one
1684 FileInfo const f(filename, true);
1686 if (!Alert::askQuestion(_("No such file"), disp_fn,
1687 _("Start a new document with this filename ?"))) {
1688 owner->message(_("Canceled"));
1691 // the user specifically chose this name. Believe them.
1692 Buffer * buffer = bufferlist.newFile(filename, "", true);
1693 owner->view()->buffer(buffer);
1698 str << _("Opening document") << ' ' << disp_fn << "...";
1700 owner->message(str.str().c_str());
1702 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1704 owner->view()->buffer(openbuf);
1706 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1707 owner->message(str.str().c_str());
1710 str << _("Could not open document") << ' ' << disp_fn;
1711 owner->message(str.str().c_str());
1716 void LyXFunc::doImport(string const & argument)
1719 string filename = split(argument, format, ' ');
1721 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1722 << " file: " << filename << endl;
1724 // need user interaction
1725 if (filename.empty()) {
1726 string initpath = lyxrc.document_path;
1728 if (owner->view()->available()) {
1729 string const trypath = owner->buffer()->filePath();
1730 // If directory is writeable, use this as default.
1731 if (IsDirWriteable(trypath))
1735 string const text = _("Select ") + formats.prettyName(format)
1736 + _(" file to import");
1738 FileDialog fileDlg(owner, text,
1740 make_pair(string(_("Documents|#o#O")),
1741 string(lyxrc.document_path)),
1742 make_pair(string(_("Examples|#E#e")),
1743 string(AddPath(system_lyxdir, "examples"))));
1745 string const extension = "*." + formats.extension(format)
1746 + "| " + formats.prettyName(format)
1747 + " (*." + formats.extension(format) + ")";
1749 FileDialog::Result result = fileDlg.Select(initpath,
1752 if (result.first == FileDialog::Later)
1755 filename = result.second;
1757 // check selected filename
1758 if (filename.empty())
1759 owner->message(_("Canceled."));
1762 if (filename.empty())
1765 // get absolute path of file
1766 filename = MakeAbsPath(filename);
1768 string const lyxfile = ChangeExtension(filename, ".lyx");
1770 // Check if the document already is open
1771 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1772 switch (Alert::askConfirmation(_("Document is already open:"),
1773 MakeDisplayPath(lyxfile, 50),
1774 _("Do you want to close that document now?\n"
1775 "('No' will just switch to the open version)")))
1778 // If close is canceled, we cancel here too.
1779 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1783 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1786 owner->message(_("Canceled."));
1791 // if the file exists already, and we didn't do
1792 // -i lyx thefile.lyx, warn
1793 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1794 if (!Alert::askQuestion(_("A document by the name"),
1795 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1796 owner->message(_("Canceled"));
1801 Importer::Import(owner, filename, format);
1805 void LyXFunc::reloadBuffer()
1807 string const fn = owner->buffer()->fileName();
1808 if (bufferlist.close(owner->buffer()))
1809 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1813 void LyXFunc::closeBuffer()
1815 if (bufferlist.close(owner->buffer()) && !quitting) {
1816 if (bufferlist.empty()) {
1817 // need this otherwise SEGV may occur while trying to
1818 // set variables that don't exist
1819 // since there's no current buffer
1820 owner->getDialogs()->hideBufferDependent();
1822 owner->view()->buffer(bufferlist.first());
1828 // Each "owner" should have it's own message method. lyxview and
1829 // the minibuffer would use the minibuffer, but lyxserver would
1830 // send an ERROR signal to its client. Alejandro 970603
1831 // This func is bit problematic when it comes to NLS, to make the
1832 // lyx servers client be language indepenent we must not translate
1833 // strings sent to this func.
1834 void LyXFunc::setErrorMessage(string const & m) const
1836 dispatch_buffer = m;
1841 void LyXFunc::setMessage(string const & m) const
1843 dispatch_buffer = m;
1847 void LyXFunc::setStatusMessage(string const & m) const
1853 string const LyXFunc::view_status_message()
1855 // When meta-fake key is pressed, show the key sequence so far + "M-".
1857 return keyseq.print() + "M-";
1860 // Else, when a non-complete key sequence is pressed,
1861 // show the available options.
1862 if (keyseq.length() > 0 && !keyseq.deleted()) {
1863 return keyseq.printOptions();
1866 if (!owner->view()->available())
1867 return _("Welcome to LyX!");
1869 return currentState(owner->view().get());