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