]> git.lyx.org Git - lyx.git/blob - src/LyXFunc.cpp
Transfer all keyboard related code and actions from LyXFunc to GuiApplication. Part...
[lyx.git] / src / LyXFunc.cpp
1 /**
2  * \file LyXFunc.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Allan Rae
13  * \author Dekel Tsur
14  * \author Martin Vermeer
15  * \author Jürgen Vigna
16  *
17  * Full author contact details are available in file CREDITS.
18  */
19
20 #include <config.h>
21
22 #include "LyXFunc.h"
23
24 #include "LayoutFile.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
27 #include "Buffer.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
31 #include "CmdDef.h"
32 #include "Color.h"
33 #include "Converter.h"
34 #include "Cursor.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
37 #include "Encoding.h"
38 #include "ErrorList.h"
39 #include "Format.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
43 #include "KeyMap.h"
44 #include "Language.h"
45 #include "Lexer.h"
46 #include "LyXAction.h"
47 #include "lyxfind.h"
48 #include "LyX.h"
49 #include "LyXRC.h"
50 #include "LyXVC.h"
51 #include "Paragraph.h"
52 #include "ParagraphParameters.h"
53 #include "ParIterator.h"
54 #include "Row.h"
55 #include "Session.h"
56 #include "SpellChecker.h"
57
58 #include "frontends/alert.h"
59 #include "frontends/Application.h"
60 #include "frontends/KeySymbol.h"
61 #include "frontends/LyXView.h"
62 #include "frontends/Selection.h"
63
64 #include "support/debug.h"
65 #include "support/environment.h"
66 #include "support/FileName.h"
67 #include "support/filetools.h"
68 #include "support/gettext.h"
69 #include "support/lassert.h"
70 #include "support/lstrings.h"
71 #include "support/Package.h"
72 #include "support/convert.h"
73 #include "support/os.h"
74
75 #include <sstream>
76 #include <vector>
77
78 using namespace std;
79 using namespace lyx::support;
80
81 namespace lyx {
82
83 using frontend::LyXView;
84
85 namespace Alert = frontend::Alert;
86
87 LyXFunc::LyXFunc()
88 {
89 }
90
91
92 //FIXME: bookmark handling is a frontend issue. This code should be transferred
93 // to GuiView and be GuiView and be window dependent.
94 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
95 {
96         LyXView * lv = theApp()->currentWindow();
97         LASSERT(lv, /**/);
98         if (!theSession().bookmarks().isValid(idx))
99                 return;
100         BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
101         LASSERT(!bm.filename.empty(), /**/);
102         string const file = bm.filename.absFilename();
103         // if the file is not opened, open it.
104         if (!theBufferList().exists(bm.filename)) {
105                 if (openFile)
106                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
107                 else
108                         return;
109         }
110         // open may fail, so we need to test it again
111         if (!theBufferList().exists(bm.filename))
112                 return;
113
114         // bm can be changed when saving
115         BookmarksSection::Bookmark tmp = bm;
116
117         // Special case idx == 0 used for back-from-back jump navigation
118         if (idx == 0)
119                 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
120
121         // if the current buffer is not that one, switch to it.
122         if (!lv->documentBufferView()
123                 || lv->documentBufferView()->buffer().fileName() != tmp.filename) {
124                 if (!switchToBuffer)
125                         return;
126                 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
127         }
128
129         // moveToPosition try paragraph id first and then paragraph (pit, pos).
130         if (!lv->documentBufferView()->moveToPosition(
131                 tmp.bottom_pit, tmp.bottom_pos, tmp.top_id, tmp.top_pos))
132                 return;
133
134         // bm changed
135         if (idx == 0)
136                 return;
137
138         // Cursor jump succeeded!
139         Cursor const & cur = lv->documentBufferView()->cursor();
140         pit_type new_pit = cur.pit();
141         pos_type new_pos = cur.pos();
142         int new_id = cur.paragraph().id();
143
144         // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
145         // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
146         if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos 
147                 || bm.top_id != new_id) {
148                 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
149                         new_pit, new_pos, new_id);
150         }
151 }
152
153
154 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
155 {
156         //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
157         FuncStatus flag;
158
159         if (cmd.action == LFUN_NOACTION) {
160                 flag.message(from_utf8(N_("Nothing to do")));
161                 flag.setEnabled(false);
162                 return flag;
163         }
164
165         switch (cmd.action) {
166         case LFUN_UNKNOWN_ACTION:
167                 flag.unknown(true);
168                 flag.setEnabled(false);
169                 break;
170
171         default:
172                 break;
173         }
174
175         if (flag.unknown()) {
176                 flag.message(from_utf8(N_("Unknown action")));
177                 return flag;
178         }
179
180         if (!flag.enabled()) {
181                 if (flag.message().empty())
182                         flag.message(from_utf8(N_("Command disabled")));
183                 return flag;
184         }
185
186         // I would really like to avoid having this switch and rather try to
187         // encode this in the function itself.
188         // -- And I'd rather let an inset decide which LFUNs it is willing
189         // to handle (Andre')
190         bool enable = true;
191         switch (cmd.action) {
192
193         // This could be used for the no-GUI version. The GUI version is handled in
194         // LyXView::getStatus(). See above.
195         /*
196         case LFUN_BUFFER_WRITE:
197         case LFUN_BUFFER_WRITE_AS: {
198                 Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
199                 enable = b && (b->isUnnamed() || !b->isClean());
200                 break;
201         }
202         */
203
204         case LFUN_BOOKMARK_GOTO: {
205                 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
206                 enable = theSession().bookmarks().isValid(num);
207                 break;
208         }
209
210         case LFUN_BOOKMARK_CLEAR:
211                 enable = theSession().bookmarks().hasValid();
212                 break;
213
214         // this one is difficult to get right. As a half-baked
215         // solution, we consider only the first action of the sequence
216         case LFUN_COMMAND_SEQUENCE: {
217                 // argument contains ';'-terminated commands
218                 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
219                 FuncRequest func(lyxaction.lookupFunc(firstcmd));
220                 func.origin = cmd.origin;
221                 flag = getStatus(func);
222                 break;
223         }
224
225         // we want to check if at least one of these is enabled
226         case LFUN_COMMAND_ALTERNATIVES: {
227                 // argument contains ';'-terminated commands
228                 string arg = to_utf8(cmd.argument());
229                 while (!arg.empty()) {
230                         string first;
231                         arg = split(arg, first, ';');
232                         FuncRequest func(lyxaction.lookupFunc(first));
233                         func.origin = cmd.origin;
234                         flag = getStatus(func);
235                         // if this one is enabled, the whole thing is
236                         if (flag.enabled())
237                                 break;
238                 }
239                 break;
240         }
241
242         case LFUN_CALL: {
243                 FuncRequest func;
244                 string name = to_utf8(cmd.argument());
245                 if (theTopLevelCmdDef().lock(name, func)) {
246                         func.origin = cmd.origin;
247                         flag = getStatus(func);
248                         theTopLevelCmdDef().release(name);
249                 } else {
250                         // catch recursion or unknown command
251                         // definition. all operations until the
252                         // recursion or unknown command definition
253                         // occurs are performed, so set the state to
254                         // enabled
255                         enable = true;
256                 }
257                 break;
258         }
259
260         case LFUN_DROP_LAYOUTS_CHOICE:
261         case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
262         case LFUN_REPEAT:
263         case LFUN_PREFERENCES_SAVE:
264         case LFUN_INSET_EDIT:
265         case LFUN_BUFFER_SAVE_AS_DEFAULT:
266                 // these are handled in our dispatch()
267                 break;
268
269         default:
270                 if (!theApp()) {
271                         enable = false;
272                         break;
273                 }
274                 if (theApp()->getStatus(cmd, flag))
275                         break;
276
277                 // Does the view know something?
278                 LyXView * lv = theApp()->currentWindow();
279                 if (!lv) {
280                         enable = false;
281                         break;
282                 }
283                 if (lv->getStatus(cmd, flag))
284                         break;
285
286                 BufferView * bv = lv->currentBufferView();
287                 BufferView * doc_bv = lv->documentBufferView();
288                 // If we do not have a BufferView, then other functions are disabled
289                 if (!bv) {
290                         enable = false;
291                         break;
292                 }
293                 // try the BufferView
294                 bool decided = bv->getStatus(cmd, flag);
295                 if (!decided)
296                         // try the Buffer
297                         decided = bv->buffer().getStatus(cmd, flag);
298                 if (!decided && doc_bv)
299                         // try the Document Buffer
300                         decided = doc_bv->buffer().getStatus(cmd, flag);
301         }
302
303         if (!enable)
304                 flag.setEnabled(false);
305
306         // the default error message if we disable the command
307         if (!flag.enabled() && flag.message().empty())
308                 flag.message(from_utf8(N_("Command disabled")));
309
310         return flag;
311 }
312
313 /// send a post-dispatch status message
314 static docstring sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
315 {
316         const bool verbose = (cmd.origin == FuncRequest::MENU
317                               || cmd.origin == FuncRequest::TOOLBAR
318                               || cmd.origin == FuncRequest::COMMANDBUFFER);
319
320         if (cmd.action == LFUN_SELF_INSERT || !verbose) {
321                 LYXERR(Debug::ACTION, "dispatch msg is " << msg);
322                 return msg;
323         }
324
325         docstring dispatch_msg = msg;
326         if (!dispatch_msg.empty())
327                 dispatch_msg += ' ';
328
329         docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
330
331         bool argsadded = false;
332
333         if (!cmd.argument().empty()) {
334                 if (cmd.action != LFUN_UNKNOWN_ACTION) {
335                         comname += ' ' + cmd.argument();
336                         argsadded = true;
337                 }
338         }
339         docstring const shortcuts = theTopLevelKeymap().
340                 printBindings(cmd, KeySequence::ForGui);
341
342         if (!shortcuts.empty())
343                 comname += ": " + shortcuts;
344         else if (!argsadded && !cmd.argument().empty())
345                 comname += ' ' + cmd.argument();
346
347         if (!comname.empty()) {
348                 comname = rtrim(comname);
349                 dispatch_msg += '(' + rtrim(comname) + ')';
350         }
351         LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
352         return dispatch_msg;
353 }
354
355
356 void LyXFunc::dispatch(FuncRequest const & cmd)
357 {
358         string const argument = to_utf8(cmd.argument());
359         FuncCode const action = cmd.action;
360
361         LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
362         //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
363
364         // we have not done anything wrong yet.
365         errorstat = false;
366         dispatch_buffer.erase();
367
368         // redraw the screen at the end (first of the two drawing steps).
369         //This is done unless explicitely requested otherwise
370         Update::flags updateFlags = Update::FitCursor;
371
372         LyXView * lv = theApp()->currentWindow();
373
374         FuncStatus const flag = getStatus(cmd);
375         if (!flag.enabled()) {
376                 // We cannot use this function here
377                 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
378                        << lyxaction.getActionName(action)
379                        << " [" << action << "] is disabled at this location");
380                 setErrorMessage(flag.message());
381                 if (lv)
382                         lv->restartCursor();
383         } else {
384                 switch (action) {
385
386                 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
387                         lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
388                         break;
389
390                 case LFUN_REPEAT: {
391                         // repeat command
392                         string countstr;
393                         string rest = split(argument, countstr, ' ');
394                         istringstream is(countstr);
395                         int count = 0;
396                         is >> count;
397                         //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
398                         for (int i = 0; i < count; ++i)
399                                 dispatch(lyxaction.lookupFunc(rest));
400                         break;
401                 }
402
403                 case LFUN_COMMAND_SEQUENCE: {
404                         // argument contains ';'-terminated commands
405                         string arg = argument;
406                         // FIXME: this LFUN should also work without any view.
407                         Buffer * buffer = (lv && lv->documentBufferView())
408                                 ? &(lv->documentBufferView()->buffer()) : 0;
409                         buffer = &lv->currentBufferView()->buffer();
410                         if (buffer && !theBufferList().isLoaded(buffer))
411                                 buffer = 0;
412                         if (buffer)
413                                 buffer->undo().beginUndoGroup();
414                         while (!arg.empty()) {
415                                 string first;
416                                 arg = split(arg, first, ';');
417                                 FuncRequest func(lyxaction.lookupFunc(first));
418                                 func.origin = cmd.origin;
419                                 dispatch(func);
420                         }
421                         if (buffer)
422                                 buffer->undo().endUndoGroup();
423                         break;
424                 }
425
426                 case LFUN_COMMAND_ALTERNATIVES: {
427                         // argument contains ';'-terminated commands
428                         string arg = argument;
429                         while (!arg.empty()) {
430                                 string first;
431                                 arg = split(arg, first, ';');
432                                 FuncRequest func(lyxaction.lookupFunc(first));
433                                 func.origin = cmd.origin;
434                                 FuncStatus stat = getStatus(func);
435                                 if (stat.enabled()) {
436                                         dispatch(func);
437                                         break;
438                                 }
439                         }
440                         break;
441                 }
442
443                 case LFUN_CALL: {
444                         FuncRequest func;
445                         if (theTopLevelCmdDef().lock(argument, func)) {
446                                 func.origin = cmd.origin;
447                                 dispatch(func);
448                                 theTopLevelCmdDef().release(argument);
449                         } else {
450                                 if (func.action == LFUN_UNKNOWN_ACTION) {
451                                         // unknown command definition
452                                         lyxerr << "Warning: unknown command definition `"
453                                                    << argument << "'"
454                                                    << endl;
455                                 } else {
456                                         // recursion detected
457                                         lyxerr << "Warning: Recursion in the command definition `"
458                                                    << argument << "' detected"
459                                                    << endl;
460                                 }
461                         }
462                         break;
463                 }
464
465                 case LFUN_PREFERENCES_SAVE: {
466                         lyxrc.write(makeAbsPath("preferences",
467                                                 package().user_support().absFilename()),
468                                     false);
469                         break;
470                 }
471
472                 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
473                         string const fname =
474                                 addName(addPath(package().user_support().absFilename(), "templates/"),
475                                         "defaults.lyx");
476                         Buffer defaults(fname);
477
478                         istringstream ss(argument);
479                         Lexer lex;
480                         lex.setStream(ss);
481                         int const unknown_tokens = defaults.readHeader(lex);
482
483                         if (unknown_tokens != 0) {
484                                 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
485                                        << unknown_tokens << " unknown token"
486                                        << (unknown_tokens == 1 ? "" : "s")
487                                        << endl;
488                         }
489
490                         if (defaults.writeFile(FileName(defaults.absFileName())))
491                                 setMessage(bformat(_("Document defaults saved in %1$s"),
492                                                    makeDisplayPath(fname)));
493                         else
494                                 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
495                         break;
496                 }
497
498                 case LFUN_BOOKMARK_GOTO:
499                         // go to bookmark, open unopened file and switch to buffer if necessary
500                         gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
501                         updateFlags = Update::FitCursor;
502                         break;
503
504                 case LFUN_BOOKMARK_CLEAR:
505                         theSession().bookmarks().clear();
506                         break;
507
508                 default:
509                         DispatchResult dr;
510
511                         LASSERT(theApp(), /**/);
512                         // Let the frontend dispatch its own actions.
513                         theApp()->dispatch(cmd, dr);
514                         if (dr.dispatched())
515                                 // Nothing more to do.
516                                 break;
517
518                         // Everything below is only for active window
519                         if (lv == 0)
520                                 break;
521
522                         // Let the current LyXView dispatch its own actions.
523                         if (lv->dispatch(cmd)) {
524                                 BufferView * bv = lv->currentBufferView();
525                                 if (bv)
526                                         updateFlags = bv->cursor().result().update();
527                                 break;
528                         }
529
530                         BufferView * bv = lv->currentBufferView();
531                         LASSERT(bv, /**/);
532
533                         // Let the current BufferView dispatch its own actions.
534                         if (bv->dispatch(cmd)) {
535                                 // The BufferView took care of its own updates if needed.
536                                 updateFlags = Update::None;
537                                 break;
538                         }
539
540                         BufferView * doc_bv = lv->documentBufferView();
541                         // Try with the document BufferView dispatch if any.
542                         if (doc_bv && doc_bv->dispatch(cmd)) {
543                                 updateFlags = Update::None;
544                                 break;
545                         }
546
547                         // OK, so try the current Buffer itself...
548                         bv->buffer().dispatch(cmd, dr);
549                         if (dr.dispatched()) {
550                                 updateFlags = dr.update();
551                                 break;
552                         }
553                         // and with the document Buffer.
554                         if (doc_bv) {
555                                 doc_bv->buffer().dispatch(cmd, dr);
556                                 if (dr.dispatched()) {
557                                         updateFlags = dr.update();
558                                         break;
559                                 }
560                         }
561
562                         // Is this a function that acts on inset at point?
563                         Inset * inset = bv->cursor().nextInset();
564                         if (lyxaction.funcHasFlag(action, LyXAction::AtPoint)
565                             && inset) {
566                                 bv->cursor().result().dispatched(true);
567                                 bv->cursor().result().update(Update::FitCursor | Update::Force);
568                                 FuncRequest tmpcmd = cmd;
569                                 inset->dispatch(bv->cursor(), tmpcmd);
570                                 if (bv->cursor().result().dispatched()) {
571                                         updateFlags = bv->cursor().result().update();
572                                         break;
573                                 }
574                         }
575
576                         // Let the current Cursor dispatch its own actions.
577                         Cursor old = bv->cursor();
578                         bv->cursor().getPos(cursorPosBeforeDispatchX_,
579                                                 cursorPosBeforeDispatchY_);
580                         bv->cursor().dispatch(cmd);
581
582                         // notify insets we just left
583                         if (bv->cursor() != old) {
584                                 old.fixIfBroken();
585                                 bool badcursor = notifyCursorLeavesOrEnters(old, bv->cursor());
586                                 if (badcursor)
587                                         bv->cursor().fixIfBroken();
588                         }
589
590                         // update completion. We do it here and not in
591                         // processKeySym to avoid another redraw just for a
592                         // changed inline completion
593                         if (cmd.origin == FuncRequest::KEYBOARD) {
594                                 if (cmd.action == LFUN_SELF_INSERT
595                                     || (cmd.action == LFUN_ERT_INSERT && bv->cursor().inMathed()))
596                                         lv->updateCompletion(bv->cursor(), true, true);
597                                 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
598                                         lv->updateCompletion(bv->cursor(), false, true);
599                                 else
600                                         lv->updateCompletion(bv->cursor(), false, false);
601                         }
602
603                         updateFlags = bv->cursor().result().update();
604                 }
605
606                 // if we executed a mutating lfun, mark the buffer as dirty
607                 Buffer * doc_buffer = (lv && lv->documentBufferView())
608                         ? &(lv->documentBufferView()->buffer()) : 0;
609                 if (doc_buffer && theBufferList().isLoaded(doc_buffer)
610                         && flag.enabled()
611                     && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
612                     && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
613                         doc_buffer->markDirty();                        
614
615                 if (lv && lv->currentBufferView()) {
616                         // BufferView::update() updates the ViewMetricsInfo and
617                         // also initializes the position cache for all insets in
618                         // (at least partially) visible top-level paragraphs.
619                         // We will redraw the screen only if needed.
620                         lv->currentBufferView()->processUpdateFlags(updateFlags);
621
622                         // Do we have a selection?
623                         theSelection().haveSelection(
624                                 lv->currentBufferView()->cursor().selection());
625                         
626                         // update gui
627                         lv->restartCursor();
628                 }
629         }
630         if (lv) {
631                 // Some messages may already be translated, so we cannot use _()
632                 lv->message(sendDispatchMessage(
633                         translateIfPossible(getMessage()), cmd));
634         }
635 }
636
637
638 // Each LyXView should have it's own message method. lyxview and
639 // the minibuffer would use the minibuffer, but lyxserver would
640 // send an ERROR signal to its client.  Alejandro 970603
641 // This function is bit problematic when it comes to NLS, to make the
642 // lyx servers client be language indepenent we must not translate
643 // strings sent to this func.
644 void LyXFunc::setErrorMessage(docstring const & m) const
645 {
646         dispatch_buffer = m;
647         errorstat = true;
648 }
649
650
651 void LyXFunc::setMessage(docstring const & m) const
652 {
653         dispatch_buffer = m;
654 }
655
656
657 } // namespace lyx