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