]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Move Lexer to support/ directory (and lyx::support namespace)
[lyx.git] / src / Counters.h
1 // -*- C++ -*-
2 /**
3  * \file Counters.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author Martin Vermeer
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef COUNTERS_H
16 #define COUNTERS_H
17
18 #include "OutputEnums.h"
19
20 #include "support/docstring.h"
21
22 #include <map>
23 #include <vector>
24
25
26 namespace lyx {
27
28 class Layout;
29
30 namespace support { class Lexer; }
31
32 /// This represents a single counter.
33 class Counter {
34 public:
35         ///
36         Counter();
37         ///
38         Counter(docstring const & mc, docstring const & ls,
39                 docstring const & lsa, docstring const & prettyformat,
40                 docstring const & guiname);
41         /// \return true on success
42         bool read(support::Lexer & lex);
43         ///
44         void set(int v);
45         ///
46         void addto(int v);
47         ///
48         int value() const;
49         ///
50         void saveValue();
51         ///
52         void restoreValue();
53         ///
54         void step();
55         ///
56         void reset();
57         /// Returns the parent counter of this counter.
58         docstring const & parent() const;
59         /// Checks if the parent counter is cnt, and if so removes
60         /// it. This is used when a counter is deleted.
61         /// \return whether we removed the parent.
62         bool checkAndRemoveParent(docstring const & cnt);
63         /// Returns a LaTeX-like string to format the counter.
64         /** This is similar to what one gets in LaTeX when using
65          *  "\the<counter>". The \c in_appendix bool tells whether we
66          *  want the version shown in an appendix.
67          */
68         docstring const & labelString(bool in_appendix) const;
69         /// Similar, but used for formatted references in XHTML output.
70         /// E.g., for a section counter it might be "section \thesection"
71         docstring const & prettyFormat() const { return prettyformat_; }
72         ///
73         docstring const & refFormat(docstring const & prefix) const;
74         ///
75         docstring const & guiName() const { return guiname_; }
76         ///
77         docstring const & latexName() const { return latexname_; }
78
79         /// Returns a map of LaTeX-like strings to format the counter.
80         /** For each language, the string is similar to what one gets
81          *  in LaTeX when using "\the<counter>". The \c in_appendix
82          *  bool tells whether we want the version shown in an
83          *  appendix. This version does not contain any \\the<counter>
84          *  expression.
85          */
86         typedef std::map<std::string, docstring> StringMap;
87         StringMap & flatLabelStrings(bool in_appendix) const;
88 private:
89         ///
90         int value_;
91         /// This is actually one less than the initial value, since the
92         /// counter is always stepped before being used.
93         int initial_value_;
94         ///
95         int saved_value_;
96         /// contains parent counter name.
97         /** The parent counter is the counter that, if stepped
98          *  (incremented) zeroes this counter. E.g. "subsection"'s
99          *  parent is "section".
100          */
101         docstring parent_;
102         /// Contains a LaTeX-like string to format the counter.
103         docstring labelstring_;
104         /// The same as labelstring_, but in appendices.
105         docstring labelstringappendix_;
106         /// Similar, but used for formatted references in XHTML output
107         docstring prettyformat_;
108         ///
109         std::map<docstring, docstring> ref_formats_;
110         ///
111         docstring guiname_;
112         /// The name used for the counter in LaTeX
113         docstring latexname_;
114         /// Cache of the labelstring with \\the<counter> expressions expanded,
115         /// indexed by language
116         mutable StringMap flatlabelstring_;
117         /// Cache of the appendix labelstring with \\the<counter> expressions expanded,
118         /// indexed by language
119         mutable StringMap flatlabelstringappendix_;
120 };
121
122
123 /// This is a class of (La)TeX type counters.
124 /// Every instantiation is an array of counters of type Counter.
125 class Counters {
126 public:
127         /// NOTE Do not call this in an attempt to clear the counters.
128         /// That will wipe out all the information we have about them
129         /// from the document class (e.g., which ones are defined).
130         /// Instead, call Counters::reset().
131         Counters();
132         /// Add new counter newc having parentc its parent,
133         /// ls as its label, and lsa as its appendix label.
134         void newCounter(docstring const & newc,
135                         docstring const & parentc,
136                         docstring const & ls,
137                         docstring const & lsa,
138                         docstring const & prettyformat,
139                         docstring const & guiname);
140         /// Checks whether the given counter exists.
141         bool hasCounter(docstring const & c) const;
142         /// reads the counter name
143         /// \param makeNew whether to make a new counter if one
144         ///        doesn't already exist
145         /// \return true on success
146         bool read(support::Lexer & lex, docstring const & name, bool makenew);
147         ///
148         void set(docstring const & ctr, int val);
149         ///
150         void addto(docstring const & ctr, int val);
151         ///
152         int value(docstring const & ctr) const;
153         ///
154         void saveValue(docstring const & ctr) const;
155         ///
156         void restoreValue(docstring const & ctr) const;
157         /// Reset recursively all the counters that are children of the one named by \c ctr.
158         void resetChildren(docstring const & ctr);
159         /// Increment by one the parent of counter named by \c ctr.
160         /// This also resets the counter named by \c ctr.
161         /// \param utype determines whether we track the counters.
162         void stepParent(docstring const & ctr, UpdateType utype);
163         /// Increment by one counter named by \c ctr, and zeroes child
164         /// counter(s) for which it is the parent.
165         /// \param utype determines whether we track the counters.
166         void step(docstring const & ctr, UpdateType utype);
167         /// Reset all counters, and all the internal data structures
168         /// used for keeping track of their values.
169         void reset();
170         /// Reset counters matched by match string.
171         void reset(docstring const & match);
172         /// Copy counter \p cnt to \p newcnt.
173         bool copy(docstring const & cnt, docstring const & newcnt);
174         /// Remove counter \p cnt.
175         bool remove(docstring const & cnt);
176         /** returns the expanded string representation of counter \c
177          *  c. The \c lang code is used to translate the string.
178          */
179         docstring theCounter(docstring const & c,
180                              std::string const & lang) const;
181         /** Replace in \c format all the LaTeX-like macros that depend
182          * on counters. The \c lang code is used to translate the
183          * string.
184          */
185         docstring counterLabel(docstring const & format,
186                                std::string const & lang) const;
187         /// returns a formatted version of the counter, using the
188         /// format given by Counter::prettyFormat().
189         docstring prettyCounter(docstring const & cntr,
190                                std::string const & lang) const;
191         /// returns a formatted version of the counter, using the
192         /// format given by Counter::prettyFormat().
193         docstring formattedCounter(docstring const & cntr,
194                                         docstring const & prefix,
195                                         std::string const & lang) const;
196         ///
197         docstring const & guiName(docstring const & cntr) const;
198         ///
199         docstring const & latexName(docstring const & cntr) const;
200         /// Are we in appendix?
201         bool appendix() const { return appendix_; }
202         /// Set the state variable indicating whether we are in appendix.
203         void appendix(bool a) { appendix_ = a; }
204         /// Returns the current enclosing float.
205         std::string const & current_float() const { return current_float_; }
206         /// Sets the current enclosing float.
207         void current_float(std::string const & f) { current_float_ = f; }
208         /// Are we in a subfloat?
209         bool isSubfloat() const { return subfloat_; }
210         /// Set the state variable indicating whether we are in a subfloat.
211         void isSubfloat(bool s) { subfloat_ = s; }
212         /// Are we in a longtable?
213         bool isLongtable() const { return longtable_; }
214         /// Set the state variable indicating whether we are in a longtable.
215         void isLongtable(bool s) { longtable_ = s; }
216
217         /// \name refstepcounter
218         // @{
219         /// The currently active counter, so far as references go.
220         /// We're trying to track \refstepcounter in LaTeX, more or less.
221         /// Note that this may be empty.
222         docstring currentCounter() const;
223         /// Called during updateBuffer() as we go through various paragraphs,
224         /// to track the layouts as we go through.
225         void setActiveLayout(Layout const & lay);
226         /// Also for updateBuffer().
227         /// Call this when entering things like footnotes, where there is now
228         /// no "last layout" and we want to restore the "last layout" on exit.
229         void clearLastLayout() { layout_stack_.push_back(nullptr); }
230         /// Call this when exiting things like footnotes.
231         void restoreLastLayout() { layout_stack_.pop_back(); }
232         ///
233         void saveLastCounter()
234                 { counter_stack_.push_back(counter_stack_.back()); }
235         ///
236         void restoreLastCounter() { counter_stack_.pop_back(); }
237         // @}
238         ///
239         std::vector<docstring> listOfCounters() const;
240 private:
241         /** expands recursively any \\the<counter> macro in the
242          *  labelstring of \c counter.  The \c lang code is used to
243          *  translate the string.
244          */
245         docstring flattenLabelString(docstring const & counter, bool in_appendix,
246                                      std::string const &lang,
247                                      std::vector<docstring> & callers) const;
248         /// Returns the value of the counter according to the
249         /// numbering scheme numbertype.
250         /** Available numbering schemes are arabic (1, 2,...), roman
251          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
252          *  B,...) and hebrew.
253          */
254         docstring labelItem(docstring const & ctr,
255                             docstring const & numbertype) const;
256         /// Used for managing the counter_stack_.
257         // @{
258         void beginEnvironment();
259         void endEnvironment();
260         // @}
261         /// Maps counter (layout) names to actual counters.
262         typedef std::map<docstring, Counter> CounterList;
263         /// Instantiate.
264         CounterList counterList_;
265         /// Are we in an appendix?
266         bool appendix_;
267         /// The current enclosing float.
268         std::string current_float_;
269         /// Are we in a subfloat?
270         bool subfloat_;
271         /// Are we in a longtable?
272         bool longtable_;
273         /// Used to keep track of active counters.
274         std::vector<docstring> counter_stack_;
275         /// Same, but for last layout.
276         std::vector<Layout const *> layout_stack_;
277 };
278
279 } // namespace lyx
280
281 #endif