1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "lyxserver.h"
27 #include "LyXAction.h"
33 #include "trans_mgr.h"
36 #include "bufferview_funcs.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/lyx_gui.h"
40 #include "FloatList.h"
41 #include "converter.h"
44 #include "TextCache.h"
46 #include "undo_funcs.h"
47 #include "ParagraphParameters.h"
49 #include "insets/insetcommand.h"
50 #include "insets/insettabular.h"
52 #include "mathed/formulamacro.h"
53 #include "mathed/math_cursor.h"
54 #include "mathed/math_inset.h"
56 #include "frontends/FileDialog.h"
57 #include "frontends/Dialogs.h"
58 #include "frontends/Toolbar.h"
59 #include "frontends/Menubar.h"
60 #include "frontends/Alert.h"
62 #include "graphics/GraphicsCache.h"
64 #include "support/lyxalgo.h"
65 #include "support/LAssert.h"
66 #include "support/filetools.h"
67 #include "support/FileInfo.h"
68 #include "support/forkedcontr.h"
69 #include "support/lstrings.h"
70 #include "support/path.h"
71 #include "support/lyxfunctional.h"
73 #include "BoostFormat.h"
89 using std::back_inserter;
91 extern BufferList bufferlist;
92 extern LyXServer * lyxserver;
93 extern bool selection_possible;
95 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
98 extern tex_accent_struct get_accent(kb_action action);
100 extern void ShowLatexLog();
103 LyXFunc::LyXFunc(LyXView * o)
106 keyseq(toplevel_keymap.get(), toplevel_keymap.get()),
107 cancel_meta_seq(toplevel_keymap.get(), toplevel_keymap.get()),
108 meta_fake_bit(key_modifier::none)
114 LyXText * LyXFunc::TEXT(bool flag = true) const
118 return view()->getLyXText();
123 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
125 if (selecting || TEXT(flag)->selection.mark()) {
126 TEXT(flag)->setSelection(view());
127 if (TEXT(flag)->bv_owner)
128 view()->toggleToggle();
130 view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
131 view()->showCursor();
133 view()->switchKeyMap();
137 void LyXFunc::handleKeyFunc(kb_action action)
139 char c = encoded_last_key;
141 if (keyseq.length()) {
145 owner->getIntl().getTransManager()
146 .deadkey(c, get_accent(action).accent, TEXT(false));
147 // Need to clear, in case the minibuffer calls these
150 // copied verbatim from do_accent_char
151 view()->update(TEXT(false),
152 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
153 TEXT(false)->selection.cursor = TEXT(false)->cursor;
157 void LyXFunc::processKeySym(LyXKeySymPtr keysym,
158 key_modifier::state state)
162 if (lyxerr.debugging(Debug::KEY)) {
163 lyxerr << "KeySym is "
164 << keysym->getSymbolName()
168 // Do nothing if we have nothing (JMarc)
169 if (!keysym->isOK()) {
170 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
175 if (keysym->isModifier()) {
176 lyxerr[Debug::KEY] << "isModifier true" << endl;
180 Encoding const * encoding = view()->getEncoding();
182 encoded_last_key = keysym->getISOEncoded(encoding ? encoding->Name() : "");
184 // Do a one-deep top-level lookup for
185 // cancel and meta-fake keys. RVDK_PATCH_5
186 cancel_meta_seq.reset();
188 int action = cancel_meta_seq.addkey(keysym, state);
189 lyxerr[Debug::KEY] << "action first set to [" << action << ']' << endl;
191 // When not cancel or meta-fake, do the normal lookup.
192 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
193 // Mostly, meta_fake_bit = key_modifier::none. RVDK_PATCH_5.
194 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
195 // remove Caps Lock and Mod2 as a modifiers
196 action = keyseq.addkey(keysym, (state | meta_fake_bit));
197 lyxerr[Debug::KEY] << "action now set to ["
198 << action << ']' << endl;
201 // Dont remove this unless you know what you are doing.
202 meta_fake_bit = key_modifier::none;
204 // can this happen now ?
205 if (action == LFUN_NOACTION) {
206 action = LFUN_PREFIX;
209 if (lyxerr.debugging(Debug::KEY)) {
210 lyxerr << "Key [action="
212 << keyseq.print() << ']'
216 // already here we know if it any point in going further
217 // why not return already here if action == -1 and
218 // num_bytes == 0? (Lgb)
220 if (keyseq.length() > 1) {
221 owner->message(keyseq.print());
225 // Maybe user can only reach the key via holding down shift.
226 // Let's see. But only if shift is the only modifier
227 if (action == LFUN_UNKNOWN_ACTION && state == key_modifier::shift) {
228 lyxerr[Debug::KEY] << "Trying without shift" << endl;
229 action = keyseq.addkey(keysym, key_modifier::none);
230 lyxerr[Debug::KEY] << "Action now " << action << endl;
233 if (action == LFUN_UNKNOWN_ACTION) {
234 // Hmm, we didn't match any of the keysequences. See
235 // if it's normal insertable text not already covered
237 if (keysym->isText() && keyseq.length() == 1) {
238 lyxerr[Debug::KEY] << "isText() is true, inserting." << endl;
239 action = LFUN_SELFINSERT;
241 lyxerr[Debug::KEY] << "Unknown, !isText() - giving up" << endl;
242 owner->message(_("Unknown function."));
247 if (action == LFUN_SELFINSERT) {
248 if (encoded_last_key != 0) {
250 arg += encoded_last_key;
252 dispatch(FuncRequest(view(), LFUN_SELFINSERT, arg));
254 lyxerr[Debug::KEY] << "SelfInsert arg[`"
255 << argument << "']" << endl;
263 FuncStatus LyXFunc::getStatus(int ac) const
265 return getStatus(lyxaction.retrieveActionArg(ac));
269 FuncStatus LyXFunc::getStatus(FuncRequest const & ev) const
272 Buffer * buf = owner->buffer();
274 if (ev.action == LFUN_NOACTION) {
275 setStatusMessage(N_("Nothing to do"));
276 return flag.disabled(true);
280 case LFUN_UNKNOWN_ACTION:
281 #ifndef HAVE_LIBAIKSAURUS
282 case LFUN_THESAURUS_ENTRY:
287 flag |= lyx_gui::getStatus(ev);
290 if (flag.unknown()) {
291 setStatusMessage(N_("Unknown action"));
295 // the default error message if we disable the command
296 setStatusMessage(N_("Command disabled"));
298 // Check whether we need a buffer
299 if (!lyxaction.funcHasFlag(ev.action, LyXAction::NoBuffer)) {
300 // Yes we need a buffer, do we have one?
303 // Can we use a readonly buffer?
304 if (buf->isReadonly() &&
305 !lyxaction.funcHasFlag(ev.action,
306 LyXAction::ReadOnly)) {
308 setStatusMessage(N_("Document is read-only"));
313 setStatusMessage(N_("Command not allowed with"
314 "out any document open"));
315 return flag.disabled(true);
319 UpdatableInset * tli = view()->theLockingInset();
321 // I would really like to avoid having this switch and rather try to
322 // encode this in the function itself.
323 bool disable = false;
326 disable = !Exporter::IsExportable(buf, "dvi")
327 || lyxrc.print_command == "none";
330 disable = !Exporter::IsExportable(buf, ev.argument);
333 disable = buf->undostack.empty();
336 disable = buf->redostack.empty();
341 UpdatableInset * in = tli;
342 if (in->lyxCode() != Inset::TABULAR_CODE) {
343 in = tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
345 if (in && static_cast<InsetTabular*>(in)->hasSelection()) {
350 disable = !mathcursor && !view()->getLyXText()->selection.set();
353 disable = lyxrc.chktex_command == "none";
356 disable = !Exporter::IsExportable(buf, "program");
359 case LFUN_LAYOUT_CHARACTER:
360 disable = tli && tli->lyxCode() == Inset::ERT_CODE;
363 case LFUN_LAYOUT_TABULAR:
365 || (tli->lyxCode() != Inset::TABULAR_CODE
366 && !tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
370 case LFUN_LAYOUT_PARAGRAPH: {
371 Inset * inset = TEXT(false)->cursor.par()->inInset();
372 disable = inset && inset->forceDefaultParagraphs(inset);
376 case LFUN_INSET_OPTARG:
377 disable = (TEXT(false)->cursor.par()->layout()->optionalargs == 0);
380 case LFUN_TABULAR_FEATURE:
383 // FIXME: check temporarily disabled
385 char align = mathcursor->valign();
390 if (ev.argument.empty()) {
394 if (!contains("tcb", ev.argument[0])) {
398 flag.setOnOff(ev.argument[0] == align);
402 char align = mathcursor->halign();
407 if (ev.argument.empty()) {
411 if (!contains("lcr", ev.argument[0])) {
415 flag.setOnOff(ev.argument[0] == align);
418 disable = !mathcursor->halign();
424 //ret.disabled(true);
425 if (tli->lyxCode() == Inset::TABULAR_CODE) {
426 ret = static_cast<InsetTabular *>(tli)
427 ->getStatus(ev.argument);
428 } else if (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
429 ret = static_cast<InsetTabular *>
430 (tli->getFirstLockingInsetOfType(Inset::TABULAR_CODE))
431 ->getStatus(ev.argument);
436 static InsetTabular inset(*owner->buffer(), 1, 1);
440 ret = inset.getStatus(ev.argument);
441 if (ret.onoff(true) || ret.onoff(false))
442 flag.setOnOff(false);
446 case LFUN_VC_REGISTER:
447 disable = buf->lyxvc.inUse();
449 case LFUN_VC_CHECKIN:
450 disable = !buf->lyxvc.inUse() || buf->isReadonly();
452 case LFUN_VC_CHECKOUT:
453 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
457 case LFUN_VC_HISTORY:
458 disable = !buf->lyxvc.inUse();
460 case LFUN_BOOKMARK_GOTO:
462 isSavedPosition(strToUnsignedInt(ev.argument));
464 case LFUN_INSET_TOGGLE: {
465 LyXText * lt = view()->getLyXText();
466 disable = !(isEditableInset(lt->getInset())
468 && lt->inset_owner->owner()
469 && lt->inset_owner->owner()->isOpen()));
473 disable = !IsFileReadable(buf->getLogName().second);
476 case LFUN_MATH_MUTATE:
478 //flag.setOnOff(mathcursor->formula()->hullType() == ev.argument);
479 flag.setOnOff(false);
484 // we just need to be in math mode to enable that
486 case LFUN_MATH_SPACE:
487 case LFUN_MATH_LIMITS:
488 case LFUN_MATH_NONUMBER:
489 case LFUN_MATH_NUMBER:
490 case LFUN_MATH_EXTERN:
491 disable = !mathcursor;
498 // the functions which insert insets
499 Inset::Code code = Inset::NO_CODE;
502 code = Inset::ERT_CODE;
504 case LFUN_INSET_GRAPHICS:
505 code = Inset::GRAPHICS_CODE;
507 case LFUN_INSET_FOOTNOTE:
508 code = Inset::FOOT_CODE;
510 case LFUN_TABULAR_INSERT:
511 code = Inset::TABULAR_CODE;
513 case LFUN_INSET_EXTERNAL:
514 code = Inset::EXTERNAL_CODE;
516 case LFUN_INSET_MARGINAL:
517 code = Inset::MARGIN_CODE;
519 case LFUN_INSET_MINIPAGE:
520 code = Inset::MINIPAGE_CODE;
522 case LFUN_INSET_FLOAT:
523 case LFUN_INSET_WIDE_FLOAT:
524 code = Inset::FLOAT_CODE;
526 case LFUN_INSET_WRAP:
527 code = Inset::WRAP_CODE;
529 case LFUN_FLOAT_LIST:
530 code = Inset::FLOAT_LIST_CODE;
533 case LFUN_INSET_LIST:
534 code = Inset::LIST_CODE;
536 case LFUN_INSET_THEOREM:
537 code = Inset::THEOREM_CODE;
540 case LFUN_INSET_CAPTION:
541 code = Inset::CAPTION_CODE;
543 case LFUN_INSERT_NOTE:
544 code = Inset::NOTE_CODE;
546 case LFUN_INSERT_LABEL:
547 code = Inset::LABEL_CODE;
549 case LFUN_INSET_OPTARG:
550 code = Inset::OPTARG_CODE;
552 case LFUN_REF_INSERT:
553 code = Inset::REF_CODE;
555 case LFUN_CITATION_CREATE:
556 case LFUN_CITATION_INSERT:
557 code = Inset::CITE_CODE;
559 case LFUN_INSERT_BIBTEX:
560 code = Inset::BIBTEX_CODE;
562 case LFUN_INDEX_INSERT:
563 code = Inset::INDEX_CODE;
565 case LFUN_INDEX_PRINT:
566 code = Inset::INDEX_PRINT_CODE;
568 case LFUN_CHILD_INSERT:
569 code = Inset::INCLUDE_CODE;
571 case LFUN_TOC_INSERT:
572 code = Inset::TOC_CODE;
574 case LFUN_PARENTINSERT:
575 code = Inset::PARENT_CODE;
579 case LFUN_INSERT_URL:
580 code = Inset::URL_CODE;
583 // always allow this, since we will inset a raw quote
584 // if an inset is not allowed.
586 case LFUN_HYPHENATION:
587 case LFUN_LIGATURE_BREAK:
589 case LFUN_MENU_SEPARATOR:
591 case LFUN_END_OF_SENTENCE:
592 code = Inset::SPECIALCHAR_CODE;
594 case LFUN_PROTECTEDSPACE:
595 // slight hack: we know this is allowed in math mode
597 code = Inset::SPECIALCHAR_CODE;
602 if (code != Inset::NO_CODE && tli && !tli->insetAllowed(code))
608 // A few general toggles
610 case LFUN_TOOLTIPS_TOGGLE:
611 flag.setOnOff(owner->getDialogs().tooltipsEnabled());
614 case LFUN_READ_ONLY_TOGGLE:
615 flag.setOnOff(buf->isReadonly());
618 flag.setOnOff(TEXT(false)->cursor.par()->params().startOfAppendix());
620 case LFUN_SWITCHBUFFER:
621 // toggle on the current buffer, but do not toggle off
622 // the other ones (is that a good idea?)
623 if (ev.argument == buf->fileName())
630 // the font related toggles
632 LyXFont const & font = TEXT(false)->real_current_font;
635 flag.setOnOff(font.emph() == LyXFont::ON);
638 flag.setOnOff(font.noun() == LyXFont::ON);
641 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
644 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
647 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
650 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
656 string tc = mathcursor->getLastCode();
659 flag.setOnOff(tc == "mathbf");
662 flag.setOnOff(tc == "mathsf");
665 flag.setOnOff(tc == "mathcal");
668 flag.setOnOff(tc == "mathrm");
671 flag.setOnOff(tc == "mathtt");
674 flag.setOnOff(tc == "mathbb");
677 flag.setOnOff(tc == "mathnormal");
684 // this one is difficult to get right. As a half-baked
685 // solution, we consider only the first action of the sequence
686 if (ev.action == LFUN_SEQUENCE) {
687 // argument contains ';'-terminated commands
688 flag = getStatus(lyxaction.LookupFunc(token(ev.argument, ';', 0)));
695 void LyXFunc::dispatch(string const & s, bool verbose)
697 int const action = lyxaction.LookupFunc(s);
699 if (action == LFUN_UNKNOWN_ACTION) {
701 boost::format fmt(_("Unknown function (%1$s)"));
703 owner->message(fmt.str());
705 string const msg = string(_("Unknown function ("))
712 dispatch(action, verbose);
716 void LyXFunc::dispatch(int ac, bool verbose)
718 dispatch(lyxaction.retrieveActionArg(ac), verbose);
723 void LyXFunc::dispatch(FuncRequest const & ev, bool verbose)
725 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << ev.action
726 <<"] arg[" << ev.argument << ']' << endl;
728 // we have not done anything wrong yet.
730 dispatch_buffer.erase();
732 #ifdef NEW_DISPATCHER
733 // We try do call the most specific dispatcher first:
734 // 1. the lockinginset's dispatch
735 // 2. the bufferview's dispatch
736 // 3. the lyxview's dispatch
739 selection_possible = false;
741 if (view()->available())
742 view()->hideCursor();
744 string argument = ev.argument;
745 kb_action action = ev.action;
747 // We cannot use this function here
748 if (getStatus(ev).disabled()) {
749 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: "
750 << lyxaction.getActionName(action)
751 << " [" << action << "] is disabled at this location"
753 setErrorMessage(getStatusMessage());
754 goto exit_with_message;
757 if (view()->available() && view()->theLockingInset()) {
758 Inset::RESULT result;
759 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
760 (!keyseq.deleted())))
762 UpdatableInset * inset = view()->theLockingInset();
766 inset->getCursorPos(view(), inset_x, dummy_y);
768 if ((action == LFUN_UNKNOWN_ACTION)
769 && argument.empty()) {
770 argument = encoded_last_key;
772 // Undo/Redo is a bit tricky for insets.
773 if (action == LFUN_UNDO) {
775 goto exit_with_message;
776 } else if (action == LFUN_REDO) {
778 goto exit_with_message;
779 } else if (((result=inset->
780 // Hand-over to inset's own dispatch:
781 localDispatch(FuncRequest(view(), action, argument))) ==
782 UpdatableInset::DISPATCHED) ||
783 (result == UpdatableInset::DISPATCHED_NOUPDATE))
784 goto exit_with_message;
785 // If UNDISPATCHED, just soldier on
786 else if (result == UpdatableInset::FINISHED) {
787 goto exit_with_message;
788 // We do not need special RTL handling here:
789 // FINISHED means that the cursor should be
790 // one position after the inset.
791 } else if (result == UpdatableInset::FINISHED_RIGHT) {
792 TEXT()->cursorRight(view());
793 moveCursorUpdate(true, false);
794 owner->view_state_changed();
795 goto exit_with_message;
796 } else if (result == UpdatableInset::FINISHED_UP) {
797 if (TEXT()->cursor.irow()->previous()) {
799 TEXT()->setCursorFromCoordinates(
800 view(), TEXT()->cursor.ix() + inset_x,
801 TEXT()->cursor.iy() -
802 TEXT()->cursor.irow()->baseline() - 1);
803 TEXT()->cursor.x_fix(TEXT()->cursor.x());
805 TEXT()->cursorUp(view());
807 moveCursorUpdate(true, false);
808 owner->view_state_changed();
810 view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
812 goto exit_with_message;
813 } else if (result == UpdatableInset::FINISHED_DOWN) {
814 if (TEXT()->cursor.irow()->next()) {
816 TEXT()->setCursorFromCoordinates(
817 view(), TEXT()->cursor.ix() + inset_x,
818 TEXT()->cursor.iy() -
819 TEXT()->cursor.irow()->baseline() +
820 TEXT()->cursor.irow()->height() + 1);
821 TEXT()->cursor.x_fix(TEXT()->cursor.x());
823 TEXT()->cursorDown(view());
826 TEXT()->cursorRight(view());
828 moveCursorUpdate(true, false);
829 owner->view_state_changed();
830 goto exit_with_message;
832 #warning I am not sure this is still right, please have a look! (Jug 20020417)
833 else { // result == UNDISPATCHED
834 //setMessage(N_("Text mode"));
836 case LFUN_UNKNOWN_ACTION:
837 case LFUN_BREAKPARAGRAPH:
839 TEXT()->cursorRight(view());
840 view()->switchKeyMap();
841 owner->view_state_changed();
844 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
845 TEXT()->cursorRight(view());
846 moveCursorUpdate(true, false);
847 owner->view_state_changed();
849 goto exit_with_message;
851 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
852 TEXT()->cursorRight(view());
853 moveCursorUpdate(true, false);
854 owner->view_state_changed();
856 goto exit_with_message;
858 if (TEXT()->cursor.row()->next())
859 TEXT()->cursorDown(view());
861 TEXT()->cursorRight(view());
862 moveCursorUpdate(true, false);
863 owner->view_state_changed();
864 goto exit_with_message;
876 if (!view()->available()) break;
877 // this function should be used always [asierra060396]
878 UpdatableInset * tli =
879 view()->theLockingInset();
881 UpdatableInset * lock = tli->getLockingInset();
884 view()->unlockInset(tli);
885 TEXT()->cursorRight(view());
886 moveCursorUpdate(true, false);
887 owner->view_state_changed();
889 tli->unlockInsetInInset(view(),
894 // Tell the paragraph dialog that we changed paragraph
895 owner->getDialogs().updateParagraph();
900 // --- Misc -------------------------------------------
901 case LFUN_WORDFINDFORWARD :
902 case LFUN_WORDFINDBACKWARD : {
903 static string last_search;
904 string searched_string;
906 if (!argument.empty()) {
907 last_search = argument;
908 searched_string = argument;
910 searched_string = last_search;
912 bool fw = (action == LFUN_WORDFINDFORWARD);
913 if (!searched_string.empty()) {
914 lyxfind::LyXFind(view(), searched_string, fw);
916 // view()->showCursor();
922 if (view()->available() && !view()->theLockingInset()) {
923 view()->update(TEXT(),
924 BufferView::SELECT|BufferView::FITCUR);
926 owner->message(keyseq.printOptions());
930 // --- Misc -------------------------------------------
931 case LFUN_EXEC_COMMAND:
932 owner->focus_command_buffer();
935 case LFUN_CANCEL: // RVDK_PATCH_5
937 meta_fake_bit = key_modifier::none;
938 if (view()->available())
939 // cancel any selection
940 dispatch(LFUN_MARK_OFF);
941 setMessage(N_("Cancel"));
944 case LFUN_META_FAKE: // RVDK_PATCH_5
946 meta_fake_bit = key_modifier::alt;
947 setMessage(keyseq.print());
951 case LFUN_READ_ONLY_TOGGLE:
952 if (owner->buffer()->lyxvc.inUse()) {
953 owner->buffer()->lyxvc.toggleReadOnly();
955 owner->buffer()->setReadonly(
956 !owner->buffer()->isReadonly());
960 case LFUN_CENTER: // this is center and redraw.
964 // --- Menus -----------------------------------------------
966 menuNew(argument, false);
969 case LFUN_MENUNEWTMPLT:
970 menuNew(argument, true);
973 case LFUN_CLOSEBUFFER:
978 if (!owner->buffer()->isUnnamed()) {
981 s1 << boost::format(_("Saving document %1$s..."))
982 % MakeDisplayPath(owner->buffer()->fileName());
984 s1 << _("Saving document ")
985 << MakeDisplayPath(owner->buffer()->fileName())
988 owner->message(STRCONV(s1.str()));
989 MenuWrite(view(), owner->buffer());
991 owner->message(STRCONV(s1.str()));
993 WriteAs(view(), owner->buffer());
997 WriteAs(view(), owner->buffer(), argument);
1000 case LFUN_MENURELOAD:
1005 Exporter::Export(owner->buffer(), argument, true);
1009 Exporter::Preview(owner->buffer(), argument);
1012 case LFUN_BUILDPROG:
1013 Exporter::Export(owner->buffer(), "program", true);
1016 case LFUN_RUNCHKTEX:
1017 MenuRunChktex(owner->buffer());
1020 case LFUN_MENUPRINT:
1021 owner->getDialogs().showPrint();
1025 if (argument == "custom")
1026 owner->getDialogs().showSendto();
1028 Exporter::Export(owner->buffer(), argument, false);
1046 InsetCommandParams p;
1049 if (action == LFUN_TOCVIEW)
1051 p.setCmdName("tableofcontents");
1053 else if (action == LFUN_LOAVIEW)
1054 p.setCmdName("listof{algorithm}{List of Algorithms}");
1055 else if (action == LFUN_LOFVIEW)
1056 p.setCmdName("listoffigures");
1058 p.setCmdName("listoftables");
1060 owner->getDialogs().createTOC(p.getAsString());
1076 case LFUN_MENUSEARCH:
1077 owner->getDialogs().showSearch();
1080 case LFUN_REMOVEERRORS:
1081 if (view()->removeAutoInsets()) {
1082 #warning repaint() or update() or nothing ?
1084 view()->fitCursor();
1088 case LFUN_DEPTH_MIN:
1089 changeDepth(view(), TEXT(false), -1);
1092 case LFUN_DEPTH_PLUS:
1093 changeDepth(view(), TEXT(false), 1);
1097 owner->getDialogs().setUserFreeFont();
1100 case LFUN_RECONFIGURE:
1101 Reconfigure(view());
1105 case LFUN_FLOATSOPERATE:
1106 if (argument == "openfoot")
1107 view()->allFloats(1,0);
1108 else if (argument == "closefoot")
1109 view()->allFloats(0,0);
1110 else if (argument == "openfig")
1111 view()->allFloats(1,1);
1112 else if (argument == "closefig")
1113 view()->allFloats(0,1);
1116 #ifdef WITH_WARNINGS
1117 //#warning Find another implementation here (or another lyxfunc)!
1120 case LFUN_HELP_ABOUTLYX:
1121 owner->getDialogs().showAboutlyx();
1124 case LFUN_HELP_TEXINFO:
1125 owner->getDialogs().showTexinfo();
1128 case LFUN_HELP_OPEN:
1130 string const arg = argument;
1132 setErrorMessage(N_("Missing argument"));
1135 owner->prohibitInput();
1136 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1137 if (fname.empty()) {
1138 lyxerr << "LyX: unable to find documentation file `"
1139 << arg << "'. Bad installation?" << endl;
1140 owner->allowInput();
1144 #if USE_BOOST_FORMAT
1145 str << boost::format(_("Opening help file %1$s..."))
1146 % MakeDisplayPath(fname);
1148 str << _("Opening help file ")
1149 << MakeDisplayPath(fname) << _("...");
1151 owner->message(STRCONV(str.str()));
1152 view()->buffer(bufferlist.loadLyXFile(fname, false));
1153 owner->allowInput();
1157 // --- version control -------------------------------
1158 case LFUN_VC_REGISTER:
1160 if (!owner->buffer()->lyxvc.inUse())
1161 owner->buffer()->lyxvc.registrer();
1165 case LFUN_VC_CHECKIN:
1167 if (owner->buffer()->lyxvc.inUse()
1168 && !owner->buffer()->isReadonly())
1169 owner->buffer()->lyxvc.checkIn();
1173 case LFUN_VC_CHECKOUT:
1175 if (owner->buffer()->lyxvc.inUse()
1176 && owner->buffer()->isReadonly())
1177 owner->buffer()->lyxvc.checkOut();
1181 case LFUN_VC_REVERT:
1183 owner->buffer()->lyxvc.revert();
1189 owner->buffer()->lyxvc.undoLast();
1193 case LFUN_VC_HISTORY:
1195 owner->getDialogs().showVCLogFile();
1199 // --- buffers ----------------------------------------
1201 case LFUN_SWITCHBUFFER:
1202 view()->buffer(bufferlist.getBuffer(argument));
1207 // servercmd: argument must be <file>:<template>
1208 Buffer * tmpbuf = NewFile(argument);
1210 view()->buffer(tmpbuf);
1214 case LFUN_FILE_OPEN:
1218 case LFUN_LATEX_LOG:
1219 owner->getDialogs().showLogFile();
1222 case LFUN_LAYOUT_DOCUMENT:
1223 owner->getDialogs().showDocument();
1226 case LFUN_LAYOUT_PARAGRAPH:
1227 owner->getDialogs().showParagraph();
1230 case LFUN_LAYOUT_CHARACTER:
1231 owner->getDialogs().showCharacter();
1234 case LFUN_LAYOUT_TABULAR:
1235 if (view()->theLockingInset()) {
1236 if (view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1237 InsetTabular * inset = static_cast<InsetTabular *>
1238 (view()->theLockingInset());
1239 inset->openLayoutDialog(view());
1240 } else if (view()->theLockingInset()->
1241 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1242 InsetTabular * inset = static_cast<InsetTabular *>(
1243 view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1244 inset->openLayoutDialog(view());
1249 case LFUN_LAYOUT_PREAMBLE:
1250 owner->getDialogs().showPreamble();
1253 case LFUN_DROP_LAYOUTS_CHOICE:
1254 owner->getToolbar().openLayoutList();
1257 case LFUN_MENU_OPEN_BY_NAME:
1258 owner->getMenubar().openByName(argument);
1259 break; // RVDK_PATCH_5
1261 case LFUN_SPELLCHECK:
1262 owner->getDialogs().showSpellchecker();
1265 // --- lyxserver commands ----------------------------
1269 setMessage(owner->buffer()->fileName());
1270 lyxerr[Debug::INFO] << "FNAME["
1271 << owner->buffer()->fileName()
1277 dispatch_buffer = keyseq.print();
1278 lyxserver->notifyClient(dispatch_buffer);
1282 case LFUN_GOTOFILEROW:
1286 istringstream istr(argument.c_str());
1287 istr >> file_name >> row;
1288 // Must replace extension of the file to be .lyx and get full path
1289 string const s(ChangeExtension(file_name, ".lyx"));
1291 // Either change buffer or load the file
1292 if (bufferlist.exists(s)) {
1293 view()->buffer(bufferlist.getBuffer(s));
1295 view()->buffer(bufferlist.loadLyXFile(s));
1298 view()->setCursorFromRow(row);
1301 // see BufferView_pimpl::center()
1302 view()->updateScrollbar();
1306 case LFUN_GOTO_PARAGRAPH:
1308 istringstream istr(argument.c_str());
1312 Paragraph * par = owner->buffer()->getParFromID(id);
1314 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1315 << id << ']' << endl;
1318 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1319 << " found." << endl;
1322 if (view()->theLockingInset())
1323 view()->unlockInset(view()->theLockingInset());
1324 if (par->inInset()) {
1325 par->inInset()->edit(view());
1328 view()->getLyXText()->setCursor(view(), par, 0);
1329 view()->switchKeyMap();
1330 owner->view_state_changed();
1333 // see BufferView_pimpl::center()
1334 view()->updateScrollbar();
1341 int const qa = lyxaction.LookupFunc(argument);
1342 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1346 // --- insert characters ----------------------------------------
1348 // --- Mathed stuff. If we are here, there is no locked inset yet.
1349 case LFUN_MATH_EXTERN:
1350 case LFUN_MATH_NUMBER:
1351 case LFUN_MATH_NONUMBER:
1352 case LFUN_MATH_LIMITS:
1354 setErrorMessage(N_("This is only allowed in math mode!"));
1358 // passthrough hat and underscore outside mathed:
1359 case LFUN_SUBSCRIPT:
1360 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "_"));
1362 case LFUN_SUPERSCRIPT:
1363 dispatch(FuncRequest(view(), LFUN_SELFINSERT, "^"));
1366 case LFUN_MATH_PANEL:
1367 owner->getDialogs().showMathPanel();
1370 case LFUN_CITATION_CREATE:
1372 InsetCommandParams p("cite");
1374 if (!argument.empty()) {
1375 // This should be set at source, ie when typing
1376 // "citation-insert foo" in the minibuffer.
1377 // Question: would pybibliographer also need to be
1378 // changed. Suspect so. Leave as-is therefore.
1379 if (contains(argument, "|")) {
1380 p.setContents(token(argument, '|', 0));
1381 p.setOptions(token(argument, '|', 1));
1383 p.setContents(argument);
1385 dispatch(FuncRequest(view(), LFUN_CITATION_INSERT, p.getAsString()));
1387 owner->getDialogs().createCitation(p.getAsString());
1391 case LFUN_CHILDOPEN:
1393 string const filename =
1394 MakeAbsPath(argument,
1395 owner->buffer()->filePath());
1396 setMessage(N_("Opening child document ") +
1397 MakeDisplayPath(filename) + "...");
1398 view()->savePosition(0);
1399 if (bufferlist.exists(filename))
1400 view()->buffer(bufferlist.getBuffer(filename));
1402 view()->buffer(bufferlist.loadLyXFile(filename));
1406 case LFUN_TOGGLECURSORFOLLOW:
1407 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1411 owner->getIntl().KeyMapOn(false);
1414 case LFUN_KMAP_PRIM:
1415 owner->getIntl().KeyMapPrim();
1419 owner->getIntl().KeyMapSec();
1422 case LFUN_KMAP_TOGGLE:
1423 owner->getIntl().ToggleKeyMap();
1428 // argument contains ';'-terminated commands
1429 while (!argument.empty()) {
1431 argument = split(argument, first, ';');
1437 case LFUN_DIALOG_PREFERENCES:
1438 owner->getDialogs().showPreferences();
1441 case LFUN_SAVEPREFERENCES:
1443 Path p(user_lyxdir);
1444 lyxrc.write("preferences");
1448 case LFUN_SCREEN_FONT_UPDATE:
1450 // handle the screen font changes.
1451 lyxrc.set_font_norm_type();
1452 lyx_gui::update_fonts();
1453 // We also need to empty the textcache so that
1454 // the buffer will be formatted correctly after
1457 // Of course we should only do the resize and the textcache.clear
1458 // if values really changed...but not very important right now. (Lgb)
1459 // All visible buffers will need resize
1465 case LFUN_SET_COLOR:
1468 string const x11_name = split(argument, lyx_name, ' ');
1469 if (lyx_name.empty() || x11_name.empty()) {
1470 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1475 bool const graphicsbg_changed =
1476 (lyx_name == lcolor.getLyXName(LColor::graphicsbg) &&
1477 x11_name != lcolor.getX11Name(LColor::graphicsbg));
1479 if (!lcolor.setColor(lyx_name, x11_name)) {
1480 #if USE_BOOST_FORMAT
1484 _("Set-color \"%1$s\" failed "
1485 "- color is undefined or "
1486 "may not be redefined"))
1489 setErrorMessage(_("Set-color ") + lyx_name
1490 + _(" failed - color is undefined"
1491 " or may not be redefined"));
1497 lyx_gui::update_color(lcolor.getFromLyXName(lyx_name));
1499 if (graphicsbg_changed) {
1500 #ifdef WITH_WARNINGS
1501 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
1504 grfx::GCache & gc = grfx::GCache::get();
1505 gc.changeDisplay(true);
1514 owner->message(argument);
1517 case LFUN_FORKS_SHOW:
1518 owner->getDialogs().showForks();
1521 case LFUN_FORKS_KILL:
1523 if (!isStrInt(argument))
1526 pid_t const pid = strToInt(argument);
1527 ForkedcallsController & fcc = ForkedcallsController::get();
1532 case LFUN_TOOLTIPS_TOGGLE:
1533 owner->getDialogs().toggleTooltips();
1537 // Then if it was none of the above
1538 // Trying the BufferView::pimpl dispatch:
1539 if (!view()->dispatch(ev))
1540 lyxerr << "A truly unknown func ["
1541 << lyxaction.getActionName(ev.action) << "]!"
1547 sendDispatchMessage(getMessage(), ev, verbose);
1551 void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & ev, bool verbose)
1553 owner->updateMenubar();
1554 owner->updateToolbar();
1556 if (ev.action == LFUN_SELFINSERT || !verbose) {
1557 lyxerr[Debug::ACTION] << "dispatch msg is " << msg << endl;
1559 owner->message(msg);
1563 string dispatch_msg = msg;
1564 if (!dispatch_msg.empty())
1565 dispatch_msg += ' ';
1567 string comname = lyxaction.getActionName(ev.action);
1569 int pseudoaction = ev.action;
1570 bool argsadded = false;
1572 if (!ev.argument.empty()) {
1573 // the pseudoaction is useful for the bindings
1574 pseudoaction = lyxaction.searchActionArg(ev.action, ev.argument);
1576 if (pseudoaction == LFUN_UNKNOWN_ACTION) {
1577 pseudoaction = ev.action;
1579 comname += ' ' + ev.argument;
1584 string const shortcuts = toplevel_keymap->findbinding(pseudoaction);
1586 if (!shortcuts.empty()) {
1587 comname += ": " + shortcuts;
1588 } else if (!argsadded && !ev.argument.empty()) {
1589 comname += ' ' + ev.argument;
1592 if (!comname.empty()) {
1593 comname = rtrim(comname);
1594 dispatch_msg += '(' + comname + ')';
1597 lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
1598 if (!dispatch_msg.empty())
1599 owner->message(dispatch_msg);
1603 void LyXFunc::setupLocalKeymap()
1605 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1606 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1610 void LyXFunc::menuNew(string const & name, bool fromTemplate)
1612 string initpath = lyxrc.document_path;
1613 string filename(name);
1615 if (view()->available()) {
1616 string const trypath = owner->buffer()->filePath();
1617 // If directory is writeable, use this as default.
1618 if (IsDirWriteable(trypath))
1622 static int newfile_number;
1624 if (filename.empty()) {
1625 filename = AddName(lyxrc.document_path,
1626 "newfile" + tostr(++newfile_number) + ".lyx");
1627 FileInfo fi(filename);
1628 while (bufferlist.exists(filename) || fi.readable()) {
1630 filename = AddName(lyxrc.document_path,
1631 "newfile" + tostr(newfile_number) +
1633 fi.newFile(filename);
1637 // The template stuff
1640 FileDialog fileDlg(owner, _("Select template file"),
1641 LFUN_SELECT_FILE_SYNC,
1642 make_pair(string(_("Documents|#o#O")),
1643 string(lyxrc.document_path)),
1644 make_pair(string(_("Templates|#T#t")),
1645 string(lyxrc.template_path)));
1647 FileDialog::Result result =
1648 fileDlg.open(lyxrc.template_path,
1649 _("*.lyx| LyX Documents (*.lyx)"));
1651 if (result.first == FileDialog::Later)
1654 string const fname = result.second;
1661 view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
1665 void LyXFunc::open(string const & fname)
1667 string initpath = lyxrc.document_path;
1669 if (view()->available()) {
1670 string const trypath = owner->buffer()->filePath();
1671 // If directory is writeable, use this as default.
1672 if (IsDirWriteable(trypath))
1678 if (fname.empty()) {
1679 FileDialog fileDlg(owner, _("Select document to open"),
1681 make_pair(string(_("Documents|#o#O")),
1682 string(lyxrc.document_path)),
1683 make_pair(string(_("Examples|#E#e")),
1684 string(AddPath(system_lyxdir, "examples"))));
1686 FileDialog::Result result =
1687 fileDlg.open(initpath,
1688 _("*.lyx| LyX Documents (*.lyx)"));
1690 if (result.first == FileDialog::Later)
1693 filename = result.second;
1695 // check selected filename
1696 if (filename.empty()) {
1697 owner->message(_("Canceled."));
1703 // get absolute path of file and add ".lyx" to the filename if
1705 string const fullpath = FileSearch(string(), filename, "lyx");
1706 if (!fullpath.empty()) {
1707 filename = fullpath;
1710 string const disp_fn(MakeDisplayPath(filename));
1712 // if the file doesn't exist, let the user create one
1713 FileInfo const f(filename, true);
1715 if (!Alert::askQuestion(_("No such file"), disp_fn,
1716 _("Start a new document with this filename ?"))) {
1717 owner->message(_("Canceled."));
1720 // the user specifically chose this name. Believe them.
1721 Buffer * buffer = bufferlist.newFile(filename, "", true);
1722 view()->buffer(buffer);
1727 #if USE_BOOST_FORMAT
1728 str << boost::format(_("Opening document %1$s...")) % disp_fn;
1730 str << _("Opening document ") << disp_fn << _("...");
1733 owner->message(STRCONV(str.str()));
1735 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1738 view()->buffer(openbuf);
1739 #if USE_BOOST_FORMAT
1740 str2 << boost::format(_("Document %1$s opened.")) % disp_fn;
1742 str2 << _("Document ") << disp_fn << _(" opened.");
1745 #if USE_BOOST_FORMAT
1746 str2 << boost::format(_("Could not open document %1$s"))
1749 str2 << _("Could not open document ") << disp_fn;
1752 owner->message(STRCONV(str2.str()));
1756 void LyXFunc::doImport(string const & argument)
1759 string filename = split(argument, format, ' ');
1761 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1762 << " file: " << filename << endl;
1764 // need user interaction
1765 if (filename.empty()) {
1766 string initpath = lyxrc.document_path;
1768 if (view()->available()) {
1769 string const trypath = owner->buffer()->filePath();
1770 // If directory is writeable, use this as default.
1771 if (IsDirWriteable(trypath))
1775 #if USE_BOOST_FORMAT
1776 boost::format fmt(_("Select %1$s file to import"));
1777 fmt % formats.prettyName(format);
1778 string const text = fmt.str();
1780 string const text = _("Select ") + formats.prettyName(format)
1781 + _(" file to import");;
1784 FileDialog fileDlg(owner, text,
1786 make_pair(string(_("Documents|#o#O")),
1787 string(lyxrc.document_path)),
1788 make_pair(string(_("Examples|#E#e")),
1789 string(AddPath(system_lyxdir, "examples"))));
1791 string const extension = "*." + formats.extension(format)
1792 + "| " + formats.prettyName(format)
1793 + " (*." + formats.extension(format) + ')';
1795 FileDialog::Result result = fileDlg.open(initpath,
1798 if (result.first == FileDialog::Later)
1801 filename = result.second;
1803 // check selected filename
1804 if (filename.empty())
1805 owner->message(_("Canceled."));
1808 if (filename.empty())
1811 // get absolute path of file
1812 filename = MakeAbsPath(filename);
1814 string const lyxfile = ChangeExtension(filename, ".lyx");
1816 // Check if the document already is open
1817 if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
1818 switch (Alert::askConfirmation(_("Document is already open:"),
1819 MakeDisplayPath(lyxfile, 50),
1820 _("Do you want to close that document now?\n"
1821 "('No' will just switch to the open version)")))
1824 // If close is canceled, we cancel here too.
1825 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1829 view()->buffer(bufferlist.getBuffer(lyxfile));
1832 owner->message(_("Canceled."));
1837 // if the file exists already, and we didn't do
1838 // -i lyx thefile.lyx, warn
1839 if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
1840 if (!Alert::askQuestion(_("A document by the name"),
1841 MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
1842 owner->message(_("Canceled."));
1847 Importer::Import(owner, filename, format);
1851 void LyXFunc::reloadBuffer()
1853 string const fn = owner->buffer()->fileName();
1854 if (bufferlist.close(owner->buffer()))
1855 view()->buffer(bufferlist.loadLyXFile(fn));
1859 void LyXFunc::closeBuffer()
1861 if (bufferlist.close(owner->buffer()) && !quitting) {
1862 if (bufferlist.empty()) {
1863 // need this otherwise SEGV may occur while trying to
1864 // set variables that don't exist
1865 // since there's no current buffer
1866 owner->getDialogs().hideBufferDependent();
1868 view()->buffer(bufferlist.first());
1874 // Each "owner" should have it's own message method. lyxview and
1875 // the minibuffer would use the minibuffer, but lyxserver would
1876 // send an ERROR signal to its client. Alejandro 970603
1877 // This func is bit problematic when it comes to NLS, to make the
1878 // lyx servers client be language indepenent we must not translate
1879 // strings sent to this func.
1880 void LyXFunc::setErrorMessage(string const & m) const
1882 dispatch_buffer = m;
1887 void LyXFunc::setMessage(string const & m) const
1889 dispatch_buffer = m;
1893 void LyXFunc::setStatusMessage(string const & m) const
1899 string const LyXFunc::view_status_message()
1901 // When meta-fake key is pressed, show the key sequence so far + "M-".
1903 return keyseq.print() + "M-";
1906 // Else, when a non-complete key sequence is pressed,
1907 // show the available options.
1908 if (keyseq.length() > 0 && !keyseq.deleted()) {
1909 return keyseq.printOptions();
1912 if (!view()->available())
1913 return _("Welcome to LyX!");
1915 return currentState(view());
1919 BufferView * LyXFunc::view() const
1922 return owner->view().get();