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