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