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