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