3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
25 #include "buffer_funcs.h"
26 #include "bufferparams.h"
27 #include "BufferView.h"
31 #include "CutAndPaste.h"
33 #include "dispatchresult.h"
34 #include "errorlist.h"
36 #include "FloatList.h"
37 #include "funcrequest.h"
43 #include "lyxrow_funcs.h"
44 #include "paragraph.h"
45 #include "paragraph_funcs.h"
46 #include "ParagraphParameters.h"
47 #include "PosIterator.h"
51 #include "frontends/font_metrics.h"
52 #include "frontends/LyXView.h"
54 #include "insets/insetbibitem.h"
55 #include "insets/insetenv.h"
56 #include "insets/insetfloat.h"
57 #include "insets/insetwrap.h"
59 #include "support/lstrings.h"
60 #include "support/textutils.h"
61 #include "support/tostr.h"
62 #include "support/std_sstream.h"
64 #include <boost/tuple/tuple.hpp>
67 using lyx::paroffset_type;
68 using lyx::support::bformat;
71 using std::ostringstream;
75 LyXText::LyXText(BufferView * bv, bool in_inset)
76 : height(0), width(0), textwidth_(bv ? bv->workWidth() : 100),
77 background_color_(LColor::background),
78 bv_owner(bv), in_inset_(in_inset), xo_(0), yo_(0)
82 void LyXText::init(BufferView * bview)
86 ParagraphList::iterator const beg = paragraphs().begin();
87 ParagraphList::iterator const end = paragraphs().end();
88 for (ParagraphList::iterator pit = beg; pit != end; ++pit)
94 current_font = getFont(beg, 0);
96 redoParagraphs(beg, end);
97 setCursorIntern(0, 0);
104 // Gets the fully instantiated font at a given position in a paragraph
105 // Basically the same routine as Paragraph::getFont() in paragraph.C.
106 // The difference is that this one is used for displaying, and thus we
107 // are allowed to make cosmetic improvements. For instance make footnotes
109 LyXFont LyXText::getFont(ParagraphList::iterator pit, pos_type pos) const
111 BOOST_ASSERT(pos >= 0);
113 LyXLayout_ptr const & layout = pit->layout();
115 BufferParams const & params = bv()->buffer()->params();
116 pos_type const body_pos = pit->beginOfBody();
118 // We specialize the 95% common case:
119 if (!pit->getDepth()) {
120 LyXFont f = pit->getFontSettings(params, pos);
123 if (layout->labeltype == LABEL_MANUAL && pos < body_pos)
124 return f.realize(layout->reslabelfont);
126 return f.realize(layout->resfont);
129 // The uncommon case need not be optimized as much
132 layoutfont = layout->labelfont;
134 layoutfont = layout->font;
136 LyXFont font = pit->getFontSettings(params, pos);
137 font.realize(layoutfont);
142 // Realize with the fonts of lesser depth.
143 //font.realize(outerFont(pit, paragraphs()));
144 font.realize(defaultfont_);
150 LyXFont LyXText::getLayoutFont(ParagraphList::iterator pit) const
152 LyXLayout_ptr const & layout = pit->layout();
154 if (!pit->getDepth())
155 return layout->resfont;
157 LyXFont font = layout->font;
158 // Realize with the fonts of lesser depth.
159 //font.realize(outerFont(pit, paragraphs()));
160 font.realize(defaultfont_);
166 LyXFont LyXText::getLabelFont(ParagraphList::iterator pit) const
168 LyXLayout_ptr const & layout = pit->layout();
170 if (!pit->getDepth())
171 return layout->reslabelfont;
173 LyXFont font = layout->labelfont;
174 // Realize with the fonts of lesser depth.
175 font.realize(outerFont(pit, paragraphs()));
176 font.realize(defaultfont_);
182 void LyXText::setCharFont(
183 ParagraphList::iterator pit, pos_type pos, LyXFont const & fnt)
186 LyXLayout_ptr const & layout = pit->layout();
188 // Get concrete layout font to reduce against
191 if (pos < pit->beginOfBody())
192 layoutfont = layout->labelfont;
194 layoutfont = layout->font;
196 // Realize against environment font information
197 if (pit->getDepth()) {
198 ParagraphList::iterator tp = pit;
199 while (!layoutfont.resolved() &&
200 tp != paragraphs().end() &&
202 tp = outerHook(tp, paragraphs());
203 if (tp != paragraphs().end())
204 layoutfont.realize(tp->layout()->font);
208 layoutfont.realize(defaultfont_);
210 // Now, reduce font against full layout font
211 font.reduce(layoutfont);
213 pit->setFont(pos, font);
217 InsetOld * LyXText::getInset() const
219 ParagraphList::iterator pit = cursorPar();
220 pos_type const pos = cursor().pos();
222 if (pos < pit->size() && pit->isInset(pos)) {
223 return pit->getInset(pos);
229 bool LyXText::toggleInset()
231 InsetOld * inset = getInset();
232 // is there an editable inset at cursor position?
233 if (!isEditableInset(inset))
235 //bv()->owner()->message(inset->editMessage());
237 // do we want to keep this?? (JMarc)
238 if (!isHighlyEditableInset(inset))
239 recUndo(cursor().par());
250 // Asger is not sure we want to do this...
251 void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
254 LyXLayout_ptr const & layout = par.layout();
255 pos_type const psize = par.size();
258 for (pos_type pos = 0; pos < psize; ++pos) {
259 if (pos < par.beginOfBody())
260 layoutfont = layout->labelfont;
262 layoutfont = layout->font;
264 LyXFont tmpfont = par.getFontSettings(params, pos);
265 tmpfont.reduce(layoutfont);
266 par.setFont(pos, tmpfont);
271 // return past-the-last paragraph influenced by a layout change on pit
272 ParagraphList::iterator
273 LyXText::undoSpan(ParagraphList::iterator pit)
275 ParagraphList::iterator end = paragraphs().end();
276 ParagraphList::iterator nextpit = boost::next(pit);
279 //because of parindents
280 if (!pit->getDepth())
281 return boost::next(nextpit);
282 //because of depth constrains
283 for (; nextpit != end; ++pit, ++nextpit) {
284 if (!pit->getDepth())
291 ParagraphList::iterator
292 LyXText::setLayout(ParagraphList::iterator start,
293 ParagraphList::iterator end,
294 string const & layout)
296 BOOST_ASSERT(start != end);
297 ParagraphList::iterator undopit = undoSpan(boost::prior(end));
298 recUndo(parOffset(start), parOffset(undopit) - 1);
300 BufferParams const & bufparams = bv()->buffer()->params();
301 LyXLayout_ptr const & lyxlayout =
302 bufparams.getLyXTextClass()[layout];
304 for (ParagraphList::iterator pit = start; pit != end; ++pit) {
305 pit->applyLayout(lyxlayout);
306 makeFontEntriesLayoutSpecific(bufparams, *pit);
307 if (lyxlayout->margintype == MARGIN_MANUAL)
308 pit->setLabelWidthString(lyxlayout->labelstring());
315 // set layout over selection and make a total rebreak of those paragraphs
316 void LyXText::setLayout(string const & layout)
318 // special handling of new environment insets
319 BufferParams const & params = bv()->buffer()->params();
320 LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
321 if (lyxlayout->is_environment) {
322 // move everything in a new environment inset
323 lyxerr << "setting layout " << layout << endl;
324 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
325 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
326 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
327 InsetOld * inset = new InsetEnvironment(params, layout);
328 if (bv()->insertInset(inset)) {
330 //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
336 ParagraphList::iterator start = getPar(selStart().par());
337 ParagraphList::iterator end = boost::next(getPar(selEnd().par()));
338 ParagraphList::iterator endpit = setLayout(start, end, layout);
340 redoParagraphs(start, endpit);
348 void getSelectionSpan(LyXText & text,
349 ParagraphList::iterator & beg,
350 ParagraphList::iterator & end)
352 if (!text.bv()->selection().set()) {
353 beg = text.cursorPar();
354 end = boost::next(beg);
356 beg = text.getPar(text.selStart());
357 end = boost::next(text.getPar(text.selEnd()));
362 bool changeDepthAllowed(bv_funcs::DEPTH_CHANGE type,
363 Paragraph const & par,
366 if (par.layout()->labeltype == LABEL_BIBLIO)
368 int const depth = par.params().depth();
369 if (type == bv_funcs::INC_DEPTH && depth < max_depth)
371 if (type == bv_funcs::DEC_DEPTH && depth > 0)
380 bool LyXText::changeDepthAllowed(bv_funcs::DEPTH_CHANGE type)
382 ParagraphList::iterator beg, end;
383 getSelectionSpan(*this, beg, end);
385 if (beg != paragraphs().begin())
386 max_depth = boost::prior(beg)->getMaxDepthAfter();
388 for (ParagraphList::iterator pit = beg; pit != end; ++pit) {
389 if (::changeDepthAllowed(type, *pit, max_depth))
391 max_depth = pit->getMaxDepthAfter();
397 void LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type)
399 ParagraphList::iterator beg, end;
400 getSelectionSpan(*this, beg, end);
402 recUndo(parOffset(beg), parOffset(end) - 1);
405 if (beg != paragraphs().begin())
406 max_depth = boost::prior(beg)->getMaxDepthAfter();
408 for (ParagraphList::iterator pit = beg; pit != end; ++pit) {
409 if (::changeDepthAllowed(type, *pit, max_depth)) {
410 int const depth = pit->params().depth();
411 if (type == bv_funcs::INC_DEPTH)
412 pit->params().depth(depth + 1);
414 pit->params().depth(depth - 1);
416 max_depth = pit->getMaxDepthAfter();
418 // this handles the counter labels, and also fixes up
419 // depth values for follow-on (child) paragraphs
424 // set font over selection and make a total rebreak of those paragraphs
425 void LyXText::setFont(LyXFont const & font, bool toggleall)
427 // if there is no selection just set the current_font
428 if (!bv()->selection().set()) {
429 // Determine basis font
431 if (cursor().pos() < cursorPar()->beginOfBody())
432 layoutfont = getLabelFont(cursorPar());
434 layoutfont = getLayoutFont(cursorPar());
436 // Update current font
437 real_current_font.update(font,
438 bv()->buffer()->params().language,
441 // Reduce to implicit settings
442 current_font = real_current_font;
443 current_font.reduce(layoutfont);
444 // And resolve it completely
445 real_current_font.realize(layoutfont);
450 // ok we have a selection.
451 recUndo(selStart().par(), selEnd().par());
454 ParagraphList::iterator beg = getPar(selStart().par());
455 ParagraphList::iterator end = getPar(selEnd().par());
457 PosIterator pos(¶graphs(), beg, selStart().pos());
458 PosIterator posend(¶graphs(), end, selEnd().pos());
460 BufferParams const & params = bv()->buffer()->params();
462 for (; pos != posend; ++pos) {
463 LyXFont f = getFont(pos.pit(), pos.pos());
464 f.update(font, params.language, toggleall);
465 setCharFont(pos.pit(), pos.pos(), f);
470 redoParagraphs(beg, ++end);
474 // the cursor set functions have a special mechanism. When they
475 // realize you left an empty paragraph, they will delete it.
477 void LyXText::cursorHome()
479 ParagraphList::iterator cpit = cursorPar();
480 setCursor(cpit, cpit->getRow(cursor().pos())->pos());
484 void LyXText::cursorEnd()
486 ParagraphList::iterator cpit = cursorPar();
487 pos_type end = cpit->getRow(cursor().pos())->endpos();
488 // if not on the last row of the par, put the cursor before
490 setCursor(cpit, end == cpit->size() ? end : end - 1);
494 void LyXText::cursorTop()
496 setCursor(paragraphs().begin(), 0);
500 void LyXText::cursorBottom()
502 ParagraphList::iterator lastpit =
503 boost::prior(paragraphs().end());
504 setCursor(lastpit, lastpit->size());
508 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
510 // If the mask is completely neutral, tell user
511 if (font == LyXFont(LyXFont::ALL_IGNORE)) {
512 // Could only happen with user style
513 bv()->owner()->message(_("No font change defined. "
514 "Use Character under the Layout menu to define font change."));
518 // Try implicit word selection
519 // If there is a change in the language the implicit word selection
521 CursorSlice resetCursor = cursor();
522 bool implicitSelection =
523 font.language() == ignore_language
524 && font.number() == LyXFont::IGNORE
525 && selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT);
528 setFont(font, toggleall);
530 // Implicit selections are cleared afterwards
531 //and cursor is set to the original position.
532 if (implicitSelection) {
534 cursor() = resetCursor;
540 string LyXText::getStringToIndex()
542 // Try implicit word selection
543 // If there is a change in the language the implicit word selection
545 CursorSlice const reset_cursor = cursor();
546 bool const implicitSelection =
547 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
550 if (!bv()->selection().set())
551 bv()->owner()->message(_("Nothing to index!"));
552 else if (selStart().par() != selEnd().par())
553 bv()->owner()->message(_("Cannot index more than one paragraph!"));
555 idxstring = selectionAsString(*bv()->buffer(), false);
557 // Reset cursors to their original position.
558 cursor() = reset_cursor;
561 // Clear the implicit selection.
562 if (implicitSelection)
569 // the DTP switches for paragraphs(). LyX will store them in the first
570 // physical paragraph. When a paragraph is broken, the top settings rest,
571 // the bottom settings are given to the new one. So I can make sure,
572 // they do not duplicate themself and you cannot play dirty tricks with
575 void LyXText::setParagraph(Spacing const & spacing, LyXAlignment align,
576 string const & labelwidthstring, bool noindent)
578 // make sure that the depth behind the selection are restored, too
579 ParagraphList::iterator undopit = undoSpan(getPar(selEnd()));
580 recUndo(selStart().par(), parOffset(undopit) - 1);
582 ParagraphList::reverse_iterator pit(getPar(selEnd().par()));
583 ParagraphList::reverse_iterator beg(getPar(selStart().par()));
585 for (--pit; pit != beg; ++pit) {
586 ParagraphParameters & params = pit->params();
587 params.spacing(spacing);
589 // does the layout allow the new alignment?
590 LyXLayout_ptr const & layout = pit->layout();
592 if (align == LYX_ALIGN_LAYOUT)
593 align = layout->align;
594 if (align & layout->alignpossible) {
595 if (align == layout->align)
596 params.align(LYX_ALIGN_LAYOUT);
600 pit->setLabelWidthString(labelwidthstring);
601 params.noindent(noindent);
604 redoParagraphs(getPar(selStart()), undopit);
608 string expandLabel(LyXTextClass const & textclass,
609 LyXLayout_ptr const & layout, bool appendix)
611 string fmt = appendix ?
612 layout->labelstring_appendix() : layout->labelstring();
614 // handle 'inherited level parts' in 'fmt',
615 // i.e. the stuff between '@' in '@Section@.\arabic{subsection}'
616 size_t const i = fmt.find('@', 0);
617 if (i != string::npos) {
618 size_t const j = fmt.find('@', i + 1);
619 if (j != string::npos) {
620 string parent(fmt, i + 1, j - i - 1);
621 string label = expandLabel(textclass, textclass[parent], appendix);
622 fmt = string(fmt, 0, i) + label + string(fmt, j + 1, string::npos);
626 return textclass.counters().counterLabel(fmt);
632 void incrementItemDepth(ParagraphList::iterator pit,
633 ParagraphList::iterator first_pit)
635 int const cur_labeltype = pit->layout()->labeltype;
637 if (cur_labeltype != LABEL_ENUMERATE && cur_labeltype != LABEL_ITEMIZE)
640 int const cur_depth = pit->getDepth();
642 ParagraphList::iterator prev_pit = boost::prior(pit);
644 int const prev_depth = prev_pit->getDepth();
645 int const prev_labeltype = prev_pit->layout()->labeltype;
646 if (prev_depth == 0 && cur_depth > 0) {
647 if (prev_labeltype == cur_labeltype) {
648 pit->itemdepth = prev_pit->itemdepth + 1;
651 } else if (prev_depth < cur_depth) {
652 if (prev_labeltype == cur_labeltype) {
653 pit->itemdepth = prev_pit->itemdepth + 1;
656 } else if (prev_depth == cur_depth) {
657 if (prev_labeltype == cur_labeltype) {
658 pit->itemdepth = prev_pit->itemdepth;
662 if (prev_pit == first_pit)
670 void resetEnumCounterIfNeeded(ParagraphList::iterator pit,
671 ParagraphList::iterator firstpit,
677 int const cur_depth = pit->getDepth();
678 ParagraphList::iterator prev_pit = boost::prior(pit);
680 int const prev_depth = prev_pit->getDepth();
681 int const prev_labeltype = prev_pit->layout()->labeltype;
682 if (prev_depth <= cur_depth) {
683 if (prev_labeltype != LABEL_ENUMERATE) {
684 switch (pit->itemdepth) {
686 counters.reset("enumi");
688 counters.reset("enumii");
690 counters.reset("enumiii");
692 counters.reset("enumiv");
698 if (prev_pit == firstpit)
708 // set the counter of a paragraph. This includes the labels
709 void LyXText::setCounter(Buffer const & buf, ParagraphList::iterator pit)
711 BufferParams const & bufparams = buf.params();
712 LyXTextClass const & textclass = bufparams.getLyXTextClass();
713 LyXLayout_ptr const & layout = pit->layout();
714 ParagraphList::iterator first_pit = paragraphs().begin();
715 Counters & counters = textclass.counters();
720 if (pit == first_pit) {
721 pit->params().appendix(pit->params().startOfAppendix());
723 pit->params().appendix(boost::prior(pit)->params().appendix());
724 if (!pit->params().appendix() &&
725 pit->params().startOfAppendix()) {
726 pit->params().appendix(true);
727 textclass.counters().reset();
730 // Maybe we have to increment the item depth.
731 incrementItemDepth(pit, first_pit);
734 // erase what was there before
735 pit->params().labelString(string());
737 if (layout->margintype == MARGIN_MANUAL) {
738 if (pit->params().labelWidthString().empty())
739 pit->setLabelWidthString(layout->labelstring());
741 pit->setLabelWidthString(string());
744 // is it a layout that has an automatic label?
745 if (layout->labeltype == LABEL_COUNTER) {
746 BufferParams const & bufparams = buf.params();
747 LyXTextClass const & textclass = bufparams.getLyXTextClass();
748 counters.step(layout->counter);
749 string label = expandLabel(textclass, layout, pit->params().appendix());
750 pit->params().labelString(label);
751 } else if (layout->labeltype == LABEL_ITEMIZE) {
752 // At some point of time we should do something more
753 // clever here, like:
754 // pit->params().labelString(
755 // bufparams.user_defined_bullet(pit->itemdepth).getText());
756 // for now, use a simple hardcoded label
758 switch (pit->itemdepth) {
773 pit->params().labelString(itemlabel);
774 } else if (layout->labeltype == LABEL_ENUMERATE) {
775 // Maybe we have to reset the enumeration counter.
776 resetEnumCounterIfNeeded(pit, first_pit, counters);
779 // Yes I know this is a really, really! bad solution
781 string enumcounter = "enum";
783 switch (pit->itemdepth) {
795 // not a valid enumdepth...
799 counters.step(enumcounter);
801 pit->params().labelString(counters.enumLabel(enumcounter));
802 } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
803 counters.step("bibitem");
804 int number = counters.value("bibitem");
805 if (pit->bibitem()) {
806 pit->bibitem()->setCounter(number);
807 pit->params().labelString(layout->labelstring());
809 // In biblio should't be following counters but...
811 string s = buf.B_(layout->labelstring());
814 if (layout->labeltype == LABEL_SENSITIVE) {
815 ParagraphList::iterator end = paragraphs().end();
816 ParagraphList::iterator tmppit = pit;
819 while (tmppit != end && tmppit->inInset()
820 // the single '=' is intended below
821 && (in = tmppit->inInset()->owner()))
823 if (in->lyxCode() == InsetOld::FLOAT_CODE ||
824 in->lyxCode() == InsetOld::WRAP_CODE) {
828 Paragraph const * owner = &ownerPar(buf, in);
830 for ( ; tmppit != end; ++tmppit)
831 if (&*tmppit == owner)
839 if (in->lyxCode() == InsetOld::FLOAT_CODE)
840 type = static_cast<InsetFloat*>(in)->params().type;
841 else if (in->lyxCode() == InsetOld::WRAP_CODE)
842 type = static_cast<InsetWrap*>(in)->params().type;
846 Floating const & fl = textclass.floats().getType(type);
848 counters.step(fl.type());
850 // Doesn't work... yet.
851 s = bformat(_("%1$s #:"), buf.B_(fl.name()));
853 // par->SetLayout(0);
854 // s = layout->labelstring;
855 s = _("Senseless: ");
858 pit->params().labelString(s);
864 // Updates all counters.
865 void LyXText::updateCounters()
868 bv()->buffer()->params().getLyXTextClass().counters().reset();
870 bool update_pos = false;
872 ParagraphList::iterator beg = paragraphs().begin();
873 ParagraphList::iterator end = paragraphs().end();
874 for (ParagraphList::iterator pit = beg; pit != end; ++pit) {
875 string const oldLabel = pit->params().labelString();
878 maxdepth = boost::prior(pit)->getMaxDepthAfter();
880 if (pit->params().depth() > maxdepth)
881 pit->params().depth(maxdepth);
883 // setCounter can potentially change the labelString.
884 setCounter(*bv()->buffer(), pit);
885 string const & newLabel = pit->params().labelString();
886 if (oldLabel != newLabel) {
887 redoParagraphInternal(pit);
893 updateParPositions();
897 void LyXText::insertInset(InsetOld * inset)
899 if (!cursorPar()->insetAllowed(inset->lyxCode()))
902 recUndo(cursor().par());
904 cursorPar()->insertInset(cursor().pos(), inset);
905 // Just to rebreak and refresh correctly.
906 // The character will not be inserted a second time
907 insertChar(Paragraph::META_INSET);
908 // If we enter a highly editable inset the cursor should be before
909 // the inset. After an undo LyX tries to call inset->edit(...)
910 // and fails if the cursor is behind the inset and getInset
911 // does not return the inset!
912 if (isHighlyEditableInset(inset))
919 void LyXText::cutSelection(bool doclear, bool realcut)
921 // Stuff what we got on the clipboard. Even if there is no selection.
923 // There is a problem with having the stuffing here in that the
924 // larger the selection the slower LyX will get. This can be
925 // solved by running the line below only when the selection has
926 // finished. The solution used currently just works, to make it
927 // faster we need to be more clever and probably also have more
928 // calls to stuffClipboard. (Lgb)
929 bv()->stuffClipboard(selectionAsString(*bv()->buffer(), true));
931 // This doesn't make sense, if there is no selection
932 if (!bv()->selection().set())
935 // OK, we have a selection. This is always between selStart()
938 // make sure that the depth behind the selection are restored, too
939 ParagraphList::iterator begpit = getPar(selStart().par());
940 ParagraphList::iterator endpit = getPar(selEnd().par());
941 ParagraphList::iterator undopit = undoSpan(endpit);
942 recUndo(selStart().par(), parOffset(undopit) - 1);
944 int endpos = selEnd().pos();
946 BufferParams const & bufparams = bv()->buffer()->params();
947 boost::tie(endpit, endpos) = realcut ?
948 CutAndPaste::cutSelection(bufparams,
951 selStart().pos(), endpos,
954 : CutAndPaste::eraseSelection(bufparams,
957 selStart().pos(), endpos,
959 // sometimes necessary
961 begpit->stripLeadingSpaces();
963 redoParagraphs(begpit, undopit);
964 // cutSelection can invalidate the cursor so we need to set
966 // we prefer the end for when tracking changes
967 cursor().pos(endpos);
968 cursor().par(parOffset(endpit));
970 // need a valid cursor. (Lgb)
976 void LyXText::copySelection()
978 // stuff the selection onto the X clipboard, from an explicit copy request
979 bv()->stuffClipboard(selectionAsString(*bv()->buffer(), true));
981 // this doesnt make sense, if there is no selection
982 if (!bv()->selection().set())
985 // ok we have a selection. This is always between selStart()
986 // and sel_end cursor
988 // copy behind a space if there is one
989 while (getPar(selStart())->size() > selStart().pos()
990 && getPar(selStart())->isLineSeparator(selStart().pos())
991 && (selStart().par() != selEnd().par()
992 || selStart().pos() < selEnd().pos()))
993 selStart().pos(selStart().pos() + 1);
995 CutAndPaste::copySelection(getPar(selStart().par()),
996 getPar(selEnd().par()),
999 bv()->buffer()->params().textclass);
1003 void LyXText::pasteSelection(size_t sel_index)
1005 // this does not make sense, if there is nothing to paste
1006 if (!CutAndPaste::checkPastePossible())
1009 recUndo(cursor().par());
1011 ParagraphList::iterator endpit;
1016 boost::tie(ppp, endpit) =
1017 CutAndPaste::pasteSelection(*bv()->buffer(),
1019 cursorPar(), cursor().pos(),
1020 bv()->buffer()->params().textclass,
1022 bufferErrors(*bv()->buffer(), el);
1023 bv()->showErrorList(_("Paste"));
1025 redoParagraphs(cursorPar(), endpit);
1029 bv()->resetAnchor();
1030 setCursor(ppp.first, ppp.second);
1036 void LyXText::setSelectionRange(lyx::pos_type length)
1041 bv()->resetAnchor();
1048 // simple replacing. The font of the first selected character is used
1049 void LyXText::replaceSelectionWithString(string const & str)
1051 recUndo(cursor().par());
1054 // Get font setting before we cut
1055 pos_type pos = selEnd().pos();
1056 LyXFont const font = getPar(selStart())
1057 ->getFontSettings(bv()->buffer()->params(),
1060 // Insert the new string
1061 string::const_iterator cit = str.begin();
1062 string::const_iterator end = str.end();
1063 for (; cit != end; ++cit) {
1064 getPar(selEnd())->insertChar(pos, (*cit), font);
1068 // Cut the selection
1069 cutSelection(true, false);
1075 // needed to insert the selection
1076 void LyXText::insertStringAsLines(string const & str)
1078 ParagraphList::iterator pit = cursorPar();
1079 pos_type pos = cursor().pos();
1080 ParagraphList::iterator endpit = boost::next(cursorPar());
1082 recUndo(cursor().par());
1084 // only to be sure, should not be neccessary
1087 bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1089 redoParagraphs(cursorPar(), endpit);
1090 bv()->resetAnchor();
1091 setCursor(pit, pos);
1096 // turn double CR to single CR, others are converted into one
1097 // blank. Then insertStringAsLines is called
1098 void LyXText::insertStringAsParagraphs(string const & str)
1100 string linestr(str);
1101 bool newline_inserted = false;
1102 string::size_type const siz = linestr.length();
1104 for (string::size_type i = 0; i < siz; ++i) {
1105 if (linestr[i] == '\n') {
1106 if (newline_inserted) {
1107 // we know that \r will be ignored by
1108 // insertStringAsLines. Of course, it is a dirty
1109 // trick, but it works...
1110 linestr[i - 1] = '\r';
1114 newline_inserted = true;
1116 } else if (IsPrintable(linestr[i])) {
1117 newline_inserted = false;
1120 insertStringAsLines(linestr);
1124 void LyXText::setCursor(ParagraphList::iterator pit, pos_type pos)
1126 setCursor(parOffset(pit), pos);
1130 bool LyXText::setCursor(paroffset_type par, pos_type pos, bool setfont,
1133 CursorSlice old_cursor = cursor();
1134 setCursorIntern(par, pos, setfont, boundary);
1135 return deleteEmptyParagraphMechanism(old_cursor);
1139 void LyXText::setCursor(CursorSlice & cur, paroffset_type par,
1140 pos_type pos, bool boundary)
1142 BOOST_ASSERT(par != int(paragraphs().size()));
1146 cur.boundary(boundary);
1148 // no rows, no fun...
1149 if (paragraphs().begin()->rows.empty())
1152 // now some strict checking
1153 ParagraphList::iterator pit = getPar(par);
1154 Row const & row = *pit->getRow(pos);
1155 pos_type const end = row.endpos();
1157 // None of these should happen, but we're scaredy-cats
1159 lyxerr << "dont like -1" << endl;
1162 BOOST_ASSERT(false);
1163 } else if (pos > pit->size()) {
1164 lyxerr << "dont like 1, pos: " << pos
1165 << " size: " << pit->size()
1166 << " row.pos():" << row.pos()
1167 << " paroffset: " << par << endl;
1170 BOOST_ASSERT(false);
1171 } else if (pos > end) {
1172 lyxerr << "dont like 2 please report" << endl;
1173 // This shouldn't happen.
1176 BOOST_ASSERT(false);
1177 } else if (pos < row.pos()) {
1178 lyxerr << "dont like 3 please report pos:" << pos
1179 << " size: " << pit->size()
1180 << " row.pos():" << row.pos()
1181 << " paroffset: " << par << endl;
1184 BOOST_ASSERT(false);
1189 void LyXText::setCursorIntern(paroffset_type par,
1190 pos_type pos, bool setfont, bool boundary)
1192 setCursor(cursor(), par, pos, boundary);
1193 bv()->x_target(cursorX() + xo_);
1199 void LyXText::setCurrentFont()
1201 pos_type pos = cursor().pos();
1202 ParagraphList::iterator pit = cursorPar();
1204 if (cursor().boundary() && pos > 0)
1208 if (pos == pit->size())
1210 else // potentional bug... BUG (Lgb)
1211 if (pit->isSeparator(pos)) {
1212 if (pos > pit->getRow(pos)->pos() &&
1213 bidi.level(pos) % 2 ==
1214 bidi.level(pos - 1) % 2)
1216 else if (pos + 1 < pit->size())
1221 BufferParams const & bufparams = bv()->buffer()->params();
1222 current_font = pit->getFontSettings(bufparams, pos);
1223 real_current_font = getFont(pit, pos);
1225 if (cursor().pos() == pit->size() &&
1226 bidi.isBoundary(*bv()->buffer(), *pit, cursor().pos()) &&
1227 !cursor().boundary()) {
1228 Language const * lang =
1229 pit->getParLanguage(bufparams);
1230 current_font.setLanguage(lang);
1231 current_font.setNumber(LyXFont::OFF);
1232 real_current_font.setLanguage(lang);
1233 real_current_font.setNumber(LyXFont::OFF);
1238 // returns the column near the specified x-coordinate of the row
1239 // x is set to the real beginning of this column
1240 pos_type LyXText::getColumnNearX(ParagraphList::iterator pit,
1241 Row const & row, int & x, bool & boundary) const
1243 double tmpx = row.x();
1244 double fill_separator = row.fill_separator();
1245 double fill_hfill = row.fill_hfill();
1246 double fill_label_hfill = row.fill_label_hfill();
1248 pos_type vc = row.pos();
1249 pos_type end = row.endpos();
1251 LyXLayout_ptr const & layout = pit->layout();
1253 bool left_side = false;
1255 pos_type body_pos = pit->beginOfBody();
1256 double last_tmpx = tmpx;
1259 (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1262 // check for empty row
1268 while (vc < end && tmpx <= x) {
1269 c = bidi.vis2log(vc);
1271 if (body_pos > 0 && c == body_pos - 1) {
1272 tmpx += fill_label_hfill +
1273 font_metrics::width(layout->labelsep, getLabelFont(pit));
1274 if (pit->isLineSeparator(body_pos - 1))
1275 tmpx -= singleWidth(pit, body_pos - 1);
1278 if (hfillExpansion(*pit, row, c)) {
1279 tmpx += singleWidth(pit, c);
1283 tmpx += fill_label_hfill;
1284 } else if (pit->isSeparator(c)) {
1285 tmpx += singleWidth(pit, c);
1287 tmpx += fill_separator;
1289 tmpx += singleWidth(pit, c);
1294 if ((tmpx + last_tmpx) / 2 > x) {
1299 BOOST_ASSERT(vc <= end); // This shouldn't happen.
1302 // This (rtl_support test) is not needed, but gives
1303 // some speedup if rtl_support == false
1304 bool const lastrow = lyxrc.rtl_support && row.endpos() == pit->size();
1306 // If lastrow is false, we don't need to compute
1307 // the value of rtl.
1308 bool const rtl = (lastrow)
1309 ? pit->isRightToLeftPar(bv()->buffer()->params())
1312 ((rtl && left_side && vc == row.pos() && x < tmpx - 5) ||
1313 (!rtl && !left_side && vc == end && x > tmpx + 5)))
1315 else if (vc == row.pos()) {
1316 c = bidi.vis2log(vc);
1317 if (bidi.level(c) % 2 == 1)
1320 c = bidi.vis2log(vc - 1);
1321 bool const rtl = (bidi.level(c) % 2 == 1);
1322 if (left_side == rtl) {
1324 boundary = bidi.isBoundary(*bv()->buffer(), *pit, c);
1328 if (row.pos() < end && c >= end && pit->isNewline(end - 1)) {
1329 if (bidi.level(end -1) % 2 == 0)
1330 tmpx -= singleWidth(pit, end - 1);
1332 tmpx += singleWidth(pit, end - 1);
1342 void LyXText::setCursorFromCoordinates(int x, int y)
1344 CursorSlice old_cursor = cursor();
1345 setCursorFromCoordinates(cursor(), x, y);
1347 deleteEmptyParagraphMechanism(old_cursor);
1351 // x,y are coordinates relative to this LyXText
1352 void LyXText::setCursorFromCoordinates(CursorSlice & cur, int x, int y)
1354 ParagraphList::iterator pit;
1355 Row const & row = *getRowNearY(y, pit);
1357 pos_type const column = getColumnNearX(pit, row, x, bound);
1358 cur.par(parOffset(pit));
1359 cur.pos(row.pos() + column);
1360 cur.boundary(bound);
1364 bool LyXText::checkAndActivateInset(bool front)
1366 if (cursor().pos() == cursorPar()->size())
1368 InsetOld * inset = cursorPar()->getInset(cursor().pos());
1369 if (!isHighlyEditableInset(inset))
1371 inset->edit(bv(), front);
1376 DispatchResult LyXText::moveRight()
1378 if (cursorPar()->isRightToLeftPar(bv()->buffer()->params()))
1379 return moveLeftIntern(false, true, false);
1381 return moveRightIntern(true, true, false);
1385 DispatchResult LyXText::moveLeft()
1387 if (cursorPar()->isRightToLeftPar(bv()->buffer()->params()))
1388 return moveRightIntern(true, true, false);
1390 return moveLeftIntern(false, true, false);
1394 DispatchResult LyXText::moveRightIntern(bool front, bool activate_inset, bool selecting)
1396 ParagraphList::iterator c_par = cursorPar();
1397 if (boost::next(c_par) == paragraphs().end()
1398 && cursor().pos() >= c_par->size())
1399 return DispatchResult(false, FINISHED_RIGHT);
1400 if (activate_inset && checkAndActivateInset(front))
1401 return DispatchResult(true, true);
1405 return DispatchResult(true);
1409 DispatchResult LyXText::moveLeftIntern(bool front,
1410 bool activate_inset, bool selecting)
1412 if (cursor().par() == 0 && cursor().pos() <= 0)
1413 return DispatchResult(false, FINISHED);
1417 if (activate_inset && checkAndActivateInset(front))
1418 return DispatchResult(true, true);
1419 return DispatchResult(true);
1423 DispatchResult LyXText::moveUp()
1425 if (cursorPar() == firstPar() && cursorRow() == firstRow())
1426 return DispatchResult(false, FINISHED_UP);
1429 return DispatchResult(true);
1433 DispatchResult LyXText::moveDown()
1435 if (cursorPar() == lastPar() && cursorRow() == lastRow())
1436 return DispatchResult(false, FINISHED_DOWN);
1439 return DispatchResult(true);
1443 bool LyXText::cursorLeft(bool internal)
1445 if (cursor().pos() > 0) {
1446 bool boundary = cursor().boundary();
1447 setCursor(cursor().par(), cursor().pos() - 1, true, false);
1448 if (!internal && !boundary &&
1449 bidi.isBoundary(*bv()->buffer(), *cursorPar(), cursor().pos() + 1))
1450 setCursor(cursor().par(), cursor().pos() + 1, true, true);
1454 if (cursor().par() != 0) {
1455 // steps into the paragraph above
1456 setCursor(cursor().par() - 1, boost::prior(cursorPar())->size());
1464 bool LyXText::cursorRight(bool internal)
1466 if (!internal && cursor().boundary()) {
1467 setCursor(cursor().par(), cursor().pos(), true, false);
1471 if (cursor().pos() != cursorPar()->size()) {
1472 setCursor(cursor().par(), cursor().pos() + 1, true, false);
1473 if (!internal && bidi.isBoundary(*bv()->buffer(), *cursorPar(),
1475 setCursor(cursor().par(), cursor().pos(), true, true);
1479 if (cursor().par() + 1 != int(paragraphs().size())) {
1480 setCursor(cursor().par() + 1, 0);
1488 void LyXText::cursorUp(bool selecting)
1490 Row const & row = *cursorRow();
1491 int x = bv()->x_target() - xo_;
1492 int y = cursorY() - row.baseline() - 1;
1493 setCursorFromCoordinates(x, y);
1496 int y_abs = y + yo_ - bv()->top_y();
1497 InsetOld * inset_hit = checkInsetHit(bv()->x_target(), y_abs);
1498 if (inset_hit && isHighlyEditableInset(inset_hit))
1499 inset_hit->edit(bv(), bv()->x_target(), y_abs);
1504 void LyXText::cursorDown(bool selecting)
1506 Row const & row = *cursorRow();
1507 int x = bv()->x_target() - xo_;
1508 int y = cursorY() - row.baseline() + row.height() + 1;
1509 setCursorFromCoordinates(x, y);
1512 int y_abs = y + yo_ - bv()->top_y();
1513 InsetOld * inset_hit = checkInsetHit(bv()->x_target(), y_abs);
1514 if (inset_hit && isHighlyEditableInset(inset_hit))
1515 inset_hit->edit(bv(), bv()->x_target(), y_abs);
1520 void LyXText::cursorUpParagraph()
1522 ParagraphList::iterator cpit = cursorPar();
1523 if (cursor().pos() > 0)
1525 else if (cpit != paragraphs().begin())
1526 setCursor(boost::prior(cpit), 0);
1530 void LyXText::cursorDownParagraph()
1532 ParagraphList::iterator pit = cursorPar();
1533 ParagraphList::iterator next_pit = boost::next(pit);
1535 if (next_pit != paragraphs().end())
1536 setCursor(next_pit, 0);
1538 setCursor(pit, pit->size());
1542 // fix the cursor `cur' after a characters has been deleted at `where'
1543 // position. Called by deleteEmptyParagraphMechanism
1544 void LyXText::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
1546 // if cursor is not in the paragraph where the delete occured,
1548 if (cur.par() != where.par())
1551 // if cursor position is after the place where the delete occured,
1553 if (cur.pos() > where.pos())
1554 cur.pos(cur.pos()-1);
1556 // check also if we don't want to set the cursor on a spot behind the
1557 // pagragraph because we erased the last character.
1558 if (cur.pos() > getPar(cur)->size())
1559 cur.pos(getPar(cur)->size());
1563 bool LyXText::deleteEmptyParagraphMechanism(CursorSlice const & old_cursor)
1565 // Would be wrong to delete anything if we have a selection.
1566 if (bv()->selection().set())
1569 // Don't do anything if the cursor is invalid
1570 if (old_cursor.par() == -1)
1573 // We allow all kinds of "mumbo-jumbo" when freespacing.
1574 ParagraphList::iterator const old_pit = getPar(old_cursor);
1575 if (old_pit->isFreeSpacing())
1578 /* Ok I'll put some comments here about what is missing.
1579 I have fixed BackSpace (and thus Delete) to not delete
1580 double-spaces automagically. I have also changed Cut,
1581 Copy and Paste to hopefully do some sensible things.
1582 There are still some small problems that can lead to
1583 double spaces stored in the document file or space at
1584 the beginning of paragraphs(). This happens if you have
1585 the cursor between to spaces and then save. Or if you
1586 cut and paste and the selection have a space at the
1587 beginning and then save right after the paste. I am
1588 sure none of these are very hard to fix, but I will
1589 put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
1590 that I can get some feedback. (Lgb)
1593 // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
1594 // delete the LineSeparator.
1597 // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
1598 // delete the LineSeparator.
1601 // If the pos around the old_cursor were spaces, delete one of them.
1602 if (old_cursor.par() != cursor().par()
1603 || old_cursor.pos() != cursor().pos()) {
1605 // Only if the cursor has really moved
1606 if (old_cursor.pos() > 0
1607 && old_cursor.pos() < old_pit->size()
1608 && old_pit->isLineSeparator(old_cursor.pos())
1609 && old_pit->isLineSeparator(old_cursor.pos() - 1)) {
1610 bool erased = old_pit->erase(old_cursor.pos() - 1);
1611 redoParagraph(old_pit);
1615 #ifdef WITH_WARNINGS
1616 #warning This will not work anymore when we have multiple views of the same buffer
1617 // In this case, we will have to correct also the cursors held by
1618 // other bufferviews. It will probably be easier to do that in a more
1619 // automated way in CursorSlice code. (JMarc 26/09/2001)
1621 // correct all cursors held by the LyXText
1622 fixCursorAfterDelete(cursor(), old_cursor);
1623 fixCursorAfterDelete(anchor(), old_cursor);
1628 // don't delete anything if this is the ONLY paragraph!
1629 if (paragraphs().size() == 1)
1632 // Do not delete empty paragraphs with keepempty set.
1633 if (old_pit->allowEmpty())
1636 // only do our magic if we changed paragraph
1637 if (old_cursor.par() == cursor().par())
1640 // record if we have deleted a paragraph
1641 // we can't possibly have deleted a paragraph before this point
1642 bool deleted = false;
1644 if (old_pit->empty()
1645 || (old_pit->size() == 1 && old_pit->isLineSeparator(0))) {
1646 // ok, we will delete something
1647 CursorSlice tmpcursor;
1651 bool selection_position_was_oldcursor_position =
1652 anchor().par() == old_cursor.par()
1653 && anchor().pos() == old_cursor.pos();
1655 tmpcursor = cursor();
1656 cursor() = old_cursor; // that undo can restore the right cursor position
1658 ParagraphList::iterator endpit = boost::next(old_pit);
1659 while (endpit != paragraphs().end() && endpit->getDepth())
1662 recUndo(parOffset(old_pit), parOffset(endpit) - 1);
1663 cursor() = tmpcursor;
1666 ParagraphList::iterator tmppit = cursorPar();
1668 paragraphs().erase(old_pit);
1669 // update cursor par offset
1670 cursor().par(parOffset(tmppit));
1673 if (selection_position_was_oldcursor_position) {
1674 // correct selection
1675 bv()->resetAnchor();
1682 if (old_pit->stripLeadingSpaces()) {
1683 redoParagraph(old_pit);
1684 bv()->resetAnchor();
1690 ParagraphList & LyXText::paragraphs() const
1692 return const_cast<ParagraphList &>(paragraphs_);
1696 void LyXText::recUndo(paroffset_type first, paroffset_type last) const
1698 recordUndo(Undo::ATOMIC, this, first, last);
1702 void LyXText::recUndo(lyx::paroffset_type par) const
1704 recordUndo(Undo::ATOMIC, this, par, par);
1708 bool LyXText::isInInset() const
1714 int defaultRowHeight()
1716 return int(font_metrics::maxHeight(LyXFont(LyXFont::ALL_SANE)) * 1.2);