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