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);
39 /// \return true on success
40 bool read(Lexer & lex);
51 /// Returns the master counter of this counter.
52 docstring const & master() const;
53 /// Checks if the master counter is cnt, and if so removes
54 /// it. This is used when a counter is deleted.
55 /// \return whether we removed the master.
56 bool checkAndRemoveMaster(docstring const & cnt);
57 /// Returns a LaTeX-like string to format the counter.
58 /** This is similar to what one gets in LaTeX when using
59 * "\the<counter>". The \c in_appendix bool tells whether we
60 * want the version shown in an appendix.
62 docstring const & labelString(bool in_appendix) const;
63 /// Similar, but used for formatted references in XHTML output.
64 /// E.g., for a section counter it might be "section \thesection"
65 docstring const & prettyFormat() const { return prettyformat_; }
67 /// Returns a map of LaTeX-like strings to format the counter.
68 /** For each language, the string is similar to what one gets
69 * in LaTeX when using "\the<counter>". The \c in_appendix
70 * bool tells whether we want the version shown in an
71 * appendix. This version does not contain any \\the<counter>
74 typedef std::map<std::string, docstring> StringMap;
75 StringMap & flatLabelStrings(bool in_appendix) const;
79 /// This is actually one less than the initial value, since the
80 /// counter is always stepped before being used.
82 /// contains master counter name.
83 /** The master counter is the counter that, if stepped
84 * (incremented) zeroes this counter. E.g. "subsection"'s
85 * master is "section".
88 /// Contains a LaTeX-like string to format the counter.
89 docstring labelstring_;
90 /// The same as labelstring_, but in appendices.
91 docstring labelstringappendix_;
92 /// Similar, but used for formatted references in XHTML output
93 docstring prettyformat_;
94 /// Cache of the labelstring with \\the<counter> expressions expanded,
95 /// indexed by language
96 mutable StringMap flatlabelstring_;
97 /// Cache of the appendix labelstring with \\the<counter> expressions expanded,
98 /// indexed by language
99 mutable StringMap flatlabelstringappendix_;
103 /// This is a class of (La)TeX type counters.
104 /// Every instantiation is an array of counters of type Counter.
107 /// NOTE Do not call this in an attempt to clear the counters.
108 /// That will wipe out all the information we have about them
109 /// from the document class (e.g., which ones are defined).
110 /// Instead, call Counters::reset().
112 /// Add new counter newc having masterc as its master,
113 /// ls as its label, and lsa as its appendix label.
114 void newCounter(docstring const & newc,
115 docstring const & masterc,
116 docstring const & ls,
117 docstring const & lsa);
118 /// Checks whether the given counter exists.
119 bool hasCounter(docstring const & c) const;
120 /// reads the counter name
121 /// \param makeNew whether to make a new counter if one
122 /// doesn't already exist
123 /// \return true on success
124 bool read(Lexer & lex, docstring const & name, bool makenew);
126 void set(docstring const & ctr, int val);
128 void addto(docstring const & ctr, int val);
130 int value(docstring const & ctr) const;
131 /// Increment by one counter named by arg, and zeroes slave
132 /// counter(s) for which it is the master.
133 /// Sub-slaves are not zeroed! That happens at slave's first
134 /// step 0->1. Seems to be sufficient.
135 /// \param utype determines whether we track the counters.
136 void step(docstring const & ctr, UpdateType utype);
137 /// Reset all counters, and all the internal data structures
138 /// used for keeping track of their values.
140 /// Reset counters matched by match string.
141 void reset(docstring const & match);
142 /// Remove counter \p cnt.
143 bool remove(docstring const & cnt);
144 /// Copy counters whose name matches match from the &from to
145 /// the &to array of counters. Empty string matches all.
146 void copy(Counters & from, Counters & to,
147 docstring const & match = docstring());
148 /** returns the expanded string representation of counter \c
149 * c. The \c lang code is used to translate the string.
151 docstring theCounter(docstring const & c,
152 std::string const & lang) const;
153 /** Replace in \c format all the LaTeX-like macros that depend
154 * on counters. The \c lang code is used to translate the
157 docstring counterLabel(docstring const & format,
158 std::string const & lang) const;
159 /// returns a formatted version of the counter, using the
160 /// format given by Counter::prettyFormat().
161 docstring prettyCounter(docstring const & cntr,
162 std::string const & lang) const;
163 /// Are we in appendix?
164 bool appendix() const { return appendix_; }
165 /// Set the state variable indicating whether we are in appendix.
166 void appendix(bool a) { appendix_ = a; }
167 /// Returns the current enclosing float.
168 std::string const & current_float() const { return current_float_; }
169 /// Sets the current enclosing float.
170 void current_float(std::string const & f) { current_float_ = f; }
171 /// Are we in a subfloat?
172 bool isSubfloat() const { return subfloat_; }
173 /// Set the state variable indicating whether we are in a subfloat.
174 void isSubfloat(bool s) { subfloat_ = s; }
175 /// Are we in a longtable?
176 bool isLongtable() const { return longtable_; }
177 /// Set the state variable indicating whether we are in a longtable.
178 void isLongtable(bool s) { longtable_ = s; }
180 /// \name refstepcounter
182 /// The currently active counter, so far as references go.
183 /// We're trying to track \refstepcounter in LaTeX, more or less.
184 /// Note that this may be empty.
185 docstring currentCounter() const;
186 /// Called during updateBuffer() as we go through various paragraphs,
187 /// to track the layouts as we go through.
188 void setActiveLayout(Layout const & lay);
189 /// Also for updateBuffer().
190 /// Call this when entering things like footnotes, where there is now
191 /// no "last layout" and we want to restore the "last layout" on exit.
192 void clearLastLayout() { layout_stack_.push_back(0); }
193 /// Call this when exiting things like footnotes.
194 void restoreLastLayout() { layout_stack_.pop_back(); }
196 void saveLastCounter()
197 { counter_stack_.push_back(counter_stack_.back()); }
199 void restoreLastCounter() { counter_stack_.pop_back(); }
202 /** expands recursively any \\the<counter> macro in the
203 * labelstring of \c counter. The \c lang code is used to
204 * translate the string.
206 docstring flattenLabelString(docstring const & counter, bool in_appendix,
207 std::string const &lang,
208 std::vector<docstring> & callers) const;
209 /// Returns the value of the counter according to the
210 /// numbering scheme numbertype.
211 /** Available numbering schemes are arabic (1, 2,...), roman
212 * (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
215 docstring labelItem(docstring const & ctr,
216 docstring const & numbertype) const;
217 /// Used for managing the counter_stack_.
219 void beginEnvironment();
220 void endEnvironment();
222 /// Maps counter (layout) names to actual counters.
223 typedef std::map<docstring, Counter> CounterList;
225 CounterList counterList_;
226 /// Are we in an appendix?
228 /// The current enclosing float.
229 std::string current_float_;
230 /// Are we in a subfloat?
232 /// Are we in a longtable?
234 /// Used to keep track of active counters.
235 std::vector<docstring> counter_stack_;
236 /// Same, but for last layout.
237 std::vector<Layout const *> layout_stack_;