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