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