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