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