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