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 master counter of this counter.
56 docstring const & master() const;
57 /// Checks if the master counter is cnt, and if so removes
58 /// it. This is used when a counter is deleted.
59 /// \return whether we removed the master.
60 bool checkAndRemoveMaster(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 /// Returns a map of LaTeX-like strings to format the counter.
74 /** For each language, the string is similar to what one gets
75 * in LaTeX when using "\the<counter>". The \c in_appendix
76 * bool tells whether we want the version shown in an
77 * appendix. This version does not contain any \\the<counter>
80 typedef std::map<std::string, docstring> StringMap;
81 StringMap & flatLabelStrings(bool in_appendix) const;
85 /// This is actually one less than the initial value, since the
86 /// counter is always stepped before being used.
90 /// contains master counter name.
91 /** The master counter is the counter that, if stepped
92 * (incremented) zeroes this counter. E.g. "subsection"'s
93 * master is "section".
96 /// Contains a LaTeX-like string to format the counter.
97 docstring labelstring_;
98 /// The same as labelstring_, but in appendices.
99 docstring labelstringappendix_;
100 /// Similar, but used for formatted references in XHTML output
101 docstring prettyformat_;
104 /// Cache of the labelstring with \\the<counter> expressions expanded,
105 /// indexed by language
106 mutable StringMap flatlabelstring_;
107 /// Cache of the appendix labelstring with \\the<counter> expressions expanded,
108 /// indexed by language
109 mutable StringMap flatlabelstringappendix_;
113 /// This is a class of (La)TeX type counters.
114 /// Every instantiation is an array of counters of type Counter.
117 /// NOTE Do not call this in an attempt to clear the counters.
118 /// That will wipe out all the information we have about them
119 /// from the document class (e.g., which ones are defined).
120 /// Instead, call Counters::reset().
122 /// Add new counter newc having masterc as its master,
123 /// ls as its label, and lsa as its appendix label.
124 void newCounter(docstring const & newc,
125 docstring const & masterc,
126 docstring const & ls,
127 docstring const & lsa,
128 docstring const & guiname);
129 /// Checks whether the given counter exists.
130 bool hasCounter(docstring const & c) const;
131 /// reads the counter name
132 /// \param makeNew whether to make a new counter if one
133 /// doesn't already exist
134 /// \return true on success
135 bool read(Lexer & lex, docstring const & name, bool makenew);
137 void set(docstring const & ctr, int val);
139 void addto(docstring const & ctr, int val);
141 int value(docstring const & ctr) const;
143 void saveValue(docstring const & ctr) const;
145 void restoreValue(docstring const & ctr) const;
146 /// Reset recursively all the counters that are slaves of the one named by \c ctr.
147 void resetSlaves(docstring const & ctr);
148 /// Increment by one master of counter named by \c ctr.
149 /// This also resets the counter named by \c ctr.
150 /// \param utype determines whether we track the counters.
151 void stepMaster(docstring const & ctr, UpdateType utype);
152 /// Increment by one counter named by \c ctr, and zeroes slave
153 /// counter(s) for which it is the master.
154 /// \param utype determines whether we track the counters.
155 void step(docstring const & ctr, UpdateType utype);
156 /// Reset all counters, and all the internal data structures
157 /// used for keeping track of their values.
159 /// Reset counters matched by match string.
160 void reset(docstring const & match);
161 /// Remove counter \p cnt.
162 bool remove(docstring const & cnt);
163 /// Copy counters whose name matches match from the &from to
164 /// the &to array of counters. Empty string matches all.
165 void copy(Counters & from, Counters & to,
166 docstring const & match = docstring());
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;
184 /// Are we in appendix?
185 bool appendix() const { return appendix_; }
186 /// Set the state variable indicating whether we are in appendix.
187 void appendix(bool a) { appendix_ = a; }
188 /// Returns the current enclosing float.
189 std::string const & current_float() const { return current_float_; }
190 /// Sets the current enclosing float.
191 void current_float(std::string const & f) { current_float_ = f; }
192 /// Are we in a subfloat?
193 bool isSubfloat() const { return subfloat_; }
194 /// Set the state variable indicating whether we are in a subfloat.
195 void isSubfloat(bool s) { subfloat_ = s; }
196 /// Are we in a longtable?
197 bool isLongtable() const { return longtable_; }
198 /// Set the state variable indicating whether we are in a longtable.
199 void isLongtable(bool s) { longtable_ = s; }
201 /// \name refstepcounter
203 /// The currently active counter, so far as references go.
204 /// We're trying to track \refstepcounter in LaTeX, more or less.
205 /// Note that this may be empty.
206 docstring currentCounter() const;
207 /// Called during updateBuffer() as we go through various paragraphs,
208 /// to track the layouts as we go through.
209 void setActiveLayout(Layout const & lay);
210 /// Also for updateBuffer().
211 /// Call this when entering things like footnotes, where there is now
212 /// no "last layout" and we want to restore the "last layout" on exit.
213 void clearLastLayout() { layout_stack_.push_back(nullptr); }
214 /// Call this when exiting things like footnotes.
215 void restoreLastLayout() { layout_stack_.pop_back(); }
217 void saveLastCounter()
218 { counter_stack_.push_back(counter_stack_.back()); }
220 void restoreLastCounter() { counter_stack_.pop_back(); }
223 std::vector<docstring> listOfCounters() const;
225 /** expands recursively any \\the<counter> macro in the
226 * labelstring of \c counter. The \c lang code is used to
227 * translate the string.
229 docstring flattenLabelString(docstring const & counter, bool in_appendix,
230 std::string const &lang,
231 std::vector<docstring> & callers) const;
232 /// Returns the value of the counter according to the
233 /// numbering scheme numbertype.
234 /** Available numbering schemes are arabic (1, 2,...), roman
235 * (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
238 docstring labelItem(docstring const & ctr,
239 docstring const & numbertype) const;
240 /// Used for managing the counter_stack_.
242 void beginEnvironment();
243 void endEnvironment();
245 /// Maps counter (layout) names to actual counters.
246 typedef std::map<docstring, Counter> CounterList;
248 CounterList counterList_;
249 /// Are we in an appendix?
251 /// The current enclosing float.
252 std::string current_float_;
253 /// Are we in a subfloat?
255 /// Are we in a longtable?
257 /// Used to keep track of active counters.
258 std::vector<docstring> counter_stack_;
259 /// Same, but for last layout.
260 std::vector<Layout const *> layout_stack_;