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