]> git.lyx.org Git - lyx.git/blob - src/text3.C
fix table crash;
[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.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                                 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 #ifdef WITH_WARNINGS
688 #warning look here
689 #endif
690                         //CursorSlice cur = cursor();
691                         backspace(cur);
692                         //anchor() = cur;
693                 } else {
694                         cutSelection(cur, true, false);
695                 }
696                 break;
697
698         case LFUN_BREAKPARAGRAPH:
699                 lyx::cap::replaceSelection(cur);
700                 breakParagraph(cur, 0);
701                 cur.resetAnchor();
702                 bv->switchKeyMap();
703                 break;
704
705         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
706                 lyx::cap::replaceSelection(cur);
707                 breakParagraph(cur, 1);
708                 cur.resetAnchor();
709                 bv->switchKeyMap();
710                 break;
711
712         case LFUN_BREAKPARAGRAPH_SKIP: {
713                 // When at the beginning of a paragraph, remove
714                 // indentation.  Otherwise, do the same as LFUN_BREAKPARAGRAPH.
715                 lyx::cap::replaceSelection(cur);
716                 if (cur.pos() == 0)
717                         cur.paragraph().params().labelWidthString(string());
718                 else
719                         breakParagraph(cur, 0);
720                 cur.resetAnchor();
721                 bv->switchKeyMap();
722                 break;
723         }
724
725         case LFUN_PARAGRAPH_SPACING: {
726                 Paragraph & par = cur.paragraph();
727                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
728                 float cur_value = 1.0;
729                 if (cur_spacing == Spacing::Other)
730                         cur_value = par.params().spacing().getValue();
731
732                 istringstream is(cmd.argument);
733                 string tmp;
734                 is >> tmp;
735                 Spacing::Space new_spacing = cur_spacing;
736                 float new_value = cur_value;
737                 if (tmp.empty()) {
738                         lyxerr << "Missing argument to `paragraph-spacing'"
739                                << endl;
740                 } else if (tmp == "single") {
741                         new_spacing = Spacing::Single;
742                 } else if (tmp == "onehalf") {
743                         new_spacing = Spacing::Onehalf;
744                 } else if (tmp == "double") {
745                         new_spacing = Spacing::Double;
746                 } else if (tmp == "other") {
747                         new_spacing = Spacing::Other;
748                         float tmpval = 0.0;
749                         is >> tmpval;
750                         lyxerr << "new_value = " << tmpval << endl;
751                         if (tmpval != 0.0)
752                                 new_value = tmpval;
753                 } else if (tmp == "default") {
754                         new_spacing = Spacing::Default;
755                 } else {
756                         lyxerr << _("Unknown spacing argument: ")
757                                << cmd.argument << endl;
758                 }
759                 if (cur_spacing != new_spacing || cur_value != new_value) {
760                         par.params().spacing(Spacing(new_spacing, new_value));
761                         redoParagraph(cur);
762                 }
763                 break;
764         }
765
766         case LFUN_INSET_APPLY: {
767                 string const name = cmd.getArg(0);
768                 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
769                 if (inset) {
770                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
771                         inset->dispatch(cur, fr);
772                 } else {
773                         FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
774                         dispatch(cur, fr);
775                 }
776                 break;
777         }
778
779         case LFUN_INSET_INSERT: {
780                 recordUndo(cur);
781                 InsetBase * inset = createInset(bv, cmd);
782                 if (inset)
783                         insertInset(cur, inset);
784                 break;
785         }
786
787         case LFUN_INSET_SETTINGS:
788                 if (cur.inset().asUpdatableInset())
789                         cur.inset().asUpdatableInset()->showInsetDialog(bv);
790                 break;
791
792         case LFUN_NEXT_INSET_TOGGLE: {
793                 InsetBase * inset = cur.nextInset();
794                 if (inset) {
795                         cur.clearSelection();
796                         FuncRequest fr = cmd;
797                         fr.action = LFUN_INSET_TOGGLE;
798                         inset->dispatch(cur, fr);
799                 }
800                 break;
801         }
802
803         case LFUN_KEYMAP_TOGGLE:
804                 cur.clearSelection();
805                 bv->switchKeyMap();
806                 break;
807
808         case LFUN_SPACE_INSERT:
809                 if (cur.paragraph().layout()->free_spacing)
810                         insertChar(cur, ' ');
811                 else
812                         doInsertInset(cur, this, cmd, false, false);
813                 moveCursor(cur, false);
814                 break;
815
816         case LFUN_HYPHENATION:
817                 specialChar(cur, InsetSpecialChar::HYPHENATION);
818                 break;
819
820         case LFUN_LIGATURE_BREAK:
821                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
822                 break;
823
824         case LFUN_LDOTS:
825                 specialChar(cur, InsetSpecialChar::LDOTS);
826                 break;
827
828         case LFUN_END_OF_SENTENCE:
829                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
830                 break;
831
832         case LFUN_MENU_SEPARATOR:
833                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
834                 break;
835
836         case LFUN_UPCASE_WORD:
837                 changeCase(cur, LyXText::text_uppercase);
838                 break;
839
840         case LFUN_LOWCASE_WORD:
841                 changeCase(cur, LyXText::text_lowercase);
842                 break;
843
844         case LFUN_CAPITALIZE_WORD:
845                 changeCase(cur, LyXText::text_capitalization);
846                 break;
847
848         case LFUN_TRANSPOSE_CHARS:
849                 recordUndo(cur);
850                 redoParagraph(cur);
851                 break;
852
853         case LFUN_PASTE:
854                 cur.message(_("Paste"));
855                 lyx::cap::replaceSelection(cur);
856 #ifdef WITH_WARNINGS
857 #warning FIXME Check if the arg is in the domain of available selections.
858 #endif
859                 if (isStrUnsignedInt(cmd.argument))
860                         pasteSelection(cur, strToUnsignedInt(cmd.argument));
861                 else
862                         pasteSelection(cur, 0);
863                 cur.clearSelection(); // bug 393
864                 bv->switchKeyMap();
865                 finishUndo();
866                 break;
867
868         case LFUN_CUT:
869                 cutSelection(cur, true, true);
870                 cur.message(_("Cut"));
871                 break;
872
873         case LFUN_COPY:
874                 copySelection(cur);
875                 cur.message(_("Copy"));
876                 break;
877
878         case LFUN_GETXY:
879                 cur.message(tostr(cursorX(cur.top())) + ' '
880                           + tostr(cursorY(cur.top())));
881                 break;
882
883         case LFUN_SETXY: {
884                 int x = 0;
885                 int y = 0;
886                 istringstream is(cmd.argument);
887                 is >> x >> y;
888                 if (!is)
889                         lyxerr << "SETXY: Could not parse coordinates in '"
890                                << cmd.argument << std::endl;
891                 else
892                         setCursorFromCoordinates(cur, x, y);
893                 break;
894         }
895
896         case LFUN_GETFONT:
897                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
898                         cur.message("E");
899                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
900                         cur.message("N");
901                 else
902                         cur.message("0");
903                 break;
904
905         case LFUN_GETLAYOUT:
906                 cur.message(cur.paragraph().layout()->name());
907                 break;
908
909         case LFUN_LAYOUT: {
910                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
911                   << cmd.argument << endl;
912
913                 // This is not the good solution to the empty argument
914                 // problem, but it will hopefully suffice for 1.2.0.
915                 // The correct solution would be to augument the
916                 // function list/array with information about what
917                 // functions needs arguments and their type.
918                 if (cmd.argument.empty()) {
919                         cur.errorMessage(_("LyX function 'layout' needs an argument."));
920                         break;
921                 }
922
923                 // Derive layout number from given argument (string)
924                 // and current buffer's textclass (number)
925                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
926                 bool hasLayout = tclass.hasLayout(cmd.argument);
927                 string layout = cmd.argument;
928
929                 // If the entry is obsolete, use the new one instead.
930                 if (hasLayout) {
931                         string const & obs = tclass[layout]->obsoleted_by();
932                         if (!obs.empty())
933                                 layout = obs;
934                 }
935
936                 if (!hasLayout) {
937                         cur.errorMessage(string(N_("Layout ")) + cmd.argument +
938                                 N_(" not known"));
939                         break;
940                 }
941
942                 bool change_layout = (current_layout != layout);
943
944                 if (!change_layout && cur.selection() &&
945                         cur.selBegin().par() != cur.selEnd().par())
946                 {
947                         par_type spit = cur.selBegin().par();
948                         par_type epit = cur.selEnd().par() + 1;
949                         while (spit != epit) {
950                                 if (pars_[spit].layout()->name() != current_layout) {
951                                         change_layout = true;
952                                         break;
953                                 }
954                                 ++spit;
955                         }
956                 }
957
958                 if (change_layout) {
959                         current_layout = layout;
960                         setLayout(cur, layout);
961                         bv->owner()->setLayout(layout);
962                         bv->switchKeyMap();
963                 }
964                 break;
965         }
966
967         case LFUN_PASTESELECTION: {
968                 cur.clearSelection();
969                 string const clip = bv->getClipboard();
970                 if (!clip.empty()) {
971                         if (cmd.argument == "paragraph")
972                                 insertStringAsParagraphs(cur, clip);
973                         else
974                                 insertStringAsLines(cur, clip);
975                 }
976                 break;
977         }
978
979         case LFUN_GOTOERROR:
980                 gotoInset(cur, InsetBase::ERROR_CODE, false);
981                 break;
982
983         case LFUN_GOTONOTE:
984                 gotoInset(cur, InsetBase::NOTE_CODE, false);
985                 break;
986
987         case LFUN_REFERENCE_GOTO: {
988                 vector<InsetOld_code> tmp;
989                 tmp.push_back(InsetBase::LABEL_CODE);
990                 tmp.push_back(InsetBase::REF_CODE);
991                 gotoInset(cur, tmp, true);
992                 break;
993         }
994
995         case LFUN_QUOTE: {
996                 lyx::cap::replaceSelection(cur);
997                 Paragraph & par = cur.paragraph();
998                 lyx::pos_type pos = cur.pos();
999                 char c;
1000                 if (pos == 0)
1001                         c = ' ';
1002                 else if (cur.prevInset() && cur.prevInset()->isSpace())
1003                         c = ' ';
1004                 else
1005                         c = par.getChar(pos - 1);
1006
1007                 LyXLayout_ptr const & style = par.layout();
1008
1009                 BufferParams const & bufparams = bv->buffer()->params();
1010                 if (!style->pass_thru
1011                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1012                         string arg = cmd.argument;
1013                         if (arg == "single")
1014                                 cur.insert(new InsetQuotes(c,
1015                                     bufparams.quotes_language,
1016                                     InsetQuotes::SingleQ));
1017                         else if (arg == "double")
1018                                 cur.insert(new InsetQuotes(c,
1019                                     bufparams.quotes_language,
1020                                     InsetQuotes::DoubleQ));
1021                         else
1022                                 cur.insert(new InsetQuotes(c, bufparams));
1023                 }
1024                 else
1025                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1026                 break;
1027         }
1028
1029         case LFUN_DATE_INSERT: {
1030                 lyx::cap::replaceSelection(cur);
1031                 time_t now_time_t = time(NULL);
1032                 struct tm * now_tm = localtime(&now_time_t);
1033                 setlocale(LC_TIME, "");
1034                 string arg;
1035                 if (!cmd.argument.empty())
1036                         arg = cmd.argument;
1037                 else
1038                         arg = lyxrc.date_insert_format;
1039                 char datetmp[32];
1040                 int const datetmp_len =
1041                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1042
1043                 for (int i = 0; i < datetmp_len; i++)
1044                         insertChar(cur, datetmp[i]);
1045
1046                 cur.resetAnchor();
1047                 moveCursor(cur, false);
1048                 break;
1049         }
1050
1051         case LFUN_MOUSE_TRIPLE:
1052                 if (cmd.button() == mouse_button::button1) {
1053                         selection_possible = true;
1054                         cursorHome(cur);
1055                         cur.resetAnchor();
1056                         cursorEnd(cur);
1057                         cur.setSelection();
1058                         bv->haveSelection(cur.selection());
1059                 }
1060                 break;
1061
1062         case LFUN_MOUSE_DOUBLE:
1063                 if (cmd.button() == mouse_button::button1) {
1064                         selection_possible = true;
1065                         selectWord(cur, lyx::WHOLE_WORD_STRICT);
1066                         bv->haveSelection(cur.selection());
1067                 }
1068                 break;
1069
1070         case LFUN_MOUSE_MOTION: {
1071                 // Only use motion with button 1
1072                 //if (cmd.button() != mouse_button::button1)
1073                 //      return false;
1074                 // We want to use only motion events for which
1075                 // the button press event was on the drawing area too.
1076                 if (!selection_possible) {
1077                         lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1078                                 "dispatch: no selection possible\n";
1079                         lyxerr << "BufferView::Pimpl::dispatch: no selection possible\n";
1080                         break;
1081                 }
1082
1083                 // ignore motions deeper nested than the real anchor
1084                 LCursor & bvcur = cur.bv().cursor();
1085                 if (bvcur.selection() && bvcur.anchor_.size() < cur.size())
1086                         break;
1087
1088                 CursorSlice old = cur.top();
1089                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1090
1091                 // This is to allow jumping over large insets
1092                 // FIXME: shouldn't be top-text-specific
1093                 if (isMainText() && cur.top() == old) {
1094                         if (cmd.y - bv->top_y() >= bv->workHeight())
1095                                 cursorDown(cur);
1096                         else if (cmd.y - bv->top_y() < 0)
1097                                 cursorUp(cur);
1098                 }
1099
1100                 // don't set anchor_
1101                 bv->cursor().setCursor(cur, true);
1102                 lyxerr << "MOTION: " << bv->cursor() << endl;
1103                 break;
1104         }
1105
1106         // Single-click on work area
1107         case LFUN_MOUSE_PRESS: {
1108                 // ok ok, this is a hack (for xforms)
1109                 // We shouldn't go further down as we really need to. Only do the
1110                 // scrolling and be done with this. Otherwise we may open some
1111                 // dialogs (Jug 20020424).
1112                 if (cmd.button() == mouse_button::button4) {
1113                         bv->scroll(-lyxrc.wheel_jump);
1114                         break;
1115                 }
1116
1117                 if (cmd.button() == mouse_button::button5) {
1118                         bv->scroll(lyxrc.wheel_jump);
1119                         break;
1120                 }
1121
1122                 // Right click on a footnote flag opens float menu
1123                 if (cmd.button() == mouse_button::button3) {
1124                         cur.clearSelection();
1125                         selection_possible = false;
1126                         break;
1127                 }
1128
1129                 // Middle button press pastes if we have a selection
1130                 // We do this here as if the selection was inside an inset
1131                 // it could get cleared on the unlocking of the inset so
1132                 // we have to check this first
1133                 bool paste_internally = false;
1134                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1135                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1136                         paste_internally = true;
1137                 }
1138
1139                 selection_possible = true;
1140
1141                 // Clear the selection
1142                 cur.clearSelection();
1143
1144                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
1145                 cur.resetAnchor();
1146                 finishUndo();
1147                 cur.x_target() = cursorX(cur.top());
1148
1149                 // Set cursor here.
1150                 bv->cursor() = cur;
1151
1152                 // Don't allow selection after a big jump.
1153                 if (bv->fitCursor())
1154                         selection_possible = false;
1155
1156                 // Insert primary selection with middle mouse
1157                 // if there is a local selection in the current buffer,
1158                 // insert this
1159                 if (cmd.button() == mouse_button::button2) {
1160                         if (paste_internally)
1161                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1162                         else
1163                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1164                         selection_possible = false;
1165                 }
1166
1167                 break;
1168         }
1169
1170         case LFUN_MOUSE_RELEASE: {
1171                 // do nothing if we used the mouse wheel
1172                 if (cmd.button() == mouse_button::button4
1173                  || cmd.button() == mouse_button::button5) {
1174                         cur.undispatched();
1175                         break;
1176                 }
1177
1178                 selection_possible = false;
1179
1180                 if (cmd.button() == mouse_button::button2)
1181                         break;
1182
1183                 // finish selection
1184                 if (cmd.button() == mouse_button::button1)
1185                         bv->haveSelection(cur.selection());
1186
1187                 bv->switchKeyMap();
1188                 bv->owner()->updateMenubar();
1189                 bv->owner()->updateToolbar();
1190                 break;
1191         }
1192
1193         case LFUN_SELFINSERT: {
1194                 if (cmd.argument.empty())
1195                         break;
1196
1197                 // Automatically delete the currently selected
1198                 // text and replace it with what is being
1199                 // typed in now. Depends on lyxrc settings
1200                 // "auto_region_delete", which defaults to
1201                 // true (on).
1202
1203                 if (lyxrc.auto_region_delete) {
1204                         if (cur.selection())
1205                                 cutSelection(cur, false, false);
1206                         bv->haveSelection(false);
1207                 }
1208
1209                 cur.clearSelection();
1210                 LyXFont const old_font = real_current_font;
1211
1212                 string::const_iterator cit = cmd.argument.begin();
1213                 string::const_iterator end = cmd.argument.end();
1214                 for (; cit != end; ++cit)
1215                         bv->owner()->getIntl().getTransManager().
1216                                 TranslateAndInsert(*cit, this);
1217
1218                 cur.resetAnchor();
1219                 moveCursor(cur, false);
1220
1221                 // real_current_font.number can change so we need to
1222                 // update the minibuffer
1223                 if (old_font != real_current_font)
1224                 bv->updateScrollbar();
1225                 break;
1226         }
1227
1228         case LFUN_URL: {
1229                 InsetCommandParams p("url");
1230                 string const data = InsetCommandMailer::params2string("url", p);
1231                 bv->owner()->getDialogs().show("url", data, 0);
1232                 break;
1233         }
1234
1235         case LFUN_HTMLURL: {
1236                 InsetCommandParams p("htmlurl");
1237                 string const data = InsetCommandMailer::params2string("url", p);
1238                 bv->owner()->getDialogs().show("url", data, 0);
1239                 break;
1240         }
1241
1242         case LFUN_INSERT_LABEL: {
1243                 InsetCommandParams p("label");
1244                 string const data = InsetCommandMailer::params2string("label", p);
1245                 bv->owner()->getDialogs().show("label", data, 0);
1246                 break;
1247         }
1248
1249
1250 #if 0
1251         case LFUN_INSET_LIST:
1252         case LFUN_INSET_THEOREM:
1253         case LFUN_INSET_CAPTION:
1254 #endif
1255         case LFUN_INSERT_NOTE:
1256         case LFUN_INSERT_CHARSTYLE:
1257         case LFUN_INSERT_BOX:
1258         case LFUN_INSERT_BRANCH:
1259         case LFUN_INSERT_BIBITEM:
1260         case LFUN_INSET_ERT:
1261         case LFUN_INSET_FLOAT:
1262         case LFUN_INSET_FOOTNOTE:
1263         case LFUN_INSET_MARGINAL:
1264         case LFUN_INSET_OPTARG:
1265         case LFUN_INSET_WIDE_FLOAT:
1266         case LFUN_INSET_WRAP:
1267         case LFUN_TABULAR_INSERT:
1268         case LFUN_ENVIRONMENT_INSERT:
1269                 // Open the inset, and move the current selection
1270                 // inside it.
1271                 doInsertInset(cur, this, cmd, true, true);
1272                 break;
1273
1274         case LFUN_INDEX_INSERT:
1275                 // Just open the inset
1276                 doInsertInset(cur, this, cmd, true, false);
1277                 break;
1278
1279         case LFUN_INDEX_PRINT:
1280         case LFUN_TOC_INSERT:
1281         case LFUN_HFILL:
1282         case LFUN_INSERT_LINE:
1283         case LFUN_INSERT_PAGEBREAK:
1284                 // do nothing fancy
1285                 doInsertInset(cur, this, cmd, false, false);
1286                 break;
1287
1288         case LFUN_DEPTH_MIN:
1289                 changeDepth(cur, DEC_DEPTH);
1290                 break;
1291
1292         case LFUN_DEPTH_PLUS:
1293                 changeDepth(cur, INC_DEPTH);
1294                 break;
1295
1296         case LFUN_MATH_DISPLAY:
1297                 mathDispatch(cur, cmd, true);
1298                 break;
1299
1300         case LFUN_MATH_IMPORT_SELECTION:
1301         case LFUN_MATH_MODE:
1302                 mathDispatch(cur, cmd, false);
1303                 break;
1304
1305         case LFUN_MATH_MACRO:
1306                 if (cmd.argument.empty())
1307                         cur.errorMessage(N_("Missing argument"));
1308                 else {
1309                         string s = cmd.argument;
1310                         string const s1 = token(s, ' ', 1);
1311                         int const nargs = s1.empty() ? 0 : atoi(s1);
1312                         string const s2 = token(s, ' ', 2);
1313                         string const type = s2.empty() ? "newcommand" : s2;
1314                         cur.insert(new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
1315                         cur.nextInset()->edit(cur, true);
1316                 }
1317                 break;
1318
1319         case LFUN_INSERT_MATH:
1320         case LFUN_INSERT_MATRIX:
1321         case LFUN_MATH_DELIM: {
1322                 cur.insert(new MathHullInset);
1323                 cur.dispatch(FuncRequest(LFUN_RIGHT));
1324                 cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
1325                 cur.dispatch(cmd);
1326                 break;
1327         }
1328
1329         case LFUN_EMPH: {
1330                 LyXFont font(LyXFont::ALL_IGNORE);
1331                 font.setEmph(LyXFont::TOGGLE);
1332                 toggleAndShow(cur, this, font);
1333                 break;
1334         }
1335
1336         case LFUN_BOLD: {
1337                 LyXFont font(LyXFont::ALL_IGNORE);
1338                 font.setSeries(LyXFont::BOLD_SERIES);
1339                 toggleAndShow(cur, this, font);
1340                 break;
1341         }
1342
1343         case LFUN_NOUN: {
1344                 LyXFont font(LyXFont::ALL_IGNORE);
1345                 font.setNoun(LyXFont::TOGGLE);
1346                 toggleAndShow(cur, this, font);
1347                 break;
1348         }
1349
1350         case LFUN_CODE: {
1351                 LyXFont font(LyXFont::ALL_IGNORE);
1352                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1353                 toggleAndShow(cur, this, font);
1354                 break;
1355         }
1356
1357         case LFUN_SANS: {
1358                 LyXFont font(LyXFont::ALL_IGNORE);
1359                 font.setFamily(LyXFont::SANS_FAMILY);
1360                 toggleAndShow(cur, this, font);
1361                 break;
1362         }
1363
1364         case LFUN_ROMAN: {
1365                 LyXFont font(LyXFont::ALL_IGNORE);
1366                 font.setFamily(LyXFont::ROMAN_FAMILY);
1367                 toggleAndShow(cur, this, font);
1368                 break;
1369         }
1370
1371         case LFUN_DEFAULT: {
1372                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1373                 toggleAndShow(cur, this, font);
1374                 break;
1375         }
1376
1377         case LFUN_UNDERLINE: {
1378                 LyXFont font(LyXFont::ALL_IGNORE);
1379                 font.setUnderbar(LyXFont::TOGGLE);
1380                 toggleAndShow(cur, this, font);
1381                 break;
1382         }
1383
1384         case LFUN_FONT_SIZE: {
1385                 LyXFont font(LyXFont::ALL_IGNORE);
1386                 font.setLyXSize(cmd.argument);
1387                 toggleAndShow(cur, this, font);
1388                 break;
1389         }
1390
1391         case LFUN_LANGUAGE: {
1392                 Language const * lang = languages.getLanguage(cmd.argument);
1393                 if (!lang)
1394                         break;
1395                 LyXFont font(LyXFont::ALL_IGNORE);
1396                 font.setLanguage(lang);
1397                 toggleAndShow(cur, this, font);
1398                 bv->switchKeyMap();
1399                 break;
1400         }
1401
1402         case LFUN_FREEFONT_APPLY:
1403                 toggleAndShow(cur, this, freefont, toggleall);
1404                 cur.message(_("Character set"));
1405                 break;
1406
1407         // Set the freefont using the contents of \param data dispatched from
1408         // the frontends and apply it at the current cursor location.
1409         case LFUN_FREEFONT_UPDATE: {
1410                 LyXFont font;
1411                 bool toggle;
1412                 if (bv_funcs::string2font(cmd.argument, font, toggle)) {
1413                         freefont = font;
1414                         toggleall = toggle;
1415                         toggleAndShow(cur, this, freefont, toggleall);
1416                         cur.message(_("Character set"));
1417                 }
1418                 break;
1419         }
1420
1421         case LFUN_FINISHED_LEFT:
1422                 lyxerr << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1423                 break;
1424
1425         case LFUN_FINISHED_RIGHT:
1426                 lyxerr << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1427                 ++cur.pos();
1428                 break;
1429
1430         case LFUN_FINISHED_UP:
1431                 lyxerr << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1432                 cursorUp(cur);
1433                 break;
1434
1435         case LFUN_FINISHED_DOWN:
1436                 lyxerr << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1437                 cursorDown(cur);
1438                 break;
1439
1440         case LFUN_LAYOUT_PARAGRAPH: {
1441                 string data;
1442                 params2string(cur.paragraph(), data);
1443                 data = "show\n" + data;
1444                 bv->owner()->getDialogs().show("paragraph", data);
1445                 break;
1446         }
1447
1448         case LFUN_PARAGRAPH_UPDATE: {
1449                 if (!bv->owner()->getDialogs().visible("paragraph"))
1450                         break;
1451                 string data;
1452                 params2string(cur.paragraph(), data);
1453
1454                 // Will the paragraph accept changes from the dialog?
1455                 InsetBase & inset = cur.inset();
1456                 bool const accept = !inset.forceDefaultParagraphs(&inset);
1457
1458                 data = "update " + tostr(accept) + '\n' + data;
1459                 bv->owner()->getDialogs().update("paragraph", data);
1460                 break;
1461         }
1462
1463         case LFUN_UMLAUT:
1464         case LFUN_CIRCUMFLEX:
1465         case LFUN_GRAVE:
1466         case LFUN_ACUTE:
1467         case LFUN_TILDE:
1468         case LFUN_CEDILLA:
1469         case LFUN_MACRON:
1470         case LFUN_DOT:
1471         case LFUN_UNDERDOT:
1472         case LFUN_UNDERBAR:
1473         case LFUN_CARON:
1474         case LFUN_SPECIAL_CARON:
1475         case LFUN_BREVE:
1476         case LFUN_TIE:
1477         case LFUN_HUNG_UMLAUT:
1478         case LFUN_CIRCLE:
1479         case LFUN_OGONEK:
1480                 bv->owner()->getLyXFunc().handleKeyFunc(cmd.action);
1481                 if (!cmd.argument.empty())
1482                         bv->owner()->getIntl().getTransManager()
1483                                 .TranslateAndInsert(cmd.argument[0], this);
1484                 break;
1485
1486         case LFUN_FLOAT_LIST: {
1487                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1488                 if (tclass.floats().typeExist(cmd.argument)) {
1489                         // not quite sure if we want this...
1490                         recordUndo(cur);
1491                         cur.clearSelection();
1492                         breakParagraph(cur);
1493
1494                         if (cur.lastpos() != 0) {
1495                                 cursorLeft(cur);
1496                                 breakParagraph(cur);
1497                         }
1498
1499                         setLayout(cur, tclass.defaultLayoutName());
1500                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1501                         insertInset(cur, new InsetFloatList(cmd.argument));
1502                 } else {
1503                         lyxerr << "Non-existent float type: "
1504                                << cmd.argument << endl;
1505                 }
1506                 break;
1507         }
1508
1509         case LFUN_ACCEPT_CHANGE: {
1510                 acceptChange(cur);
1511                 break;
1512         }
1513
1514         case LFUN_REJECT_CHANGE: {
1515                 rejectChange(cur);
1516                 break;
1517         }
1518
1519         case LFUN_THESAURUS_ENTRY: {
1520                 string arg = cmd.argument;
1521                 if (arg.empty()) {
1522                         arg = cur.selectionAsString(false);
1523                         // FIXME
1524                         if (arg.size() > 100 || arg.empty()) {
1525                                 // Get word or selection
1526                                 selectWordWhenUnderCursor(cur, lyx::WHOLE_WORD);
1527                                 arg = cur.selectionAsString(false);
1528                         }
1529                 }
1530                 bv->owner()->getDialogs().show("thesaurus", arg);
1531                 break;
1532         }
1533
1534         case LFUN_PARAGRAPH_APPLY: {
1535                 // Given data, an encoding of the ParagraphParameters
1536                 // generated in the Paragraph dialog, this function sets
1537                 // the current paragraph appropriately.
1538                 istringstream is(cmd.argument);
1539                 LyXLex lex(0, 0);
1540                 lex.setStream(is);
1541                 ParagraphParameters params;
1542                 params.read(lex);
1543                 setParagraph(cur,
1544                                          params.spacing(),
1545                                          params.align(),
1546                                          params.labelWidthString(),
1547                                          params.noindent());
1548                 cur.message(_("Paragraph layout set"));
1549                 break;
1550         }
1551
1552         case LFUN_INSET_DIALOG_SHOW: {
1553                 InsetBase * inset = cur.nextInset();
1554                 if (inset) {
1555                         FuncRequest fr(LFUN_INSET_DIALOG_SHOW);
1556                         inset->dispatch(cur, fr);
1557                 }
1558                 break;
1559         }
1560
1561         case LFUN_ESCAPE:
1562                 if (cur.selection()) {
1563                         cur.selection() = false;
1564                 } else {
1565                         cur.undispatched();
1566                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
1567                 }
1568                 break;
1569
1570         default:
1571                 cur.undispatched();
1572                 break;
1573         }
1574 }
1575
1576
1577 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1578         FuncStatus & flag) const
1579 {
1580         BOOST_ASSERT(cur.text() == this);
1581         LyXFont const & font = real_current_font;
1582         bool enable = true;
1583
1584         switch (cmd.action) {
1585
1586         case LFUN_DEPTH_MIN:
1587                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1588                 break;
1589
1590         case LFUN_DEPTH_PLUS:
1591                 enable = changeDepthAllowed(cur, INC_DEPTH);
1592                 break;
1593
1594         case LFUN_INSET_OPTARG:
1595                 enable = cur.paragraph().layout()->optionalargs;
1596                 break;
1597
1598         case LFUN_APPENDIX:
1599                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1600                 break;
1601
1602 #if 0
1603         // the functions which insert insets
1604         InsetOld::Code code = InsetOld::NO_CODE;
1605         switch (cmd.action) {
1606         case LFUN_DIALOG_SHOW_NEW_INSET:
1607                 if (cmd.argument == "bibitem")
1608                         code = InsetOld::BIBITEM_CODE;
1609                 else if (cmd.argument == "bibtex")
1610                         code = InsetOld::BIBTEX_CODE;
1611                 else if (cmd.argument == "box")
1612                         code = InsetOld::BOX_CODE;
1613                 else if (cmd.argument == "branch")
1614                         code = InsetOld::BRANCH_CODE;
1615                 else if (cmd.argument == "citation")
1616                         code = InsetOld::CITE_CODE;
1617                 else if (cmd.argument == "ert")
1618                         code = InsetOld::ERT_CODE;
1619                 else if (cmd.argument == "external")
1620                         code = InsetOld::EXTERNAL_CODE;
1621                 else if (cmd.argument == "float")
1622                         code = InsetOld::FLOAT_CODE;
1623                 else if (cmd.argument == "graphics")
1624                         code = InsetOld::GRAPHICS_CODE;
1625                 else if (cmd.argument == "include")
1626                         code = InsetOld::INCLUDE_CODE;
1627                 else if (cmd.argument == "index")
1628                         code = InsetOld::INDEX_CODE;
1629                 else if (cmd.argument == "label")
1630                         code = InsetOld::LABEL_CODE;
1631                 else if (cmd.argument == "note")
1632                         code = InsetOld::NOTE_CODE;
1633                 else if (cmd.argument == "ref")
1634                         code = InsetOld::REF_CODE;
1635                 else if (cmd.argument == "toc")
1636                         code = InsetOld::TOC_CODE;
1637                 else if (cmd.argument == "url")
1638                         code = InsetOld::URL_CODE;
1639                 else if (cmd.argument == "vspace")
1640                         code = InsetOld::VSPACE_CODE;
1641                 else if (cmd.argument == "wrap")
1642                         code = InsetOld::WRAP_CODE;
1643                 break;
1644
1645         case LFUN_INSET_ERT:
1646                 code = InsetOld::ERT_CODE;
1647                 break;
1648         case LFUN_INSET_FOOTNOTE:
1649                 code = InsetOld::FOOT_CODE;
1650                 break;
1651         case LFUN_TABULAR_INSERT:
1652                 code = InsetOld::TABULAR_CODE;
1653                 break;
1654         case LFUN_INSET_MARGINAL:
1655                 code = InsetOld::MARGIN_CODE;
1656                 break;
1657         case LFUN_INSET_FLOAT:
1658         case LFUN_INSET_WIDE_FLOAT:
1659                 code = InsetOld::FLOAT_CODE;
1660                 break;
1661         case LFUN_INSET_WRAP:
1662                 code = InsetOld::WRAP_CODE;
1663                 break;
1664         case LFUN_FLOAT_LIST:
1665                 code = InsetOld::FLOAT_LIST_CODE;
1666                 break;
1667 #if 0
1668         case LFUN_INSET_LIST:
1669                 code = InsetOld::LIST_CODE;
1670                 break;
1671         case LFUN_INSET_THEOREM:
1672                 code = InsetOld::THEOREM_CODE;
1673                 break;
1674 #endif
1675         case LFUN_INSET_CAPTION:
1676                 code = InsetOld::CAPTION_CODE;
1677                 break;
1678         case LFUN_INSERT_NOTE:
1679                 code = InsetOld::NOTE_CODE;
1680                 break;
1681         case LFUN_INSERT_CHARSTYLE:
1682                 code = InsetOld::CHARSTYLE_CODE;
1683                 if (buf->params().getLyXTextClass().charstyles().empty())
1684                         enable = false;
1685                 break;
1686         case LFUN_INSERT_BOX:
1687                 code = InsetOld::BOX_CODE;
1688                 break;
1689         case LFUN_INSERT_BRANCH:
1690                 code = InsetOld::BRANCH_CODE;
1691                 if (buf->params().branchlist().empty())
1692                         enable = false;
1693                 break;
1694         case LFUN_INSERT_LABEL:
1695                 code = InsetOld::LABEL_CODE;
1696                 break;
1697         case LFUN_INSET_OPTARG:
1698                 code = InsetOld::OPTARG_CODE;
1699                 break;
1700         case LFUN_ENVIRONMENT_INSERT:
1701                 code = InsetOld::BOX_CODE;
1702                 break;
1703         case LFUN_INDEX_INSERT:
1704                 code = InsetOld::INDEX_CODE;
1705                 break;
1706         case LFUN_INDEX_PRINT:
1707                 code = InsetOld::INDEX_PRINT_CODE;
1708                 break;
1709         case LFUN_TOC_INSERT:
1710                 code = InsetOld::TOC_CODE;
1711                 break;
1712         case LFUN_HTMLURL:
1713         case LFUN_URL:
1714                 code = InsetOld::URL_CODE;
1715                 break;
1716         case LFUN_QUOTE:
1717                 // always allow this, since we will inset a raw quote
1718                 // if an inset is not allowed.
1719                 break;
1720         case LFUN_HYPHENATION:
1721         case LFUN_LIGATURE_BREAK:
1722         case LFUN_HFILL:
1723         case LFUN_MENU_SEPARATOR:
1724         case LFUN_LDOTS:
1725         case LFUN_END_OF_SENTENCE:
1726                 code = InsetOld::SPECIALCHAR_CODE;
1727                 break;
1728         case LFUN_SPACE_INSERT:
1729                 // slight hack: we know this is allowed in math mode
1730                 if (cur.inTexted())
1731                         code = InsetOld::SPACE_CODE;
1732                 break;
1733         case LFUN_INSET_DIALOG_SHOW: {
1734                 InsetBase * inset = cur.nextInset();
1735                 enable = inset;
1736                 if (inset) {
1737                         code = inset->lyxCode();
1738                         if (!(code == InsetOld::INCLUDE_CODE
1739                                 || code == InsetOld::BIBTEX_CODE
1740                                 || code == InsetOld::FLOAT_LIST_CODE
1741                                 || code == InsetOld::TOC_CODE))
1742                                 enable = false;
1743                 }
1744                 break;
1745         }
1746         default:
1747                 break;
1748         }
1749
1750         if (code != InsetOld::NO_CODE
1751                         && (cur.empty() || !cur.inset().insetAllowed(code)))
1752                 enable = false;
1753
1754 #endif
1755
1756         case LFUN_DIALOG_SHOW_NEW_INSET:
1757         case LFUN_INSET_ERT:
1758         case LFUN_INSERT_BOX:
1759         case LFUN_INSERT_BRANCH:
1760         case LFUN_ENVIRONMENT_INSERT:
1761         case LFUN_INDEX_INSERT:
1762         case LFUN_INDEX_PRINT:
1763         case LFUN_TOC_INSERT:
1764         case LFUN_HTMLURL:
1765         case LFUN_URL:
1766         case LFUN_QUOTE:
1767         case LFUN_HYPHENATION:
1768         case LFUN_LIGATURE_BREAK:
1769         case LFUN_HFILL:
1770         case LFUN_MENU_SEPARATOR:
1771         case LFUN_LDOTS:
1772         case LFUN_END_OF_SENTENCE:
1773         case LFUN_SPACE_INSERT:
1774         case LFUN_INSET_DIALOG_SHOW:
1775                 break;
1776
1777         case LFUN_EMPH:
1778                 flag.setOnOff(font.emph() == LyXFont::ON);
1779                 break;
1780
1781         case LFUN_NOUN:
1782                 flag.setOnOff(font.noun() == LyXFont::ON);
1783                 break;
1784
1785         case LFUN_BOLD:
1786                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1787                 break;
1788
1789         case LFUN_SANS:
1790                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1791                 break;
1792
1793         case LFUN_ROMAN:
1794                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1795                 break;
1796
1797         case LFUN_CODE:
1798                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1799                 break;
1800
1801         case LFUN_DELETE_WORD_FORWARD:
1802         case LFUN_DELETE_WORD_BACKWARD:
1803         case LFUN_DELETE_LINE_FORWARD:
1804         case LFUN_WORDRIGHT:
1805         case LFUN_WORDLEFT:
1806         case LFUN_ENDBUF:
1807         case LFUN_BEGINNINGBUF:
1808         case LFUN_RIGHT:
1809         case LFUN_RIGHTSEL:
1810         case LFUN_LEFT:
1811         case LFUN_LEFTSEL:
1812         case LFUN_UP:
1813         case LFUN_UPSEL:
1814         case LFUN_DOWN:
1815         case LFUN_DOWNSEL:
1816         case LFUN_UP_PARAGRAPHSEL:
1817         case LFUN_DOWN_PARAGRAPHSEL:
1818         case LFUN_PRIORSEL:
1819         case LFUN_NEXTSEL:
1820         case LFUN_HOMESEL:
1821         case LFUN_ENDSEL:
1822         case LFUN_WORDRIGHTSEL:
1823         case LFUN_WORDLEFTSEL:
1824         case LFUN_WORDSEL:
1825         case LFUN_UP_PARAGRAPH:
1826         case LFUN_DOWN_PARAGRAPH:
1827         case LFUN_PRIOR:
1828         case LFUN_NEXT:
1829         case LFUN_HOME:
1830         case LFUN_END:
1831         case LFUN_BREAKLINE:
1832         case LFUN_DELETE:
1833         case LFUN_DELETE_SKIP:
1834         case LFUN_BACKSPACE:
1835         case LFUN_BACKSPACE_SKIP:
1836         case LFUN_BREAKPARAGRAPH:
1837         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1838         case LFUN_BREAKPARAGRAPH_SKIP:
1839         case LFUN_PARAGRAPH_SPACING:
1840         case LFUN_INSET_APPLY:
1841         case LFUN_INSET_INSERT:
1842         case LFUN_NEXT_INSET_TOGGLE:
1843         case LFUN_UPCASE_WORD:
1844         case LFUN_LOWCASE_WORD:
1845         case LFUN_CAPITALIZE_WORD:
1846         case LFUN_TRANSPOSE_CHARS:
1847         case LFUN_PASTE:
1848         case LFUN_CUT:
1849         case LFUN_COPY:
1850         case LFUN_GETXY:
1851         case LFUN_SETXY:
1852         case LFUN_GETFONT:
1853         case LFUN_GETLAYOUT:
1854         case LFUN_LAYOUT:
1855         case LFUN_PASTESELECTION:
1856         case LFUN_GOTOERROR:
1857         case LFUN_GOTONOTE:
1858         case LFUN_REFERENCE_GOTO:
1859         case LFUN_DATE_INSERT:
1860         case LFUN_SELFINSERT:
1861         case LFUN_INSERT_LABEL:
1862         case LFUN_INSERT_NOTE:
1863         case LFUN_INSERT_CHARSTYLE:
1864         case LFUN_INSERT_BIBITEM:
1865         case LFUN_INSET_FLOAT:
1866         case LFUN_INSET_FOOTNOTE:
1867         case LFUN_INSET_MARGINAL:
1868         case LFUN_INSET_WIDE_FLOAT:
1869         case LFUN_INSET_WRAP:
1870         case LFUN_TABULAR_INSERT:
1871         case LFUN_INSERT_LINE:
1872         case LFUN_INSERT_PAGEBREAK:
1873         case LFUN_MATH_DISPLAY:
1874         case LFUN_MATH_IMPORT_SELECTION:
1875         case LFUN_MATH_MODE:
1876         case LFUN_MATH_MACRO:
1877         case LFUN_INSERT_MATH:
1878         case LFUN_INSERT_MATRIX:
1879         case LFUN_MATH_DELIM:
1880         case LFUN_DEFAULT:
1881         case LFUN_UNDERLINE:
1882         case LFUN_FONT_SIZE:
1883         case LFUN_LANGUAGE:
1884         case LFUN_FREEFONT_APPLY:
1885         case LFUN_FREEFONT_UPDATE:
1886         case LFUN_LAYOUT_PARAGRAPH:
1887         case LFUN_PARAGRAPH_UPDATE:
1888         case LFUN_UMLAUT:
1889         case LFUN_CIRCUMFLEX:
1890         case LFUN_GRAVE:
1891         case LFUN_ACUTE:
1892         case LFUN_TILDE:
1893         case LFUN_CEDILLA:
1894         case LFUN_MACRON:
1895         case LFUN_DOT:
1896         case LFUN_UNDERDOT:
1897         case LFUN_UNDERBAR:
1898         case LFUN_CARON:
1899         case LFUN_SPECIAL_CARON:
1900         case LFUN_BREVE:
1901         case LFUN_TIE:
1902         case LFUN_HUNG_UMLAUT:
1903         case LFUN_CIRCLE:
1904         case LFUN_OGONEK:
1905         case LFUN_FLOAT_LIST:
1906         case LFUN_ACCEPT_CHANGE:
1907         case LFUN_REJECT_CHANGE:
1908         case LFUN_THESAURUS_ENTRY:
1909         case LFUN_PARAGRAPH_APPLY:
1910         case LFUN_ESCAPE:
1911         case LFUN_KEYMAP_TOGGLE:
1912                 // these are handled in our dispatch()
1913                 enable = true;
1914                 break;
1915
1916         default:
1917                 enable = false;
1918                 break;
1919         }
1920         flag.enabled(enable);
1921         return true;
1922 }