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