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