]> git.lyx.org Git - lyx.git/blob - src/text3.C
fix compiler warnings about unused parameter
[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::pasteClipboard;
80 using cap::pasteSelection;
81 using cap::replaceSelection;
82
83 using support::isStrUnsignedInt;
84 using support::token;
85
86 using std::endl;
87 using std::string;
88 using std::istringstream;
89 using std::ostringstream;
90
91
92 extern string current_layout;
93
94
95 namespace {
96
97         // globals...
98         LyXFont freefont(LyXFont::ALL_IGNORE);
99         bool toggleall = false;
100
101
102         void toggleAndShow(LCursor & cur, LyXText * text,
103                 LyXFont const & font, bool toggleall = true)
104         {
105                 text->toggleFree(cur, font, toggleall);
106
107                 if (font.language() != ignore_language ||
108                                 font.number() != LyXFont::IGNORE) {
109                         Paragraph & par = cur.paragraph();
110                         text->bidi.computeTables(par, cur.buffer(), cur.textRow());
111                         if (cur.boundary() !=
112                                         text->bidi.isBoundary(cur.buffer(), par,
113                                                         cur.pos(),
114                                                         text->real_current_font))
115                                 text->setCursor(cur, cur.pit(), cur.pos(),
116                                                 false, !cur.boundary());
117                 }
118         }
119
120
121         void moveCursor(LCursor & cur, bool selecting)
122         {
123                 if (selecting || cur.mark())
124                         cur.setSelection();
125                 theSelection().haveSelection(cur.selection());
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                         if (!cur.selection())
558                                 recordUndo(cur);
559                         cap::replaceSelection(cur);
560                         cur.insert(new InsetNewline);
561                         cur.posRight();
562                         moveCursor(cur, false);
563                 }
564                 break;
565         }
566
567         case LFUN_CHAR_DELETE_FORWARD:
568                 if (!cur.selection()) {
569                         if (cur.pos() == cur.paragraph().size())
570                                 // Par boundary, force full-screen update
571                                 singleParUpdate = false;
572                         needsUpdate |= erase(cur);
573                         cur.resetAnchor();
574                         // It is possible to make it a lot faster still
575                         // just comment out the line below...
576                 } else {
577                         cutSelection(cur, true, false);
578                         singleParUpdate = false;
579                 }
580                 moveCursor(cur, false);
581                 break;
582
583         case LFUN_DELETE_FORWARD_SKIP:
584                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
585                 if (!cur.selection()) {
586                         if (cur.pos() == cur.lastpos()) {
587                                 cursorRight(cur);
588                                 cursorLeft(cur);
589                         }
590                         erase(cur);
591                         cur.resetAnchor();
592                 } else {
593                         cutSelection(cur, true, false);
594                 }
595                 break;
596
597
598         case LFUN_CHAR_DELETE_BACKWARD:
599                 if (!cur.selection()) {
600                         if (bv->getIntl().getTransManager().backspace()) {
601                                 // Par boundary, full-screen update
602                                 if (cur.pos() == 0)
603                                         singleParUpdate = false;
604                                 needsUpdate |= backspace(cur);
605                                 cur.resetAnchor();
606                                 // It is possible to make it a lot faster still
607                                 // just comment out the line below...
608                         }
609                 } else {
610                         cutSelection(cur, true, false);
611                         singleParUpdate = false;
612                 }
613                 bv->switchKeyMap();
614                 break;
615
616         case LFUN_DELETE_BACKWARD_SKIP:
617                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
618                 if (!cur.selection()) {
619 #ifdef WITH_WARNINGS
620 #warning look here
621 #endif
622                         //CursorSlice cur = cursor();
623                         backspace(cur);
624                         //anchor() = cur;
625                 } else {
626                         cutSelection(cur, true, false);
627                 }
628                 break;
629
630         case LFUN_BREAK_PARAGRAPH:
631                 cap::replaceSelection(cur);
632                 breakParagraph(cur, 0);
633                 cur.resetAnchor();
634                 bv->switchKeyMap();
635                 break;
636
637         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
638                 cap::replaceSelection(cur);
639                 breakParagraph(cur, 1);
640                 cur.resetAnchor();
641                 bv->switchKeyMap();
642                 break;
643
644         case LFUN_BREAK_PARAGRAPH_SKIP: {
645                 // When at the beginning of a paragraph, remove
646                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
647                 cap::replaceSelection(cur);
648                 if (cur.pos() == 0)
649                         cur.paragraph().params().labelWidthString(docstring());
650                 else
651                         breakParagraph(cur, 0);
652                 cur.resetAnchor();
653                 bv->switchKeyMap();
654                 break;
655         }
656
657         case LFUN_PARAGRAPH_SPACING: {
658                 Paragraph & par = cur.paragraph();
659                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
660                 string cur_value = "1.0";
661                 if (cur_spacing == Spacing::Other)
662                         cur_value = par.params().spacing().getValueAsString();
663
664                 istringstream is(to_utf8(cmd.argument()));
665                 string tmp;
666                 is >> tmp;
667                 Spacing::Space new_spacing = cur_spacing;
668                 string new_value = cur_value;
669                 if (tmp.empty()) {
670                         lyxerr << "Missing argument to `paragraph-spacing'"
671                                << endl;
672                 } else if (tmp == "single") {
673                         new_spacing = Spacing::Single;
674                 } else if (tmp == "onehalf") {
675                         new_spacing = Spacing::Onehalf;
676                 } else if (tmp == "double") {
677                         new_spacing = Spacing::Double;
678                 } else if (tmp == "other") {
679                         new_spacing = Spacing::Other;
680                         string tmpval = "0.0";
681                         is >> tmpval;
682                         lyxerr << "new_value = " << tmpval << endl;
683                         if (tmpval != "0.0")
684                                 new_value = tmpval;
685                 } else if (tmp == "default") {
686                         new_spacing = Spacing::Default;
687                 } else {
688                         lyxerr << to_utf8(_("Unknown spacing argument: "))
689                                << to_utf8(cmd.argument()) << endl;
690                 }
691                 if (cur_spacing != new_spacing || cur_value != new_value)
692                         par.params().spacing(Spacing(new_spacing, new_value));
693                 break;
694         }
695
696         case LFUN_INSET_INSERT: {
697                 recordUndo(cur);
698                 InsetBase * inset = createInset(bv, cmd);
699                 if (inset) {
700                         insertInset(cur, inset);
701                         cur.posRight();
702                 }
703                 break;
704         }
705
706         case LFUN_INSET_DISSOLVE:
707                 needsUpdate |= dissolveInset(cur);
708                 break;
709
710         case LFUN_INSET_SETTINGS:
711                 cur.inset().showInsetDialog(bv);
712                 break;
713
714         case LFUN_SPACE_INSERT:
715                 if (cur.paragraph().layout()->free_spacing)
716                         insertChar(cur, ' ');
717                 else {
718                         doInsertInset(cur, this, cmd, false, false);
719                         cur.posRight();
720                 }
721                 moveCursor(cur, false);
722                 break;
723
724         case LFUN_HYPHENATION_POINT_INSERT:
725                 specialChar(cur, InsetSpecialChar::HYPHENATION);
726                 break;
727
728         case LFUN_LIGATURE_BREAK_INSERT:
729                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
730                 break;
731
732         case LFUN_DOTS_INSERT:
733                 specialChar(cur, InsetSpecialChar::LDOTS);
734                 break;
735
736         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
737                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
738                 break;
739
740         case LFUN_MENU_SEPARATOR_INSERT:
741                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
742                 break;
743
744         case LFUN_WORD_UPCASE:
745                 changeCase(cur, LyXText::text_uppercase);
746                 break;
747
748         case LFUN_WORD_LOWCASE:
749                 changeCase(cur, LyXText::text_lowercase);
750                 break;
751
752         case LFUN_WORD_CAPITALIZE:
753                 changeCase(cur, LyXText::text_capitalization);
754                 break;
755
756         case LFUN_CHARS_TRANSPOSE:
757                 charsTranspose(cur);
758                 break;
759
760         case LFUN_PASTE:
761                 cur.message(_("Paste"));
762                 cap::replaceSelection(cur);
763                 if (cmd.argument().empty() && !theClipboard().isInternal())
764                         pasteClipboard(cur, bv->buffer()->errorList("Paste"));
765                 else {
766                         string const arg(to_utf8(cmd.argument()));
767                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
768                                         isStrUnsignedInt(arg) ?
769                                                 convert<unsigned int>(arg) :
770                                                 0);
771                 }
772                 bv->buffer()->errors("Paste");
773                 cur.clearSelection(); // bug 393
774                 bv->switchKeyMap();
775                 finishUndo();
776                 break;
777
778         case LFUN_CUT:
779                 cutSelection(cur, true, true);
780                 cur.message(_("Cut"));
781                 break;
782
783         case LFUN_COPY:
784                 copySelection(cur);
785                 cur.message(_("Copy"));
786                 break;
787
788         case LFUN_SERVER_GET_XY:
789                 cur.message(from_utf8(
790                         convert<string>(cursorX(cur.bv(), cur.top(), cur.boundary()))
791                         + ' ' + convert<string>(cursorY(cur.bv(), cur.top(), cur.boundary()))));
792                 break;
793
794         case LFUN_SERVER_SET_XY: {
795                 int x = 0;
796                 int y = 0;
797                 istringstream is(to_utf8(cmd.argument()));
798                 is >> x >> y;
799                 if (!is)
800                         lyxerr << "SETXY: Could not parse coordinates in '"
801                                << to_utf8(cmd.argument()) << std::endl;
802                 else
803                         setCursorFromCoordinates(cur, x, y);
804                 break;
805         }
806
807         case LFUN_SERVER_GET_FONT:
808                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
809                         cur.message(from_ascii("E"));
810                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
811                         cur.message(from_ascii("N"));
812                 else
813                         cur.message(from_ascii("0"));
814                 break;
815
816         case LFUN_SERVER_GET_LAYOUT:
817                 cur.message(from_utf8(cur.paragraph().layout()->name()));
818                 break;
819
820         case LFUN_LAYOUT: {
821                 string layout = to_ascii(cmd.argument());
822                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) " << layout << endl;
823
824                 // Derive layout number from given argument (string)
825                 // and current buffer's textclass (number)
826                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
827                 if (layout.empty())
828                         layout = tclass.defaultLayoutName();
829                 bool hasLayout = tclass.hasLayout(layout);
830
831                 // If the entry is obsolete, use the new one instead.
832                 if (hasLayout) {
833                         string const & obs = tclass[layout]->obsoleted_by();
834                         if (!obs.empty())
835                                 layout = obs;
836                 }
837
838                 if (!hasLayout) {
839                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
840                                 from_utf8(N_(" not known")));
841                         break;
842                 }
843
844                 bool change_layout = (current_layout != layout);
845
846                 if (!change_layout && cur.selection() &&
847                         cur.selBegin().pit() != cur.selEnd().pit())
848                 {
849                         pit_type spit = cur.selBegin().pit();
850                         pit_type epit = cur.selEnd().pit() + 1;
851                         while (spit != epit) {
852                                 if (pars_[spit].layout()->name() != current_layout) {
853                                         change_layout = true;
854                                         break;
855                                 }
856                                 ++spit;
857                         }
858                 }
859
860                 if (change_layout) {
861                         current_layout = layout;
862                         setLayout(cur, layout);
863                         // inform the GUI that the layout has changed.
864                         bv->layoutChanged(layout);
865                         bv->switchKeyMap();
866                 }
867                 break;
868         }
869
870         case LFUN_CLIPBOARD_PASTE:
871                 cur.clearSelection();
872                 pasteClipboard(cur, bv->buffer()->errorList("Paste"),
873                                cmd.argument() == "paragraph");
874                 bv->buffer()->errors("Paste");
875                 break;
876
877         case LFUN_PRIMARY_SELECTION_PASTE:
878                 pasteString(cur, theSelection().get(),
879                             cmd.argument() == "paragraph");
880                 break;
881
882         case LFUN_UNICODE_INSERT: {
883                 if (cmd.argument().empty())
884                         break;
885                 docstring hexstring = cmd.argument();
886                 if (lyx::support::isHex(hexstring)) {
887                         char_type c = lyx::support::hexToInt(hexstring);
888                         if (c > 32 && c < 0x10ffff) {
889                                 lyxerr << "Inserting c: " << c << endl;
890                                 docstring s = docstring(1, c);
891                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
892                         }
893                 }
894                 break;
895         }
896                 
897         case LFUN_QUOTE_INSERT: {
898                 cap::replaceSelection(cur);
899                 Paragraph & par = cur.paragraph();
900                 pos_type pos = cur.pos();
901                 char_type c;
902                 if (pos == 0)
903                         c = ' ';
904                 else if (cur.prevInset() && cur.prevInset()->isSpace())
905                         c = ' ';
906                 else
907                         c = par.getChar(pos - 1);
908
909                 LyXLayout_ptr const & style = par.layout();
910
911                 BufferParams const & bufparams = bv->buffer()->params();
912                 if (!style->pass_thru
913                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
914                         string arg = to_utf8(cmd.argument());
915                         if (arg == "single")
916                                 cur.insert(new InsetQuotes(c,
917                                     bufparams.quotes_language,
918                                     InsetQuotes::SingleQ));
919                         else
920                                 cur.insert(new InsetQuotes(c,
921                                     bufparams.quotes_language,
922                                     InsetQuotes::DoubleQ));
923                         cur.posRight();
924                 }
925                 else
926                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
927                 break;
928         }
929
930         case LFUN_DATE_INSERT:
931                 if (cmd.argument().empty())
932                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
933                                 formatted_time(current_time())));
934                 else
935                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
936                                 formatted_time(current_time(), to_utf8(cmd.argument()))));
937                 break;
938
939         case LFUN_MOUSE_TRIPLE:
940                 if (cmd.button() == mouse_button::button1) {
941                         cursorHome(cur);
942                         cur.resetAnchor();
943                         cursorEnd(cur);
944                         cur.setSelection();
945                         bv->cursor() = cur;
946                         theSelection().haveSelection(cur.selection());
947                 }
948                 break;
949
950         case LFUN_MOUSE_DOUBLE:
951                 if (cmd.button() == mouse_button::button1) {
952                         selectWord(cur, WHOLE_WORD_STRICT);
953                         bv->cursor() = cur;
954                         theSelection().haveSelection(cur.selection());
955                 }
956                 break;
957
958         // Single-click on work area
959         case LFUN_MOUSE_PRESS: {
960                 // Right click on a footnote flag opens float menu
961                 if (cmd.button() == mouse_button::button3)
962                         cur.clearSelection();
963
964                 // Middle button press pastes if we have a selection
965                 // We do this here as if the selection was inside an inset
966                 // it could get cleared on the unlocking of the inset so
967                 // we have to check this first
968                 bool paste_internally = false;
969                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
970                         // Copy the selection to the clipboard stack. This
971                         // is done for two reasons:
972                         // - We want it to appear in the "Edit->Paste recent"
973                         //   menu.
974                         // - We can then use the normal copy/paste machinery
975                         //   instead of theSelection().get() to preserve
976                         //   formatting of the pasted stuff.
977                         cap::copySelectionToStack(cur.bv().cursor());
978                         paste_internally = true;
979                 }
980
981                 // we have to update after dePM triggered
982                 bool update = bv->mouseSetCursor(cur);
983
984                 // Insert primary selection with middle mouse
985                 // if there is a local selection in the current buffer,
986                 // insert this
987                 if (cmd.button() == mouse_button::button2) {
988                         if (paste_internally)
989                                 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
990                         else
991                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
992                 }
993
994                 if (!update && cmd.button() == mouse_button::button1) {
995                         needsUpdate = false;
996                         cur.noUpdate();
997                 }
998
999                 break;
1000         }
1001
1002         case LFUN_MOUSE_MOTION: {
1003                 // Only use motion with button 1
1004                 //if (cmd.button() != mouse_button::button1)
1005                 //      return false;
1006
1007                 // ignore motions deeper nested than the real anchor
1008                 LCursor & bvcur = cur.bv().cursor();
1009                 if (bvcur.anchor_.hasPart(cur)) {
1010                         CursorSlice old = bvcur.top();
1011
1012                         int const wh = bv->workHeight();
1013                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1014
1015                         setCursorFromCoordinates(cur, cmd.x, y);
1016                         cur.x_target() = cmd.x;
1017                         if (cmd.y >= wh)
1018                                 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1019                         else if (cmd.y < 0)
1020                                 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1021                         // This is to allow jumping over large insets
1022                         if (cur.top() == old) {
1023                                 if (cmd.y >= wh)
1024                                         lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1025                                 else if (cmd.y < 0)
1026                                         lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1027                         }
1028
1029                         if (cur.top() == old)
1030                                 cur.noUpdate();
1031                         else {
1032                                 // don't set anchor_
1033                                 bvcur.setCursor(cur);
1034                                 bvcur.selection() = true;
1035                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1036                         }
1037
1038                 } else
1039                         cur.undispatched();
1040                 break;
1041         }
1042
1043         case LFUN_MOUSE_RELEASE: {
1044                 if (cmd.button() == mouse_button::button2)
1045                         break;
1046
1047                 // finish selection
1048                 if (cmd.button() == mouse_button::button1) {
1049                         if (cur.selection())
1050                                 theSelection().haveSelection(true);
1051                         needsUpdate = false;
1052                         cur.noUpdate();
1053                 }
1054
1055                 bv->switchKeyMap();
1056                 break;
1057         }
1058
1059         case LFUN_SELF_INSERT: {
1060                 if (cmd.argument().empty())
1061                         break;
1062
1063                 // Automatically delete the currently selected
1064                 // text and replace it with what is being
1065                 // typed in now. Depends on lyxrc settings
1066                 // "auto_region_delete", which defaults to
1067                 // true (on).
1068
1069                 if (lyxrc.auto_region_delete) {
1070                         if (cur.selection())
1071                                 cutSelection(cur, false, false);
1072                                 // cutSelection clears the X selection.
1073                         else
1074                                 theSelection().haveSelection(false);
1075                 }
1076
1077                 cur.clearSelection();
1078                 LyXFont const old_font = real_current_font;
1079
1080                 docstring::const_iterator cit = cmd.argument().begin();
1081                 docstring::const_iterator end = cmd.argument().end();
1082                 for (; cit != end; ++cit)
1083 #if 0
1084                         bv->getIntl().getTransManager().
1085                                 translateAndInsert(*cit, this);
1086 #else
1087                         insertChar(bv->cursor(), *cit);
1088 #endif
1089
1090                 cur.resetAnchor();
1091                 moveCursor(cur, false);
1092                 break;
1093         }
1094
1095         case LFUN_URL_INSERT: {
1096                 InsetCommandParams p("url");
1097                 string const data = InsetCommandMailer::params2string("url", p);
1098                 bv->showInsetDialog("url", data, 0);
1099                 break;
1100         }
1101
1102         case LFUN_HTML_INSERT: {
1103                 InsetCommandParams p("htmlurl");
1104                 string const data = InsetCommandMailer::params2string("url", p);
1105                 bv->showInsetDialog("url", data, 0);
1106                 break;
1107         }
1108
1109         case LFUN_LABEL_INSERT: {
1110                 InsetCommandParams p("label");
1111                 // Try to generate a valid label
1112                 p["name"] = (cmd.argument().empty()) ?
1113                         cur.getPossibleLabel() :
1114                         cmd.argument();
1115                 string const data = InsetCommandMailer::params2string("label", p);
1116
1117                 if (cmd.argument().empty()) {
1118                         bv->showInsetDialog("label", data, 0);
1119                 } else {
1120                         FuncRequest fr(LFUN_INSET_INSERT, data);
1121                         dispatch(cur, fr);
1122                 }
1123                 break;
1124         }
1125
1126
1127 #if 0
1128         case LFUN_LIST_INSERT:
1129         case LFUN_THEOREM_INSERT:
1130         case LFUN_CAPTION_INSERT:
1131 #endif
1132         case LFUN_NOTE_INSERT:
1133         case LFUN_CHARSTYLE_INSERT:
1134         case LFUN_BOX_INSERT:
1135         case LFUN_BRANCH_INSERT:
1136         case LFUN_BIBITEM_INSERT:
1137         case LFUN_ERT_INSERT:
1138         case LFUN_FOOTNOTE_INSERT:
1139         case LFUN_MARGINALNOTE_INSERT:
1140         case LFUN_OPTIONAL_INSERT:
1141         case LFUN_ENVIRONMENT_INSERT:
1142                 // Open the inset, and move the current selection
1143                 // inside it.
1144                 doInsertInset(cur, this, cmd, true, true);
1145                 cur.posRight();
1146                 break;
1147
1148         case LFUN_TABULAR_INSERT:
1149                 // if there were no arguments, just open the dialog
1150                 if (doInsertInset(cur, this, cmd, false, true))
1151                         cur.posRight();
1152                 else
1153                         bv->showDialog("tabularcreate");
1154
1155                 break;
1156
1157         case LFUN_FLOAT_INSERT:
1158         case LFUN_FLOAT_WIDE_INSERT:
1159         case LFUN_WRAP_INSERT:
1160                 doInsertInset(cur, this, cmd, true, true);
1161                 cur.posRight();
1162                 // FIXME: the "Caption" name should not be hardcoded,
1163                 // but given by the float definition.
1164                 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1165                 break;
1166
1167         case LFUN_INDEX_INSERT:
1168         case LFUN_NOMENCL_INSERT: {
1169                 InsetBase * inset = createInset(&cur.bv(), cmd);
1170                 if (!inset)
1171                         break;
1172
1173                 recordUndo(cur);
1174                 cur.clearSelection();
1175                 insertInset(cur, inset);
1176                 inset->edit(cur, true);
1177                 // Show the dialog for the nomenclature entry, since the
1178                 // description entry still needs to be filled in.
1179                 if (cmd.action == LFUN_NOMENCL_INSERT)
1180                         InsetCommandMailer("nomenclature",
1181                                 *reinterpret_cast<InsetCommand *>(inset)).showDialog(&cur.bv());
1182                 cur.posRight();
1183                 break;
1184         }
1185
1186         case LFUN_INDEX_PRINT:
1187         case LFUN_NOMENCL_PRINT:
1188         case LFUN_TOC_INSERT:
1189         case LFUN_HFILL_INSERT:
1190         case LFUN_LINE_INSERT:
1191         case LFUN_PAGEBREAK_INSERT:
1192         case LFUN_CLEARPAGE_INSERT:
1193         case LFUN_CLEARDOUBLEPAGE_INSERT:
1194                 // do nothing fancy
1195                 doInsertInset(cur, this, cmd, false, false);
1196                 cur.posRight();
1197                 break;
1198
1199         case LFUN_DEPTH_DECREMENT:
1200                 changeDepth(cur, DEC_DEPTH);
1201                 break;
1202
1203         case LFUN_DEPTH_INCREMENT:
1204                 changeDepth(cur, INC_DEPTH);
1205                 break;
1206
1207         case LFUN_MATH_DISPLAY:
1208                 mathDispatch(cur, cmd, true);
1209                 break;
1210
1211         case LFUN_MATH_IMPORT_SELECTION:
1212         case LFUN_MATH_MODE:
1213                 if (cmd.argument() == "on")
1214                         // don't pass "on" as argument
1215                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1216                 else
1217                         mathDispatch(cur, cmd, false);
1218                 break;
1219
1220         case LFUN_MATH_MACRO:
1221                 if (cmd.argument().empty())
1222                         cur.errorMessage(from_utf8(N_("Missing argument")));
1223                 else {
1224                         string s = to_utf8(cmd.argument());
1225                         string const s1 = token(s, ' ', 1);
1226                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1227                         string const s2 = token(s, ' ', 2);
1228                         string const type = s2.empty() ? "newcommand" : s2;
1229                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1230                         //cur.nextInset()->edit(cur, true);
1231                 }
1232                 break;
1233
1234         // passthrough hat and underscore outside mathed:
1235         case LFUN_MATH_SUBSCRIPT:
1236                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1237                 break;
1238         case LFUN_MATH_SUPERSCRIPT:
1239                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1240                 break;
1241
1242         case LFUN_MATH_INSERT:
1243         case LFUN_MATH_MATRIX:
1244         case LFUN_MATH_DELIM:
1245         case LFUN_MATH_BIGDELIM: {
1246                 cur.insert(new InsetMathHull(hullSimple));
1247                 cur.dispatch(FuncRequest(LFUN_CHAR_FORWARD));
1248                 cur.dispatch(cmd);
1249                 break;
1250         }
1251
1252         case LFUN_FONT_EMPH: {
1253                 LyXFont font(LyXFont::ALL_IGNORE);
1254                 font.setEmph(LyXFont::TOGGLE);
1255                 toggleAndShow(cur, this, font);
1256                 break;
1257         }
1258
1259         case LFUN_FONT_BOLD: {
1260                 LyXFont font(LyXFont::ALL_IGNORE);
1261                 font.setSeries(LyXFont::BOLD_SERIES);
1262                 toggleAndShow(cur, this, font);
1263                 break;
1264         }
1265
1266         case LFUN_FONT_NOUN: {
1267                 LyXFont font(LyXFont::ALL_IGNORE);
1268                 font.setNoun(LyXFont::TOGGLE);
1269                 toggleAndShow(cur, this, font);
1270                 break;
1271         }
1272
1273         case LFUN_FONT_CODE: {
1274                 LyXFont font(LyXFont::ALL_IGNORE);
1275                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1276                 toggleAndShow(cur, this, font);
1277                 break;
1278         }
1279
1280         case LFUN_FONT_SANS: {
1281                 LyXFont font(LyXFont::ALL_IGNORE);
1282                 font.setFamily(LyXFont::SANS_FAMILY);
1283                 toggleAndShow(cur, this, font);
1284                 break;
1285         }
1286
1287         case LFUN_FONT_ROMAN: {
1288                 LyXFont font(LyXFont::ALL_IGNORE);
1289                 font.setFamily(LyXFont::ROMAN_FAMILY);
1290                 toggleAndShow(cur, this, font);
1291                 break;
1292         }
1293
1294         case LFUN_FONT_DEFAULT: {
1295                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1296                 toggleAndShow(cur, this, font);
1297                 break;
1298         }
1299
1300         case LFUN_FONT_UNDERLINE: {
1301                 LyXFont font(LyXFont::ALL_IGNORE);
1302                 font.setUnderbar(LyXFont::TOGGLE);
1303                 toggleAndShow(cur, this, font);
1304                 break;
1305         }
1306
1307         case LFUN_FONT_SIZE: {
1308                 LyXFont font(LyXFont::ALL_IGNORE);
1309                 font.setLyXSize(to_utf8(cmd.argument()));
1310                 toggleAndShow(cur, this, font);
1311                 break;
1312         }
1313
1314         case LFUN_LANGUAGE: {
1315                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1316                 if (!lang)
1317                         break;
1318                 LyXFont font(LyXFont::ALL_IGNORE);
1319                 font.setLanguage(lang);
1320                 toggleAndShow(cur, this, font);
1321                 bv->switchKeyMap();
1322                 break;
1323         }
1324
1325         case LFUN_FONT_FREE_APPLY:
1326                 toggleAndShow(cur, this, freefont, toggleall);
1327                 cur.message(_("Character set"));
1328                 break;
1329
1330         // Set the freefont using the contents of \param data dispatched from
1331         // the frontends and apply it at the current cursor location.
1332         case LFUN_FONT_FREE_UPDATE: {
1333                 LyXFont font;
1334                 bool toggle;
1335                 if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) {
1336                         freefont = font;
1337                         toggleall = toggle;
1338                         toggleAndShow(cur, this, freefont, toggleall);
1339                         cur.message(_("Character set"));
1340                 }
1341                 break;
1342         }
1343
1344         case LFUN_FINISHED_LEFT:
1345                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1346                 break;
1347
1348         case LFUN_FINISHED_RIGHT:
1349                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1350                 ++cur.pos();
1351                 break;
1352
1353         case LFUN_FINISHED_UP:
1354                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1355                 cursorUp(cur);
1356                 break;
1357
1358         case LFUN_FINISHED_DOWN:
1359                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1360                 cursorDown(cur);
1361                 break;
1362
1363         case LFUN_LAYOUT_PARAGRAPH: {
1364                 string data;
1365                 params2string(cur.paragraph(), data);
1366                 data = "show\n" + data;
1367                 bv->showDialogWithData("paragraph", data);
1368                 break;
1369         }
1370
1371         case LFUN_PARAGRAPH_UPDATE: {
1372                 string data;
1373                 params2string(cur.paragraph(), data);
1374
1375                 // Will the paragraph accept changes from the dialog?
1376                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1377
1378                 data = "update " + convert<string>(accept) + '\n' + data;
1379                 bv->updateDialog("paragraph", data);
1380                 break;
1381         }
1382
1383         case LFUN_ACCENT_UMLAUT:
1384         case LFUN_ACCENT_CIRCUMFLEX:
1385         case LFUN_ACCENT_GRAVE:
1386         case LFUN_ACCENT_ACUTE:
1387         case LFUN_ACCENT_TILDE:
1388         case LFUN_ACCENT_CEDILLA:
1389         case LFUN_ACCENT_MACRON:
1390         case LFUN_ACCENT_DOT:
1391         case LFUN_ACCENT_UNDERDOT:
1392         case LFUN_ACCENT_UNDERBAR:
1393         case LFUN_ACCENT_CARON:
1394         case LFUN_ACCENT_SPECIAL_CARON:
1395         case LFUN_ACCENT_BREVE:
1396         case LFUN_ACCENT_TIE:
1397         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1398         case LFUN_ACCENT_CIRCLE:
1399         case LFUN_ACCENT_OGONEK:
1400                 theLyXFunc().handleKeyFunc(cmd.action);
1401                 if (!cmd.argument().empty())
1402                         // FIXME: Are all these characters encoded in one byte in utf8?
1403                         bv->getIntl().getTransManager()
1404                                 .translateAndInsert(cmd.argument()[0], this, cur);
1405                 break;
1406
1407         case LFUN_FLOAT_LIST: {
1408                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1409                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1410                         // not quite sure if we want this...
1411                         recordUndo(cur);
1412                         cur.clearSelection();
1413                         breakParagraph(cur);
1414
1415                         if (cur.lastpos() != 0) {
1416                                 cursorLeft(cur);
1417                                 breakParagraph(cur);
1418                         }
1419
1420                         setLayout(cur, tclass.defaultLayoutName());
1421                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, docstring(), 0);
1422                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1423                         cur.posRight();
1424                 } else {
1425                         lyxerr << "Non-existent float type: "
1426                                << to_utf8(cmd.argument()) << endl;
1427                 }
1428                 break;
1429         }
1430
1431         case LFUN_CHANGE_ACCEPT: {
1432                 acceptOrRejectChange(cur, true);
1433                 break;
1434         }
1435
1436         case LFUN_CHANGE_REJECT: {
1437                 acceptOrRejectChange(cur, false);
1438                 break;
1439         }
1440
1441         case LFUN_THESAURUS_ENTRY: {
1442                 docstring arg = cmd.argument();
1443                 if (arg.empty()) {
1444                         arg = cur.selectionAsString(false);
1445                         // FIXME
1446                         if (arg.size() > 100 || arg.empty()) {
1447                                 // Get word or selection
1448                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1449                                 arg = cur.selectionAsString(false);
1450                         }
1451                 }
1452                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1453                 break;
1454         }
1455
1456         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1457                 // Given data, an encoding of the ParagraphParameters
1458                 // generated in the Paragraph dialog, this function sets
1459                 // the current paragraph appropriately.
1460                 istringstream is(to_utf8(cmd.argument()));
1461                 LyXLex lex(0, 0);
1462                 lex.setStream(is);
1463                 ParagraphParameters params;
1464                 params.read(lex);
1465                 setParagraph(cur,
1466                              params.spacing(),
1467                              params.align(),
1468                              params.labelWidthString(),
1469                              params.noindent());
1470                 cur.message(_("Paragraph layout set"));
1471                 break;
1472         }
1473
1474         case LFUN_ESCAPE:
1475                 if (cur.selection()) {
1476                         cur.selection() = false;
1477                 } else {
1478                         cur.undispatched();
1479                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1480                 }
1481                 break;
1482
1483         default:
1484                 lyxerr[Debug::ACTION]
1485                         << BOOST_CURRENT_FUNCTION
1486                         << ": Command " << cmd
1487                         << " not DISPATCHED by LyXText" << endl;
1488                 cur.undispatched();
1489                 break;
1490         }
1491
1492         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1493
1494         // FIXME: The cursor flag is reset two lines below
1495         // so we need to check here if some of the LFUN did touch that.
1496         // for now only LyXText::erase() and LyXText::backspace() do that.
1497         // The plan is to verify all the LFUNs and then to remove this
1498         // singleParUpdate boolean altogether.
1499         if (cur.result().update() & Update::Force) {
1500                 singleParUpdate = false;
1501                 needsUpdate = true;
1502         }
1503
1504         // FIXME: the following code should go in favor of fine grained
1505         // update flag treatment.
1506         if (singleParUpdate) {
1507                 // Inserting characters does not change par height
1508                 ParagraphMetrics const & pms 
1509                         = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
1510                 if (pms.dim().height()
1511                     == olddim.height()) {
1512                         // if so, update _only_ this paragraph
1513                         cur.updateFlags(Update::SinglePar |
1514                                 Update::FitCursor |
1515                                 Update::MultiParSel);
1516                         return;
1517                 } else
1518                         needsUpdate = true;
1519         }
1520
1521         if (!needsUpdate
1522             && &oldTopSlice.inset() == &cur.inset()
1523             && oldTopSlice.idx() == cur.idx()
1524             && !sel // sel is a backup of cur.selection() at the biginning of the function.
1525             && !cur.selection())
1526                 // FIXME: it would be better if we could just do this
1527                 //
1528                 //if (cur.result().update() != Update::FitCursor)
1529                 //      cur.noUpdate();
1530                 // 
1531                 // But some LFUNs do not set Update::FitCursor when needed, so we
1532                 // do it for all. This is not very harmfull as FitCursor will provoke
1533                 // a full redraw only if needed but still, a proper review of all LFUN
1534                 // should be done and this needsUpdate boolean can then be removed.
1535                 cur.updateFlags(Update::FitCursor);
1536         else
1537                 cur.updateFlags(Update::Force | Update::FitCursor);
1538 }
1539
1540
1541 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1542                         FuncStatus & flag) const
1543 {
1544         BOOST_ASSERT(cur.text() == this);
1545
1546         LyXFont const & font = real_current_font;
1547         bool enable = true;
1548         InsetBase::Code code = InsetBase::NO_CODE;
1549
1550         switch (cmd.action) {
1551
1552         case LFUN_DEPTH_DECREMENT:
1553                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1554                 break;
1555
1556         case LFUN_DEPTH_INCREMENT:
1557                 enable = changeDepthAllowed(cur, INC_DEPTH);
1558                 break;
1559
1560         case LFUN_APPENDIX:
1561                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1562                 return true;
1563
1564         case LFUN_BIBITEM_INSERT:
1565                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1566                 break;
1567
1568         case LFUN_DIALOG_SHOW_NEW_INSET:
1569                 if (cmd.argument() == "bibitem")
1570                         code = InsetBase::BIBITEM_CODE;
1571                 else if (cmd.argument() == "bibtex")
1572                         code = InsetBase::BIBTEX_CODE;
1573                 else if (cmd.argument() == "box")
1574                         code = InsetBase::BOX_CODE;
1575                 else if (cmd.argument() == "branch")
1576                         code = InsetBase::BRANCH_CODE;
1577                 else if (cmd.argument() == "citation")
1578                         code = InsetBase::CITE_CODE;
1579                 else if (cmd.argument() == "ert")
1580                         code = InsetBase::ERT_CODE;
1581                 else if (cmd.argument() == "external")
1582                         code = InsetBase::EXTERNAL_CODE;
1583                 else if (cmd.argument() == "float")
1584                         code = InsetBase::FLOAT_CODE;
1585                 else if (cmd.argument() == "graphics")
1586                         code = InsetBase::GRAPHICS_CODE;
1587                 else if (cmd.argument() == "include")
1588                         code = InsetBase::INCLUDE_CODE;
1589                 else if (cmd.argument() == "index")
1590                         code = InsetBase::INDEX_CODE;
1591                 else if (cmd.argument() == "nomenclature")
1592                         code = InsetBase::NOMENCL_CODE;
1593                 else if (cmd.argument() == "label")
1594                         code = InsetBase::LABEL_CODE;
1595                 else if (cmd.argument() == "note")
1596                         code = InsetBase::NOTE_CODE;
1597                 else if (cmd.argument() == "ref")
1598                         code = InsetBase::REF_CODE;
1599                 else if (cmd.argument() == "toc")
1600                         code = InsetBase::TOC_CODE;
1601                 else if (cmd.argument() == "url")
1602                         code = InsetBase::URL_CODE;
1603                 else if (cmd.argument() == "vspace")
1604                         code = InsetBase::VSPACE_CODE;
1605                 else if (cmd.argument() == "wrap")
1606                         code = InsetBase::WRAP_CODE;
1607                 break;
1608
1609         case LFUN_ERT_INSERT:
1610                 code = InsetBase::ERT_CODE;
1611                 break;
1612         case LFUN_FOOTNOTE_INSERT:
1613                 code = InsetBase::FOOT_CODE;
1614                 break;
1615         case LFUN_TABULAR_INSERT:
1616                 code = InsetBase::TABULAR_CODE;
1617                 break;
1618         case LFUN_MARGINALNOTE_INSERT:
1619                 code = InsetBase::MARGIN_CODE;
1620                 break;
1621         case LFUN_FLOAT_INSERT:
1622         case LFUN_FLOAT_WIDE_INSERT:
1623                 code = InsetBase::FLOAT_CODE;
1624                 break;
1625         case LFUN_WRAP_INSERT:
1626                 code = InsetBase::WRAP_CODE;
1627                 break;
1628         case LFUN_FLOAT_LIST:
1629                 code = InsetBase::FLOAT_LIST_CODE;
1630                 break;
1631 #if 0
1632         case LFUN_LIST_INSERT:
1633                 code = InsetBase::LIST_CODE;
1634                 break;
1635         case LFUN_THEOREM_INSERT:
1636                 code = InsetBase::THEOREM_CODE;
1637                 break;
1638 #endif
1639         case LFUN_CAPTION_INSERT:
1640                 code = InsetBase::CAPTION_CODE;
1641                 break;
1642         case LFUN_NOTE_INSERT:
1643                 code = InsetBase::NOTE_CODE;
1644                 break;
1645         case LFUN_CHARSTYLE_INSERT:
1646                 code = InsetBase::CHARSTYLE_CODE;
1647                 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1648                         enable = false;
1649                 break;
1650         case LFUN_BOX_INSERT:
1651                 code = InsetBase::BOX_CODE;
1652                 break;
1653         case LFUN_BRANCH_INSERT:
1654                 code = InsetBase::BRANCH_CODE;
1655                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1656                         enable = false;
1657                 break;
1658         case LFUN_LABEL_INSERT:
1659                 code = InsetBase::LABEL_CODE;
1660                 break;
1661         case LFUN_OPTIONAL_INSERT:
1662                 code = InsetBase::OPTARG_CODE;
1663                 enable = numberOfOptArgs(cur.paragraph())
1664                         < cur.paragraph().layout()->optionalargs;
1665                 break;
1666         case LFUN_ENVIRONMENT_INSERT:
1667                 code = InsetBase::BOX_CODE;
1668                 break;
1669         case LFUN_INDEX_INSERT:
1670                 code = InsetBase::INDEX_CODE;
1671                 break;
1672         case LFUN_INDEX_PRINT:
1673                 code = InsetBase::INDEX_PRINT_CODE;
1674                 break;
1675         case LFUN_NOMENCL_INSERT:
1676                 code = InsetBase::NOMENCL_CODE;
1677                 break;
1678         case LFUN_NOMENCL_PRINT:
1679                 code = InsetBase::NOMENCL_PRINT_CODE;
1680                 break;
1681         case LFUN_TOC_INSERT:
1682                 code = InsetBase::TOC_CODE;
1683                 break;
1684         case LFUN_HTML_INSERT:
1685         case LFUN_URL_INSERT:
1686                 code = InsetBase::URL_CODE;
1687                 break;
1688         case LFUN_QUOTE_INSERT:
1689                 // always allow this, since we will inset a raw quote
1690                 // if an inset is not allowed.
1691                 break;
1692         case LFUN_HYPHENATION_POINT_INSERT:
1693         case LFUN_LIGATURE_BREAK_INSERT:
1694         case LFUN_HFILL_INSERT:
1695         case LFUN_MENU_SEPARATOR_INSERT:
1696         case LFUN_DOTS_INSERT:
1697         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1698                 code = InsetBase::SPECIALCHAR_CODE;
1699                 break;
1700         case LFUN_SPACE_INSERT:
1701                 // slight hack: we know this is allowed in math mode
1702                 if (cur.inTexted())
1703                         code = InsetBase::SPACE_CODE;
1704                 break;
1705
1706         case LFUN_INSET_MODIFY:
1707                 // We need to disable this, because we may get called for a
1708                 // tabular cell via
1709                 // InsetTabular::getStatus() -> InsetText::getStatus()
1710                 // and we don't handle LFUN_INSET_MODIFY.
1711                 enable = false;
1712                 break;
1713
1714         case LFUN_FONT_EMPH:
1715                 flag.setOnOff(font.emph() == LyXFont::ON);
1716                 return true;
1717
1718         case LFUN_FONT_NOUN:
1719                 flag.setOnOff(font.noun() == LyXFont::ON);
1720                 return true;
1721
1722         case LFUN_FONT_BOLD:
1723                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1724                 return true;
1725
1726         case LFUN_FONT_SANS:
1727                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1728                 return true;
1729
1730         case LFUN_FONT_ROMAN:
1731                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1732                 return true;
1733
1734         case LFUN_FONT_CODE:
1735                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1736                 return true;
1737
1738         case LFUN_CUT:
1739         case LFUN_COPY:
1740                 enable = cur.selection();
1741                 break;
1742
1743         case LFUN_PASTE:
1744                 if (cmd.argument().empty()) {
1745                         if (theClipboard().isInternal())
1746                                 enable = cap::numberOfSelections() > 0;
1747                         else
1748                                 enable = !theClipboard().empty();
1749                 } else {
1750                         string const arg = to_utf8(cmd.argument());
1751                         if (isStrUnsignedInt(arg)) {
1752                                 unsigned int n = convert<unsigned int>(arg);
1753                                 enable = cap::numberOfSelections() > n;
1754                         } else
1755                                 // unknown argument
1756                                 enable = false;
1757                 }
1758                 break;
1759
1760         case LFUN_CLIPBOARD_PASTE:
1761                 enable = !theClipboard().empty();
1762                 break;
1763
1764         case LFUN_PRIMARY_SELECTION_PASTE:
1765                 enable = cur.selection() || !theSelection().empty();
1766                 break;
1767
1768         case LFUN_PARAGRAPH_MOVE_UP:
1769                 enable = cur.pit() > 0 && !cur.selection();
1770                 break;
1771
1772         case LFUN_PARAGRAPH_MOVE_DOWN:
1773                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1774                 break;
1775
1776         case LFUN_INSET_DISSOLVE:
1777                 enable = !isMainText(*cur.bv().buffer()) && cur.inset().nargs() == 1;
1778                 break;
1779
1780         case LFUN_CHANGE_ACCEPT:
1781         case LFUN_CHANGE_REJECT:
1782                 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
1783                 // In principle, these LFUNs should only be enabled if there
1784                 // is a change at the current position/in the current selection.
1785                 // However, without proper optimizations, this will inevitably
1786                 // result in unacceptable performance - just imagine a user who
1787                 // wants to select the complete content of a long document.
1788                 enable = true;
1789                 break;
1790
1791         case LFUN_WORD_DELETE_FORWARD:
1792         case LFUN_WORD_DELETE_BACKWARD:
1793         case LFUN_LINE_DELETE:
1794         case LFUN_WORD_FORWARD:
1795         case LFUN_WORD_BACKWARD:
1796         case LFUN_CHAR_FORWARD:
1797         case LFUN_CHAR_FORWARD_SELECT:
1798         case LFUN_CHAR_BACKWARD:
1799         case LFUN_CHAR_BACKWARD_SELECT:
1800         case LFUN_UP:
1801         case LFUN_UP_SELECT:
1802         case LFUN_DOWN:
1803         case LFUN_DOWN_SELECT:
1804         case LFUN_PARAGRAPH_UP_SELECT:
1805         case LFUN_PARAGRAPH_DOWN_SELECT:
1806         case LFUN_SCREEN_UP_SELECT:
1807         case LFUN_SCREEN_DOWN_SELECT:
1808         case LFUN_LINE_BEGIN_SELECT:
1809         case LFUN_LINE_END_SELECT:
1810         case LFUN_WORD_FORWARD_SELECT:
1811         case LFUN_WORD_BACKWARD_SELECT:
1812         case LFUN_WORD_SELECT:
1813         case LFUN_PARAGRAPH_UP:
1814         case LFUN_PARAGRAPH_DOWN:
1815         case LFUN_SCREEN_UP:
1816         case LFUN_SCREEN_DOWN:
1817         case LFUN_LINE_BEGIN:
1818         case LFUN_LINE_END:
1819         case LFUN_BREAK_LINE:
1820         case LFUN_CHAR_DELETE_FORWARD:
1821         case LFUN_DELETE_FORWARD_SKIP:
1822         case LFUN_CHAR_DELETE_BACKWARD:
1823         case LFUN_DELETE_BACKWARD_SKIP:
1824         case LFUN_BREAK_PARAGRAPH:
1825         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1826         case LFUN_BREAK_PARAGRAPH_SKIP:
1827         case LFUN_PARAGRAPH_SPACING:
1828         case LFUN_INSET_INSERT:
1829         case LFUN_WORD_UPCASE:
1830         case LFUN_WORD_LOWCASE:
1831         case LFUN_WORD_CAPITALIZE:
1832         case LFUN_CHARS_TRANSPOSE:
1833         case LFUN_SERVER_GET_XY:
1834         case LFUN_SERVER_SET_XY:
1835         case LFUN_SERVER_GET_FONT:
1836         case LFUN_SERVER_GET_LAYOUT:
1837         case LFUN_LAYOUT:
1838         case LFUN_DATE_INSERT:
1839         case LFUN_SELF_INSERT:
1840         case LFUN_LINE_INSERT:
1841         case LFUN_PAGEBREAK_INSERT:
1842         case LFUN_CLEARPAGE_INSERT:
1843         case LFUN_CLEARDOUBLEPAGE_INSERT:
1844         case LFUN_MATH_DISPLAY:
1845         case LFUN_MATH_IMPORT_SELECTION:
1846         case LFUN_MATH_MODE:
1847         case LFUN_MATH_MACRO:
1848         case LFUN_MATH_MATRIX:
1849         case LFUN_MATH_DELIM:
1850         case LFUN_MATH_BIGDELIM:
1851         case LFUN_MATH_SUBSCRIPT:
1852         case LFUN_MATH_SUPERSCRIPT:
1853         case LFUN_FONT_DEFAULT:
1854         case LFUN_FONT_UNDERLINE:
1855         case LFUN_FONT_SIZE:
1856         case LFUN_LANGUAGE:
1857         case LFUN_FONT_FREE_APPLY:
1858         case LFUN_FONT_FREE_UPDATE:
1859         case LFUN_LAYOUT_PARAGRAPH:
1860         case LFUN_PARAGRAPH_UPDATE:
1861         case LFUN_ACCENT_UMLAUT:
1862         case LFUN_ACCENT_CIRCUMFLEX:
1863         case LFUN_ACCENT_GRAVE:
1864         case LFUN_ACCENT_ACUTE:
1865         case LFUN_ACCENT_TILDE:
1866         case LFUN_ACCENT_CEDILLA:
1867         case LFUN_ACCENT_MACRON:
1868         case LFUN_ACCENT_DOT:
1869         case LFUN_ACCENT_UNDERDOT:
1870         case LFUN_ACCENT_UNDERBAR:
1871         case LFUN_ACCENT_CARON:
1872         case LFUN_ACCENT_SPECIAL_CARON:
1873         case LFUN_ACCENT_BREVE:
1874         case LFUN_ACCENT_TIE:
1875         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1876         case LFUN_ACCENT_CIRCLE:
1877         case LFUN_ACCENT_OGONEK:
1878         case LFUN_THESAURUS_ENTRY:
1879         case LFUN_PARAGRAPH_PARAMS_APPLY:
1880         case LFUN_ESCAPE:
1881         case LFUN_BUFFER_END:
1882         case LFUN_BUFFER_BEGIN:
1883         case LFUN_BUFFER_BEGIN_SELECT:
1884         case LFUN_BUFFER_END_SELECT:
1885         case LFUN_UNICODE_INSERT:
1886                 // these are handled in our dispatch()
1887                 enable = true;
1888                 break;
1889
1890         default:
1891                 return false;
1892         }
1893
1894         if (code != InsetBase::NO_CODE
1895             && (cur.empty() || !cur.inset().insetAllowed(code)))
1896                 enable = false;
1897
1898         flag.enabled(enable);
1899         return true;
1900 }
1901
1902
1903 void LyXText::pasteString(LCursor & cur, docstring const & clip,
1904                 bool asParagraphs)
1905 {
1906         cur.clearSelection();
1907         if (!clip.empty()) {
1908                 recordUndo(cur);
1909                 if (asParagraphs)
1910                         insertStringAsParagraphs(cur, clip);
1911                 else
1912                         insertStringAsLines(cur, clip);
1913         }
1914 }
1915
1916 } // namespace lyx