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