]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
Fixed cut&paste bugs and added freespacing for ERT Insets.
[lyx.git] / src / BufferView2.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <fstream>
12 #include <algorithm>
13
14 #include <config.h>
15
16 #include "BufferView.h"
17 #include "buffer.h"
18 #include "lyxcursor.h"
19 #include "lyxtext.h"
20 #include "insets/inseterror.h"
21 #include "LyXView.h"
22 #include "bufferlist.h"
23 #include "support/FileInfo.h"
24 #include "lyxscreen.h"
25 #include "support/filetools.h"
26 #include "frontends/Alert.h"
27 #include "LaTeX.h"
28 #include "BufferView_pimpl.h"
29 #include "insets/insetcommand.h" //ChangeRefs
30 #include "support/lyxfunctional.h" //equal_1st_in_pair
31 #include "language.h"
32 #include "gettext.h"
33 #include "undo_funcs.h"
34 #include "debug.h"
35 #include "iterators.h"
36
37 extern BufferList bufferlist;
38
39 using std::pair;
40 using std::endl;
41 using std::ifstream;
42 using std::vector;
43 using std::find;
44 using std::count;
45 using std::count_if;
46
47
48 // Inserts a file into current document
49 bool BufferView::insertLyXFile(string const & filen)
50         //
51         // Copyright CHT Software Service GmbH
52         // Uwe C. Schroeder
53         //
54         // Insert a Lyxformat - file into current buffer
55         //
56         // Moved from lyx_cb.C (Lgb)
57 {
58         if (filen.empty()) return false;
59
60         string const fname = MakeAbsPath(filen);
61
62         // check if file exist
63         FileInfo const fi(fname);
64
65         if (!fi.readable()) {
66                 Alert::alert(_("Error!"),
67                            _("Specified file is unreadable: "),
68                            MakeDisplayPath(fname, 50));
69                 return false;
70         }
71         
72         beforeChange(text);
73
74         ifstream ifs(fname.c_str());
75         if (!ifs) {
76                 Alert::alert(_("Error!"),
77                            _("Cannot open specified file: "),
78                            MakeDisplayPath(fname, 50));
79                 return false;
80         }
81         
82         int const c = ifs.peek();
83        
84         LyXLex lex(0, 0);
85         lex.setStream(ifs);
86
87         bool res = true;
88
89         if (c == '#') {
90                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
91                 res = buffer()->readFile(lex, text->cursor.par());
92         } else {
93                 lyxerr[Debug::INFO] << "Will insert file without header" 
94                                     << endl;
95                 res = buffer()->readLyXformat2(lex, text->cursor.par());
96         }
97
98         resize();
99         return res;
100 }
101
102
103 bool BufferView::removeAutoInsets()
104 {
105         Paragraph * par = buffer()->paragraph;
106
107         LyXCursor tmpcursor = text->cursor;
108         LyXCursor cursor;
109
110         bool a = false;
111
112         while (par) {
113                 // this has to be done before the delete
114                 text->setCursor(this, cursor, par, 0);
115                 if (par->autoDeleteInsets()){
116                         a = true;
117                         text->redoParagraphs(this, cursor,
118                                              cursor.par()->next());
119                         text->fullRebreak(this);
120                 }
121                 par = par->next();
122         }
123
124         // avoid forbidden cursor positions caused by error removing
125         if (tmpcursor.pos() > tmpcursor.par()->size())
126                 tmpcursor.pos(tmpcursor.par()->size());
127
128         text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
129
130         return a;
131 }
132
133
134 void BufferView::insertErrors(TeXErrors & terr)
135 {
136         // Save the cursor position
137         LyXCursor cursor = text->cursor;
138
139         for (TeXErrors::Errors::const_iterator cit = terr.begin();
140              cit != terr.end();
141              ++cit) {
142                 string const desctext(cit->error_desc);
143                 string const errortext(cit->error_text);
144                 string const msgtxt = desctext + '\n' + errortext;
145                 int const errorrow = cit->error_in_line;
146
147                 // Insert error string for row number
148                 int tmpid = -1; 
149                 int tmppos = -1;
150
151                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
152                         buffer()->texrow.increasePos(tmpid, tmppos);
153                 }
154                 
155                 Paragraph * texrowpar = 0;
156
157                 if (tmpid == -1) {
158                         texrowpar = text->firstParagraph();
159                         tmppos = 0;
160                 } else {
161                         texrowpar = buffer()->getParFromID(tmpid);
162                 }
163
164                 if (texrowpar == 0)
165                         continue;
166
167                 InsetError * new_inset = new InsetError(msgtxt);
168                 text->setCursorIntern(this, texrowpar, tmppos);
169                 text->insertInset(this, new_inset);
170                 text->fullRebreak(this);
171         }
172         // Restore the cursor position
173         text->setCursorIntern(this, cursor.par(), cursor.pos());
174 }
175
176
177 void BufferView::setCursorFromRow(int row)
178 {
179         int tmpid = -1; 
180         int tmppos = -1;
181
182         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
183
184         Paragraph * texrowpar;
185
186         if (tmpid == -1) {
187                 texrowpar = text->firstParagraph();
188                 tmppos = 0;
189         } else {
190                 texrowpar = buffer()->getParFromID(tmpid);
191         }
192         text->setCursor(this, texrowpar, tmppos);
193 }
194
195
196 bool BufferView::insertInset(Inset * inset, string const & lout)
197 {
198         return pimpl_->insertInset(inset, lout);
199 }
200
201
202 /* This is also a buffer property (ale) */
203 // Not so sure about that. a goto Label function can not be buffer local, just
204 // think how this will work in a multiwindo/buffer environment, all the
205 // cursors in all the views showing this buffer will move. (Lgb)
206 // OK, then no cursor action should be allowed in buffer. (ale)
207 bool BufferView::gotoLabel(string const & label)
208
209 {
210         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
211              it != buffer()->inset_iterator_end(); ++it) {
212                 vector<string> labels = (*it)->getLabelList();
213                 if (find(labels.begin(),labels.end(),label)
214                      != labels.end()) {
215                         beforeChange(text);
216                         text->setCursor(this, it.getPar(), it.getPos());
217                         text->selection.cursor = text->cursor;
218                         update(text, BufferView::SELECT|BufferView::FITCUR);
219                         return true;
220                 }
221         }
222         return false;
223 }
224
225
226 void BufferView::menuUndo()
227 {
228         if (available()) {
229                 owner()->message(_("Undo"));
230                 hideCursor();
231                 beforeChange(text);
232                 update(text, BufferView::SELECT|BufferView::FITCUR);
233                 if (!textUndo(this))
234                         owner()->message(_("No further undo information"));
235                 else
236                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
237                 setState();
238         }
239 }
240
241
242 void BufferView::menuRedo()
243 {
244         if (theLockingInset()) {
245                 owner()->message(_("Redo not yet supported in math mode"));
246                 return;
247         }    
248    
249         if (available()) {
250                 owner()->message(_("Redo"));
251                 hideCursor();
252                 beforeChange(text);
253                 update(text, BufferView::SELECT|BufferView::FITCUR);
254                 if (!textRedo(this))
255                         owner()->message(_("No further redo information"));
256                 else
257                         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
258                 setState();
259         }
260 }
261
262
263 void BufferView::copyEnvironment()
264 {
265         if (available()) {
266                 text->copyEnvironmentType();
267                 owner()->message(_("Paragraph environment type copied"));
268         }
269 }
270
271
272 void BufferView::pasteEnvironment()
273 {
274         if (available()) {
275                 text->pasteEnvironmentType(this);
276                 owner()->message(_("Paragraph environment type set"));
277                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
278         }
279 }
280
281
282 void BufferView::copy()
283 {
284         if (available()) {
285                 text->copySelection(this);
286                 owner()->message(_("Copy"));
287         }
288 }
289
290
291 void BufferView::cut(bool realcut)
292 {
293         if (available()) {
294                 hideCursor();
295                 update(text, BufferView::SELECT|BufferView::FITCUR);
296                 text->cutSelection(this, true, realcut);
297                 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
298                 owner()->message(_("Cut"));
299         }
300 }
301
302
303 void BufferView::paste()
304 {
305         if (!available()) return;
306
307         owner()->message(_("Paste"));
308
309         hideCursor();
310         // clear the selection
311         toggleSelection();
312         text->clearSelection();
313         update(text, BufferView::SELECT|BufferView::FITCUR);
314         
315         // paste
316         text->pasteSelection(this);
317         update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
318         
319         // clear the selection 
320         toggleSelection();
321         text->clearSelection();
322         update(text, BufferView::SELECT|BufferView::FITCUR);
323 }
324
325
326 /* these functions are for the spellchecker */ 
327 string const BufferView::nextWord(float & value)
328 {
329         if (!available()) {
330                 value = 1;
331                 return string();
332         }
333
334         return text->selectNextWordToSpellcheck(this, value);
335 }
336
337   
338 void BufferView::selectLastWord()
339 {
340         if (!available()) return;
341    
342         LyXCursor cur = text->selection.cursor;
343         hideCursor();
344         beforeChange(text);
345         text->selection.cursor = cur;
346         text->selectSelectedWord(this);
347         toggleSelection(false);
348         update(text, BufferView::SELECT|BufferView::FITCUR);
349 }
350
351
352 void BufferView::endOfSpellCheck()
353 {
354         if (!available()) return;
355    
356         hideCursor();
357         beforeChange(text);
358         text->selectSelectedWord(this);
359         text->clearSelection();
360         update(text, BufferView::SELECT|BufferView::FITCUR);
361 }
362
363
364 void BufferView::replaceWord(string const & replacestring)
365 {
366         if (!available()) return;
367
368         LyXText * tt = getLyXText();
369         hideCursor();
370         update(tt, BufferView::SELECT|BufferView::FITCUR);
371    
372         /* clear the selection (if there is any) */ 
373         toggleSelection(false);
374         update(tt, BufferView::SELECT|BufferView::FITCUR);
375    
376         /* clear the selection (if there is any) */ 
377         toggleSelection(false);
378         tt->replaceSelectionWithString(this, replacestring);
379    
380         tt->setSelectionOverString(this, replacestring);
381
382         // Go back so that replacement string is also spellchecked
383         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
384                 tt->cursorLeft(this);
385         }
386         update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
387 }
388 // End of spellchecker stuff
389
390
391 bool BufferView::lockInset(UpdatableInset * inset)
392 {
393         // don't relock if we're already locked
394         if (theLockingInset() == inset)
395                 return true;
396         if (!theLockingInset() && inset) {
397                 theLockingInset(inset);
398                 return true;
399         } else if (inset) {
400             return theLockingInset()->lockInsetInInset(this, inset);
401         }
402         return false;
403 }
404
405
406 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
407 {
408         if (available() && theLockingInset()) {
409                 LyXCursor cursor = text->cursor;
410                 Inset * locking_inset = theLockingInset()->getLockingInset();
411
412                 if ((cursor.pos() - 1 >= 0) &&
413                     cursor.par()->isInset(cursor.pos() - 1) &&
414                     (cursor.par()->getInset(cursor.pos() - 1) ==
415                      locking_inset))
416                         text->setCursor(this, cursor,
417                                         cursor.par(), cursor.pos() - 1);
418                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
419                 LyXText * txt = getLyXText();
420                 if (locking_inset->isTextInset() &&
421                     locking_inset->lyxCode() != Inset::ERT_CODE &&
422                     (txt->real_current_font.language() !=
423                      buffer()->params.language
424                      || txt->real_current_font.isVisibleRightToLeft()
425                      != buffer()->params.language->RightToLeft()))
426                         shape = (txt->real_current_font.isVisibleRightToLeft())
427                                 ? LyXScreen::REVERSED_L_SHAPE
428                                 : LyXScreen::L_SHAPE;
429                 y += cursor.y() + theLockingInset()->insetInInsetY();
430                 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
431                                                   shape);
432         }
433 }
434
435
436 void BufferView::hideLockedInsetCursor()
437 {
438         if (theLockingInset() && available()) {
439                 pimpl_->screen_->hideCursor();
440         }
441 }
442
443
444 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
445 {
446         if (theLockingInset() && available()) {
447                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
448                 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
449                         updateScrollbar();
450         }
451 }
452
453
454 int BufferView::unlockInset(UpdatableInset * inset)
455 {
456         if (inset && theLockingInset() == inset) {
457                 inset->insetUnlock(this);
458                 theLockingInset(0);
459                 finishUndo();
460                 return 0;
461         } else if (inset && theLockingInset() &&
462                    theLockingInset()->unlockInsetInInset(this, inset)) {
463                 finishUndo();
464                 return 0;
465         }
466         return bufferlist.unlockInset(inset);
467 }
468
469
470 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
471 {
472         if (!theLockingInset())
473                 return; // shouldn't happen
474         if (kind == Undo::EDIT) // in this case insets would not be stored!
475                 kind = Undo::FINISH;
476         setUndo(this, kind,
477                 text->cursor.par(),
478                 text->cursor.par()->next());
479 }
480
481
482 void BufferView::updateInset(Inset * inset, bool mark_dirty)
483 {
484         pimpl_->updateInset(inset, mark_dirty);
485 }
486
487
488 bool BufferView::ChangeInsets(Inset::Code code,
489                               string const & from, string const & to)
490 {
491         bool flag = false;
492         LyXCursor cursor = text->cursor;
493         LyXCursor tmpcursor = cursor;
494         cursor.par(tmpcursor.par());
495         cursor.pos(tmpcursor.pos());
496
497         ParIterator end = buffer()->par_iterator_end();
498         for (ParIterator it = buffer()->par_iterator_begin();
499              it != end; ++it) {
500                 Paragraph * par = *it;
501                 bool flag2 = false;
502                 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
503                      it2 != par->inset_iterator_end(); ++it2) {
504                         if ((*it2)->lyxCode() == code) {
505                                 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
506                                 if (inset->getContents() == from) {
507                                         inset->setContents(to);
508                                         flag2 = true;
509                                 }
510                         }
511                 }
512                 if (flag2) {
513                         flag = true;
514 #warning Fix me
515                         // The test it.size()==1 was needed to prevent crashes.
516                         // How to set the cursor corretly when it.size()>1 ??
517                         if (it.size() == 1) {
518                                 text->setCursorIntern(this, par, 0);
519                                 text->redoParagraphs(this, text->cursor,
520                                                      text->cursor.par()->next());
521                                 text->fullRebreak(this);
522                         }
523                 }
524         }
525         text->setCursorIntern(this, cursor.par(), cursor.pos());
526         return flag;
527 }
528
529
530 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
531 {
532         // Check if the label 'from' appears more than once
533         vector<string> labels = buffer()->getLabelList();
534         if (count(labels.begin(), labels.end(), from) > 1)
535                 return false;
536
537         return ChangeInsets(Inset::REF_CODE, from, to);
538 }
539
540
541 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
542 {
543
544         vector<pair<string,string> > keys = buffer()->getBibkeyList();  
545         if (count_if(keys.begin(), keys.end(), 
546                      lyx::equal_1st_in_pair<string,string>(from)) 
547             > 1)
548                 return false;
549
550         return ChangeInsets(Inset::CITE_CODE, from, to);
551 }
552
553
554 UpdatableInset * BufferView::theLockingInset() const
555 {
556         // If NULL is not allowed we should put an Assert here. (Lgb)
557         if (text)
558                 return text->the_locking_inset;
559         return 0;
560 }
561
562
563 void BufferView::theLockingInset(UpdatableInset * inset)
564 {
565         text->the_locking_inset = inset;
566 }
567
568
569 LyXText * BufferView::getLyXText() const
570 {
571         if (theLockingInset()) {
572                 LyXText * txt = theLockingInset()->getLyXText(this, true);
573                 if (txt)
574                         return txt;
575         }
576         return text;
577 }
578
579
580 LyXText * BufferView::getParentText(Inset * inset) const
581 {
582         if (inset->owner()) {
583                 LyXText * txt = inset->getLyXText(this);
584                 inset = inset->owner();
585                 while (inset && inset->getLyXText(this) == txt)
586                         inset = inset->owner();
587                 if (inset)
588                         return inset->getLyXText(this);
589         }
590         return text;
591 }
592
593
594 Language const * BufferView::getParentLanguage(Inset * inset) const
595 {
596         LyXText * text = getParentText(inset);
597         return text->cursor.par()->getFontSettings(buffer()->params,
598                                                    text->cursor.pos()).language();
599 }