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