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