1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
12 #include "CutAndPaste.h"
13 #include "BufferView.h"
15 #include "lyxparagraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
22 #pragma implementation
27 extern BufferView * current_view;
29 // Jürgen, note that this means that you cannot currently have a list
30 // of selections cut/copied. So IMHO later we should have a
31 // list/vector/deque that we could store
32 // struct selection_item {
33 // LyXParagraph * buf;
34 // LyXTextClassList::size_type textclass;
36 // in and some method of choosing beween them (based on the first few chars
37 // in the selection probably.) This would be a nice feature and quite
38 // easy to implement. (Lgb)
40 // Sure but I just cleaned up this code for now with the same functionality
41 // as before. I also want to add a XClipboard function so that we can copy
42 // text from LyX to some other X-application in the form of ASCII or in the
43 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
44 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
45 // then do a middle mouse button click in the application you want and have
46 // the whole formula there in LaTeX-Code. (Jug)
50 LyXParagraph * buf = 0;
51 LyXTextClassList::size_type textclass = 0;
53 // for now here this should be in another Cut&Paste Class!
54 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
55 // member of the CutAndPaste class and in addition it was private.
56 // Perhaps it even should take a parameter? (Lgb)
62 LyXParagraph * tmppar;
75 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
76 int start, int & end, char tc, bool doclear)
78 if (!startpar || (start > startpar->size()))
86 startpar == (*endpar)) {
87 // only within one paragraph
88 buf = new LyXParagraph;
89 LyXParagraph::size_type i = start;
90 if (end > startpar->size())
91 end = startpar->size();
92 for (; i < end; ++i) {
93 startpar->CopyIntoMinibuffer(*current_view->buffer(),
95 startpar->Erase(start);
97 buf->InsertFromMinibuffer(buf->size());
101 // more than one paragraph
102 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
104 *endpar = (*endpar)->next();
107 startpar->BreakParagraphConservative(current_view->buffer()->params,
110 // store the selection
111 buf = startpar->next();
114 (*endpar)->previous()->next(0);
117 startpar->next(*endpar);
119 (*endpar)->previous(startpar);
121 // the cut selection should begin with standard layout
124 // paste the paragraphs again, if possible
126 startpar->next()->StripLeadingSpaces(textclass);
127 if (startpar->HasSameLayout(startpar->next()) ||
128 !startpar->next()->size()) {
129 startpar->PasteParagraph(current_view->buffer()->params);
130 (*endpar) = startpar; // this because endpar gets deleted here!
137 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
138 int start, int end, char tc)
140 if (!startpar || (start > startpar->size()))
148 startpar == endpar) {
149 // only within one paragraph
150 buf = new LyXParagraph;
151 LyXParagraph::size_type i = start;
152 if (end > startpar->size())
153 end = startpar->size();
154 for (; i < end; ++i) {
155 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
156 buf->InsertFromMinibuffer(buf->size());
159 // copy more than one paragraph
160 // clone the paragraphs within the selection
161 LyXParagraph * tmppar = startpar;
162 buf = new LyXParagraph(*tmppar);
163 LyXParagraph * tmppar2 = buf;
165 while (tmppar != endpar
167 tmppar = tmppar->next();
168 tmppar2->next(new LyXParagraph(*tmppar));
169 tmppar2->next()->previous(tmppar2);
170 tmppar2 = tmppar2->next();
174 // the buf paragraph is too big
175 LyXParagraph::size_type tmpi2 = start;
176 for (; tmpi2; --tmpi2)
179 // now tmppar 2 is too big, delete all after end
181 while (tmppar2->size() > tmpi2) {
182 tmppar2->Erase(tmppar2->size() - 1);
189 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
192 if (!checkPastePossible(*par))
195 if (pos > (*par)->size())
196 pos = (*par)->size();
198 // LyXParagraph * tmpbuf;
199 LyXParagraph * tmppar = *par;
202 // There are two cases: cutbuffer only one paragraph or many
204 // only within a paragraph
205 LyXParagraph * tmpbuf = new LyXParagraph(*buf);
207 // Some provisions should be done here for checking
208 // if we are inserting at the beginning of a
209 // paragraph. If there are a space at the beginning
210 // of the text to insert and we are inserting at
211 // the beginning of the paragraph the space should
213 while (buf->size()) {
214 // This is an attempt to fix the
215 // "never insert a space at the
216 // beginning of a paragraph" problem.
217 if (!tmppos && buf->IsLineSeparator(0)) {
220 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
222 if (tmppar->InsertFromMinibuffer(tmppos))
228 *endpar = tmppar->next();
233 // make a copy of the simple cut_buffer
234 LyXParagraph * tmpbuf = buf;
235 LyXParagraph * simple_cut_clone = new LyXParagraph(*tmpbuf);
236 LyXParagraph * tmpbuf2 = simple_cut_clone;
238 while (tmpbuf->next()) {
239 tmpbuf = tmpbuf->next();
240 tmpbuf2->next(new LyXParagraph(*tmpbuf));
241 tmpbuf2->next()->previous(tmpbuf2);
242 tmpbuf2 = tmpbuf2->next();
245 // make sure there is no class difference
246 SwitchLayoutsBetweenClasses(textclass, tc, buf);
248 // make the buf exactly the same layout than
249 // the cursor paragraph
250 buf->MakeSameLayout(*par);
252 // find the end of the buffer
253 LyXParagraph * lastbuffer = buf;
254 while (lastbuffer->next())
255 lastbuffer = lastbuffer->next();
257 bool paste_the_end = false;
259 // open the paragraph for inserting the buf
261 if (((*par)->size() > pos) || !(*par)->next()) {
262 (*par)->BreakParagraphConservative(current_view->buffer()->params,
264 paste_the_end = true;
266 // set the end for redoing later
267 *endpar = (*par)->next()->next();
270 lastbuffer->next((*par)->next());
271 (*par)->next()->previous(lastbuffer);
276 if ((*par)->next() == lastbuffer)
279 (*par)->PasteParagraph(current_view->buffer()->params);
280 // store the new cursor position
282 pos = lastbuffer->size();
283 // maybe some pasting
284 if (lastbuffer->next() && paste_the_end) {
285 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
286 lastbuffer->PasteParagraph(current_view->buffer()->params);
287 } else if (!lastbuffer->next()->size()) {
288 lastbuffer->next()->MakeSameLayout(lastbuffer);
289 lastbuffer->PasteParagraph(current_view->buffer()->params);
290 } else if (!lastbuffer->size()) {
291 lastbuffer->MakeSameLayout(lastbuffer->next());
292 lastbuffer->PasteParagraph(current_view->buffer()->params);
294 lastbuffer->next()->StripLeadingSpaces(tc);
296 // restore the simple cut buffer
297 buf = simple_cut_clone;
304 int CutAndPaste::nrOfParagraphs()
310 LyXParagraph * tmppar = buf;
311 while(tmppar->next()) {
313 tmppar = tmppar->next();
319 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
320 LyXTextClassList::size_type c2,
324 if (!par || c1 == c2)
328 string const name = textclasslist.NameOfLayout(c1,
331 pair<bool, LyXTextClass::LayoutList::size_type> pp =
332 textclasslist.NumberOfLayout(c2, name);
335 } else { // layout not found
336 // use default layout "Standard" (0)
341 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
343 string const s = _("Layout had to be changed from\n")
345 + textclasslist.NameOfLayout(c2, par->layout)
346 + _("\nbecause of class conversion from\n")
347 + textclasslist.NameOfClass(c1) + _(" to ")
348 + textclasslist.NameOfClass(c2);
349 InsetError * new_inset = new InsetError(s);
350 par->InsertInset(0, new_inset);
358 bool CutAndPaste::checkPastePossible(LyXParagraph *)
360 if (!buf) return false;