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