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