]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / TextCache.C
1 /**
2  * \file TextCache.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "TextCache.h"
14 #include "buffer.h"
15 #include "lyxtext.h"
16 #include "bufferlist.h"
17 #include "debug.h"
18
19 #include <algorithm>
20
21 using std::ostream;
22 using std::for_each;
23 using std::remove_if;
24 using std::find_if;
25 using std::endl;
26 using std::make_pair;
27
28 extern BufferList bufferlist;
29
30 namespace {
31
32 class text_fits {
33 public:
34         text_fits(Buffer * b, int p)
35                 : buf(b), pw(p) {}
36         bool operator()(TextCache::value_type const & vt) const {
37                 if (vt.first == buf && vt.second.first == pw)
38                         return true;
39                 return false;
40         }
41 private:
42         Buffer * buf;
43         int pw;
44 };
45
46
47 class show_text {
48 public:
49         show_text(ostream & o) : os(o) {}
50         void operator()(TextCache::value_type const & vt) {
51                 os << "\tBuffer: " << vt.first
52                    << "\tWidth: " << vt.second.first << endl;
53         }
54 private:
55         ostream & os;
56 };
57
58
59 class delete_text {
60 public:
61         void operator()(TextCache::value_type & vt) {
62                 delete vt.second.second;
63         }
64 };
65
66 } // namespace anon
67
68
69 LyXText * TextCache::findFit(Buffer * b, int p)
70 {
71         Cache::iterator it = find_if(cache.begin(), cache.end(),
72                                      text_fits(b, p));
73         if (it != cache.end()) {
74                 LyXText * tmp = it->second.second;
75                 cache.erase(it);
76                 return tmp;
77         }
78         return 0;
79 }
80
81
82 void TextCache::show(ostream & os, string const & str)
83 {
84         os << "TextCache: " << str << endl;
85         for_each(cache.begin(), cache.end(), show_text(os));
86 }
87
88
89 void TextCache::show(ostream & os, TextCache::value_type const & vt)
90 {
91         show_text st(os);
92         st(vt);
93 }
94
95
96 void TextCache::add(Buffer * buf, int workwidth, LyXText * text)
97 {
98         lyxerr[Debug::INFO] << "TextCache::add " << text;
99         if (bufferlist.isLoaded(buf)) {
100                 cache[buf] = make_pair(workwidth, text);
101                 lyxerr[Debug::INFO] << " added" << endl;
102         } else {
103                 delete text;
104                 lyxerr[Debug::INFO] << " deleted" << endl;
105         }
106 }
107
108
109 void TextCache::clear()
110 {
111         for_each(cache.begin(), cache.end(), delete_text());
112         cache.clear();
113 }
114
115
116 void TextCache::removeAllWithBuffer(Buffer * buf)
117 {
118         cache.erase(buf);
119 }
120
121 // Global instance
122 TextCache textcache;