1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
24 #pragma implementation
27 #include "support/lyxalgo.h"
31 #include "bufferlist.h"
32 #include "BufferView.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 "mathed/math_cursor.h"
63 #include "mathed/math_inset.h"
64 #include "spellchecker.h"
65 #include "minibuffer.h"
68 #include "lyx_gui_misc.h"
69 #include "support/filetools.h"
70 #include "support/FileInfo.h"
71 #include "support/syscall.h"
72 #include "support/lstrings.h"
73 #include "support/path.h"
74 #include "support/lyxfunctional.h"
79 #include "trans_mgr.h"
82 #include "bufferview_funcs.h"
83 #include "frontends/FileDialog.h"
84 #include "frontends/Dialogs.h"
85 #include "frontends/Toolbar.h"
86 #include "frontends/Menubar.h"
87 #include "FloatList.h"
88 #include "converter.h"
91 #include "FontLoader.h"
92 #include "TextCache.h"
100 extern BufferList bufferlist;
101 extern LyXServer * lyxserver;
102 extern int greek_kb_flag;
103 extern bool selection_possible;
104 extern void MenuSendto();
106 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
108 extern void show_symbols_form(LyXFunc *);
110 extern LyXAction lyxaction;
112 extern tex_accent_struct get_accent(kb_action action);
114 extern LyXTextClass::size_type current_layout;
116 extern void ShowLatexLog();
122 class MiniBufferController : public SigC::Object {
125 MiniBufferController() {
129 &MiniBufferController::receiveCommand));
133 &MiniBufferController::receiveArg));
136 void receiveCmd(string const & cmd) {}
138 void receiveArg(string const & arg) {}
146 MiniBufferController mb_ctrl;
152 /* === globals =========================================================== */
154 // Initialization of static member var
155 bool LyXFunc::show_sc = true;
158 LyXFunc::LyXFunc(LyXView * o)
162 lyx_dead_action = LFUN_NOACTION;
163 lyx_calling_dead_action = LFUN_NOACTION;
169 LyXText * LyXFunc::TEXT(bool flag = true) const
172 return owner->view()->text;
173 return owner->view()->getLyXText();
177 // I changed this func slightly. I commented out the ...FinishUndo(),
178 // this means that all places that used to have a moveCursorUpdate, now
179 // have a ...FinishUndo() as the preceeding statement. I have also added
180 // a moveCursorUpdate to some of the functions that updated the cursor, but
181 // that did not show its new position.
183 void LyXFunc::moveCursorUpdate(bool flag, bool selecting)
185 if (selecting || TEXT(flag)->selection.mark()) {
186 TEXT(flag)->setSelection(owner->view());
187 if (TEXT(flag)->bv_owner)
188 owner->view()->toggleToggle();
190 owner->view()->update(TEXT(flag), BufferView::SELECT|BufferView::FITCUR);
191 owner->view()->showCursor();
193 /* ---> Everytime the cursor is moved, show the current font state. */
194 // should this too me moved out of this func?
195 owner->view()->setState();
199 void LyXFunc::handleKeyFunc(kb_action action)
201 char c = keyseq.getiso();
203 if (keyseq.length != -1) c = 0;
205 owner->getIntl()->getTrans()
206 .deadkey(c, get_accent(action).accent, TEXT(false));
207 // Need to reset, in case the minibuffer calls these
211 // copied verbatim from do_accent_char
212 owner->view()->update(TEXT(false),
213 BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
214 TEXT(false)->selection.cursor = TEXT(false)->cursor;
218 void LyXFunc::processKeySym(KeySym keysym, unsigned int state)
222 if (lyxerr.debugging(Debug::KEY)) {
223 char const * tmp = XKeysymToString(keysym);
224 string const stm = (tmp ? tmp : "");
225 lyxerr << "KeySym is "
228 << keysym << "] State is ["
232 // Do nothing if we have nothing (JMarc)
233 if (keysym == NoSymbol) {
234 lyxerr[Debug::KEY] << "Empty kbd action (probably composing)"
239 // Can we be sure that this will work for all X-Windows
240 // implementations? (Lgb)
241 // This code snippet makes lyx ignore some keys. Perhaps
242 // all of them should be explictly mentioned?
243 if ((keysym >= XK_Shift_L && keysym <= XK_Hyper_R)
244 || keysym == XK_Mode_switch || keysym == 0x0) {
248 // Do a one-deep top-level lookup for
249 // cancel and meta-fake keys. RVDK_PATCH_5
250 cancel_meta_seq.reset();
252 int action = cancel_meta_seq.addkey(keysym, state
253 &(ShiftMask|ControlMask
255 if (lyxerr.debugging(Debug::KEY)) {
256 lyxerr << "action first set to [" << action << "]" << endl;
259 // When not cancel or meta-fake, do the normal lookup.
260 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
261 // Mostly, meta_fake_bit = 0. RVDK_PATCH_5.
262 if ((action != LFUN_CANCEL) && (action != LFUN_META_FAKE)) {
263 if (lyxerr.debugging(Debug::KEY)) {
264 lyxerr << "meta_fake_bit is ["
265 << meta_fake_bit << "]" << endl;
267 // remove Caps Lock and Mod2 as a modifiers
268 action = keyseq.addkey(keysym,
269 (state | meta_fake_bit)
270 &(ShiftMask|ControlMask
272 if (lyxerr.debugging(Debug::KEY)) {
273 lyxerr << "action now set to ["
274 << action << "]" << endl;
277 // Dont remove this unless you know what you are doing.
280 if (action == 0) action = LFUN_PREFIX;
282 if (lyxerr.debugging(Debug::KEY)) {
285 lyxerr << "Key [action="
291 // already here we know if it any point in going further
292 // why not return already here if action == -1 and
293 // num_bytes == 0? (Lgb)
295 if (keyseq.length > 1 || keyseq.length < -1) {
302 // It is unknown, but what if we remove all
303 // the modifiers? (Lgb)
304 action = keyseq.addkey(keysym, 0);
306 if (lyxerr.debugging(Debug::KEY)) {
307 lyxerr << "Removing modifiers...\n"
308 << "Action now set to ["
309 << action << "]" << endl;
312 owner->message(_("Unknown function."));
317 if (action == LFUN_SELFINSERT) {
318 // This is very X dependant.
319 unsigned int c = keysym;
321 switch (c & 0x0000FF00) {
322 // latin 1 byte 3 = 0
323 case 0x00000000: break;
324 // latin 2 byte 3 = 1
326 // latin 3 byte 3 = 2
328 // latin 4 byte 3 = 3
330 // latin 8 byte 3 = 18 (0x12)
332 // latin 9 byte 3 = 19 (0x13)
341 argument = static_cast<char>(c);
342 lyxerr[Debug::KEY] << "SelfInsert arg[`"
343 << argument << "']" << endl;
346 bool tmp_sc = show_sc;
348 Dispatch(action, argument);
355 func_status::value_type LyXFunc::getStatus(int ac) const
357 return getStatus(ac, string());
361 func_status::value_type LyXFunc::getStatus(int ac,
362 string const & not_to_use_arg) const
365 func_status::value_type flag = func_status::OK;
367 Buffer * buf = owner->buffer();
369 if (lyxaction.isPseudoAction(ac))
370 action = lyxaction.retrieveActionArg(ac, argument);
372 action = static_cast<kb_action>(ac);
373 if (!not_to_use_arg.empty())
374 argument = not_to_use_arg; // exept here
377 if (action == LFUN_UNKNOWN_ACTION) {
378 setErrorMessage(N_("Unknown action"));
379 return func_status::Unknown;
382 // Check whether we need a buffer
383 if (!lyxaction.funcHasFlag(action, LyXAction::NoBuffer)) {
384 // Yes we need a buffer, do we have one?
387 // Can we use a readonly buffer?
388 if (buf->isReadonly() &&
389 !lyxaction.funcHasFlag(action,
390 LyXAction::ReadOnly)) {
392 setErrorMessage(N_("Document is read-only"));
393 flag |= func_status::Disabled;
397 setErrorMessage(N_("Command not allowed with"
398 "out any document open"));
399 flag |= func_status::Disabled;
404 // I would really like to avoid having this switch and rather try to
405 // encode this in the function itself.
406 bool disable = false;
409 disable = !Exporter::IsExportable(buf, "dvi")
410 || lyxrc.print_command == "none";
413 disable = argument == "fax" &&
414 !Exporter::IsExportable(buf, argument);
417 disable = buf->undostack.empty();
420 disable = buf->redostack.empty();
422 case LFUN_SPELLCHECK:
423 disable = lyxrc.isp_command == "none";
426 disable = lyxrc.chktex_command == "none";
429 disable = !Exporter::IsExportable(buf, "program");
432 case LFUN_INSERTFOOTNOTE:
433 // Disable insertion of floats in a tabular.
435 if (owner->view()->theLockingInset()) {
436 disable = (owner->view()->theLockingInset()->lyxCode() == Inset::TABULAR_CODE) ||
437 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
441 case LFUN_LAYOUT_TABULAR:
443 if (owner->view()->theLockingInset()) {
444 disable = (owner->view()->theLockingInset()->lyxCode() != Inset::TABULAR_CODE) &&
445 !owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE);
449 case LFUN_TABULAR_FEATURE:
451 if (owner->view()->theLockingInset()) {
452 func_status::value_type ret = func_status::Disabled;
453 if (owner->view()->theLockingInset()->lyxCode() == Inset::TABULAR_CODE) {
454 ret = static_cast<InsetTabular *>
455 (owner->view()->theLockingInset())->
457 } else if (owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE)) {
458 ret = static_cast<InsetTabular *>
459 (owner->view()->theLockingInset()->
460 getFirstLockingInsetOfType(Inset::TABULAR_CODE))->
466 static InsetTabular inset(*owner->buffer(), 1, 1);
467 func_status::value_type ret;
470 ret = inset.getStatus(argument);
471 if ((ret & func_status::ToggleOn) ||
472 (ret & func_status::ToggleOff))
473 flag |= func_status::ToggleOff;
477 case LFUN_VC_REGISTER:
478 disable = buf->lyxvc.inUse();
480 case LFUN_VC_CHECKIN:
481 disable = !buf->lyxvc.inUse() || buf->isReadonly();
483 case LFUN_VC_CHECKOUT:
484 disable = !buf->lyxvc.inUse() || !buf->isReadonly();
488 case LFUN_VC_HISTORY:
489 disable = !buf->lyxvc.inUse();
491 case LFUN_BOOKMARK_GOTO:
492 disable = !owner->view()->
493 isSavedPosition(strToUnsignedInt(argument));
494 case LFUN_MATH_VALIGN: {
495 Inset * tli = owner->view()->theLockingInset();
496 if (tli && (tli->lyxCode() == Inset::MATH_CODE
497 || tli->lyxCode() == Inset::MATHMACRO_CODE)) {
498 char align = mathcursor->valign();
503 if (argument.empty()) {
504 flag = func_status::OK;
507 if (!contains("tcb", argument[0])) {
511 if (argument[0] == align)
512 flag |= func_status::ToggleOn;
514 flag |= func_status::ToggleOff;
519 case LFUN_MATH_HALIGN: {
520 Inset * tli = owner->view()->theLockingInset();
521 if (tli && (tli->lyxCode() == Inset::MATH_CODE
522 || tli->lyxCode() == Inset::MATHMACRO_CODE)) {
523 char align = mathcursor->halign();
528 if (argument.empty()) {
529 flag = func_status::OK;
532 if (!contains("lcr", argument[0])) {
536 if (argument[0] == align)
537 flag |= func_status::ToggleOn;
539 flag |= func_status::ToggleOff;
544 case LFUN_MATH_MUTATE: {
545 Inset * tli = owner->view()->theLockingInset();
546 if (tli && (tli->lyxCode() == Inset::MATH_CODE)) {
547 MathInsetTypes type = mathcursor->par()->GetType();
548 func_status::value_type box = func_status::ToggleOff;
549 if (argument == "inline") {
550 if (type == LM_OT_SIMPLE)
551 box = func_status::ToggleOn;
552 } else if (argument == "display") {
553 if (type == LM_OT_EQUATION)
554 box = func_status::ToggleOn;
555 } else if (argument == "eqnarray") {
556 if (type == LM_OT_EQNARRAY)
557 box = func_status::ToggleOn;
558 } else if (argument == "align") {
559 if (type == LM_OT_ALIGN)
560 box = func_status::ToggleOn;
562 box = func_status::OK;
574 flag |= func_status::Disabled;
577 func_status::value_type box = func_status::ToggleOff;
578 LyXFont const & font =
579 TEXT(false)->real_current_font;
582 if (font.emph() == LyXFont::ON)
583 box = func_status::ToggleOn;
586 if (font.noun() == LyXFont::ON)
587 box = func_status::ToggleOn;
590 if (font.series() == LyXFont::BOLD_SERIES)
591 box = func_status::ToggleOn;
595 if (font.latex() == LyXFont::ON)
596 box = func_status::ToggleOn;
599 case LFUN_READ_ONLY_TOGGLE:
600 if (buf->isReadonly())
601 box = func_status::ToggleOn;
604 box = func_status::OK;
614 // temporary dispatch method
615 void LyXFunc::miniDispatch(string const & s)
623 string const LyXFunc::Dispatch(string const & s)
625 // Split command string into command and argument
627 string line = frontStrip(s);
628 string const arg = strip(frontStrip(split(line, cmd, ' ')));
630 return Dispatch(lyxaction.LookupFunc(cmd), arg);
634 string const LyXFunc::Dispatch(int ac,
635 string const & do_not_use_this_arg)
637 lyxerr[Debug::ACTION] << "LyXFunc::Dispatch: action[" << ac
638 <<"] arg[" << do_not_use_this_arg << "]" << endl;
643 // we have not done anything wrong yet.
645 dispatch_buffer.erase();
647 // if action is a pseudo-action, we need the real action
648 if (lyxaction.isPseudoAction(ac)) {
650 action = static_cast<kb_action>
651 (lyxaction.retrieveActionArg(ac, tmparg));
655 action = static_cast<kb_action>(ac);
656 if (!do_not_use_this_arg.empty())
657 argument = do_not_use_this_arg; // except here
660 #ifdef NEW_DISPATCHER
661 // We try do call the most specific dispatcher first:
662 // 1. the lockinginset's dispatch
663 // 2. the bufferview's dispatch
664 // 3. the lyxview's dispatch
667 selection_possible = false;
669 if (owner->view()->available())
670 owner->view()->hideCursor();
672 // We cannot use this function here
673 if (getStatus(ac, do_not_use_this_arg) & func_status::Disabled)
674 goto exit_with_message;
677 if (owner->view()->available() && owner->view()->theLockingInset()) {
678 UpdatableInset::RESULT result;
679 if ((action > 1) || ((action == LFUN_UNKNOWN_ACTION) &&
680 (keyseq.length >= -1)))
682 if ((action==LFUN_UNKNOWN_ACTION) && argument.empty()){
683 argument = keyseq.getiso();
685 // Undo/Redo is a bit tricky for insets.
686 if (action == LFUN_UNDO) {
689 UpdatableInset * inset =
690 owner->view()->theLockingInset();
691 inset->getCursorPos(owner->view(), slx, sly);
692 owner->view()->unlockInset(inset);
693 owner->view()->menuUndo();
694 if (TEXT()->cursor.par()->
695 isInset(TEXT()->cursor.pos())) {
696 inset = static_cast<UpdatableInset*>(
697 TEXT()->cursor.par()->
704 inset->edit(owner->view(),slx,sly,0);
706 } else if (action == LFUN_REDO) {
709 UpdatableInset * inset = owner->view()->
711 inset->getCursorPos(owner->view(), slx, sly);
712 owner->view()->unlockInset(inset);
713 owner->view()->menuRedo();
714 inset = static_cast<UpdatableInset*>(
715 TEXT()->cursor.par()->
719 inset->edit(owner->view(),slx,sly,0);
721 } else if (((result=owner->view()->theLockingInset()->
722 localDispatch(owner->view(), action,
724 UpdatableInset::DISPATCHED) ||
725 (result == UpdatableInset::DISPATCHED_NOUPDATE))
728 //setMessage(N_("Text mode"));
730 case LFUN_UNKNOWN_ACTION:
731 case LFUN_BREAKPARAGRAPH:
733 TEXT()->cursorRight(owner->view());
734 owner->view()->setState();
738 if (!TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
739 TEXT()->cursorRight(owner->view());
740 moveCursorUpdate(true, false);
745 if (TEXT()->cursor.par()->isRightToLeftPar(owner->buffer()->params)) {
746 TEXT()->cursorRight(owner->view());
747 moveCursorUpdate(true, false);
752 if (TEXT()->cursor.row()->next())
753 TEXT()->cursorDown(owner->view());
755 TEXT()->cursorRight(owner->view());
756 moveCursorUpdate(true, false);
766 lyx::Assert(action != LFUN_SELECT_FILE_SYNC);
772 if (!owner->view()->available()) break;
774 // this function should be used always [asierra060396]
775 UpdatableInset * tli =
776 owner->view()->theLockingInset();
778 UpdatableInset * lock = tli->getLockingInset();
781 owner->view()->unlockInset(tli);
782 TEXT()->cursorRight(owner->view());
783 moveCursorUpdate(true, false);
786 tli->unlockInsetInInset(owner->view(),
794 // --- Misc -------------------------------------------
795 case LFUN_WORDFINDFORWARD :
796 case LFUN_WORDFINDBACKWARD : {
797 static string last_search;
798 string searched_string;
800 if (!argument.empty()) {
801 last_search = argument;
802 searched_string = argument;
804 searched_string = last_search;
807 if (!searched_string.empty() &&
808 ((action == LFUN_WORDFINDBACKWARD) ?
809 SearchBackward(owner->view(), searched_string) :
810 SearchForward(owner->view(), searched_string))) {
812 // ??? What is that ???
813 owner->view()->update(TEXT(), BufferView::SELECT|BufferView::FITCUR);
816 // clear the selection (if there is any)
817 owner->view()->toggleSelection();
818 TEXT()->clearSelection(owner->view());
820 // Move cursor so that successive C-s 's will not stand in place.
821 if (action == LFUN_WORDFINDFORWARD )
822 TEXT()->cursorRightOneWord(owner->view());
823 TEXT()->finishUndo();
824 moveCursorUpdate(true, false);
827 // set the new selection
828 // SetSelectionOverLenChars(owner->view()->currentBuffer()->text, iLenSelected);
829 owner->view()->toggleSelection(false);
832 // REMOVED : if (owner->view()->getWorkArea()->focus)
833 owner->view()->showCursor();
839 if (owner->view()->available() && !owner->view()->theLockingInset()) {
840 owner->view()->update(TEXT(),
841 BufferView::SELECT|BufferView::FITCUR);
844 keyseq.print(buf, true);
849 // --- Misc -------------------------------------------
850 case LFUN_EXEC_COMMAND:
852 std::vector<string> allCmds;
853 std::transform(lyxaction.func_begin(), lyxaction.func_end(),
854 std::back_inserter(allCmds), lyx::firster());
855 static std::vector<string> hist;
856 owner->getMiniBuffer()->getString(MiniBuffer::spaces,
861 case LFUN_CANCEL: // RVDK_PATCH_5
864 if (owner->view()->available())
865 // cancel any selection
866 Dispatch(LFUN_MARK_OFF);
867 setMessage(N_("Cancel"));
870 case LFUN_META_FAKE: // RVDK_PATCH_5
872 meta_fake_bit = Mod1Mask;
874 keyseq.print(buf, true);
875 setMessage(buf); // RVDK_PATCH_5
879 case LFUN_READ_ONLY_TOGGLE:
880 if (owner->buffer()->lyxvc.inUse()) {
881 owner->buffer()->lyxvc.toggleReadOnly();
883 owner->buffer()->setReadonly(
884 !owner->buffer()->isReadonly());
888 case LFUN_CENTER: // this is center and redraw.
889 owner->view()->center();
892 // --- Menus -----------------------------------------------
897 case LFUN_MENUNEWTMPLT:
901 case LFUN_CLOSEBUFFER:
906 if (!owner->buffer()->isUnnamed()) {
908 s1 << _("Saving document") << ' '
909 << MakeDisplayPath(owner->buffer()->fileName() + "...");
910 owner->message(s1.str().c_str());
911 MenuWrite(owner->view(), owner->buffer());
913 WriteAs(owner->view(), owner->buffer());
917 WriteAs(owner->view(), owner->buffer(), argument);
920 case LFUN_MENURELOAD:
925 Exporter::Export(owner->buffer(), argument, true);
929 Exporter::Preview(owner->buffer(), argument);
933 Exporter::Export(owner->buffer(), "program", true);
937 MenuRunChktex(owner->buffer());
941 owner->getDialogs()->showPrint();
945 if (argument == "custom")
948 Exporter::Export(owner->buffer(), argument, false);
966 InsetCommandParams p;
969 if (action == LFUN_TOCVIEW)
971 p.setCmdName("tableofcontents");
973 else if (action == LFUN_LOAVIEW )
974 p.setCmdName("listof{algorithm}{List of Algorithms}");
975 else if (action == LFUN_LOFVIEW)
976 p.setCmdName("listoffigures");
978 p.setCmdName("listoftables");
980 owner->getDialogs()->createTOC(p.getAsString());
984 case LFUN_DIALOG_TABULAR_INSERT:
985 owner->getDialogs()->showTabularCreate();
993 AutoSave(owner->view());
997 owner->view()->menuUndo();
1001 owner->view()->menuRedo();
1004 case LFUN_MENUSEARCH:
1005 owner->getDialogs()->showSearch();
1008 case LFUN_REMOVEERRORS:
1009 if (owner->view()->removeAutoInsets()) {
1010 owner->view()->redraw();
1011 owner->view()->fitCursor(TEXT());
1016 changeDepth(owner->view(), TEXT(false), 0);
1019 case LFUN_DEPTH_MIN:
1020 changeDepth(owner->view(), TEXT(false), -1);
1023 case LFUN_DEPTH_PLUS:
1024 changeDepth(owner->view(), TEXT(false), 1);
1028 owner->getDialogs()->setUserFreeFont();
1034 owner->view()->setState();
1039 case LFUN_RECONFIGURE:
1040 Reconfigure(owner->view());
1044 case LFUN_FLOATSOPERATE:
1045 if (argument == "openfoot")
1046 owner->view()->allFloats(1,0);
1047 else if (argument == "closefoot")
1048 owner->view()->allFloats(0,0);
1049 else if (argument == "openfig")
1050 owner->view()->allFloats(1,1);
1051 else if (argument == "closefig")
1052 owner->view()->allFloats(0,1);
1055 #ifdef WITH_WARNINGS
1056 #warning Find another implementation here (or another lyxfunc)!
1059 case LFUN_HELP_COPYRIGHT:
1060 owner->getDialogs()->showCopyright();
1063 case LFUN_HELP_CREDITS:
1064 owner->getDialogs()->showCredits();
1067 case LFUN_HELP_OPEN:
1069 string const arg = argument;
1071 setErrorMessage(N_("Missing argument"));
1074 ProhibitInput(owner->view());
1075 string const fname = i18nLibFileSearch("doc", arg, "lyx");
1076 if (fname.empty()) {
1077 lyxerr << "LyX: unable to find documentation file `"
1078 << arg << "'. Bad installation?" << endl;
1079 AllowInput(owner->view());
1083 str << _("Opening help file") << ' '
1084 << MakeDisplayPath(fname) << "...";
1085 owner->message(str.str().c_str());
1086 owner->view()->buffer(bufferlist.loadLyXFile(fname,false));
1087 AllowInput(owner->view());
1091 case LFUN_HELP_VERSION: {
1092 ProhibitInput(owner->view());
1093 string msg(_("LyX Version "));
1097 fl_show_message(msg.c_str(),
1098 (_("Library directory: ")
1099 + MakeDisplayPath(system_lyxdir)).c_str(),
1100 (_("User directory: ")
1101 + MakeDisplayPath(user_lyxdir)).c_str());
1102 AllowInput(owner->view());
1106 // --- version control -------------------------------
1107 case LFUN_VC_REGISTER:
1109 if (!owner->buffer()->lyxvc.inUse())
1110 owner->buffer()->lyxvc.registrer();
1114 case LFUN_VC_CHECKIN:
1116 if (owner->buffer()->lyxvc.inUse()
1117 && !owner->buffer()->isReadonly())
1118 owner->buffer()->lyxvc.checkIn();
1122 case LFUN_VC_CHECKOUT:
1124 if (owner->buffer()->lyxvc.inUse()
1125 && owner->buffer()->isReadonly())
1126 owner->buffer()->lyxvc.checkOut();
1130 case LFUN_VC_REVERT:
1132 owner->buffer()->lyxvc.revert();
1138 owner->buffer()->lyxvc.undoLast();
1142 case LFUN_VC_HISTORY:
1144 owner->getDialogs()->showVCLogFile();
1148 // --- buffers ----------------------------------------
1150 case LFUN_SWITCHBUFFER:
1151 owner->view()->buffer(bufferlist.getBuffer(argument));
1156 // servercmd: argument must be <file>:<template>
1157 Buffer * tmpbuf = NewLyxFile(argument);
1159 owner->view()->buffer(tmpbuf);
1163 case LFUN_FILE_OPEN:
1167 case LFUN_LATEX_LOG:
1168 owner->getDialogs()->showLogFile();
1173 lyxerr[Debug::INFO] << "LFUN_LAYOUTNO: (arg) " << argument << endl;
1174 int sel = strToInt(argument);
1175 lyxerr[Debug::INFO] << "LFUN_LAYOUTNO: (sel) "<< sel << endl;
1177 // Should this give a setMessage instead?
1179 return string(); // illegal argument
1181 --sel; // sel 1..., but layout 0...
1183 // Pretend we got the name instead.
1184 Dispatch(int(LFUN_LAYOUT),
1185 textclasslist.NameOfLayout(owner->view()
1186 ->buffer()->params.textclass,
1191 case LFUN_LAYOUT_DOCUMENT:
1192 owner->getDialogs()->showDocument();
1195 case LFUN_LAYOUT_PARAGRAPH:
1196 owner->getDialogs()->showParagraph();
1199 case LFUN_LAYOUT_CHARACTER:
1200 owner->getDialogs()->showCharacter();
1203 case LFUN_LAYOUT_TABULAR:
1204 if (owner->view()->theLockingInset()) {
1205 if (owner->view()->theLockingInset()->lyxCode()==Inset::TABULAR_CODE) {
1206 InsetTabular * inset = static_cast<InsetTabular *>
1207 (owner->view()->theLockingInset());
1208 inset->openLayoutDialog(owner->view());
1209 } else if (owner->view()->theLockingInset()->
1210 getFirstLockingInsetOfType(Inset::TABULAR_CODE)!=0) {
1211 InsetTabular * inset = static_cast<InsetTabular *>(
1212 owner->view()->theLockingInset()->getFirstLockingInsetOfType(Inset::TABULAR_CODE));
1213 inset->openLayoutDialog(owner->view());
1218 case LFUN_LAYOUT_PREAMBLE:
1219 owner->getDialogs()->showPreamble();
1222 case LFUN_LAYOUT_SAVE_DEFAULT:
1223 MenuLayoutSave(owner->view());
1226 case LFUN_DROP_LAYOUTS_CHOICE:
1227 owner->getToolbar()->openLayoutList();
1230 case LFUN_MENU_OPEN_BY_NAME:
1231 owner->getMenubar()->openByName(argument);
1232 break; // RVDK_PATCH_5
1234 case LFUN_SPELLCHECK:
1235 if (lyxrc.isp_command != "none")
1236 ShowSpellChecker(owner->view());
1237 break; // RVDK_PATCH_5
1239 // --- lyxserver commands ----------------------------
1243 setMessage(owner->buffer()->fileName());
1244 lyxerr[Debug::INFO] << "FNAME["
1245 << owner->buffer()->fileName()
1253 dispatch_buffer = buf;
1254 lyxserver->notifyClient(dispatch_buffer);
1258 case LFUN_GOTOFILEROW:
1261 char file_name[100];
1263 ::sscanf(argument.c_str(), " %s %d", file_name, &row);
1267 istringstream istr(argument.c_str());
1268 istr >> file_name >> row;
1270 // Must replace extension of the file to be .lyx and get full path
1271 string const s(ChangeExtension(file_name, ".lyx"));
1273 // Either change buffer or load the file
1274 if (bufferlist.exists(s)) {
1275 owner->view()->buffer(bufferlist.getBuffer(s));
1277 owner->view()->buffer(bufferlist.loadLyXFile(s));
1281 owner->view()->setCursorFromRow(row);
1284 owner->view()->center();
1288 case LFUN_GOTO_PARAGRAPH:
1290 istringstream istr(argument.c_str());
1294 Paragraph * par = TEXT()->getParFromID(id);
1296 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1297 << id << "]" << std::endl;
1302 TEXT()->setCursor(owner->view(), par, 0);
1303 owner->view()->setState();
1307 owner->view()->center();
1314 int const qa = lyxaction.LookupFunc(argument);
1315 setMessage(lyxaction.helpText(static_cast<kb_action>(qa)));
1319 // --- toolbar ----------------------------------
1320 case LFUN_PUSH_TOOLBAR:
1322 int nth = strToInt(argument);
1324 setErrorMessage(N_("Push-toolbar needs argument > 0"));
1326 owner->getToolbar()->push(nth);
1331 case LFUN_ADD_TO_TOOLBAR:
1333 if (lyxerr.debugging(Debug::GUI)) {
1334 lyxerr << "LFUN_ADD_TO_TOOLBAR:"
1335 "argument = `" << argument << '\'' << endl;
1337 string tmp(argument);
1338 //lyxerr <<string("Argument: ") + argument);
1339 //lyxerr <<string("Tmp : ") + tmp);
1341 setErrorMessage(N_("Usage: toolbar-add-to <LyX command>"));
1343 owner->getToolbar()->add(argument, false);
1344 owner->getToolbar()->set();
1349 // --- insert characters ----------------------------------------
1351 // --- Mathed stuff. If we are here, there is no locked inset yet.
1356 if (!greek_kb_flag) {
1358 setMessage(N_("Math greek mode on"));
1365 case LFUN_GREEK_TOGGLE:
1367 greek_kb_flag = greek_kb_flag ? 0 : 2;
1368 if (greek_kb_flag) {
1369 setMessage(N_("Math greek keyboard on"));
1371 setMessage(N_("Math greek keyboard off"));
1376 case LFUN_MATH_EXTERN:
1377 case LFUN_MATH_NUMBER:
1378 case LFUN_MATH_NONUMBER:
1379 case LFUN_MATH_LIMITS:
1381 setErrorMessage(N_("This is only allowed in math mode!"));
1385 case LFUN_MATH_PANEL:
1386 owner->getDialogs()->showMathPanel();
1389 case LFUN_CITATION_CREATE:
1391 InsetCommandParams p( "cite" );
1393 if (!argument.empty()) {
1394 // This should be set at source, ie when typing
1395 // "citation-insert foo" in the minibuffer.
1396 // Question: would pybibliographer also need to be
1397 // changed. Suspect so. Leave as-is therefore.
1398 if (contains(argument, "|")) {
1399 p.setContents( token(argument, '|', 0) );
1400 p.setOptions( token(argument, '|', 1) );
1402 p.setContents( argument );
1404 Dispatch(LFUN_CITATION_INSERT, p.getAsString());
1406 owner->getDialogs()->createCitation( p.getAsString() );
1410 case LFUN_CHILDOPEN:
1412 string const filename =
1413 MakeAbsPath(argument,
1414 OnlyPath(owner->buffer()->fileName()));
1415 setMessage(N_("Opening child document ") +
1416 MakeDisplayPath(filename) + "...");
1417 owner->view()->savePosition(0);
1418 if (bufferlist.exists(filename))
1419 owner->view()->buffer(bufferlist.getBuffer(filename));
1421 owner->view()->buffer(bufferlist.loadLyXFile(filename));
1425 case LFUN_TOGGLECURSORFOLLOW:
1426 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1429 case LFUN_KMAP_OFF: // keymap off
1430 owner->getIntl()->KeyMapOn(false);
1433 case LFUN_KMAP_PRIM: // primary keymap
1434 owner->getIntl()->KeyMapPrim();
1437 case LFUN_KMAP_SEC: // secondary keymap
1438 owner->getIntl()->KeyMapSec();
1441 case LFUN_KMAP_TOGGLE: // toggle keymap
1442 owner->getIntl()->ToggleKeyMap();
1447 // argument contains ';'-terminated commands
1448 while (argument.find(';') != string::npos) {
1450 argument = split(argument, first, ';');
1456 case LFUN_DIALOG_PREFERENCES:
1457 owner->getDialogs()->showPreferences();
1460 case LFUN_SAVEPREFERENCES:
1462 Path p(user_lyxdir);
1463 lyxrc.write("preferences");
1467 case LFUN_SCREEN_FONT_UPDATE:
1469 // handle the screen font changes.
1471 lyxrc.set_font_norm_type();
1472 fontloader.update();
1473 // Of course we should only do the resize and the textcache.clear
1474 // if values really changed...but not very important right now. (Lgb)
1475 // All visible buffers will need resize
1477 // We also need to empty the textcache so that
1478 // the buffer will be formatted correctly after
1484 case LFUN_SET_COLOR:
1487 string const x11_name = split(argument, lyx_name, ' ');
1488 if (lyx_name.empty() || x11_name.empty()) {
1489 setErrorMessage(N_("Syntax: set-color <lyx_name>"
1494 if (!lcolor.setColor(lyx_name, x11_name)) {
1495 static string const err1 (N_("Set-color \""));
1496 static string const err2 (
1497 N_("\" failed - color is undefined "
1498 "or may not be redefined"));
1499 setErrorMessage(_(err1) + lyx_name + _(err2));
1502 lyxColorHandler->updateColor(lcolor.getFromLyXName(lyx_name));
1503 owner->view()->redraw();
1508 owner->message(argument);
1511 case LFUN_MESSAGE_PUSH:
1512 owner->messagePush(argument);
1515 case LFUN_MESSAGE_POP:
1516 owner->messagePop();
1520 // Then if it was none of the above
1521 if (!owner->view()->Dispatch(action, argument))
1522 lyxerr << "A truly unknown func ["
1523 << action << "]!" << endl;
1529 commandshortcut.erase();
1531 if (lyxrc.display_shortcuts && show_sc) {
1532 if (action != LFUN_SELFINSERT) {
1533 // Put name of command and list of shortcuts
1534 // for it in minibuffer
1535 string comname = lyxaction.getActionName(action);
1537 int pseudoaction = action;
1538 bool argsadded = false;
1540 if (!argument.empty()) {
1541 // If we have the command with argument,
1544 lyxaction.searchActionArg(action,
1547 if (pseudoaction == -1) {
1548 pseudoaction = action;
1550 comname += " " + argument;
1555 string const shortcuts =
1556 toplevel_keymap->findbinding(pseudoaction);
1558 if (!shortcuts.empty()) {
1559 comname += ": " + shortcuts;
1560 } else if (!argsadded) {
1561 comname += " " + argument;
1564 if (!comname.empty()) {
1565 comname = strip(comname);
1566 commandshortcut = "(" + comname + ')';
1568 // Here we could even add a small pause,
1569 // to annoy the user and make him learn
1571 // No! That will just annoy, not teach
1572 // anything. The user will read the messages
1573 // if they are interested. (Asger)
1578 string const res = getMessage();
1581 if (!commandshortcut.empty()) {
1582 owner->getMiniBuffer()->addSet(commandshortcut);
1585 string const msg(_(res) + ' ' + commandshortcut);
1586 owner->message(msg);
1593 void LyXFunc::setupLocalKeymap()
1595 keyseq.stdmap = keyseq.curmap = toplevel_keymap.get();
1596 cancel_meta_seq.stdmap = cancel_meta_seq.curmap = toplevel_keymap.get();
1600 void LyXFunc::MenuNew(bool fromTemplate)
1602 string initpath = lyxrc.document_path;
1604 if (owner->view()->available()) {
1605 string const trypath = owner->buffer()->filepath;
1606 // If directory is writeable, use this as default.
1607 if (IsDirWriteable(trypath) == 1)
1611 static int newfile_number;
1614 if (lyxrc.new_ask_filename) {
1615 FileDialog fileDlg(owner, _("Enter filename for new document"),
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)"),
1627 if (result.second.empty()) {
1628 owner->message(_("Canceled."));
1629 lyxerr[Debug::INFO] << "New Document Cancelled." << endl;
1633 // get absolute path of file and make sure the filename ends
1635 s = MakeAbsPath(result.second);
1636 if (!IsLyXFilename(s))
1639 // Check if the document already is open
1640 if (bufferlist.exists(s)) {
1641 switch (AskConfirmation(_("Document is already open:"),
1642 MakeDisplayPath(s, 50),
1643 _("Do you want to close that document now?\n"
1644 "('No' will just switch to the open version)")))
1646 case 1: // Yes: close the document
1647 if (!bufferlist.close(bufferlist.getBuffer(s)))
1648 // If close is canceled, we cancel here too.
1651 case 2: // No: switch to the open document
1652 owner->view()->buffer(bufferlist.getBuffer(s));
1654 case 3: // Cancel: Do nothing
1655 owner->message(_("Canceled."));
1659 // Check whether the file already exists
1661 if (fi.readable() &&
1662 AskQuestion(_("File already exists:"),
1663 MakeDisplayPath(s, 50),
1664 _("Do you want to open the document?"))) {
1666 string const disp_fn(MakeDisplayPath(s));
1669 str << _("Opening document") << ' '
1670 << disp_fn << "...";
1672 owner->message(str.str().c_str());
1673 //XFlush(fl_get_display());
1674 owner->view()->buffer(bufferlist.loadLyXFile(s));
1676 str2 << _("Document") << ' '
1677 << disp_fn << ' ' << _("opened.");
1679 owner->message(str2.str().c_str());
1684 s = AddName(lyxrc.document_path,
1685 "newfile" + tostr(++newfile_number) + ".lyx");
1687 while (bufferlist.exists(s) || fi.readable()) {
1689 s = AddName(lyxrc.document_path,
1690 "newfile" + tostr(newfile_number) +
1696 // The template stuff
1699 FileDialog fileDlg(owner, _("Select template file"),
1700 LFUN_SELECT_FILE_SYNC,
1701 make_pair(string(_("Documents")),
1702 string(lyxrc.document_path)),
1703 make_pair(string(_("Templates")),
1704 string(lyxrc.template_path)));
1706 FileDialog::Result result =
1707 fileDlg.Select(lyxrc.template_path,
1708 _("*.lyx|LyX Documents (*.lyx)"));
1710 if (result.first == FileDialog::Later)
1713 string const fname = result.second;
1720 // find a free buffer
1721 lyxerr[Debug::INFO] << "Find a free buffer." << endl;
1722 owner->view()->buffer(bufferlist.newFile(s, templname));
1726 void LyXFunc::Open(string const & fname)
1728 string initpath = lyxrc.document_path;
1730 if (owner->view()->available()) {
1731 string const trypath = owner->buffer()->filepath;
1732 // If directory is writeable, use this as default.
1733 if (IsDirWriteable(trypath) == 1)
1739 if (fname.empty()) {
1740 FileDialog fileDlg(owner, _("Select document to open"),
1742 make_pair(string(_("Documents")),
1743 string(lyxrc.document_path)),
1744 make_pair(string(_("Examples")),
1745 string(AddPath(system_lyxdir, "examples"))));
1747 FileDialog::Result result =
1748 fileDlg.Select(initpath,
1749 "*.lyx|LyX Documents (*.lyx)");
1751 if (result.first == FileDialog::Later)
1754 filename = result.second;
1756 // check selected filename
1757 if (filename.empty()) {
1758 owner->message(_("Canceled."));
1764 // get absolute path of file and add ".lyx" to the filename if
1766 filename = FileSearch(string(), filename, "lyx");
1769 string const disp_fn(MakeDisplayPath(filename));
1772 str << _("Opening document") << ' ' << disp_fn << "...";
1774 owner->message(str.str().c_str());
1776 Buffer * openbuf = bufferlist.loadLyXFile(filename);
1778 owner->view()->buffer(openbuf);
1780 str << _("Document") << ' ' << disp_fn << ' ' << _("opened.");
1781 owner->message(str.str().c_str());
1784 str << _("Could not open docuent") << ' ' << disp_fn;
1785 owner->message(str.str().c_str());
1790 // checks for running without gui are missing.
1791 void LyXFunc::doImport(string const & argument)
1794 string filename = split(argument, format, ' ');
1795 lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
1796 << " file: " << filename << endl;
1798 if (filename.empty()) { // need user interaction
1799 string initpath = lyxrc.document_path;
1801 if (owner->view()->available()) {
1802 string const trypath = owner->buffer()->filepath;
1803 // If directory is writeable, use this as default.
1804 if (IsDirWriteable(trypath) == 1)
1808 string const text = _("Select ") + formats.PrettyName(format)
1809 + _(" file to import");
1811 FileDialog fileDlg(owner, text,
1813 make_pair(string(_("Documents")),
1814 string(lyxrc.document_path)),
1815 make_pair(string(_("Examples")),
1816 string(AddPath(system_lyxdir, "examples"))));
1818 string const extension = "*." + formats.Extension(format)
1819 + "| " + formats.PrettyName(format)
1820 + " (*." + formats.Extension(format) + ")";
1822 FileDialog::Result result = fileDlg.Select(initpath,
1825 if (result.first == FileDialog::Later)
1828 filename = result.second;
1830 // check selected filename
1831 if (filename.empty())
1832 owner->message(_("Canceled."));
1835 // still no filename? abort
1836 if (filename.empty())
1839 // get absolute path of file
1840 filename = MakeAbsPath(filename);
1842 string const lyxfile = ChangeExtension(filename, ".lyx");
1844 // Check if the document already is open
1845 if (bufferlist.exists(lyxfile)) {
1846 switch (AskConfirmation(_("Document is already open:"),
1847 MakeDisplayPath(lyxfile, 50),
1848 _("Do you want to close that document now?\n"
1849 "('No' will just switch to the open version)")))
1851 case 1: // Yes: close the document
1852 if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
1853 // If close is canceled, we cancel here too.
1856 case 2: // No: switch to the open document
1857 owner->view()->buffer(bufferlist.getBuffer(lyxfile));
1859 case 3: // Cancel: Do nothing
1860 owner->message(_("Canceled."));
1865 // Check if a LyX document by the same root exists in filesystem
1866 FileInfo const f(lyxfile, true);
1867 if (f.exist() && !AskQuestion(_("A document by the name"),
1868 MakeDisplayPath(lyxfile),
1869 _("already exists. Overwrite?"))) {
1870 owner->message(_("Canceled"));
1873 // filename should be valid now
1875 Importer::Import(owner, filename, format);
1879 void LyXFunc::reloadBuffer()
1881 string const fn = owner->buffer()->fileName();
1882 if (bufferlist.close(owner->buffer()))
1883 owner->view()->buffer(bufferlist.loadLyXFile(fn));
1887 void LyXFunc::CloseBuffer()
1889 if (bufferlist.close(owner->buffer()) && !quitting) {
1890 if (bufferlist.empty()) {
1891 // need this otherwise SEGV may occur while trying to
1892 // set variables that don't exist
1893 // since there's no current buffer
1894 owner->getDialogs()->hideBufferDependent();
1896 owner->view()->buffer(bufferlist.first());
1902 // Each "owner" should have it's own message method. lyxview and
1903 // the minibuffer would use the minibuffer, but lyxserver would
1904 // send an ERROR signal to its client. Alejandro 970603
1905 // This func is bit problematic when it comes to NLS, to make the
1906 // lyx servers client be language indepenent we must not translate
1907 // strings sent to this func.
1908 void LyXFunc::setErrorMessage(string const & m) const
1910 dispatch_buffer = m;
1915 void LyXFunc::setMessage(string const & m)
1917 dispatch_buffer = m;
1921 void LyXFunc::initMiniBuffer()
1923 string text = _("Welcome to LyX!");
1925 // When meta-fake key is pressed, show the key sequence so far + "M-".
1931 // Else, when a non-complete key sequence is pressed,
1932 // show the available options.
1933 else if (keyseqUncomplete())
1934 text = keyseqOptions();
1936 // Else, show the buffer state.
1937 else if (owner->view()->available()) {
1938 Buffer * tmpbuf = owner->buffer();
1940 string const nicename =
1941 MakeDisplayPath(tmpbuf->fileName());
1942 // Should we do this instead? (kindo like emacs)
1943 // leaves more room for other information
1946 if (tmpbuf->lyxvc.inUse()) {
1948 text += tmpbuf->lyxvc.version();
1950 text += tmpbuf->lyxvc.locker();
1951 if (tmpbuf->isReadonly())
1954 } else if (tmpbuf->isReadonly())
1956 if (!tmpbuf->isLyxClean())
1957 text += _(" (Changed)");
1959 if (text != _("Welcome to LyX!")) // this is a hack
1960 text = _("* No document open *");
1963 owner->message(text);