]> git.lyx.org Git - lyx.git/blob - src/text3.C
e48cb3acaad38f46fe4a3099e8b77a16b1ce34fe
[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 "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "cursor.h"
24 #include "debug.h"
25 #include "dispatchresult.h"
26 #include "factory.h"
27 #include "funcrequest.h"
28 #include "gettext.h"
29 #include "intl.h"
30 #include "language.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphParameters.h"
36 #include "undo.h"
37 #include "vspace.h"
38
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41
42 #include "insets/insetcommand.h"
43 #include "insets/insetnewline.h"
44 #include "insets/insetspecialchar.h"
45 #include "insets/insettext.h"
46
47 #include "support/lstrings.h"
48 #include "support/tostr.h"
49 #include "support/std_sstream.h"
50
51 #include "mathed/formulabase.h"
52
53 #include <clocale>
54
55 using bv_funcs::replaceSelection;
56
57 using lyx::pos_type;
58
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
61
62 using std::endl;
63 using std::find;
64 using std::string;
65 using std::istringstream;
66 using std::vector;
67
68
69 extern string current_layout;
70
71 // the selection possible is needed, that only motion events are
72 // used, where the bottom press event was on the drawing area too
73 bool selection_possible = false;
74
75
76 namespace {
77
78         // globals...
79         LyXFont freefont(LyXFont::ALL_IGNORE);
80         bool toggleall = false;
81
82
83         void toggleAndShow(BufferView * bv, LyXText * text,
84                 LyXFont const & font, bool toggleall = true)
85         {
86                 if (!bv->available())
87                         return;
88
89                 text->toggleFree(font, toggleall);
90                 bv->update();
91
92                 if (font.language() != ignore_language ||
93                                 font.number() != LyXFont::IGNORE) {
94                         CursorSlice & cur = text->cursor();
95                         Paragraph & par = *text->cursorPar();
96                         text->bidi.computeTables(par, *bv->buffer(),
97                                 *par.getRow(cur.pos()));
98                         if (cur.boundary() !=
99                                         text->bidi.isBoundary(*bv->buffer(), par,
100                                                         cur.pos(),
101                                                         text->real_current_font))
102                                 text->setCursor(cur.par(), cur.pos(),
103                                                 false, !cur.boundary());
104                 }
105         }
106
107
108         /// Apply the contents of freefont at the current cursor location.
109         void apply_freefont(BufferView * bv, LyXText * text)
110         {
111                 toggleAndShow(bv, text, freefont, toggleall);
112                 bv->owner()->view_state_changed();
113                 bv->owner()->message(_("Character set"));
114         }
115
116
117         /** Set the freefont using the contents of \param data dispatched from
118          *  the frontends and apply it at the current cursor location.
119          */
120         void update_and_apply_freefont(BufferView * bv, LyXText * text,
121                 string const & data)
122         {
123                 LyXFont font;
124                 bool toggle;
125                 if (bv_funcs::string2font(data, font, toggle)) {
126                         freefont = font;
127                         toggleall = toggle;
128                         apply_freefont(bv, text);
129                 }
130         }
131
132
133         void emph(BufferView * bv, LyXText * text)
134         {
135                 LyXFont font(LyXFont::ALL_IGNORE);
136                 font.setEmph(LyXFont::TOGGLE);
137                 toggleAndShow(bv, text, font);
138         }
139
140
141         void bold(BufferView * bv, LyXText * text)
142         {
143                 LyXFont font(LyXFont::ALL_IGNORE);
144                 font.setSeries(LyXFont::BOLD_SERIES);
145                 toggleAndShow(bv, text, font);
146         }
147
148
149         void noun(BufferView * bv, LyXText * text)
150         {
151                 LyXFont font(LyXFont::ALL_IGNORE);
152                 font.setNoun(LyXFont::TOGGLE);
153                 toggleAndShow(bv, text, font);
154         }
155
156
157         void lang(BufferView * bv, string const & l, LyXText * text)
158         {
159                 Language const * lang = languages.getLanguage(l);
160                 if (!lang)
161                         return;
162
163                 LyXFont font(LyXFont::ALL_IGNORE);
164                 font.setLanguage(lang);
165                 toggleAndShow(bv, text, font);
166         }
167
168
169         void code(BufferView * bv, LyXText * text)
170         {
171                 LyXFont font(LyXFont::ALL_IGNORE);
172                 font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
173                 toggleAndShow(bv, text, font);
174         }
175
176
177         void sans(BufferView * bv, LyXText * text)
178         {
179                 LyXFont font(LyXFont::ALL_IGNORE);
180                 font.setFamily(LyXFont::SANS_FAMILY);
181                 toggleAndShow(bv, text, font);
182         }
183
184
185         void roman(BufferView * bv, LyXText * text)
186         {
187                 LyXFont font(LyXFont::ALL_IGNORE);
188                 font.setFamily(LyXFont::ROMAN_FAMILY);
189                 toggleAndShow(bv, text, font);
190         }
191
192
193         void styleReset(BufferView * bv, LyXText * text)
194         {
195                 LyXFont font(LyXFont::ALL_INHERIT, ignore_language);
196                 toggleAndShow(bv, text, font);
197         }
198
199
200         void underline(BufferView * bv, LyXText * text)
201         {
202                 LyXFont font(LyXFont::ALL_IGNORE);
203                 font.setUnderbar(LyXFont::TOGGLE);
204                 toggleAndShow(bv, text, font);
205         }
206
207
208         void fontSize(BufferView * bv, string const & size, LyXText * text)
209         {
210                 LyXFont font(LyXFont::ALL_IGNORE);
211                 font.setLyXSize(size);
212                 toggleAndShow(bv, text, font);
213         }
214
215
216         void moveCursor(BufferView * bv, bool selecting)
217         {
218                 LyXText * lt = bv->getLyXText();
219
220                 if (selecting || bv->selection().mark())
221                         lt->setSelection();
222
223                 if (!bv->selection().set())
224                         bv->haveSelection(false);
225                 bv->switchKeyMap();
226         }
227
228
229         void finishChange(BufferView * bv, bool selecting = false)
230         {
231                 finishUndo();
232                 moveCursor(bv, selecting);
233                 bv->owner()->view_state_changed();
234         }
235
236 } // anon namespace
237
238
239 namespace bv_funcs {
240
241 string const freefont2string()
242 {
243         string data;
244         if (font2string(freefont, toggleall, data))
245                 return data;
246         return string();
247 }
248
249 }
250
251
252 InsetOld * LyXText::checkInsetHit(int x, int y)
253 {
254         ParagraphList::iterator pit;
255         ParagraphList::iterator end;
256
257         getParsInRange(paragraphs(),
258                        bv()->top_y() - yo_,
259                        bv()->top_y() - yo_ + bv()->workHeight(),
260                        pit, end);
261
262         lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
263         for ( ; pit != end; ++pit) {
264                 InsetList::iterator iit = pit->insetlist.begin();
265                 InsetList::iterator iend = pit->insetlist.end();
266                 for ( ; iit != iend; ++iit) {
267                         InsetOld * inset = iit->inset;
268                         //lyxerr << "examining inset " << inset
269                         //      << " xy: " << inset->x() << "/" << inset->y()
270                         //      << " x: " << inset->x() << "..." << inset->x() + inset->width()
271                         //      << " y: " << inset->y() - inset->ascent() << "..."
272                         //      << inset->y() + inset->descent()
273                         //      << endl;
274                         if (x >= inset->x()
275                             && x <= inset->x() + inset->width()
276                             && y >= inset->y() - inset->ascent()
277                             && y <= inset->y() + inset->descent())
278                         {
279                                 lyxerr << "Hit inset: " << inset << endl;
280                                 return inset;
281                         }
282                 }
283         }
284         lyxerr << "No inset hit. " << endl;
285         return 0;
286 }
287
288
289 bool LyXText::gotoNextInset(vector<InsetOld::Code> const & codes,
290                             string const & contents)
291 {
292         ParagraphList::iterator end = paragraphs().end();
293         ParagraphList::iterator pit = cursorPar();
294         pos_type pos = cursor().pos();
295
296         InsetOld * inset;
297         do {
298                 if (pos + 1 < pit->size()) {
299                         ++pos;
300                 } else  {
301                         ++pit;
302                         pos = 0;
303                 }
304
305         } while (pit != end &&
306                  !(pit->isInset(pos) &&
307                    (inset = pit->getInset(pos)) != 0 &&
308                    find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
309                    (contents.empty() ||
310                     static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
311                     == contents)));
312
313         if (pit == end)
314                 return false;
315
316         setCursor(parOffset(pit), pos, false);
317         return true;
318 }
319
320
321 void LyXText::gotoInset(vector<InsetOld::Code> const & codes,
322                         bool same_content)
323 {
324         clearSelection();
325
326         string contents;
327         if (same_content && cursor().pos() < cursorPar()->size()
328             && cursorPar()->isInset(cursor().pos())) {
329                 InsetOld const * inset = cursorPar()->getInset(cursor().pos());
330                 if (find(codes.begin(), codes.end(), inset->lyxCode())
331                     != codes.end())
332                         contents = static_cast<InsetCommand const *>(inset)->getContents();
333         }
334
335         if (!gotoNextInset(codes, contents)) {
336                 if (cursor().pos() || cursorPar() != paragraphs().begin()) {
337                         CursorSlice tmp = cursor();
338                         cursor().par(0);
339                         cursor().pos(0);
340                         if (!gotoNextInset(codes, contents)) {
341                                 cursor() = tmp;
342                                 bv()->owner()->message(_("No more insets"));
343                         }
344                 } else {
345                         bv()->owner()->message(_("No more insets"));
346                 }
347         }
348         bv()->update();
349         bv()->resetAnchor();
350 }
351
352
353 void LyXText::gotoInset(InsetOld::Code code, bool same_content)
354 {
355         gotoInset(vector<InsetOld::Code>(1, code), same_content);
356 }
357
358
359 void LyXText::cursorPrevious()
360 {
361
362         RowList::iterator crit = cursorRow();
363         ParagraphList::iterator cpar = cursorPar();
364
365         int x = bv()->x_target() - xo_;
366         int y = bv()->top_y() - yo_;
367         setCursorFromCoordinates(x, y);
368
369         if (cpar == cursorPar() && crit == cursorRow()) {
370                 // we have a row which is taller than the workarea. The
371                 // simplest solution is to move to the previous row instead.
372                 cursorUp(true);
373         }
374
375         bv()->updateScrollbar();
376         finishUndo();
377 }
378
379
380 void LyXText::cursorNext()
381 {
382         RowList::iterator crit = cursorRow();
383         ParagraphList::iterator cpar = cursorPar();
384
385         int x = bv()->x_target() - xo_;
386         int y = bv()->top_y() + bv()->workHeight() - yo_;
387         setCursorFromCoordinates(x, y);
388
389         if (cpar == cursorPar() && crit == cursorRow()) {
390                 // we have a row which is taller than the workarea. The
391                 // simplest solution is to move to the next row instead.
392                 cursorDown(true);
393         }
394
395         bv()->updateScrollbar();
396         finishUndo();
397 }
398
399
400 namespace {
401
402 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
403 {
404         bv->update();
405         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
406         replaceSelection(lt);
407         if (!bv->insertInset(new_inset))
408                 delete new_inset;
409         else
410                 bv->update();
411 }
412
413
414 void doInsertInset(FuncRequest const & cmd, bool edit, bool pastesel)
415 {
416         InsetOld * inset = createInset(cmd);
417         if (!inset)
418                 return;
419
420         BufferView * bv = cmd.view();
421
422         bool gotsel = false;
423         if (bv->selection().set()) {
424                 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
425                 gotsel = true;
426         }
427         if (bv->insertInset(inset)) {
428                 if (edit)
429                         inset->edit(bv, true);
430                 if (gotsel && pastesel)
431                         bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
432         } else
433                 delete inset;
434 }
435
436 } // anon namespace
437
438
439 void LyXText::number()
440 {
441         LyXFont font(LyXFont::ALL_IGNORE);
442         font.setNumber(LyXFont::TOGGLE);
443         toggleAndShow(bv(), this, font);
444 }
445
446
447 bool LyXText::rtl() const
448 {
449         return cursorPar()->isRightToLeftPar(bv()->buffer()->params());
450 }
451
452
453 DispatchResult LyXText::dispatch(FuncRequest const & cmd)
454 {
455         //lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
456
457         BufferView * bv = cmd.view();
458
459         switch (cmd.action) {
460
461         case LFUN_APPENDIX: {
462                 ParagraphList::iterator pit = cursorPar();
463                 bool start = !pit->params().startOfAppendix();
464
465                 // ensure that we have only one start_of_appendix in this document
466                 ParagraphList::iterator tmp = paragraphs().begin();
467                 ParagraphList::iterator end = paragraphs().end();
468
469                 for (; tmp != end; ++tmp) {
470                         if (tmp->params().startOfAppendix()) {
471                                 recUndo(parOffset(tmp));
472                                 tmp->params().startOfAppendix(false);
473                                 redoParagraph(tmp);
474                                 break;
475                         }
476                 }
477
478                 recUndo(parOffset(pit));
479                 pit->params().startOfAppendix(start);
480
481                 // we can set the refreshing parameters now
482                 updateCounters();
483                 redoParagraph(cursorPar());
484                 bv->update();
485                 break;
486         }
487
488         case LFUN_DELETE_WORD_FORWARD:
489                 clearSelection();
490                 deleteWordForward();
491                 finishChange(bv);
492                 break;
493
494         case LFUN_DELETE_WORD_BACKWARD:
495                 clearSelection();
496                 deleteWordBackward();
497                 finishChange(bv);
498                 break;
499
500         case LFUN_DELETE_LINE_FORWARD:
501                 clearSelection();
502                 deleteLineForward();
503                 finishChange(bv);
504                 break;
505
506         case LFUN_WORDRIGHT:
507                 if (!bv->selection().mark())
508                         clearSelection();
509                 if (rtl())
510                         cursorLeftOneWord();
511                 else
512                         cursorRightOneWord();
513                 finishChange(bv);
514                 break;
515
516         case LFUN_WORDLEFT:
517                 if (!bv->selection().mark())
518                         clearSelection();
519                 if (rtl())
520                         cursorRightOneWord();
521                 else
522                         cursorLeftOneWord();
523                 finishChange(bv);
524                 break;
525
526         case LFUN_BEGINNINGBUF:
527                 if (!bv->selection().mark())
528                         clearSelection();
529                 cursorTop();
530                 finishChange(bv);
531                 break;
532
533         case LFUN_ENDBUF:
534                 if (bv->selection().mark())
535                         clearSelection();
536                 cursorBottom();
537                 finishChange(bv);
538                 break;
539
540         case LFUN_RIGHTSEL:
541                 if (!bv->selection().set())
542                         bv->resetAnchor();
543                 if (rtl())
544                         cursorLeft(bv);
545                 else
546                         cursorRight(bv);
547                 finishChange(bv, true);
548                 break;
549
550         case LFUN_LEFTSEL:
551                 if (!bv->selection().set())
552                         bv->resetAnchor();
553                 if (rtl())
554                         cursorRight(bv);
555                 else
556                         cursorLeft(bv);
557                 finishChange(bv, true);
558                 break;
559
560         case LFUN_UPSEL:
561                 if (!bv->selection().set())
562                         bv->resetAnchor();
563                 cursorUp(true);
564                 finishChange(bv, true);
565                 break;
566
567         case LFUN_DOWNSEL:
568                 if (!bv->selection().set())
569                         bv->resetAnchor();
570                 cursorDown(true);
571                 finishChange(bv, true);
572                 break;
573
574         case LFUN_UP_PARAGRAPHSEL:
575                 if (!bv->selection().set())
576                         bv->resetAnchor();
577                 cursorUpParagraph();
578                 finishChange(bv, true);
579                 break;
580
581         case LFUN_DOWN_PARAGRAPHSEL:
582                 if (!bv->selection().set())
583                         bv->resetAnchor();
584                 cursorDownParagraph();
585                 finishChange(bv, true);
586                 break;
587
588         case LFUN_PRIORSEL:
589                 if (!bv->selection().set())
590                         bv->resetAnchor();
591                 cursorPrevious();
592                 finishChange(bv, true);
593                 break;
594
595         case LFUN_NEXTSEL:
596                 if (!bv->selection().set())
597                         bv->resetAnchor();
598                 cursorNext();
599                 finishChange(bv, true);
600                 break;
601
602         case LFUN_HOMESEL:
603                 if (!bv->selection().set())
604                         bv->resetAnchor();
605                 cursorHome();
606                 finishChange(bv, true);
607                 break;
608
609         case LFUN_ENDSEL:
610                 if (!bv->selection().set())
611                         bv->resetAnchor();
612                 cursorEnd();
613                 finishChange(bv, true);
614                 break;
615
616         case LFUN_WORDRIGHTSEL:
617                 if (!bv->selection().set())
618                         bv->resetAnchor();
619                 if (rtl())
620                         cursorLeftOneWord();
621                 else
622                         cursorRightOneWord();
623                 finishChange(bv, true);
624                 break;
625
626         case LFUN_WORDLEFTSEL:
627                 if (!bv->selection().set())
628                         bv->resetAnchor();
629                 if (rtl())
630                         cursorRightOneWord();
631                 else
632                         cursorLeftOneWord();
633                 finishChange(bv, true);
634                 break;
635
636         case LFUN_WORDSEL: {
637                 selectWord(lyx::WHOLE_WORD);
638                 finishChange(bv, true);
639                 break;
640         }
641
642         case LFUN_RIGHT:
643                 finishChange(bv);
644                 return moveRight();
645
646         case LFUN_LEFT:
647                 finishChange(bv);
648                 return moveLeft();
649
650         case LFUN_UP:
651                 finishChange(bv);
652                 return moveUp();
653
654         case LFUN_DOWN:
655                 finishChange(bv);
656                 return moveDown();
657
658         case LFUN_UP_PARAGRAPH:
659                 if (!bv->selection().mark())
660                         clearSelection();
661                 cursorUpParagraph();
662                 finishChange(bv);
663                 break;
664
665         case LFUN_DOWN_PARAGRAPH:
666                 if (!bv->selection().mark())
667                         clearSelection();
668                 cursorDownParagraph();
669                 finishChange(bv, false);
670                 break;
671
672         case LFUN_PRIOR:
673                 if (!bv->selection().mark())
674                         clearSelection();
675                 finishChange(bv, false);
676                 if (cursorPar() == firstPar() && cursorRow() == firstRow())
677                         return DispatchResult(false, FINISHED_UP);
678                 cursorPrevious();
679                 break;
680
681         case LFUN_NEXT:
682                 if (!bv->selection().mark())
683                         clearSelection();
684                 finishChange(bv, false);
685                 if (cursorPar() == lastPar() && cursorRow() == lastRow())
686                         return DispatchResult(false, FINISHED_DOWN);
687                 cursorNext();
688                 break;
689
690         case LFUN_HOME:
691                 if (!bv->selection().mark())
692                         clearSelection();
693                 cursorHome();
694                 finishChange(bv, false);
695                 break;
696
697         case LFUN_END:
698                 if (!bv->selection().mark())
699                         clearSelection();
700                 cursorEnd();
701                 finishChange(bv, false);
702                 break;
703
704         case LFUN_BREAKLINE: {
705                 lyx::pos_type body = cursorPar()->beginOfBody();
706
707                 // Not allowed by LaTeX (labels or empty par)
708                 if (cursor().pos() <= body)
709                         break;
710
711                 replaceSelection(bv->getLyXText());
712                 insertInset(new InsetNewline);
713                 moveCursor(bv, false);
714                 break;
715         }
716
717         case LFUN_DELETE:
718                 if (!bv->selection().set()) {
719                         Delete();
720                         bv->resetAnchor();
721                         // It is possible to make it a lot faster still
722                         // just comment out the line below...
723                 } else {
724                         cutSelection(true, false);
725                 }
726                 moveCursor(bv, false);
727                 bv->owner()->view_state_changed();
728                 break;
729
730         case LFUN_DELETE_SKIP:
731                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
732                 if (!bv->selection().set()) {
733                         if (cursor().pos() == cursorPar()->size()) {
734                                 cursorRight(bv);
735                                 cursorLeft(bv);
736                                 Delete();
737                                 bv->resetAnchor();
738                         } else {
739                                 Delete();
740                                 bv->resetAnchor();
741                         }
742                 } else {
743                         cutSelection(true, false);
744                 }
745                 bv->update();
746                 break;
747
748
749         case LFUN_BACKSPACE:
750                 if (!bv->selection().set()) {
751                         if (bv->owner()->getIntl().getTransManager().backspace()) {
752                                 backspace();
753                                 bv->resetAnchor();
754                                 // It is possible to make it a lot faster still
755                                 // just comment out the line below...
756                         }
757                 } else {
758                         cutSelection(true, false);
759                 }
760                 bv->owner()->view_state_changed();
761                 bv->switchKeyMap();
762                 bv->update();
763                 break;
764
765         case LFUN_BACKSPACE_SKIP:
766                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
767                 if (!bv->selection().set()) {
768                         CursorSlice cur = cursor();
769                         backspace();
770                         anchor() = cur;
771                 } else {
772                         cutSelection(true, false);
773                 }
774                 bv->update();
775                 break;
776
777         case LFUN_BREAKPARAGRAPH:
778                 replaceSelection(bv->getLyXText());
779                 breakParagraph(bv->buffer()->paragraphs(), 0);
780                 bv->update();
781                 bv->resetAnchor();
782                 bv->switchKeyMap();
783                 bv->owner()->view_state_changed();
784                 break;
785
786         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
787                 replaceSelection(bv->getLyXText());
788                 breakParagraph(bv->buffer()->paragraphs(), 1);
789                 bv->update();
790                 bv->resetAnchor();
791                 bv->switchKeyMap();
792                 bv->owner()->view_state_changed();
793                 break;
794
795         case LFUN_BREAKPARAGRAPH_SKIP: {
796                 // When at the beginning of a paragraph, remove
797                 // indentation and add a "defskip" at the top.
798                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
799                 CursorSlice cur = cursor();
800                 replaceSelection(bv->getLyXText());
801                 if (cur.pos() == 0) {
802                         ParagraphParameters & params = getPar(cur)->params();
803                         setParagraph(
804                                         params.spacing(),
805                                         params.align(),
806                                         params.labelWidthString(), 1);
807                 } else {
808                         breakParagraph(bv->buffer()->paragraphs(), 0);
809                 }
810                 bv->update();
811                 anchor() = cur;
812                 bv->switchKeyMap();
813                 bv->owner()->view_state_changed();
814                 break;
815         }
816
817         case LFUN_PARAGRAPH_SPACING: {
818                 ParagraphList::iterator pit = cursorPar();
819                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
820                 float cur_value = 1.0;
821                 if (cur_spacing == Spacing::Other)
822                         cur_value = pit->params().spacing().getValue();
823
824                 istringstream is(cmd.argument);
825                 string tmp;
826                 is >> tmp;
827                 Spacing::Space new_spacing = cur_spacing;
828                 float new_value = cur_value;
829                 if (tmp.empty()) {
830                         lyxerr << "Missing argument to `paragraph-spacing'"
831                                << endl;
832                 } else if (tmp == "single") {
833                         new_spacing = Spacing::Single;
834                 } else if (tmp == "onehalf") {
835                         new_spacing = Spacing::Onehalf;
836                 } else if (tmp == "double") {
837                         new_spacing = Spacing::Double;
838                 } else if (tmp == "other") {
839                         new_spacing = Spacing::Other;
840                         float tmpval = 0.0;
841                         is >> tmpval;
842                         lyxerr << "new_value = " << tmpval << endl;
843                         if (tmpval != 0.0)
844                                 new_value = tmpval;
845                 } else if (tmp == "default") {
846                         new_spacing = Spacing::Default;
847                 } else {
848                         lyxerr << _("Unknown spacing argument: ")
849                                << cmd.argument << endl;
850                 }
851                 if (cur_spacing != new_spacing || cur_value != new_value) {
852                         pit->params().spacing(Spacing(new_spacing, new_value));
853                         redoParagraph();
854                         bv->update();
855                 }
856                 break;
857         }
858
859         case LFUN_INSET_APPLY: {
860                 string const name = cmd.getArg(0);
861                 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
862                 if (inset) {
863                         FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
864                         inset->dispatch(fr);
865                 } else {
866                         FuncRequest fr(bv, LFUN_INSET_INSERT, cmd.argument);
867                         dispatch(fr);
868                 }
869                 break;
870         }
871
872         case LFUN_INSET_INSERT: {
873                 InsetOld * inset = createInset(cmd);
874                 if (inset && !bv->insertInset(inset))
875                         delete inset;
876                 break;
877         }
878
879         case LFUN_INSET_SETTINGS:
880                 bv->fullCursor().innerInset()->showInsetDialog(bv);
881                 break;
882
883         case LFUN_INSET_TOGGLE:
884                 clearSelection();
885                 if (!toggleInset())
886                         return DispatchResult(false);
887                 bv->update();
888                 bv->switchKeyMap();
889                 break;
890
891         case LFUN_SPACE_INSERT:
892                 if (cursorPar()->layout()->free_spacing)
893                         insertChar(' ');
894                 else
895                         doInsertInset(cmd, false, false);
896                 moveCursor(bv, false);
897                 break;
898
899         case LFUN_HYPHENATION:
900                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
901                 break;
902
903         case LFUN_LIGATURE_BREAK:
904                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
905                 break;
906
907         case LFUN_LDOTS:
908                 specialChar(this, bv, InsetSpecialChar::LDOTS);
909                 break;
910
911         case LFUN_END_OF_SENTENCE:
912                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
913                 break;
914
915         case LFUN_MENU_SEPARATOR:
916                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
917                 break;
918
919         case LFUN_MARK_OFF:
920                 clearSelection();
921                 bv->update();
922                 bv->resetAnchor();
923                 cmd.message(N_("Mark off"));
924                 break;
925
926         case LFUN_MARK_ON:
927                 clearSelection();
928                 bv->selection().mark(true);
929                 bv->update();
930                 bv->resetAnchor();
931                 cmd.message(N_("Mark on"));
932                 break;
933
934         case LFUN_SETMARK:
935                 clearSelection();
936                 if (bv->selection().mark()) {
937                         cmd.message(N_("Mark removed"));
938                 } else {
939                         bv->selection().mark(true);
940                         cmd.message(N_("Mark set"));
941                 }
942                 bv->resetAnchor();
943                 bv->update();
944                 break;
945
946         case LFUN_UPCASE_WORD:
947                 changeCase(LyXText::text_uppercase);
948                 bv->update();
949                 break;
950
951         case LFUN_LOWCASE_WORD:
952                 changeCase(LyXText::text_lowercase);
953                 bv->update();
954                 break;
955
956         case LFUN_CAPITALIZE_WORD:
957                 changeCase(LyXText::text_capitalization);
958                 bv->update();
959                 break;
960
961         case LFUN_TRANSPOSE_CHARS:
962                 recUndo(cursor().par());
963                 redoParagraph();
964                 bv->update();
965                 break;
966
967         case LFUN_PASTE:
968                 cmd.message(_("Paste"));
969                 replaceSelection(bv->getLyXText());
970 #warning FIXME Check if the arg is in the domain of available selections.
971                 if (isStrUnsignedInt(cmd.argument))
972                         pasteSelection(strToUnsignedInt(cmd.argument));
973                 else
974                         pasteSelection(0);
975                 clearSelection(); // bug 393
976                 bv->update();
977                 bv->switchKeyMap();
978                 finishUndo();
979                 break;
980
981         case LFUN_CUT:
982                 cutSelection(true, true);
983                 cmd.message(_("Cut"));
984                 bv->update();
985                 break;
986
987         case LFUN_COPY:
988                 copySelection();
989                 cmd.message(_("Copy"));
990                 break;
991
992         case LFUN_BEGINNINGBUFSEL:
993                 if (in_inset_)
994                         return DispatchResult(false);
995                 if (!bv->selection().set())
996                         bv->resetAnchor();
997                 cursorTop();
998                 finishChange(bv, true);
999                 break;
1000
1001         case LFUN_ENDBUFSEL:
1002                 if (in_inset_)
1003                         return DispatchResult(false);
1004                 if (!bv->selection().set())
1005                         bv->resetAnchor();
1006                 cursorBottom();
1007                 finishChange(bv, true);
1008                 break;
1009
1010         case LFUN_GETXY:
1011                 cmd.message(tostr(cursorX()) + ' ' + tostr(cursorY()));
1012                 break;
1013
1014         case LFUN_SETXY: {
1015                 int x = 0;
1016                 int y = 0;
1017                 istringstream is(cmd.argument);
1018                 is >> x >> y;
1019                 if (!is)
1020                         lyxerr << "SETXY: Could not parse coordinates in '"
1021                                << cmd.argument << std::endl;
1022                 else
1023                         setCursorFromCoordinates(x, y);
1024                 break;
1025         }
1026
1027         case LFUN_GETFONT:
1028                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
1029                         cmd.message("E");
1030                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1031                         cmd.message("N");
1032                 else
1033                         cmd.message("0");
1034                 break;
1035
1036         case LFUN_GETLAYOUT:
1037                 cmd.message(cursorPar()->layout()->name());
1038                 break;
1039
1040         case LFUN_LAYOUT: {
1041                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1042                   << cmd.argument << endl;
1043
1044                 // This is not the good solution to the empty argument
1045                 // problem, but it will hopefully suffice for 1.2.0.
1046                 // The correct solution would be to augument the
1047                 // function list/array with information about what
1048                 // functions needs arguments and their type.
1049                 if (cmd.argument.empty()) {
1050                         cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1051                         break;
1052                 }
1053
1054                 // Derive layout number from given argument (string)
1055                 // and current buffer's textclass (number)
1056                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1057                 bool hasLayout = tclass.hasLayout(cmd.argument);
1058                 string layout = cmd.argument;
1059
1060                 // If the entry is obsolete, use the new one instead.
1061                 if (hasLayout) {
1062                         string const & obs = tclass[layout]->obsoleted_by();
1063                         if (!obs.empty())
1064                                 layout = obs;
1065                 }
1066
1067                 if (!hasLayout) {
1068                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1069                                 N_(" not known"));
1070                         break;
1071                 }
1072
1073                 bool change_layout = (current_layout != layout);
1074
1075                 if (!change_layout && bv->selection().set() &&
1076                         selStart().par() != selEnd().par())
1077                 {
1078                         ParagraphList::iterator spit = getPar(selStart());
1079                         ParagraphList::iterator epit = boost::next(getPar(selEnd()));
1080                         while (spit != epit) {
1081                                 if (spit->layout()->name() != current_layout) {
1082                                         change_layout = true;
1083                                         break;
1084                                 }
1085                                 ++spit;
1086                         }
1087                 }
1088
1089                 if (change_layout) {
1090                         current_layout = layout;
1091                         setLayout(layout);
1092                         bv->owner()->setLayout(layout);
1093                         bv->update();
1094                         bv->switchKeyMap();
1095                 }
1096                 break;
1097         }
1098
1099         case LFUN_PASTESELECTION: {
1100                 // this was originally a bv->text->clearSelection(), i.e
1101                 // the outermost LyXText!
1102                 clearSelection();
1103                 string const clip = bv->getClipboard();
1104                 if (!clip.empty()) {
1105                         if (cmd.argument == "paragraph")
1106                                 insertStringAsParagraphs(clip);
1107                         else
1108                                 insertStringAsLines(clip);
1109                         bv->update();
1110                 }
1111                 break;
1112         }
1113
1114         case LFUN_GOTOERROR:
1115                 gotoInset(InsetOld::ERROR_CODE, false);
1116                 break;
1117
1118         case LFUN_GOTONOTE:
1119                 gotoInset(InsetOld::NOTE_CODE, false);
1120                 break;
1121
1122         case LFUN_REFERENCE_GOTO: {
1123                 vector<InsetOld::Code> tmp;
1124                 tmp.push_back(InsetOld::LABEL_CODE);
1125                 tmp.push_back(InsetOld::REF_CODE);
1126                 gotoInset(tmp, true);
1127                 break;
1128         }
1129
1130         case LFUN_QUOTE: {
1131                 replaceSelection(bv->getLyXText());
1132                 ParagraphList::iterator pit = cursorPar();
1133                 lyx::pos_type pos = cursor().pos();
1134                 char c;
1135                 if (!pos)
1136                         c = ' ';
1137                 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1138                         c = ' ';
1139                 else
1140                         c = pit->getChar(pos - 1);
1141
1142                 LyXLayout_ptr const & style = pit->layout();
1143
1144                 BufferParams const & bufparams = bv->buffer()->params();
1145                 if (style->pass_thru ||
1146                     pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1147                     !bv->insertInset(new InsetQuotes(c, bufparams)))
1148                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1149                 break;
1150         }
1151
1152         case LFUN_DATE_INSERT: {
1153                 replaceSelection(bv->getLyXText());
1154                 time_t now_time_t = time(NULL);
1155                 struct tm * now_tm = localtime(&now_time_t);
1156                 setlocale(LC_TIME, "");
1157                 string arg;
1158                 if (!cmd.argument.empty())
1159                         arg = cmd.argument;
1160                 else
1161                         arg = lyxrc.date_insert_format;
1162                 char datetmp[32];
1163                 int const datetmp_len =
1164                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1165
1166                 for (int i = 0; i < datetmp_len; i++)
1167                         insertChar(datetmp[i]);
1168
1169                 bv->resetAnchor();
1170                 moveCursor(bv, false);
1171                 break;
1172         }
1173
1174         case LFUN_MOUSE_TRIPLE:
1175                 if (!bv->buffer())
1176                         break;
1177                 if (cmd.button() == mouse_button::button1) {
1178                         selection_possible = true;
1179                         cursorHome();
1180                         bv->resetAnchor();
1181                         cursorEnd();
1182                         setSelection();
1183                         bv->haveSelection(bv->selection().set());
1184                 }
1185                 break;
1186
1187         case LFUN_MOUSE_DOUBLE:
1188                 if (!bv->buffer())
1189                         break;
1190                 if (cmd.button() == mouse_button::button1) {
1191                         selection_possible = true;
1192                         selectWord(lyx::WHOLE_WORD_STRICT);
1193                         bv->haveSelection(bv->selection().set());
1194                 }
1195                 break;
1196
1197         case LFUN_MOUSE_MOTION: {
1198                 // Only use motion with button 1
1199                 //if (ev.button() != mouse_button::button1)
1200                 //      return false;
1201
1202                 if (!bv->buffer())
1203                         break;
1204                 // The test for not selection possible is needed, that
1205                 // only motion events are used, where the bottom press
1206                 // event was on the drawing area too
1207                 if (!selection_possible) {
1208                         lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1209                                 "Dispatch: no selection possible\n";
1210                         break;
1211                 }
1212                 RowList::iterator cursorrow = cursorRow();
1213
1214                 setCursorFromCoordinates(cmd.x, cmd.y);
1215
1216                 // This is to allow jumping over large insets
1217                 // FIXME: shouldn't be top-text-specific
1218                 if (cursorrow == cursorRow() && !in_inset_) {
1219                         if (cmd.y - bv->top_y() >= bv->workHeight())
1220                                 cursorDown(true);
1221                         else if (cmd.y - bv->top_y() < 0)
1222                                 cursorUp(true);
1223                 }
1224                 setSelection();
1225                 break;
1226         }
1227
1228         // Single-click on work area
1229         case LFUN_MOUSE_PRESS: {
1230                 if (!bv->buffer())
1231                         break;
1232
1233                 // ok ok, this is a hack (for xforms)
1234                 // We shouldn't go further down as we really should only do the
1235                 // scrolling and be done with this. Otherwise we may open some
1236                 // dialogs (Jug 20020424).
1237                 if (cmd.button() == mouse_button::button4) {
1238                         bv->scroll(-lyxrc.wheel_jump);
1239                         break;
1240                 }
1241                 if (cmd.button() == mouse_button::button5) {
1242                         bv->scroll(lyxrc.wheel_jump);
1243                         break;
1244                 }
1245
1246                 // Middle button press pastes if we have a selection
1247                 // We do this here as if the selection was inside an inset
1248                 // it could get cleared on the unlocking of the inset so
1249                 // we have to check this first
1250                 bool paste_internally = false;
1251                 if (cmd.button() == mouse_button::button2 && bv->selection().set()) {
1252                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1253                         paste_internally = true;
1254                 }
1255
1256                 selection_possible = true;
1257
1258                 // Clear the selection
1259                 clearSelection();
1260
1261                 // Right click on a footnote flag opens float menu
1262                 if (cmd.button() == mouse_button::button3) {
1263                         selection_possible = false;
1264                         break;
1265                 }
1266
1267                 setCursorFromCoordinates(cmd.x, cmd.y);
1268                 bv->resetAnchor();
1269                 finishUndo();
1270                 bv->x_target(cursorX() + xo_);
1271
1272                 if (bv->fitCursor())
1273                         selection_possible = false;
1274
1275                 // Insert primary selection with middle mouse
1276                 // if there is a local selection in the current buffer,
1277                 // insert this
1278                 if (cmd.button() == mouse_button::button2) {
1279                         if (paste_internally)
1280                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1281                         else
1282                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1283                         selection_possible = false;
1284                 }
1285                 break;
1286         }
1287
1288         case LFUN_MOUSE_RELEASE: {
1289                 if (!bv->buffer())
1290                         break;
1291
1292                 // do nothing if we used the mouse wheel
1293                 if (cmd.button() == mouse_button::button4
1294                  || cmd.button() == mouse_button::button5)
1295                         return DispatchResult(true, false);
1296
1297                 selection_possible = false;
1298
1299                 if (cmd.button() == mouse_button::button2)
1300                         break;
1301
1302                 // finish selection
1303                 if (cmd.button() == mouse_button::button1)
1304                         bv->haveSelection(bv->selection().set());
1305
1306                 bv->switchKeyMap();
1307                 bv->owner()->view_state_changed();
1308                 bv->owner()->updateMenubar();
1309                 bv->owner()->updateToolbar();
1310                 break;
1311         }
1312
1313         case LFUN_SELFINSERT: {
1314                 if (cmd.argument.empty())
1315                         break;
1316
1317                 // Automatically delete the currently selected
1318                 // text and replace it with what is being
1319                 // typed in now. Depends on lyxrc settings
1320                 // "auto_region_delete", which defaults to
1321                 // true (on).
1322
1323                 if (lyxrc.auto_region_delete) {
1324                         if (bv->selection().set())
1325                                 cutSelection(false, false);
1326                         bv->haveSelection(false);
1327                 }
1328
1329                 clearSelection();
1330                 LyXFont const old_font = real_current_font;
1331
1332                 string::const_iterator cit = cmd.argument.begin();
1333                 string::const_iterator end = cmd.argument.end();
1334                 for (; cit != end; ++cit)
1335                         bv->owner()->getIntl().getTransManager().
1336                                 TranslateAndInsert(*cit, this);
1337
1338                 bv->resetAnchor();
1339                 moveCursor(bv, false);
1340
1341                 // real_current_font.number can change so we need to
1342                 // update the minibuffer
1343                 if (old_font != real_current_font)
1344                         bv->owner()->view_state_changed();
1345                 bv->updateScrollbar();
1346                 break;
1347         }
1348
1349         case LFUN_URL: {
1350                 InsetCommandParams p("url");
1351                 string const data = InsetCommandMailer::params2string("url", p);
1352                 bv->owner()->getDialogs().show("url", data, 0);
1353                 break;
1354         }
1355
1356         case LFUN_HTMLURL: {
1357                 InsetCommandParams p("htmlurl");
1358                 string const data = InsetCommandMailer::params2string("url", p);
1359                 bv->owner()->getDialogs().show("url", data, 0);
1360                 break;
1361         }
1362
1363         case LFUN_INSERT_LABEL: {
1364                 InsetCommandParams p("label");
1365                 string const data = InsetCommandMailer::params2string("label", p);
1366                 bv->owner()->getDialogs().show("label", data, 0);
1367                 break;
1368         }
1369
1370
1371 #if 0
1372         case LFUN_INSET_LIST:
1373         case LFUN_INSET_THEOREM:
1374         case LFUN_INSET_CAPTION:
1375 #endif
1376         case LFUN_INSERT_NOTE:
1377         case LFUN_INSERT_CHARSTYLE:
1378         case LFUN_INSERT_BOX:
1379         case LFUN_INSERT_BRANCH:
1380         case LFUN_INSERT_BIBITEM:
1381         case LFUN_INSET_ERT:
1382         case LFUN_INSET_FLOAT:
1383         case LFUN_INSET_FOOTNOTE:
1384         case LFUN_INSET_MARGINAL:
1385         case LFUN_INSET_OPTARG:
1386         case LFUN_INSET_WIDE_FLOAT:
1387         case LFUN_INSET_WRAP:
1388         case LFUN_TABULAR_INSERT:
1389         case LFUN_ENVIRONMENT_INSERT:
1390                 // Open the inset, and move the current selection
1391                 // inside it.
1392                 doInsertInset(cmd, true, true);
1393                 break;
1394
1395         case LFUN_INDEX_INSERT:
1396                 // Just open the inset
1397                 doInsertInset(cmd, true, false);
1398                 break;
1399
1400         case LFUN_INDEX_PRINT:
1401         case LFUN_TOC_INSERT:
1402         case LFUN_HFILL:
1403         case LFUN_INSERT_LINE:
1404         case LFUN_INSERT_PAGEBREAK:
1405                 // do nothing fancy
1406                 doInsertInset(cmd, false, false);
1407                 break;
1408
1409         case LFUN_DEPTH_MIN:
1410                 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH);
1411                 bv->update();
1412                 break;
1413
1414         case LFUN_DEPTH_PLUS:
1415                 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH);
1416                 bv->update();
1417                 break;
1418
1419         case LFUN_MATH_DELIM:
1420         case LFUN_MATH_DISPLAY:
1421         case LFUN_INSERT_MATH:
1422         case LFUN_MATH_LIMITS:
1423         case LFUN_MATH_MACRO:
1424         case LFUN_MATH_MUTATE:
1425         case LFUN_MATH_SPACE:
1426         case LFUN_MATH_IMPORT_SELECTION:
1427         case LFUN_MATH_MODE:
1428         case LFUN_MATH_NONUMBER:
1429         case LFUN_MATH_NUMBER:
1430         case LFUN_MATH_EXTERN:
1431         case LFUN_MATH_SIZE:
1432                 mathDispatch(cmd);
1433                 break;
1434
1435         case LFUN_EMPH:
1436                 emph(bv, this);
1437                 bv->owner()->view_state_changed();
1438                 break;
1439
1440         case LFUN_BOLD:
1441                 bold(bv, this);
1442                 bv->owner()->view_state_changed();
1443                 break;
1444
1445         case LFUN_NOUN:
1446                 noun(bv, this);
1447                 bv->owner()->view_state_changed();
1448                 break;
1449
1450         case LFUN_CODE:
1451                 code(bv, this);
1452                 bv->owner()->view_state_changed();
1453                 break;
1454
1455         case LFUN_SANS:
1456                 sans(bv, this);
1457                 bv->owner()->view_state_changed();
1458                 break;
1459
1460         case LFUN_ROMAN:
1461                 roman(bv, this);
1462                 bv->owner()->view_state_changed();
1463                 break;
1464
1465         case LFUN_DEFAULT:
1466                 styleReset(bv, this);
1467                 bv->owner()->view_state_changed();
1468                 break;
1469
1470         case LFUN_UNDERLINE:
1471                 underline(bv, this);
1472                 bv->owner()->view_state_changed();
1473                 break;
1474
1475         case LFUN_FONT_SIZE:
1476                 fontSize(bv, cmd.argument, this);
1477                 bv->owner()->view_state_changed();
1478                 break;
1479
1480         case LFUN_LANGUAGE:
1481                 lang(bv, cmd.argument, this);
1482                 bv->switchKeyMap();
1483                 bv->owner()->view_state_changed();
1484                 break;
1485
1486         case LFUN_FREEFONT_APPLY:
1487                 apply_freefont(bv, this);
1488                 break;
1489
1490         case LFUN_FREEFONT_UPDATE:
1491                 update_and_apply_freefont(bv, this, cmd.argument);
1492                 break;
1493
1494         case LFUN_FINISHED_LEFT:
1495                 lyxerr << "swallow LFUN_FINISHED_LEFT" << endl;
1496                 if (rtl())
1497                         cursorRight(true);
1498                 break;
1499
1500         case LFUN_FINISHED_RIGHT:
1501                 lyxerr << "swallow LFUN_FINISHED_RIGHT" << endl;
1502                 if (!rtl())
1503                         cursorRight(true);
1504                 break;
1505
1506         case LFUN_FINISHED_UP:
1507                 lyxerr << "swallow LFUN_FINISHED_UP" << endl;
1508                 cursorUp(true);
1509                 break;
1510
1511         case LFUN_FINISHED_DOWN:
1512                 lyxerr << "swallow LFUN_FINISHED_DOWN" << endl;
1513                 cursorDown(true);
1514                 break;
1515
1516         default:
1517                 return DispatchResult(false);
1518         }
1519
1520         return DispatchResult(true, true);
1521 }