]> git.lyx.org Git - lyx.git/blob - src/text3.C
make macrfos readable again
[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_QUOTE_INSERT: {
897                 cap::replaceSelection(cur);
898                 Paragraph & par = cur.paragraph();
899                 pos_type pos = cur.pos();
900                 char_type c;
901                 if (pos == 0)
902                         c = ' ';
903                 else if (cur.prevInset() && cur.prevInset()->isSpace())
904                         c = ' ';
905                 else
906                         c = par.getChar(pos - 1);
907
908                 LyXLayout_ptr const & style = par.layout();
909
910                 BufferParams const & bufparams = bv->buffer()->params();
911                 if (!style->pass_thru
912                     && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
913                         string arg = to_utf8(cmd.argument());
914                         if (arg == "single")
915                                 cur.insert(new InsetQuotes(c,
916                                     bufparams.quotes_language,
917                                     InsetQuotes::SingleQ));
918                         else
919                                 cur.insert(new InsetQuotes(c,
920                                     bufparams.quotes_language,
921                                     InsetQuotes::DoubleQ));
922                         cur.posRight();
923                 }
924                 else
925                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
926                 break;
927         }
928
929         case LFUN_DATE_INSERT:
930                 if (cmd.argument().empty())
931                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
932                                 formatted_time(current_time())));
933                 else
934                         lyx::dispatch(FuncRequest(LFUN_SELF_INSERT,
935                                 formatted_time(current_time(), to_utf8(cmd.argument()))));
936                 break;
937
938         case LFUN_MOUSE_TRIPLE:
939                 if (cmd.button() == mouse_button::button1) {
940                         cursorHome(cur);
941                         cur.resetAnchor();
942                         cursorEnd(cur);
943                         cur.setSelection();
944                         bv->cursor() = cur;
945                         theSelection().haveSelection(cur.selection());
946                 }
947                 break;
948
949         case LFUN_MOUSE_DOUBLE:
950                 if (cmd.button() == mouse_button::button1) {
951                         selectWord(cur, WHOLE_WORD_STRICT);
952                         bv->cursor() = cur;
953                         theSelection().haveSelection(cur.selection());
954                 }
955                 break;
956
957         // Single-click on work area
958         case LFUN_MOUSE_PRESS: {
959                 // Right click on a footnote flag opens float menu
960                 if (cmd.button() == mouse_button::button3)
961                         cur.clearSelection();
962
963                 // Middle button press pastes if we have a selection
964                 // We do this here as if the selection was inside an inset
965                 // it could get cleared on the unlocking of the inset so
966                 // we have to check this first
967                 bool paste_internally = false;
968                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
969                         lyx::dispatch(FuncRequest(LFUN_COPY));
970                         paste_internally = true;
971                 }
972
973                 bv->mouseSetCursor(cur);
974
975                 // Insert primary selection with middle mouse
976                 // if there is a local selection in the current buffer,
977                 // insert this
978                 if (cmd.button() == mouse_button::button2) {
979                         if (paste_internally)
980                                 lyx::dispatch(FuncRequest(LFUN_PASTE));
981                         else
982                                 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
983                 }
984
985                 break;
986         }
987
988         case LFUN_MOUSE_MOTION: {
989                 // Only use motion with button 1
990                 //if (cmd.button() != mouse_button::button1)
991                 //      return false;
992
993                 // ignore motions deeper nested than the real anchor
994                 LCursor & bvcur = cur.bv().cursor();
995                 if (bvcur.anchor_.hasPart(cur)) {
996                         CursorSlice old = bvcur.top();
997
998                         int const wh = bv->workHeight();
999                         int const y = std::max(0, std::min(wh - 1, cmd.y));
1000
1001                         setCursorFromCoordinates(cur, cmd.x, y);
1002                         cur.x_target() = cmd.x;
1003                         if (cmd.y >= wh)
1004                                 cursorDown(cur);
1005                         else if (cmd.y < 0)
1006                                 cursorUp(cur);
1007                         // This is to allow jumping over large insets
1008                         if (cur.top() == old) {
1009                                 if (cmd.y >= wh)
1010                                         cursorDown(cur);
1011                                 else if (cmd.y < 0)
1012                                         cursorUp(cur);
1013                         }
1014
1015                         if (cur.top() == old)
1016                                 cur.noUpdate();
1017                         else {
1018                                 // don't set anchor_
1019                                 bvcur.setCursor(cur);
1020                                 bvcur.selection() = true;
1021                                 //lyxerr << "MOTION: " << bv->cursor() << endl;
1022                         }
1023
1024                 } else
1025                         cur.undispatched();
1026                 break;
1027         }
1028
1029         case LFUN_MOUSE_RELEASE: {
1030                 if (cmd.button() == mouse_button::button2)
1031                         break;
1032
1033                 // finish selection
1034                 if (cmd.button() == mouse_button::button1)
1035                         theSelection().haveSelection(cur.selection());
1036
1037                 bv->switchKeyMap();
1038                 break;
1039         }
1040
1041         case LFUN_SELF_INSERT: {
1042                 if (cmd.argument().empty())
1043                         break;
1044
1045                 // Automatically delete the currently selected
1046                 // text and replace it with what is being
1047                 // typed in now. Depends on lyxrc settings
1048                 // "auto_region_delete", which defaults to
1049                 // true (on).
1050
1051                 if (lyxrc.auto_region_delete) {
1052                         if (cur.selection())
1053                                 cutSelection(cur, false, false);
1054                         theSelection().haveSelection(false);
1055                 }
1056
1057                 cur.clearSelection();
1058                 LyXFont const old_font = real_current_font;
1059
1060                 docstring::const_iterator cit = cmd.argument().begin();
1061                 docstring::const_iterator end = cmd.argument().end();
1062                 for (; cit != end; ++cit)
1063 #if 0
1064                         bv->getIntl().getTransManager().
1065                                 translateAndInsert(*cit, this);
1066 #else
1067                         insertChar(bv->cursor(), *cit);
1068 #endif
1069
1070                 cur.resetAnchor();
1071                 moveCursor(cur, false);
1072                 break;
1073         }
1074
1075         case LFUN_URL_INSERT: {
1076                 InsetCommandParams p("url");
1077                 string const data = InsetCommandMailer::params2string("url", p);
1078                 bv->showInsetDialog("url", data, 0);
1079                 break;
1080         }
1081
1082         case LFUN_HTML_INSERT: {
1083                 InsetCommandParams p("htmlurl");
1084                 string const data = InsetCommandMailer::params2string("url", p);
1085                 bv->showInsetDialog("url", data, 0);
1086                 break;
1087         }
1088
1089         case LFUN_LABEL_INSERT: {
1090                 InsetCommandParams p("label");
1091                 // Try to generate a valid label
1092                 p["name"] = (cmd.argument().empty()) ?
1093                         // FIXME UNICODE
1094                         from_utf8(cur.getPossibleLabel()) :
1095                         cmd.argument();
1096                 string const data = InsetCommandMailer::params2string("label", p);
1097
1098                 if (cmd.argument().empty()) {
1099                         bv->showInsetDialog("label", data, 0);
1100                 } else {
1101                         FuncRequest fr(LFUN_INSET_INSERT, data);
1102                         dispatch(cur, fr);
1103                 }
1104                 break;
1105         }
1106
1107
1108 #if 0
1109         case LFUN_LIST_INSERT:
1110         case LFUN_THEOREM_INSERT:
1111         case LFUN_CAPTION_INSERT:
1112 #endif
1113         case LFUN_NOTE_INSERT:
1114         case LFUN_CHARSTYLE_INSERT:
1115         case LFUN_BOX_INSERT:
1116         case LFUN_BRANCH_INSERT:
1117         case LFUN_BIBITEM_INSERT:
1118         case LFUN_ERT_INSERT:
1119         case LFUN_FOOTNOTE_INSERT:
1120         case LFUN_MARGINALNOTE_INSERT:
1121         case LFUN_OPTIONAL_INSERT:
1122         case LFUN_ENVIRONMENT_INSERT:
1123                 // Open the inset, and move the current selection
1124                 // inside it.
1125                 doInsertInset(cur, this, cmd, true, true);
1126                 cur.posRight();
1127                 break;
1128
1129         case LFUN_TABULAR_INSERT:
1130                 // if there were no arguments, just open the dialog
1131                 if (doInsertInset(cur, this, cmd, false, true))
1132                         cur.posRight();
1133                 else
1134                         bv->showDialog("tabularcreate");
1135
1136                 break;
1137
1138         case LFUN_FLOAT_INSERT:
1139         case LFUN_FLOAT_WIDE_INSERT:
1140         case LFUN_WRAP_INSERT:
1141                 doInsertInset(cur, this, cmd, true, true);
1142                 cur.posRight();
1143                 // FIXME: the "Caption" name should not be hardcoded,
1144                 // but given by the float definition.
1145                 cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
1146                 break;
1147
1148         case LFUN_INDEX_INSERT: {
1149                 InsetBase * inset = createInset(&cur.bv(), cmd);
1150                 if (!inset)
1151                         break;
1152
1153                 recordUndo(cur);
1154                 cur.clearSelection();
1155                 insertInset(cur, inset);
1156                 inset->edit(cur, true);
1157                 cur.posRight();
1158                 break;
1159         }
1160
1161         case LFUN_INDEX_PRINT:
1162         case LFUN_TOC_INSERT:
1163         case LFUN_HFILL_INSERT:
1164         case LFUN_LINE_INSERT:
1165         case LFUN_PAGEBREAK_INSERT:
1166                 // do nothing fancy
1167                 doInsertInset(cur, this, cmd, false, false);
1168                 cur.posRight();
1169                 break;
1170
1171         case LFUN_DEPTH_DECREMENT:
1172                 changeDepth(cur, DEC_DEPTH);
1173                 break;
1174
1175         case LFUN_DEPTH_INCREMENT:
1176                 changeDepth(cur, INC_DEPTH);
1177                 break;
1178
1179         case LFUN_MATH_DISPLAY:
1180                 mathDispatch(cur, cmd, true);
1181                 break;
1182
1183         case LFUN_MATH_IMPORT_SELECTION:
1184         case LFUN_MATH_MODE:
1185                 if (cmd.argument() == "on")
1186                         // don't pass "on" as argument
1187                         mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1188                 else
1189                         mathDispatch(cur, cmd, false);
1190                 break;
1191
1192         case LFUN_MATH_MACRO:
1193                 if (cmd.argument().empty())
1194                         cur.errorMessage(from_utf8(N_("Missing argument")));
1195                 else {
1196                         string s = to_utf8(cmd.argument());
1197                         string const s1 = token(s, ' ', 1);
1198                         int const nargs = s1.empty() ? 0 : convert<int>(s1);
1199                         string const s2 = token(s, ' ', 2);
1200                         string const type = s2.empty() ? "newcommand" : s2;
1201                         cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, from_utf8(type)));
1202                         //cur.nextInset()->edit(cur, true);
1203                 }
1204                 break;
1205
1206         // passthrough hat and underscore outside mathed:
1207         case LFUN_MATH_SUBSCRIPT:
1208                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1209                 break;
1210         case LFUN_MATH_SUPERSCRIPT:
1211                 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1212                 break;
1213
1214         case LFUN_MATH_INSERT:
1215         case LFUN_MATH_MATRIX:
1216         case LFUN_MATH_DELIM:
1217         case LFUN_MATH_BIGDELIM: {
1218                 cur.insert(new InsetMathHull(hullSimple));
1219                 cur.dispatch(FuncRequest(LFUN_CHAR_FORWARD));
1220                 cur.dispatch(cmd);
1221                 break;
1222         }
1223
1224         case LFUN_FONT_EMPH: {
1225                 LyXFont font(LyXFont::ALL_IGNORE);
1226                 font.setEmph(LyXFont::TOGGLE);
1227                 toggleAndShow(cur, this, font);
1228                 break;
1229         }
1230
1231         case LFUN_FONT_BOLD: {
1232                 LyXFont font(LyXFont::ALL_IGNORE);
1233                 font.setSeries(LyXFont::BOLD_SERIES);
1234                 toggleAndShow(cur, this, font);
1235                 break;
1236         }
1237
1238         case LFUN_FONT_NOUN: {
1239                 LyXFont font(LyXFont::ALL_IGNORE);
1240                 font.setNoun(LyXFont::TOGGLE);
1241                 toggleAndShow(cur, this, font);
1242                 break;
1243         }
1244
1245         case LFUN_FONT_CODE: {
1246                 LyXFont font(LyXFont::ALL_IGNORE);
1247                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
1248                 toggleAndShow(cur, this, font);
1249                 break;
1250         }
1251
1252         case LFUN_FONT_SANS: {
1253                 LyXFont font(LyXFont::ALL_IGNORE);
1254                 font.setFamily(LyXFont::SANS_FAMILY);
1255                 toggleAndShow(cur, this, font);
1256                 break;
1257         }
1258
1259         case LFUN_FONT_ROMAN: {
1260                 LyXFont font(LyXFont::ALL_IGNORE);
1261                 font.setFamily(LyXFont::ROMAN_FAMILY);
1262                 toggleAndShow(cur, this, font);
1263                 break;
1264         }
1265
1266         case LFUN_FONT_DEFAULT: {
1267                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
1268                 toggleAndShow(cur, this, font);
1269                 break;
1270         }
1271
1272         case LFUN_FONT_UNDERLINE: {
1273                 LyXFont font(LyXFont::ALL_IGNORE);
1274                 font.setUnderbar(LyXFont::TOGGLE);
1275                 toggleAndShow(cur, this, font);
1276                 break;
1277         }
1278
1279         case LFUN_FONT_SIZE: {
1280                 LyXFont font(LyXFont::ALL_IGNORE);
1281                 font.setLyXSize(to_utf8(cmd.argument()));
1282                 toggleAndShow(cur, this, font);
1283                 break;
1284         }
1285
1286         case LFUN_LANGUAGE: {
1287                 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1288                 if (!lang)
1289                         break;
1290                 LyXFont font(LyXFont::ALL_IGNORE);
1291                 font.setLanguage(lang);
1292                 toggleAndShow(cur, this, font);
1293                 bv->switchKeyMap();
1294                 break;
1295         }
1296
1297         case LFUN_FONT_FREE_APPLY:
1298                 toggleAndShow(cur, this, freefont, toggleall);
1299                 cur.message(_("Character set"));
1300                 break;
1301
1302         // Set the freefont using the contents of \param data dispatched from
1303         // the frontends and apply it at the current cursor location.
1304         case LFUN_FONT_FREE_UPDATE: {
1305                 LyXFont font;
1306                 bool toggle;
1307                 if (bv_funcs::string2font(to_utf8(cmd.argument()), font, toggle)) {
1308                         freefont = font;
1309                         toggleall = toggle;
1310                         toggleAndShow(cur, this, freefont, toggleall);
1311                         cur.message(_("Character set"));
1312                 }
1313                 break;
1314         }
1315
1316         case LFUN_FINISHED_LEFT:
1317                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_LEFT:\n" << cur << endl;
1318                 break;
1319
1320         case LFUN_FINISHED_RIGHT:
1321                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_RIGHT:\n" << cur << endl;
1322                 ++cur.pos();
1323                 break;
1324
1325         case LFUN_FINISHED_UP:
1326                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_UP:\n" << cur << endl;
1327                 cursorUp(cur);
1328                 break;
1329
1330         case LFUN_FINISHED_DOWN:
1331                 lyxerr[Debug::DEBUG] << "handle LFUN_FINISHED_DOWN:\n" << cur << endl;
1332                 cursorDown(cur);
1333                 break;
1334
1335         case LFUN_LAYOUT_PARAGRAPH: {
1336                 string data;
1337                 params2string(cur.paragraph(), data);
1338                 data = "show\n" + data;
1339                 bv->showDialogWithData("paragraph", data);
1340                 break;
1341         }
1342
1343         case LFUN_PARAGRAPH_UPDATE: {
1344                 string data;
1345                 params2string(cur.paragraph(), data);
1346
1347                 // Will the paragraph accept changes from the dialog?
1348                 bool const accept = !cur.inset().forceDefaultParagraphs(cur.idx());
1349
1350                 data = "update " + convert<string>(accept) + '\n' + data;
1351                 bv->updateDialog("paragraph", data);
1352                 break;
1353         }
1354
1355         case LFUN_ACCENT_UMLAUT:
1356         case LFUN_ACCENT_CIRCUMFLEX:
1357         case LFUN_ACCENT_GRAVE:
1358         case LFUN_ACCENT_ACUTE:
1359         case LFUN_ACCENT_TILDE:
1360         case LFUN_ACCENT_CEDILLA:
1361         case LFUN_ACCENT_MACRON:
1362         case LFUN_ACCENT_DOT:
1363         case LFUN_ACCENT_UNDERDOT:
1364         case LFUN_ACCENT_UNDERBAR:
1365         case LFUN_ACCENT_CARON:
1366         case LFUN_ACCENT_SPECIAL_CARON:
1367         case LFUN_ACCENT_BREVE:
1368         case LFUN_ACCENT_TIE:
1369         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1370         case LFUN_ACCENT_CIRCLE:
1371         case LFUN_ACCENT_OGONEK:
1372                 theLyXFunc().handleKeyFunc(cmd.action);
1373                 if (!cmd.argument().empty())
1374                         // FIXME: Are all these characters encoded in one byte in utf8?
1375                         bv->getIntl().getTransManager()
1376                                 .translateAndInsert(cmd.argument()[0], this);
1377                 break;
1378
1379         case LFUN_FLOAT_LIST: {
1380                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1381                 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1382                         // not quite sure if we want this...
1383                         recordUndo(cur);
1384                         cur.clearSelection();
1385                         breakParagraph(cur);
1386
1387                         if (cur.lastpos() != 0) {
1388                                 cursorLeft(cur);
1389                                 breakParagraph(cur);
1390                         }
1391
1392                         setLayout(cur, tclass.defaultLayoutName());
1393                         setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1394                         insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1395                         cur.posRight();
1396                 } else {
1397                         lyxerr << "Non-existent float type: "
1398                                << to_utf8(cmd.argument()) << endl;
1399                 }
1400                 break;
1401         }
1402
1403         case LFUN_CHANGE_ACCEPT: {
1404                 acceptChange(cur);
1405                 break;
1406         }
1407
1408         case LFUN_CHANGE_REJECT: {
1409                 rejectChange(cur);
1410                 break;
1411         }
1412
1413         case LFUN_THESAURUS_ENTRY: {
1414                 docstring arg = cmd.argument();
1415                 if (arg.empty()) {
1416                         arg = cur.selectionAsString(false);
1417                         // FIXME
1418                         if (arg.size() > 100 || arg.empty()) {
1419                                 // Get word or selection
1420                                 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1421                                 arg = cur.selectionAsString(false);
1422                         }
1423                 }
1424                 bv->showDialogWithData("thesaurus", to_utf8(arg));
1425                 break;
1426         }
1427
1428         case LFUN_PARAGRAPH_PARAMS_APPLY: {
1429                 // Given data, an encoding of the ParagraphParameters
1430                 // generated in the Paragraph dialog, this function sets
1431                 // the current paragraph appropriately.
1432                 istringstream is(to_utf8(cmd.argument()));
1433                 LyXLex lex(0, 0);
1434                 lex.setStream(is);
1435                 ParagraphParameters params;
1436                 params.read(lex);
1437                 // FIXME UNICODE
1438                 setParagraph(cur,
1439                              params.spacing(),
1440                              params.align(),
1441                              to_ascii(params.labelWidthString()),
1442                              params.noindent());
1443                 cur.message(_("Paragraph layout set"));
1444                 break;
1445         }
1446
1447         case LFUN_ESCAPE:
1448                 if (cur.selection()) {
1449                         cur.selection() = false;
1450                 } else {
1451                         cur.undispatched();
1452                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1453                 }
1454                 break;
1455
1456         default:
1457                 lyxerr[Debug::ACTION]
1458                         << BOOST_CURRENT_FUNCTION
1459                         << ": Command " << cmd
1460                         << " not DISPATCHED by LyXText" << endl;
1461                 cur.undispatched();
1462                 break;
1463         }
1464
1465         needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1466         if (singleParUpdate)
1467                 // Inserting characters does not change par height
1468                 if (cur.bottom().paragraph().dim().height()
1469                     == olddim.height()) {
1470                         // if so, update _only_ this paragraph
1471                         cur.updateFlags(Update::SinglePar |
1472                                 Update::FitCursor |
1473                                 Update::MultiParSel);
1474                         return;
1475                 } else
1476                         needsUpdate = true;
1477         if (!needsUpdate
1478             && &oldTopSlice.inset() == &cur.inset()
1479             && oldTopSlice.idx() == cur.idx()
1480             && !sel
1481             && !cur.selection())
1482                 cur.noUpdate();
1483         else
1484                 cur.updateFlags(Update::Force | Update::FitCursor);
1485 }
1486
1487
1488 bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
1489                         FuncStatus & flag) const
1490 {
1491         BOOST_ASSERT(cur.text() == this);
1492
1493         LyXFont const & font = real_current_font;
1494         bool enable = true;
1495         InsetBase::Code code = InsetBase::NO_CODE;
1496
1497         switch (cmd.action) {
1498
1499         case LFUN_DEPTH_DECREMENT:
1500                 enable = changeDepthAllowed(cur, DEC_DEPTH);
1501                 break;
1502
1503         case LFUN_DEPTH_INCREMENT:
1504                 enable = changeDepthAllowed(cur, INC_DEPTH);
1505                 break;
1506
1507         case LFUN_APPENDIX:
1508                 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1509                 return true;
1510
1511         case LFUN_BIBITEM_INSERT:
1512                 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
1513                 break;
1514
1515         case LFUN_DIALOG_SHOW_NEW_INSET:
1516                 if (cmd.argument() == "bibitem")
1517                         code = InsetBase::BIBITEM_CODE;
1518                 else if (cmd.argument() == "bibtex")
1519                         code = InsetBase::BIBTEX_CODE;
1520                 else if (cmd.argument() == "box")
1521                         code = InsetBase::BOX_CODE;
1522                 else if (cmd.argument() == "branch")
1523                         code = InsetBase::BRANCH_CODE;
1524                 else if (cmd.argument() == "citation")
1525                         code = InsetBase::CITE_CODE;
1526                 else if (cmd.argument() == "ert")
1527                         code = InsetBase::ERT_CODE;
1528                 else if (cmd.argument() == "external")
1529                         code = InsetBase::EXTERNAL_CODE;
1530                 else if (cmd.argument() == "float")
1531                         code = InsetBase::FLOAT_CODE;
1532                 else if (cmd.argument() == "graphics")
1533                         code = InsetBase::GRAPHICS_CODE;
1534                 else if (cmd.argument() == "include")
1535                         code = InsetBase::INCLUDE_CODE;
1536                 else if (cmd.argument() == "index")
1537                         code = InsetBase::INDEX_CODE;
1538                 else if (cmd.argument() == "label")
1539                         code = InsetBase::LABEL_CODE;
1540                 else if (cmd.argument() == "note")
1541                         code = InsetBase::NOTE_CODE;
1542                 else if (cmd.argument() == "ref")
1543                         code = InsetBase::REF_CODE;
1544                 else if (cmd.argument() == "toc")
1545                         code = InsetBase::TOC_CODE;
1546                 else if (cmd.argument() == "url")
1547                         code = InsetBase::URL_CODE;
1548                 else if (cmd.argument() == "vspace")
1549                         code = InsetBase::VSPACE_CODE;
1550                 else if (cmd.argument() == "wrap")
1551                         code = InsetBase::WRAP_CODE;
1552                 break;
1553
1554         case LFUN_ERT_INSERT:
1555                 code = InsetBase::ERT_CODE;
1556                 break;
1557         case LFUN_FOOTNOTE_INSERT:
1558                 code = InsetBase::FOOT_CODE;
1559                 break;
1560         case LFUN_TABULAR_INSERT:
1561                 code = InsetBase::TABULAR_CODE;
1562                 break;
1563         case LFUN_MARGINALNOTE_INSERT:
1564                 code = InsetBase::MARGIN_CODE;
1565                 break;
1566         case LFUN_FLOAT_INSERT:
1567         case LFUN_FLOAT_WIDE_INSERT:
1568                 code = InsetBase::FLOAT_CODE;
1569                 break;
1570         case LFUN_WRAP_INSERT:
1571                 code = InsetBase::WRAP_CODE;
1572                 break;
1573         case LFUN_FLOAT_LIST:
1574                 code = InsetBase::FLOAT_LIST_CODE;
1575                 break;
1576 #if 0
1577         case LFUN_LIST_INSERT:
1578                 code = InsetBase::LIST_CODE;
1579                 break;
1580         case LFUN_THEOREM_INSERT:
1581                 code = InsetBase::THEOREM_CODE;
1582                 break;
1583 #endif
1584         case LFUN_CAPTION_INSERT:
1585                 code = InsetBase::CAPTION_CODE;
1586                 break;
1587         case LFUN_NOTE_INSERT:
1588                 code = InsetBase::NOTE_CODE;
1589                 break;
1590         case LFUN_CHARSTYLE_INSERT:
1591                 code = InsetBase::CHARSTYLE_CODE;
1592                 if (cur.buffer().params().getLyXTextClass().charstyles().empty())
1593                         enable = false;
1594                 break;
1595         case LFUN_BOX_INSERT:
1596                 code = InsetBase::BOX_CODE;
1597                 break;
1598         case LFUN_BRANCH_INSERT:
1599                 code = InsetBase::BRANCH_CODE;
1600                 if (cur.buffer().getMasterBuffer()->params().branchlist().empty())
1601                         enable = false;
1602                 break;
1603         case LFUN_LABEL_INSERT:
1604                 code = InsetBase::LABEL_CODE;
1605                 break;
1606         case LFUN_OPTIONAL_INSERT:
1607                 code = InsetBase::OPTARG_CODE;
1608                 enable = numberOfOptArgs(cur.paragraph())
1609                         < cur.paragraph().layout()->optionalargs;
1610                 break;
1611         case LFUN_ENVIRONMENT_INSERT:
1612                 code = InsetBase::BOX_CODE;
1613                 break;
1614         case LFUN_INDEX_INSERT:
1615                 code = InsetBase::INDEX_CODE;
1616                 break;
1617         case LFUN_INDEX_PRINT:
1618                 code = InsetBase::INDEX_PRINT_CODE;
1619                 break;
1620         case LFUN_TOC_INSERT:
1621                 code = InsetBase::TOC_CODE;
1622                 break;
1623         case LFUN_HTML_INSERT:
1624         case LFUN_URL_INSERT:
1625                 code = InsetBase::URL_CODE;
1626                 break;
1627         case LFUN_QUOTE_INSERT:
1628                 // always allow this, since we will inset a raw quote
1629                 // if an inset is not allowed.
1630                 break;
1631         case LFUN_HYPHENATION_POINT_INSERT:
1632         case LFUN_LIGATURE_BREAK_INSERT:
1633         case LFUN_HFILL_INSERT:
1634         case LFUN_MENU_SEPARATOR_INSERT:
1635         case LFUN_DOTS_INSERT:
1636         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1637                 code = InsetBase::SPECIALCHAR_CODE;
1638                 break;
1639         case LFUN_SPACE_INSERT:
1640                 // slight hack: we know this is allowed in math mode
1641                 if (cur.inTexted())
1642                         code = InsetBase::SPACE_CODE;
1643                 break;
1644
1645         case LFUN_INSET_MODIFY:
1646                 // We need to disable this, because we may get called for a
1647                 // tabular cell via
1648                 // InsetTabular::getStatus() -> InsetText::getStatus()
1649                 // and we don't handle LFUN_INSET_MODIFY.
1650                 enable = false;
1651                 break;
1652
1653         case LFUN_FONT_EMPH:
1654                 flag.setOnOff(font.emph() == LyXFont::ON);
1655                 return true;
1656
1657         case LFUN_FONT_NOUN:
1658                 flag.setOnOff(font.noun() == LyXFont::ON);
1659                 return true;
1660
1661         case LFUN_FONT_BOLD:
1662                 flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
1663                 return true;
1664
1665         case LFUN_FONT_SANS:
1666                 flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
1667                 return true;
1668
1669         case LFUN_FONT_ROMAN:
1670                 flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
1671                 return true;
1672
1673         case LFUN_FONT_CODE:
1674                 flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
1675                 return true;
1676
1677         case LFUN_CUT:
1678         case LFUN_COPY:
1679                 enable = cur.selection();
1680                 break;
1681
1682         case LFUN_PASTE:
1683                 enable = cap::numberOfSelections() > 0;
1684                 break;
1685
1686         case LFUN_PARAGRAPH_MOVE_UP:
1687                 enable = cur.pit() > 0 && !cur.selection();
1688                 break;
1689
1690         case LFUN_PARAGRAPH_MOVE_DOWN:
1691                 enable = cur.pit() < cur.lastpit() && !cur.selection();
1692                 break;
1693
1694         case LFUN_INSET_DISSOLVE:
1695                 enable = !isMainText() && cur.inset().nargs() == 1;
1696                 break;
1697
1698         case LFUN_CHANGE_ACCEPT:
1699         case LFUN_CHANGE_REJECT:
1700                 enable = true; // FIXME: Change tracking (MG)
1701                 break;
1702
1703         case LFUN_WORD_DELETE_FORWARD:
1704         case LFUN_WORD_DELETE_BACKWARD:
1705         case LFUN_LINE_DELETE:
1706         case LFUN_WORD_FORWARD:
1707         case LFUN_WORD_BACKWARD:
1708         case LFUN_CHAR_FORWARD:
1709         case LFUN_CHAR_FORWARD_SELECT:
1710         case LFUN_CHAR_BACKWARD:
1711         case LFUN_CHAR_BACKWARD_SELECT:
1712         case LFUN_UP:
1713         case LFUN_UP_SELECT:
1714         case LFUN_DOWN:
1715         case LFUN_DOWN_SELECT:
1716         case LFUN_PARAGRAPH_UP_SELECT:
1717         case LFUN_PARAGRAPH_DOWN_SELECT:
1718         case LFUN_SCREEN_UP_SELECT:
1719         case LFUN_SCREEN_DOWN_SELECT:
1720         case LFUN_LINE_BEGIN_SELECT:
1721         case LFUN_LINE_END_SELECT:
1722         case LFUN_WORD_FORWARD_SELECT:
1723         case LFUN_WORD_BACKWARD_SELECT:
1724         case LFUN_WORD_SELECT:
1725         case LFUN_PARAGRAPH_UP:
1726         case LFUN_PARAGRAPH_DOWN:
1727         case LFUN_SCREEN_UP:
1728         case LFUN_SCREEN_DOWN:
1729         case LFUN_LINE_BEGIN:
1730         case LFUN_LINE_END:
1731         case LFUN_BREAK_LINE:
1732         case LFUN_CHAR_DELETE_FORWARD:
1733         case LFUN_DELETE_FORWARD_SKIP:
1734         case LFUN_CHAR_DELETE_BACKWARD:
1735         case LFUN_DELETE_BACKWARD_SKIP:
1736         case LFUN_BREAK_PARAGRAPH:
1737         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1738         case LFUN_BREAK_PARAGRAPH_SKIP:
1739         case LFUN_PARAGRAPH_SPACING:
1740         case LFUN_INSET_INSERT:
1741         case LFUN_WORD_UPCASE:
1742         case LFUN_WORD_LOWCASE:
1743         case LFUN_WORD_CAPITALIZE:
1744         case LFUN_CHARS_TRANSPOSE:
1745         case LFUN_SERVER_GET_XY:
1746         case LFUN_SERVER_SET_XY:
1747         case LFUN_SERVER_GET_FONT:
1748         case LFUN_SERVER_GET_LAYOUT:
1749         case LFUN_LAYOUT:
1750         case LFUN_CLIPBOARD_PASTE:
1751         case LFUN_PRIMARY_SELECTION_PASTE:
1752         case LFUN_DATE_INSERT:
1753         case LFUN_SELF_INSERT:
1754         case LFUN_LINE_INSERT:
1755         case LFUN_PAGEBREAK_INSERT:
1756         case LFUN_MATH_DISPLAY:
1757         case LFUN_MATH_IMPORT_SELECTION:
1758         case LFUN_MATH_MODE:
1759         case LFUN_MATH_MACRO:
1760         case LFUN_MATH_MATRIX:
1761         case LFUN_MATH_DELIM:
1762         case LFUN_MATH_BIGDELIM:
1763         case LFUN_MATH_SUBSCRIPT:
1764         case LFUN_MATH_SUPERSCRIPT:
1765         case LFUN_FONT_DEFAULT:
1766         case LFUN_FONT_UNDERLINE:
1767         case LFUN_FONT_SIZE:
1768         case LFUN_LANGUAGE:
1769         case LFUN_FONT_FREE_APPLY:
1770         case LFUN_FONT_FREE_UPDATE:
1771         case LFUN_LAYOUT_PARAGRAPH:
1772         case LFUN_PARAGRAPH_UPDATE:
1773         case LFUN_ACCENT_UMLAUT:
1774         case LFUN_ACCENT_CIRCUMFLEX:
1775         case LFUN_ACCENT_GRAVE:
1776         case LFUN_ACCENT_ACUTE:
1777         case LFUN_ACCENT_TILDE:
1778         case LFUN_ACCENT_CEDILLA:
1779         case LFUN_ACCENT_MACRON:
1780         case LFUN_ACCENT_DOT:
1781         case LFUN_ACCENT_UNDERDOT:
1782         case LFUN_ACCENT_UNDERBAR:
1783         case LFUN_ACCENT_CARON:
1784         case LFUN_ACCENT_SPECIAL_CARON:
1785         case LFUN_ACCENT_BREVE:
1786         case LFUN_ACCENT_TIE:
1787         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1788         case LFUN_ACCENT_CIRCLE:
1789         case LFUN_ACCENT_OGONEK:
1790         case LFUN_THESAURUS_ENTRY:
1791         case LFUN_PARAGRAPH_PARAMS_APPLY:
1792         case LFUN_ESCAPE:
1793         case LFUN_BUFFER_END:
1794         case LFUN_BUFFER_BEGIN:
1795         case LFUN_BUFFER_BEGIN_SELECT:
1796         case LFUN_BUFFER_END_SELECT:
1797                 // these are handled in our dispatch()
1798                 enable = true;
1799                 break;
1800
1801         default:
1802                 return false;
1803         }
1804
1805         if (code != InsetBase::NO_CODE
1806             && (cur.empty() || !cur.inset().insetAllowed(code)))
1807                 enable = false;
1808
1809         flag.enabled(enable);
1810         return true;
1811 }
1812
1813
1814 } // namespace lyx