1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
25 #pragma implementation
31 #include "bufferlist.h"
32 #include "ColorHandler.h"
33 #include "lyxserver.h"
38 #include "LyXAction.h"
39 #include "insets/inseturl.h"
40 #include "insets/insetlatexaccent.h"
41 #include "insets/insettoc.h"
42 #include "insets/insetref.h"
43 #include "insets/insetparent.h"
44 #include "insets/insetindex.h"
45 #include "insets/insetinclude.h"
46 #include "insets/insetbib.h"
47 #include "insets/insetcite.h"
48 #include "insets/insettext.h"
49 #include "insets/insetert.h"
50 #include "insets/insetexternal.h"
51 #include "insets/insetgraphics.h"
52 #include "insets/insetfoot.h"
53 #include "insets/insetmarginal.h"
54 #include "insets/insetminipage.h"
55 #include "insets/insetfloat.h"
56 #include "insets/insetlist.h"
57 #include "insets/insettabular.h"
58 #include "insets/insettheorem.h"
59 #include "insets/insetcaption.h"
60 #include "mathed/formulamacro.h"
61 #include "spellchecker.h" // RVDK_PATCH_5
62 #include "minibuffer.h"
65 #include "lyx_gui_misc.h"
66 #include "support/filetools.h"
67 #include "support/FileInfo.h"
68 #include "support/syscall.h"
69 #include "support/lstrings.h"
70 #include "support/path.h"
71 #include "support/lyxfunctional.h"
76 #include "trans_mgr.h"
79 #include "bufferview_funcs.h"
80 #include "frontends/FileDialog.h"
81 #include "frontends/Dialogs.h"
82 #include "frontends/Toolbar.h"
83 #include "frontends/Menubar.h"
84 #include "FloatList.h"
85 #include "converter.h"
88 #include "FontLoader.h"
89 #include "TextCache.h"
97 extern void math_insert_symbol(BufferView *, string const &);
98 extern bool math_insert_greek(BufferView *, char);
99 extern BufferList bufferlist;
100 extern LyXServer * lyxserver;
101 extern int greek_kb_flag;
102 extern bool selection_possible;
103 extern void MenuSendto();
105 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
107 extern void show_symbols_form(LyXFunc *);
109 extern LyXAction lyxaction;
111 extern tex_accent_struct get_accent(kb_action action);
113 extern LyXTextClass::size_type current_layout;
115 extern void ShowLatexLog();
117 /* === globals =========================================================== */
119 bool LyXFunc::show_sc = true;
121 LyXFunc::LyXFunc(LyXView * o)
125 lyx_dead_action = LFUN_NOACTION;
126 lyx_calling_dead_action = LFUN_NOACTION;
131 LyXText * LyXFunc::TEXT(bool flag=true) const
134 return owner->view()->text;
135 return owner->view()->getLyXText();
138 // I changed this func slightly. I commented out the ...FinishUndo(),
139 // this means that all places that used to have a moveCursorUpdate, now
140 // have a ...FinishUndo() as the preceeding statement. I have also added
141 // a moveCursorUpdate to some of the functions that updated the cursor, but
142 // that did not show its new position.
144 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
146 if (selecting || TEXT(flag)->mark_set) {
147 TEXT(flag)->SetSelection(owner->view());
148 if (TEXT(flag)->bv_owner)
149 owner->view()->toggleToggle();
151 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
152 owner->view()->showCursor();
154 /* ---> Everytime the cursor is moved, show the current font state. */
155 // should this too me moved out of this func?
156 //owner->showState();
157 owner->view()->setState();
161 void LyXFunc::handleKeyFunc(kb_action action)
165 if (keyseq.length == -1 && keyseq.getiso() != 0)
167 owner->getIntl()->getTrans()
168 .deadkey(c, get_accent(action).accent, TEXT(false));
169 // Need to reset, in case the minibuffer calls these
173 // copied verbatim from do_accent_char
174 owner->view()->update(TEXT(false),
175 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
176 TEXT(false)->sel_cursor = TEXT(false)->cursor;
180 int LyXFunc::processKeySym(KeySym keysym, unsigned int state)
184 if (lyxerr.debugging(Debug::KEY)) {
185 char * tmp = XKeysymToString(keysym);
186 string const stm = (tmp ? tmp : "");
187 lyxerr << "KeySym is "
190 << keysym << "] State is ["
194 // Do nothing if we have nothing (JMarc)
195 if (keysym == NoSymbol) {
196 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
202 if (owner->view()->available()) {
203 // this function should be used always [asierra060396]
204 UpdatableInset * tli = owner->view()->theLockingInset();
205 if (tli && (keysym == XK_Escape)) {
206 if (tli == tli->GetLockingInset()) {
207 owner->view()->unlockInset(tli);
208 TEXT()->CursorRight(owner->view());
209 moveCursorUpdate(true, false);
212 tli->UnlockInsetInInset(owner->view(),
213 tli->GetLockingInset(),true);
220 // Can we be sure that this will work for all X-Windows
221 // implementations? (Lgb)
222 // This code snippet makes lyx ignore some keys. Perhaps
223 // all of them should be explictly mentioned?
224 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
225 || keysym == XK_Mode_switch || keysym == 0x0)
228 // Do a one-deep top-level lookup for
229 // cancel and meta-fake keys. RVDK_PATCH_5
230 cancel_meta_seq.reset();
232 int action = cancel_meta_seq.addkey(keysym, state
233 &(ShiftMask|ControlMask
235 if (lyxerr.debugging(Debug::KEY)) {
236 lyxerr << "action first set to [" << action << "]" << endl;
239 // When not cancel or meta-fake, do the normal lookup.
240 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
241 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
242 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
243 if (lyxerr.debugging(Debug::KEY)) {
244 lyxerr << "meta_fake_bit is [" << meta_fake_bit << "]" << endl;
246 // remove Caps Lock and Mod2 as a modifiers
247 action = keyseq.addkey(keysym,
248 (state | meta_fake_bit)
249 &(ShiftMask|ControlMask
251 if (lyxerr.debugging(Debug::KEY)) {
252 lyxerr << "action now set to [" << action << "]" << endl;
255 // Dont remove this unless you know what you are doing.
258 if (action == 0) action = LFUN_PREFIX;
260 if (lyxerr.debugging(Debug::KEY)) {
269 // already here we know if it any point in going further
270 // why not return already here if action == -1 and
271 // num_bytes == 0? (Lgb)
273 if (keyseq.length > 1 || keyseq.length < -1) {
276 owner->getMiniBuffer()->Set(buf);
280 if (keyseq.length < -1) { // unknown key sequence...
283 owner->getMiniBuffer()->Set(_("Unknown sequence:"), buf);
287 char const isochar = keyseq.getiso();
288 if (!(state & ControlMask) &&
289 !(state & Mod1Mask) &&
290 (isochar && keysym < 0xF000)) {
293 if (argument.empty()) {
294 lyxerr.debug() << "Empty argument!" << endl;
295 // This can`t possibly be of any use
296 // so we`ll skip the dispatch.
299 } else if (action == LFUN_SELFINSERT) {
300 // We must set the argument to the char looked up by
305 xke.send_event = False;
306 xke.display = fl_get_display();
316 xke.keycode = XKeysymToKeycode(fl_get_display(), keysym);
317 xke.same_screen = True;
320 int res = XLookupString(&xke, ret, 10, &tmpkeysym, 0);
321 //Assert(keysym == tmpkeysym);
322 lyxerr[Debug::KEY] << "TmpKeysym ["
323 << tmpkeysym << "]" << endl;
326 argument = string(ret, res);
327 lyxerr[Debug::KEY] << "SelfInsert arg["
328 << argument << "]" << endl;
332 bool tmp_sc = show_sc;
334 Dispatch(action, argument);
340 LyXFunc::func_status LyXFunc::getStatus(int ac) const
342 return getStatus(ac, string());
345 LyXFunc::func_status LyXFunc::getStatus(int ac, string const & not_to_use_arg) const
348 func_status flag = LyXFunc::OK;
350 Buffer * buf = owner->buffer();
352 if (lyxaction.isPseudoAction(ac))
353 action = lyxaction.retrieveActionArg(ac, argument);
355 action = static_cast<kb_action>(ac);
356 if (!not_to_use_arg.empty())
357 argument = not_to_use_arg; // exept here
360 if (action == LFUN_UNKNOWN_ACTION) {
361 setErrorMessage(N_("Unknown action"));
362 return LyXFunc::Unknown;
365 // Check whether we need a buffer
366 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
367 // Yes we need a buffer, do we have one?
370 // Can we use a readonly buffer?
371 if (buf->isReadonly() &&
372 !lyxaction.funcHasFlag(action,
373 LyXAction::ReadOnly)) {
375 setErrorMessage(N_("Document is read-only"));
376 flag |= LyXFunc::Disabled;
380 setErrorMessage(N_("Command not allowed with"
381 "out any document open"));
382 flag |= LyXFunc::Disabled;
387 // I would really like to avoid having this switch and rather try to
388 // encode this in the function itself.
389 bool disable = false;
392 disable = !Exporter::IsExportable(buf, "dvi")
393 || lyxrc.print_command == "none";
396 disable = argument == "fax" &&
397 !Exporter::IsExportable(buf, argument);
400 disable = buf->undostack.empty();
403 disable = buf->redostack.empty();
405 case LFUN_SPELLCHECK:
406 disable = lyxrc.isp_command == "none";
409 disable = lyxrc.chktex_command == "none";
412 disable = !Exporter::IsExportable(buf, "program");
415 case LFUN_INSERTFOOTNOTE:
417 case LFUN_MARGINMELT:
418 // Disable insertion of floats in a tabular.
420 if (owner->view()->theLockingInset()) {
421 disable = (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) ||
422 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
426 case LFUN_LAYOUT_TABULAR:
428 if (owner->view()->theLockingInset()) {
429 disable = (owner->view()->theLockingInset()->LyxCode() != Inset::TABULAR_CODE) &&
430 !owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
434 case LFUN_TABULAR_FEATURE:
436 if (owner->view()->theLockingInset()) {
437 func_status ret = LyXFunc::Disabled;
438 if (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) {
439 ret = static_cast<InsetTabular *>
440 (owner->view()->theLockingInset())->
442 } else if (owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
443 ret = static_cast<InsetTabular *>
444 (owner->view()->theLockingInset()->
445 GetFirstLockingInsetOfType(Inset::TABULAR_CODE))->
451 static InsetTabular inset(*owner->buffer(), 1, 1);
455 ret = inset.getStatus(argument);
456 if ((ret & LyXFunc::ToggleOn) ||
457 (ret & LyXFunc::ToggleOff))
458 flag |= LyXFunc::ToggleOff;
462 case LFUN_VC_REGISTER:
463 disable = buf->lyxvc.inUse();
465 case LFUN_VC_CHECKIN:
466 disable = !buf->lyxvc.inUse() || buf->isReadonly();
468 case LFUN_VC_CHECKOUT:
469 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
473 case LFUN_VC_HISTORY:
474 disable = !buf->lyxvc.inUse();
476 case LFUN_BOOKMARK_GOTO:
477 disable = !owner->view()->
478 isSavedPosition(strToUnsignedInt(argument));
483 flag |= LyXFunc::Disabled;
486 func_status box = LyXFunc::ToggleOff;
487 LyXFont const & font =
488 TEXT(false)->real_current_font;
491 if (font.emph() == LyXFont::ON)
492 box = LyXFunc::ToggleOn;
495 if (font.noun() == LyXFont::ON)
496 box = LyXFunc::ToggleOn;
499 if (font.series() == LyXFont::BOLD_SERIES)
500 box = LyXFunc::ToggleOn;
503 if (font.latex() == LyXFont::ON)
504 box = LyXFunc::ToggleOn;
517 string const LyXFunc::Dispatch(string const & s)
519 // Split command string into command and argument
521 string line = frontStrip(s);
522 string arg = strip(frontStrip(split(line, cmd, ' ')));
524 return Dispatch(lyxaction.LookupFunc(cmd), arg);
528 string const LyXFunc::Dispatch(int ac,
529 string const & do_not_use_this_arg)
531 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
532 <<"] arg[" << do_not_use_this_arg << "]" << endl;
537 // we have not done anything wrong yet.
539 dispatch_buffer.erase();
541 // if action is a pseudo-action, we need the real action
542 if (lyxaction.isPseudoAction(ac)) {
544 action = static_cast<kb_action>
545 (lyxaction.retrieveActionArg(ac, tmparg));
549 action = static_cast<kb_action>(ac);
550 if (!do_not_use_this_arg.empty())
551 argument = do_not_use_this_arg; // except here
554 selection_possible = false;
556 if (owner->view()->available())
557 owner->view()->hideCursor();
559 // We cannot use this function here
560 if (getStatus(ac, do_not_use_this_arg) & Disabled)
561 goto exit_with_message;
563 commandshortcut.erase();
565 if (lyxrc.display_shortcuts && show_sc) {
566 if (action != LFUN_SELFINSERT) {
567 // Put name of command and list of shortcuts
568 // for it in minibuffer
569 string comname = lyxaction.getActionName(action);
571 int pseudoaction = action;
572 bool argsadded = false;
574 if (!argument.empty()) {
575 // If we have the command with argument,
578 lyxaction.searchActionArg(action,
581 if (pseudoaction == -1) {
582 pseudoaction = action;
584 comname += " " + argument;
589 string const shortcuts =
590 toplevel_keymap->findbinding(pseudoaction);
592 if (!shortcuts.empty()) {
593 comname += ": " + shortcuts;
594 } else if (!argsadded) {
595 comname += " " + argument;
598 if (!comname.empty()) {
599 comname = strip(comname);
600 commandshortcut = "(" + comname + ')';
601 owner->getMiniBuffer()->Set(commandshortcut);
602 // Here we could even add a small pause,
603 // to annoy the user and make him learn
605 // No! That will just annoy, not teach
606 // anything. The user will read the messages
607 // if they are interested. (Asger)
612 if (owner->view()->available() && owner->view()->theLockingInset()) {
613 UpdatableInset::RESULT result;
614 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
615 (keyseq.length >= -1)))
617 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
618 argument = keyseq.getiso();
620 // Undo/Redo pre 0.13 is a bit tricky for insets.
621 if (action == LFUN_UNDO) {
624 UpdatableInset * inset =
625 owner->view()->theLockingInset();
626 inset->GetCursorPos(owner->view(), slx, sly);
627 owner->view()->unlockInset(inset);
628 owner->view()->menuUndo();
629 if (TEXT()->cursor.par()->
630 IsInset(TEXT()->cursor.pos())) {
631 inset = static_cast<UpdatableInset*>(
632 TEXT()->cursor.par()->
639 inset->Edit(owner->view(),slx,sly,0);
641 } else if (action == LFUN_REDO) {
644 UpdatableInset * inset = owner->view()->
646 inset->GetCursorPos(owner->view(), slx, sly);
647 owner->view()->unlockInset(inset);
648 owner->view()->menuRedo();
649 inset = static_cast<UpdatableInset*>(
650 TEXT()->cursor.par()->
654 inset->Edit(owner->view(),slx,sly,0);
656 } else if (((result=owner->view()->theLockingInset()->
657 LocalDispatch(owner->view(), action,
659 UpdatableInset::DISPATCHED) ||
660 (result == UpdatableInset::DISPATCHED_NOUPDATE))
663 //setMessage(N_("Text mode"));
665 case LFUN_UNKNOWN_ACTION:
666 case LFUN_BREAKPARAGRAPH:
668 TEXT()->CursorRight(owner->view());
669 owner->view()->setState();
673 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
674 TEXT()->CursorRight(owner->view());
675 moveCursorUpdate(true, false);
680 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
681 TEXT()->CursorRight(owner->view());
682 moveCursorUpdate(true, false);
687 TEXT()->CursorDown(owner->view());
688 moveCursorUpdate(true, false);
698 Assert(action != LFUN_SELECT_FILE_SYNC);
701 // --- Misc -------------------------------------------
702 case LFUN_WORDFINDFORWARD :
703 case LFUN_WORDFINDBACKWARD : {
704 static string last_search;
705 string searched_string;
707 if (!argument.empty()) {
708 last_search = argument;
709 searched_string = argument;
711 searched_string = last_search;
714 if (!searched_string.empty() &&
715 ((action == LFUN_WORDFINDBACKWARD) ?
716 SearchBackward(owner->view(), searched_string) :
717 SearchForward(owner->view(), searched_string))) {
719 // ??? What is that ???
720 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
723 // clear the selection (if there is any)
724 owner->view()->toggleSelection();
725 TEXT()->ClearSelection(owner->view());
727 // Move cursor so that successive C-s 's will not stand in place.
728 if (action == LFUN_WORDFINDFORWARD )
729 TEXT()->CursorRightOneWord(owner->view());
730 TEXT()->FinishUndo();
731 moveCursorUpdate(true, false);
734 // set the new selection
735 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
736 owner->view()->toggleSelection(false);
739 // REMOVED : if (owner->view()->getWorkArea()->focus)
740 owner->view()->showCursor();
746 if (owner->view()->available()) {
747 owner->view()->update(TEXT(),
748 BufferView::SELECT|BufferView::FITCUR);
751 keyseq.print(buf, true);
752 owner->getMiniBuffer()->Set(buf, string(), string(), 1);
756 // --- Misc -------------------------------------------
757 case LFUN_EXEC_COMMAND:
758 owner->getMiniBuffer()->PrepareForCommand();
761 case LFUN_CANCEL: // RVDK_PATCH_5
764 if (owner->view()->available())
765 // cancel any selection
766 Dispatch(LFUN_MARK_OFF);
767 setMessage(N_("Cancel"));
770 case LFUN_META_FAKE: // RVDK_PATCH_5
772 meta_fake_bit = Mod1Mask;
774 keyseq.print(buf, true);
775 setMessage(buf); // RVDK_PATCH_5
779 case LFUN_READ_ONLY_TOGGLE:
780 if (owner->buffer()->lyxvc.inUse()) {
781 owner->buffer()->lyxvc.toggleReadOnly();
783 owner->buffer()->setReadonly(
784 !owner->buffer()->isReadonly());
788 case LFUN_CENTER: // this is center and redraw.
789 owner->view()->center();
792 // --- Menus -----------------------------------------------
797 case LFUN_MENUNEWTMPLT:
801 case LFUN_CLOSEBUFFER:
806 if (!owner->buffer()->isUnnamed()) {
807 owner->getMiniBuffer()->Set(_("Saving document"),
808 MakeDisplayPath(owner->buffer()->fileName()),
810 MenuWrite(owner->view(), owner->buffer());
812 WriteAs(owner->view(), owner->buffer());
816 WriteAs(owner->view(), owner->buffer(), argument);
819 case LFUN_MENURELOAD:
824 Exporter::Export(owner->buffer(), argument, true);
828 Exporter::Preview(owner->buffer(), argument);
832 Exporter::Export(owner->buffer(), "program", true);
836 MenuRunChktex(owner->buffer());
840 owner->getDialogs()->showPrint();
844 if (argument == "custom")
847 Exporter::Export(owner->buffer(), argument, false);
863 InsetCommandParams p;
865 if (action == LFUN_TOCVIEW )
866 p.setCmdName( "tableofcontents" );
867 else if (action == LFUN_LOAVIEW )
868 p.setCmdName( "listofalgorithms" );
869 else if (action == LFUN_LOFVIEW )
870 p.setCmdName( "listoffigures" );
872 p.setCmdName( "listoftables" );
874 owner->getDialogs()->createTOC( p.getAsString() );
878 case LFUN_DIALOG_TABULAR_INSERT:
879 owner->getDialogs()->showTabularCreate();
887 AutoSave(owner->view());
891 owner->view()->menuUndo();
895 owner->view()->menuRedo();
898 case LFUN_MENUSEARCH:
899 owner->getDialogs()->showSearch();
902 case LFUN_REMOVEERRORS:
903 if (owner->view()->removeAutoInsets()) {
904 owner->view()->redraw();
905 owner->view()->fitCursor(TEXT());
911 owner->view()->openStuff();
915 case LFUN_HYPHENATION:
916 owner->view()->hyphenationPoint();
920 owner->view()->ldots();
923 case LFUN_END_OF_SENTENCE:
924 owner->view()->endOfSentenceDot();
927 case LFUN_MENU_SEPARATOR:
928 owner->view()->menuSeparator();
932 owner->view()->hfill();
936 changeDepth(owner->view(), TEXT(false), 0);
940 changeDepth(owner->view(), TEXT(false), -1);
943 case LFUN_DEPTH_PLUS:
944 changeDepth(owner->view(), TEXT(false), 1);
948 owner->getDialogs()->setUserFreeFont();
953 owner->view()->setState();
961 case LFUN_RECONFIGURE:
962 Reconfigure(owner->view());
966 if (owner->view()->available()
967 && !TEXT()->selection
968 && TEXT()->cursor.par()->footnoteflag
969 != LyXParagraph::NO_FOOTNOTE)
970 { // only melt footnotes with FOOTMELT, not margins etc
971 if (TEXT()->cursor.par()->footnotekind == LyXParagraph::FOOTNOTE)
976 owner->view()->setState();
979 case LFUN_MARGINMELT:
980 if (owner->view()->available()
981 && !TEXT()->selection
982 && TEXT()->cursor.par()->footnoteflag
983 != LyXParagraph::NO_FOOTNOTE) {
985 if (TEXT()->cursor.par()->footnotekind == LyXParagraph::MARGIN)
988 Margin(owner->view());
989 owner->view()->setState();
993 case LFUN_FLOATSOPERATE:
994 if (argument == "openfoot")
995 owner->view()->allFloats(1,0);
996 else if (argument == "closefoot")
997 owner->view()->allFloats(0,0);
998 else if (argument == "openfig")
999 owner->view()->allFloats(1,1);
1000 else if (argument == "closefig")
1001 owner->view()->allFloats(0,1);
1004 #warning Find another implementation here (or another lyxfunc)!
1006 case LFUN_HELP_COPYRIGHT:
1007 owner->getDialogs()->showCopyright();
1010 case LFUN_HELP_CREDITS:
1011 owner->getDialogs()->showCredits();
1014 case LFUN_HELP_OPEN: {
1015 string arg = argument;
1017 setErrorMessage(N_("Missing argument"));
1020 ProhibitInput(owner->view());
1021 string fname = i18nLibFileSearch("doc", arg, "lyx");
1022 if (fname.empty()) {
1023 lyxerr << "LyX: unable to find documentation file `"
1024 << arg << "'. Bad installation?" << endl;
1025 AllowInput(owner->view());
1028 owner->getMiniBuffer()->Set(_("Opening help file"),
1029 MakeDisplayPath(fname),"...");
1030 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1031 AllowInput(owner->view());
1035 case LFUN_HELP_VERSION: {
1036 ProhibitInput(owner->view());
1037 string msg(_("LyX Version "));
1041 fl_show_message(msg.c_str(),
1042 (_("Library directory: ")
1043 + MakeDisplayPath(system_lyxdir)).c_str(),
1044 (_("User directory: ")
1045 + MakeDisplayPath(user_lyxdir)).c_str());
1046 AllowInput(owner->view());
1050 // --- version control -------------------------------
1051 case LFUN_VC_REGISTER:
1053 if (!owner->buffer()->lyxvc.inUse())
1054 owner->buffer()->lyxvc.registrer();
1058 case LFUN_VC_CHECKIN:
1060 if (owner->buffer()->lyxvc.inUse()
1061 && !owner->buffer()->isReadonly())
1062 owner->buffer()->lyxvc.checkIn();
1066 case LFUN_VC_CHECKOUT:
1068 if (owner->buffer()->lyxvc.inUse()
1069 && owner->buffer()->isReadonly())
1070 owner->buffer()->lyxvc.checkOut();
1074 case LFUN_VC_REVERT:
1076 owner->buffer()->lyxvc.revert();
1082 owner->buffer()->lyxvc.undoLast();
1086 case LFUN_VC_HISTORY:
1088 owner->getDialogs()->showVCLogFile();
1092 // --- buffers ----------------------------------------
1094 case LFUN_SWITCHBUFFER:
1095 owner->view()->buffer(bufferlist.getBuffer(argument));
1100 // servercmd: argument must be <file>:<template>
1101 Buffer * tmpbuf = NewLyxFile(argument);
1103 owner->view()->buffer(tmpbuf);
1107 case LFUN_FILE_OPEN:
1111 case LFUN_LATEX_LOG:
1112 owner->getDialogs()->showLogFile();
1117 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1118 int sel = strToInt(argument);
1119 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1121 // Should this give a setMessage instead?
1123 return string(); // illegal argument
1125 --sel; // sel 1..., but layout 0...
1127 // Pretend we got the name instead.
1128 Dispatch(int(LFUN_LAYOUT),
1129 textclasslist.NameOfLayout(owner->view()
1130 ->buffer()->params.textclass,
1135 case LFUN_LAYOUT_DOCUMENT:
1136 owner->getDialogs()->showLayoutDocument();
1139 case LFUN_LAYOUT_PARAGRAPH:
1140 owner->getDialogs()->showLayoutParagraph();
1143 case LFUN_LAYOUT_CHARACTER:
1144 owner->getDialogs()->showLayoutCharacter();
1147 case LFUN_LAYOUT_TABULAR:
1148 if (owner->view()->theLockingInset()) {
1149 if (owner->view()->theLockingInset()->LyxCode()==Inset::TABULAR_CODE) {
1150 InsetTabular * inset = static_cast<InsetTabular *>
1151 (owner->view()->theLockingInset());
1152 inset->OpenLayoutDialog(owner->view());
1153 } else if (owner->view()->theLockingInset()->
1154 GetFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1155 InsetTabular * inset = static_cast<InsetTabular *>(
1156 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE));
1157 inset->OpenLayoutDialog(owner->view());
1162 case LFUN_LAYOUT_PREAMBLE:
1163 owner->getDialogs()->showPreamble();
1166 case LFUN_LAYOUT_SAVE_DEFAULT:
1167 MenuLayoutSave(owner->view());
1170 case LFUN_DROP_LAYOUTS_CHOICE:
1171 owner->getToolbar()->openLayoutList();
1174 case LFUN_MENU_OPEN_BY_NAME:
1175 owner->getMenubar()->openByName(argument);
1176 break; // RVDK_PATCH_5
1178 case LFUN_SPELLCHECK:
1179 if (lyxrc.isp_command != "none")
1180 ShowSpellChecker(owner->view());
1181 break; // RVDK_PATCH_5
1183 // --- lyxserver commands ----------------------------
1187 setMessage(owner->buffer()->fileName());
1188 lyxerr.debug() << "FNAME["
1189 << owner->buffer()->fileName()
1197 dispatch_buffer = buf;
1198 lyxserver->notifyClient(dispatch_buffer);
1202 case LFUN_GOTOFILEROW:
1204 char file_name[100];
1206 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1208 // Must replace extension of the file to be .lyx and get full path
1209 string s = ChangeExtension(string(file_name), ".lyx");
1211 // Either change buffer or load the file
1212 if (bufferlist.exists(s))
1213 owner->view()->buffer(bufferlist.getBuffer(s));
1215 owner->view()->buffer(bufferlist.loadLyXFile(s));
1218 owner->view()->setCursorFromRow(row);
1221 owner->view()->center();
1225 case LFUN_GOTO_PARAGRAPH:
1227 istringstream istr(argument.c_str());
1231 LyXParagraph * par = TEXT()->GetParFromID(id);
1234 TEXT()->SetCursor(owner->view(), par, 0);
1235 owner->view()->setState();
1239 owner->view()->center();
1246 int const qa = lyxaction.LookupFunc(argument);
1247 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1251 // --- toolbar ----------------------------------
1252 case LFUN_PUSH_TOOLBAR:
1254 int nth = strToInt(argument);
1256 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1258 owner->getToolbar()->push(nth);
1263 case LFUN_ADD_TO_TOOLBAR:
1265 if (lyxerr.debugging(Debug::GUI)) {
1266 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1267 "argument = `" << argument << '\'' << endl;
1269 string tmp(argument);
1270 //lyxerr <<string("Argument: ") + argument);
1271 //lyxerr <<string("Tmp : ") + tmp);
1273 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1275 owner->getToolbar()->add(argument, false);
1276 owner->getToolbar()->set();
1281 // --- insert characters ----------------------------------------
1283 // --- Mathed stuff. If we are here, there is no locked inset yet.
1288 if (!greek_kb_flag) {
1290 setMessage(N_("Math greek mode on"));
1297 case LFUN_GREEK_TOGGLE:
1299 greek_kb_flag = greek_kb_flag ? 0 : 2;
1300 if (greek_kb_flag) {
1301 setMessage(N_("Math greek keyboard on"));
1303 setMessage(N_("Math greek keyboard off"));
1308 case LFUN_MATH_NUMBER:
1309 case LFUN_MATH_LIMITS:
1311 setErrorMessage(N_("This is only allowed in math mode!"));
1316 case LFUN_MATH_PANEL:
1318 show_symbols_form(this);
1322 case LFUN_CITATION_CREATE:
1324 InsetCommandParams p( "cite" );
1326 if (!argument.empty()) {
1327 // This should be set at source, ie when typing
1328 // "citation-insert foo" in the minibuffer.
1329 // Question: would pybibliographer also need to be
1330 // changed. Suspect so. Leave as-is therefore.
1331 if (contains(argument, "|")) {
1332 p.setContents( token(argument, '|', 0) );
1333 p.setOptions( token(argument, '|', 1) );
1335 p.setContents( argument );
1337 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1339 owner->getDialogs()->createCitation( p.getAsString() );
1343 case LFUN_CHILDOPEN:
1345 string const filename =
1346 MakeAbsPath(argument,
1347 OnlyPath(owner->buffer()->fileName()));
1348 setMessage(N_("Opening child document ") +
1349 MakeDisplayPath(filename) + "...");
1350 owner->view()->savePosition(0);
1351 if (bufferlist.exists(filename))
1352 owner->view()->buffer(bufferlist.getBuffer(filename));
1354 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1359 case LFUN_INSERTFOOTNOTE:
1361 LyXParagraph::footnote_kind kind;
1362 if (argument == "footnote") {
1363 kind = LyXParagraph::FOOTNOTE;
1364 } else if (argument == "margin") {
1365 kind = LyXParagraph::MARGIN;
1366 } else if (argument == "figure") {
1367 kind = LyXParagraph::FIG;
1368 } else if (argument == "table") {
1369 kind = LyXParagraph::TAB;
1370 } else if (argument == "wide-fig") {
1371 kind = LyXParagraph::WIDE_FIG;
1372 } else if (argument == "wide-tab") {
1373 kind = LyXParagraph::WIDE_TAB;
1374 } else if (argument == "algorithm") {
1375 kind = LyXParagraph::ALGORITHM;
1377 setErrorMessage(N_("Unknown kind of footnote"));
1380 TEXT()->InsertFootnoteEnvironment(owner->view(), kind);
1381 owner->view()->update(TEXT(),
1382 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1383 owner->view()->setState();
1387 case LFUN_TOGGLECURSORFOLLOW:
1388 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1391 case LFUN_KMAP_OFF: // keymap off
1392 owner->getIntl()->KeyMapOn(false);
1395 case LFUN_KMAP_PRIM: // primary keymap
1396 owner->getIntl()->KeyMapPrim();
1399 case LFUN_KMAP_SEC: // secondary keymap
1400 owner->getIntl()->KeyMapSec();
1403 case LFUN_KMAP_TOGGLE: // toggle keymap
1404 owner->getIntl()->ToggleKeyMap();
1409 // argument contains ';'-terminated commands
1410 while (argument.find(';') != string::npos) {
1412 argument = split(argument, first, ';');
1418 case LFUN_DIALOG_PREFERENCES:
1419 owner->getDialogs()->showPreferences();
1422 case LFUN_SAVEPREFERENCES:
1424 Path p(user_lyxdir);
1425 lyxrc.write("preferences");
1429 case LFUN_SCREEN_FONT_UPDATE:
1431 // handle the screen font changes.
1433 lyxrc.set_font_norm_type();
1434 fontloader.update();
1435 // Of course we should only do the resize and the textcache.clear
1436 // if values really changed...but not very important right now. (Lgb)
1437 // All buffers will need resize
1438 bufferlist.resize();
1439 // We also need to empty the textcache so that
1440 // the buffer will be formatted correctly after
1446 case LFUN_SET_COLOR:
1449 string const x11_name = split(argument, lyx_name, ' ');
1450 if (lyx_name.empty() || x11_name.empty()) {
1451 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1456 if (!lcolor.setColor(lyx_name, x11_name)) {
1457 static string const err1 (N_("Set-color \""));
1458 static string const err2 (
1459 N_("\" failed - color is undefined "
1460 "or may not be redefined"));
1461 setErrorMessage(_(err1) + lyx_name + _(err2));
1464 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1465 owner->view()->redraw();
1470 // Then if it was none of the above
1471 if (!owner->view()->Dispatch(action, argument))
1472 lyxerr << "A truly unknown func!" << endl;
1478 string const res = getMessage();
1481 if (!commandshortcut.empty()) {
1482 string const newbuf =
1483 owner->getMiniBuffer()->GetText();
1484 if (newbuf != commandshortcut) {
1485 owner->getMiniBuffer()->Set(newbuf
1493 msg += commandshortcut;
1494 owner->getMiniBuffer()->Set(msg);
1501 void LyXFunc::setupLocalKeymap()
1503 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1504 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1508 void LyXFunc::MenuNew(bool fromTemplate)
1510 string initpath = lyxrc.document_path;
1512 if (owner->view()->available()) {
1513 string const trypath = owner->buffer()->filepath;
1514 // If directory is writeable, use this as default.
1515 if (IsDirWriteable(trypath) == 1)
1519 static int newfile_number = 0;
1522 if (lyxrc.new_ask_filename) {
1523 FileDialog fileDlg(owner, _("Enter filename for new document"),
1524 LFUN_SELECT_FILE_SYNC,
1525 make_pair(string(_("Documents")), string(lyxrc.document_path)),
1526 make_pair(string(_("Templates")), string(lyxrc.template_path)));
1528 FileDialog::Result result = fileDlg.Select(initpath, _("*.lyx|LyX Documents (*.lyx)"), _("newfile"));
1530 if (result.second.empty()) {
1531 owner->getMiniBuffer()->Set(_("Canceled."));
1532 lyxerr.debug() << "New Document Cancelled." << endl;
1536 // get absolute path of file and make sure the filename ends
1538 s = MakeAbsPath(result.second);
1539 if (!IsLyXFilename(s))
1542 // Check if the document already is open
1543 if (bufferlist.exists(s)) {
1544 switch (AskConfirmation(_("Document is already open:"),
1545 MakeDisplayPath(s, 50),
1546 _("Do you want to close that document now?\n"
1547 "('No' will just switch to the open version)")))
1549 case 1: // Yes: close the document
1550 if (!bufferlist.close(bufferlist.getBuffer(s)))
1551 // If close is canceled, we cancel here too.
1554 case 2: // No: switch to the open document
1555 owner->view()->buffer(bufferlist.getBuffer(s));
1557 case 3: // Cancel: Do nothing
1558 owner->getMiniBuffer()->Set(_("Canceled."));
1562 // Check whether the file already exists
1563 if (IsLyXFilename(s)) {
1565 if (fi.readable() &&
1566 AskQuestion(_("File already exists:"),
1567 MakeDisplayPath(s, 50),
1568 _("Do you want to open the document?"))) {
1570 owner->getMiniBuffer()->Set(_("Opening document"),
1571 MakeDisplayPath(s), "...");
1572 XFlush(fl_get_display());
1573 owner->view()->buffer(
1574 bufferlist.loadLyXFile(s));
1575 owner->getMiniBuffer()->Set(_("Document"),
1582 s = AddName(lyxrc.document_path,
1583 "newfile" + tostr(++newfile_number) + ".lyx");
1585 while (bufferlist.exists(s) || fi.readable()) {
1587 s = AddName(lyxrc.document_path,
1588 "newfile" + tostr(newfile_number) +
1594 // The template stuff
1597 FileDialog fileDlg(owner, _("Select template file"),
1598 LFUN_SELECT_FILE_SYNC,
1599 make_pair(string(_("Documents")), string(lyxrc.document_path)),
1600 make_pair(string(_("Templates")), string(lyxrc.template_path)));
1602 FileDialog::Result result = fileDlg.Select(initpath, _("*.lyx|LyX Documents (*.lyx)"));
1604 if (result.first == FileDialog::Later)
1607 string const fname = result.second;
1614 // find a free buffer
1615 lyxerr.debug() << "Find a free buffer." << endl;
1616 owner->view()->buffer(bufferlist.newFile(s, templname));
1620 void LyXFunc::Open(string const & fname)
1622 string initpath = lyxrc.document_path;
1624 if (owner->view()->available()) {
1625 string const trypath = owner->buffer()->filepath;
1626 // If directory is writeable, use this as default.
1627 if (IsDirWriteable(trypath) == 1)
1633 if (fname.empty()) {
1634 FileDialog fileDlg(owner, _("Select document to open"),
1636 make_pair(string(_("Documents")), string(lyxrc.document_path)),
1637 make_pair(string(_("Examples")), string(AddPath(system_lyxdir, "examples"))));
1639 FileDialog::Result result = fileDlg.Select(initpath, "*.lyx|LyX Documents (*.lyx)");
1641 if (result.first == FileDialog::Later)
1644 filename = result.second;
1646 // check selected filename
1647 if (filename.empty()) {
1648 owner->getMiniBuffer()->Set(_("Canceled."));
1654 // get absolute path of file and make sure the filename ends
1656 filename = MakeAbsPath(filename);
1657 if (!IsLyXFilename(filename))
1661 owner->getMiniBuffer()->Set(_("Opening document"),
1662 MakeDisplayPath(filename), "...");
1663 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1665 owner->view()->buffer(openbuf);
1666 owner->getMiniBuffer()->Set(_("Document"),
1667 MakeDisplayPath(filename),
1670 owner->getMiniBuffer()->Set(_("Could not open document"),
1671 MakeDisplayPath(filename));
1676 // checks for running without gui are missing.
1677 void LyXFunc::doImport(string const & argument)
1680 string filename = split(argument, format, ' ');
1681 lyxerr.debug() << "LyXFunc::doImport: " << format
1682 << " file: " << filename << endl;
1684 if (filename.empty()) { // need user interaction
1685 string initpath = lyxrc.document_path;
1687 if (owner->view()->available()) {
1688 string const trypath = owner->buffer()->filepath;
1689 // If directory is writeable, use this as default.
1690 if (IsDirWriteable(trypath) == 1)
1694 string const text = _("Select ") + formats.PrettyName(format)
1695 + _(" file to import");
1697 FileDialog fileDlg(owner, text,
1699 make_pair(string(_("Documents")), string(lyxrc.document_path)),
1700 make_pair(string(_("Examples")), string(AddPath(system_lyxdir, "examples"))));
1702 string const extension = "*." + formats.Extension(format) + "| " +
1703 formats.PrettyName(format) + " (*." + formats.Extension(format) + ")";
1705 FileDialog::Result result = fileDlg.Select(initpath, extension);
1707 if (result.first == FileDialog::Later)
1710 filename = result.second;
1712 // check selected filename
1713 if (filename.empty())
1714 owner->getMiniBuffer()->Set(_("Canceled."));
1717 // still no filename? abort
1718 if (filename.empty())
1721 // get absolute path of file
1722 filename = MakeAbsPath(filename);
1724 string const lyxfile = ChangeExtension(filename, ".lyx");
1726 // Check if the document already is open
1727 if (bufferlist.exists(lyxfile)) {
1728 switch (AskConfirmation(_("Document is already open:"),
1729 MakeDisplayPath(lyxfile, 50),
1730 _("Do you want to close that document now?\n"
1731 "('No' will just switch to the open version)")))
1733 case 1: // Yes: close the document
1734 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1735 // If close is canceled, we cancel here too.
1738 case 2: // No: switch to the open document
1739 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1741 case 3: // Cancel: Do nothing
1742 owner->getMiniBuffer()->Set(_("Canceled."));
1747 // Check if a LyX document by the same root exists in filesystem
1748 FileInfo const f(lyxfile, true);
1749 if (f.exist() && !AskQuestion(_("A document by the name"),
1750 MakeDisplayPath(lyxfile),
1751 _("already exists. Overwrite?"))) {
1752 owner->getMiniBuffer()->Set(_("Canceled."));
1755 // filename should be valid now
1757 Importer::Import(owner, filename, format);
1761 void LyXFunc::reloadBuffer()
1763 string const fn = owner->buffer()->fileName();
1764 if (bufferlist.close(owner->buffer()))
1765 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1769 void LyXFunc::CloseBuffer()
1771 if (bufferlist.close(owner->buffer()) && !quitting) {
1772 if (bufferlist.empty()) {
1773 // need this otherwise SEGV may occur while trying to
1774 // set variables that don't exist
1775 // since there's no current buffer
1776 owner->getDialogs()->hideBufferDependent();
1778 owner->view()->buffer(bufferlist.first());
1784 // Each "owner" should have it's own message method. lyxview and
1785 // the minibuffer would use the minibuffer, but lyxserver would
1786 // send an ERROR signal to its client. Alejandro 970603
1787 // This func is bit problematic when it comes to NLS, to make the
1788 // lyx servers client be language indepenent we must not translate
1789 // strings sent to this func.
1790 void LyXFunc::setErrorMessage(string const & m) const
1792 dispatch_buffer = m;
1797 void LyXFunc::setMessage(string const & m)
1799 dispatch_buffer = m;