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