4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author Martin Vermeer
12 * Full author contact details are available in file CREDITS.
18 #include "OutputEnums.h"
20 #include "support/docstring.h"
31 /// This represents a single counter.
37 Counter(docstring const & mc, docstring const & ls,
38 docstring const & lsa, docstring const & guiname);
39 /// \return true on success
40 bool read(Lexer & lex);
55 /// Returns the parent counter of this counter.
56 docstring const & parent() const;
57 /// Checks if the parent counter is cnt, and if so removes
58 /// it. This is used when a counter is deleted.
59 /// \return whether we removed the parent.
60 bool checkAndRemoveParent(docstring const & cnt);
61 /// Returns a LaTeX-like string to format the counter.
62 /** This is similar to what one gets in LaTeX when using
63 * "\the<counter>". The \c in_appendix bool tells whether we
64 * want the version shown in an appendix.
66 docstring const & labelString(bool in_appendix) const;
67 /// Similar, but used for formatted references in XHTML output.
68 /// E.g., for a section counter it might be "section \thesection"
69 docstring const & prettyFormat() const { return prettyformat_; }
71 docstring const & guiName() const { return guiname_; }
73 docstring const & latexName() const { return latexname_; }
75 /// Returns a map of LaTeX-like strings to format the counter.
76 /** For each language, the string is similar to what one gets
77 * in LaTeX when using "\the<counter>". The \c in_appendix
78 * bool tells whether we want the version shown in an
79 * appendix. This version does not contain any \\the<counter>
82 typedef std::map<std::string, docstring> StringMap;
83 StringMap & flatLabelStrings(bool in_appendix) const;
87 /// This is actually one less than the initial value, since the
88 /// counter is always stepped before being used.
92 /// contains parent counter name.
93 /** The parent counter is the counter that, if stepped
94 * (incremented) zeroes this counter. E.g. "subsection"'s
95 * parent is "section".
98 /// Contains a LaTeX-like string to format the counter.
99 docstring labelstring_;
100 /// The same as labelstring_, but in appendices.
101 docstring labelstringappendix_;
102 /// Similar, but used for formatted references in XHTML output
103 docstring prettyformat_;
106 /// The name used for the counter in LaTeX
107 docstring latexname_;
108 /// Cache of the labelstring with \\the<counter> expressions expanded,
109 /// indexed by language
110 mutable StringMap flatlabelstring_;
111 /// Cache of the appendix labelstring with \\the<counter> expressions expanded,
112 /// indexed by language
113 mutable StringMap flatlabelstringappendix_;
117 /// This is a class of (La)TeX type counters.
118 /// Every instantiation is an array of counters of type Counter.
121 /// NOTE Do not call this in an attempt to clear the counters.
122 /// That will wipe out all the information we have about them
123 /// from the document class (e.g., which ones are defined).
124 /// Instead, call Counters::reset().
126 /// Add new counter newc having parentc its parent,
127 /// ls as its label, and lsa as its appendix label.
128 void newCounter(docstring const & newc,
129 docstring const & parentc,
130 docstring const & ls,
131 docstring const & lsa,
132 docstring const & guiname);
133 /// Checks whether the given counter exists.
134 bool hasCounter(docstring const & c) const;
135 /// reads the counter name
136 /// \param makeNew whether to make a new counter if one
137 /// doesn't already exist
138 /// \return true on success
139 bool read(Lexer & lex, docstring const & name, bool makenew);
141 void set(docstring const & ctr, int val);
143 void addto(docstring const & ctr, int val);
145 int value(docstring const & ctr) const;
147 void saveValue(docstring const & ctr) const;
149 void restoreValue(docstring const & ctr) const;
150 /// Reset recursively all the counters that are children of the one named by \c ctr.
151 void resetChildren(docstring const & ctr);
152 /// Increment by one the parent of counter named by \c ctr.
153 /// This also resets the counter named by \c ctr.
154 /// \param utype determines whether we track the counters.
155 void stepParent(docstring const & ctr, UpdateType utype);
156 /// Increment by one counter named by \c ctr, and zeroes child
157 /// counter(s) for which it is the parent.
158 /// \param utype determines whether we track the counters.
159 void step(docstring const & ctr, UpdateType utype);
160 /// Reset all counters, and all the internal data structures
161 /// used for keeping track of their values.
163 /// Reset counters matched by match string.
164 void reset(docstring const & match);
165 /// Remove counter \p cnt.
166 bool remove(docstring const & cnt);
167 /** returns the expanded string representation of counter \c
168 * c. The \c lang code is used to translate the string.
170 docstring theCounter(docstring const & c,
171 std::string const & lang) const;
172 /** Replace in \c format all the LaTeX-like macros that depend
173 * on counters. The \c lang code is used to translate the
176 docstring counterLabel(docstring const & format,
177 std::string const & lang) const;
178 /// returns a formatted version of the counter, using the
179 /// format given by Counter::prettyFormat().
180 docstring prettyCounter(docstring const & cntr,
181 std::string const & lang) const;
183 docstring const & guiName(docstring const & cntr) const;
185 docstring const & latexName(docstring const & cntr) const;
186 /// Are we in appendix?
187 bool appendix() const { return appendix_; }
188 /// Set the state variable indicating whether we are in appendix.
189 void appendix(bool a) { appendix_ = a; }
190 /// Returns the current enclosing float.
191 std::string const & current_float() const { return current_float_; }
192 /// Sets the current enclosing float.
193 void current_float(std::string const & f) { current_float_ = f; }
194 /// Are we in a subfloat?
195 bool isSubfloat() const { return subfloat_; }
196 /// Set the state variable indicating whether we are in a subfloat.
197 void isSubfloat(bool s) { subfloat_ = s; }
198 /// Are we in a longtable?
199 bool isLongtable() const { return longtable_; }
200 /// Set the state variable indicating whether we are in a longtable.
201 void isLongtable(bool s) { longtable_ = s; }
203 /// \name refstepcounter
205 /// The currently active counter, so far as references go.
206 /// We're trying to track \refstepcounter in LaTeX, more or less.
207 /// Note that this may be empty.
208 docstring currentCounter() const;
209 /// Called during updateBuffer() as we go through various paragraphs,
210 /// to track the layouts as we go through.
211 void setActiveLayout(Layout const & lay);
212 /// Also for updateBuffer().
213 /// Call this when entering things like footnotes, where there is now
214 /// no "last layout" and we want to restore the "last layout" on exit.
215 void clearLastLayout() { layout_stack_.push_back(nullptr); }
216 /// Call this when exiting things like footnotes.
217 void restoreLastLayout() { layout_stack_.pop_back(); }
219 void saveLastCounter()
220 { counter_stack_.push_back(counter_stack_.back()); }
222 void restoreLastCounter() { counter_stack_.pop_back(); }
225 std::vector<docstring> listOfCounters() const;
227 /** expands recursively any \\the<counter> macro in the
228 * labelstring of \c counter. The \c lang code is used to
229 * translate the string.
231 docstring flattenLabelString(docstring const & counter, bool in_appendix,
232 std::string const &lang,
233 std::vector<docstring> & callers) const;
234 /// Returns the value of the counter according to the
235 /// numbering scheme numbertype.
236 /** Available numbering schemes are arabic (1, 2,...), roman
237 * (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
240 docstring labelItem(docstring const & ctr,
241 docstring const & numbertype) const;
242 /// Used for managing the counter_stack_.
244 void beginEnvironment();
245 void endEnvironment();
247 /// Maps counter (layout) names to actual counters.
248 typedef std::map<docstring, Counter> CounterList;
250 CounterList counterList_;
251 /// Are we in an appendix?
253 /// The current enclosing float.
254 std::string current_float_;
255 /// Are we in a subfloat?
257 /// Are we in a longtable?
259 /// Used to keep track of active counters.
260 std::vector<docstring> counter_stack_;
261 /// Same, but for last layout.
262 std::vector<Layout const *> layout_stack_;