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