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