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