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