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