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