]> git.lyx.org Git - lyx.git/blob - src/text3.C
Fix screen updates with arrow key navigation.
[lyx.git] / src / text3.C
1 /**
2  * \file text3.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "lyxtext.h"
19
20 #include "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
23 #include "buffer.h"
24 #include "buffer_funcs.h"
25 #include "bufferparams.h"
26 #include "BufferView.h"
27 #include "cursor.h"
28 #include "coordcache.h"
29 #include "CutAndPaste.h"
30 #include "debug.h"
31 #include "dispatchresult.h"
32 #include "errorlist.h"
33 #include "factory.h"
34 #include "funcrequest.h"
35 #include "gettext.h"
36 #include "intl.h"
37 #include "language.h"
38 #include "LyXAction.h"
39 #include "lyxfunc.h"
40 #include "lyxlex.h"
41 #include "lyxrc.h"
42 #include "lyxrow.h"
43 #include "paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "undo.h"
47 #include "vspace.h"
48 #include "pariterator.h"
49
50 #include "frontends/Clipboard.h"
51 #include "frontends/Selection.h"
52
53 #include "insets/insetcommand.h"
54 #include "insets/insetfloatlist.h"
55 #include "insets/insetnewline.h"
56 #include "insets/insetquotes.h"
57 #include "insets/insetspecialchar.h"
58 #include "insets/insettext.h"
59
60 #include "support/lstrings.h"
61 #include "support/lyxlib.h"
62 #include "support/convert.h"
63 #include "support/lyxtime.h"
64
65 #include "mathed/InsetMathHull.h"
66 #include "mathed/MathMacroTemplate.h"
67
68 #include <boost/current_function.hpp>
69
70 #include <clocale>
71 #include <sstream>
72
73
74 namespace lyx {
75
76 using cap::copySelection;
77 using cap::cutSelection;
78 using cap::pasteSelection;
79 using cap::replaceSelection;
80
81 using support::isStrUnsignedInt;
82 using support::token;
83
84 using std::endl;
85 using std::string;
86 using std::istringstream;
87 using std::ostringstream;
88
89
90 extern string current_layout;
91
92
93 namespace {
94
95         // globals...
96         LyXFont freefont(LyXFont::ALL_IGNORE);
97         bool toggleall = false;
98
99
100         void toggleAndShow(LCursor & cur, LyXText * text,
101                 LyXFont const & font, bool toggleall = true)
102         {
103                 text->toggleFree(cur, font, toggleall);
104
105                 if (font.language() != ignore_language ||
106                                 font.number() != LyXFont::IGNORE) {
107                         Paragraph & par = cur.paragraph();
108                         text->bidi.computeTables(par, cur.buffer(), cur.textRow());
109                         if (cur.boundary() !=
110                                         text->bidi.isBoundary(cur.buffer(), par,
111                                                         cur.pos(),
112                                                         text->real_current_font))
113                                 text->setCursor(cur, cur.pit(), cur.pos(),
114                                                 false, !cur.boundary());
115                 }
116         }
117
118
119         void moveCursor(LCursor & cur, bool selecting)
120         {
121                 if (selecting || cur.mark())
122                         cur.setSelection();
123                 if (!cur.selection())
124                         theSelection().haveSelection(false);
125                 cur.bv().switchKeyMap();
126         }
127
128
129         void finishChange(LCursor & cur, bool selecting)
130         {
131                 finishUndo();
132                 moveCursor(cur, selecting);
133         }
134
135
136         void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
137         {
138                 recordUndo(cur);
139                 docstring sel = cur.selectionAsString(false);
140                 //lyxerr << "selection is: '" << sel << "'" << endl;
141
142                 // It may happen that sel is empty but there is a selection
143                 replaceSelection(cur);
144
145                 if (sel.empty()) {
146                         const int old_pos = cur.pos();
147                         cur.insert(new InsetMathHull(hullSimple));
148                         BOOST_ASSERT(old_pos == cur.pos());
149                         cur.nextInset()->edit(cur, true);
150                         // don't do that also for LFUN_MATH_MODE
151                         // unless you want end up with always changing
152                         // to mathrm when opening an inlined inset --
153                         // I really hate "LyXfunc overloading"...
154                         if (display)
155                                 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
156                         // Avoid an unnecessary undo step if cmd.argument
157                         // is empty
158                         if (!cmd.argument().empty())
159                                 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
160                                                          cmd.argument()));
161                 } else {
162                         // create a macro if we see "\\newcommand"
163                         // somewhere, and an ordinary formula
164                         // otherwise
165                         if (sel.find(from_ascii("\\newcommand")) == string::npos
166                             && sel.find(from_ascii("\\def")) == string::npos)
167                         {
168                                 InsetMathHull * formula = new InsetMathHull;
169                                 istringstream is(to_utf8(sel));
170                                 LyXLex lex(0, 0);
171                                 lex.setStream(is);
172                                 formula->read(cur.buffer(), lex);
173                                 if (formula->getType() == hullNone)
174                                         // Don't create pseudo formulas if
175                                         // delimiters are left out
176                                         formula->mutate(hullSimple);
177                                 cur.insert(formula);
178                         } else {
179                                 cur.insert(new MathMacroTemplate(sel));
180                         }
181                 }
182                 cur.message(from_utf8(N_("Math editor mode")));
183         }
184
185 } // namespace anon
186
187
188
189 namespace bv_funcs {
190
191 string const freefont2string()
192 {
193         string data;
194         if (font2string(freefont, toggleall, data))
195                 return data;
196         return string();
197 }
198
199 }
200
201 void LyXText::cursorPrevious(LCursor & cur)
202 {
203         pos_type cpos = cur.pos();
204         pit_type cpar = cur.pit();
205
206         int x = cur.x_target();
207
208         // FIXME: there would maybe a need for this 'updated' boolean in the future...
209         bool updated = setCursorFromCoordinates(cur, x, 0);
210         updated |= cursorUp(cur);
211
212         if (cpar == cur.pit() && cpos == cur.pos()) {
213                 // we have a row which is taller than the workarea. The
214                 // simplest solution is to move to the previous row instead.
215                 updated |= cursorUp(cur);
216         }
217
218         finishUndo();
219         cur.updateFlags(Update::Force | Update::FitCursor);
220 }
221
222
223 void LyXText::cursorNext(LCursor & cur)
224 {
225         pos_type cpos = cur.pos();
226         pit_type cpar = cur.pit();
227
228         int x = cur.x_target();
229         // FIXME: there would maybe a need for this 'updated' boolean in the future...
230         bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
231         updated |= cursorDown(cur);
232
233         if (cpar == cur.pit() && cpos == cur.pos()) {
234                 // we have a row which is taller than the workarea. The
235                 // simplest solution is to move to the next row instead.
236                 updated |= cursorDown(cur);
237         }
238
239         finishUndo();
240         cur.updateFlags(Update::Force | Update::FitCursor);
241 }
242
243
244 namespace {
245
246 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
247 {
248         cap::replaceSelection(cur);
249         cur.insert(new InsetSpecialChar(kind));
250         cur.posRight();
251 }
252
253
254 bool doInsertInset(LCursor & cur, LyXText * text,
255         FuncRequest const & cmd, bool edit, bool pastesel)
256 {
257         InsetBase * inset = createInset(&cur.bv(), cmd);
258         if (!inset)
259                 return false;
260
261         recordUndo(cur);
262         bool gotsel = false;
263         if (cur.selection()) {
264                 lyx::dispatch(FuncRequest(LFUN_CUT));
265                 gotsel = true;
266         }
267         text->insertInset(cur, inset);
268
269         if (edit)
270                 inset->edit(cur, true);
271
272         if (gotsel && pastesel) {
273                 lyx::dispatch(FuncRequest(LFUN_PASTE));
274                 // reset first par to default
275                 if (cur.lastpit() != 0 || cur.lastpos() != 0) {
276                         LyXLayout_ptr const layout =
277                                 cur.buffer().params().getLyXTextClass().defaultLayout();
278                         cur.text()->paragraphs().begin()->layout(layout);
279                 }
280         }
281         return true;
282 }
283
284
285 } // anon namespace
286
287
288 void LyXText::number(LCursor & cur)
289 {
290         LyXFont font(LyXFont::ALL_IGNORE);
291         font.setNumber(LyXFont::TOGGLE);
292         toggleAndShow(cur, this, font);
293 }
294
295
296 bool LyXText::isRTL(Buffer const & buffer, Paragraph const & par) const
297 {
298         return par.isRightToLeftPar(buffer.params());
299 }
300
301
302 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
303 {
304         lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
305
306         // FIXME: We use the update flag to indicates wether a singlePar or a
307         // full screen update is needed. We reset it here but shall we restore it
308         // at the end?
309         cur.noUpdate();
310
311         BOOST_ASSERT(cur.text() == this);
312         BufferView * bv = &cur.bv();
313         CursorSlice oldTopSlice = cur.top();
314         bool oldBoundary = cur.boundary();
315         bool sel = cur.selection();
316         // Signals that, even if needsUpdate == false, an update of the
317         // cursor paragraph is required
318         bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
319                 LyXAction::SingleParUpdate);
320         // Signals that a full-screen update is required
321         bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
322                 LyXAction::NoUpdate) || singleParUpdate);
323         // Remember the old paragraph metric (_outer_ paragraph!)
324         Dimension olddim = cur.bottom().paragraph().dim();
325
326         switch (cmd.action) {
327
328         case LFUN_PARAGRAPH_MOVE_DOWN: {
329                 pit_type const pit = cur.pit();
330                 recUndo(cur, pit, pit + 1);
331                 finishUndo();
332                 std::swap(pars_[pit], pars_[pit + 1]);
333
334                 ParIterator begin(cur);
335                 ++cur.pit();
336                 ParIterator end = boost::next(ParIterator(cur));
337                 updateLabels(cur.buffer(), begin, end);
338
339                 needsUpdate = true;
340                 break;
341         }
342
343         case LFUN_PARAGRAPH_MOVE_UP: {
344                 pit_type const pit = cur.pit();
345                 recUndo(cur, pit - 1, pit);
346                 finishUndo();
347                 std::swap(pars_[pit], pars_[pit - 1]);
348
349                 ParIterator end = boost::next(ParIterator(cur));
350                 --cur.pit();
351                 ParIterator begin(cur);
352                 updateLabels(cur.buffer(), begin, end);
353
354                 needsUpdate = true;
355                 break;
356         }
357
358         case LFUN_APPENDIX: {
359                 Paragraph & par = cur.paragraph();
360                 bool start = !par.params().startOfAppendix();
361
362 #ifdef WITH_WARNINGS
363 #warning The code below only makes sense at top level.
364 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
365 #endif
366                 // ensure that we have only one start_of_appendix in this document
367                 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
368                         if (pars_[tmp].params().startOfAppendix()) {
369                                 recUndo(cur, tmp);
370                                 pars_[tmp].params().startOfAppendix(false);
371                                 break;
372                         }
373                 }
374
375                 recordUndo(cur);
376                 par.params().startOfAppendix(start);
377
378                 // we can set the refreshing parameters now
379                 updateLabels(cur.buffer());
380                 break;
381         }
382
383         case LFUN_WORD_DELETE_FORWARD:
384                 cur.clearSelection();
385                 deleteWordForward(cur);
386                 finishChange(cur, false);
387                 break;
388
389         case LFUN_WORD_DELETE_BACKWARD:
390                 cur.clearSelection();
391                 deleteWordBackward(cur);
392                 finishChange(cur, false);
393                 break;
394
395         case LFUN_LINE_DELETE:
396                 cur.clearSelection();
397                 deleteLineForward(cur);
398                 finishChange(cur, false);
399                 break;
400
401         case LFUN_BUFFER_BEGIN:
402         case LFUN_BUFFER_BEGIN_SELECT:
403                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
404                 if (cur.depth() == 1) {
405                         needsUpdate |= cursorTop(cur);
406                 } else {
407                         cur.undispatched();
408                 }
409                 break;
410
411         case LFUN_BUFFER_END:
412         case LFUN_BUFFER_END_SELECT:
413                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
414                 if (cur.depth() == 1) {
415                         needsUpdate |= cursorBottom(cur);
416                 } else {
417                         cur.undispatched();
418                 }
419                 break;
420
421         case LFUN_CHAR_FORWARD:
422         case LFUN_CHAR_FORWARD_SELECT:
423                 //lyxerr << BOOST_CURRENT_FUNCTION
424                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
425                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
426                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
427                         needsUpdate |= cursorLeft(cur);
428                 else
429                         needsUpdate |= cursorRight(cur);
430
431                 if (!needsUpdate && oldTopSlice == cur.top()
432                                 && cur.boundary() == oldBoundary) {
433                         cur.undispatched();
434                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
435                 }
436                 break;
437
438         case LFUN_CHAR_BACKWARD:
439         case LFUN_CHAR_BACKWARD_SELECT:
440                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
441                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
442                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
443                         needsUpdate |= cursorRight(cur);
444                 else
445                         needsUpdate |= cursorLeft(cur);
446
447                 if (!needsUpdate && oldTopSlice == cur.top()
448                         && cur.boundary() == oldBoundary) {
449                         cur.undispatched();
450                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
451                 }
452                 break;
453
454         case LFUN_UP:
455         case LFUN_UP_SELECT:
456                 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
457                 needsUpdate |= cur.selHandle(cmd.action == LFUN_UP_SELECT);
458
459                 needsUpdate |= cursorUp(cur);
460
461                 if (!needsUpdate && oldTopSlice == cur.top()
462                           && cur.boundary() == oldBoundary) {
463                         cur.undispatched();
464                         cmd = FuncRequest(LFUN_FINISHED_UP);
465                 }
466                 break;
467
468         case LFUN_DOWN:
469         case LFUN_DOWN_SELECT:
470                 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
471                 needsUpdate |= cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
472                 needsUpdate |= cursorDown(cur);
473
474                 if (!needsUpdate && oldTopSlice == cur.top() &&
475                     cur.boundary() == oldBoundary)
476                 {
477                         cur.undispatched();
478                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
479                 }
480                 break;
481
482         case LFUN_PARAGRAPH_UP:
483         case LFUN_PARAGRAPH_UP_SELECT:
484                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
485                 needsUpdate |= cursorUpParagraph(cur);
486                 break;
487
488         case LFUN_PARAGRAPH_DOWN:
489         case LFUN_PARAGRAPH_DOWN_SELECT:
490                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
491                 needsUpdate |= cursorDownParagraph(cur);
492                 break;
493
494         case LFUN_SCREEN_UP:
495         case LFUN_SCREEN_UP_SELECT:
496                 needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_UP_SELECT);
497                 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
498                         cur.undispatched();
499                         cmd = FuncRequest(LFUN_FINISHED_UP);
500                 } else {
501                         cursorPrevious(cur);
502                 }
503                 break;
504
505         case LFUN_SCREEN_DOWN:
506         case LFUN_SCREEN_DOWN_SELECT:
507                 needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_DOWN_SELECT);
508                 if (cur.pit() == cur.lastpit()
509                           && cur.textRow().endpos() == cur.lastpos()) {
510                         cur.undispatched();
511                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
512                 } else {
513                         cursorNext(cur);
514                 }
515                 break;
516
517         case LFUN_LINE_BEGIN:
518         case LFUN_LINE_BEGIN_SELECT:
519                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
520                 needsUpdate |= cursorHome(cur);
521                 break;
522
523         case LFUN_LINE_END:
524         case LFUN_LINE_END_SELECT:
525                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
526                 needsUpdate |= cursorEnd(cur);
527                 break;
528
529         case LFUN_WORD_FORWARD:
530         case LFUN_WORD_FORWARD_SELECT:
531                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
532                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
533                         needsUpdate |= cursorLeftOneWord(cur);
534                 else
535                         needsUpdate |= cursorRightOneWord(cur);
536                 break;
537
538         case LFUN_WORD_BACKWARD:
539         case LFUN_WORD_BACKWARD_SELECT:
540                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
541                 if (isRTL(*cur.bv().buffer(), cur.paragraph()))
542                         needsUpdate |= cursorRightOneWord(cur);
543                 else
544                         needsUpdate |= cursorLeftOneWord(cur);
545                 break;
546
547         case LFUN_WORD_SELECT: {
548                 selectWord(cur, WHOLE_WORD);
549                 finishChange(cur, true);
550                 break;
551         }
552
553         case LFUN_BREAK_LINE: {
554                 // Not allowed by LaTeX (labels or empty par)
555                 if (cur.pos() > cur.paragraph().beginOfBody()) {
556                         cap::replaceSelection(cur);
557                         cur.insert(new InsetNewline);
558                         cur.posRight();
559                         moveCursor(cur, false);
560                 }
561                 break;
562         }
563
564         case LFUN_CHAR_DELETE_FORWARD:
565                 if (!cur.selection()) {
566                         if (cur.pos() == cur.paragraph().size())
567                                 // Par boundary, force full-screen update
568                                 singleParUpdate = false;
569                         needsUpdate |= erase(cur);
570                         cur.resetAnchor();
571                         // It is possible to make it a lot faster still
572                         // just comment out the line below...
573                 } else {
574                         cutSelection(cur, true, false);
575                         singleParUpdate = false;
576                 }
577                 moveCursor(cur, false);
578                 break;
579
580         case LFUN_DELETE_FORWARD_SKIP:
581                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
582                 if (!cur.selection()) {
583                         if (cur.pos() == cur.lastpos()) {
584                                 cursorRight(cur);
585                                 cursorLeft(cur);
586                         }
587                         erase(cur);
588                         cur.resetAnchor();
589                 } else {
590                         cutSelection(cur, true, false);
591                 }
592                 break;
593
594
595         case LFUN_CHAR_DELETE_BACKWARD:
596                 if (!cur.selection()) {
597                         if (bv->getIntl().getTransManager().backspace()) {
598                                 // Par boundary, full-screen update
599                                 if (cur.pos() == 0)
600                                         singleParUpdate = false;
601                                 needsUpdate |= backspace(cur);
602                                 cur.resetAnchor();
603                                 // It is possible to make it a lot faster still
604                                 // just comment out the line below...
605                         }
606                 } else {
607                         cutSelection(cur, true, false);
608                         singleParUpdate = false;
609                 }
610                 bv->switchKeyMap();
611                 break;
612
613         case LFUN_DELETE_BACKWARD_SKIP:
614                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
615                 if (!cur.selection()) {
616 #ifdef WITH_WARNINGS
617 #warning look here
618 #endif
619                         //CursorSlice cur = cursor();
620                         backspace(cur);
621                         //anchor() = cur;
622                 } else {
623                         cutSelection(cur, true, false);
624                 }
625                 break;
626
627         case LFUN_BREAK_PARAGRAPH:
628                 cap::replaceSelection(cur);
629                 breakParagraph(cur, 0);
630                 cur.resetAnchor();
631                 bv->switchKeyMap();
632                 break;
633
634         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
635                 cap::replaceSelection(cur);
636                 breakParagraph(cur, 1);
637                 cur.resetAnchor();
638                 bv->switchKeyMap();
639                 break;
640
641         case LFUN_BREAK_PARAGRAPH_SKIP: {
642                 // When at the beginning of a paragraph, remove
643                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
644                 cap::replaceSelection(cur);
645                 if (cur.pos() == 0)
646                         cur.paragraph().params().labelWidthString(docstring());
647                 else
648                         breakParagraph(cur, 0);
649                 cur.resetAnchor();
650                 bv->switchKeyMap();
651                 break;
652         }
653
654         case LFUN_PARAGRAPH_SPACING: {
655                 Paragraph & par = cur.paragraph();
656                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
657                 string cur_value = "1.0";
658                 if (cur_spacing == Spacing::Other)
659                         cur_value = par.params().spacing().getValueAsString();
660
661                 istringstream is(to_utf8(cmd.argument()));
662                 string tmp;
663                 is >> tmp;
664                 Spacing::Space new_spacing = cur_spacing;
665                 string new_value = cur_value;
666                 if (tmp.empty()) {
667                         lyxerr << "Missing argument to `paragraph-spacing'"
668                                << endl;
669                 } else if (tmp == "single") {
670                         new_spacing = Spacing::Single;
671                 } else if (tmp == "onehalf") {
672                         new_spacing = Spacing::Onehalf;
673                 } else if (tmp == "double") {
674                         new_spacing = Spacing::Double;
675                 } else if (tmp == "other") {
676                         new_spacing = Spacing::Other;
677                         string tmpval = "0.0";
678                         is >> tmpval;
679                         lyxerr << "new_value = " << tmpval << endl;
680                         if (tmpval != "0.0")
681                                 new_value = tmpval;
682                 } else if (tmp == "default") {
683                         new_spacing = Spacing::Default;
684                 } else {
685                         lyxerr << to_utf8(_("Unknown spacing argument: "))
686                                << to_utf8(cmd.argument()) << endl;
687                 }
688                 if (cur_spacing != new_spacing || cur_value != new_value)
689                         par.params().spacing(Spacing(new_spacing, new_value));
690                 break;
691         }
692
693         case LFUN_INSET_INSERT: {
694                 recordUndo(cur);
695                 InsetBase * inset = createInset(bv, cmd);
696                 if (inset) {
697                         insertInset(cur, inset);
698                         cur.posRight();
699                 }
700                 break;
701         }
702
703         case LFUN_INSET_DISSOLVE:
704                 needsUpdate |= dissolveInset(cur);
705                 break;
706
707         case LFUN_INSET_SETTINGS:
708                 cur.inset().showInsetDialog(bv);
709                 break;
710
711         case LFUN_SPACE_INSERT:
712                 if (cur.paragraph().layout()->free_spacing)
713                         insertChar(cur, ' ');
714                 else {
715                         doInsertInset(cur, this, cmd, false, false);
716                         cur.posRight();
717                 }
718                 moveCursor(cur, false);
719                 break;
720
721         case LFUN_HYPHENATION_POINT_INSERT:
722                 specialChar(cur, InsetSpecialChar::HYPHENATION);
723                 break;
724
725         case LFUN_LIGATURE_BREAK_INSERT:
726                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
727                 break;
728
729         case LFUN_DOTS_INSERT:
730                 specialChar(cur, InsetSpecialChar::LDOTS);
731                 break;
732
733         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
734                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
735                 break;
736
737         case LFUN_MENU_SEPARATOR_INSERT:
738                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
739                 break;
740
741         case LFUN_WORD_UPCASE:
742                 changeCase(cur, LyXText::text_uppercase);
743                 break;
744
745         case LFUN_WORD_LOWCASE:
746                 changeCase(cur, LyXText::text_lowercase);
747                 break;
748
749         case LFUN_WORD_CAPITALIZE:
750                 changeCase(cur, LyXText::text_capitalization);
751                 break;
752
753         case LFUN_CHARS_TRANSPOSE:
754                 charsTranspose(cur);
755                 break;
756
757         case LFUN_PASTE:
758                 cur.message(_("Paste"));
759                 cap::replaceSelection(cur);
760                 if (isStrUnsignedInt(to_utf8(cmd.argument())))
761                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
762                         convert<unsigned int>(to_utf8(cmd.argument())));
763                 else
764                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
765                         0);
766                 bv->buffer()->errors("Paste");
767                 cur.clearSelection(); // bug 393
768                 bv->switchKeyMap();
769                 finishUndo();
770                 break;
771
772         case LFUN_CUT:
773                 cutSelection(cur, true, true);
774                 cur.message(_("Cut"));
775                 break;
776
777         case LFUN_COPY:
778                 copySelection(cur);
779                 cur.message(_("Copy"));
780                 break;
781
782         case LFUN_SERVER_GET_XY:
783                 cur.message(from_utf8(
784                         convert<string>(cursorX(cur.buffer(), cur.top(), cur.boundary()))
785                         + ' ' + convert<string>(cursorY(cur.top(), cur.boundary()))));
786                 break;
787
788         case LFUN_SERVER_SET_XY: {
789                 int x = 0;
790                 int y = 0;
791                 istringstream is(to_utf8(cmd.argument()));
792                 is >> x >> y;
793                 if (!is)
794                         lyxerr << "SETXY: Could not parse coordinates in '"
795                                << to_utf8(cmd.argument()) << std::endl;
796                 else
797                         setCursorFromCoordinates(cur, x, y);
798                 break;
799         }
800
801         case LFUN_SERVER_GET_FONT:
802                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
803                         cur.message(from_ascii("E"));
804                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
805                         cur.message(from_ascii("N"));
806                 else
807                         cur.message(from_ascii("0"));
808                 break;
809
810         case LFUN_SERVER_GET_LAYOUT:
811                 cur.message(from_utf8(cur.paragraph().layout()->name()));
812                 break;
813
814         case LFUN_LAYOUT: {
815                 string layout = to_ascii(cmd.argument());
816                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) " << layout << endl;
817
818                 // Derive layout number from given argument (string)
819                 // and current buffer's textclass (number)
820                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
821                 if (layout.empty())
822                         layout = tclass.defaultLayoutName();
823                 bool hasLayout = tclass.hasLayout(layout);
824
825                 // If the entry is obsolete, use the new one instead.
826                 if (hasLayout) {
827                         string const & obs = tclass[layout]->obsoleted_by();
828                         if (!obs.empty())
829                                 layout = obs;
830                 }
831
832                 if (!hasLayout) {
833                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
834                                 from_utf8(N_(" not known")));
835                         break;
836                 }
837
838                 bool change_layout = (current_layout != layout);
839
840                 if (!change_layout && cur.selection() &&
841                         cur.selBegin().pit() != cur.selEnd().pit())
842                 {
843                         pit_type spit = cur.selBegin().pit();
844                         pit_type epit = cur.selEnd().pit() + 1;
845                         while (spit != epit) {
846                                 if (pars_[spit].layout()->name() != current_layout) {
847                                         change_layout = true;
848                                         break;
849                                 }
850                                 ++spit;
851                         }
852                 }
853
854                 if (change_layout) {
855                         current_layout = layout;
856                         setLayout(cur, layout);
857                         // inform the GUI that the layout has changed.
858                         bv->layoutChanged(layout);
859                         bv->switchKeyMap();
860                 }
861                 break;
862         }
863
864         case LFUN_CLIPBOARD_PASTE: {
865                 cur.clearSelection();
866                 docstring const clip = theClipboard().get();
867                 if (!clip.empty()) {
868                         recordUndo(cur);
869                         if (cmd.argument() == "paragraph")
870                                 insertStringAsParagraphs(cur, clip);
871                         else
872                                 insertStringAsLines(cur, clip);
873                 }
874                 break;
875         }
876
877         case LFUN_PRIMARY_SELECTION_PASTE: {
878                 cur.clearSelection();
879                 docstring const clip = theSelection().get();
880                 if (!clip.empty()) {
881                         recordUndo(cur);
882                         if (cmd.argument() == "paragraph")
883                                 insertStringAsParagraphs(cur, clip);
884                         else
885                                 insertStringAsLines(cur, clip);
886                 }
887                 break;
888         }
889
890         case LFUN_UNICODE_INSERT: {
891                 if (cmd.argument().empty())
892                         break;
893                 docstring hexstring = cmd.argument();
894                 if (lyx::support::isHex(hexstring)) {
895                         char_type c = lyx::support::hexToInt(hexstring);
896                         if (c > 32 && c < 0x10ffff) {
897                                 lyxerr << "Inserting c: " << c << endl;
898                                 docstring s = docstring(1, c);
899                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
900                         }
901                 }
902                 break;
903         }
904                 
905         case LFUN_QUOTE_INSERT: {
906                 cap::replaceSelection(cur);
907                 Paragraph & par = cur.paragraph();
908                 pos_type pos = cur.pos();
909                 char_type c;
910                 if (pos == 0)
911                         c = ' ';
912                 else if (cur.prevInset() && cur.prevInset()->isSpace())
913                         c = ' ';
914                 else
915                         c = par.getChar(pos - 1);
916
917                 LyXLayout_ptr const & style = par.layout();
918
919                 BufferParams const & bufparams = bv->buffer()->params();
920                 if (!style->pass_thru
921                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
922                         string arg = to_utf8(cmd.argument());
923                         if (arg == "single")
924                                 cur.insert(new InsetQuotes(c,
925                                     bufparams.quotes_language,
926                                     InsetQuotes::SingleQ));
927                         else
928                                 cur.insert(new InsetQuotes(c,
929                                     bufparams.quotes_language,
930                                     InsetQuotes::DoubleQ));
931                         cur.posRight();
932                 }
933                 else
934                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
935                 break;
936         }
937
938         case LFUN_DATE_INSERT:
939                 if (cmd.argument().empty())
940                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
941                                 formatted_time(current_time())));
942                 else
943                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
944                                 formatted_time(current_time(), to_utf8(cmd.argument()))));
945                 break;
946
947         case LFUN_MOUSE_TRIPLE:
948                 if (cmd.button() == mouse_button::button1) {
949                         cursorHome(cur);
950                         cur.resetAnchor();
951                         cursorEnd(cur);
952                         cur.setSelection();
953                         bv->cursor() = cur;
954                         theSelection().haveSelection(cur.selection());
955                 }
956                 break;
957
958         case LFUN_MOUSE_DOUBLE:
959                 if (cmd.button() == mouse_button::button1) {
960                         selectWord(cur, WHOLE_WORD_STRICT);
961                         bv->cursor() = cur;
962                         theSelection().haveSelection(cur.selection());
963                 }
964                 break;
965
966         // Single-click on work area
967         case LFUN_MOUSE_PRESS: {
968                 // Right click on a footnote flag opens float menu
969                 if (cmd.button() == mouse_button::button3)
970                         cur.clearSelection();
971
972                 // Middle button press pastes if we have a selection
973                 // We do this here as if the selection was inside an inset
974                 // it could get cleared on the unlocking of the inset so
975                 // we have to check this first
976                 bool paste_internally = false;
977                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
978                         lyx::dispatch(FuncRequest(LFUN_COPY));
979                         paste_internally = true;
980                 }
981
982                 bv->mouseSetCursor(cur);
983
984                 // Insert primary selection with middle mouse
985                 // if there is a local selection in the current buffer,
986                 // insert this
987                 if (cmd.button() == mouse_button::button2) {
988                         if (paste_internally)
989                                 lyx::dispatch(FuncRequest(LFUN_PASTE));
990                         else
991                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
992                 }
993
994                 if (cmd.button() == mouse_button::button1) {
995                         needsUpdate = false;
996                         cur.noUpdate();
997                 }
998
999                 break;
1000         }
1001
1002         case LFUN_MOUSE_MOTION: {
1003                 // Only use motion with button 1
1004                 //if (cmd.button() != mouse_button::button1)
1005                 //      return false;
1006
1007                 // ignore motions deeper nested than the real anchor
1008                 LCursor & bvcur = cur.bv().cursor();
1009                 if (bvcur.anchor_.hasPart(cur)) {
1010                         CursorSlice old = bvcur.top();
1011
1012                         int const wh = bv->workHeight();
1013                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1014
1015                         setCursorFromCoordinates(cur, cmd.x, y);
1016                         cur.x_target() = cmd.x;
1017                         if (cmd.y >= wh)
1018                                 cursorDown(cur);
1019                         else if (cmd.y < 0)
1020                                 cursorUp(cur);
1021                         // This is to allow jumping over large insets
1022                         if (cur.top() == old) {
1023                                 if (cmd.y >= wh)
1024                                         cursorDown(cur);
1025                                 else if (cmd.y < 0)
1026                                         cursorUp(cur);
1027                         }
1028
1029                         if (cur.top() == old)
1030                                 cur.noUpdate();
1031                         else {
1032                                 // don't set anchor_
1033                                 bvcur.setCursor(cur);
1034                                 bvcur.selection() = true;
1035                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1036                         }
1037
1038                 } else
1039                         cur.undispatched();
1040                 break;
1041         }
1042
1043         case LFUN_MOUSE_RELEASE: {
1044                 if (cmd.button() == mouse_button::button2)
1045                         break;
1046
1047                 // finish selection
1048                 if (cmd.button() == mouse_button::button1) {
1049                         if (cur.selection())
1050                                 theSelection().haveSelection(cur.selection());
1051                         needsUpdate = false;
1052                         cur.noUpdate();
1053                 }
1054
1055                 bv->switchKeyMap();
1056                 break;
1057         }
1058
1059         case LFUN_SELF_INSERT: {
1060                 if (cmd.argument().empty())
1061                         break;
1062
1063                 // Automatically delete the currently selected
1064                 // text and replace it with what is being
1065                 // typed in now. Depends on lyxrc settings
1066                 // "auto_region_delete", which defaults to
1067                 // true (on).
1068
1069                 if (lyxrc.auto_region_delete) {
1070                         if (cur.selection())
1071                                 cutSelection(cur, false, false);
1072                         theSelection().haveSelection(false);
1073                 }
1074
1075                 cur.clearSelection();
1076                 LyXFont const old_font = real_current_font;
1077
1078                 docstring::const_iterator cit = cmd.argument().begin();
1079                 docstring::const_iterator end = cmd.argument().end();
1080                 for (; cit != end; ++cit)
1081 #if 0
1082                         bv->getIntl().getTransManager().
1083                                 translateAndInsert(*cit, this);
1084 #else
1085                         insertChar(bv->cursor(), *cit);
1086 #endif
1087
1088                 cur.resetAnchor();
1089                 moveCursor(cur, false);
1090                 break;
1091         }
1092
1093         case LFUN_URL_INSERT: {
1094                 InsetCommandParams p("url");
1095                 string const data = InsetCommandMailer::params2string("url", p);
1096                 bv->showInsetDialog("url", data, 0);
1097                 break;
1098         }
1099
1100         case LFUN_HTML_INSERT: {
1101                 InsetCommandParams p("htmlurl");
1102                 string const data = InsetCommandMailer::params2string("url", p);
1103                 bv->showInsetDialog("url", data, 0);
1104                 break;
1105         }
1106
1107         case LFUN_LABEL_INSERT: {
1108                 InsetCommandParams p("label");
1109                 // Try to generate a valid label
1110                 p["name"] = (cmd.argument().empty()) ?
1111                         cur.getPossibleLabel() :
1112                         cmd.argument();
1113                 string const data = InsetCommandMailer::params2string("label", p);
1114
1115                 if (cmd.argument().empty()) {
1116                         bv->showInsetDialog("label", data, 0);
1117                 } else {
1118                         FuncRequest fr(LFUN_INSET_INSERT, data);
1119                         dispatch(cur, fr);
1120                 }
1121                 break;
1122         }
1123
1124
1125 #if 0
1126         case LFUN_LIST_INSERT:
1127         case LFUN_THEOREM_INSERT:
1128         case LFUN_CAPTION_INSERT:
1129 #endif
1130         case LFUN_NOTE_INSERT:
1131         case LFUN_CHARSTYLE_INSERT:
1132         case LFUN_BOX_INSERT:
1133         case LFUN_BRANCH_INSERT:
1134         case LFUN_BIBITEM_INSERT:
1135         case LFUN_ERT_INSERT:
1136         case LFUN_FOOTNOTE_INSERT:
1137         case LFUN_MARGINALNOTE_INSERT:
1138         case LFUN_OPTIONAL_INSERT:
1139         case LFUN_ENVIRONMENT_INSERT:
1140                 // Open the inset, and move the current selection
1141                 // inside it.
1142                 doInsertInset(cur, this, cmd, true, true);
1143                 cur.posRight();
1144                 break;
1145
1146         case LFUN_TABULAR_INSERT:
1147                 // if there were no arguments, just open the dialog
1148                 if (doInsertInset(cur, this, cmd, false, true))
1149                         cur.posRight();
1150                 else
1151                         bv->showDialog("tabularcreate");
1152
1153                 break;
1154
1155         case LFUN_FLOAT_INSERT:
1156         case LFUN_FLOAT_WIDE_INSERT:
1157         case LFUN_WRAP_INSERT:
1158                 doInsertInset(cur, this, cmd, true, true);
1159                 cur.posRight();
1160                 // FIXME: the "Caption" name should not be hardcoded,
1161                 // but given by the float definition.
1162                 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1163                 break;
1164
1165         case LFUN_INDEX_INSERT:
1166         case LFUN_NOMENCL_INSERT: {
1167                 InsetBase * inset = createInset(&cur.bv(), cmd);
1168                 if (!inset)
1169                         break;
1170
1171                 recordUndo(cur);
1172                 cur.clearSelection();
1173                 insertInset(cur, inset);
1174                 inset->edit(cur, true);
1175                 // Show the dialog for the nomenclature entry, since the
1176                 // description entry still needs to be filled in.
1177                 if (cmd.action == LFUN_NOMENCL_INSERT)
1178                         InsetCommandMailer("nomenclature",
1179                                 *reinterpret_cast<InsetCommand *>(inset)).showDialog(&cur.bv());
1180                 cur.posRight();
1181                 break;
1182         }
1183
1184         case LFUN_INDEX_PRINT:
1185         case LFUN_NOMENCL_PRINT:
1186         case LFUN_TOC_INSERT:
1187         case LFUN_HFILL_INSERT:
1188         case LFUN_LINE_INSERT:
1189         case LFUN_PAGEBREAK_INSERT:
1190         case LFUN_CLEARPAGE_INSERT:
1191         case LFUN_CLEARDOUBLEPAGE_INSERT:
1192                 // do nothing fancy
1193                 doInsertInset(cur, this, cmd, false, false);
1194                 cur.posRight();
1195                 break;
1196
1197         case LFUN_DEPTH_DECREMENT:
1198                 changeDepth(cur, DEC_DEPTH);
1199                 break;
1200
1201         case LFUN_DEPTH_INCREMENT:
1202                 changeDepth(cur, INC_DEPTH);
1203                 break;
1204
1205         case LFUN_MATH_DISPLAY:
1206                 mathDispatch(cur, cmd, true);
1207                 break;
1208
1209         case LFUN_MATH_IMPORT_SELECTION:
1210         case LFUN_MATH_MODE:
1211                 if (cmd.argument() == "on")
1212                         // don't pass "on" as argument
1213                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1214                 else
1215                         mathDispatch(cur, cmd, false);
1216                 break;
1217
1218         case LFUN_MATH_MACRO:
1219                 if (cmd.argument().empty())
1220                         cur.errorMessage(from_utf8(N_("Missing argument")));
1221                 else {
1222                         string s = to_utf8(cmd.argument());
1223                         string const s1 = token(s, ' ', 1);
1224                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1225                         string const s2 = token(s, ' ', 2);
1226                         string const type = s2.empty() ? "newcommand" : s2;
1227                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1228                         //cur.nextInset()->edit(cur, true);
1229                 }
1230                 break;
1231
1232         // passthrough hat and underscore outside mathed:
1233         case LFUN_MATH_SUBSCRIPT:
1234                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1235                 break;
1236         case LFUN_MATH_SUPERSCRIPT:
1237                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1238                 break;
1239
1240         case LFUN_MATH_INSERT:
1241         case LFUN_MATH_MATRIX:
1242         case LFUN_MATH_DELIM:
1243         case LFUN_MATH_BIGDELIM: {
1244                 cur.insert(new InsetMathHull(hullSimple));
1245                 cur.dispatch(FuncRequest(LFUN_CHAR_FORWARD));
1246                 cur.dispatch(cmd);
1247                 break;
1248         }
1249
1250         case LFUN_FONT_EMPH: {
1251                 LyXFont font(LyXFont::ALL_IGNORE);
1252                 font.setEmph(LyXFont::TOGGLE);
1253                 toggleAndShow(cur, this, font);
1254                 break;
1255         }
1256
1257         case LFUN_FONT_BOLD: {
1258                 LyXFont font(LyXFont::ALL_IGNORE);
1259                 font.setSeries(LyXFont::BOLD_SERIES);
1260                 toggleAndShow(cur, this, font);
1261                 break;
1262         }
1263
1264         case LFUN_FONT_NOUN: {
1265                 LyXFont font(LyXFont::ALL_IGNORE);
1266                 font.setNoun(LyXFont::TOGGLE);
1267                 toggleAndShow(cur, this, font);
1268                 break;
1269         }
1270
1271         case LFUN_FONT_CODE: {
1272                 LyXFont font(LyXFont::ALL_IGNORE);
1273                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1274                 toggleAndShow(cur, this, font);
1275                 break;
1276         }
1277
1278         case LFUN_FONT_SANS: {
1279                 LyXFont font(LyXFont::ALL_IGNORE);
1280                 font.setFamily(LyXFont::SANS_FAMILY);
1281                 toggleAndShow(cur, this, font);
1282                 break;
1283         }
1284
1285         case LFUN_FONT_ROMAN: {
1286                 LyXFont font(LyXFont::ALL_IGNORE);
1287                 font.setFamily(LyXFont::ROMAN_FAMILY);
1288                 toggleAndShow(cur, this, font);
1289                 break;
1290         }
1291
1292         case LFUN_FONT_DEFAULT: {
1293                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1294                 toggleAndShow(cur, this, font);
1295                 break;
1296         }
1297
1298         case LFUN_FONT_UNDERLINE: {
1299                 LyXFont font(LyXFont::ALL_IGNORE);
1300                 font.setUnderbar(LyXFont::TOGGLE);
1301                 toggleAndShow(cur, this, font);
1302                 break;
1303         }
1304
1305         case LFUN_FONT_SIZE: {
1306                 LyXFont font(LyXFont::ALL_IGNORE);
1307                 font.setLyXSize(to_utf8(cmd.argument()));
1308                 toggleAndShow(cur, this, font);
1309                 break;
1310         }
1311
1312         case LFUN_LANGUAGE: {
1313                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1314                 if (!lang)
1315                         break;
1316                 LyXFont font(LyXFont::ALL_IGNORE);
1317                 font.setLanguage(lang);
1318                 toggleAndShow(cur, this, font);
1319                 bv->switchKeyMap();
1320                 break;
1321         }
1322
1323         case LFUN_FONT_FREE_APPLY:
1324                 toggleAndShow(cur, this, freefont, toggleall);
1325                 cur.message(_("Character set"));
1326                 break;
1327
1328         // Set the freefont using the contents of \param data dispatched from
1329         // the frontends and apply it at the current cursor location.
1330         case LFUN_FONT_FREE_UPDATE: {
1331                 LyXFont font;
1332                 bool toggle;
1333                 if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) {
1334                         freefont = font;
1335                         toggleall = toggle;
1336                         toggleAndShow(cur, this, freefont, toggleall);
1337                         cur.message(_("Character set"));
1338                 }
1339                 break;
1340         }
1341
1342         case LFUN_FINISHED_LEFT:
1343                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1344                 break;
1345
1346         case LFUN_FINISHED_RIGHT:
1347                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1348                 ++cur.pos();
1349                 break;
1350
1351         case LFUN_FINISHED_UP:
1352                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1353                 cursorUp(cur);
1354                 break;
1355
1356         case LFUN_FINISHED_DOWN:
1357                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1358                 cursorDown(cur);
1359                 break;
1360
1361         case LFUN_LAYOUT_PARAGRAPH: {
1362                 string data;
1363                 params2string(cur.paragraph(), data);
1364                 data = "show\n" + data;
1365                 bv->showDialogWithData("paragraph", data);
1366                 break;
1367         }
1368
1369         case LFUN_PARAGRAPH_UPDATE: {
1370                 string data;
1371                 params2string(cur.paragraph(), data);
1372
1373                 // Will the paragraph accept changes from the dialog?
1374                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1375
1376                 data = "update " + convert<string>(accept) + '\n' + data;
1377                 bv->updateDialog("paragraph", data);
1378                 break;
1379         }
1380
1381         case LFUN_ACCENT_UMLAUT:
1382         case LFUN_ACCENT_CIRCUMFLEX:
1383         case LFUN_ACCENT_GRAVE:
1384         case LFUN_ACCENT_ACUTE:
1385         case LFUN_ACCENT_TILDE:
1386         case LFUN_ACCENT_CEDILLA:
1387         case LFUN_ACCENT_MACRON:
1388         case LFUN_ACCENT_DOT:
1389         case LFUN_ACCENT_UNDERDOT:
1390         case LFUN_ACCENT_UNDERBAR:
1391         case LFUN_ACCENT_CARON:
1392         case LFUN_ACCENT_SPECIAL_CARON:
1393         case LFUN_ACCENT_BREVE:
1394         case LFUN_ACCENT_TIE:
1395         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1396         case LFUN_ACCENT_CIRCLE:
1397         case LFUN_ACCENT_OGONEK:
1398                 theLyXFunc().handleKeyFunc(cmd.action);
1399                 if (!cmd.argument().empty())
1400                         // FIXME: Are all these characters encoded in one byte in utf8?
1401                         bv->getIntl().getTransManager()
1402                                 .translateAndInsert(cmd.argument()[0], this, cur);
1403                 break;
1404
1405         case LFUN_FLOAT_LIST: {
1406                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1407                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1408                         // not quite sure if we want this...
1409                         recordUndo(cur);
1410                         cur.clearSelection();
1411                         breakParagraph(cur);
1412
1413                         if (cur.lastpos() != 0) {
1414                                 cursorLeft(cur);
1415                                 breakParagraph(cur);
1416                         }
1417
1418                         setLayout(cur, tclass.defaultLayoutName());
1419                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, docstring(), 0);
1420                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1421                         cur.posRight();
1422                 } else {
1423                         lyxerr << "Non-existent float type: "
1424                                << to_utf8(cmd.argument()) << endl;
1425                 }
1426                 break;
1427         }
1428
1429         case LFUN_CHANGE_ACCEPT: {
1430                 acceptChange(cur);
1431                 break;
1432         }
1433
1434         case LFUN_CHANGE_REJECT: {
1435                 rejectChange(cur);
1436                 break;
1437         }
1438
1439         case LFUN_THESAURUS_ENTRY: {
1440                 docstring arg = cmd.argument();
1441                 if (arg.empty()) {
1442                         arg = cur.selectionAsString(false);
1443                         // FIXME
1444                         if (arg.size() > 100 || arg.empty()) {
1445                                 // Get word or selection
1446                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1447                                 arg = cur.selectionAsString(false);
1448                         }
1449                 }
1450                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1451                 break;
1452         }
1453
1454         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1455                 // Given data, an encoding of the ParagraphParameters
1456                 // generated in the Paragraph dialog, this function sets
1457                 // the current paragraph appropriately.
1458                 istringstream is(to_utf8(cmd.argument()));
1459                 LyXLex lex(0, 0);
1460                 lex.setStream(is);
1461                 ParagraphParameters params;
1462                 params.read(lex);
1463                 setParagraph(cur,
1464                              params.spacing(),
1465                              params.align(),
1466                              params.labelWidthString(),
1467                              params.noindent());
1468                 cur.message(_("Paragraph layout set"));
1469                 break;
1470         }
1471
1472         case LFUN_ESCAPE:
1473                 if (cur.selection()) {
1474                         cur.selection() = false;
1475                 } else {
1476                         cur.undispatched();
1477                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1478                 }
1479                 break;
1480
1481         default:
1482                 lyxerr[Debug::ACTION]
1483                         << BOOST_CURRENT_FUNCTION
1484                         << ": Command " << cmd
1485                         << " not DISPATCHED by LyXText" << endl;
1486                 cur.undispatched();
1487                 break;
1488         }
1489
1490         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1491
1492         // FIXME: The cursor flag is reset two lines below
1493         // so we need to check here if some of the LFUN did touch that.
1494         // for now only LyXText::erase() and LyXText::backspace() do that.
1495         // The plan is to verify all the LFUNs and then to remove this
1496         // singleParUpdate boolean altogether.
1497         if (cur.result().update() & Update::Force) {
1498                 singleParUpdate = false;
1499                 needsUpdate = true;
1500         }
1501
1502         // FIXME: the following code should go in favor of fine grained
1503         // update flag treatment.
1504         if (singleParUpdate)
1505                 // Inserting characters does not change par height
1506                 if (cur.bottom().paragraph().dim().height()
1507                     == olddim.height()) {
1508                         // if so, update _only_ this paragraph
1509                         cur.updateFlags(Update::SinglePar |
1510                                 Update::FitCursor |
1511                                 Update::MultiParSel);
1512                         return;
1513                 } else
1514                         needsUpdate = true;
1515
1516         if (!needsUpdate
1517             && &oldTopSlice.inset() == &cur.inset()
1518             && oldTopSlice.idx() == cur.idx()
1519             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1520             && !cur.selection())
1521                 // FIXME: it would be better if we could just do this
1522                 //
1523                 //if (cur.result().update() != Update::FitCursor)
1524                 //      cur.noUpdate();
1525                 // 
1526                 // But some LFUNs do not set Update::FitCursor when needed, so we
1527                 // do it for all. This is not very harmfull as FitCursor will provoke
1528                 // a full redraw only if needed but still, a proper review of all LFUN
1529                 // should be done and this needsUpdate boolean can then be removed.
1530                 cur.updateFlags(Update::FitCursor);
1531         else
1532                 cur.updateFlags(Update::Force | Update::FitCursor);
1533 }
1534
1535
1536 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1537                         FuncStatus & flag) const
1538 {
1539         BOOST_ASSERT(cur.text() == this);
1540
1541         LyXFont const & font = real_current_font;
1542         bool enable = true;
1543         InsetBase::Code code = InsetBase::NO_CODE;
1544
1545         switch (cmd.action) {
1546
1547         case LFUN_DEPTH_DECREMENT:
1548                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1549                 break;
1550
1551         case LFUN_DEPTH_INCREMENT:
1552                 enable = changeDepthAllowed(cur, INC_DEPTH);
1553                 break;
1554
1555         case LFUN_APPENDIX:
1556                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1557                 return true;
1558
1559         case LFUN_BIBITEM_INSERT:
1560                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1561                 break;
1562
1563         case LFUN_DIALOG_SHOW_NEW_INSET:
1564                 if (cmd.argument() == "bibitem")
1565                         code = InsetBase::BIBITEM_CODE;
1566                 else if (cmd.argument() == "bibtex")
1567                         code = InsetBase::BIBTEX_CODE;
1568                 else if (cmd.argument() == "box")
1569                         code = InsetBase::BOX_CODE;
1570                 else if (cmd.argument() == "branch")
1571                         code = InsetBase::BRANCH_CODE;
1572                 else if (cmd.argument() == "citation")
1573                         code = InsetBase::CITE_CODE;
1574                 else if (cmd.argument() == "ert")
1575                         code = InsetBase::ERT_CODE;
1576                 else if (cmd.argument() == "external")
1577                         code = InsetBase::EXTERNAL_CODE;
1578                 else if (cmd.argument() == "float")
1579                         code = InsetBase::FLOAT_CODE;
1580                 else if (cmd.argument() == "graphics")
1581                         code = InsetBase::GRAPHICS_CODE;
1582                 else if (cmd.argument() == "include")
1583                         code = InsetBase::INCLUDE_CODE;
1584                 else if (cmd.argument() == "index")
1585                         code = InsetBase::INDEX_CODE;
1586                 else if (cmd.argument() == "nomenclature")
1587                         code = InsetBase::NOMENCL_CODE;
1588                 else if (cmd.argument() == "label")
1589                         code = InsetBase::LABEL_CODE;
1590                 else if (cmd.argument() == "note")
1591                         code = InsetBase::NOTE_CODE;
1592                 else if (cmd.argument() == "ref")
1593                         code = InsetBase::REF_CODE;
1594                 else if (cmd.argument() == "toc")
1595                         code = InsetBase::TOC_CODE;
1596                 else if (cmd.argument() == "url")
1597                         code = InsetBase::URL_CODE;
1598                 else if (cmd.argument() == "vspace")
1599                         code = InsetBase::VSPACE_CODE;
1600                 else if (cmd.argument() == "wrap")
1601                         code = InsetBase::WRAP_CODE;
1602                 break;
1603
1604         case LFUN_ERT_INSERT:
1605                 code = InsetBase::ERT_CODE;
1606                 break;
1607         case LFUN_FOOTNOTE_INSERT:
1608                 code = InsetBase::FOOT_CODE;
1609                 break;
1610         case LFUN_TABULAR_INSERT:
1611                 code = InsetBase::TABULAR_CODE;
1612                 break;
1613         case LFUN_MARGINALNOTE_INSERT:
1614                 code = InsetBase::MARGIN_CODE;
1615                 break;
1616         case LFUN_FLOAT_INSERT:
1617         case LFUN_FLOAT_WIDE_INSERT:
1618                 code = InsetBase::FLOAT_CODE;
1619                 break;
1620         case LFUN_WRAP_INSERT:
1621                 code = InsetBase::WRAP_CODE;
1622                 break;
1623         case LFUN_FLOAT_LIST:
1624                 code = InsetBase::FLOAT_LIST_CODE;
1625                 break;
1626 #if 0
1627         case LFUN_LIST_INSERT:
1628                 code = InsetBase::LIST_CODE;
1629                 break;
1630         case LFUN_THEOREM_INSERT:
1631                 code = InsetBase::THEOREM_CODE;
1632                 break;
1633 #endif
1634         case LFUN_CAPTION_INSERT:
1635                 code = InsetBase::CAPTION_CODE;
1636                 break;
1637         case LFUN_NOTE_INSERT:
1638                 code = InsetBase::NOTE_CODE;
1639                 break;
1640         case LFUN_CHARSTYLE_INSERT:
1641                 code = InsetBase::CHARSTYLE_CODE;
1642                 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1643                         enable = false;
1644                 break;
1645         case LFUN_BOX_INSERT:
1646                 code = InsetBase::BOX_CODE;
1647                 break;
1648         case LFUN_BRANCH_INSERT:
1649                 code = InsetBase::BRANCH_CODE;
1650                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1651                         enable = false;
1652                 break;
1653         case LFUN_LABEL_INSERT:
1654                 code = InsetBase::LABEL_CODE;
1655                 break;
1656         case LFUN_OPTIONAL_INSERT:
1657                 code = InsetBase::OPTARG_CODE;
1658                 enable = numberOfOptArgs(cur.paragraph())
1659                         < cur.paragraph().layout()->optionalargs;
1660                 break;
1661         case LFUN_ENVIRONMENT_INSERT:
1662                 code = InsetBase::BOX_CODE;
1663                 break;
1664         case LFUN_INDEX_INSERT:
1665                 code = InsetBase::INDEX_CODE;
1666                 break;
1667         case LFUN_INDEX_PRINT:
1668                 code = InsetBase::INDEX_PRINT_CODE;
1669                 break;
1670         case LFUN_NOMENCL_INSERT:
1671                 code = InsetBase::NOMENCL_CODE;
1672                 break;
1673         case LFUN_NOMENCL_PRINT:
1674                 code = InsetBase::NOMENCL_PRINT_CODE;
1675                 break;
1676         case LFUN_TOC_INSERT:
1677                 code = InsetBase::TOC_CODE;
1678                 break;
1679         case LFUN_HTML_INSERT:
1680         case LFUN_URL_INSERT:
1681                 code = InsetBase::URL_CODE;
1682                 break;
1683         case LFUN_QUOTE_INSERT:
1684                 // always allow this, since we will inset a raw quote
1685                 // if an inset is not allowed.
1686                 break;
1687         case LFUN_HYPHENATION_POINT_INSERT:
1688         case LFUN_LIGATURE_BREAK_INSERT:
1689         case LFUN_HFILL_INSERT:
1690         case LFUN_MENU_SEPARATOR_INSERT:
1691         case LFUN_DOTS_INSERT:
1692         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1693                 code = InsetBase::SPECIALCHAR_CODE;
1694                 break;
1695         case LFUN_SPACE_INSERT:
1696                 // slight hack: we know this is allowed in math mode
1697                 if (cur.inTexted())
1698                         code = InsetBase::SPACE_CODE;
1699                 break;
1700
1701         case LFUN_INSET_MODIFY:
1702                 // We need to disable this, because we may get called for a
1703                 // tabular cell via
1704                 // InsetTabular::getStatus() -> InsetText::getStatus()
1705                 // and we don't handle LFUN_INSET_MODIFY.
1706                 enable = false;
1707                 break;
1708
1709         case LFUN_FONT_EMPH:
1710                 flag.setOnOff(font.emph() == LyXFont::ON);
1711                 return true;
1712
1713         case LFUN_FONT_NOUN:
1714                 flag.setOnOff(font.noun() == LyXFont::ON);
1715                 return true;
1716
1717         case LFUN_FONT_BOLD:
1718                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1719                 return true;
1720
1721         case LFUN_FONT_SANS:
1722                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1723                 return true;
1724
1725         case LFUN_FONT_ROMAN:
1726                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1727                 return true;
1728
1729         case LFUN_FONT_CODE:
1730                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1731                 return true;
1732
1733         case LFUN_CUT:
1734         case LFUN_COPY:
1735                 enable = cur.selection();
1736                 break;
1737
1738         case LFUN_PASTE:
1739                 enable = cap::numberOfSelections() > 0;
1740                 break;
1741
1742         case LFUN_PARAGRAPH_MOVE_UP:
1743                 enable = cur.pit() > 0 && !cur.selection();
1744                 break;
1745
1746         case LFUN_PARAGRAPH_MOVE_DOWN:
1747                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1748                 break;
1749
1750         case LFUN_INSET_DISSOLVE:
1751                 enable = !isMainText(*cur.bv().buffer()) && cur.inset().nargs() == 1;
1752                 break;
1753
1754         case LFUN_CHANGE_ACCEPT:
1755         case LFUN_CHANGE_REJECT:
1756                 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1757                 // In principle, these LFUNs should only be enabled if there
1758                 // is a change at the current position/in the current selection.
1759                 // However, without proper optimizations, this will inevitably
1760                 // result in unacceptable performance - just imagine a user who
1761                 // wants to select the complete content of a long document.
1762                 enable = true;
1763                 break;
1764
1765         case LFUN_WORD_DELETE_FORWARD:
1766         case LFUN_WORD_DELETE_BACKWARD:
1767         case LFUN_LINE_DELETE:
1768         case LFUN_WORD_FORWARD:
1769         case LFUN_WORD_BACKWARD:
1770         case LFUN_CHAR_FORWARD:
1771         case LFUN_CHAR_FORWARD_SELECT:
1772         case LFUN_CHAR_BACKWARD:
1773         case LFUN_CHAR_BACKWARD_SELECT:
1774         case LFUN_UP:
1775         case LFUN_UP_SELECT:
1776         case LFUN_DOWN:
1777         case LFUN_DOWN_SELECT:
1778         case LFUN_PARAGRAPH_UP_SELECT:
1779         case LFUN_PARAGRAPH_DOWN_SELECT:
1780         case LFUN_SCREEN_UP_SELECT:
1781         case LFUN_SCREEN_DOWN_SELECT:
1782         case LFUN_LINE_BEGIN_SELECT:
1783         case LFUN_LINE_END_SELECT:
1784         case LFUN_WORD_FORWARD_SELECT:
1785         case LFUN_WORD_BACKWARD_SELECT:
1786         case LFUN_WORD_SELECT:
1787         case LFUN_PARAGRAPH_UP:
1788         case LFUN_PARAGRAPH_DOWN:
1789         case LFUN_SCREEN_UP:
1790         case LFUN_SCREEN_DOWN:
1791         case LFUN_LINE_BEGIN:
1792         case LFUN_LINE_END:
1793         case LFUN_BREAK_LINE:
1794         case LFUN_CHAR_DELETE_FORWARD:
1795         case LFUN_DELETE_FORWARD_SKIP:
1796         case LFUN_CHAR_DELETE_BACKWARD:
1797         case LFUN_DELETE_BACKWARD_SKIP:
1798         case LFUN_BREAK_PARAGRAPH:
1799         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1800         case LFUN_BREAK_PARAGRAPH_SKIP:
1801         case LFUN_PARAGRAPH_SPACING:
1802         case LFUN_INSET_INSERT:
1803         case LFUN_WORD_UPCASE:
1804         case LFUN_WORD_LOWCASE:
1805         case LFUN_WORD_CAPITALIZE:
1806         case LFUN_CHARS_TRANSPOSE:
1807         case LFUN_SERVER_GET_XY:
1808         case LFUN_SERVER_SET_XY:
1809         case LFUN_SERVER_GET_FONT:
1810         case LFUN_SERVER_GET_LAYOUT:
1811         case LFUN_LAYOUT:
1812         case LFUN_CLIPBOARD_PASTE:
1813         case LFUN_PRIMARY_SELECTION_PASTE:
1814         case LFUN_DATE_INSERT:
1815         case LFUN_SELF_INSERT:
1816         case LFUN_LINE_INSERT:
1817         case LFUN_PAGEBREAK_INSERT:
1818         case LFUN_CLEARPAGE_INSERT:
1819         case LFUN_CLEARDOUBLEPAGE_INSERT:
1820         case LFUN_MATH_DISPLAY:
1821         case LFUN_MATH_IMPORT_SELECTION:
1822         case LFUN_MATH_MODE:
1823         case LFUN_MATH_MACRO:
1824         case LFUN_MATH_MATRIX:
1825         case LFUN_MATH_DELIM:
1826         case LFUN_MATH_BIGDELIM:
1827         case LFUN_MATH_SUBSCRIPT:
1828         case LFUN_MATH_SUPERSCRIPT:
1829         case LFUN_FONT_DEFAULT:
1830         case LFUN_FONT_UNDERLINE:
1831         case LFUN_FONT_SIZE:
1832         case LFUN_LANGUAGE:
1833         case LFUN_FONT_FREE_APPLY:
1834         case LFUN_FONT_FREE_UPDATE:
1835         case LFUN_LAYOUT_PARAGRAPH:
1836         case LFUN_PARAGRAPH_UPDATE:
1837         case LFUN_ACCENT_UMLAUT:
1838         case LFUN_ACCENT_CIRCUMFLEX:
1839         case LFUN_ACCENT_GRAVE:
1840         case LFUN_ACCENT_ACUTE:
1841         case LFUN_ACCENT_TILDE:
1842         case LFUN_ACCENT_CEDILLA:
1843         case LFUN_ACCENT_MACRON:
1844         case LFUN_ACCENT_DOT:
1845         case LFUN_ACCENT_UNDERDOT:
1846         case LFUN_ACCENT_UNDERBAR:
1847         case LFUN_ACCENT_CARON:
1848         case LFUN_ACCENT_SPECIAL_CARON:
1849         case LFUN_ACCENT_BREVE:
1850         case LFUN_ACCENT_TIE:
1851         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1852         case LFUN_ACCENT_CIRCLE:
1853         case LFUN_ACCENT_OGONEK:
1854         case LFUN_THESAURUS_ENTRY:
1855         case LFUN_PARAGRAPH_PARAMS_APPLY:
1856         case LFUN_ESCAPE:
1857         case LFUN_BUFFER_END:
1858         case LFUN_BUFFER_BEGIN:
1859         case LFUN_BUFFER_BEGIN_SELECT:
1860         case LFUN_BUFFER_END_SELECT:
1861         case LFUN_UNICODE_INSERT:
1862                 // these are handled in our dispatch()
1863                 enable = true;
1864                 break;
1865
1866         default:
1867                 return false;
1868         }
1869
1870         if (code != InsetBase::NO_CODE
1871             && (cur.empty() || !cur.inset().insetAllowed(code)))
1872                 enable = false;
1873
1874         flag.enabled(enable);
1875         return true;
1876 }
1877
1878
1879 } // namespace lyx