]> git.lyx.org Git - lyx.git/blob - src/text3.C
fix wheel handling for xforms
[lyx.git] / src / text3.C
1 /**
2  * \file text3.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "lyxtext.h"
19
20 #include "FloatList.h"
21 #include "FuncStatus.h"
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "cursor.h"
26 #include "CutAndPaste.h"
27 #include "debug.h"
28 #include "dispatchresult.h"
29 #include "factory.h"
30 #include "funcrequest.h"
31 #include "gettext.h"
32 #include "intl.h"
33 #include "language.h"
34 #include "lyxfunc.h"
35 #include "lyxlex.h"
36 #include "lyxrc.h"
37 #include "lyxrow.h"
38 #include "paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
41 #include "undo.h"
42 #include "vspace.h"
43
44 #include "frontends/Dialogs.h"
45 #include "frontends/LyXView.h"
46
47 #include "insets/insetcommand.h"
48 #include "insets/insetfloatlist.h"
49 #include "insets/insetnewline.h"
50 #include "insets/insetquotes.h"
51 #include "insets/insetspecialchar.h"
52 #include "insets/insettext.h"
53
54 #include "support/lstrings.h"
55 #include "support/lyxlib.h"
56 #include "support/tostr.h"
57
58 #include "mathed/math_hullinset.h"
59 #include "mathed/math_macrotemplate.h"
60
61 #include <clocale>
62 #include <sstream>
63
64 using lyx::pos_type;
65
66 using lyx::cap::copySelection;
67 using lyx::cap::cutSelection;
68 using lyx::cap::pasteSelection;
69 using lyx::cap::replaceSelection;
70
71 using lyx::support::isStrUnsignedInt;
72 using lyx::support::strToUnsignedInt;
73 using lyx::support::atoi;
74 using lyx::support::token;
75
76 using std::endl;
77 using std::find;
78 using std::string;
79 using std::istringstream;
80 using std::vector;
81
82
83 extern string current_layout;
84
85 // the selection possible is needed, that only motion events are
86 // used, where the button press event was on the drawing area too
87 bool selection_possible = false;
88
89
90 namespace {
91
92         // globals...
93         LyXFont freefont(LyXFont::ALL_IGNORE);
94         bool toggleall = false;
95
96
97         void toggleAndShow(LCursor & cur, LyXText * text,
98                 LyXFont const & font, bool toggleall = true)
99         {
100                 text->toggleFree(cur, font, toggleall);
101
102                 if (font.language() != ignore_language ||
103                                 font.number() != LyXFont::IGNORE) {
104                         Paragraph & par = cur.paragraph();
105                         text->bidi.computeTables(par, cur.buffer(), cur.textRow());
106                         if (cur.boundary() !=
107                                         text->bidi.isBoundary(cur.buffer(), par,
108                                                         cur.pos(),
109                                                         text->real_current_font))
110                                 text->setCursor(cur, cur.par(), cur.pos(),
111                                                 false, !cur.boundary());
112                 }
113         }
114
115
116         void moveCursor(LCursor & cur, bool selecting)
117         {
118                 if (selecting || cur.mark())
119                         cur.setSelection();
120                 if (!cur.selection())
121                         cur.bv().haveSelection(false);
122                 cur.bv().switchKeyMap();
123         }
124
125
126         void finishChange(LCursor & cur, bool selecting)
127         {
128                 finishUndo();
129                 moveCursor(cur, selecting);
130         }
131
132
133         void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
134         {
135                 recordUndo(cur);
136                 string sel = cur.selectionAsString(false);
137                 lyxerr << "selection is: '" << sel << "'" << endl;
138
139                 if (sel.empty()) {
140                         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                 // Right click on a footnote flag opens float menu
1122                 if (cmd.button() == mouse_button::button3) {
1123                         cur.clearSelection();
1124                         selection_possible = false;
1125                         break;
1126                 }
1127
1128                 // Middle button press pastes if we have a selection
1129                 // We do this here as if the selection was inside an inset
1130                 // it could get cleared on the unlocking of the inset so
1131                 // we have to check this first
1132                 bool paste_internally = false;
1133                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1134                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1135                         paste_internally = true;
1136                 }
1137
1138                 selection_possible = true;
1139
1140                 // Clear the selection
1141                 cur.clearSelection();
1142
1143                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1144                 cur.resetAnchor();
1145                 finishUndo();
1146                 cur.x_target() = cursorX(cur.top());
1147
1148                 // Has the cursor just left the inset?
1149                 if (bv->cursor().inMathed() && !cur.inMathed())
1150                         bv->cursor().inset().notifyCursorLeaves(bv->cursor());
1151
1152                 // Set cursor here.
1153                 bv->cursor() = cur;
1154
1155                 // Don't allow selection after a big jump.
1156                 if (bv->fitCursor())
1157                         selection_possible = false;
1158
1159                 // Insert primary selection with middle mouse
1160                 // if there is a local selection in the current buffer,
1161                 // insert this
1162                 if (cmd.button() == mouse_button::button2) {
1163                         if (paste_internally)
1164                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1165                         else
1166                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1167                         selection_possible = false;
1168                 }
1169
1170                 break;
1171         }
1172
1173         case LFUN_MOUSE_RELEASE: {
1174                 selection_possible = false;
1175
1176                 if (cmd.button() == mouse_button::button2)
1177                         break;
1178
1179                 // finish selection
1180                 if (cmd.button() == mouse_button::button1)
1181                         bv->haveSelection(cur.selection());
1182
1183                 bv->switchKeyMap();
1184                 bv->owner()->updateMenubar();
1185                 bv->owner()->updateToolbars();
1186                 break;
1187         }
1188
1189         case LFUN_SELFINSERT: {
1190                 if (cmd.argument.empty())
1191                         break;
1192
1193                 // Automatically delete the currently selected
1194                 // text and replace it with what is being
1195                 // typed in now. Depends on lyxrc settings
1196                 // "auto_region_delete", which defaults to
1197                 // true (on).
1198
1199                 if (lyxrc.auto_region_delete) {
1200                         if (cur.selection())
1201                                 cutSelection(cur, false, false);
1202                         bv->haveSelection(false);
1203                 }
1204
1205                 cur.clearSelection();
1206                 LyXFont const old_font = real_current_font;
1207
1208                 string::const_iterator cit = cmd.argument.begin();
1209                 string::const_iterator end = cmd.argument.end();
1210                 for (; cit != end; ++cit)
1211                         bv->owner()->getIntl().getTransManager().
1212                                 TranslateAndInsert(*cit, this);
1213
1214                 cur.resetAnchor();
1215                 moveCursor(cur, false);
1216
1217                 // real_current_font.number can change so we need to
1218                 // update the minibuffer
1219                 if (old_font != real_current_font)
1220                 bv->updateScrollbar();
1221                 break;
1222         }
1223
1224         case LFUN_URL: {
1225                 InsetCommandParams p("url");
1226                 string const data = InsetCommandMailer::params2string("url", p);
1227                 bv->owner()->getDialogs().show("url", data, 0);
1228                 break;
1229         }
1230
1231         case LFUN_HTMLURL: {
1232                 InsetCommandParams p("htmlurl");
1233                 string const data = InsetCommandMailer::params2string("url", p);
1234                 bv->owner()->getDialogs().show("url", data, 0);
1235                 break;
1236         }
1237
1238         case LFUN_INSERT_LABEL: {
1239                 // Try to generate a valid label
1240                 string const contents = cmd.argument.empty() ?
1241                         cur.getPossibleLabel() : cmd.argument;
1242
1243                 InsetCommandParams p("label", contents);
1244                 string const data = InsetCommandMailer::params2string("label", p);
1245
1246                 if (cmd.argument.empty()) {
1247                         bv->owner()->getDialogs().show("label", data, 0);
1248                 } else {
1249                         FuncRequest fr(LFUN_INSET_INSERT, data);
1250                         dispatch(cur, fr);
1251                 }
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         // avoid to update when navigating
1582         if (moving
1583             && &sl.inset() == &cur.inset()
1584             && sl.idx() == cur.idx()
1585             && sel == false
1586             && cur.selection() == false)
1587                 cur.noUpdate();
1588 }
1589
1590
1591 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1592         FuncStatus & flag) const
1593 {
1594         BOOST_ASSERT(cur.text() == this);
1595         LyXFont const & font = real_current_font;
1596         bool enable = true;
1597
1598         switch (cmd.action) {
1599
1600         case LFUN_DEPTH_MIN:
1601                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1602                 break;
1603
1604         case LFUN_DEPTH_PLUS:
1605                 enable = changeDepthAllowed(cur, INC_DEPTH);
1606                 break;
1607
1608         case LFUN_INSET_OPTARG:
1609                 enable = cur.paragraph().layout()->optionalargs;
1610                 break;
1611
1612         case LFUN_APPENDIX:
1613                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1614                 break;
1615
1616 #if 0
1617         // the functions which insert insets
1618         InsetOld::Code code = InsetOld::NO_CODE;
1619         switch (cmd.action) {
1620         case LFUN_DIALOG_SHOW_NEW_INSET:
1621                 if (cmd.argument == "bibitem")
1622                         code = InsetOld::BIBITEM_CODE;
1623                 else if (cmd.argument == "bibtex")
1624                         code = InsetOld::BIBTEX_CODE;
1625                 else if (cmd.argument == "box")
1626                         code = InsetOld::BOX_CODE;
1627                 else if (cmd.argument == "branch")
1628                         code = InsetOld::BRANCH_CODE;
1629                 else if (cmd.argument == "citation")
1630                         code = InsetOld::CITE_CODE;
1631                 else if (cmd.argument == "ert")
1632                         code = InsetOld::ERT_CODE;
1633                 else if (cmd.argument == "external")
1634                         code = InsetOld::EXTERNAL_CODE;
1635                 else if (cmd.argument == "float")
1636                         code = InsetOld::FLOAT_CODE;
1637                 else if (cmd.argument == "graphics")
1638                         code = InsetOld::GRAPHICS_CODE;
1639                 else if (cmd.argument == "include")
1640                         code = InsetOld::INCLUDE_CODE;
1641                 else if (cmd.argument == "index")
1642                         code = InsetOld::INDEX_CODE;
1643                 else if (cmd.argument == "label")
1644                         code = InsetOld::LABEL_CODE;
1645                 else if (cmd.argument == "note")
1646                         code = InsetOld::NOTE_CODE;
1647                 else if (cmd.argument == "ref")
1648                         code = InsetOld::REF_CODE;
1649                 else if (cmd.argument == "toc")
1650                         code = InsetOld::TOC_CODE;
1651                 else if (cmd.argument == "url")
1652                         code = InsetOld::URL_CODE;
1653                 else if (cmd.argument == "vspace")
1654                         code = InsetOld::VSPACE_CODE;
1655                 else if (cmd.argument == "wrap")
1656                         code = InsetOld::WRAP_CODE;
1657                 break;
1658
1659         case LFUN_INSET_ERT:
1660                 code = InsetOld::ERT_CODE;
1661                 break;
1662         case LFUN_INSET_FOOTNOTE:
1663                 code = InsetOld::FOOT_CODE;
1664                 break;
1665         case LFUN_TABULAR_INSERT:
1666                 code = InsetOld::TABULAR_CODE;
1667                 break;
1668         case LFUN_INSET_MARGINAL:
1669                 code = InsetOld::MARGIN_CODE;
1670                 break;
1671         case LFUN_INSET_FLOAT:
1672         case LFUN_INSET_WIDE_FLOAT:
1673                 code = InsetOld::FLOAT_CODE;
1674                 break;
1675         case LFUN_INSET_WRAP:
1676                 code = InsetOld::WRAP_CODE;
1677                 break;
1678         case LFUN_FLOAT_LIST:
1679                 code = InsetOld::FLOAT_LIST_CODE;
1680                 break;
1681 #if 0
1682         case LFUN_INSET_LIST:
1683                 code = InsetOld::LIST_CODE;
1684                 break;
1685         case LFUN_INSET_THEOREM:
1686                 code = InsetOld::THEOREM_CODE;
1687                 break;
1688 #endif
1689         case LFUN_INSET_CAPTION:
1690                 code = InsetOld::CAPTION_CODE;
1691                 break;
1692         case LFUN_INSERT_NOTE:
1693                 code = InsetOld::NOTE_CODE;
1694                 break;
1695         case LFUN_INSERT_CHARSTYLE:
1696                 code = InsetOld::CHARSTYLE_CODE;
1697                 if (buf->params().getLyXTextClass().charstyles().empty())
1698                         enable = false;
1699                 break;
1700         case LFUN_INSERT_BOX:
1701                 code = InsetOld::BOX_CODE;
1702                 break;
1703         case LFUN_INSERT_BRANCH:
1704                 code = InsetOld::BRANCH_CODE;
1705                 if (buf->params().branchlist().empty())
1706                         enable = false;
1707                 break;
1708         case LFUN_INSERT_LABEL:
1709                 code = InsetOld::LABEL_CODE;
1710                 break;
1711         case LFUN_INSET_OPTARG:
1712                 code = InsetOld::OPTARG_CODE;
1713                 break;
1714         case LFUN_ENVIRONMENT_INSERT:
1715                 code = InsetOld::BOX_CODE;
1716                 break;
1717         case LFUN_INDEX_INSERT:
1718                 code = InsetOld::INDEX_CODE;
1719                 break;
1720         case LFUN_INDEX_PRINT:
1721                 code = InsetOld::INDEX_PRINT_CODE;
1722                 break;
1723         case LFUN_TOC_INSERT:
1724                 code = InsetOld::TOC_CODE;
1725                 break;
1726         case LFUN_HTMLURL:
1727         case LFUN_URL:
1728                 code = InsetOld::URL_CODE;
1729                 break;
1730         case LFUN_QUOTE:
1731                 // always allow this, since we will inset a raw quote
1732                 // if an inset is not allowed.
1733                 break;
1734         case LFUN_HYPHENATION:
1735         case LFUN_LIGATURE_BREAK:
1736         case LFUN_HFILL:
1737         case LFUN_MENU_SEPARATOR:
1738         case LFUN_LDOTS:
1739         case LFUN_END_OF_SENTENCE:
1740                 code = InsetOld::SPECIALCHAR_CODE;
1741                 break;
1742         case LFUN_SPACE_INSERT:
1743                 // slight hack: we know this is allowed in math mode
1744                 if (cur.inTexted())
1745                         code = InsetOld::SPACE_CODE;
1746                 break;
1747         case LFUN_INSET_DIALOG_SHOW: {
1748                 InsetBase * inset = cur.nextInset();
1749                 enable = inset;
1750                 if (inset) {
1751                         code = inset->lyxCode();
1752                         if (!(code == InsetOld::INCLUDE_CODE
1753                                 || code == InsetOld::BIBTEX_CODE
1754                                 || code == InsetOld::FLOAT_LIST_CODE
1755                                 || code == InsetOld::TOC_CODE))
1756                                 enable = false;
1757                 }
1758                 break;
1759         }
1760         default:
1761                 break;
1762         }
1763
1764         if (code != InsetOld::NO_CODE
1765                         && (cur.empty() || !cur.inset().insetAllowed(code)))
1766                 enable = false;
1767
1768 #endif
1769
1770         case LFUN_DIALOG_SHOW_NEW_INSET:
1771         case LFUN_INSET_ERT:
1772         case LFUN_INSERT_BOX:
1773         case LFUN_INSERT_BRANCH:
1774         case LFUN_ENVIRONMENT_INSERT:
1775         case LFUN_INDEX_INSERT:
1776         case LFUN_INDEX_PRINT:
1777         case LFUN_TOC_INSERT:
1778         case LFUN_HTMLURL:
1779         case LFUN_URL:
1780         case LFUN_QUOTE:
1781         case LFUN_HYPHENATION:
1782         case LFUN_LIGATURE_BREAK:
1783         case LFUN_HFILL:
1784         case LFUN_MENU_SEPARATOR:
1785         case LFUN_LDOTS:
1786         case LFUN_END_OF_SENTENCE:
1787         case LFUN_SPACE_INSERT:
1788         case LFUN_INSET_DIALOG_SHOW:
1789                 break;
1790
1791         case LFUN_EMPH:
1792                 flag.setOnOff(font.emph() == LyXFont::ON);
1793                 break;
1794
1795         case LFUN_NOUN:
1796                 flag.setOnOff(font.noun() == LyXFont::ON);
1797                 break;
1798
1799         case LFUN_BOLD:
1800                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1801                 break;
1802
1803         case LFUN_SANS:
1804                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1805                 break;
1806
1807         case LFUN_ROMAN:
1808                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1809                 break;
1810
1811         case LFUN_CODE:
1812                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1813                 break;
1814
1815         case LFUN_DELETE_WORD_FORWARD:
1816         case LFUN_DELETE_WORD_BACKWARD:
1817         case LFUN_DELETE_LINE_FORWARD:
1818         case LFUN_WORDRIGHT:
1819         case LFUN_WORDLEFT:
1820         case LFUN_ENDBUF:
1821         case LFUN_BEGINNINGBUF:
1822         case LFUN_RIGHT:
1823         case LFUN_RIGHTSEL:
1824         case LFUN_LEFT:
1825         case LFUN_LEFTSEL:
1826         case LFUN_UP:
1827         case LFUN_UPSEL:
1828         case LFUN_DOWN:
1829         case LFUN_DOWNSEL:
1830         case LFUN_UP_PARAGRAPHSEL:
1831         case LFUN_DOWN_PARAGRAPHSEL:
1832         case LFUN_PRIORSEL:
1833         case LFUN_NEXTSEL:
1834         case LFUN_HOMESEL:
1835         case LFUN_ENDSEL:
1836         case LFUN_WORDRIGHTSEL:
1837         case LFUN_WORDLEFTSEL:
1838         case LFUN_WORDSEL:
1839         case LFUN_UP_PARAGRAPH:
1840         case LFUN_DOWN_PARAGRAPH:
1841         case LFUN_PRIOR:
1842         case LFUN_NEXT:
1843         case LFUN_HOME:
1844         case LFUN_END:
1845         case LFUN_BREAKLINE:
1846         case LFUN_DELETE:
1847         case LFUN_DELETE_SKIP:
1848         case LFUN_BACKSPACE:
1849         case LFUN_BACKSPACE_SKIP:
1850         case LFUN_BREAKPARAGRAPH:
1851         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1852         case LFUN_BREAKPARAGRAPH_SKIP:
1853         case LFUN_PARAGRAPH_SPACING:
1854         case LFUN_INSET_APPLY:
1855         case LFUN_INSET_INSERT:
1856         case LFUN_NEXT_INSET_TOGGLE:
1857         case LFUN_UPCASE_WORD:
1858         case LFUN_LOWCASE_WORD:
1859         case LFUN_CAPITALIZE_WORD:
1860         case LFUN_TRANSPOSE_CHARS:
1861         case LFUN_PASTE:
1862         case LFUN_CUT:
1863         case LFUN_COPY:
1864         case LFUN_GETXY:
1865         case LFUN_SETXY:
1866         case LFUN_GETFONT:
1867         case LFUN_GETLAYOUT:
1868         case LFUN_LAYOUT:
1869         case LFUN_PASTESELECTION:
1870         case LFUN_GOTOERROR:
1871         case LFUN_GOTONOTE:
1872         case LFUN_REFERENCE_GOTO:
1873         case LFUN_DATE_INSERT:
1874         case LFUN_SELFINSERT:
1875         case LFUN_INSERT_LABEL:
1876         case LFUN_INSERT_NOTE:
1877         case LFUN_INSERT_CHARSTYLE:
1878         case LFUN_INSERT_BIBITEM:
1879         case LFUN_INSET_FLOAT:
1880         case LFUN_INSET_FOOTNOTE:
1881         case LFUN_INSET_MARGINAL:
1882         case LFUN_INSET_WIDE_FLOAT:
1883         case LFUN_INSET_WRAP:
1884         case LFUN_TABULAR_INSERT:
1885         case LFUN_INSERT_LINE:
1886         case LFUN_INSERT_PAGEBREAK:
1887         case LFUN_MATH_DISPLAY:
1888         case LFUN_MATH_IMPORT_SELECTION:
1889         case LFUN_MATH_MODE:
1890         case LFUN_MATH_MACRO:
1891         case LFUN_INSERT_MATH:
1892         case LFUN_INSERT_MATRIX:
1893         case LFUN_MATH_DELIM:
1894         case LFUN_DEFAULT:
1895         case LFUN_UNDERLINE:
1896         case LFUN_FONT_SIZE:
1897         case LFUN_LANGUAGE:
1898         case LFUN_FREEFONT_APPLY:
1899         case LFUN_FREEFONT_UPDATE:
1900         case LFUN_LAYOUT_PARAGRAPH:
1901         case LFUN_PARAGRAPH_UPDATE:
1902         case LFUN_UMLAUT:
1903         case LFUN_CIRCUMFLEX:
1904         case LFUN_GRAVE:
1905         case LFUN_ACUTE:
1906         case LFUN_TILDE:
1907         case LFUN_CEDILLA:
1908         case LFUN_MACRON:
1909         case LFUN_DOT:
1910         case LFUN_UNDERDOT:
1911         case LFUN_UNDERBAR:
1912         case LFUN_CARON:
1913         case LFUN_SPECIAL_CARON:
1914         case LFUN_BREVE:
1915         case LFUN_TIE:
1916         case LFUN_HUNG_UMLAUT:
1917         case LFUN_CIRCLE:
1918         case LFUN_OGONEK:
1919         case LFUN_FLOAT_LIST:
1920         case LFUN_ACCEPT_CHANGE:
1921         case LFUN_REJECT_CHANGE:
1922         case LFUN_THESAURUS_ENTRY:
1923         case LFUN_PARAGRAPH_APPLY:
1924         case LFUN_ESCAPE:
1925         case LFUN_KEYMAP_TOGGLE:
1926                 // these are handled in our dispatch()
1927                 enable = true;
1928                 break;
1929
1930         default:
1931                 enable = false;
1932                 break;
1933         }
1934         flag.enabled(enable);
1935         return true;
1936 }