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