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