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 void LyXFunc::processKeySym(KeySym keysym, unsigned int state)
221 if (lyxerr.debugging(Debug::KEY)) {
222 char const * 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)"
240 // Can we be sure that this will work for all X-Windows
241 // implementations? (Lgb)
242 // This code snippet makes lyx ignore some keys. Perhaps
243 // all of them should be explictly mentioned?
244 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
245 || keysym == XK_Mode_switch || keysym == 0x0) {
250 // Do a one-deep top-level lookup for
251 // cancel and meta-fake keys. RVDK_PATCH_5
252 cancel_meta_seq.reset();
254 int action = cancel_meta_seq.addkey(keysym, state
255 &(ShiftMask|ControlMask
257 if (lyxerr.debugging(Debug::KEY)) {
258 lyxerr << "action first set to [" << action << "]" << endl;
261 // When not cancel or meta-fake, do the normal lookup.
262 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
263 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
264 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
265 if (lyxerr.debugging(Debug::KEY)) {
266 lyxerr << "meta_fake_bit is ["
267 << meta_fake_bit << "]" << endl;
269 // remove Caps Lock and Mod2 as a modifiers
270 action = keyseq.addkey(keysym,
271 (state | meta_fake_bit)
272 &(ShiftMask|ControlMask
274 if (lyxerr.debugging(Debug::KEY)) {
275 lyxerr << "action now set to ["
276 << action << "]" << endl;
279 // Dont remove this unless you know what you are doing.
282 if (action == 0) action = LFUN_PREFIX;
284 if (lyxerr.debugging(Debug::KEY)) {
287 lyxerr << "Key [action="
293 // already here we know if it any point in going further
294 // why not return already here if action == -1 and
295 // num_bytes == 0? (Lgb)
297 if (keyseq.length > 1 || keyseq.length < -1) {
304 // It is unknown, but what if we remove all
305 // the modifiers? (Lgb)
306 action = keyseq.addkey(keysym, 0);
308 if (lyxerr.debugging(Debug::KEY)) {
309 lyxerr << "Removing modifiers...\n"
310 << "Action now set to ["
311 << action << "]" << endl;
314 owner->message(_("Unknown function."));
320 if (action == LFUN_SELFINSERT) {
321 // This is very X dependant.
322 unsigned int c = keysym;
324 switch (c & 0x0000FF00) {
325 // latin 1 byte 3 = 0
326 case 0x00000000: break;
327 // latin 2 byte 3 = 1
329 // latin 3 byte 3 = 2
331 // latin 4 byte 3 = 3
333 // latin 8 byte 3 = 18 (0x12)
335 // latin 9 byte 3 = 19 (0x13)
344 argument = static_cast<char>(c);
345 lyxerr[Debug::KEY] << "SelfInsert arg[`"
346 << argument << "']" << endl;
349 bool tmp_sc = show_sc;
351 Dispatch(action, argument);
358 LyXFunc::func_status LyXFunc::getStatus(int ac) const
360 return getStatus(ac, string());
364 LyXFunc::func_status LyXFunc::getStatus(int ac,
365 string const & not_to_use_arg) const
368 func_status flag = LyXFunc::OK;
370 Buffer * buf = owner->buffer();
372 if (lyxaction.isPseudoAction(ac))
373 action = lyxaction.retrieveActionArg(ac, argument);
375 action = static_cast<kb_action>(ac);
376 if (!not_to_use_arg.empty())
377 argument = not_to_use_arg; // exept here
380 if (action == LFUN_UNKNOWN_ACTION) {
381 setErrorMessage(N_("Unknown action"));
382 return LyXFunc::Unknown;
385 // Check whether we need a buffer
386 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
387 // Yes we need a buffer, do we have one?
390 // Can we use a readonly buffer?
391 if (buf->isReadonly() &&
392 !lyxaction.funcHasFlag(action,
393 LyXAction::ReadOnly)) {
395 setErrorMessage(N_("Document is read-only"));
396 flag |= LyXFunc::Disabled;
400 setErrorMessage(N_("Command not allowed with"
401 "out any document open"));
402 flag |= LyXFunc::Disabled;
407 // I would really like to avoid having this switch and rather try to
408 // encode this in the function itself.
409 bool disable = false;
412 disable = !Exporter::IsExportable(buf, "dvi")
413 || lyxrc.print_command == "none";
416 disable = argument == "fax" &&
417 !Exporter::IsExportable(buf, argument);
420 disable = buf->undostack.empty();
423 disable = buf->redostack.empty();
425 case LFUN_SPELLCHECK:
426 disable = lyxrc.isp_command == "none";
429 disable = lyxrc.chktex_command == "none";
432 disable = !Exporter::IsExportable(buf, "program");
435 case LFUN_INSERTFOOTNOTE:
436 // Disable insertion of floats in a tabular.
438 if (owner->view()->theLockingInset()) {
439 disable = (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) ||
440 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
444 case LFUN_LAYOUT_TABULAR:
446 if (owner->view()->theLockingInset()) {
447 disable = (owner->view()->theLockingInset()->LyxCode() != Inset::TABULAR_CODE) &&
448 !owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE);
452 case LFUN_TABULAR_FEATURE:
454 if (owner->view()->theLockingInset()) {
455 func_status ret = LyXFunc::Disabled;
456 if (owner->view()->theLockingInset()->LyxCode() == Inset::TABULAR_CODE) {
457 ret = static_cast<InsetTabular *>
458 (owner->view()->theLockingInset())->
460 } else if (owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
461 ret = static_cast<InsetTabular *>
462 (owner->view()->theLockingInset()->
463 GetFirstLockingInsetOfType(Inset::TABULAR_CODE))->
469 static InsetTabular inset(*owner->buffer(), 1, 1);
473 ret = inset.getStatus(argument);
474 if ((ret & LyXFunc::ToggleOn) ||
475 (ret & LyXFunc::ToggleOff))
476 flag |= LyXFunc::ToggleOff;
480 case LFUN_VC_REGISTER:
481 disable = buf->lyxvc.inUse();
483 case LFUN_VC_CHECKIN:
484 disable = !buf->lyxvc.inUse() || buf->isReadonly();
486 case LFUN_VC_CHECKOUT:
487 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
491 case LFUN_VC_HISTORY:
492 disable = !buf->lyxvc.inUse();
494 case LFUN_BOOKMARK_GOTO:
495 disable = !owner->view()->
496 isSavedPosition(strToUnsignedInt(argument));
501 flag |= LyXFunc::Disabled;
504 func_status box = LyXFunc::ToggleOff;
505 LyXFont const & font =
506 TEXT(false)->real_current_font;
509 if (font.emph() == LyXFont::ON)
510 box = LyXFunc::ToggleOn;
513 if (font.noun() == LyXFont::ON)
514 box = LyXFunc::ToggleOn;
517 if (font.series() == LyXFont::BOLD_SERIES)
518 box = LyXFunc::ToggleOn;
521 if (font.latex() == LyXFont::ON)
522 box = LyXFunc::ToggleOn;
535 // temporary dispatch method
536 void LyXFunc::miniDispatch(string const & s)
542 string const LyXFunc::Dispatch(string const & s)
544 // Split command string into command and argument
546 string line = frontStrip(s);
547 string arg = strip(frontStrip(split(line, cmd, ' ')));
549 return Dispatch(lyxaction.LookupFunc(cmd), arg);
553 string const LyXFunc::Dispatch(int ac,
554 string const & do_not_use_this_arg)
556 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
557 <<"] arg[" << do_not_use_this_arg << "]" << endl;
562 // we have not done anything wrong yet.
564 dispatch_buffer.erase();
566 // if action is a pseudo-action, we need the real action
567 if (lyxaction.isPseudoAction(ac)) {
569 action = static_cast<kb_action>
570 (lyxaction.retrieveActionArg(ac, tmparg));
574 action = static_cast<kb_action>(ac);
575 if (!do_not_use_this_arg.empty())
576 argument = do_not_use_this_arg; // except here
579 selection_possible = false;
581 if (owner->view()->available())
582 owner->view()->hideCursor();
584 // We cannot use this function here
585 if (getStatus(ac, do_not_use_this_arg) & Disabled)
586 goto exit_with_message;
588 commandshortcut.erase();
590 if (lyxrc.display_shortcuts && show_sc) {
591 if (action != LFUN_SELFINSERT) {
592 // Put name of command and list of shortcuts
593 // for it in minibuffer
594 string comname = lyxaction.getActionName(action);
596 int pseudoaction = action;
597 bool argsadded = false;
599 if (!argument.empty()) {
600 // If we have the command with argument,
603 lyxaction.searchActionArg(action,
606 if (pseudoaction == -1) {
607 pseudoaction = action;
609 comname += " " + argument;
614 string const shortcuts =
615 toplevel_keymap->findbinding(pseudoaction);
617 if (!shortcuts.empty()) {
618 comname += ": " + shortcuts;
619 } else if (!argsadded) {
620 comname += " " + argument;
623 if (!comname.empty()) {
624 comname = strip(comname);
625 commandshortcut = "(" + comname + ')';
626 owner->message(commandshortcut);
628 // Here we could even add a small pause,
629 // to annoy the user and make him learn
631 // No! That will just annoy, not teach
632 // anything. The user will read the messages
633 // if they are interested. (Asger)
638 if (owner->view()->available() && owner->view()->theLockingInset()) {
639 UpdatableInset::RESULT result;
640 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
641 (keyseq.length >= -1)))
643 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
644 argument = keyseq.getiso();
646 // Undo/Redo pre 0.13 is a bit tricky for insets.
647 if (action == LFUN_UNDO) {
650 UpdatableInset * inset =
651 owner->view()->theLockingInset();
652 inset->GetCursorPos(owner->view(), slx, sly);
653 owner->view()->unlockInset(inset);
654 owner->view()->menuUndo();
655 if (TEXT()->cursor.par()->
656 IsInset(TEXT()->cursor.pos())) {
657 inset = static_cast<UpdatableInset*>(
658 TEXT()->cursor.par()->
665 inset->Edit(owner->view(),slx,sly,0);
667 } else if (action == LFUN_REDO) {
670 UpdatableInset * inset = owner->view()->
672 inset->GetCursorPos(owner->view(), slx, sly);
673 owner->view()->unlockInset(inset);
674 owner->view()->menuRedo();
675 inset = static_cast<UpdatableInset*>(
676 TEXT()->cursor.par()->
680 inset->Edit(owner->view(),slx,sly,0);
682 } else if (((result=owner->view()->theLockingInset()->
683 LocalDispatch(owner->view(), action,
685 UpdatableInset::DISPATCHED) ||
686 (result == UpdatableInset::DISPATCHED_NOUPDATE))
689 //setMessage(N_("Text mode"));
691 case LFUN_UNKNOWN_ACTION:
692 case LFUN_BREAKPARAGRAPH:
694 TEXT()->CursorRight(owner->view());
695 owner->view()->setState();
699 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
700 TEXT()->CursorRight(owner->view());
701 moveCursorUpdate(true, false);
706 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
707 TEXT()->CursorRight(owner->view());
708 moveCursorUpdate(true, false);
713 TEXT()->CursorDown(owner->view());
714 moveCursorUpdate(true, false);
724 lyx::Assert(action != LFUN_SELECT_FILE_SYNC);
730 if (!owner->view()->available()) break;
732 // this function should be used always [asierra060396]
733 UpdatableInset * tli =
734 owner->view()->theLockingInset();
736 UpdatableInset * lock = tli->GetLockingInset();
739 owner->view()->unlockInset(tli);
740 TEXT()->CursorRight(owner->view());
741 moveCursorUpdate(true, false);
744 tli->UnlockInsetInInset(owner->view(),
752 // --- Misc -------------------------------------------
753 case LFUN_WORDFINDFORWARD :
754 case LFUN_WORDFINDBACKWARD : {
755 static string last_search;
756 string searched_string;
758 if (!argument.empty()) {
759 last_search = argument;
760 searched_string = argument;
762 searched_string = last_search;
765 if (!searched_string.empty() &&
766 ((action == LFUN_WORDFINDBACKWARD) ?
767 SearchBackward(owner->view(), searched_string) :
768 SearchForward(owner->view(), searched_string))) {
770 // ??? What is that ???
771 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
774 // clear the selection (if there is any)
775 owner->view()->toggleSelection();
776 TEXT()->ClearSelection(owner->view());
778 // Move cursor so that successive C-s 's will not stand in place.
779 if (action == LFUN_WORDFINDFORWARD )
780 TEXT()->CursorRightOneWord(owner->view());
781 TEXT()->FinishUndo();
782 moveCursorUpdate(true, false);
785 // set the new selection
786 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
787 owner->view()->toggleSelection(false);
790 // REMOVED : if (owner->view()->getWorkArea()->focus)
791 owner->view()->showCursor();
797 if (owner->view()->available()) {
798 owner->view()->update(TEXT(),
799 BufferView::SELECT|BufferView::FITCUR);
802 keyseq.print(buf, true);
807 // --- Misc -------------------------------------------
808 case LFUN_EXEC_COMMAND:
810 std::vector<string> allCmds;
811 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
812 std::back_inserter(allCmds), lyx::firster());
813 static std::vector<string> hist;
814 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
819 case LFUN_CANCEL: // RVDK_PATCH_5
822 if (owner->view()->available())
823 // cancel any selection
824 Dispatch(LFUN_MARK_OFF);
825 setMessage(N_("Cancel"));
828 case LFUN_META_FAKE: // RVDK_PATCH_5
830 meta_fake_bit = Mod1Mask;
832 keyseq.print(buf, true);
833 setMessage(buf); // RVDK_PATCH_5
837 case LFUN_READ_ONLY_TOGGLE:
838 if (owner->buffer()->lyxvc.inUse()) {
839 owner->buffer()->lyxvc.toggleReadOnly();
841 owner->buffer()->setReadonly(
842 !owner->buffer()->isReadonly());
846 case LFUN_CENTER: // this is center and redraw.
847 owner->view()->center();
850 // --- Menus -----------------------------------------------
855 case LFUN_MENUNEWTMPLT:
859 case LFUN_CLOSEBUFFER:
864 if (!owner->buffer()->isUnnamed()) {
865 string const s1 = _("Saving document") + ' '
866 + MakeDisplayPath(owner->buffer()->fileName()
870 MenuWrite(owner->view(), owner->buffer());
872 WriteAs(owner->view(), owner->buffer());
876 WriteAs(owner->view(), owner->buffer(), argument);
879 case LFUN_MENURELOAD:
884 Exporter::Export(owner->buffer(), argument, true);
888 Exporter::Preview(owner->buffer(), argument);
892 Exporter::Export(owner->buffer(), "program", true);
896 MenuRunChktex(owner->buffer());
900 owner->getDialogs()->showPrint();
904 if (argument == "custom")
907 Exporter::Export(owner->buffer(), argument, false);
925 InsetCommandParams p;
928 if (action == LFUN_TOCVIEW)
930 p.setCmdName("tableofcontents");
932 else if (action == LFUN_LOAVIEW )
933 p.setCmdName("listof{algorithm}{List of Algorithms}");
934 else if (action == LFUN_LOFVIEW)
935 p.setCmdName("listoffigures");
937 p.setCmdName("listoftables");
939 owner->getDialogs()->createTOC(p.getAsString());
943 case LFUN_DIALOG_TABULAR_INSERT:
944 owner->getDialogs()->showTabularCreate();
952 AutoSave(owner->view());
956 owner->view()->menuUndo();
960 owner->view()->menuRedo();
963 case LFUN_MENUSEARCH:
964 owner->getDialogs()->showSearch();
967 case LFUN_REMOVEERRORS:
968 if (owner->view()->removeAutoInsets()) {
969 owner->view()->redraw();
970 owner->view()->fitCursor(TEXT());
975 changeDepth(owner->view(), TEXT(false), 0);
979 changeDepth(owner->view(), TEXT(false), -1);
982 case LFUN_DEPTH_PLUS:
983 changeDepth(owner->view(), TEXT(false), 1);
987 owner->getDialogs()->setUserFreeFont();
992 owner->view()->setState();
996 case LFUN_RECONFIGURE:
997 Reconfigure(owner->view());
1001 case LFUN_FLOATSOPERATE:
1002 if (argument == "openfoot")
1003 owner->view()->allFloats(1,0);
1004 else if (argument == "closefoot")
1005 owner->view()->allFloats(0,0);
1006 else if (argument == "openfig")
1007 owner->view()->allFloats(1,1);
1008 else if (argument == "closefig")
1009 owner->view()->allFloats(0,1);
1012 #ifdef WITH_WARNINGS
1013 #warning Find another implementation here (or another lyxfunc)!
1016 case LFUN_HELP_COPYRIGHT:
1017 owner->getDialogs()->showCopyright();
1020 case LFUN_HELP_CREDITS:
1021 owner->getDialogs()->showCredits();
1024 case LFUN_HELP_OPEN:
1026 string const arg = argument;
1028 setErrorMessage(N_("Missing argument"));
1031 ProhibitInput(owner->view());
1032 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1033 if (fname.empty()) {
1034 lyxerr << "LyX: unable to find documentation file `"
1035 << arg << "'. Bad installation?" << endl;
1036 AllowInput(owner->view());
1039 string const str = _("Opening help file") + ' '
1040 + MakeDisplayPath(fname) + "...";
1042 owner->message(str);
1043 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1044 AllowInput(owner->view());
1048 case LFUN_HELP_VERSION: {
1049 ProhibitInput(owner->view());
1050 string msg(_("LyX Version "));
1054 fl_show_message(msg.c_str(),
1055 (_("Library directory: ")
1056 + MakeDisplayPath(system_lyxdir)).c_str(),
1057 (_("User directory: ")
1058 + MakeDisplayPath(user_lyxdir)).c_str());
1059 AllowInput(owner->view());
1063 // --- version control -------------------------------
1064 case LFUN_VC_REGISTER:
1066 if (!owner->buffer()->lyxvc.inUse())
1067 owner->buffer()->lyxvc.registrer();
1071 case LFUN_VC_CHECKIN:
1073 if (owner->buffer()->lyxvc.inUse()
1074 && !owner->buffer()->isReadonly())
1075 owner->buffer()->lyxvc.checkIn();
1079 case LFUN_VC_CHECKOUT:
1081 if (owner->buffer()->lyxvc.inUse()
1082 && owner->buffer()->isReadonly())
1083 owner->buffer()->lyxvc.checkOut();
1087 case LFUN_VC_REVERT:
1089 owner->buffer()->lyxvc.revert();
1095 owner->buffer()->lyxvc.undoLast();
1099 case LFUN_VC_HISTORY:
1101 owner->getDialogs()->showVCLogFile();
1105 // --- buffers ----------------------------------------
1107 case LFUN_SWITCHBUFFER:
1108 owner->view()->buffer(bufferlist.getBuffer(argument));
1113 // servercmd: argument must be <file>:<template>
1114 Buffer * tmpbuf = NewLyxFile(argument);
1116 owner->view()->buffer(tmpbuf);
1120 case LFUN_FILE_OPEN:
1124 case LFUN_LATEX_LOG:
1125 owner->getDialogs()->showLogFile();
1130 lyxerr.debug() << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1131 int sel = strToInt(argument);
1132 lyxerr.debug() << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1134 // Should this give a setMessage instead?
1136 return string(); // illegal argument
1138 --sel; // sel 1..., but layout 0...
1140 // Pretend we got the name instead.
1141 Dispatch(int(LFUN_LAYOUT),
1142 textclasslist.NameOfLayout(owner->view()
1143 ->buffer()->params.textclass,
1148 case LFUN_LAYOUT_DOCUMENT:
1149 owner->getDialogs()->showLayoutDocument();
1152 case LFUN_LAYOUT_PARAGRAPH:
1153 owner->getDialogs()->showLayoutParagraph();
1156 case LFUN_LAYOUT_CHARACTER:
1157 owner->getDialogs()->showLayoutCharacter();
1160 case LFUN_LAYOUT_TABULAR:
1161 if (owner->view()->theLockingInset()) {
1162 if (owner->view()->theLockingInset()->LyxCode()==Inset::TABULAR_CODE) {
1163 InsetTabular * inset = static_cast<InsetTabular *>
1164 (owner->view()->theLockingInset());
1165 inset->OpenLayoutDialog(owner->view());
1166 } else if (owner->view()->theLockingInset()->
1167 GetFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1168 InsetTabular * inset = static_cast<InsetTabular *>(
1169 owner->view()->theLockingInset()->GetFirstLockingInsetOfType(Inset::TABULAR_CODE));
1170 inset->OpenLayoutDialog(owner->view());
1175 case LFUN_LAYOUT_PREAMBLE:
1176 owner->getDialogs()->showPreamble();
1179 case LFUN_LAYOUT_SAVE_DEFAULT:
1180 MenuLayoutSave(owner->view());
1183 case LFUN_DROP_LAYOUTS_CHOICE:
1184 owner->getToolbar()->openLayoutList();
1187 case LFUN_MENU_OPEN_BY_NAME:
1188 owner->getMenubar()->openByName(argument);
1189 break; // RVDK_PATCH_5
1191 case LFUN_SPELLCHECK:
1192 if (lyxrc.isp_command != "none")
1193 ShowSpellChecker(owner->view());
1194 break; // RVDK_PATCH_5
1196 // --- lyxserver commands ----------------------------
1200 setMessage(owner->buffer()->fileName());
1201 lyxerr.debug() << "FNAME["
1202 << owner->buffer()->fileName()
1210 dispatch_buffer = buf;
1211 lyxserver->notifyClient(dispatch_buffer);
1215 case LFUN_GOTOFILEROW:
1217 char file_name[100];
1219 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1221 // Must replace extension of the file to be .lyx and get full path
1222 string s = ChangeExtension(string(file_name), ".lyx");
1224 // Either change buffer or load the file
1225 if (bufferlist.exists(s))
1226 owner->view()->buffer(bufferlist.getBuffer(s));
1228 owner->view()->buffer(bufferlist.loadLyXFile(s));
1231 owner->view()->setCursorFromRow(row);
1234 owner->view()->center();
1238 case LFUN_GOTO_PARAGRAPH:
1240 istringstream istr(argument.c_str());
1244 LyXParagraph * par = TEXT()->GetParFromID(id);
1249 TEXT()->SetCursor(owner->view(), par, 0);
1250 owner->view()->setState();
1254 owner->view()->center();
1261 int const qa = lyxaction.LookupFunc(argument);
1262 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1266 // --- toolbar ----------------------------------
1267 case LFUN_PUSH_TOOLBAR:
1269 int nth = strToInt(argument);
1271 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1273 owner->getToolbar()->push(nth);
1278 case LFUN_ADD_TO_TOOLBAR:
1280 if (lyxerr.debugging(Debug::GUI)) {
1281 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1282 "argument = `" << argument << '\'' << endl;
1284 string tmp(argument);
1285 //lyxerr <<string("Argument: ") + argument);
1286 //lyxerr <<string("Tmp : ") + tmp);
1288 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1290 owner->getToolbar()->add(argument, false);
1291 owner->getToolbar()->set();
1296 // --- insert characters ----------------------------------------
1298 // --- Mathed stuff. If we are here, there is no locked inset yet.
1303 if (!greek_kb_flag) {
1305 setMessage(N_("Math greek mode on"));
1312 case LFUN_GREEK_TOGGLE:
1314 greek_kb_flag = greek_kb_flag ? 0 : 2;
1315 if (greek_kb_flag) {
1316 setMessage(N_("Math greek keyboard on"));
1318 setMessage(N_("Math greek keyboard off"));
1323 case LFUN_MATH_EXTERN:
1324 case LFUN_MATH_NUMBER:
1325 case LFUN_MATH_LIMITS:
1327 setErrorMessage(N_("This is only allowed in math mode!"));
1331 case LFUN_MATH_PANEL:
1332 owner->getDialogs()->showMathPanel();
1335 case LFUN_CITATION_CREATE:
1337 InsetCommandParams p( "cite" );
1339 if (!argument.empty()) {
1340 // This should be set at source, ie when typing
1341 // "citation-insert foo" in the minibuffer.
1342 // Question: would pybibliographer also need to be
1343 // changed. Suspect so. Leave as-is therefore.
1344 if (contains(argument, "|")) {
1345 p.setContents( token(argument, '|', 0) );
1346 p.setOptions( token(argument, '|', 1) );
1348 p.setContents( argument );
1350 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1352 owner->getDialogs()->createCitation( p.getAsString() );
1356 case LFUN_CHILDOPEN:
1358 string const filename =
1359 MakeAbsPath(argument,
1360 OnlyPath(owner->buffer()->fileName()));
1361 setMessage(N_("Opening child document ") +
1362 MakeDisplayPath(filename) + "...");
1363 owner->view()->savePosition(0);
1364 if (bufferlist.exists(filename))
1365 owner->view()->buffer(bufferlist.getBuffer(filename));
1367 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1371 case LFUN_TOGGLECURSORFOLLOW:
1372 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1375 case LFUN_KMAP_OFF: // keymap off
1376 owner->getIntl()->KeyMapOn(false);
1379 case LFUN_KMAP_PRIM: // primary keymap
1380 owner->getIntl()->KeyMapPrim();
1383 case LFUN_KMAP_SEC: // secondary keymap
1384 owner->getIntl()->KeyMapSec();
1387 case LFUN_KMAP_TOGGLE: // toggle keymap
1388 owner->getIntl()->ToggleKeyMap();
1393 // argument contains ';'-terminated commands
1394 while (argument.find(';') != string::npos) {
1396 argument = split(argument, first, ';');
1402 case LFUN_DIALOG_PREFERENCES:
1403 owner->getDialogs()->showPreferences();
1406 case LFUN_SAVEPREFERENCES:
1408 Path p(user_lyxdir);
1409 lyxrc.write("preferences");
1413 case LFUN_SCREEN_FONT_UPDATE:
1415 // handle the screen font changes.
1417 lyxrc.set_font_norm_type();
1418 fontloader.update();
1419 // Of course we should only do the resize and the textcache.clear
1420 // if values really changed...but not very important right now. (Lgb)
1421 // All buffers will need resize
1422 bufferlist.resize();
1423 // We also need to empty the textcache so that
1424 // the buffer will be formatted correctly after
1430 case LFUN_SET_COLOR:
1433 string const x11_name = split(argument, lyx_name, ' ');
1434 if (lyx_name.empty() || x11_name.empty()) {
1435 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1440 if (!lcolor.setColor(lyx_name, x11_name)) {
1441 static string const err1 (N_("Set-color \""));
1442 static string const err2 (
1443 N_("\" failed - color is undefined "
1444 "or may not be redefined"));
1445 setErrorMessage(_(err1) + lyx_name + _(err2));
1448 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1449 owner->view()->redraw();
1454 owner->message(argument);
1457 case LFUN_MESSAGE_PUSH:
1458 owner->messagePush(argument);
1461 case LFUN_MESSAGE_POP:
1462 owner->messagePop();
1466 // Then if it was none of the above
1467 if (!owner->view()->Dispatch(action, argument))
1468 lyxerr << "A truly unknown func ["
1469 << action << "]!" << endl;
1475 string const res = getMessage();
1478 if (!commandshortcut.empty()) {
1479 owner->getMiniBuffer()->addSet(commandshortcut);
1482 string const msg(_(res) + ' ' + commandshortcut);
1483 owner->message(msg);
1490 void LyXFunc::setupLocalKeymap()
1492 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1493 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1497 void LyXFunc::MenuNew(bool fromTemplate)
1499 string initpath = lyxrc.document_path;
1501 if (owner->view()->available()) {
1502 string const trypath = owner->buffer()->filepath;
1503 // If directory is writeable, use this as default.
1504 if (IsDirWriteable(trypath) == 1)
1508 static int newfile_number = 0;
1511 if (lyxrc.new_ask_filename) {
1512 FileDialog fileDlg(owner, _("Enter filename for new document"),
1513 LFUN_SELECT_FILE_SYNC,
1514 make_pair(string(_("Documents")),
1515 string(lyxrc.document_path)),
1516 make_pair(string(_("Templates")),
1517 string(lyxrc.template_path)));
1519 FileDialog::Result result =
1520 fileDlg.Select(initpath,
1521 _("*.lyx|LyX Documents (*.lyx)"),
1524 if (result.second.empty()) {
1525 owner->message(_("Canceled."));
1526 lyxerr.debug() << "New Document Cancelled." << endl;
1530 // get absolute path of file and make sure the filename ends
1532 s = MakeAbsPath(result.second);
1533 if (!IsLyXFilename(s))
1536 // Check if the document already is open
1537 if (bufferlist.exists(s)) {
1538 switch (AskConfirmation(_("Document is already open:"),
1539 MakeDisplayPath(s, 50),
1540 _("Do you want to close that document now?\n"
1541 "('No' will just switch to the open version)")))
1543 case 1: // Yes: close the document
1544 if (!bufferlist.close(bufferlist.getBuffer(s)))
1545 // If close is canceled, we cancel here too.
1548 case 2: // No: switch to the open document
1549 owner->view()->buffer(bufferlist.getBuffer(s));
1551 case 3: // Cancel: Do nothing
1552 owner->message(_("Canceled."));
1556 // Check whether the file already exists
1557 if (IsLyXFilename(s)) {
1559 if (fi.readable() &&
1560 AskQuestion(_("File already exists:"),
1561 MakeDisplayPath(s, 50),
1562 _("Do you want to open the document?"))) {
1564 owner->message(_("Opening document") + ' '
1565 + MakeDisplayPath(s) + "...");
1566 XFlush(fl_get_display());
1567 owner->view()->buffer(
1568 bufferlist.loadLyXFile(s));
1569 owner->message(_("Document") + ' '
1570 + MakeDisplayPath(s) + ' '
1576 s = AddName(lyxrc.document_path,
1577 "newfile" + tostr(++newfile_number) + ".lyx");
1579 while (bufferlist.exists(s) || fi.readable()) {
1581 s = AddName(lyxrc.document_path,
1582 "newfile" + tostr(newfile_number) +
1588 // The template stuff
1591 FileDialog fileDlg(owner, _("Select template file"),
1592 LFUN_SELECT_FILE_SYNC,
1593 make_pair(string(_("Documents")),
1594 string(lyxrc.document_path)),
1595 make_pair(string(_("Templates")),
1596 string(lyxrc.template_path)));
1598 FileDialog::Result result =
1599 fileDlg.Select(initpath,
1600 _("*.lyx|LyX Documents (*.lyx)"));
1602 if (result.first == FileDialog::Later)
1605 string const fname = result.second;
1612 // find a free buffer
1613 lyxerr.debug() << "Find a free buffer." << endl;
1614 owner->view()->buffer(bufferlist.newFile(s, templname));
1618 void LyXFunc::Open(string const & fname)
1620 string initpath = lyxrc.document_path;
1622 if (owner->view()->available()) {
1623 string const trypath = owner->buffer()->filepath;
1624 // If directory is writeable, use this as default.
1625 if (IsDirWriteable(trypath) == 1)
1631 if (fname.empty()) {
1632 FileDialog fileDlg(owner, _("Select document to open"),
1634 make_pair(string(_("Documents")),
1635 string(lyxrc.document_path)),
1636 make_pair(string(_("Examples")),
1637 string(AddPath(system_lyxdir, "examples"))));
1639 FileDialog::Result result =
1640 fileDlg.Select(initpath,
1641 "*.lyx|LyX Documents (*.lyx)");
1643 if (result.first == FileDialog::Later)
1646 filename = result.second;
1648 // check selected filename
1649 if (filename.empty()) {
1650 owner->message(_("Canceled."));
1656 // get absolute path of file and make sure the filename ends
1658 filename = MakeAbsPath(filename);
1659 if (!IsLyXFilename(filename))
1663 owner->message(_("Opening document") + ' '
1664 + MakeDisplayPath(filename) + "...");
1665 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1667 owner->view()->buffer(openbuf);
1668 owner->message(_("Document") + ' '
1669 + MakeDisplayPath(filename)
1670 + ' ' + _("opened."));
1672 owner->message(_("Could not open document") + ' '
1673 + MakeDisplayPath(filename));
1678 // checks for running without gui are missing.
1679 void LyXFunc::doImport(string const & argument)
1682 string filename = split(argument, format, ' ');
1683 lyxerr.debug() << "LyXFunc::doImport: " << format
1684 << " file: " << filename << endl;
1686 if (filename.empty()) { // need user interaction
1687 string initpath = lyxrc.document_path;
1689 if (owner->view()->available()) {
1690 string const trypath = owner->buffer()->filepath;
1691 // If directory is writeable, use this as default.
1692 if (IsDirWriteable(trypath) == 1)
1696 string const text = _("Select ") + formats.PrettyName(format)
1697 + _(" file to import");
1699 FileDialog fileDlg(owner, text,
1701 make_pair(string(_("Documents")),
1702 string(lyxrc.document_path)),
1703 make_pair(string(_("Examples")),
1704 string(AddPath(system_lyxdir, "examples"))));
1706 string const extension = "*." + formats.Extension(format)
1707 + "| " + formats.PrettyName(format)
1708 + " (*." + formats.Extension(format) + ")";
1710 FileDialog::Result result = fileDlg.Select(initpath,
1713 if (result.first == FileDialog::Later)
1716 filename = result.second;
1718 // check selected filename
1719 if (filename.empty())
1720 owner->message(_("Canceled."));
1723 // still no filename? abort
1724 if (filename.empty())
1727 // get absolute path of file
1728 filename = MakeAbsPath(filename);
1730 string const lyxfile = ChangeExtension(filename, ".lyx");
1732 // Check if the document already is open
1733 if (bufferlist.exists(lyxfile)) {
1734 switch (AskConfirmation(_("Document is already open:"),
1735 MakeDisplayPath(lyxfile, 50),
1736 _("Do you want to close that document now?\n"
1737 "('No' will just switch to the open version)")))
1739 case 1: // Yes: close the document
1740 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1741 // If close is canceled, we cancel here too.
1744 case 2: // No: switch to the open document
1745 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1747 case 3: // Cancel: Do nothing
1748 owner->message(_("Canceled."));
1753 // Check if a LyX document by the same root exists in filesystem
1754 FileInfo const f(lyxfile, true);
1755 if (f.exist() && !AskQuestion(_("A document by the name"),
1756 MakeDisplayPath(lyxfile),
1757 _("already exists. Overwrite?"))) {
1758 owner->message(_("Canceled"));
1761 // filename should be valid now
1763 Importer::Import(owner, filename, format);
1767 void LyXFunc::reloadBuffer()
1769 string const fn = owner->buffer()->fileName();
1770 if (bufferlist.close(owner->buffer()))
1771 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1775 void LyXFunc::CloseBuffer()
1777 if (bufferlist.close(owner->buffer()) && !quitting) {
1778 if (bufferlist.empty()) {
1779 // need this otherwise SEGV may occur while trying to
1780 // set variables that don't exist
1781 // since there's no current buffer
1782 owner->getDialogs()->hideBufferDependent();
1784 owner->view()->buffer(bufferlist.first());
1790 // Each "owner" should have it's own message method. lyxview and
1791 // the minibuffer would use the minibuffer, but lyxserver would
1792 // send an ERROR signal to its client. Alejandro 970603
1793 // This func is bit problematic when it comes to NLS, to make the
1794 // lyx servers client be language indepenent we must not translate
1795 // strings sent to this func.
1796 void LyXFunc::setErrorMessage(string const & m) const
1798 dispatch_buffer = m;
1803 void LyXFunc::setMessage(string const & m)
1805 dispatch_buffer = m;
1809 void LyXFunc::initMiniBuffer()
1811 string text = _("Welcome to LyX!");
1813 // When meta-fake key is pressed, show the key sequence so far + "M-".
1819 // Else, when a non-complete key sequence is pressed,
1820 // show the available options.
1821 else if (keyseqUncomplete())
1822 text = keyseqOptions();
1824 // Else, show the buffer state.
1825 else if (owner->view()->available()) {
1826 Buffer * tmpbuf = owner->buffer();
1828 string const nicename =
1829 MakeDisplayPath(tmpbuf->fileName());
1830 // Should we do this instead? (kindo like emacs)
1831 // leaves more room for other information
1834 if (tmpbuf->lyxvc.inUse()) {
1836 text += tmpbuf->lyxvc.version();
1838 text += tmpbuf->lyxvc.locker();
1839 if (tmpbuf->isReadonly())
1842 } else if (tmpbuf->isReadonly())
1844 if (!tmpbuf->isLyxClean())
1845 text += _(" (Changed)");
1847 if (text != _("Welcome to LyX!")) // this is a hack
1848 text = _("* No document open *");
1851 owner->message(text);