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