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