]> git.lyx.org Git - lyx.git/blob - src/text3.C
more cursor dispatch
[lyx.git] / src / text3.C
1 /**
2  * \file text3.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "lyxtext.h"
19
20 #include "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "cursor.h"
24 #include "debug.h"
25 #include "dispatchresult.h"
26 #include "factory.h"
27 #include "funcrequest.h"
28 #include "gettext.h"
29 #include "intl.h"
30 #include "language.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphParameters.h"
36 #include "undo.h"
37 #include "vspace.h"
38
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41
42 #include "insets/insetcommand.h"
43 #include "insets/insetnewline.h"
44 #include "insets/insetspecialchar.h"
45 #include "insets/insettext.h"
46
47 #include "support/lstrings.h"
48 #include "support/tostr.h"
49 #include "support/std_sstream.h"
50
51 #include "mathed/math_hullinset.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 button 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(LCursor & cur, bool selecting)
217         {
218                 if (selecting || cur.mark())
219                         cur.setSelection();
220                 if (!cur.selection())
221                         cur.bv().haveSelection(false);
222                 cur.bv().switchKeyMap();
223         }
224
225
226         void finishChange(BufferView * bv, bool selecting = false)
227         {
228                 finishUndo();
229                 moveCursor(bv->cursor(), selecting);
230                 bv->owner()->view_state_changed();
231         }
232
233 } // anon namespace
234
235
236 namespace bv_funcs {
237
238 string const freefont2string()
239 {
240         string data;
241         if (font2string(freefont, toggleall, data))
242                 return data;
243         return string();
244 }
245
246 }
247
248
249 InsetBase * LyXText::checkInsetHit(int x, int y)
250 {
251         ParagraphList::iterator pit;
252         ParagraphList::iterator end;
253
254         getParsInRange(paragraphs(),
255                        bv()->top_y(),
256                        bv()->top_y() + bv()->workHeight(),
257                        pit, end);
258
259         lyxerr << "checkInsetHit: x: " << x << " y: " << y << endl;
260         for ( ; pit != end; ++pit) {
261                 InsetList::iterator iit = pit->insetlist.begin();
262                 InsetList::iterator iend = pit->insetlist.end();
263                 for ( ; iit != iend; ++iit) {
264                         InsetBase * inset = iit->inset;
265 #if 1
266                         lyxerr << "examining inset " << inset
267                         //<< " xo/yo: " << inset->xo() << "/" << inset->yo()
268                                 << " xo: " << inset->xo() << "..." << inset->xo() + inset->width()
269                                 << " yo: " << inset->yo() - inset->ascent() << "..."
270                                 << inset->yo() + inset->descent() << endl;
271 #endif
272                         if (inset->covers(x, y - bv()->top_y())) {
273                                 lyxerr << "Hit inset: " << inset << endl;
274                                 return inset;
275                         }
276                 }
277         }
278         lyxerr << "No inset hit. " << endl;
279         return 0;
280 }
281
282
283 bool LyXText::gotoNextInset(vector<InsetOld_code> const & codes,
284                             string const & contents)
285 {
286         ParagraphList::iterator end = paragraphs().end();
287         ParagraphList::iterator pit = cursorPar();
288         pos_type pos = cursor().pos();
289
290         InsetBase * inset;
291         do {
292                 if (pos + 1 < pit->size()) {
293                         ++pos;
294                 } else  {
295                         ++pit;
296                         pos = 0;
297                 }
298
299         } while (pit != end &&
300                  !(pit->isInset(pos) &&
301                    (inset = pit->getInset(pos)) != 0 &&
302                    find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
303                    (contents.empty() ||
304                     static_cast<InsetCommand *>(pit->getInset(pos))->getContents()
305                     == contents)));
306
307         if (pit == end)
308                 return false;
309
310         setCursor(parOffset(pit), pos, false);
311         return true;
312 }
313
314
315 void LyXText::gotoInset(vector<InsetOld_code> const & codes, bool same_content)
316 {
317         LCursor & cur = bv()->cursor();
318         cur.clearSelection();
319
320         string contents;
321         if (same_content
322             && cur.pos() < cur.lastpos()
323             && cur.paragraph().isInset(cur.pos())) {
324                 InsetBase const * inset = cur.paragraph().getInset(cur.pos());
325                 if (find(codes.begin(), codes.end(), inset->lyxCode())
326                     != codes.end())
327                         contents = static_cast<InsetCommand const *>(inset)->getContents();
328         }
329
330         if (!gotoNextInset(codes, contents)) {
331                 if (cur.pos() || cur.par() != 0) {
332                         CursorSlice tmp = cursor();
333                         cur.par() = 0;
334                         cur.pos() = 0;
335                         if (!gotoNextInset(codes, contents)) {
336                                 cursor() = tmp;
337                                 cur.bv().owner()->message(_("No more insets"));
338                         }
339                 } else {
340                         cur.bv().owner()->message(_("No more insets"));
341                 }
342         }
343         cur.bv().update();
344         cur.resetAnchor();
345 }
346
347
348 void LyXText::gotoInset(InsetOld_code code, bool same_content)
349 {
350         gotoInset(vector<InsetOld_code>(1, code), same_content);
351 }
352
353
354 void LyXText::cursorPrevious()
355 {
356         LCursor & cur = bv()->cursor();
357         pos_type cpos = cur.pos();
358         lyx::paroffset_type cpar = cur.par();
359
360         int x = bv()->cursor().x_target();
361         int y = bv()->top_y();
362         setCursorFromCoordinates(x, y);
363
364         if (cpar == cur.par() && cpos == cur.pos()) {
365                 // we have a row which is taller than the workarea. The
366                 // simplest solution is to move to the previous row instead.
367                 cursorUp(true);
368         }
369
370         bv()->updateScrollbar();
371         finishUndo();
372 }
373
374
375 void LyXText::cursorNext()
376 {
377         LCursor & cur = bv()->cursor();
378         pos_type cpos = cur.pos();
379         lyx::paroffset_type cpar = cur.par();
380
381         int x = cur.x_target();
382         int y = bv()->top_y() + bv()->workHeight();
383         setCursorFromCoordinates(x, y);
384
385         if (cpar == cur.par() && cpos == cur.pos()) {
386                 // we have a row which is taller than the workarea. The
387                 // simplest solution is to move to the next row instead.
388                 cursorDown(true);
389         }
390
391         bv()->updateScrollbar();
392         finishUndo();
393 }
394
395
396 namespace {
397
398 void specialChar(LyXText * text, BufferView * bv, InsetSpecialChar::Kind kind)
399 {
400         bv->update();
401         InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
402         replaceSelection(text);
403         if (!bv->insertInset(new_inset))
404                 delete new_inset;
405         else
406                 bv->update();
407 }
408
409
410 void doInsertInset(BufferView * bv, FuncRequest const & cmd,
411         bool edit, bool pastesel)
412 {
413         InsetBase * inset = createInset(bv, cmd);
414         if (!inset)
415                 return;
416
417         bool gotsel = false;
418         if (bv->cursor().selection()) {
419                 bv->owner()->dispatch(FuncRequest(LFUN_CUT));
420                 gotsel = true;
421         }
422         if (bv->insertInset(inset)) {
423                 if (edit)
424                         inset->edit(bv->cursor(), true);
425                 if (gotsel && pastesel)
426                         bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
427         } else {
428                 delete inset;
429         }
430 }
431
432 } // anon namespace
433
434
435 void LyXText::number()
436 {
437         LyXFont font(LyXFont::ALL_IGNORE);
438         font.setNumber(LyXFont::TOGGLE);
439         toggleAndShow(bv(), this, font);
440 }
441
442
443 bool LyXText::rtl() const
444 {
445         return cursorPar()->isRightToLeftPar(bv()->buffer()->params());
446 }
447
448
449 DispatchResult LyXText::dispatch(LCursor & cur, FuncRequest const & cmd)
450 {
451         lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
452         //lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
453
454         BufferView * bv = &cur.bv();
455
456         switch (cmd.action) {
457
458         case LFUN_APPENDIX: {
459                 ParagraphList::iterator pit = cursorPar();
460                 bool start = !pit->params().startOfAppendix();
461
462                 // ensure that we have only one start_of_appendix in this document
463                 ParagraphList::iterator tmp = paragraphs().begin();
464                 ParagraphList::iterator end = paragraphs().end();
465
466                 for (; tmp != end; ++tmp) {
467                         if (tmp->params().startOfAppendix()) {
468                                 recUndo(parOffset(tmp));
469                                 tmp->params().startOfAppendix(false);
470                                 redoParagraph(tmp);
471                                 break;
472                         }
473                 }
474
475                 recUndo(parOffset(pit));
476                 pit->params().startOfAppendix(start);
477
478                 // we can set the refreshing parameters now
479                 updateCounters();
480                 redoParagraph(cursorPar());
481                 bv->update();
482                 break;
483         }
484
485         case LFUN_DELETE_WORD_FORWARD:
486                 cur.clearSelection();
487                 deleteWordForward();
488                 finishChange(bv);
489                 break;
490
491         case LFUN_DELETE_WORD_BACKWARD:
492                 cur.clearSelection();
493                 deleteWordBackward();
494                 finishChange(bv);
495                 break;
496
497         case LFUN_DELETE_LINE_FORWARD:
498                 cur.clearSelection();
499                 deleteLineForward();
500                 finishChange(bv);
501                 break;
502
503         case LFUN_WORDRIGHT:
504                 if (!cur.mark())
505                         cur.clearSelection();
506                 if (rtl())
507                         cursorLeftOneWord();
508                 else
509                         cursorRightOneWord();
510                 finishChange(bv);
511                 break;
512
513         case LFUN_WORDLEFT:
514                 if (!cur.mark())
515                         cur.clearSelection();
516                 if (rtl())
517                         cursorRightOneWord();
518                 else
519                         cursorLeftOneWord();
520                 finishChange(bv);
521                 break;
522
523         case LFUN_BEGINNINGBUF:
524                 if (!cur.mark())
525                         cur.clearSelection();
526                 cursorTop();
527                 finishChange(bv);
528                 break;
529
530         case LFUN_ENDBUF:
531                 if (!cur.mark())
532                         cur.clearSelection();
533                 cursorBottom();
534                 finishChange(bv);
535                 break;
536
537         case LFUN_RIGHTSEL:
538                 if (!cur.selection())
539                         cur.resetAnchor();
540                 if (rtl())
541                         cursorLeft(true);
542                 else
543                         cursorRight(true);
544                 finishChange(bv, true);
545                 break;
546
547         case LFUN_LEFTSEL:
548                 if (!cur.selection())
549                         cur.resetAnchor();
550                 if (rtl())
551                         cursorRight(true);
552                 else
553                         cursorLeft(true);
554                 finishChange(bv, true);
555                 break;
556
557         case LFUN_UPSEL:
558                 if (!cur.selection())
559                         cur.resetAnchor();
560                 cursorUp(true);
561                 finishChange(bv, true);
562                 break;
563
564         case LFUN_DOWNSEL:
565                 if (!cur.selection())
566                         cur.resetAnchor();
567                 cursorDown(true);
568                 finishChange(bv, true);
569                 break;
570
571         case LFUN_UP_PARAGRAPHSEL:
572                 if (!cur.selection())
573                         cur.resetAnchor();
574                 cursorUpParagraph();
575                 finishChange(bv, true);
576                 break;
577
578         case LFUN_DOWN_PARAGRAPHSEL:
579                 if (!cur.selection())
580                         cur.resetAnchor();
581                 cursorDownParagraph();
582                 finishChange(bv, true);
583                 break;
584
585         case LFUN_PRIORSEL:
586                 if (!cur.selection())
587                         cur.resetAnchor();
588                 cursorPrevious();
589                 finishChange(bv, true);
590                 break;
591
592         case LFUN_NEXTSEL:
593                 if (!cur.selection())
594                         cur.resetAnchor();
595                 cursorNext();
596                 finishChange(bv, true);
597                 break;
598
599         case LFUN_HOMESEL:
600                 if (!cur.selection())
601                         cur.resetAnchor();
602                 cursorHome();
603                 finishChange(bv, true);
604                 break;
605
606         case LFUN_ENDSEL:
607                 if (!cur.selection())
608                         cur.resetAnchor();
609                 cursorEnd();
610                 finishChange(bv, true);
611                 break;
612
613         case LFUN_WORDRIGHTSEL:
614                 if (!cur.selection())
615                         cur.resetAnchor();
616                 if (rtl())
617                         cursorLeftOneWord();
618                 else
619                         cursorRightOneWord();
620                 finishChange(bv, true);
621                 break;
622
623         case LFUN_WORDLEFTSEL:
624                 if (!cur.selection())
625                         cur.resetAnchor();
626                 if (rtl())
627                         cursorRightOneWord();
628                 else
629                         cursorLeftOneWord();
630                 finishChange(bv, true);
631                 break;
632
633         case LFUN_WORDSEL: {
634                 selectWord(lyx::WHOLE_WORD);
635                 finishChange(bv, true);
636                 break;
637         }
638
639         case LFUN_RIGHT:
640                 finishChange(bv);
641                 return moveRight();
642
643         case LFUN_LEFT:
644                 finishChange(bv);
645                 return moveLeft();
646
647         case LFUN_UP:
648                 finishChange(bv);
649                 return moveUp();
650
651         case LFUN_DOWN:
652                 finishChange(bv);
653                 return moveDown();
654
655         case LFUN_UP_PARAGRAPH:
656                 if (!cur.mark())
657                         cur.clearSelection();
658                 cursorUpParagraph();
659                 finishChange(bv);
660                 break;
661
662         case LFUN_DOWN_PARAGRAPH:
663                 if (!cur.mark())
664                         cur.clearSelection();
665                 cursorDownParagraph();
666                 finishChange(bv, false);
667                 break;
668
669         case LFUN_PRIOR:
670                 if (!cur.mark())
671                         cur.clearSelection();
672                 finishChange(bv, false);
673                 if (cur.par() == 0 && cursorRow() == firstRow())
674                         return DispatchResult(false, FINISHED_UP);
675                 cursorPrevious();
676                 break;
677
678         case LFUN_NEXT:
679                 if (!cur.mark())
680                         cur.clearSelection();
681                 finishChange(bv, false);
682                 if (cur.par() == cur.lastpar() && cursorRow() == lastRow())
683                         return DispatchResult(false, FINISHED_DOWN);
684                 cursorNext();
685                 break;
686
687         case LFUN_HOME:
688                 if (!cur.mark())
689                         cur.clearSelection();
690                 cursorHome();
691                 finishChange(bv, false);
692                 break;
693
694         case LFUN_END:
695                 if (!cur.mark())
696                         cur.clearSelection();
697                 cursorEnd();
698                 finishChange(bv, false);
699                 break;
700
701         case LFUN_BREAKLINE: {
702                 lyx::pos_type body = cursorPar()->beginOfBody();
703
704                 // Not allowed by LaTeX (labels or empty par)
705                 if (cursor().pos() <= body)
706                         break;
707
708                 replaceSelection(bv->getLyXText());
709                 insertInset(new InsetNewline);
710                 moveCursor(cur, false);
711                 break;
712         }
713
714         case LFUN_DELETE:
715                 if (!cur.selection()) {
716                         Delete();
717                         cur.resetAnchor();
718                         // It is possible to make it a lot faster still
719                         // just comment out the line below...
720                 } else {
721                         cutSelection(true, false);
722                 }
723                 moveCursor(cur, false);
724                 bv->owner()->view_state_changed();
725                 break;
726
727         case LFUN_DELETE_SKIP:
728                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
729                 if (!cur.selection()) {
730                         if (cursor().pos() == cursorPar()->size()) {
731                                 cursorRight(bv);
732                                 cursorLeft(bv);
733                                 Delete();
734                                 cur.resetAnchor();
735                         } else {
736                                 Delete();
737                                 cur.resetAnchor();
738                         }
739                 } else {
740                         cutSelection(true, false);
741                 }
742                 bv->update();
743                 break;
744
745
746         case LFUN_BACKSPACE:
747                 if (!cur.selection()) {
748                         if (bv->owner()->getIntl().getTransManager().backspace()) {
749                                 backspace();
750                                 cur.resetAnchor();
751                                 // It is possible to make it a lot faster still
752                                 // just comment out the line below...
753                         }
754                 } else {
755                         cutSelection(true, false);
756                 }
757                 bv->owner()->view_state_changed();
758                 bv->switchKeyMap();
759                 bv->update();
760                 break;
761
762         case LFUN_BACKSPACE_SKIP:
763                 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
764                 if (!cur.selection()) {
765 #warning look here
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                 cur.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                 cur.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 #warning look here
798 //              CursorSlice cur = cursor();
799                 replaceSelection(bv->getLyXText());
800                 if (cur.pos() == 0) {
801                         ParagraphParameters & params = getPar(cur.current())->params();
802                         setParagraph(
803                                         params.spacing(),
804                                         params.align(),
805                                         params.labelWidthString(), 1);
806                 } else {
807                         breakParagraph(bv->buffer()->paragraphs(), 0);
808                 }
809                 bv->update();
810 //      anchor() = cur;
811                 bv->switchKeyMap();
812                 bv->owner()->view_state_changed();
813                 break;
814         }
815
816         case LFUN_PARAGRAPH_SPACING: {
817                 ParagraphList::iterator pit = cursorPar();
818                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
819                 float cur_value = 1.0;
820                 if (cur_spacing == Spacing::Other)
821                         cur_value = pit->params().spacing().getValue();
822
823                 istringstream is(cmd.argument);
824                 string tmp;
825                 is >> tmp;
826                 Spacing::Space new_spacing = cur_spacing;
827                 float new_value = cur_value;
828                 if (tmp.empty()) {
829                         lyxerr << "Missing argument to `paragraph-spacing'"
830                                << endl;
831                 } else if (tmp == "single") {
832                         new_spacing = Spacing::Single;
833                 } else if (tmp == "onehalf") {
834                         new_spacing = Spacing::Onehalf;
835                 } else if (tmp == "double") {
836                         new_spacing = Spacing::Double;
837                 } else if (tmp == "other") {
838                         new_spacing = Spacing::Other;
839                         float tmpval = 0.0;
840                         is >> tmpval;
841                         lyxerr << "new_value = " << tmpval << endl;
842                         if (tmpval != 0.0)
843                                 new_value = tmpval;
844                 } else if (tmp == "default") {
845                         new_spacing = Spacing::Default;
846                 } else {
847                         lyxerr << _("Unknown spacing argument: ")
848                                << cmd.argument << endl;
849                 }
850                 if (cur_spacing != new_spacing || cur_value != new_value) {
851                         pit->params().spacing(Spacing(new_spacing, new_value));
852                         redoParagraph();
853                         bv->update();
854                 }
855                 break;
856         }
857
858         case LFUN_INSET_APPLY: {
859                 string const name = cmd.getArg(0);
860                 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
861                 if (inset)
862                         inset->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
863                 else
864                         dispatch(cur, FuncRequest(LFUN_INSET_INSERT, cmd.argument));
865                 break;
866         }
867
868         case LFUN_INSET_INSERT: {
869                 InsetBase * inset = createInset(bv, cmd);
870                 if (inset && !bv->insertInset(inset))
871                         delete inset;
872                 break;
873         }
874
875         case LFUN_INSET_SETTINGS:
876                 if (cur.inset() && cur.inset()->asUpdatableInset())
877                         cur.inset()->asUpdatableInset()->showInsetDialog(bv);
878                 break;
879
880         case LFUN_INSET_TOGGLE:
881                 cur.clearSelection();
882                 if (!toggleInset())
883                         return DispatchResult(false);
884                 bv->update();
885                 bv->switchKeyMap();
886                 break;
887
888         case LFUN_SPACE_INSERT:
889                 if (cursorPar()->layout()->free_spacing)
890                         insertChar(' ');
891                 else
892                         doInsertInset(bv, cmd, false, false);
893                 moveCursor(cur, false);
894                 break;
895
896         case LFUN_HYPHENATION:
897                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
898                 break;
899
900         case LFUN_LIGATURE_BREAK:
901                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
902                 break;
903
904         case LFUN_LDOTS:
905                 specialChar(this, bv, InsetSpecialChar::LDOTS);
906                 break;
907
908         case LFUN_END_OF_SENTENCE:
909                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
910                 break;
911
912         case LFUN_MENU_SEPARATOR:
913                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
914                 break;
915
916         case LFUN_UPCASE_WORD:
917                 changeCase(LyXText::text_uppercase);
918                 bv->update();
919                 break;
920
921         case LFUN_LOWCASE_WORD:
922                 changeCase(LyXText::text_lowercase);
923                 bv->update();
924                 break;
925
926         case LFUN_CAPITALIZE_WORD:
927                 changeCase(LyXText::text_capitalization);
928                 bv->update();
929                 break;
930
931         case LFUN_TRANSPOSE_CHARS:
932                 recUndo(cursor().par());
933                 redoParagraph();
934                 bv->update();
935                 break;
936
937         case LFUN_PASTE:
938                 cur.message(_("Paste"));
939                 replaceSelection(bv->getLyXText());
940 #warning FIXME Check if the arg is in the domain of available selections.
941                 if (isStrUnsignedInt(cmd.argument))
942                         pasteSelection(strToUnsignedInt(cmd.argument));
943                 else
944                         pasteSelection(0);
945                 cur.clearSelection(); // bug 393
946                 bv->update();
947                 bv->switchKeyMap();
948                 finishUndo();
949                 break;
950
951         case LFUN_CUT:
952                 cutSelection(true, true);
953                 cur.message(_("Cut"));
954                 bv->update();
955                 break;
956
957         case LFUN_COPY:
958                 copySelection();
959                 cur.message(_("Copy"));
960                 break;
961
962         case LFUN_BEGINNINGBUFSEL:
963                 if (in_inset_)
964                         return DispatchResult(false);
965                 if (!cur.selection())
966                         cur.resetAnchor();
967                 cursorTop();
968                 finishChange(bv, true);
969                 break;
970
971         case LFUN_ENDBUFSEL:
972                 if (in_inset_)
973                         return DispatchResult(false);
974                 if (!cur.selection())
975                         cur.resetAnchor();
976                 cursorBottom();
977                 finishChange(bv, true);
978                 break;
979
980         case LFUN_GETXY:
981                 cur.message(tostr(cursorX(cur.current())) + ' '
982                           + tostr(cursorY(cur.current())));
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                         cur.message("E");
1001                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1002                         cur.message("N");
1003                 else
1004                         cur.message("0");
1005                 break;
1006
1007         case LFUN_GETLAYOUT:
1008                 cur.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                         cur.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                         cur.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 && cur.selection() &&
1047                         cur.selBegin().par() != cur.selEnd().par())
1048                 {
1049                         ParagraphList::iterator spit = getPar(cur.selBegin());
1050                         ParagraphList::iterator epit = boost::next(getPar(cur.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                 cur.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(InsetBase::ERROR_CODE, false);
1085                 break;
1086
1087         case LFUN_GOTONOTE:
1088                 gotoInset(InsetBase::NOTE_CODE, false);
1089                 break;
1090
1091         case LFUN_REFERENCE_GOTO: {
1092                 vector<InsetOld_code> tmp;
1093                 tmp.push_back(InsetBase::LABEL_CODE);
1094                 tmp.push_back(InsetBase::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                 cur.resetAnchor();
1139                 moveCursor(cur, false);
1140                 break;
1141         }
1142
1143         case LFUN_MOUSE_TRIPLE:
1144                 if (bv->buffer() && cmd.button() == mouse_button::button1) {
1145                         selection_possible = true;
1146                         cursorHome();
1147                         cur.resetAnchor();
1148                         cursorEnd();
1149                         cur.setSelection();
1150                         bv->haveSelection(cur.selection());
1151                 }
1152                 break;
1153
1154         case LFUN_MOUSE_DOUBLE:
1155                 if (bv->buffer() && cmd.button() == mouse_button::button1) {
1156                         selection_possible = true;
1157                         selectWord(lyx::WHOLE_WORD_STRICT);
1158                         bv->haveSelection(cur.selection());
1159                 }
1160                 break;
1161
1162         case LFUN_MOUSE_MOTION: {
1163 #if 0
1164                 // Only use motion with button 1
1165                 //if (ev.button() != mouse_button::button1)
1166                 //      return false;
1167                 // don't set anchor_
1168                 bv->cursor().cursor_ = cur.cursor_;
1169
1170                 if (!bv->buffer())
1171                         break;
1172                 // The test for not selection possible is needed, that
1173                 // only motion events are used, where the bottom press
1174                 // event was on the drawing area too
1175                 if (!selection_possible) {
1176                         lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1177                                 "Dispatch: no selection possible\n";
1178                         break;
1179                 }
1180                 RowList::iterator cursorrow = cursorRow();
1181
1182                 setCursorFromCoordinates(cmd.x, cmd.y);
1183
1184                 // This is to allow jumping over large insets
1185                 // FIXME: shouldn't be top-text-specific
1186                 if (cursorrow == cursorRow() && !in_inset_) {
1187                         if (cmd.y - bv->top_y() >= bv->workHeight())
1188                                 cursorDown(true);
1189                         else if (cmd.y - bv->top_y() < 0)
1190                                 cursorUp(true);
1191                 }
1192                 cur.setSelection();
1193 #endif
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 && cur.selection()) {
1221                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1222                         paste_internally = true;
1223                 }
1224
1225                 selection_possible = true;
1226
1227                 // Clear the selection
1228                 cur.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(cur.current(), cmd.x, cmd.y);
1237                 cur.resetAnchor();
1238                 finishUndo();
1239                 cur.x_target() = cursorX(cur.current());
1240
1241                 // set cursor and anchor to this position
1242                 bv->cursor() = cur;
1243
1244                 if (bv->fitCursor())
1245                         selection_possible = false;
1246
1247                 // Insert primary selection with middle mouse
1248                 // if there is a local selection in the current buffer,
1249                 // insert this
1250                 if (cmd.button() == mouse_button::button2) {
1251                         if (paste_internally)
1252                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1253                         else
1254                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1255                         selection_possible = false;
1256                 }
1257                 break;
1258         }
1259
1260         case LFUN_MOUSE_RELEASE: {
1261                 if (!bv->buffer())
1262                         break;
1263
1264                 // do nothing if we used the mouse wheel
1265                 if (cmd.button() == mouse_button::button4
1266                  || cmd.button() == mouse_button::button5)
1267                         return DispatchResult(true, false);
1268
1269                 selection_possible = false;
1270
1271                 if (cmd.button() == mouse_button::button2)
1272                         break;
1273
1274                 // finish selection
1275                 if (cmd.button() == mouse_button::button1)
1276                         bv->haveSelection(cur.selection());
1277
1278                 bv->switchKeyMap();
1279                 bv->owner()->view_state_changed();
1280                 bv->owner()->updateMenubar();
1281                 bv->owner()->updateToolbar();
1282                 break;
1283         }
1284
1285         case LFUN_SELFINSERT: {
1286                 if (cmd.argument.empty())
1287                         break;
1288
1289                 // Automatically delete the currently selected
1290                 // text and replace it with what is being
1291                 // typed in now. Depends on lyxrc settings
1292                 // "auto_region_delete", which defaults to
1293                 // true (on).
1294
1295                 if (lyxrc.auto_region_delete) {
1296                         if (cur.selection())
1297                                 cutSelection(false, false);
1298                         bv->haveSelection(false);
1299                 }
1300
1301                 cur.clearSelection();
1302                 LyXFont const old_font = real_current_font;
1303
1304                 string::const_iterator cit = cmd.argument.begin();
1305                 string::const_iterator end = cmd.argument.end();
1306                 for (; cit != end; ++cit)
1307                         bv->owner()->getIntl().getTransManager().
1308                                 TranslateAndInsert(*cit, this);
1309
1310                 cur.resetAnchor();
1311                 moveCursor(cur, false);
1312
1313                 // real_current_font.number can change so we need to
1314                 // update the minibuffer
1315                 if (old_font != real_current_font)
1316                         bv->owner()->view_state_changed();
1317                 bv->updateScrollbar();
1318                 break;
1319         }
1320
1321         case LFUN_URL: {
1322                 InsetCommandParams p("url");
1323                 string const data = InsetCommandMailer::params2string("url", p);
1324                 bv->owner()->getDialogs().show("url", data, 0);
1325                 break;
1326         }
1327
1328         case LFUN_HTMLURL: {
1329                 InsetCommandParams p("htmlurl");
1330                 string const data = InsetCommandMailer::params2string("url", p);
1331                 bv->owner()->getDialogs().show("url", data, 0);
1332                 break;
1333         }
1334
1335         case LFUN_INSERT_LABEL: {
1336                 InsetCommandParams p("label");
1337                 string const data = InsetCommandMailer::params2string("label", p);
1338                 bv->owner()->getDialogs().show("label", data, 0);
1339                 break;
1340         }
1341
1342
1343 #if 0
1344         case LFUN_INSET_LIST:
1345         case LFUN_INSET_THEOREM:
1346         case LFUN_INSET_CAPTION:
1347 #endif
1348         case LFUN_INSERT_NOTE:
1349         case LFUN_INSERT_CHARSTYLE:
1350         case LFUN_INSERT_BOX:
1351         case LFUN_INSERT_BRANCH:
1352         case LFUN_INSERT_BIBITEM:
1353         case LFUN_INSET_ERT:
1354         case LFUN_INSET_FLOAT:
1355         case LFUN_INSET_FOOTNOTE:
1356         case LFUN_INSET_MARGINAL:
1357         case LFUN_INSET_OPTARG:
1358         case LFUN_INSET_WIDE_FLOAT:
1359         case LFUN_INSET_WRAP:
1360         case LFUN_TABULAR_INSERT:
1361         case LFUN_ENVIRONMENT_INSERT:
1362                 // Open the inset, and move the current selection
1363                 // inside it.
1364                 doInsertInset(bv, cmd, true, true);
1365                 break;
1366
1367         case LFUN_INDEX_INSERT:
1368                 // Just open the inset
1369                 doInsertInset(bv, cmd, true, false);
1370                 break;
1371
1372         case LFUN_INDEX_PRINT:
1373         case LFUN_TOC_INSERT:
1374         case LFUN_HFILL:
1375         case LFUN_INSERT_LINE:
1376         case LFUN_INSERT_PAGEBREAK:
1377                 // do nothing fancy
1378                 doInsertInset(bv, cmd, false, false);
1379                 break;
1380
1381         case LFUN_DEPTH_MIN:
1382                 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH);
1383                 bv->update();
1384                 break;
1385
1386         case LFUN_DEPTH_PLUS:
1387                 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH);
1388                 bv->update();
1389                 break;
1390
1391         case LFUN_MATH_DELIM:
1392         case LFUN_MATH_DISPLAY:
1393         case LFUN_INSERT_MATH:
1394         case LFUN_MATH_LIMITS:
1395         case LFUN_MATH_MACRO:
1396         case LFUN_MATH_MUTATE:
1397         case LFUN_MATH_SPACE:
1398         case LFUN_MATH_IMPORT_SELECTION:
1399         case LFUN_MATH_MODE:
1400         case LFUN_MATH_NONUMBER:
1401         case LFUN_MATH_NUMBER:
1402         case LFUN_MATH_EXTERN:
1403         case LFUN_MATH_SIZE:
1404                 mathDispatch(cur, cmd);
1405                 break;
1406
1407         case LFUN_EMPH:
1408                 emph(bv, this);
1409                 bv->owner()->view_state_changed();
1410                 break;
1411
1412         case LFUN_BOLD:
1413                 bold(bv, this);
1414                 bv->owner()->view_state_changed();
1415                 break;
1416
1417         case LFUN_NOUN:
1418                 noun(bv, this);
1419                 bv->owner()->view_state_changed();
1420                 break;
1421
1422         case LFUN_CODE:
1423                 code(bv, this);
1424                 bv->owner()->view_state_changed();
1425                 break;
1426
1427         case LFUN_SANS:
1428                 sans(bv, this);
1429                 bv->owner()->view_state_changed();
1430                 break;
1431
1432         case LFUN_ROMAN:
1433                 roman(bv, this);
1434                 bv->owner()->view_state_changed();
1435                 break;
1436
1437         case LFUN_DEFAULT:
1438                 styleReset(bv, this);
1439                 bv->owner()->view_state_changed();
1440                 break;
1441
1442         case LFUN_UNDERLINE:
1443                 underline(bv, this);
1444                 bv->owner()->view_state_changed();
1445                 break;
1446
1447         case LFUN_FONT_SIZE:
1448                 fontSize(bv, cmd.argument, this);
1449                 bv->owner()->view_state_changed();
1450                 break;
1451
1452         case LFUN_LANGUAGE:
1453                 lang(bv, cmd.argument, this);
1454                 bv->switchKeyMap();
1455                 bv->owner()->view_state_changed();
1456                 break;
1457
1458         case LFUN_FREEFONT_APPLY:
1459                 apply_freefont(bv, this);
1460                 break;
1461
1462         case LFUN_FREEFONT_UPDATE:
1463                 update_and_apply_freefont(bv, this, cmd.argument);
1464                 break;
1465
1466         case LFUN_FINISHED_LEFT:
1467                 lyxerr << "handle LFUN_FINISHED_LEFT" << endl;
1468                 cur.pop(cur.currentDepth());
1469                 cur.bv().cursor() = cur;
1470                 if (rtl())
1471                         cursorLeft(true);
1472                 break;
1473
1474         case LFUN_FINISHED_RIGHT:
1475                 lyxerr << "handle LFUN_FINISHED_RIGHT" << endl;
1476                 cur.pop(cur.currentDepth());
1477                 cur.bv().cursor() = cur;
1478                 if (!rtl())
1479                         cursorRight(true);
1480                 break;
1481
1482         case LFUN_FINISHED_UP:
1483                 lyxerr << "handle LFUN_FINISHED_UP" << endl;
1484                 cur.pop(cur.currentDepth());
1485                 cur.bv().cursor() = cur;
1486                 cursorUp(true);
1487                 break;
1488
1489         case LFUN_FINISHED_DOWN:
1490                 lyxerr << "handle LFUN_FINISHED_DOWN" << endl;
1491                 cur.pop(cur.currentDepth());
1492                 cur.bv().cursor() = cur;
1493                 cursorDown(true);
1494                 break;
1495
1496         default:
1497                 return DispatchResult(false);
1498         }
1499
1500         return DispatchResult(true, true);
1501 }