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