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