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