]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
change LyXScreen names to begin with lower case
[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
27 extern BufferView * current_view;
28
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 //       Paragraph * buf;
34 //       LyXTextClassList::size_type textclass;
35 // };
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)
39 //
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)
47
48 namespace {
49
50 Paragraph * buf = 0;
51 LyXTextClassList::size_type textclass = 0;
52
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)
57 void DeleteBuffer()
58 {
59         if (!buf)
60                 return;
61         
62         Paragraph * tmppar;
63         
64         while (buf) {
65                 tmppar =  buf;
66                 buf = buf->next();
67                 delete tmppar;
68         }
69         buf = 0;
70 }
71
72 } // namespace anon
73
74
75 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
76                                int start, int & end, char tc, bool doclear)
77 {
78         if (!startpar || (start > startpar->size()))
79                 return false;
80         
81         DeleteBuffer();
82         
83         textclass = tc;
84         
85         if (!(*endpar) ||
86             startpar == (*endpar)) {
87                 // only within one paragraph
88                 buf = new Paragraph;
89                 Paragraph::size_type i = start;
90                 if (end > startpar->size())
91                         end = startpar->size();
92                 for (; i < end; ++i) {
93                         startpar->copyIntoMinibuffer(*current_view->buffer(),
94                                                      start);
95                         startpar->erase(start);
96                         
97                         buf->insertFromMinibuffer(buf->size());
98                 }
99                 end = start - 1;
100         } else {
101                 // more than one paragraph
102                 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
103                                                       end);
104                 *endpar = (*endpar)->next();
105                 end = 0;
106                 
107                 startpar->breakParagraphConservative(current_view->buffer()->params,
108                                                      start);
109                 
110                 // store the selection
111                 buf = startpar->next();
112                 
113                 buf->previous(0);
114                 (*endpar)->previous()->next(0);
115                 
116                 // cut the selection
117                 startpar->next(*endpar);
118                 
119                 (*endpar)->previous(startpar);
120                 
121                 // the cut selection should begin with standard layout
122                 buf->clear(); 
123                 
124                 // paste the paragraphs again, if possible
125                 if (doclear)
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!
131                 }
132         }
133         return true;
134 }
135
136
137 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
138                                 int start, int end, char tc)
139 {
140         if (!startpar || (start > startpar->size()))
141                 return false;
142         
143         DeleteBuffer();
144         
145         textclass = tc;
146         
147         if (!endpar ||
148             startpar == endpar) {
149                 // only within one paragraph
150                 buf = new Paragraph;
151                 Paragraph::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());
157                 }
158         } else {
159                 // copy more than one paragraph
160                 // clone the paragraphs within the selection
161                 Paragraph * tmppar = startpar;
162                 buf = new Paragraph(*tmppar);
163                 Paragraph * tmppar2 = buf;
164                 
165                 while (tmppar != endpar
166                        && tmppar->next()) {
167                         tmppar = tmppar->next();
168                         tmppar2->next(new Paragraph(*tmppar));
169                         tmppar2->next()->previous(tmppar2);
170                         tmppar2 = tmppar2->next();
171                 }
172                 tmppar2->next(0);
173                 
174                 // the buf paragraph is too big
175                 Paragraph::size_type tmpi2 = start;
176                 for (; tmpi2; --tmpi2)
177                         buf->erase(0);
178                 
179                 // now tmppar 2 is too big, delete all after end
180                 tmpi2 = end;
181                 while (tmppar2->size() > tmpi2) {
182                         tmppar2->erase(tmppar2->size() - 1);
183                 }
184         }
185         return true;
186 }
187
188
189 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
190                                  int & pos, char tc)
191 {
192         if (!checkPastePossible(*par))
193                 return false;
194         
195         if (pos > (*par)->size())
196                 pos = (*par)->size();
197         
198         // Paragraph * tmpbuf;
199         Paragraph * tmppar = *par;
200         int tmppos = pos;
201         
202         // There are two cases: cutbuffer only one paragraph or many
203         if (!buf->next()) {
204                 // only within a paragraph
205                 Paragraph * tmpbuf = new Paragraph(*buf);
206                 
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
212                 // be removed.
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)) {
218                                 buf->erase(0);
219                         } else {
220                                 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
221                                 buf->erase(0);
222                                 if (tmppar->insertFromMinibuffer(tmppos))
223                                         ++tmppos;
224                         }
225                 }
226                 delete buf;
227                 buf = tmpbuf;
228                 *endpar = tmppar->next();
229                 pos = tmppos;
230         } else {
231                 // many paragraphs
232                 
233                 // make a copy of the simple cut_buffer
234                 Paragraph * tmpbuf = buf;
235                 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf);
236                 Paragraph * tmpbuf2 = simple_cut_clone;
237                 
238                 while (tmpbuf->next()) {
239                         tmpbuf = tmpbuf->next();
240                         tmpbuf2->next(new Paragraph(*tmpbuf));
241                         tmpbuf2->next()->previous(tmpbuf2);
242                         tmpbuf2 = tmpbuf2->next();
243                 }
244                 
245                 // make sure there is no class difference
246                 SwitchLayoutsBetweenClasses(textclass, tc, buf);
247                 
248                 // make the buf exactly the same layout than
249                 // the cursor paragraph
250                 buf->makeSameLayout(*par);
251                 
252                 // find the end of the buffer
253                 Paragraph * lastbuffer = buf;
254                 while (lastbuffer->next())
255                         lastbuffer = lastbuffer->next();
256                 
257                 bool paste_the_end = false;
258                 
259                 // open the paragraph for inserting the buf
260                 // if necessary
261                 if (((*par)->size() > pos) || !(*par)->next()) {
262                         (*par)->breakParagraphConservative(current_view->buffer()->params,
263                                                            pos);
264                         paste_the_end = true;
265                 }
266                 // set the end for redoing later
267                 *endpar = (*par)->next()->next();
268                 
269                 // paste it!
270                 lastbuffer->next((*par)->next());
271                 (*par)->next()->previous(lastbuffer);
272                 
273                 (*par)->next(buf);
274                 buf->previous(*par);
275                 
276                 if ((*par)->next() == lastbuffer)
277                         lastbuffer = *par;
278                 
279                 (*par)->pasteParagraph(current_view->buffer()->params);
280                 // store the new cursor position
281                 *par = lastbuffer;
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);
293                         } else
294                                 lastbuffer->next()->stripLeadingSpaces(tc);
295                 }
296                 // restore the simple cut buffer
297                 buf = simple_cut_clone;
298         }
299         
300         return true;
301 }
302
303
304 int CutAndPaste::nrOfParagraphs()
305 {
306         if (!buf)
307                 return 0;
308         
309         int n = 1;
310         Paragraph * tmppar = buf;
311         while(tmppar->next()) {
312                 ++n;
313                 tmppar = tmppar->next();
314         }
315         return n;
316 }
317
318
319 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
320                                              LyXTextClassList::size_type c2,
321                                              Paragraph * par)
322 {
323         int ret = 0;
324         if (!par || c1 == c2)
325                 return ret;
326         
327         while (par) {
328                 string const name = textclasslist.NameOfLayout(c1,
329                                                                par->layout);
330                 int lay = 0;
331                 pair<bool, LyXTextClass::LayoutList::size_type> pp =
332                         textclasslist.NumberOfLayout(c2, name);
333                 if (pp.first) {
334                         lay = pp.second;
335                 } else { // layout not found
336                         // use default layout "Standard" (0)
337                         lay = 0;
338                 }
339                 par->layout = lay;
340                 
341                 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
342                         ++ret;
343                         string const s = _("Layout had to be changed from\n")
344                                 + name + _(" to ")
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);
351                 }
352                 par = par->next();
353         }
354         return ret;
355 }
356
357
358 bool CutAndPaste::checkPastePossible(Paragraph *)
359 {
360         if (!buf) return false;
361         
362         return true;
363 }