]> git.lyx.org Git - lyx.git/blob - src/text3.C
c9461f4648afe8aed4d7d87a0c07b315db3388e4
[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 (cursorPar() == firstPar() && 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 (cursorPar() == 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                         CursorSlice cur = cursor();
766                         backspace();
767                         anchor() = cur;
768                 } else {
769                         cutSelection(true, false);
770                 }
771                 bv->update();
772                 break;
773
774         case LFUN_BREAKPARAGRAPH:
775                 replaceSelection(bv->getLyXText());
776                 breakParagraph(bv->buffer()->paragraphs(), 0);
777                 bv->update();
778                 cur.resetAnchor();
779                 bv->switchKeyMap();
780                 bv->owner()->view_state_changed();
781                 break;
782
783         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
784                 replaceSelection(bv->getLyXText());
785                 breakParagraph(bv->buffer()->paragraphs(), 1);
786                 bv->update();
787                 cur.resetAnchor();
788                 bv->switchKeyMap();
789                 bv->owner()->view_state_changed();
790                 break;
791
792         case LFUN_BREAKPARAGRAPH_SKIP: {
793                 // When at the beginning of a paragraph, remove
794                 // indentation and add a "defskip" at the top.
795                 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
796                 CursorSlice cur = cursor();
797                 replaceSelection(bv->getLyXText());
798                 if (cur.pos() == 0) {
799                         ParagraphParameters & params = getPar(cur)->params();
800                         setParagraph(
801                                         params.spacing(),
802                                         params.align(),
803                                         params.labelWidthString(), 1);
804                 } else {
805                         breakParagraph(bv->buffer()->paragraphs(), 0);
806                 }
807                 bv->update();
808                 anchor() = cur;
809                 bv->switchKeyMap();
810                 bv->owner()->view_state_changed();
811                 break;
812         }
813
814         case LFUN_PARAGRAPH_SPACING: {
815                 ParagraphList::iterator pit = cursorPar();
816                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
817                 float cur_value = 1.0;
818                 if (cur_spacing == Spacing::Other)
819                         cur_value = pit->params().spacing().getValue();
820
821                 istringstream is(cmd.argument);
822                 string tmp;
823                 is >> tmp;
824                 Spacing::Space new_spacing = cur_spacing;
825                 float new_value = cur_value;
826                 if (tmp.empty()) {
827                         lyxerr << "Missing argument to `paragraph-spacing'"
828                                << endl;
829                 } else if (tmp == "single") {
830                         new_spacing = Spacing::Single;
831                 } else if (tmp == "onehalf") {
832                         new_spacing = Spacing::Onehalf;
833                 } else if (tmp == "double") {
834                         new_spacing = Spacing::Double;
835                 } else if (tmp == "other") {
836                         new_spacing = Spacing::Other;
837                         float tmpval = 0.0;
838                         is >> tmpval;
839                         lyxerr << "new_value = " << tmpval << endl;
840                         if (tmpval != 0.0)
841                                 new_value = tmpval;
842                 } else if (tmp == "default") {
843                         new_spacing = Spacing::Default;
844                 } else {
845                         lyxerr << _("Unknown spacing argument: ")
846                                << cmd.argument << endl;
847                 }
848                 if (cur_spacing != new_spacing || cur_value != new_value) {
849                         pit->params().spacing(Spacing(new_spacing, new_value));
850                         redoParagraph();
851                         bv->update();
852                 }
853                 break;
854         }
855
856         case LFUN_INSET_APPLY: {
857                 string const name = cmd.getArg(0);
858                 InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
859                 if (inset)
860                         inset->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
861                 else
862                         dispatch(cur, FuncRequest(LFUN_INSET_INSERT, cmd.argument));
863                 break;
864         }
865
866         case LFUN_INSET_INSERT: {
867                 InsetBase * inset = createInset(bv, cmd);
868                 if (inset && !bv->insertInset(inset))
869                         delete inset;
870                 break;
871         }
872
873         case LFUN_INSET_SETTINGS:
874                 if (cur.inset() && cur.inset()->asUpdatableInset())
875                         cur.inset()->asUpdatableInset()->showInsetDialog(bv);
876                 break;
877
878         case LFUN_INSET_TOGGLE:
879                 cur.clearSelection();
880                 if (!toggleInset())
881                         return DispatchResult(false);
882                 bv->update();
883                 bv->switchKeyMap();
884                 break;
885
886         case LFUN_SPACE_INSERT:
887                 if (cursorPar()->layout()->free_spacing)
888                         insertChar(' ');
889                 else
890                         doInsertInset(bv, cmd, false, false);
891                 moveCursor(cur, false);
892                 break;
893
894         case LFUN_HYPHENATION:
895                 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
896                 break;
897
898         case LFUN_LIGATURE_BREAK:
899                 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
900                 break;
901
902         case LFUN_LDOTS:
903                 specialChar(this, bv, InsetSpecialChar::LDOTS);
904                 break;
905
906         case LFUN_END_OF_SENTENCE:
907                 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
908                 break;
909
910         case LFUN_MENU_SEPARATOR:
911                 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
912                 break;
913
914         case LFUN_UPCASE_WORD:
915                 changeCase(LyXText::text_uppercase);
916                 bv->update();
917                 break;
918
919         case LFUN_LOWCASE_WORD:
920                 changeCase(LyXText::text_lowercase);
921                 bv->update();
922                 break;
923
924         case LFUN_CAPITALIZE_WORD:
925                 changeCase(LyXText::text_capitalization);
926                 bv->update();
927                 break;
928
929         case LFUN_TRANSPOSE_CHARS:
930                 recUndo(cursor().par());
931                 redoParagraph();
932                 bv->update();
933                 break;
934
935         case LFUN_PASTE:
936                 cmd.message(_("Paste"));
937                 replaceSelection(bv->getLyXText());
938 #warning FIXME Check if the arg is in the domain of available selections.
939                 if (isStrUnsignedInt(cmd.argument))
940                         pasteSelection(strToUnsignedInt(cmd.argument));
941                 else
942                         pasteSelection(0);
943                 cur.clearSelection(); // bug 393
944                 bv->update();
945                 bv->switchKeyMap();
946                 finishUndo();
947                 break;
948
949         case LFUN_CUT:
950                 cutSelection(true, true);
951                 cmd.message(_("Cut"));
952                 bv->update();
953                 break;
954
955         case LFUN_COPY:
956                 copySelection();
957                 cmd.message(_("Copy"));
958                 break;
959
960         case LFUN_BEGINNINGBUFSEL:
961                 if (in_inset_)
962                         return DispatchResult(false);
963                 if (!cur.selection())
964                         cur.resetAnchor();
965                 cursorTop();
966                 finishChange(bv, true);
967                 break;
968
969         case LFUN_ENDBUFSEL:
970                 if (in_inset_)
971                         return DispatchResult(false);
972                 if (!cur.selection())
973                         cur.resetAnchor();
974                 cursorBottom();
975                 finishChange(bv, true);
976                 break;
977
978         case LFUN_GETXY:
979                 cmd.message(tostr(cursorX(cur.current())) + ' '
980                           + tostr(cursorY(cur.current())));
981                 break;
982
983         case LFUN_SETXY: {
984                 int x = 0;
985                 int y = 0;
986                 istringstream is(cmd.argument);
987                 is >> x >> y;
988                 if (!is)
989                         lyxerr << "SETXY: Could not parse coordinates in '"
990                                << cmd.argument << std::endl;
991                 else
992                         setCursorFromCoordinates(x, y);
993                 break;
994         }
995
996         case LFUN_GETFONT:
997                 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
998                         cmd.message("E");
999                 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
1000                         cmd.message("N");
1001                 else
1002                         cmd.message("0");
1003                 break;
1004
1005         case LFUN_GETLAYOUT:
1006                 cmd.message(cursorPar()->layout()->name());
1007                 break;
1008
1009         case LFUN_LAYOUT: {
1010                 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1011                   << cmd.argument << endl;
1012
1013                 // This is not the good solution to the empty argument
1014                 // problem, but it will hopefully suffice for 1.2.0.
1015                 // The correct solution would be to augument the
1016                 // function list/array with information about what
1017                 // functions needs arguments and their type.
1018                 if (cmd.argument.empty()) {
1019                         cmd.errorMessage(_("LyX function 'layout' needs an argument."));
1020                         break;
1021                 }
1022
1023                 // Derive layout number from given argument (string)
1024                 // and current buffer's textclass (number)
1025                 LyXTextClass const & tclass = bv->buffer()->params().getLyXTextClass();
1026                 bool hasLayout = tclass.hasLayout(cmd.argument);
1027                 string layout = cmd.argument;
1028
1029                 // If the entry is obsolete, use the new one instead.
1030                 if (hasLayout) {
1031                         string const & obs = tclass[layout]->obsoleted_by();
1032                         if (!obs.empty())
1033                                 layout = obs;
1034                 }
1035
1036                 if (!hasLayout) {
1037                         cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
1038                                 N_(" not known"));
1039                         break;
1040                 }
1041
1042                 bool change_layout = (current_layout != layout);
1043
1044                 if (!change_layout && cur.selection() &&
1045                         cur.selBegin().par() != cur.selEnd().par())
1046                 {
1047                         ParagraphList::iterator spit = getPar(cur.selBegin());
1048                         ParagraphList::iterator epit = boost::next(getPar(cur.selEnd()));
1049                         while (spit != epit) {
1050                                 if (spit->layout()->name() != current_layout) {
1051                                         change_layout = true;
1052                                         break;
1053                                 }
1054                                 ++spit;
1055                         }
1056                 }
1057
1058                 if (change_layout) {
1059                         current_layout = layout;
1060                         setLayout(layout);
1061                         bv->owner()->setLayout(layout);
1062                         bv->update();
1063                         bv->switchKeyMap();
1064                 }
1065                 break;
1066         }
1067
1068         case LFUN_PASTESELECTION: {
1069                 cur.clearSelection();
1070                 string const clip = bv->getClipboard();
1071                 if (!clip.empty()) {
1072                         if (cmd.argument == "paragraph")
1073                                 insertStringAsParagraphs(clip);
1074                         else
1075                                 insertStringAsLines(clip);
1076                         bv->update();
1077                 }
1078                 break;
1079         }
1080
1081         case LFUN_GOTOERROR:
1082                 gotoInset(InsetBase::ERROR_CODE, false);
1083                 break;
1084
1085         case LFUN_GOTONOTE:
1086                 gotoInset(InsetBase::NOTE_CODE, false);
1087                 break;
1088
1089         case LFUN_REFERENCE_GOTO: {
1090                 vector<InsetOld_code> tmp;
1091                 tmp.push_back(InsetBase::LABEL_CODE);
1092                 tmp.push_back(InsetBase::REF_CODE);
1093                 gotoInset(tmp, true);
1094                 break;
1095         }
1096
1097         case LFUN_QUOTE: {
1098                 replaceSelection(bv->getLyXText());
1099                 ParagraphList::iterator pit = cursorPar();
1100                 lyx::pos_type pos = cursor().pos();
1101                 char c;
1102                 if (!pos)
1103                         c = ' ';
1104                 else if (pit->isInset(pos - 1) && pit->getInset(pos - 1)->isSpace())
1105                         c = ' ';
1106                 else
1107                         c = pit->getChar(pos - 1);
1108
1109                 LyXLayout_ptr const & style = pit->layout();
1110
1111                 BufferParams const & bufparams = bv->buffer()->params();
1112                 if (style->pass_thru ||
1113                     pit->getFontSettings(bufparams,pos).language()->lang() == "hebrew" ||
1114                     !bv->insertInset(new InsetQuotes(c, bufparams)))
1115                         bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1116                 break;
1117         }
1118
1119         case LFUN_DATE_INSERT: {
1120                 replaceSelection(bv->getLyXText());
1121                 time_t now_time_t = time(NULL);
1122                 struct tm * now_tm = localtime(&now_time_t);
1123                 setlocale(LC_TIME, "");
1124                 string arg;
1125                 if (!cmd.argument.empty())
1126                         arg = cmd.argument;
1127                 else
1128                         arg = lyxrc.date_insert_format;
1129                 char datetmp[32];
1130                 int const datetmp_len =
1131                         ::strftime(datetmp, 32, arg.c_str(), now_tm);
1132
1133                 for (int i = 0; i < datetmp_len; i++)
1134                         insertChar(datetmp[i]);
1135
1136                 cur.resetAnchor();
1137                 moveCursor(cur, false);
1138                 break;
1139         }
1140
1141         case LFUN_MOUSE_TRIPLE:
1142                 if (bv->buffer() && cmd.button() == mouse_button::button1) {
1143                         selection_possible = true;
1144                         cursorHome();
1145                         cur.resetAnchor();
1146                         cursorEnd();
1147                         cur.setSelection();
1148                         bv->haveSelection(cur.selection());
1149                 }
1150                 break;
1151
1152         case LFUN_MOUSE_DOUBLE:
1153                 if (bv->buffer() && cmd.button() == mouse_button::button1) {
1154                         selection_possible = true;
1155                         selectWord(lyx::WHOLE_WORD_STRICT);
1156                         bv->haveSelection(cur.selection());
1157                 }
1158                 break;
1159
1160         case LFUN_MOUSE_MOTION: {
1161 #if 0
1162                 // Only use motion with button 1
1163                 //if (ev.button() != mouse_button::button1)
1164                 //      return false;
1165                 // don't set anchor_
1166                 bv->cursor().cursor_ = cur.cursor_;
1167
1168                 if (!bv->buffer())
1169                         break;
1170                 // The test for not selection possible is needed, that
1171                 // only motion events are used, where the bottom press
1172                 // event was on the drawing area too
1173                 if (!selection_possible) {
1174                         lyxerr[Debug::ACTION] << "BufferView::Pimpl::"
1175                                 "Dispatch: no selection possible\n";
1176                         break;
1177                 }
1178                 RowList::iterator cursorrow = cursorRow();
1179
1180                 setCursorFromCoordinates(cmd.x, cmd.y);
1181
1182                 // This is to allow jumping over large insets
1183                 // FIXME: shouldn't be top-text-specific
1184                 if (cursorrow == cursorRow() && !in_inset_) {
1185                         if (cmd.y - bv->top_y() >= bv->workHeight())
1186                                 cursorDown(true);
1187                         else if (cmd.y - bv->top_y() < 0)
1188                                 cursorUp(true);
1189                 }
1190                 cur.setSelection();
1191 #endif
1192                 break;
1193         }
1194
1195         // Single-click on work area
1196         case LFUN_MOUSE_PRESS: {
1197                 if (!bv->buffer())
1198                         break;
1199
1200                 // ok ok, this is a hack (for xforms)
1201                 // We shouldn't go further down as we really should only do the
1202                 // scrolling and be done with this. Otherwise we may open some
1203                 // dialogs (Jug 20020424).
1204                 if (cmd.button() == mouse_button::button4) {
1205                         bv->scroll(-lyxrc.wheel_jump);
1206                         break;
1207                 }
1208                 if (cmd.button() == mouse_button::button5) {
1209                         bv->scroll(lyxrc.wheel_jump);
1210                         break;
1211                 }
1212
1213                 // Middle button press pastes if we have a selection
1214                 // We do this here as if the selection was inside an inset
1215                 // it could get cleared on the unlocking of the inset so
1216                 // we have to check this first
1217                 bool paste_internally = false;
1218                 if (cmd.button() == mouse_button::button2 && cur.selection()) {
1219                         bv->owner()->dispatch(FuncRequest(LFUN_COPY));
1220                         paste_internally = true;
1221                 }
1222
1223                 selection_possible = true;
1224
1225                 // Clear the selection
1226                 cur.clearSelection();
1227
1228                 // Right click on a footnote flag opens float menu
1229                 if (cmd.button() == mouse_button::button3) {
1230                         selection_possible = false;
1231                         break;
1232                 }
1233
1234                 setCursorFromCoordinates(cur.current(), cmd.x, cmd.y);
1235                 cur.resetAnchor();
1236                 finishUndo();
1237                 cur.x_target() = cursorX(cur.current());
1238
1239                 // set cursor and anchor to this position
1240                 bv->cursor() = cur;
1241
1242                 if (bv->fitCursor())
1243                         selection_possible = false;
1244
1245                 // Insert primary selection with middle mouse
1246                 // if there is a local selection in the current buffer,
1247                 // insert this
1248                 if (cmd.button() == mouse_button::button2) {
1249                         if (paste_internally)
1250                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTE));
1251                         else
1252                                 bv->owner()->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1253                         selection_possible = false;
1254                 }
1255                 break;
1256         }
1257
1258         case LFUN_MOUSE_RELEASE: {
1259                 if (!bv->buffer())
1260                         break;
1261
1262                 // do nothing if we used the mouse wheel
1263                 if (cmd.button() == mouse_button::button4
1264                  || cmd.button() == mouse_button::button5)
1265                         return DispatchResult(true, false);
1266
1267                 selection_possible = false;
1268
1269                 if (cmd.button() == mouse_button::button2)
1270                         break;
1271
1272                 // finish selection
1273                 if (cmd.button() == mouse_button::button1)
1274                         bv->haveSelection(cur.selection());
1275
1276                 bv->switchKeyMap();
1277                 bv->owner()->view_state_changed();
1278                 bv->owner()->updateMenubar();
1279                 bv->owner()->updateToolbar();
1280                 break;
1281         }
1282
1283         case LFUN_SELFINSERT: {
1284                 if (cmd.argument.empty())
1285                         break;
1286
1287                 // Automatically delete the currently selected
1288                 // text and replace it with what is being
1289                 // typed in now. Depends on lyxrc settings
1290                 // "auto_region_delete", which defaults to
1291                 // true (on).
1292
1293                 if (lyxrc.auto_region_delete) {
1294                         if (cur.selection())
1295                                 cutSelection(false, false);
1296                         bv->haveSelection(false);
1297                 }
1298
1299                 cur.clearSelection();
1300                 LyXFont const old_font = real_current_font;
1301
1302                 string::const_iterator cit = cmd.argument.begin();
1303                 string::const_iterator end = cmd.argument.end();
1304                 for (; cit != end; ++cit)
1305                         bv->owner()->getIntl().getTransManager().
1306                                 TranslateAndInsert(*cit, this);
1307
1308                 cur.resetAnchor();
1309                 moveCursor(cur, false);
1310
1311                 // real_current_font.number can change so we need to
1312                 // update the minibuffer
1313                 if (old_font != real_current_font)
1314                         bv->owner()->view_state_changed();
1315                 bv->updateScrollbar();
1316                 break;
1317         }
1318
1319         case LFUN_URL: {
1320                 InsetCommandParams p("url");
1321                 string const data = InsetCommandMailer::params2string("url", p);
1322                 bv->owner()->getDialogs().show("url", data, 0);
1323                 break;
1324         }
1325
1326         case LFUN_HTMLURL: {
1327                 InsetCommandParams p("htmlurl");
1328                 string const data = InsetCommandMailer::params2string("url", p);
1329                 bv->owner()->getDialogs().show("url", data, 0);
1330                 break;
1331         }
1332
1333         case LFUN_INSERT_LABEL: {
1334                 InsetCommandParams p("label");
1335                 string const data = InsetCommandMailer::params2string("label", p);
1336                 bv->owner()->getDialogs().show("label", data, 0);
1337                 break;
1338         }
1339
1340
1341 #if 0
1342         case LFUN_INSET_LIST:
1343         case LFUN_INSET_THEOREM:
1344         case LFUN_INSET_CAPTION:
1345 #endif
1346         case LFUN_INSERT_NOTE:
1347         case LFUN_INSERT_CHARSTYLE:
1348         case LFUN_INSERT_BOX:
1349         case LFUN_INSERT_BRANCH:
1350         case LFUN_INSERT_BIBITEM:
1351         case LFUN_INSET_ERT:
1352         case LFUN_INSET_FLOAT:
1353         case LFUN_INSET_FOOTNOTE:
1354         case LFUN_INSET_MARGINAL:
1355         case LFUN_INSET_OPTARG:
1356         case LFUN_INSET_WIDE_FLOAT:
1357         case LFUN_INSET_WRAP:
1358         case LFUN_TABULAR_INSERT:
1359         case LFUN_ENVIRONMENT_INSERT:
1360                 // Open the inset, and move the current selection
1361                 // inside it.
1362                 doInsertInset(bv, cmd, true, true);
1363                 break;
1364
1365         case LFUN_INDEX_INSERT:
1366                 // Just open the inset
1367                 doInsertInset(bv, cmd, true, false);
1368                 break;
1369
1370         case LFUN_INDEX_PRINT:
1371         case LFUN_TOC_INSERT:
1372         case LFUN_HFILL:
1373         case LFUN_INSERT_LINE:
1374         case LFUN_INSERT_PAGEBREAK:
1375                 // do nothing fancy
1376                 doInsertInset(bv, cmd, false, false);
1377                 break;
1378
1379         case LFUN_DEPTH_MIN:
1380                 bv_funcs::changeDepth(bv, this, bv_funcs::DEC_DEPTH);
1381                 bv->update();
1382                 break;
1383
1384         case LFUN_DEPTH_PLUS:
1385                 bv_funcs::changeDepth(bv, this, bv_funcs::INC_DEPTH);
1386                 bv->update();
1387                 break;
1388
1389         case LFUN_MATH_DELIM:
1390         case LFUN_MATH_DISPLAY:
1391         case LFUN_INSERT_MATH:
1392         case LFUN_MATH_LIMITS:
1393         case LFUN_MATH_MACRO:
1394         case LFUN_MATH_MUTATE:
1395         case LFUN_MATH_SPACE:
1396         case LFUN_MATH_IMPORT_SELECTION:
1397         case LFUN_MATH_MODE:
1398         case LFUN_MATH_NONUMBER:
1399         case LFUN_MATH_NUMBER:
1400         case LFUN_MATH_EXTERN:
1401         case LFUN_MATH_SIZE:
1402                 mathDispatch(cur, cmd);
1403                 break;
1404
1405         case LFUN_EMPH:
1406                 emph(bv, this);
1407                 bv->owner()->view_state_changed();
1408                 break;
1409
1410         case LFUN_BOLD:
1411                 bold(bv, this);
1412                 bv->owner()->view_state_changed();
1413                 break;
1414
1415         case LFUN_NOUN:
1416                 noun(bv, this);
1417                 bv->owner()->view_state_changed();
1418                 break;
1419
1420         case LFUN_CODE:
1421                 code(bv, this);
1422                 bv->owner()->view_state_changed();
1423                 break;
1424
1425         case LFUN_SANS:
1426                 sans(bv, this);
1427                 bv->owner()->view_state_changed();
1428                 break;
1429
1430         case LFUN_ROMAN:
1431                 roman(bv, this);
1432                 bv->owner()->view_state_changed();
1433                 break;
1434
1435         case LFUN_DEFAULT:
1436                 styleReset(bv, this);
1437                 bv->owner()->view_state_changed();
1438                 break;
1439
1440         case LFUN_UNDERLINE:
1441                 underline(bv, this);
1442                 bv->owner()->view_state_changed();
1443                 break;
1444
1445         case LFUN_FONT_SIZE:
1446                 fontSize(bv, cmd.argument, this);
1447                 bv->owner()->view_state_changed();
1448                 break;
1449
1450         case LFUN_LANGUAGE:
1451                 lang(bv, cmd.argument, this);
1452                 bv->switchKeyMap();
1453                 bv->owner()->view_state_changed();
1454                 break;
1455
1456         case LFUN_FREEFONT_APPLY:
1457                 apply_freefont(bv, this);
1458                 break;
1459
1460         case LFUN_FREEFONT_UPDATE:
1461                 update_and_apply_freefont(bv, this, cmd.argument);
1462                 break;
1463
1464         case LFUN_FINISHED_LEFT:
1465                 lyxerr << "swallow LFUN_FINISHED_LEFT" << endl;
1466                 if (rtl())
1467                         cursorLeft(true);
1468                 break;
1469
1470         case LFUN_FINISHED_RIGHT:
1471                 lyxerr << "swallow LFUN_FINISHED_RIGHT" << endl;
1472                 if (!rtl())
1473                         cursorRight(true);
1474                 break;
1475
1476         case LFUN_FINISHED_UP:
1477                 lyxerr << "swallow LFUN_FINISHED_UP" << endl;
1478                 cursorUp(true);
1479                 break;
1480
1481         case LFUN_FINISHED_DOWN:
1482                 lyxerr << "swallow LFUN_FINISHED_DOWN" << endl;
1483                 cursorDown(true);
1484                 break;
1485
1486         default:
1487                 return DispatchResult(false);
1488         }
1489
1490         return DispatchResult(true, true);
1491 }