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