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