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