1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2002 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
20 #include "bufferparams.h"
22 #include "ParagraphParameters.h"
25 #include "support/lstrings.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/screen.h"
28 #include "frontends/WorkArea.h"
29 #include "insets/insetspecialchar.h"
30 #include "insets/insettext.h"
31 #include "insets/insetcommand.h"
32 #include "undo_funcs.h"
36 extern string current_layout;
39 bool LyXText::gotoNextInset(BufferView * bv,
40 vector<Inset::Code> const & codes, string const & contents) const
42 LyXCursor res = cursor;
45 if (res.pos() < res.par()->size() - 1) {
46 res.pos(res.pos() + 1);
48 res.par(res.par()->next());
53 !(res.par()->isInset(res.pos())
54 && (inset = res.par()->getInset(res.pos())) != 0
55 && find(codes.begin(), codes.end(), inset->lyxCode())
57 && (contents.empty() ||
58 static_cast<InsetCommand *>(
59 res.par()->getInset(res.pos()))->getContents()
63 setCursor(bv, res.par(), res.pos(), false);
70 void LyXText::gotoInset(BufferView * bv, vector<Inset::Code> const & codes,
74 bv->beforeChange(this);
78 if (same_content && cursor.par()->isInset(cursor.pos())) {
79 Inset const * inset = cursor.par()->getInset(cursor.pos());
80 if (find(codes.begin(), codes.end(), inset->lyxCode())
82 contents = static_cast<InsetCommand const *>(inset)->getContents();
85 if (!gotoNextInset(bv, codes, contents)) {
86 if (cursor.pos() || cursor.par() != ownerParagraph()) {
87 LyXCursor tmp = cursor;
88 cursor.par(ownerParagraph());
90 if (!gotoNextInset(bv, codes, contents)) {
92 bv->owner()->message(_("No more insets"));
95 bv->owner()->message(_("No more insets"));
99 selection.cursor = cursor;
103 void LyXText::gotoInset(BufferView * bv, Inset::Code code, bool same_content)
105 gotoInset(bv, vector<Inset::Code>(1, code), same_content);
109 void LyXText::cursorPrevious(BufferView * bv)
111 if (!cursor.row()->previous()) {
113 int new_y = bv->text->first_y - bv->workarea().workHeight();
114 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
115 bv->updateScrollbar();
121 Row * cursorrow = cursor.row();
123 setCursorFromCoordinates(bv, cursor.x_fix(), y);
127 if (cursorrow == bv->text->cursor.row()) {
128 // we have a row which is higher than the workarea so we leave the
129 // cursor on the start of the row and move only the draw up as soon
130 // as we move the cursor or do something while inside the row (it may
131 // span several workarea-heights) we'll move to the top again, but this
132 // is better than just jump down and only display part of the row.
133 new_y = bv->text->first_y - bv->workarea().workHeight();
136 new_y = bv->text->cursor.iy()
137 + bv->theLockingInset()->insetInInsetY() + y
138 + cursor.row()->height()
139 - bv->workarea().workHeight() + 1;
142 - cursor.row()->baseline()
143 + cursor.row()->height()
144 - bv->workarea().workHeight() + 1;
147 bv->screen().draw(bv->text, bv, new_y < 0 ? 0 : new_y);
148 if (cursor.row()->previous()) {
150 setCursor(bv, cur, cursor.row()->previous()->par(),
151 cursor.row()->previous()->pos(), false);
152 if (cur.y() > first_y) {
156 bv->updateScrollbar();
160 void LyXText::cursorNext(BufferView * bv)
162 if (!cursor.row()->next()) {
163 int y = cursor.y() - cursor.row()->baseline() +
164 cursor.row()->height();
165 if (y > int(first_y + bv->workarea().workHeight())) {
166 bv->screen().draw(bv->text, bv,
167 bv->text->first_y + bv->workarea().workHeight());
168 bv->updateScrollbar();
173 int y = first_y + bv->workarea().workHeight();
174 if (inset_owner && !first_y) {
175 y -= (bv->text->cursor.iy()
177 + bv->theLockingInset()->insetInInsetY());
182 Row * cursorrow = cursor.row();
183 setCursorFromCoordinates(bv, cursor.x_fix(), y);
184 // + workarea().workHeight());
188 if (cursorrow == bv->text->cursor.row()) {
189 // we have a row which is higher than the workarea so we leave the
190 // cursor on the start of the row and move only the draw down as soon
191 // as we move the cursor or do something while inside the row (it may
192 // span several workarea-heights) we'll move to the top again, but this
193 // is better than just jump down and only display part of the row.
194 new_y = bv->text->first_y + bv->workarea().workHeight();
197 new_y = bv->text->cursor.iy()
198 + bv->theLockingInset()->insetInInsetY()
199 + y - cursor.row()->baseline();
201 new_y = cursor.y() - cursor.row()->baseline();
204 bv->screen().draw(bv->text, bv, new_y);
205 if (cursor.row()->next()) {
207 setCursor(bv, cur, cursor.row()->next()->par(),
208 cursor.row()->next()->pos(), false);
209 if (cur.y() < int(first_y + bv->workarea().workHeight())) {
210 cursorDown(bv, true);
213 bv->updateScrollbar();
217 void LyXText::update(BufferView * bv, bool changed)
219 BufferView::UpdateCodes c = BufferView::SELECT | BufferView::FITCUR;
221 bv->update(this, c | BufferView::CHANGE);
227 void specialChar(LyXText * lt, BufferView * bv, InsetSpecialChar::Kind kind)
231 InsetSpecialChar * new_inset = new InsetSpecialChar(kind);
232 if (!bv->insertInset(new_inset))
235 bv->updateInset(new_inset, true);
239 Inset::RESULT LyXText::dispatch(FuncRequest const & cmd)
241 lyxerr[Debug::ACTION] << "LyXFunc::dispatch: action[" << cmd.action
242 <<"] arg[" << cmd.argument << "]" << endl;
244 BufferView * bv = cmd.view();
246 switch (cmd.action) {
248 case LFUN_APPENDIX: {
249 Paragraph * par = cursor.par();
250 bool start = !par->params().startOfAppendix();
252 // ensure that we have only one start_of_appendix in this document
253 Paragraph * tmp = ownerParagraph();
254 for (; tmp; tmp = tmp->next())
255 tmp->params().startOfAppendix(false);
257 par->params().startOfAppendix(start);
259 // we can set the refreshing parameters now
260 status(cmd.view(), LyXText::NEED_MORE_REFRESH);
262 refresh_row = 0; // not needed for full update
263 updateCounters(cmd.view());
264 setCursor(cmd.view(), cursor.par(), cursor.pos());
269 case LFUN_DELETE_WORD_FORWARD:
270 bv->beforeChange(this);
272 deleteWordForward(bv);
277 case LFUN_DELETE_WORD_BACKWARD:
278 bv->beforeChange(this);
280 deleteWordBackward(bv);
285 case LFUN_DELETE_LINE_FORWARD:
286 bv->beforeChange(this);
288 deleteLineForward(bv);
295 if (!selection.mark())
296 bv->beforeChange(this);
303 if (!selection.mark())
304 bv->beforeChange(this);
306 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
307 cursorLeftOneWord(bv);
309 cursorRightOneWord(bv);
314 if (!selection.mark())
315 bv->beforeChange(this);
317 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
318 cursorRightOneWord(bv);
320 cursorLeftOneWord(bv);
324 case LFUN_BEGINNINGBUF:
325 if (!selection.mark())
326 bv->beforeChange(this);
333 if (selection.mark())
334 bv->beforeChange(this);
342 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
346 bv->finishChange(true);
351 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
355 bv->finishChange(true);
361 bv->finishChange(true);
366 cursorDown(bv, true);
367 bv->finishChange(true);
370 case LFUN_UP_PARAGRAPHSEL:
372 cursorUpParagraph(bv);
373 bv->finishChange(true);
376 case LFUN_DOWN_PARAGRAPHSEL:
378 cursorDownParagraph(bv);
379 bv->finishChange(true);
385 bv->finishChange(true);
397 bv->finishChange(true);
403 bv->finishChange(true);
406 case LFUN_WORDRIGHTSEL:
408 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
409 cursorLeftOneWord(bv);
411 cursorRightOneWord(bv);
412 bv->finishChange(true);
415 case LFUN_WORDLEFTSEL:
417 if (cursor.par()->isRightToLeftPar(bv->buffer()->params))
418 cursorRightOneWord(bv);
420 cursorLeftOneWord(bv);
421 bv->finishChange(true);
425 bool is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
426 if (!selection.mark())
427 bv->beforeChange(this);
430 cursorLeft(bv, false);
431 if (cursor.pos() < cursor.par()->size()
432 && cursor.par()->isInset(cursor.pos())
433 && isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
434 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
435 cmd.message(tmpinset->editMessage());
436 tmpinset->edit(bv, !is_rtl);
440 cursorRight(bv, false);
441 bv->finishChange(false);
446 // This is soooo ugly. Isn`t it possible to make
448 bool const is_rtl = cursor.par()->isRightToLeftPar(bv->buffer()->params);
449 if (!selection.mark())
450 bv->beforeChange(this);
452 LyXCursor const cur = cursor;
454 cursorLeft(bv, false);
455 if ((is_rtl || cur != cursor) && // only if really moved!
456 cursor.pos() < cursor.par()->size() &&
457 cursor.par()->isInset(cursor.pos()) &&
458 isHighlyEditableInset(cursor.par()->getInset(cursor.pos()))) {
459 Inset * tmpinset = cursor.par()->getInset(cursor.pos());
460 cmd.message(tmpinset->editMessage());
461 tmpinset->edit(bv, is_rtl);
465 cursorRight(bv, false);
466 bv->finishChange(false);
471 if (!selection.mark())
472 bv->beforeChange(this);
473 bv->update(this, BufferView::UPDATE);
475 bv->finishChange(false);
479 if (!selection.mark())
480 bv->beforeChange(this);
481 bv->update(this, BufferView::UPDATE);
486 case LFUN_UP_PARAGRAPH:
487 if (!selection.mark())
488 bv->beforeChange(this);
489 bv->update(this, BufferView::UPDATE);
490 cursorUpParagraph(bv);
494 case LFUN_DOWN_PARAGRAPH:
495 if (!selection.mark())
496 bv->beforeChange(this);
497 bv->update(this, BufferView::UPDATE);
498 cursorDownParagraph(bv);
499 bv->finishChange(false);
503 if (!selection.mark())
504 bv->beforeChange(this);
505 bv->update(this, BufferView::UPDATE);
507 bv->finishChange(false);
510 // moveCursorUpdate(false, false);
511 // owner_->view_state_changed();
515 if (!selection.mark())
516 bv->beforeChange(this);
517 bv->update(this, BufferView::UPDATE);
519 bv->finishChange(false);
523 if (!selection.mark())
524 bv->beforeChange(this);
527 bv->finishChange(false);
531 if (!selection.mark())
532 bv->beforeChange(this);
535 bv->finishChange(false);
539 bv->beforeChange(this);
540 insertChar(bv, Paragraph::META_NEWLINE);
542 setCursor(bv, cursor.par(), cursor.pos());
543 bv->moveCursorUpdate(false);
547 if (!selection.set()) {
549 selection.cursor = cursor;
551 // It is possible to make it a lot faster still
552 // just comment out the line below...
556 cutSelection(bv, true);
559 bv->moveCursorUpdate(false);
560 bv->owner()->view_state_changed();
564 case LFUN_DELETE_SKIP:
565 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
566 if (!selection.set()) {
567 LyXCursor cur = cursor;
568 if (cur.pos() == cur.par()->size()) {
572 && !(cur.par()->params().spaceTop()
573 == VSpace (VSpace::NONE))) {
575 cur.par()->params().lineTop(),
576 cur.par()->params().lineBottom(),
577 cur.par()->params().pagebreakTop(),
578 cur.par()->params().pagebreakBottom(),
579 VSpace(VSpace::NONE),
580 cur.par()->params().spaceBottom(),
581 cur.par()->params().spacing(),
582 cur.par()->params().align(),
583 cur.par()->params().labelWidthString(), 0);
589 selection.cursor = cursor;
593 selection.cursor = cursor;
597 cutSelection(bv, true);
604 if (!selection.set()) {
605 if (bv->owner()->getIntl().getTransManager().backspace()) {
607 selection.cursor = cursor;
609 // It is possible to make it a lot faster still
610 // just comment out the line below...
615 cutSelection(bv, true);
618 bv->owner()->view_state_changed();
622 case LFUN_BACKSPACE_SKIP:
623 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
624 if (!selection.set()) {
625 LyXCursor cur = cursor;
627 && !(cur.par()->params().spaceTop()
628 == VSpace (VSpace::NONE))) {
630 cur.par()->params().lineTop(),
631 cur.par()->params().lineBottom(),
632 cur.par()->params().pagebreakTop(),
633 cur.par()->params().pagebreakBottom(),
634 VSpace(VSpace::NONE), cur.par()->params().spaceBottom(),
635 cur.par()->params().spacing(),
636 cur.par()->params().align(),
637 cur.par()->params().labelWidthString(), 0);
640 selection.cursor = cur;
644 cutSelection(bv, true);
649 case LFUN_BREAKPARAGRAPH:
650 bv->beforeChange(this);
651 breakParagraph(bv, 0);
653 selection.cursor = cursor;
655 bv->owner()->view_state_changed();
658 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
659 bv->beforeChange(this);
660 breakParagraph(bv, 1);
662 selection.cursor = cursor;
664 bv->owner()->view_state_changed();
667 case LFUN_BREAKPARAGRAPH_SKIP: {
668 // When at the beginning of a paragraph, remove
669 // indentation and add a "defskip" at the top.
670 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
671 LyXCursor cur = cursor;
672 bv->beforeChange(this);
673 if (cur.pos() == 0) {
674 if (cur.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
676 cur.par()->params().lineTop(),
677 cur.par()->params().lineBottom(),
678 cur.par()->params().pagebreakTop(),
679 cur.par()->params().pagebreakBottom(),
680 VSpace(VSpace::DEFSKIP), cur.par()->params().spaceBottom(),
681 cur.par()->params().spacing(),
682 cur.par()->params().align(),
683 cur.par()->params().labelWidthString(), 1);
688 breakParagraph(bv, 0);
692 selection.cursor = cur;
694 bv->owner()->view_state_changed();
698 case LFUN_PARAGRAPH_SPACING: {
699 Paragraph * par = cursor.par();
700 Spacing::Space cur_spacing = par->params().spacing().getSpace();
701 float cur_value = 1.0;
702 if (cur_spacing == Spacing::Other)
703 cur_value = par->params().spacing().getValue();
705 istringstream is(cmd.argument.c_str());
708 Spacing::Space new_spacing = cur_spacing;
709 float new_value = cur_value;
711 lyxerr << "Missing argument to `paragraph-spacing'"
713 } else if (tmp == "single") {
714 new_spacing = Spacing::Single;
715 } else if (tmp == "onehalf") {
716 new_spacing = Spacing::Onehalf;
717 } else if (tmp == "double") {
718 new_spacing = Spacing::Double;
719 } else if (tmp == "other") {
720 new_spacing = Spacing::Other;
723 lyxerr << "new_value = " << tmpval << endl;
726 } else if (tmp == "default") {
727 new_spacing = Spacing::Default;
729 lyxerr << _("Unknown spacing argument: ")
730 << cmd.argument << endl;
732 if (cur_spacing != new_spacing || cur_value != new_value) {
733 par->params().spacing(Spacing(new_spacing, new_value));
740 case LFUN_INSET_TOGGLE:
742 bv->beforeChange(this);
749 case LFUN_PROTECTEDSPACE:
750 if (cursor.par()->layout()->free_spacing) {
754 specialChar(this, bv, InsetSpecialChar::PROTECTED_SEPARATOR);
756 bv->moveCursorUpdate(false);
759 case LFUN_HYPHENATION:
760 specialChar(this, bv, InsetSpecialChar::HYPHENATION);
763 case LFUN_LIGATURE_BREAK:
764 specialChar(this, bv, InsetSpecialChar::LIGATURE_BREAK);
768 specialChar(this, bv, InsetSpecialChar::LDOTS);
774 insertChar(bv, Paragraph::META_HFILL);
778 case LFUN_END_OF_SENTENCE:
779 specialChar(this, bv, InsetSpecialChar::END_OF_SENTENCE);
782 case LFUN_MENU_SEPARATOR:
783 specialChar(this, bv, InsetSpecialChar::MENU_SEPARATOR);
787 bv->beforeChange(this);
789 selection.cursor = cursor;
790 cmd.message(N_("Mark off"));
794 bv->beforeChange(this);
795 selection.mark(true);
797 selection.cursor = cursor;
798 cmd.message(N_("Mark on"));
802 bv->beforeChange(this);
803 if (selection.mark()) {
805 cmd.message(N_("Mark removed"));
807 selection.mark(true);
809 cmd.message(N_("Mark set"));
811 selection.cursor = cursor;
814 case LFUN_UPCASE_WORD:
816 changeCase(bv, LyXText::text_uppercase);
818 bv->updateInset(inset_owner, true);
822 case LFUN_LOWCASE_WORD:
824 changeCase(bv, LyXText::text_lowercase);
826 bv->updateInset(inset_owner, true);
830 case LFUN_CAPITALIZE_WORD:
832 changeCase(bv, LyXText::text_capitalization);
834 bv->updateInset(inset_owner, true);
838 case LFUN_TRANSPOSE_CHARS:
842 bv->updateInset(inset_owner, true);
847 cmd.message(_("Paste"));
849 // clear the selection
850 bv->toggleSelection();
854 clearSelection(); // bug 393
863 cutSelection(bv, true);
865 cmd.message(_("Cut"));
870 cmd.message(_("Copy"));
873 case LFUN_BEGINNINGBUFSEL:
875 return Inset::UNDISPATCHED;
878 bv->finishChange(true);
883 return Inset::UNDISPATCHED;
886 bv->finishChange(true);
890 cmd.message(tostr(cursor.x()) + ' ' + tostr(cursor.y()));
896 istringstream is(cmd.argument.c_str());
899 lyxerr << "SETXY: Could not parse coordinates in '"
900 << cmd.argument << std::endl;
902 setCursorFromCoordinates(bv, x, y);
907 if (current_font.shape() == LyXFont::ITALIC_SHAPE)
909 else if (current_font.shape() == LyXFont::SMALLCAPS_SHAPE)
916 cmd.message(tostr(cursor.par()->layout()));
920 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
921 << cmd.argument << endl;
923 // This is not the good solution to the empty argument
924 // problem, but it will hopefully suffice for 1.2.0.
925 // The correct solution would be to augument the
926 // function list/array with information about what
927 // functions needs arguments and their type.
928 if (cmd.argument.empty()) {
929 cmd.errorMessage(_("LyX function 'layout' needs an argument."));
933 // Derive layout number from given argument (string)
934 // and current buffer's textclass (number)
935 LyXTextClass const & tclass = bv->buffer()->params.getLyXTextClass();
936 bool hasLayout = tclass.hasLayout(cmd.argument);
937 string layout = cmd.argument;
939 // If the entry is obsolete, use the new one instead.
941 string const & obs = tclass[layout]->obsoleted_by();
947 cmd.errorMessage(string(N_("Layout ")) + cmd.argument +
952 bool change_layout = (current_layout != layout);
953 if (!change_layout && selection.set() &&
954 selection.start.par() != selection.end.par())
956 Paragraph * spar = selection.start.par();
957 Paragraph * epar = selection.end.par()->next();
958 while (spar != epar) {
959 if (spar->layout()->name() != current_layout) {
960 change_layout = true;
967 current_layout = layout;
969 setLayout(bv, layout);
970 bv->owner()->setLayout(layout);
977 case LFUN_PASTESELECTION: {
981 // this was originally a beforeChange(bv->text), i.e
982 // the outermost LyXText!
983 bv->beforeChange(this);
984 string const clip(bv->workarea().getClipboard());
986 if (cmd.argument == "paragraph")
987 insertStringAsParagraphs(bv, clip);
989 insertStringAsLines(bv, clip);
997 gotoInset(bv, Inset::ERROR_CODE, false);
1001 gotoInset(bv, Inset::NOTE_CODE, false);
1004 case LFUN_REFERENCE_GOTO:
1006 vector<Inset::Code> tmp;
1007 tmp.push_back(Inset::LABEL_CODE);
1008 tmp.push_back(Inset::REF_CODE);
1009 gotoInset(bv, tmp, true);
1013 case LFUN_SELFINSERT: {
1014 if (cmd.argument.empty())
1017 // Automatically delete the currently selected
1018 // text and replace it with what is being
1019 // typed in now. Depends on lyxrc settings
1020 // "auto_region_delete", which defaults to
1023 if (lyxrc.auto_region_delete) {
1024 if (selection.set()) {
1025 cutSelection(bv, false, false);
1028 bv->workarea().haveSelection(false);
1031 bv->beforeChange(this);
1032 LyXFont const old_font(real_current_font);
1034 string::const_iterator cit = cmd.argument.begin();
1035 string::const_iterator end = cmd.argument.end();
1036 for (; cit != end; ++cit)
1037 bv->owner()->getIntl().getTransManager().
1038 TranslateAndInsert(*cit, this);
1041 selection.cursor = cursor;
1042 bv->moveCursorUpdate(false);
1044 // real_current_font.number can change so we need to
1045 // update the minibuffer
1046 if (old_font != real_current_font)
1047 bv->owner()->view_state_changed();
1052 return Inset::UNDISPATCHED;
1055 return Inset::DISPATCHED;