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
28 #include "support/lyxalgo.h"
32 #include "bufferlist.h"
33 #include "ColorHandler.h"
34 #include "lyxserver.h"
35 #include "figure_form.h"
39 #include "LyXAction.h"
40 #include "insets/inseturl.h"
41 #include "insets/insetlatexaccent.h"
42 #include "insets/insettoc.h"
43 #include "insets/insetref.h"
44 #include "insets/insetparent.h"
45 #include "insets/insetindex.h"
46 #include "insets/insetinclude.h"
47 #include "insets/insetbib.h"
48 #include "insets/insetcite.h"
49 #include "insets/insettext.h"
50 #include "insets/insetert.h"
51 #include "insets/insetexternal.h"
52 #include "insets/insetgraphics.h"
53 #include "insets/insetfoot.h"
54 #include "insets/insetmarginal.h"
55 #include "insets/insetminipage.h"
56 #include "insets/insetfloat.h"
57 #include "insets/insetlist.h"
58 #include "insets/insettabular.h"
59 #include "insets/insettheorem.h"
60 #include "insets/insetcaption.h"
61 #include "mathed/formulamacro.h"
62 #include "spellchecker.h" // RVDK_PATCH_5
63 #include "minibuffer.h"
66 #include "lyx_gui_misc.h"
67 #include "support/filetools.h"
68 #include "support/FileInfo.h"
69 #include "support/syscall.h"
70 #include "support/lstrings.h"
71 #include "support/path.h"
72 #include "support/lyxfunctional.h"
77 #include "trans_mgr.h"
80 #include "bufferview_funcs.h"
81 #include "frontends/FileDialog.h"
82 #include "frontends/Dialogs.h"
83 #include "frontends/Toolbar.h"
84 #include "frontends/Menubar.h"
85 #include "FloatList.h"
86 #include "converter.h"
89 #include "FontLoader.h"
90 #include "TextCache.h"
98 extern BufferList bufferlist;
99 extern LyXServer * lyxserver;
100 extern int greek_kb_flag;
101 extern bool selection_possible;
102 extern void MenuSendto();
104 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
106 extern void show_symbols_form(LyXFunc *);
108 extern LyXAction lyxaction;
110 extern tex_accent_struct get_accent(kb_action action);
112 extern LyXTextClass::size_type current_layout;
114 extern void ShowLatexLog();
120 class MiniBufferController : public SigC::Object {
123 MiniBufferController() {
127 &MiniBufferController::receiveCommand));
131 &MiniBufferController::receiveArg));
134 void receiveCmd(string const & cmd) {}
136 void receiveArg(string const & arg) {}
144 MiniBufferController mb_ctrl;
150 /* === globals =========================================================== */
152 // Initialization of static member var
153 bool LyXFunc::show_sc = true;
156 LyXFunc::LyXFunc(LyXView * o)
160 lyx_dead_action = LFUN_NOACTION;
161 lyx_calling_dead_action = LFUN_NOACTION;
167 LyXText * LyXFunc::TEXT(bool flag = true) const
170 return owner->view()->text;
171 return owner->view()->getLyXText();
175 // I changed this func slightly. I commented out the ...FinishUndo(),
176 // this means that all places that used to have a moveCursorUpdate, now
177 // have a ...FinishUndo() as the preceeding statement. I have also added
178 // a moveCursorUpdate to some of the functions that updated the cursor, but
179 // that did not show its new position.
181 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
183 if (selecting || TEXT(flag)->mark_set) {
184 TEXT(flag)->SetSelection(owner->view());
185 if (TEXT(flag)->bv_owner)
186 owner->view()->toggleToggle();
188 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
189 owner->view()->showCursor();
191 /* ---> Everytime the cursor is moved, show the current font state. */
192 // should this too me moved out of this func?
193 //owner->showState();
194 owner->view()->setState();
198 void LyXFunc::handleKeyFunc(kb_action action)
200 char c = keyseq.getiso();
202 if (keyseq.length != -1) c = 0;
204 owner->getIntl()->getTrans()
205 .deadkey(c, get_accent(action).accent, TEXT(false));
206 // Need to reset, in case the minibuffer calls these
210 // copied verbatim from do_accent_char
211 owner->view()->update(TEXT(false),
212 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
213 TEXT(false)->sel_cursor = TEXT(false)->cursor;
217 int LyXFunc::processKeySym(KeySym keysym, unsigned int state)
221 if (lyxerr.debugging(Debug::KEY)) {
222 char * tmp = XKeysymToString(keysym);
223 string const stm = (tmp ? tmp : "");
224 lyxerr << "KeySym is "
227 << keysym << "] State is ["
231 // Do nothing if we have nothing (JMarc)
232 if (keysym == NoSymbol) {
233 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
239 if (owner->view()->available()) {
240 // this function should be used always [asierra060396]
241 UpdatableInset * tli = owner->view()->theLockingInset();
242 if (tli && (keysym == XK_Escape)) {
243 if (tli == tli->GetLockingInset()) {
244 owner->view()->unlockInset(tli);
245 TEXT()->CursorRight(owner->view());
246 moveCursorUpdate(true, false);
249 tli->UnlockInsetInInset(owner->view(),
250 tli->GetLockingInset(),
258 // Can we be sure that this will work for all X-Windows
259 // implementations? (Lgb)
260 // This code snippet makes lyx ignore some keys. Perhaps
261 // all of them should be explictly mentioned?
262 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
263 || keysym == XK_Mode_switch || keysym == 0x0)
266 // Do a one-deep top-level lookup for
267 // cancel and meta-fake keys. RVDK_PATCH_5
268 cancel_meta_seq.reset();
270 int action = cancel_meta_seq.addkey(keysym, state
271 &(ShiftMask|ControlMask
273 if (lyxerr.debugging(Debug::KEY)) {
274 lyxerr << "action first set to [" << action << "]" << endl;
277 // When not cancel or meta-fake, do the normal lookup.
278 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
279 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
280 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
281 if (lyxerr.debugging(Debug::KEY)) {
282 lyxerr << "meta_fake_bit is ["
283 << meta_fake_bit << "]" << endl;
285 // remove Caps Lock and Mod2 as a modifiers
286 action = keyseq.addkey(keysym,
287 (state | meta_fake_bit)
288 &(ShiftMask|ControlMask
290 if (lyxerr.debugging(Debug::KEY)) {
291 lyxerr << "action now set to ["
292 << action << "]" << endl;
295 // Dont remove this unless you know what you are doing.
298 if (action == 0) action = LFUN_PREFIX;
300 if (lyxerr.debugging(Debug::KEY)) {
309 // already here we know if it any point in going further
310 // why not return already here if action == -1 and
311 // num_bytes == 0? (Lgb)
313 if (keyseq.length > 1 || keyseq.length < -1) {
321 if (keyseq.length < -1) { // unknown key sequence...
324 owner->message(_("Unknown sequence:") + ' ' + buf);
328 char const isochar = keyseq.getiso();
329 if (!(state & ControlMask) &&
330 !(state & Mod1Mask) &&
331 (isochar && keysym < 0xF000)) {
334 if (argument.empty()) {
335 lyxerr.debug() << "Empty argument!" << endl;
336 // This can`t possibly be of any use
337 // so we`ll skip the dispatch.
341 owner->message(_("Unknown function."));
344 } else if (action == LFUN_SELFINSERT) {
345 // We must set the argument to the char looked up by
350 xke.send_event = False;
351 xke.display = fl_get_display();
361 xke.keycode = XKeysymToKeycode(fl_get_display(), keysym);
362 xke.same_screen = True;
365 int const res = XLookupString(&xke, ret, 10, &tmpkeysym, 0);
366 //Assert(keysym == tmpkeysym);
367 lyxerr[Debug::KEY] << "TmpKeysym ["
368 << tmpkeysym << "]" << endl;
371 argument = string(ret, res);
372 lyxerr[Debug::KEY] << "SelfInsert arg["
373 << argument << "]" << endl;
377 bool tmp_sc = show_sc;
379 Dispatch(action, argument);
386 LyXFunc::func_status LyXFunc::getStatus(int ac) const
388 return getStatus(ac, string());
392 LyXFunc::func_status LyXFunc::getStatus(int ac,
393 string const & not_to_use_arg) const
396 func_status flag = LyXFunc::OK;
398 Buffer * buf = owner->buffer();
400 if (lyxaction.isPseudoAction(ac))
401 action = lyxaction.retrieveActionArg(ac, argument);
403 action = static_cast<kb_action>(ac);
404 if (!not_to_use_arg.empty())
405 argument = not_to_use_arg; // exept here
408 if (action == LFUN_UNKNOWN_ACTION) {
409 setErrorMessage(N_("Unknown action"));
410 return LyXFunc::Unknown;
413 // Check whether we need a buffer
414 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
415 // Yes we need a buffer, do we have one?
418 // Can we use a readonly buffer?
419 if (buf->isReadonly() &&
420 !lyxaction.funcHasFlag(action,
421 LyXAction::ReadOnly)) {
423 setErrorMessage(N_("Document is read-only"));
424 flag |= LyXFunc::Disabled;
428 setErrorMessage(N_("Command not allowed with"
429 "out any document open"));
430 flag |= LyXFunc::Disabled;
435 // I would really like to avoid having this switch and rather try to
436 // encode this in the function itself.
437 bool disable = false;
440 disable = !Exporter::IsExportable(buf, "dvi")
441 || lyxrc.print_command == "none";
444 disable = argument == "fax" &&
445 !Exporter::IsExportable(buf, argument);
448 disable = buf->undostack.empty();
451 disable = buf->redostack.empty();
453 case LFUN_SPELLCHECK:
454 disable = lyxrc.isp_command == "none";
457 disable = lyxrc.chktex_command == "none";
460 disable = !Exporter::IsExportable(buf, "program");
463 case LFUN_INSERTFOOTNOTE:
464 // Disable insertion of floats in a tabular.
466 if (owner->view()->theLockingInset()) {
467 disable = (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) ||
468 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
472 case LFUN_LAYOUT_TABULAR:
474 if (owner->view()->theLockingInset()) {
475 disable = (owner->view()->theLockingInset()->LyxCode() != Inset::TABULAR_CODE) &&
476 !owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
480 case LFUN_TABULAR_FEATURE:
482 if (owner->view()->theLockingInset()) {
483 func_status ret = LyXFunc::Disabled;
484 if (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) {
485 ret = static_cast<InsetTabular *>
486 (owner->view()->theLockingInset())->
488 } else if (owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
489 ret = static_cast<InsetTabular *>
490 (owner->view()->theLockingInset()->
491 GetFirstLockingInsetOfType(Inset::TABULAR_CODE))->
497 static InsetTabular inset(*owner->buffer(), 1, 1);
501 ret = inset.getStatus(argument);
502 if ((ret & LyXFunc::ToggleOn) ||
503 (ret & LyXFunc::ToggleOff))
504 flag |= LyXFunc::ToggleOff;
508 case LFUN_VC_REGISTER:
509 disable = buf->lyxvc.inUse();
511 case LFUN_VC_CHECKIN:
512 disable = !buf->lyxvc.inUse() || buf->isReadonly();
514 case LFUN_VC_CHECKOUT:
515 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
519 case LFUN_VC_HISTORY:
520 disable = !buf->lyxvc.inUse();
522 case LFUN_BOOKMARK_GOTO:
523 disable = !owner->view()->
524 isSavedPosition(strToUnsignedInt(argument));
529 flag |= LyXFunc::Disabled;
532 func_status box = LyXFunc::ToggleOff;
533 LyXFont const & font =
534 TEXT(false)->real_current_font;
537 if (font.emph() == LyXFont::ON)
538 box = LyXFunc::ToggleOn;
541 if (font.noun() == LyXFont::ON)
542 box = LyXFunc::ToggleOn;
545 if (font.series() == LyXFont::BOLD_SERIES)
546 box = LyXFunc::ToggleOn;
549 if (font.latex() == LyXFont::ON)
550 box = LyXFunc::ToggleOn;
563 // temporary dispatch method
564 void LyXFunc::miniDispatch(string const & s)
570 string const LyXFunc::Dispatch(string const & s)
572 // Split command string into command and argument
574 string line = frontStrip(s);
575 string arg = strip(frontStrip(split(line, cmd, ' ')));
577 return Dispatch(lyxaction.LookupFunc(cmd), arg);
581 string const LyXFunc::Dispatch(int ac,
582 string const & do_not_use_this_arg)
584 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
585 <<"] arg[" << do_not_use_this_arg << "]" << endl;
590 // we have not done anything wrong yet.
592 dispatch_buffer.erase();
594 // if action is a pseudo-action, we need the real action
595 if (lyxaction.isPseudoAction(ac)) {
597 action = static_cast<kb_action>
598 (lyxaction.retrieveActionArg(ac, tmparg));
602 action = static_cast<kb_action>(ac);
603 if (!do_not_use_this_arg.empty())
604 argument = do_not_use_this_arg; // except here
607 selection_possible = false;
609 if (owner->view()->available())
610 owner->view()->hideCursor();
612 // We cannot use this function here
613 if (getStatus(ac, do_not_use_this_arg) & Disabled)
614 goto exit_with_message;
616 commandshortcut.erase();
618 if (lyxrc.display_shortcuts && show_sc) {
619 if (action != LFUN_SELFINSERT) {
620 // Put name of command and list of shortcuts
621 // for it in minibuffer
622 string comname = lyxaction.getActionName(action);
624 int pseudoaction = action;
625 bool argsadded = false;
627 if (!argument.empty()) {
628 // If we have the command with argument,
631 lyxaction.searchActionArg(action,
634 if (pseudoaction == -1) {
635 pseudoaction = action;
637 comname += " " + argument;
642 string const shortcuts =
643 toplevel_keymap->findbinding(pseudoaction);
645 if (!shortcuts.empty()) {
646 comname += ": " + shortcuts;
647 } else if (!argsadded) {
648 comname += " " + argument;
651 if (!comname.empty()) {
652 comname = strip(comname);
653 commandshortcut = "(" + comname + ')';
654 owner->message(commandshortcut);
656 // Here we could even add a small pause,
657 // to annoy the user and make him learn
659 // No! That will just annoy, not teach
660 // anything. The user will read the messages
661 // if they are interested. (Asger)
666 if (owner->view()->available() && owner->view()->theLockingInset()) {
667 UpdatableInset::RESULT result;
668 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
669 (keyseq.length >= -1)))
671 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
672 argument = keyseq.getiso();
674 // Undo/Redo pre 0.13 is a bit tricky for insets.
675 if (action == LFUN_UNDO) {
678 UpdatableInset * inset =
679 owner->view()->theLockingInset();
680 inset->GetCursorPos(owner->view(), slx, sly);
681 owner->view()->unlockInset(inset);
682 owner->view()->menuUndo();
683 if (TEXT()->cursor.par()->
684 IsInset(TEXT()->cursor.pos())) {
685 inset = static_cast<UpdatableInset*>(
686 TEXT()->cursor.par()->
693 inset->Edit(owner->view(),slx,sly,0);
695 } else if (action == LFUN_REDO) {
698 UpdatableInset * inset = owner->view()->
700 inset->GetCursorPos(owner->view(), slx, sly);
701 owner->view()->unlockInset(inset);
702 owner->view()->menuRedo();
703 inset = static_cast<UpdatableInset*>(
704 TEXT()->cursor.par()->
708 inset->Edit(owner->view(),slx,sly,0);
710 } else if (((result=owner->view()->theLockingInset()->
711 LocalDispatch(owner->view(), action,
713 UpdatableInset::DISPATCHED) ||
714 (result == UpdatableInset::DISPATCHED_NOUPDATE))
717 //setMessage(N_("Text mode"));
719 case LFUN_UNKNOWN_ACTION:
720 case LFUN_BREAKPARAGRAPH:
722 TEXT()->CursorRight(owner->view());
723 owner->view()->setState();
727 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
728 TEXT()->CursorRight(owner->view());
729 moveCursorUpdate(true, false);
734 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
735 TEXT()->CursorRight(owner->view());
736 moveCursorUpdate(true, false);
741 TEXT()->CursorDown(owner->view());
742 moveCursorUpdate(true, false);
752 lyx::Assert(action != LFUN_SELECT_FILE_SYNC);
755 // --- Misc -------------------------------------------
756 case LFUN_WORDFINDFORWARD :
757 case LFUN_WORDFINDBACKWARD : {
758 static string last_search;
759 string searched_string;
761 if (!argument.empty()) {
762 last_search = argument;
763 searched_string = argument;
765 searched_string = last_search;
768 if (!searched_string.empty() &&
769 ((action == LFUN_WORDFINDBACKWARD) ?
770 SearchBackward(owner->view(), searched_string) :
771 SearchForward(owner->view(), searched_string))) {
773 // ??? What is that ???
774 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
777 // clear the selection (if there is any)
778 owner->view()->toggleSelection();
779 TEXT()->ClearSelection(owner->view());
781 // Move cursor so that successive C-s 's will not stand in place.
782 if (action == LFUN_WORDFINDFORWARD )
783 TEXT()->CursorRightOneWord(owner->view());
784 TEXT()->FinishUndo();
785 moveCursorUpdate(true, false);
788 // set the new selection
789 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
790 owner->view()->toggleSelection(false);
793 // REMOVED : if (owner->view()->getWorkArea()->focus)
794 owner->view()->showCursor();
800 if (owner->view()->available()) {
801 owner->view()->update(TEXT(),
802 BufferView::SELECT|BufferView::FITCUR);
805 keyseq.print(buf, true);
806 //owner->getMiniBuffer()->Set(buf, string(), string(), 1);
811 // --- Misc -------------------------------------------
812 case LFUN_EXEC_COMMAND:
814 std::vector<string> allCmds;
815 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
816 std::back_inserter(allCmds), lyx::firster());
817 static std::vector<string> hist;
818 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
823 case LFUN_CANCEL: // RVDK_PATCH_5
826 if (owner->view()->available())
827 // cancel any selection
828 Dispatch(LFUN_MARK_OFF);
829 setMessage(N_("Cancel"));
832 case LFUN_META_FAKE: // RVDK_PATCH_5
834 meta_fake_bit = Mod1Mask;
836 keyseq.print(buf, true);
837 setMessage(buf); // RVDK_PATCH_5
841 case LFUN_READ_ONLY_TOGGLE:
842 if (owner->buffer()->lyxvc.inUse()) {
843 owner->buffer()->lyxvc.toggleReadOnly();
845 owner->buffer()->setReadonly(
846 !owner->buffer()->isReadonly());
850 case LFUN_CENTER: // this is center and redraw.
851 owner->view()->center();
854 // --- Menus -----------------------------------------------
859 case LFUN_MENUNEWTMPLT:
863 case LFUN_CLOSEBUFFER:
868 if (!owner->buffer()->isUnnamed()) {
869 string const s1 = _("Saving document") + ' '
870 + MakeDisplayPath(owner->buffer()->fileName()
874 MenuWrite(owner->view(), owner->buffer());
876 WriteAs(owner->view(), owner->buffer());
880 WriteAs(owner->view(), owner->buffer(), argument);
883 case LFUN_MENURELOAD:
888 Exporter::Export(owner->buffer(), argument, true);
892 Exporter::Preview(owner->buffer(), argument);
896 Exporter::Export(owner->buffer(), "program", true);
900 MenuRunChktex(owner->buffer());
904 owner->getDialogs()->showPrint();
908 if (argument == "custom")
911 Exporter::Export(owner->buffer(), argument, false);
929 InsetCommandParams p;
932 if (action == LFUN_TOCVIEW)
934 p.setCmdName("tableofcontents");
936 else if (action == LFUN_LOAVIEW )
937 p.setCmdName("listof{algorithm}{List of Algorithms}");
938 else if (action == LFUN_LOFVIEW)
939 p.setCmdName("listoffigures");
941 p.setCmdName("listoftables");
943 owner->getDialogs()->createTOC(p.getAsString());
947 case LFUN_DIALOG_TABULAR_INSERT:
948 owner->getDialogs()->showTabularCreate();
956 AutoSave(owner->view());
960 owner->view()->menuUndo();
964 owner->view()->menuRedo();
967 case LFUN_MENUSEARCH:
968 owner->getDialogs()->showSearch();
971 case LFUN_REMOVEERRORS:
972 if (owner->view()->removeAutoInsets()) {
973 owner->view()->redraw();
974 owner->view()->fitCursor(TEXT());
978 case LFUN_HYPHENATION:
979 owner->view()->hyphenationPoint();
983 owner->view()->ldots();
986 case LFUN_END_OF_SENTENCE:
987 owner->view()->endOfSentenceDot();
990 case LFUN_MENU_SEPARATOR:
991 owner->view()->menuSeparator();
995 owner->view()->hfill();
999 changeDepth(owner->view(), TEXT(false), 0);
1002 case LFUN_DEPTH_MIN:
1003 changeDepth(owner->view(), TEXT(false), -1);
1006 case LFUN_DEPTH_PLUS:
1007 changeDepth(owner->view(), TEXT(false), 1);
1011 owner->getDialogs()->setUserFreeFont();
1016 owner->view()->setState();
1020 case LFUN_RECONFIGURE:
1021 Reconfigure(owner->view());
1025 case LFUN_FLOATSOPERATE:
1026 if (argument == "openfoot")
1027 owner->view()->allFloats(1,0);
1028 else if (argument == "closefoot")
1029 owner->view()->allFloats(0,0);
1030 else if (argument == "openfig")
1031 owner->view()->allFloats(1,1);
1032 else if (argument == "closefig")
1033 owner->view()->allFloats(0,1);
1036 #ifdef WITH_WARNINGS
1037 #warning Find another implementation here (or another lyxfunc)!
1040 case LFUN_HELP_COPYRIGHT:
1041 owner->getDialogs()->showCopyright();
1044 case LFUN_HELP_CREDITS:
1045 owner->getDialogs()->showCredits();
1048 case LFUN_HELP_OPEN:
1050 string const arg = argument;
1052 setErrorMessage(N_("Missing argument"));
1055 ProhibitInput(owner->view());
1056 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1057 if (fname.empty()) {
1058 lyxerr << "LyX: unable to find documentation file `"
1059 << arg << "'. Bad installation?" << endl;
1060 AllowInput(owner->view());
1063 string const str = _("Opening help file") + ' '
1064 + MakeDisplayPath(fname) + "...";
1066 owner->message(str);
1067 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1068 AllowInput(owner->view());
1072 case LFUN_HELP_VERSION: {
1073 ProhibitInput(owner->view());
1074 string msg(_("LyX Version "));
1078 fl_show_message(msg.c_str(),
1079 (_("Library directory: ")
1080 + MakeDisplayPath(system_lyxdir)).c_str(),
1081 (_("User directory: ")
1082 + MakeDisplayPath(user_lyxdir)).c_str());
1083 AllowInput(owner->view());
1087 // --- version control -------------------------------
1088 case LFUN_VC_REGISTER:
1090 if (!owner->buffer()->lyxvc.inUse())
1091 owner->buffer()->lyxvc.registrer();
1095 case LFUN_VC_CHECKIN:
1097 if (owner->buffer()->lyxvc.inUse()
1098 && !owner->buffer()->isReadonly())
1099 owner->buffer()->lyxvc.checkIn();
1103 case LFUN_VC_CHECKOUT:
1105 if (owner->buffer()->lyxvc.inUse()
1106 && owner->buffer()->isReadonly())
1107 owner->buffer()->lyxvc.checkOut();
1111 case LFUN_VC_REVERT:
1113 owner->buffer()->lyxvc.revert();
1119 owner->buffer()->lyxvc.undoLast();
1123 case LFUN_VC_HISTORY:
1125 owner->getDialogs()->showVCLogFile();
1129 // --- buffers ----------------------------------------
1131 case LFUN_SWITCHBUFFER:
1132 owner->view()->buffer(bufferlist.getBuffer(argument));
1137 // servercmd: argument must be <file>:<template>
1138 Buffer * tmpbuf = NewLyxFile(argument);
1140 owner->view()->buffer(tmpbuf);
1144 case LFUN_FILE_OPEN:
1148 case LFUN_LATEX_LOG:
1149 owner->getDialogs()->showLogFile();
1154 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1155 int sel = strToInt(argument);
1156 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1158 // Should this give a setMessage instead?
1160 return string(); // illegal argument
1162 --sel; // sel 1..., but layout 0...
1164 // Pretend we got the name instead.
1165 Dispatch(int(LFUN_LAYOUT),
1166 textclasslist.NameOfLayout(owner->view()
1167 ->buffer()->params.textclass,
1172 case LFUN_LAYOUT_DOCUMENT:
1173 owner->getDialogs()->showLayoutDocument();
1176 case LFUN_LAYOUT_PARAGRAPH:
1177 owner->getDialogs()->showLayoutParagraph();
1180 case LFUN_LAYOUT_CHARACTER:
1181 owner->getDialogs()->showLayoutCharacter();
1184 case LFUN_LAYOUT_TABULAR:
1185 if (owner->view()->theLockingInset()) {
1186 if (owner->view()->theLockingInset()->LyxCode()==Inset::TABULAR_CODE) {
1187 InsetTabular * inset = static_cast<InsetTabular *>
1188 (owner->view()->theLockingInset());
1189 inset->OpenLayoutDialog(owner->view());
1190 } else if (owner->view()->theLockingInset()->
1191 GetFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1192 InsetTabular * inset = static_cast<InsetTabular *>(
1193 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE));
1194 inset->OpenLayoutDialog(owner->view());
1199 case LFUN_LAYOUT_PREAMBLE:
1200 owner->getDialogs()->showPreamble();
1203 case LFUN_LAYOUT_SAVE_DEFAULT:
1204 MenuLayoutSave(owner->view());
1207 case LFUN_DROP_LAYOUTS_CHOICE:
1208 owner->getToolbar()->openLayoutList();
1211 case LFUN_MENU_OPEN_BY_NAME:
1212 owner->getMenubar()->openByName(argument);
1213 break; // RVDK_PATCH_5
1215 case LFUN_SPELLCHECK:
1216 if (lyxrc.isp_command != "none")
1217 ShowSpellChecker(owner->view());
1218 break; // RVDK_PATCH_5
1220 // --- lyxserver commands ----------------------------
1224 setMessage(owner->buffer()->fileName());
1225 lyxerr.debug() << "FNAME["
1226 << owner->buffer()->fileName()
1234 dispatch_buffer = buf;
1235 lyxserver->notifyClient(dispatch_buffer);
1239 case LFUN_GOTOFILEROW:
1241 char file_name[100];
1243 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1245 // Must replace extension of the file to be .lyx and get full path
1246 string s = ChangeExtension(string(file_name), ".lyx");
1248 // Either change buffer or load the file
1249 if (bufferlist.exists(s))
1250 owner->view()->buffer(bufferlist.getBuffer(s));
1252 owner->view()->buffer(bufferlist.loadLyXFile(s));
1255 owner->view()->setCursorFromRow(row);
1258 owner->view()->center();
1262 case LFUN_GOTO_PARAGRAPH:
1264 istringstream istr(argument.c_str());
1268 LyXParagraph * par = TEXT()->GetParFromID(id);
1273 TEXT()->SetCursor(owner->view(), par, 0);
1274 owner->view()->setState();
1278 owner->view()->center();
1285 int const qa = lyxaction.LookupFunc(argument);
1286 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1290 // --- toolbar ----------------------------------
1291 case LFUN_PUSH_TOOLBAR:
1293 int nth = strToInt(argument);
1295 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1297 owner->getToolbar()->push(nth);
1302 case LFUN_ADD_TO_TOOLBAR:
1304 if (lyxerr.debugging(Debug::GUI)) {
1305 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1306 "argument = `" << argument << '\'' << endl;
1308 string tmp(argument);
1309 //lyxerr <<string("Argument: ") + argument);
1310 //lyxerr <<string("Tmp : ") + tmp);
1312 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1314 owner->getToolbar()->add(argument, false);
1315 owner->getToolbar()->set();
1320 // --- insert characters ----------------------------------------
1322 // --- Mathed stuff. If we are here, there is no locked inset yet.
1327 if (!greek_kb_flag) {
1329 setMessage(N_("Math greek mode on"));
1336 case LFUN_GREEK_TOGGLE:
1338 greek_kb_flag = greek_kb_flag ? 0 : 2;
1339 if (greek_kb_flag) {
1340 setMessage(N_("Math greek keyboard on"));
1342 setMessage(N_("Math greek keyboard off"));
1347 case LFUN_MATH_EXTERN:
1348 case LFUN_MATH_NUMBER:
1349 case LFUN_MATH_LIMITS:
1351 setErrorMessage(N_("This is only allowed in math mode!"));
1355 case LFUN_MATH_PANEL:
1356 owner->getDialogs()->showMathPanel();
1359 case LFUN_CITATION_CREATE:
1361 InsetCommandParams p( "cite" );
1363 if (!argument.empty()) {
1364 // This should be set at source, ie when typing
1365 // "citation-insert foo" in the minibuffer.
1366 // Question: would pybibliographer also need to be
1367 // changed. Suspect so. Leave as-is therefore.
1368 if (contains(argument, "|")) {
1369 p.setContents( token(argument, '|', 0) );
1370 p.setOptions( token(argument, '|', 1) );
1372 p.setContents( argument );
1374 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1376 owner->getDialogs()->createCitation( p.getAsString() );
1380 case LFUN_CHILDOPEN:
1382 string const filename =
1383 MakeAbsPath(argument,
1384 OnlyPath(owner->buffer()->fileName()));
1385 setMessage(N_("Opening child document ") +
1386 MakeDisplayPath(filename) + "...");
1387 owner->view()->savePosition(0);
1388 if (bufferlist.exists(filename))
1389 owner->view()->buffer(bufferlist.getBuffer(filename));
1391 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1395 case LFUN_TOGGLECURSORFOLLOW:
1396 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1399 case LFUN_KMAP_OFF: // keymap off
1400 owner->getIntl()->KeyMapOn(false);
1403 case LFUN_KMAP_PRIM: // primary keymap
1404 owner->getIntl()->KeyMapPrim();
1407 case LFUN_KMAP_SEC: // secondary keymap
1408 owner->getIntl()->KeyMapSec();
1411 case LFUN_KMAP_TOGGLE: // toggle keymap
1412 owner->getIntl()->ToggleKeyMap();
1417 // argument contains ';'-terminated commands
1418 while (argument.find(';') != string::npos) {
1420 argument = split(argument, first, ';');
1426 case LFUN_DIALOG_PREFERENCES:
1427 owner->getDialogs()->showPreferences();
1430 case LFUN_SAVEPREFERENCES:
1432 Path p(user_lyxdir);
1433 lyxrc.write("preferences");
1437 case LFUN_SCREEN_FONT_UPDATE:
1439 // handle the screen font changes.
1441 lyxrc.set_font_norm_type();
1442 fontloader.update();
1443 // Of course we should only do the resize and the textcache.clear
1444 // if values really changed...but not very important right now. (Lgb)
1445 // All buffers will need resize
1446 bufferlist.resize();
1447 // We also need to empty the textcache so that
1448 // the buffer will be formatted correctly after
1454 case LFUN_SET_COLOR:
1457 string const x11_name = split(argument, lyx_name, ' ');
1458 if (lyx_name.empty() || x11_name.empty()) {
1459 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1464 if (!lcolor.setColor(lyx_name, x11_name)) {
1465 static string const err1 (N_("Set-color \""));
1466 static string const err2 (
1467 N_("\" failed - color is undefined "
1468 "or may not be redefined"));
1469 setErrorMessage(_(err1) + lyx_name + _(err2));
1472 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1473 owner->view()->redraw();
1478 owner->message(argument);
1481 case LFUN_MESSAGE_PUSH:
1482 owner->messagePush(argument);
1485 case LFUN_MESSAGE_POP:
1486 owner->messagePop();
1490 // Then if it was none of the above
1491 if (!owner->view()->Dispatch(action, argument))
1492 lyxerr << "A truly unknown func ["
1493 << action << "]!" << endl;
1499 string const res = getMessage();
1502 if (!commandshortcut.empty()) {
1503 owner->getMiniBuffer()->addSet(commandshortcut);
1506 string const msg(_(res) + ' ' + commandshortcut);
1507 owner->message(msg);
1514 void LyXFunc::setupLocalKeymap()
1516 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1517 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1521 void LyXFunc::MenuNew(bool fromTemplate)
1523 string initpath = lyxrc.document_path;
1525 if (owner->view()->available()) {
1526 string const trypath = owner->buffer()->filepath;
1527 // If directory is writeable, use this as default.
1528 if (IsDirWriteable(trypath) == 1)
1532 static int newfile_number = 0;
1535 if (lyxrc.new_ask_filename) {
1536 FileDialog fileDlg(owner, _("Enter filename for new document"),
1537 LFUN_SELECT_FILE_SYNC,
1538 make_pair(string(_("Documents")),
1539 string(lyxrc.document_path)),
1540 make_pair(string(_("Templates")),
1541 string(lyxrc.template_path)));
1543 FileDialog::Result result =
1544 fileDlg.Select(initpath,
1545 _("*.lyx|LyX Documents (*.lyx)"),
1548 if (result.second.empty()) {
1549 owner->message(_("Canceled."));
1550 lyxerr.debug() << "New Document Cancelled." << endl;
1554 // get absolute path of file and make sure the filename ends
1556 s = MakeAbsPath(result.second);
1557 if (!IsLyXFilename(s))
1560 // Check if the document already is open
1561 if (bufferlist.exists(s)) {
1562 switch (AskConfirmation(_("Document is already open:"),
1563 MakeDisplayPath(s, 50),
1564 _("Do you want to close that document now?\n"
1565 "('No' will just switch to the open version)")))
1567 case 1: // Yes: close the document
1568 if (!bufferlist.close(bufferlist.getBuffer(s)))
1569 // If close is canceled, we cancel here too.
1572 case 2: // No: switch to the open document
1573 owner->view()->buffer(bufferlist.getBuffer(s));
1575 case 3: // Cancel: Do nothing
1576 owner->message(_("Canceled."));
1580 // Check whether the file already exists
1581 if (IsLyXFilename(s)) {
1583 if (fi.readable() &&
1584 AskQuestion(_("File already exists:"),
1585 MakeDisplayPath(s, 50),
1586 _("Do you want to open the document?"))) {
1588 owner->message(_("Opening document") + ' '
1589 + MakeDisplayPath(s) + "...");
1590 XFlush(fl_get_display());
1591 owner->view()->buffer(
1592 bufferlist.loadLyXFile(s));
1593 owner->message(_("Document") + ' '
1594 + MakeDisplayPath(s) + ' '
1600 s = AddName(lyxrc.document_path,
1601 "newfile" + tostr(++newfile_number) + ".lyx");
1603 while (bufferlist.exists(s) || fi.readable()) {
1605 s = AddName(lyxrc.document_path,
1606 "newfile" + tostr(newfile_number) +
1612 // The template stuff
1615 FileDialog fileDlg(owner, _("Select template file"),
1616 LFUN_SELECT_FILE_SYNC,
1617 make_pair(string(_("Documents")),
1618 string(lyxrc.document_path)),
1619 make_pair(string(_("Templates")),
1620 string(lyxrc.template_path)));
1622 FileDialog::Result result =
1623 fileDlg.Select(initpath,
1624 _("*.lyx|LyX Documents (*.lyx)"));
1626 if (result.first == FileDialog::Later)
1629 string const fname = result.second;
1636 // find a free buffer
1637 lyxerr.debug() << "Find a free buffer." << endl;
1638 owner->view()->buffer(bufferlist.newFile(s, templname));
1642 void LyXFunc::Open(string const & fname)
1644 string initpath = lyxrc.document_path;
1646 if (owner->view()->available()) {
1647 string const trypath = owner->buffer()->filepath;
1648 // If directory is writeable, use this as default.
1649 if (IsDirWriteable(trypath) == 1)
1655 if (fname.empty()) {
1656 FileDialog fileDlg(owner, _("Select document to open"),
1658 make_pair(string(_("Documents")),
1659 string(lyxrc.document_path)),
1660 make_pair(string(_("Examples")),
1661 string(AddPath(system_lyxdir, "examples"))));
1663 FileDialog::Result result =
1664 fileDlg.Select(initpath,
1665 "*.lyx|LyX Documents (*.lyx)");
1667 if (result.first == FileDialog::Later)
1670 filename = result.second;
1672 // check selected filename
1673 if (filename.empty()) {
1674 owner->message(_("Canceled."));
1680 // get absolute path of file and make sure the filename ends
1682 filename = MakeAbsPath(filename);
1683 if (!IsLyXFilename(filename))
1687 owner->message(_("Opening document") + ' '
1688 + MakeDisplayPath(filename) + "...");
1689 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1691 owner->view()->buffer(openbuf);
1692 owner->message(_("Document") + ' '
1693 + MakeDisplayPath(filename)
1694 + ' ' + _("opened."));
1696 owner->message(_("Could not open document") + ' '
1697 + MakeDisplayPath(filename));
1702 // checks for running without gui are missing.
1703 void LyXFunc::doImport(string const & argument)
1706 string filename = split(argument, format, ' ');
1707 lyxerr.debug() << "LyXFunc::doImport: " << format
1708 << " file: " << filename << endl;
1710 if (filename.empty()) { // need user interaction
1711 string initpath = lyxrc.document_path;
1713 if (owner->view()->available()) {
1714 string const trypath = owner->buffer()->filepath;
1715 // If directory is writeable, use this as default.
1716 if (IsDirWriteable(trypath) == 1)
1720 string const text = _("Select ") + formats.PrettyName(format)
1721 + _(" file to import");
1723 FileDialog fileDlg(owner, text,
1725 make_pair(string(_("Documents")),
1726 string(lyxrc.document_path)),
1727 make_pair(string(_("Examples")),
1728 string(AddPath(system_lyxdir, "examples"))));
1730 string const extension = "*." + formats.Extension(format)
1731 + "| " + formats.PrettyName(format)
1732 + " (*." + formats.Extension(format) + ")";
1734 FileDialog::Result result = fileDlg.Select(initpath,
1737 if (result.first == FileDialog::Later)
1740 filename = result.second;
1742 // check selected filename
1743 if (filename.empty())
1744 owner->message(_("Canceled."));
1747 // still no filename? abort
1748 if (filename.empty())
1751 // get absolute path of file
1752 filename = MakeAbsPath(filename);
1754 string const lyxfile = ChangeExtension(filename, ".lyx");
1756 // Check if the document already is open
1757 if (bufferlist.exists(lyxfile)) {
1758 switch (AskConfirmation(_("Document is already open:"),
1759 MakeDisplayPath(lyxfile, 50),
1760 _("Do you want to close that document now?\n"
1761 "('No' will just switch to the open version)")))
1763 case 1: // Yes: close the document
1764 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1765 // If close is canceled, we cancel here too.
1768 case 2: // No: switch to the open document
1769 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1771 case 3: // Cancel: Do nothing
1772 owner->message(_("Canceled."));
1777 // Check if a LyX document by the same root exists in filesystem
1778 FileInfo const f(lyxfile, true);
1779 if (f.exist() && !AskQuestion(_("A document by the name"),
1780 MakeDisplayPath(lyxfile),
1781 _("already exists. Overwrite?"))) {
1782 owner->message(_("Canceled"));
1785 // filename should be valid now
1787 Importer::Import(owner, filename, format);
1791 void LyXFunc::reloadBuffer()
1793 string const fn = owner->buffer()->fileName();
1794 if (bufferlist.close(owner->buffer()))
1795 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1799 void LyXFunc::CloseBuffer()
1801 if (bufferlist.close(owner->buffer()) && !quitting) {
1802 if (bufferlist.empty()) {
1803 // need this otherwise SEGV may occur while trying to
1804 // set variables that don't exist
1805 // since there's no current buffer
1806 owner->getDialogs()->hideBufferDependent();
1808 owner->view()->buffer(bufferlist.first());
1814 // Each "owner" should have it's own message method. lyxview and
1815 // the minibuffer would use the minibuffer, but lyxserver would
1816 // send an ERROR signal to its client. Alejandro 970603
1817 // This func is bit problematic when it comes to NLS, to make the
1818 // lyx servers client be language indepenent we must not translate
1819 // strings sent to this func.
1820 void LyXFunc::setErrorMessage(string const & m) const
1822 dispatch_buffer = m;
1827 void LyXFunc::setMessage(string const & m)
1829 dispatch_buffer = m;
1833 void LyXFunc::initMiniBuffer()
1835 string text = _("Welcome to LyX!");
1837 // When meta-fake key is pressed, show the key sequence so far + "M-".
1843 // Else, when a non-complete key sequence is pressed,
1844 // show the available options.
1845 else if (keyseqUncomplete())
1846 text = keyseqOptions();
1848 // Else, show the buffer state.
1849 else if (owner->view()->available()) {
1850 Buffer * tmpbuf = owner->buffer();
1852 string const nicename =
1853 MakeDisplayPath(tmpbuf->fileName());
1854 // Should we do this instead? (kindo like emacs)
1855 // leaves more room for other information
1858 if (tmpbuf->lyxvc.inUse()) {
1860 text += tmpbuf->lyxvc.version();
1862 text += tmpbuf->lyxvc.locker();
1863 if (tmpbuf->isReadonly())
1866 } else if (tmpbuf->isReadonly())
1868 if (!tmpbuf->isLyxClean())
1869 text += _(" (Changed)");
1871 if (text != _("Welcome to LyX!")) // this is a hack
1872 text = _("* No document open *");
1875 owner->message(text);