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