]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
hfilltextclean.diff
[lyx.git] / src / CutAndPaste.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #include "CutAndPaste.h"
13 #include "BufferView.h"
14 #include "buffer.h"
15 #include "paragraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
19 #include "gettext.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 using std::pair;
26 using lyx::pos_type;
27 using lyx::layout_type;
28 using lyx::textclass_type;
29
30 extern BufferView * current_view;
31
32 // Jürgen, note that this means that you cannot currently have a list
33 // of selections cut/copied. So IMHO later we should have a
34 // list/vector/deque that we could store
35 // struct selection_item {
36 //       Paragraph * buf;
37 //       LyXTextClassList::size_type textclass;
38 // };
39 // in and some method of choosing beween them (based on the first few chars
40 // in the selection probably.) This would be a nice feature and quite
41 // easy to implement. (Lgb)
42 //
43 // Sure but I just cleaned up this code for now with the same functionality
44 // as before. I also want to add a XClipboard function so that we can copy
45 // text from LyX to some other X-application in the form of ASCII or in the
46 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
47 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
48 // then do a middle mouse button click in the application you want and have
49 // the whole formula there in LaTeX-Code. (Jug)
50
51 namespace {
52
53 Paragraph * buf = 0;
54 textclass_type textclass = 0;
55
56 // for now here this should be in another Cut&Paste Class!
57 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
58 // member of the CutAndPaste class and in addition it was private.
59 // Perhaps it even should take a parameter? (Lgb)
60 void DeleteBuffer()
61 {
62         if (!buf)
63                 return;
64         
65         Paragraph * tmppar;
66         
67         while (buf) {
68                 tmppar =  buf;
69                 buf = buf->next();
70                 delete tmppar;
71         }
72         buf = 0;
73 }
74
75 } // namespace anon
76
77
78 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
79                                int start, int & end, char tc, bool doclear,
80                                                            bool realcut)
81 {
82         if (!startpar || (start > startpar->size()))
83                 return false;
84         
85         if (realcut)
86                 DeleteBuffer();
87         
88         textclass = tc;
89         
90         if (!(*endpar) || startpar == (*endpar)) {
91                 // only within one paragraph
92                 if (realcut)
93                         buf = new Paragraph;
94                 pos_type i = start;
95                 if (end > startpar->size())
96                         end = startpar->size();
97                 for (; i < end; ++i) {
98                         if (realcut)
99                                 startpar->copyIntoMinibuffer(*current_view->buffer(),
100                                                              start);
101                         startpar->erase(start);
102                         if (realcut)
103                                 buf->insertFromMinibuffer(buf->size());
104                 }
105                 end = start - 1;
106         } else {
107                 // more than one paragraph
108                 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
109                                                       end);
110                 *endpar = (*endpar)->next();
111                 end = 0;
112                 
113                 startpar->breakParagraphConservative(current_view->buffer()->params,
114                                                      start);
115                 
116                 // store the selection
117                 if (realcut) {
118                         buf = startpar->next();
119                         buf->previous(0);
120                 } else {
121                         startpar->next()->previous(0);
122                 }
123                 (*endpar)->previous()->next(0);
124                 
125                 // cut the selection
126                 startpar->next(*endpar);
127                 
128                 (*endpar)->previous(startpar);
129                 
130                 // the cut selection should begin with standard layout
131                 if (realcut)
132                         buf->clear(); 
133                 
134                 // paste the paragraphs again, if possible
135                 if (doclear)
136                         startpar->next()->stripLeadingSpaces(textclass);
137                 if (startpar->hasSameLayout(startpar->next()) ||
138                     !startpar->next()->size()) {
139                         startpar->pasteParagraph(current_view->buffer()->params);
140                         (*endpar) = startpar; // this because endpar gets deleted here!
141                 }
142                 // this paragraph's are of noone's owner!
143                 Paragraph * p = buf;
144                 while(p) {
145                         p->setInsetOwner(0);
146                         p = p->next();
147                 }
148         }
149         return true;
150 }
151
152
153 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
154                                 int start, int end, char tc)
155 {
156         if (!startpar || (start > startpar->size()))
157                 return false;
158         
159         DeleteBuffer();
160         
161         textclass = tc;
162         
163         if (!endpar || startpar == endpar) {
164                 // only within one paragraph
165                 buf = new Paragraph;
166                 pos_type i = start;
167                 if (end > startpar->size())
168                         end = startpar->size();
169                 for (; i < end; ++i) {
170                         startpar->copyIntoMinibuffer(*current_view->buffer(), i);
171                         buf->insertFromMinibuffer(buf->size());
172                 }
173         } else {
174                 // copy more than one paragraph
175                 // clone the paragraphs within the selection
176                 Paragraph * tmppar = startpar;
177                 buf = new Paragraph(*tmppar, false);
178                 Paragraph * tmppar2 = buf;
179                 
180                 while (tmppar != endpar
181                        && tmppar->next()) {
182                         tmppar = tmppar->next();
183                         tmppar2->next(new Paragraph(*tmppar, false));
184                         tmppar2->next()->previous(tmppar2);
185                         tmppar2 = tmppar2->next();
186                 }
187                 tmppar2->next(0);
188                 
189                 // the buf paragraph is too big
190                 pos_type tmpi2 = start;
191                 for (; tmpi2; --tmpi2)
192                         buf->erase(0);
193                 
194                 // now tmppar 2 is too big, delete all after end
195                 tmpi2 = end;
196                 while (tmppar2->size() > tmpi2) {
197                         tmppar2->erase(tmppar2->size() - 1);
198                 }
199                 // this paragraph's are of noone's owner!
200                 tmppar = buf;
201                 while(tmppar) {
202                         tmppar->setInsetOwner(0);
203                         tmppar = tmppar->next();
204                 }
205         }
206         return true;
207 }
208
209
210 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
211                                  int & pos, char tc)
212 {
213         if (!checkPastePossible(*par))
214                 return false;
215         
216         if (pos > (*par)->size())
217                 pos = (*par)->size();
218         
219         // Paragraph * tmpbuf;
220         Paragraph * tmppar = *par;
221         int tmppos = pos;
222         
223         // There are two cases: cutbuffer only one paragraph or many
224         if (!buf->next()) {
225                 // only within a paragraph
226                 Paragraph * tmpbuf = new Paragraph(*buf, false);
227                 
228                 // Some provisions should be done here for checking
229                 // if we are inserting at the beginning of a
230                 // paragraph. If there are a space at the beginning
231                 // of the text to insert and we are inserting at
232                 // the beginning of the paragraph the space should
233                 // be removed.
234                 while (buf->size()) {
235                         // This is an attempt to fix the
236                         // "never insert a space at the
237                         // beginning of a paragraph" problem.
238                         if (!tmppos && buf->isLineSeparator(0)) {
239                                 buf->erase(0);
240                         } else {
241                                 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
242                                 buf->erase(0);
243                                 if (tmppar->insertFromMinibuffer(tmppos))
244                                         ++tmppos;
245                         }
246                 }
247                 delete buf;
248                 buf = tmpbuf;
249                 *endpar = tmppar->next();
250                 pos = tmppos;
251         } else {
252                 // many paragraphs
253                 
254                 // make a copy of the simple cut_buffer
255                 Paragraph * tmpbuf = buf;
256                 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
257                 Paragraph * tmpbuf2 = simple_cut_clone;
258                 
259                 while (tmpbuf->next()) {
260                         tmpbuf = tmpbuf->next();
261                         tmpbuf2->next(new Paragraph(*tmpbuf, false));
262                         tmpbuf2->next()->previous(tmpbuf2);
263                         tmpbuf2 = tmpbuf2->next();
264                 }
265                 
266                 // make sure there is no class difference
267                 SwitchLayoutsBetweenClasses(textclass, tc, buf);
268                 
269                 // make the buf exactly the same layout than
270                 // the cursor paragraph
271                 buf->makeSameLayout(*par);
272                 
273                 // find the end of the buffer
274                 Paragraph * lastbuffer = buf;
275                 while (lastbuffer->next())
276                         lastbuffer = lastbuffer->next();
277                 
278                 bool paste_the_end = false;
279                 
280                 // open the paragraph for inserting the buf
281                 // if necessary
282                 if (((*par)->size() > pos) || !(*par)->next()) {
283                         (*par)->breakParagraphConservative(current_view->buffer()->params,
284                                                            pos);
285                         paste_the_end = true;
286                 }
287                 // set the end for redoing later
288                 *endpar = (*par)->next()->next();
289                 
290                 // paste it!
291                 lastbuffer->next((*par)->next());
292                 (*par)->next()->previous(lastbuffer);
293                 
294                 (*par)->next(buf);
295                 buf->previous(*par);
296                 
297                 if ((*par)->next() == lastbuffer)
298                         lastbuffer = *par;
299                 
300                 (*par)->pasteParagraph(current_view->buffer()->params);
301                 // store the new cursor position
302                 *par = lastbuffer;
303                 pos = lastbuffer->size();
304                 // maybe some pasting
305                 if (lastbuffer->next() && paste_the_end) {
306                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
307                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
308                         } else if (!lastbuffer->next()->size()) {
309                                 lastbuffer->next()->makeSameLayout(lastbuffer);
310                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
311                         } else if (!lastbuffer->size()) {
312                                 lastbuffer->makeSameLayout(lastbuffer->next());
313                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
314                         } else
315                                 lastbuffer->next()->stripLeadingSpaces(tc);
316                 }
317                 // restore the simple cut buffer
318                 buf = simple_cut_clone;
319         }
320         
321         return true;
322 }
323
324
325 int CutAndPaste::nrOfParagraphs()
326 {
327         if (!buf)
328                 return 0;
329         
330         int n = 1;
331         Paragraph * tmppar = buf;
332         while(tmppar->next()) {
333                 ++n;
334                 tmppar = tmppar->next();
335         }
336         return n;
337 }
338
339
340 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
341                                              textclass_type c2, Paragraph * par)
342 {
343         int ret = 0;
344         if (!par || c1 == c2)
345                 return ret;
346         
347         while (par) {
348                 string const name = textclasslist.NameOfLayout(c1, par->layout);
349                 int lay = 0;
350                 pair<bool, layout_type> pp =
351                         textclasslist.NumberOfLayout(c2, name);
352                 if (pp.first) {
353                         lay = pp.second;
354                 } else { // layout not found
355                         // use default layout "Standard" (0)
356                         lay = 0;
357                 }
358                 par->layout = lay;
359                 
360                 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
361                         ++ret;
362                         string const s = _("Layout had to be changed from\n")
363                                 + name + _(" to ")
364                                 + textclasslist.NameOfLayout(c2, par->layout)
365                                 + _("\nbecause of class conversion from\n")
366                                 + textclasslist.NameOfClass(c1) + _(" to ")
367                                 + textclasslist.NameOfClass(c2);
368                         InsetError * new_inset = new InsetError(s);
369                         par->insertInset(0, new_inset);
370                 }
371                 par = par->next();
372         }
373         return ret;
374 }
375
376
377 bool CutAndPaste::checkPastePossible(Paragraph *)
378 {
379         if (!buf) return false;
380         
381         return true;
382 }