1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2000 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 = tmppar->Clone();
163 LyXParagraph * tmppar2 = buf;
165 while (tmppar != endpar
167 tmppar = tmppar->next();
168 tmppar2->next(tmppar->Clone());
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 tmpbuf = buf->Clone();
206 // Some provisions should be done here for checking
207 // if we are inserting at the beginning of a
208 // paragraph. If there are a space at the beginning
209 // of the text to insert and we are inserting at
210 // the beginning of the paragraph the space should
212 while (buf->size()) {
213 // This is an attempt to fix the
214 // "never insert a space at the
215 // beginning of a paragraph" problem.
216 if (!tmppos && buf->IsLineSeparator(0)) {
219 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
221 if (tmppar->InsertFromMinibuffer(tmppos))
227 *endpar = tmppar->next();
232 // make a copy of the simple cut_buffer
234 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
235 LyXParagraph * tmpbuf2 = simple_cut_clone;
236 while (tmpbuf->next()) {
237 tmpbuf = tmpbuf->next();
238 tmpbuf2->next(tmpbuf->Clone());
239 tmpbuf2->next()->previous(tmpbuf2);
240 tmpbuf2 = tmpbuf2->next();
243 // make sure there is no class difference
244 SwitchLayoutsBetweenClasses(textclass, tc, buf);
246 // make the buf exactly the same layout than
247 // the cursor paragraph
248 buf->MakeSameLayout(*par);
250 // find the end of the buffer
251 LyXParagraph * lastbuffer = buf;
252 while (lastbuffer->next())
253 lastbuffer = lastbuffer->next();
255 bool paste_the_end = false;
257 // open the paragraph for inserting the buf
259 if (((*par)->size() > pos) || !(*par)->next()) {
260 (*par)->BreakParagraphConservative(current_view->buffer()->params,
262 paste_the_end = true;
264 // set the end for redoing later
265 *endpar = (*par)->next()->next();
268 lastbuffer->next((*par)->next());
269 (*par)->next()->previous(lastbuffer);
274 if ((*par)->next() == lastbuffer)
277 (*par)->PasteParagraph(current_view->buffer()->params);
278 // store the new cursor position
280 pos = lastbuffer->size();
281 // maybe some pasting
282 if (lastbuffer->next() && paste_the_end) {
283 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
284 lastbuffer->PasteParagraph(current_view->buffer()->params);
285 } else if (!lastbuffer->next()->size()) {
286 lastbuffer->next()->MakeSameLayout(lastbuffer);
287 lastbuffer->PasteParagraph(current_view->buffer()->params);
288 } else if (!lastbuffer->size()) {
289 lastbuffer->MakeSameLayout(lastbuffer->next());
290 lastbuffer->PasteParagraph(current_view->buffer()->params);
292 lastbuffer->next()->StripLeadingSpaces(tc);
294 // restore the simple cut buffer
295 buf = simple_cut_clone;
302 int CutAndPaste::nrOfParagraphs()
308 LyXParagraph * tmppar = buf;
309 while(tmppar->next()) {
311 tmppar = tmppar->next();
317 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
318 LyXTextClassList::size_type c2,
322 if (!par || c1 == c2)
326 string name = textclasslist.NameOfLayout(c1, par->layout);
328 pair<bool, LyXTextClass::LayoutList::size_type> pp =
329 textclasslist.NumberOfLayout(c2, name);
332 } else { // layout not found
333 // use default layout "Standard" (0)
338 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
340 string s = _("Layout had to be changed from\n")
342 + textclasslist.NameOfLayout(c2, par->layout)
343 + _("\nbecause of class conversion from\n")
344 + textclasslist.NameOfClass(c1) + _(" to ")
345 + textclasslist.NameOfClass(c2);
346 InsetError * new_inset = new InsetError(s);
347 par->InsertInset(0, new_inset);
355 bool CutAndPaste::checkPastePossible(LyXParagraph *)
357 if (!buf) return false;