]> git.lyx.org Git - lyx.git/blob - src/text3.C
* Painter.h:
[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 "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
23 #include "buffer.h"
24 #include "buffer_funcs.h"
25 #include "bufferparams.h"
26 #include "BufferView.h"
27 #include "cursor.h"
28 #include "coordcache.h"
29 #include "CutAndPaste.h"
30 #include "debug.h"
31 #include "dispatchresult.h"
32 #include "errorlist.h"
33 #include "factory.h"
34 #include "funcrequest.h"
35 #include "gettext.h"
36 #include "intl.h"
37 #include "language.h"
38 #include "LyXAction.h"
39 #include "lyxfunc.h"
40 #include "lyxlex.h"
41 #include "lyxrc.h"
42 #include "lyxrow.h"
43 #include "paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "undo.h"
47 #include "vspace.h"
48 #include "pariterator.h"
49
50 #include "frontends/Clipboard.h"
51 #include "frontends/Selection.h"
52
53 #include "insets/insetcommand.h"
54 #include "insets/insetfloatlist.h"
55 #include "insets/insetnewline.h"
56 #include "insets/insetquotes.h"
57 #include "insets/insetspecialchar.h"
58 #include "insets/insettext.h"
59
60 #include "support/lstrings.h"
61 #include "support/lyxlib.h"
62 #include "support/convert.h"
63 #include "support/lyxtime.h"
64
65 #include "mathed/InsetMathHull.h"
66 #include "mathed/MathMacroTemplate.h"
67
68 #include <boost/current_function.hpp>
69
70 #include <clocale>
71 #include <sstream>
72
73
74 namespace lyx {
75
76 using cap::copySelection;
77 using cap::cutSelection;
78 using cap::pasteSelection;
79 using cap::replaceSelection;
80
81 using support::isStrUnsignedInt;
82 using support::token;
83
84 using std::endl;
85 using std::string;
86 using std::istringstream;
87 using std::ostringstream;
88
89
90 extern string current_layout;
91
92
93 namespace {
94
95         // globals...
96         LyXFont freefont(LyXFont::ALL_IGNORE);
97         bool toggleall = false;
98
99
100         void toggleAndShow(LCursor & cur, LyXText * text,
101                 LyXFont const & font, bool toggleall = true)
102         {
103                 text->toggleFree(cur, font, toggleall);
104
105                 if (font.language() != ignore_language ||
106                                 font.number() != LyXFont::IGNORE) {
107                         Paragraph & par = cur.paragraph();
108                         text->bidi.computeTables(par, cur.buffer(), cur.textRow());
109                         if (cur.boundary() !=
110                                         text->bidi.isBoundary(cur.buffer(), par,
111                                                         cur.pos(),
112                                                         text->real_current_font))
113                                 text->setCursor(cur, cur.pit(), cur.pos(),
114                                                 false, !cur.boundary());
115                 }
116         }
117
118
119         void moveCursor(LCursor & cur, bool selecting)
120         {
121                 if (selecting || cur.mark())
122                         cur.setSelection();
123                 if (!cur.selection())
124                         theSelection().haveSelection(false);
125                 cur.bv().switchKeyMap();
126         }
127
128
129         void finishChange(LCursor & cur, bool selecting)
130         {
131                 finishUndo();
132                 moveCursor(cur, selecting);
133         }
134
135
136         void mathDispatch(LCursor & cur, FuncRequest const & cmd, bool display)
137         {
138                 recordUndo(cur);
139                 docstring sel = cur.selectionAsString(false);
140                 //lyxerr << "selection is: '" << sel << "'" << endl;
141
142                 // It may happen that sel is empty but there is a selection
143                 replaceSelection(cur);
144
145                 if (sel.empty()) {
146                         const int old_pos = cur.pos();
147                         cur.insert(new InsetMathHull(hullSimple));
148                         BOOST_ASSERT(old_pos == cur.pos());
149                         cur.nextInset()->edit(cur, true);
150                         // don't do that also for LFUN_MATH_MODE
151                         // unless you want end up with always changing
152                         // to mathrm when opening an inlined inset --
153                         // I really hate "LyXfunc overloading"...
154                         if (display)
155                                 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
156                         // Avoid an unnecessary undo step if cmd.argument
157                         // is empty
158                         if (!cmd.argument().empty())
159                                 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
160                                                          cmd.argument()));
161                 } else {
162                         // create a macro if we see "\\newcommand"
163                         // somewhere, and an ordinary formula
164                         // otherwise
165                         if (sel.find(from_ascii("\\newcommand")) == string::npos
166                             && sel.find(from_ascii("\\def")) == string::npos)
167                         {
168                                 InsetMathHull * formula = new InsetMathHull;
169                                 istringstream is(to_utf8(sel));
170                                 LyXLex lex(0, 0);
171                                 lex.setStream(is);
172                                 formula->read(cur.buffer(), lex);
173                                 if (formula->getType() == hullNone)
174                                         // Don't create pseudo formulas if
175                                         // delimiters are left out
176                                         formula->mutate(hullSimple);
177                                 cur.insert(formula);
178                         } else {
179                                 cur.insert(new MathMacroTemplate(sel));
180                         }
181                 }
182                 cur.message(from_utf8(N_("Math editor mode")));
183         }
184
185 } // namespace anon
186
187
188
189 namespace bv_funcs {
190
191 string const freefont2string()
192 {
193         string data;
194         if (font2string(freefont, toggleall, data))
195                 return data;
196         return string();
197 }
198
199 }
200
201 bool LyXText::cursorPrevious(LCursor & cur)
202 {
203         pos_type cpos = cur.pos();
204         pit_type cpar = cur.pit();
205
206         int x = cur.x_target();
207
208         bool updated = setCursorFromCoordinates(cur, x, 0);
209         if (updated)
210                 cur.bv().update();
211         updated |= cursorUp(cur);
212
213         if (cpar == cur.pit() && cpos == cur.pos()) {
214                 // we have a row which is taller than the workarea. The
215                 // simplest solution is to move to the previous row instead.
216                 updated |= cursorUp(cur);
217         }
218
219         finishUndo();
220         return updated;
221 }
222
223
224 bool LyXText::cursorNext(LCursor & cur)
225 {
226         pos_type cpos = cur.pos();
227         pit_type cpar = cur.pit();
228
229         int x = cur.x_target();
230         bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
231         if (updated)
232                 cur.bv().update();
233         updated |= cursorDown(cur);
234
235         if (cpar == cur.pit() && cpos == cur.pos()) {
236                 // we have a row which is taller than the workarea. The
237                 // simplest solution is to move to the next row instead.
238                 updated |= cursorDown(cur);
239         }
240
241         finishUndo();
242         return updated;
243 }
244
245
246 namespace {
247
248 void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
249 {
250         cap::replaceSelection(cur);
251         cur.insert(new InsetSpecialChar(kind));
252         cur.posRight();
253 }
254
255
256 bool doInsertInset(LCursor & cur, LyXText * text,
257         FuncRequest const & cmd, bool edit, bool pastesel)
258 {
259         InsetBase * inset = createInset(&cur.bv(), cmd);
260         if (!inset)
261                 return false;
262
263         recordUndo(cur);
264         bool gotsel = false;
265         if (cur.selection()) {
266                 lyx::dispatch(FuncRequest(LFUN_CUT));
267                 gotsel = true;
268         }
269         text->insertInset(cur, inset);
270
271         if (edit)
272                 inset->edit(cur, true);
273
274         if (gotsel && pastesel) {
275                 lyx::dispatch(FuncRequest(LFUN_PASTE));
276                 // reset first par to default
277                 if (cur.lastpit() != 0 || cur.lastpos() != 0) {
278                         LyXLayout_ptr const layout =
279                                 cur.buffer().params().getLyXTextClass().defaultLayout();
280                         cur.text()->paragraphs().begin()->layout(layout);
281                 }
282         }
283         return true;
284 }
285
286
287 } // anon namespace
288
289
290 void LyXText::number(LCursor & cur)
291 {
292         LyXFont font(LyXFont::ALL_IGNORE);
293         font.setNumber(LyXFont::TOGGLE);
294         toggleAndShow(cur, this, font);
295 }
296
297
298 bool LyXText::isRTL(Paragraph const & par) const
299 {
300         return par.isRightToLeftPar(bv()->buffer()->params());
301 }
302
303
304 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
305 {
306         lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
307
308         BOOST_ASSERT(cur.text() == this);
309         BufferView * bv = &cur.bv();
310         CursorSlice oldTopSlice = cur.top();
311         bool oldBoundary = cur.boundary();
312         bool sel = cur.selection();
313         // Signals that, even if needsUpdate == false, an update of the
314         // cursor paragraph is required
315         bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
316                 LyXAction::SingleParUpdate);
317         // Signals that a full-screen update is required
318         bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
319                 LyXAction::NoUpdate) || singleParUpdate);
320         // Remember the old paragraph metric (_outer_ paragraph!)
321         Dimension olddim = cur.bottom().paragraph().dim();
322
323         switch (cmd.action) {
324
325         case LFUN_PARAGRAPH_MOVE_DOWN: {
326                 pit_type const pit = cur.pit();
327                 recUndo(pit, pit + 1);
328                 finishUndo();
329                 std::swap(pars_[pit], pars_[pit + 1]);
330
331                 ParIterator begin(cur);
332                 ++cur.pit();
333                 ParIterator end = boost::next(ParIterator(cur));
334                 updateLabels(cur.buffer(), begin, end);
335
336                 needsUpdate = true;
337                 break;
338         }
339
340         case LFUN_PARAGRAPH_MOVE_UP: {
341                 pit_type const pit = cur.pit();
342                 recUndo(pit - 1, pit);
343                 finishUndo();
344                 std::swap(pars_[pit], pars_[pit - 1]);
345
346                 ParIterator end = boost::next(ParIterator(cur));
347                 --cur.pit();
348                 ParIterator begin(cur);
349                 updateLabels(cur.buffer(), begin, end);
350
351                 needsUpdate = true;
352                 break;
353         }
354
355         case LFUN_APPENDIX: {
356                 Paragraph & par = cur.paragraph();
357                 bool start = !par.params().startOfAppendix();
358
359 #ifdef WITH_WARNINGS
360 #warning The code below only makes sense at top level.
361 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
362 #endif
363                 // ensure that we have only one start_of_appendix in this document
364                 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
365                         if (pars_[tmp].params().startOfAppendix()) {
366                                 recUndo(tmp);
367                                 pars_[tmp].params().startOfAppendix(false);
368                                 break;
369                         }
370                 }
371
372                 recordUndo(cur);
373                 par.params().startOfAppendix(start);
374
375                 // we can set the refreshing parameters now
376                 updateLabels(cur.buffer());
377                 break;
378         }
379
380         case LFUN_WORD_DELETE_FORWARD:
381                 cur.clearSelection();
382                 deleteWordForward(cur);
383                 finishChange(cur, false);
384                 break;
385
386         case LFUN_WORD_DELETE_BACKWARD:
387                 cur.clearSelection();
388                 deleteWordBackward(cur);
389                 finishChange(cur, false);
390                 break;
391
392         case LFUN_LINE_DELETE:
393                 cur.clearSelection();
394                 deleteLineForward(cur);
395                 finishChange(cur, false);
396                 break;
397
398         case LFUN_BUFFER_BEGIN:
399         case LFUN_BUFFER_BEGIN_SELECT:
400                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
401                 if (cur.depth() == 1) {
402                         needsUpdate |= cursorTop(cur);
403                 } else {
404                         cur.undispatched();
405                 }
406                 break;
407
408         case LFUN_BUFFER_END:
409         case LFUN_BUFFER_END_SELECT:
410                 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
411                 if (cur.depth() == 1) {
412                         needsUpdate |= cursorBottom(cur);
413                 } else {
414                         cur.undispatched();
415                 }
416                 break;
417
418         case LFUN_CHAR_FORWARD:
419         case LFUN_CHAR_FORWARD_SELECT:
420                 //lyxerr << BOOST_CURRENT_FUNCTION
421                 //       << " LFUN_CHAR_FORWARD[SEL]:\n" << cur << endl;
422                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
423                 if (isRTL(cur.paragraph()))
424                         needsUpdate |= cursorLeft(cur);
425                 else
426                         needsUpdate |= cursorRight(cur);
427
428                 if (!needsUpdate && oldTopSlice == cur.top()
429                                 && cur.boundary() == oldBoundary) {
430                         cur.undispatched();
431                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
432                 }
433                 break;
434
435         case LFUN_CHAR_BACKWARD:
436         case LFUN_CHAR_BACKWARD_SELECT:
437                 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
438                 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
439                 if (isRTL(cur.paragraph()))
440                         needsUpdate |= cursorRight(cur);
441                 else
442                         needsUpdate |= cursorLeft(cur);
443
444                 if (!needsUpdate && oldTopSlice == cur.top()
445                         && cur.boundary() == oldBoundary) {
446                         cur.undispatched();
447                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
448                 }
449                 break;
450
451         case LFUN_UP:
452         case LFUN_UP_SELECT:
453                 //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
454                 needsUpdate |= cur.selHandle(cmd.action == LFUN_UP_SELECT);
455
456                 needsUpdate |= cursorUp(cur);
457
458                 if (!needsUpdate && oldTopSlice == cur.top()
459                           && cur.boundary() == oldBoundary) {
460                         cur.undispatched();
461                         cmd = FuncRequest(LFUN_FINISHED_UP);
462                 }
463                 break;
464
465         case LFUN_DOWN:
466         case LFUN_DOWN_SELECT:
467                 //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
468                 needsUpdate |= cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
469                 needsUpdate |= cursorDown(cur);
470
471                 if (!needsUpdate && oldTopSlice == cur.top() &&
472                     cur.boundary() == oldBoundary)
473                 {
474                         cur.undispatched();
475                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
476                 }
477                 break;
478
479         case LFUN_PARAGRAPH_UP:
480         case LFUN_PARAGRAPH_UP_SELECT:
481                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
482                 needsUpdate |= cursorUpParagraph(cur);
483                 break;
484
485         case LFUN_PARAGRAPH_DOWN:
486         case LFUN_PARAGRAPH_DOWN_SELECT:
487                 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
488                 needsUpdate |= cursorDownParagraph(cur);
489                 break;
490
491         case LFUN_SCREEN_UP:
492         case LFUN_SCREEN_UP_SELECT:
493                 needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_UP_SELECT);
494                 if (cur.pit() == 0 && cur.textRow().pos() == 0) {
495                         cur.undispatched();
496                         cmd = FuncRequest(LFUN_FINISHED_UP);
497                 } else {
498                         needsUpdate |= cursorPrevious(cur);
499                 }
500                 break;
501
502         case LFUN_SCREEN_DOWN:
503         case LFUN_SCREEN_DOWN_SELECT:
504                 needsUpdate |= cur.selHandle(cmd.action == LFUN_SCREEN_DOWN_SELECT);
505                 if (cur.pit() == cur.lastpit()
506                           && cur.textRow().endpos() == cur.lastpos()) {
507                         cur.undispatched();
508                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
509                 } else {
510                         needsUpdate |= cursorNext(cur);
511                 }
512                 break;
513
514         case LFUN_LINE_BEGIN:
515         case LFUN_LINE_BEGIN_SELECT:
516                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
517                 needsUpdate |= cursorHome(cur);
518                 break;
519
520         case LFUN_LINE_END:
521         case LFUN_LINE_END_SELECT:
522                 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
523                 needsUpdate |= cursorEnd(cur);
524                 break;
525
526         case LFUN_WORD_FORWARD:
527         case LFUN_WORD_FORWARD_SELECT:
528                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
529                 if (isRTL(cur.paragraph()))
530                         needsUpdate |= cursorLeftOneWord(cur);
531                 else
532                         needsUpdate |= cursorRightOneWord(cur);
533                 break;
534
535         case LFUN_WORD_BACKWARD:
536         case LFUN_WORD_BACKWARD_SELECT:
537                 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
538                 if (isRTL(cur.paragraph()))
539                         needsUpdate |= cursorRightOneWord(cur);
540                 else
541                         needsUpdate |= cursorLeftOneWord(cur);
542                 break;
543
544         case LFUN_WORD_SELECT: {
545                 selectWord(cur, WHOLE_WORD);
546                 finishChange(cur, true);
547                 break;
548         }
549
550         case LFUN_BREAK_LINE: {
551                 // Not allowed by LaTeX (labels or empty par)
552                 if (cur.pos() > cur.paragraph().beginOfBody()) {
553                         cap::replaceSelection(cur);
554                         cur.insert(new InsetNewline);
555                         cur.posRight();
556                         moveCursor(cur, false);
557                 }
558                 break;
559         }
560
561         case LFUN_CHAR_DELETE_FORWARD:
562                 if (!cur.selection()) {
563                         if (cur.pos() == cur.paragraph().size())
564                                 // Par boundary, force full-screen update
565                                 singleParUpdate = false;
566                         needsUpdate |= erase(cur);
567                         cur.resetAnchor();
568                         // It is possible to make it a lot faster still
569                         // just comment out the line below...
570                 } else {
571                         cutSelection(cur, true, false);
572                         singleParUpdate = false;
573                 }
574                 moveCursor(cur, false);
575                 break;
576
577         case LFUN_DELETE_FORWARD_SKIP:
578                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
579                 if (!cur.selection()) {
580                         if (cur.pos() == cur.lastpos()) {
581                                 cursorRight(cur);
582                                 cursorLeft(cur);
583                         }
584                         erase(cur);
585                         cur.resetAnchor();
586                 } else {
587                         cutSelection(cur, true, false);
588                 }
589                 break;
590
591
592         case LFUN_CHAR_DELETE_BACKWARD:
593                 if (!cur.selection()) {
594                         if (bv->getIntl().getTransManager().backspace()) {
595                                 // Par boundary, full-screen update
596                                 if (cur.pos() == 0)
597                                         singleParUpdate = false;
598                                 needsUpdate |= backspace(cur);
599                                 cur.resetAnchor();
600                                 // It is possible to make it a lot faster still
601                                 // just comment out the line below...
602                         }
603                 } else {
604                         cutSelection(cur, true, false);
605                         singleParUpdate = false;
606                 }
607                 bv->switchKeyMap();
608                 break;
609
610         case LFUN_DELETE_BACKWARD_SKIP:
611                 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
612                 if (!cur.selection()) {
613 #ifdef WITH_WARNINGS
614 #warning look here
615 #endif
616                         //CursorSlice cur = cursor();
617                         backspace(cur);
618                         //anchor() = cur;
619                 } else {
620                         cutSelection(cur, true, false);
621                 }
622                 break;
623
624         case LFUN_BREAK_PARAGRAPH:
625                 cap::replaceSelection(cur);
626                 breakParagraph(cur, 0);
627                 cur.resetAnchor();
628                 bv->switchKeyMap();
629                 break;
630
631         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
632                 cap::replaceSelection(cur);
633                 breakParagraph(cur, 1);
634                 cur.resetAnchor();
635                 bv->switchKeyMap();
636                 break;
637
638         case LFUN_BREAK_PARAGRAPH_SKIP: {
639                 // When at the beginning of a paragraph, remove
640                 // indentation.  Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
641                 cap::replaceSelection(cur);
642                 if (cur.pos() == 0)
643                         cur.paragraph().params().labelWidthString(docstring());
644                 else
645                         breakParagraph(cur, 0);
646                 cur.resetAnchor();
647                 bv->switchKeyMap();
648                 break;
649         }
650
651         case LFUN_PARAGRAPH_SPACING: {
652                 Paragraph & par = cur.paragraph();
653                 Spacing::Space cur_spacing = par.params().spacing().getSpace();
654                 string cur_value = "1.0";
655                 if (cur_spacing == Spacing::Other)
656                         cur_value = par.params().spacing().getValueAsString();
657
658                 istringstream is(to_utf8(cmd.argument()));
659                 string tmp;
660                 is >> tmp;
661                 Spacing::Space new_spacing = cur_spacing;
662                 string new_value = cur_value;
663                 if (tmp.empty()) {
664                         lyxerr << "Missing argument to `paragraph-spacing'"
665                                << endl;
666                 } else if (tmp == "single") {
667                         new_spacing = Spacing::Single;
668                 } else if (tmp == "onehalf") {
669                         new_spacing = Spacing::Onehalf;
670                 } else if (tmp == "double") {
671                         new_spacing = Spacing::Double;
672                 } else if (tmp == "other") {
673                         new_spacing = Spacing::Other;
674                         string tmpval = "0.0";
675                         is >> tmpval;
676                         lyxerr << "new_value = " << tmpval << endl;
677                         if (tmpval != "0.0")
678                                 new_value = tmpval;
679                 } else if (tmp == "default") {
680                         new_spacing = Spacing::Default;
681                 } else {
682                         lyxerr << to_utf8(_("Unknown spacing argument: "))
683                                << to_utf8(cmd.argument()) << endl;
684                 }
685                 if (cur_spacing != new_spacing || cur_value != new_value)
686                         par.params().spacing(Spacing(new_spacing, new_value));
687                 break;
688         }
689
690         case LFUN_INSET_INSERT: {
691                 recordUndo(cur);
692                 InsetBase * inset = createInset(bv, cmd);
693                 if (inset) {
694                         insertInset(cur, inset);
695                         cur.posRight();
696                 }
697                 break;
698         }
699
700         case LFUN_INSET_DISSOLVE:
701                 needsUpdate |= dissolveInset(cur);
702                 break;
703
704         case LFUN_INSET_SETTINGS:
705                 cur.inset().showInsetDialog(bv);
706                 break;
707
708         case LFUN_SPACE_INSERT:
709                 if (cur.paragraph().layout()->free_spacing)
710                         insertChar(cur, ' ');
711                 else {
712                         doInsertInset(cur, this, cmd, false, false);
713                         cur.posRight();
714                 }
715                 moveCursor(cur, false);
716                 break;
717
718         case LFUN_HYPHENATION_POINT_INSERT:
719                 specialChar(cur, InsetSpecialChar::HYPHENATION);
720                 break;
721
722         case LFUN_LIGATURE_BREAK_INSERT:
723                 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
724                 break;
725
726         case LFUN_DOTS_INSERT:
727                 specialChar(cur, InsetSpecialChar::LDOTS);
728                 break;
729
730         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
731                 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
732                 break;
733
734         case LFUN_MENU_SEPARATOR_INSERT:
735                 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
736                 break;
737
738         case LFUN_WORD_UPCASE:
739                 changeCase(cur, LyXText::text_uppercase);
740                 break;
741
742         case LFUN_WORD_LOWCASE:
743                 changeCase(cur, LyXText::text_lowercase);
744                 break;
745
746         case LFUN_WORD_CAPITALIZE:
747                 changeCase(cur, LyXText::text_capitalization);
748                 break;
749
750         case LFUN_CHARS_TRANSPOSE:
751                 recordUndo(cur);
752                 break;
753
754         case LFUN_PASTE:
755                 cur.message(_("Paste"));
756                 cap::replaceSelection(cur);
757                 if (isStrUnsignedInt(to_utf8(cmd.argument())))
758                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
759                         convert<unsigned int>(to_utf8(cmd.argument())));
760                 else
761                         pasteSelection(cur, bv->buffer()->errorList("Paste"),
762                         0);
763                 bv->buffer()->errors("Paste");
764                 cur.clearSelection(); // bug 393
765                 bv->switchKeyMap();
766                 finishUndo();
767                 break;
768
769         case LFUN_CUT:
770                 cutSelection(cur, true, true);
771                 cur.message(_("Cut"));
772                 break;
773
774         case LFUN_COPY:
775                 copySelection(cur);
776                 cur.message(_("Copy"));
777                 break;
778
779         case LFUN_SERVER_GET_XY:
780                 cur.message(from_utf8(
781                         convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
782                           + convert<string>(cursorY(cur.top(), cur.boundary()))));
783                 break;
784
785         case LFUN_SERVER_SET_XY: {
786                 int x = 0;
787                 int y = 0;
788                 istringstream is(to_utf8(cmd.argument()));
789                 is >> x >> y;
790                 if (!is)
791                         lyxerr << "SETXY: Could not parse coordinates in '"
792                                << to_utf8(cmd.argument()) << std::endl;
793                 else
794                         setCursorFromCoordinates(cur, x, y);
795                 break;
796         }
797
798         case LFUN_SERVER_GET_FONT:
799                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
800                         cur.message(from_ascii("E"));
801                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
802                         cur.message(from_ascii("N"));
803                 else
804                         cur.message(from_ascii("0"));
805                 break;
806
807         case LFUN_SERVER_GET_LAYOUT:
808                 cur.message(from_utf8(cur.paragraph().layout()->name()));
809                 break;
810
811         case LFUN_LAYOUT: {
812                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
813                   << to_utf8(cmd.argument()) << endl;
814
815                 // This is not the good solution to the empty argument
816                 // problem, but it will hopefully suffice for 1.2.0.
817                 // The correct solution would be to augument the
818                 // function list/array with information about what
819                 // functions needs arguments and their type.
820                 if (cmd.argument().empty()) {
821                         cur.errorMessage(_("LyX function 'layout' needs an argument."));
822                         break;
823                 }
824
825                 // Derive layout number from given argument (string)
826                 // and current buffer's textclass (number)
827                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
828                 bool hasLayout = tclass.hasLayout(to_utf8(cmd.argument()));
829                 string layout = to_utf8(cmd.argument());
830
831                 // If the entry is obsolete, use the new one instead.
832                 if (hasLayout) {
833                         string const & obs = tclass[layout]->obsoleted_by();
834                         if (!obs.empty())
835                                 layout = obs;
836                 }
837
838                 if (!hasLayout) {
839                         cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
840                                 from_utf8(N_(" not known")));
841                         break;
842                 }
843
844                 bool change_layout = (current_layout != layout);
845
846                 if (!change_layout && cur.selection() &&
847                         cur.selBegin().pit() != cur.selEnd().pit())
848                 {
849                         pit_type spit = cur.selBegin().pit();
850                         pit_type epit = cur.selEnd().pit() + 1;
851                         while (spit != epit) {
852                                 if (pars_[spit].layout()->name() != current_layout) {
853                                         change_layout = true;
854                                         break;
855                                 }
856                                 ++spit;
857                         }
858                 }
859
860                 if (change_layout) {
861                         current_layout = layout;
862                         setLayout(cur, layout);
863                         // inform the GUI that the layout has changed.
864                         bv->layoutChanged(layout);
865                         bv->switchKeyMap();
866                 }
867                 break;
868         }
869
870         case LFUN_CLIPBOARD_PASTE: {
871                 cur.clearSelection();
872                 docstring const clip = theClipboard().get();
873                 if (!clip.empty()) {
874                         recordUndo(cur);
875                         if (cmd.argument() == "paragraph")
876                                 insertStringAsParagraphs(cur, clip);
877                         else
878                                 insertStringAsLines(cur, clip);
879                 }
880                 break;
881         }
882
883         case LFUN_PRIMARY_SELECTION_PASTE: {
884                 cur.clearSelection();
885                 docstring const clip = theSelection().get();
886                 if (!clip.empty()) {
887                         recordUndo(cur);
888                         if (cmd.argument() == "paragraph")
889                                 insertStringAsParagraphs(cur, clip);
890                         else
891                                 insertStringAsLines(cur, clip);
892                 }
893                 break;
894         }
895
896         case LFUN_UNICODE_INSERT: {
897                 if (cmd.argument().empty())
898                         break;
899                 docstring hexstring = cmd.argument();
900                 if (lyx::support::isHex(hexstring)) {
901                         char_type c = lyx::support::hexToInt(hexstring);
902                         if (c > 32 && c < 0x10ffff) {
903                                 lyxerr << "Inserting c: " << c << endl;
904                                 docstring s = docstring(1, c);
905                                 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
906                         }
907                 }
908                 break;
909         }
910                 
911         case LFUN_QUOTE_INSERT: {
912                 cap::replaceSelection(cur);
913                 Paragraph & par = cur.paragraph();
914                 pos_type pos = cur.pos();
915                 char_type c;
916                 if (pos == 0)
917                         c = ' ';
918                 else if (cur.prevInset() && cur.prevInset()->isSpace())
919                         c = ' ';
920                 else
921                         c = par.getChar(pos - 1);
922
923                 LyXLayout_ptr const & style = par.layout();
924
925                 BufferParams const & bufparams = bv->buffer()->params();
926                 if (!style->pass_thru
927                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
928                         string arg = to_utf8(cmd.argument());
929                         if (arg == "single")
930                                 cur.insert(new InsetQuotes(c,
931                                     bufparams.quotes_language,
932                                     InsetQuotes::SingleQ));
933                         else
934                                 cur.insert(new InsetQuotes(c,
935                                     bufparams.quotes_language,
936                                     InsetQuotes::DoubleQ));
937                         cur.posRight();
938                 }
939                 else
940                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
941                 break;
942         }
943
944         case LFUN_DATE_INSERT:
945                 if (cmd.argument().empty())
946                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
947                                 formatted_time(current_time())));
948                 else
949                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
950                                 formatted_time(current_time(), to_utf8(cmd.argument()))));
951                 break;
952
953         case LFUN_MOUSE_TRIPLE:
954                 if (cmd.button() == mouse_button::button1) {
955                         cursorHome(cur);
956                         cur.resetAnchor();
957                         cursorEnd(cur);
958                         cur.setSelection();
959                         bv->cursor() = cur;
960                         theSelection().haveSelection(cur.selection());
961                 }
962                 break;
963
964         case LFUN_MOUSE_DOUBLE:
965                 if (cmd.button() == mouse_button::button1) {
966                         selectWord(cur, WHOLE_WORD_STRICT);
967                         bv->cursor() = cur;
968                         theSelection().haveSelection(cur.selection());
969                 }
970                 break;
971
972         // Single-click on work area
973         case LFUN_MOUSE_PRESS: {
974                 // Right click on a footnote flag opens float menu
975                 if (cmd.button() == mouse_button::button3)
976                         cur.clearSelection();
977
978                 // Middle button press pastes if we have a selection
979                 // We do this here as if the selection was inside an inset
980                 // it could get cleared on the unlocking of the inset so
981                 // we have to check this first
982                 bool paste_internally = false;
983                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
984                         lyx::dispatch(FuncRequest(LFUN_COPY));
985                         paste_internally = true;
986                 }
987
988                 bv->mouseSetCursor(cur);
989
990                 // Insert primary selection with middle mouse
991                 // if there is a local selection in the current buffer,
992                 // insert this
993                 if (cmd.button() == mouse_button::button2) {
994                         if (paste_internally)
995                                 lyx::dispatch(FuncRequest(LFUN_PASTE));
996                         else
997                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
998                 }
999
1000                 break;
1001         }
1002
1003         case LFUN_MOUSE_MOTION: {
1004                 // Only use motion with button 1
1005                 //if (cmd.button() != mouse_button::button1)
1006                 //      return false;
1007
1008                 // ignore motions deeper nested than the real anchor
1009                 LCursor & bvcur = cur.bv().cursor();
1010                 if (bvcur.anchor_.hasPart(cur)) {
1011                         CursorSlice old = bvcur.top();
1012
1013                         int const wh = bv->workHeight();
1014                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1015
1016                         setCursorFromCoordinates(cur, cmd.x, y);
1017                         cur.x_target() = cmd.x;
1018                         if (cmd.y >= wh)
1019                                 cursorDown(cur);
1020                         else if (cmd.y < 0)
1021                                 cursorUp(cur);
1022                         // This is to allow jumping over large insets
1023                         if (cur.top() == old) {
1024                                 if (cmd.y >= wh)
1025                                         cursorDown(cur);
1026                                 else if (cmd.y < 0)
1027                                         cursorUp(cur);
1028                         }
1029
1030                         if (cur.top() == old)
1031                                 cur.noUpdate();
1032                         else {
1033                                 // don't set anchor_
1034                                 bvcur.setCursor(cur);
1035                                 bvcur.selection() = true;
1036                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1037                         }
1038
1039                 } else
1040                         cur.undispatched();
1041                 break;
1042         }
1043
1044         case LFUN_MOUSE_RELEASE: {
1045                 if (cmd.button() == mouse_button::button2)
1046                         break;
1047
1048                 // finish selection
1049                 if (cmd.button() == mouse_button::button1)
1050                         theSelection().haveSelection(cur.selection());
1051
1052                 bv->switchKeyMap();
1053                 break;
1054         }
1055
1056         case LFUN_SELF_INSERT: {
1057                 if (cmd.argument().empty())
1058                         break;
1059
1060                 // Automatically delete the currently selected
1061                 // text and replace it with what is being
1062                 // typed in now. Depends on lyxrc settings
1063                 // "auto_region_delete", which defaults to
1064                 // true (on).
1065
1066                 if (lyxrc.auto_region_delete) {
1067                         if (cur.selection())
1068                                 cutSelection(cur, false, false);
1069                         theSelection().haveSelection(false);
1070                 }
1071
1072                 cur.clearSelection();
1073                 LyXFont const old_font = real_current_font;
1074
1075                 docstring::const_iterator cit = cmd.argument().begin();
1076                 docstring::const_iterator end = cmd.argument().end();
1077                 for (; cit != end; ++cit)
1078 #if 0
1079                         bv->getIntl().getTransManager().
1080                                 translateAndInsert(*cit, this);
1081 #else
1082                         insertChar(bv->cursor(), *cit);
1083 #endif
1084
1085                 cur.resetAnchor();
1086                 moveCursor(cur, false);
1087                 break;
1088         }
1089
1090         case LFUN_URL_INSERT: {
1091                 InsetCommandParams p("url");
1092                 string const data = InsetCommandMailer::params2string("url", p);
1093                 bv->showInsetDialog("url", data, 0);
1094                 break;
1095         }
1096
1097         case LFUN_HTML_INSERT: {
1098                 InsetCommandParams p("htmlurl");
1099                 string const data = InsetCommandMailer::params2string("url", p);
1100                 bv->showInsetDialog("url", data, 0);
1101                 break;
1102         }
1103
1104         case LFUN_LABEL_INSERT: {
1105                 InsetCommandParams p("label");
1106                 // Try to generate a valid label
1107                 p["name"] = (cmd.argument().empty()) ?
1108                         // FIXME UNICODE
1109                         from_utf8(cur.getPossibleLabel()) :
1110                         cmd.argument();
1111                 string const data = InsetCommandMailer::params2string("label", p);
1112
1113                 if (cmd.argument().empty()) {
1114                         bv->showInsetDialog("label", data, 0);
1115                 } else {
1116                         FuncRequest fr(LFUN_INSET_INSERT, data);
1117                         dispatch(cur, fr);
1118                 }
1119                 break;
1120         }
1121
1122
1123 #if 0
1124         case LFUN_LIST_INSERT:
1125         case LFUN_THEOREM_INSERT:
1126         case LFUN_CAPTION_INSERT:
1127 #endif
1128         case LFUN_NOTE_INSERT:
1129         case LFUN_CHARSTYLE_INSERT:
1130         case LFUN_BOX_INSERT:
1131         case LFUN_BRANCH_INSERT:
1132         case LFUN_BIBITEM_INSERT:
1133         case LFUN_ERT_INSERT:
1134         case LFUN_FOOTNOTE_INSERT:
1135         case LFUN_MARGINALNOTE_INSERT:
1136         case LFUN_OPTIONAL_INSERT:
1137         case LFUN_ENVIRONMENT_INSERT:
1138                 // Open the inset, and move the current selection
1139                 // inside it.
1140                 doInsertInset(cur, this, cmd, true, true);
1141                 cur.posRight();
1142                 break;
1143
1144         case LFUN_TABULAR_INSERT:
1145                 // if there were no arguments, just open the dialog
1146                 if (doInsertInset(cur, this, cmd, false, true))
1147                         cur.posRight();
1148                 else
1149                         bv->showDialog("tabularcreate");
1150
1151                 break;
1152
1153         case LFUN_FLOAT_INSERT:
1154         case LFUN_FLOAT_WIDE_INSERT:
1155         case LFUN_WRAP_INSERT:
1156                 doInsertInset(cur, this, cmd, true, true);
1157                 cur.posRight();
1158                 // FIXME: the "Caption" name should not be hardcoded,
1159                 // but given by the float definition.
1160                 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1161                 break;
1162
1163         case LFUN_INDEX_INSERT: {
1164                 InsetBase * inset = createInset(&cur.bv(), cmd);
1165                 if (!inset)
1166                         break;
1167
1168                 recordUndo(cur);
1169                 cur.clearSelection();
1170                 insertInset(cur, inset);
1171                 inset->edit(cur, true);
1172                 cur.posRight();
1173                 break;
1174         }
1175
1176         case LFUN_INDEX_PRINT:
1177         case LFUN_TOC_INSERT:
1178         case LFUN_HFILL_INSERT:
1179         case LFUN_LINE_INSERT:
1180         case LFUN_PAGEBREAK_INSERT:
1181                 // do nothing fancy
1182                 doInsertInset(cur, this, cmd, false, false);
1183                 cur.posRight();
1184                 break;
1185
1186         case LFUN_DEPTH_DECREMENT:
1187                 changeDepth(cur, DEC_DEPTH);
1188                 break;
1189
1190         case LFUN_DEPTH_INCREMENT:
1191                 changeDepth(cur, INC_DEPTH);
1192                 break;
1193
1194         case LFUN_MATH_DISPLAY:
1195                 mathDispatch(cur, cmd, true);
1196                 break;
1197
1198         case LFUN_MATH_IMPORT_SELECTION:
1199         case LFUN_MATH_MODE:
1200                 if (cmd.argument() == "on")
1201                         // don't pass "on" as argument
1202                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1203                 else
1204                         mathDispatch(cur, cmd, false);
1205                 break;
1206
1207         case LFUN_MATH_MACRO:
1208                 if (cmd.argument().empty())
1209                         cur.errorMessage(from_utf8(N_("Missing argument")));
1210                 else {
1211                         string s = to_utf8(cmd.argument());
1212                         string const s1 = token(s, ' ', 1);
1213                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1214                         string const s2 = token(s, ' ', 2);
1215                         string const type = s2.empty() ? "newcommand" : s2;
1216                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1217                         //cur.nextInset()->edit(cur, true);
1218                 }
1219                 break;
1220
1221         // passthrough hat and underscore outside mathed:
1222         case LFUN_MATH_SUBSCRIPT:
1223                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1224                 break;
1225         case LFUN_MATH_SUPERSCRIPT:
1226                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1227                 break;
1228
1229         case LFUN_MATH_INSERT:
1230         case LFUN_MATH_MATRIX:
1231         case LFUN_MATH_DELIM:
1232         case LFUN_MATH_BIGDELIM: {
1233                 cur.insert(new InsetMathHull(hullSimple));
1234                 cur.dispatch(FuncRequest(LFUN_CHAR_FORWARD));
1235                 cur.dispatch(cmd);
1236                 break;
1237         }
1238
1239         case LFUN_FONT_EMPH: {
1240                 LyXFont font(LyXFont::ALL_IGNORE);
1241                 font.setEmph(LyXFont::TOGGLE);
1242                 toggleAndShow(cur, this, font);
1243                 break;
1244         }
1245
1246         case LFUN_FONT_BOLD: {
1247                 LyXFont font(LyXFont::ALL_IGNORE);
1248                 font.setSeries(LyXFont::BOLD_SERIES);
1249                 toggleAndShow(cur, this, font);
1250                 break;
1251         }
1252
1253         case LFUN_FONT_NOUN: {
1254                 LyXFont font(LyXFont::ALL_IGNORE);
1255                 font.setNoun(LyXFont::TOGGLE);
1256                 toggleAndShow(cur, this, font);
1257                 break;
1258         }
1259
1260         case LFUN_FONT_CODE: {
1261                 LyXFont font(LyXFont::ALL_IGNORE);
1262                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1263                 toggleAndShow(cur, this, font);
1264                 break;
1265         }
1266
1267         case LFUN_FONT_SANS: {
1268                 LyXFont font(LyXFont::ALL_IGNORE);
1269                 font.setFamily(LyXFont::SANS_FAMILY);
1270                 toggleAndShow(cur, this, font);
1271                 break;
1272         }
1273
1274         case LFUN_FONT_ROMAN: {
1275                 LyXFont font(LyXFont::ALL_IGNORE);
1276                 font.setFamily(LyXFont::ROMAN_FAMILY);
1277                 toggleAndShow(cur, this, font);
1278                 break;
1279         }
1280
1281         case LFUN_FONT_DEFAULT: {
1282                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1283                 toggleAndShow(cur, this, font);
1284                 break;
1285         }
1286
1287         case LFUN_FONT_UNDERLINE: {
1288                 LyXFont font(LyXFont::ALL_IGNORE);
1289                 font.setUnderbar(LyXFont::TOGGLE);
1290                 toggleAndShow(cur, this, font);
1291                 break;
1292         }
1293
1294         case LFUN_FONT_SIZE: {
1295                 LyXFont font(LyXFont::ALL_IGNORE);
1296                 font.setLyXSize(to_utf8(cmd.argument()));
1297                 toggleAndShow(cur, this, font);
1298                 break;
1299         }
1300
1301         case LFUN_LANGUAGE: {
1302                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1303                 if (!lang)
1304                         break;
1305                 LyXFont font(LyXFont::ALL_IGNORE);
1306                 font.setLanguage(lang);
1307                 toggleAndShow(cur, this, font);
1308                 bv->switchKeyMap();
1309                 break;
1310         }
1311
1312         case LFUN_FONT_FREE_APPLY:
1313                 toggleAndShow(cur, this, freefont, toggleall);
1314                 cur.message(_("Character set"));
1315                 break;
1316
1317         // Set the freefont using the contents of \param data dispatched from
1318         // the frontends and apply it at the current cursor location.
1319         case LFUN_FONT_FREE_UPDATE: {
1320                 LyXFont font;
1321                 bool toggle;
1322                 if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) {
1323                         freefont = font;
1324                         toggleall = toggle;
1325                         toggleAndShow(cur, this, freefont, toggleall);
1326                         cur.message(_("Character set"));
1327                 }
1328                 break;
1329         }
1330
1331         case LFUN_FINISHED_LEFT:
1332                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1333                 break;
1334
1335         case LFUN_FINISHED_RIGHT:
1336                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1337                 ++cur.pos();
1338                 break;
1339
1340         case LFUN_FINISHED_UP:
1341                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1342                 cursorUp(cur);
1343                 break;
1344
1345         case LFUN_FINISHED_DOWN:
1346                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1347                 cursorDown(cur);
1348                 break;
1349
1350         case LFUN_LAYOUT_PARAGRAPH: {
1351                 string data;
1352                 params2string(cur.paragraph(), data);
1353                 data = "show\n" + data;
1354                 bv->showDialogWithData("paragraph", data);
1355                 break;
1356         }
1357
1358         case LFUN_PARAGRAPH_UPDATE: {
1359                 string data;
1360                 params2string(cur.paragraph(), data);
1361
1362                 // Will the paragraph accept changes from the dialog?
1363                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1364
1365                 data = "update " + convert<string>(accept) + '\n' + data;
1366                 bv->updateDialog("paragraph", data);
1367                 break;
1368         }
1369
1370         case LFUN_ACCENT_UMLAUT:
1371         case LFUN_ACCENT_CIRCUMFLEX:
1372         case LFUN_ACCENT_GRAVE:
1373         case LFUN_ACCENT_ACUTE:
1374         case LFUN_ACCENT_TILDE:
1375         case LFUN_ACCENT_CEDILLA:
1376         case LFUN_ACCENT_MACRON:
1377         case LFUN_ACCENT_DOT:
1378         case LFUN_ACCENT_UNDERDOT:
1379         case LFUN_ACCENT_UNDERBAR:
1380         case LFUN_ACCENT_CARON:
1381         case LFUN_ACCENT_SPECIAL_CARON:
1382         case LFUN_ACCENT_BREVE:
1383         case LFUN_ACCENT_TIE:
1384         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1385         case LFUN_ACCENT_CIRCLE:
1386         case LFUN_ACCENT_OGONEK:
1387                 theLyXFunc().handleKeyFunc(cmd.action);
1388                 if (!cmd.argument().empty())
1389                         // FIXME: Are all these characters encoded in one byte in utf8?
1390                         bv->getIntl().getTransManager()
1391                                 .translateAndInsert(cmd.argument()[0], this);
1392                 break;
1393
1394         case LFUN_FLOAT_LIST: {
1395                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1396                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1397                         // not quite sure if we want this...
1398                         recordUndo(cur);
1399                         cur.clearSelection();
1400                         breakParagraph(cur);
1401
1402                         if (cur.lastpos() != 0) {
1403                                 cursorLeft(cur);
1404                                 breakParagraph(cur);
1405                         }
1406
1407                         setLayout(cur, tclass.defaultLayoutName());
1408                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1409                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1410                         cur.posRight();
1411                 } else {
1412                         lyxerr << "Non-existent float type: "
1413                                << to_utf8(cmd.argument()) << endl;
1414                 }
1415                 break;
1416         }
1417
1418         case LFUN_CHANGE_ACCEPT: {
1419                 acceptChange(cur);
1420                 break;
1421         }
1422
1423         case LFUN_CHANGE_REJECT: {
1424                 rejectChange(cur);
1425                 break;
1426         }
1427
1428         case LFUN_THESAURUS_ENTRY: {
1429                 docstring arg = cmd.argument();
1430                 if (arg.empty()) {
1431                         arg = cur.selectionAsString(false);
1432                         // FIXME
1433                         if (arg.size() > 100 || arg.empty()) {
1434                                 // Get word or selection
1435                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1436                                 arg = cur.selectionAsString(false);
1437                         }
1438                 }
1439                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1440                 break;
1441         }
1442
1443         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1444                 // Given data, an encoding of the ParagraphParameters
1445                 // generated in the Paragraph dialog, this function sets
1446                 // the current paragraph appropriately.
1447                 istringstream is(to_utf8(cmd.argument()));
1448                 LyXLex lex(0, 0);
1449                 lex.setStream(is);
1450                 ParagraphParameters params;
1451                 params.read(lex);
1452                 // FIXME UNICODE
1453                 setParagraph(cur,
1454                              params.spacing(),
1455                              params.align(),
1456                              to_ascii(params.labelWidthString()),
1457                              params.noindent());
1458                 cur.message(_("Paragraph layout set"));
1459                 break;
1460         }
1461
1462         case LFUN_ESCAPE:
1463                 if (cur.selection()) {
1464                         cur.selection() = false;
1465                 } else {
1466                         cur.undispatched();
1467                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1468                 }
1469                 break;
1470
1471         default:
1472                 lyxerr[Debug::ACTION]
1473                         << BOOST_CURRENT_FUNCTION
1474                         << ": Command " << cmd
1475                         << " not DISPATCHED by LyXText" << endl;
1476                 cur.undispatched();
1477                 break;
1478         }
1479
1480         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1481         if (singleParUpdate)
1482                 // Inserting characters does not change par height
1483                 if (cur.bottom().paragraph().dim().height()
1484                     == olddim.height()) {
1485                         // if so, update _only_ this paragraph
1486                         cur.updateFlags(Update::SinglePar |
1487                                 Update::FitCursor |
1488                                 Update::MultiParSel);
1489                         return;
1490                 } else
1491                         needsUpdate = true;
1492         if (!needsUpdate
1493             && &oldTopSlice.inset() == &cur.inset()
1494             && oldTopSlice.idx() == cur.idx()
1495             && !sel
1496             && !cur.selection())
1497                 cur.noUpdate();
1498         else
1499                 cur.updateFlags(Update::Force | Update::FitCursor);
1500 }
1501
1502
1503 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1504                         FuncStatus & flag) const
1505 {
1506         BOOST_ASSERT(cur.text() == this);
1507
1508         LyXFont const & font = real_current_font;
1509         bool enable = true;
1510         InsetBase::Code code = InsetBase::NO_CODE;
1511
1512         switch (cmd.action) {
1513
1514         case LFUN_DEPTH_DECREMENT:
1515                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1516                 break;
1517
1518         case LFUN_DEPTH_INCREMENT:
1519                 enable = changeDepthAllowed(cur, INC_DEPTH);
1520                 break;
1521
1522         case LFUN_APPENDIX:
1523                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1524                 return true;
1525
1526         case LFUN_BIBITEM_INSERT:
1527                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1528                 break;
1529
1530         case LFUN_DIALOG_SHOW_NEW_INSET:
1531                 if (cmd.argument() == "bibitem")
1532                         code = InsetBase::BIBITEM_CODE;
1533                 else if (cmd.argument() == "bibtex")
1534                         code = InsetBase::BIBTEX_CODE;
1535                 else if (cmd.argument() == "box")
1536                         code = InsetBase::BOX_CODE;
1537                 else if (cmd.argument() == "branch")
1538                         code = InsetBase::BRANCH_CODE;
1539                 else if (cmd.argument() == "citation")
1540                         code = InsetBase::CITE_CODE;
1541                 else if (cmd.argument() == "ert")
1542                         code = InsetBase::ERT_CODE;
1543                 else if (cmd.argument() == "external")
1544                         code = InsetBase::EXTERNAL_CODE;
1545                 else if (cmd.argument() == "float")
1546                         code = InsetBase::FLOAT_CODE;
1547                 else if (cmd.argument() == "graphics")
1548                         code = InsetBase::GRAPHICS_CODE;
1549                 else if (cmd.argument() == "include")
1550                         code = InsetBase::INCLUDE_CODE;
1551                 else if (cmd.argument() == "index")
1552                         code = InsetBase::INDEX_CODE;
1553                 else if (cmd.argument() == "label")
1554                         code = InsetBase::LABEL_CODE;
1555                 else if (cmd.argument() == "note")
1556                         code = InsetBase::NOTE_CODE;
1557                 else if (cmd.argument() == "ref")
1558                         code = InsetBase::REF_CODE;
1559                 else if (cmd.argument() == "toc")
1560                         code = InsetBase::TOC_CODE;
1561                 else if (cmd.argument() == "url")
1562                         code = InsetBase::URL_CODE;
1563                 else if (cmd.argument() == "vspace")
1564                         code = InsetBase::VSPACE_CODE;
1565                 else if (cmd.argument() == "wrap")
1566                         code = InsetBase::WRAP_CODE;
1567                 break;
1568
1569         case LFUN_ERT_INSERT:
1570                 code = InsetBase::ERT_CODE;
1571                 break;
1572         case LFUN_FOOTNOTE_INSERT:
1573                 code = InsetBase::FOOT_CODE;
1574                 break;
1575         case LFUN_TABULAR_INSERT:
1576                 code = InsetBase::TABULAR_CODE;
1577                 break;
1578         case LFUN_MARGINALNOTE_INSERT:
1579                 code = InsetBase::MARGIN_CODE;
1580                 break;
1581         case LFUN_FLOAT_INSERT:
1582         case LFUN_FLOAT_WIDE_INSERT:
1583                 code = InsetBase::FLOAT_CODE;
1584                 break;
1585         case LFUN_WRAP_INSERT:
1586                 code = InsetBase::WRAP_CODE;
1587                 break;
1588         case LFUN_FLOAT_LIST:
1589                 code = InsetBase::FLOAT_LIST_CODE;
1590                 break;
1591 #if 0
1592         case LFUN_LIST_INSERT:
1593                 code = InsetBase::LIST_CODE;
1594                 break;
1595         case LFUN_THEOREM_INSERT:
1596                 code = InsetBase::THEOREM_CODE;
1597                 break;
1598 #endif
1599         case LFUN_CAPTION_INSERT:
1600                 code = InsetBase::CAPTION_CODE;
1601                 break;
1602         case LFUN_NOTE_INSERT:
1603                 code = InsetBase::NOTE_CODE;
1604                 break;
1605         case LFUN_CHARSTYLE_INSERT:
1606                 code = InsetBase::CHARSTYLE_CODE;
1607                 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1608                         enable = false;
1609                 break;
1610         case LFUN_BOX_INSERT:
1611                 code = InsetBase::BOX_CODE;
1612                 break;
1613         case LFUN_BRANCH_INSERT:
1614                 code = InsetBase::BRANCH_CODE;
1615                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1616                         enable = false;
1617                 break;
1618         case LFUN_LABEL_INSERT:
1619                 code = InsetBase::LABEL_CODE;
1620                 break;
1621         case LFUN_OPTIONAL_INSERT:
1622                 code = InsetBase::OPTARG_CODE;
1623                 enable = numberOfOptArgs(cur.paragraph())
1624                         < cur.paragraph().layout()->optionalargs;
1625                 break;
1626         case LFUN_ENVIRONMENT_INSERT:
1627                 code = InsetBase::BOX_CODE;
1628                 break;
1629         case LFUN_INDEX_INSERT:
1630                 code = InsetBase::INDEX_CODE;
1631                 break;
1632         case LFUN_INDEX_PRINT:
1633                 code = InsetBase::INDEX_PRINT_CODE;
1634                 break;
1635         case LFUN_TOC_INSERT:
1636                 code = InsetBase::TOC_CODE;
1637                 break;
1638         case LFUN_HTML_INSERT:
1639         case LFUN_URL_INSERT:
1640                 code = InsetBase::URL_CODE;
1641                 break;
1642         case LFUN_QUOTE_INSERT:
1643                 // always allow this, since we will inset a raw quote
1644                 // if an inset is not allowed.
1645                 break;
1646         case LFUN_HYPHENATION_POINT_INSERT:
1647         case LFUN_LIGATURE_BREAK_INSERT:
1648         case LFUN_HFILL_INSERT:
1649         case LFUN_MENU_SEPARATOR_INSERT:
1650         case LFUN_DOTS_INSERT:
1651         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1652                 code = InsetBase::SPECIALCHAR_CODE;
1653                 break;
1654         case LFUN_SPACE_INSERT:
1655                 // slight hack: we know this is allowed in math mode
1656                 if (cur.inTexted())
1657                         code = InsetBase::SPACE_CODE;
1658                 break;
1659
1660         case LFUN_INSET_MODIFY:
1661                 // We need to disable this, because we may get called for a
1662                 // tabular cell via
1663                 // InsetTabular::getStatus() -> InsetText::getStatus()
1664                 // and we don't handle LFUN_INSET_MODIFY.
1665                 enable = false;
1666                 break;
1667
1668         case LFUN_FONT_EMPH:
1669                 flag.setOnOff(font.emph() == LyXFont::ON);
1670                 return true;
1671
1672         case LFUN_FONT_NOUN:
1673                 flag.setOnOff(font.noun() == LyXFont::ON);
1674                 return true;
1675
1676         case LFUN_FONT_BOLD:
1677                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1678                 return true;
1679
1680         case LFUN_FONT_SANS:
1681                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1682                 return true;
1683
1684         case LFUN_FONT_ROMAN:
1685                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1686                 return true;
1687
1688         case LFUN_FONT_CODE:
1689                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1690                 return true;
1691
1692         case LFUN_CUT:
1693         case LFUN_COPY:
1694                 enable = cur.selection();
1695                 break;
1696
1697         case LFUN_PASTE:
1698                 enable = cap::numberOfSelections() > 0;
1699                 break;
1700
1701         case LFUN_PARAGRAPH_MOVE_UP:
1702                 enable = cur.pit() > 0 && !cur.selection();
1703                 break;
1704
1705         case LFUN_PARAGRAPH_MOVE_DOWN:
1706                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1707                 break;
1708
1709         case LFUN_INSET_DISSOLVE:
1710                 enable = !isMainText() && cur.inset().nargs() == 1;
1711                 break;
1712
1713         case LFUN_CHANGE_ACCEPT:
1714         case LFUN_CHANGE_REJECT:
1715                 enable = true; // FIXME: Change tracking (MG)
1716                 break;
1717
1718         case LFUN_WORD_DELETE_FORWARD:
1719         case LFUN_WORD_DELETE_BACKWARD:
1720         case LFUN_LINE_DELETE:
1721         case LFUN_WORD_FORWARD:
1722         case LFUN_WORD_BACKWARD:
1723         case LFUN_CHAR_FORWARD:
1724         case LFUN_CHAR_FORWARD_SELECT:
1725         case LFUN_CHAR_BACKWARD:
1726         case LFUN_CHAR_BACKWARD_SELECT:
1727         case LFUN_UP:
1728         case LFUN_UP_SELECT:
1729         case LFUN_DOWN:
1730         case LFUN_DOWN_SELECT:
1731         case LFUN_PARAGRAPH_UP_SELECT:
1732         case LFUN_PARAGRAPH_DOWN_SELECT:
1733         case LFUN_SCREEN_UP_SELECT:
1734         case LFUN_SCREEN_DOWN_SELECT:
1735         case LFUN_LINE_BEGIN_SELECT:
1736         case LFUN_LINE_END_SELECT:
1737         case LFUN_WORD_FORWARD_SELECT:
1738         case LFUN_WORD_BACKWARD_SELECT:
1739         case LFUN_WORD_SELECT:
1740         case LFUN_PARAGRAPH_UP:
1741         case LFUN_PARAGRAPH_DOWN:
1742         case LFUN_SCREEN_UP:
1743         case LFUN_SCREEN_DOWN:
1744         case LFUN_LINE_BEGIN:
1745         case LFUN_LINE_END:
1746         case LFUN_BREAK_LINE:
1747         case LFUN_CHAR_DELETE_FORWARD:
1748         case LFUN_DELETE_FORWARD_SKIP:
1749         case LFUN_CHAR_DELETE_BACKWARD:
1750         case LFUN_DELETE_BACKWARD_SKIP:
1751         case LFUN_BREAK_PARAGRAPH:
1752         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1753         case LFUN_BREAK_PARAGRAPH_SKIP:
1754         case LFUN_PARAGRAPH_SPACING:
1755         case LFUN_INSET_INSERT:
1756         case LFUN_WORD_UPCASE:
1757         case LFUN_WORD_LOWCASE:
1758         case LFUN_WORD_CAPITALIZE:
1759         case LFUN_CHARS_TRANSPOSE:
1760         case LFUN_SERVER_GET_XY:
1761         case LFUN_SERVER_SET_XY:
1762         case LFUN_SERVER_GET_FONT:
1763         case LFUN_SERVER_GET_LAYOUT:
1764         case LFUN_LAYOUT:
1765         case LFUN_CLIPBOARD_PASTE:
1766         case LFUN_PRIMARY_SELECTION_PASTE:
1767         case LFUN_DATE_INSERT:
1768         case LFUN_SELF_INSERT:
1769         case LFUN_LINE_INSERT:
1770         case LFUN_PAGEBREAK_INSERT:
1771         case LFUN_MATH_DISPLAY:
1772         case LFUN_MATH_IMPORT_SELECTION:
1773         case LFUN_MATH_MODE:
1774         case LFUN_MATH_MACRO:
1775         case LFUN_MATH_MATRIX:
1776         case LFUN_MATH_DELIM:
1777         case LFUN_MATH_BIGDELIM:
1778         case LFUN_MATH_SUBSCRIPT:
1779         case LFUN_MATH_SUPERSCRIPT:
1780         case LFUN_FONT_DEFAULT:
1781         case LFUN_FONT_UNDERLINE:
1782         case LFUN_FONT_SIZE:
1783         case LFUN_LANGUAGE:
1784         case LFUN_FONT_FREE_APPLY:
1785         case LFUN_FONT_FREE_UPDATE:
1786         case LFUN_LAYOUT_PARAGRAPH:
1787         case LFUN_PARAGRAPH_UPDATE:
1788         case LFUN_ACCENT_UMLAUT:
1789         case LFUN_ACCENT_CIRCUMFLEX:
1790         case LFUN_ACCENT_GRAVE:
1791         case LFUN_ACCENT_ACUTE:
1792         case LFUN_ACCENT_TILDE:
1793         case LFUN_ACCENT_CEDILLA:
1794         case LFUN_ACCENT_MACRON:
1795         case LFUN_ACCENT_DOT:
1796         case LFUN_ACCENT_UNDERDOT:
1797         case LFUN_ACCENT_UNDERBAR:
1798         case LFUN_ACCENT_CARON:
1799         case LFUN_ACCENT_SPECIAL_CARON:
1800         case LFUN_ACCENT_BREVE:
1801         case LFUN_ACCENT_TIE:
1802         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1803         case LFUN_ACCENT_CIRCLE:
1804         case LFUN_ACCENT_OGONEK:
1805         case LFUN_THESAURUS_ENTRY:
1806         case LFUN_PARAGRAPH_PARAMS_APPLY:
1807         case LFUN_ESCAPE:
1808         case LFUN_BUFFER_END:
1809         case LFUN_BUFFER_BEGIN:
1810         case LFUN_BUFFER_BEGIN_SELECT:
1811         case LFUN_BUFFER_END_SELECT:
1812         case LFUN_UNICODE_INSERT:
1813                 // these are handled in our dispatch()
1814                 enable = true;
1815                 break;
1816
1817         default:
1818                 return false;
1819         }
1820
1821         if (code != InsetBase::NO_CODE
1822             && (cur.empty() || !cur.inset().insetAllowed(code)))
1823                 enable = false;
1824
1825         flag.enabled(enable);
1826         return true;
1827 }
1828
1829
1830 } // namespace lyx