]> git.lyx.org Git - lyx.git/blob - src/Counters.h
tex2lyx: support for multiple indices and subindices
[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 class Lexer;
30
31 /// This represents a single counter.
32 class Counter {
33 public:
34         ///
35         Counter();
36         ///
37         Counter(docstring const & mc, docstring const & ls, 
38                 docstring const & lsa);
39         /// \return true on success
40         bool read(Lexer & lex);
41         ///
42         void set(int v);
43         ///
44         void addto(int v);
45         ///
46         int value() const;
47         ///
48         void step();
49         ///
50         void reset();
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.
61          */
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_; }
66         
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>
72          *  expression.
73          */
74         typedef std::map<std::string, docstring> StringMap;
75         StringMap & flatLabelStrings(bool in_appendix) const;
76 private:
77         ///
78         int value_;
79         /// contains master counter name.
80         /** The master counter is the counter that, if stepped
81          *  (incremented) zeroes this counter. E.g. "subsection"'s
82          *  master is "section".
83          */
84         docstring master_;
85         /// Contains a LaTeX-like string to format the counter.
86         docstring labelstring_;
87         /// The same as labelstring_, but in appendices.
88         docstring labelstringappendix_;
89         /// Similar, but used for formatted references in XHTML output
90         docstring prettyformat_;
91         /// Cache of the labelstring with \\the<counter> expressions expanded, 
92         /// indexed by language
93         mutable StringMap flatlabelstring_;
94         /// Cache of the appendix labelstring with \\the<counter> expressions expanded, 
95         /// indexed by language
96         mutable StringMap flatlabelstringappendix_;
97 };
98
99
100 /// This is a class of (La)TeX type counters.
101 /// Every instantiation is an array of counters of type Counter.
102 class Counters {
103 public:
104         /// NOTE Do not call this in an attempt to clear the counters.
105         /// That will wipe out all the information we have about them
106         /// from the document class (e.g., which ones are defined).
107         /// Instead, call Counters::reset().
108         Counters();
109         /// Add new counter newc having masterc as its master, 
110         /// ls as its label, and lsa as its appendix label.
111         void newCounter(docstring const & newc,
112                         docstring const & masterc,
113                         docstring const & ls,
114                         docstring const & lsa);
115         /// Checks whether the given counter exists.
116         bool hasCounter(docstring const & c) const;
117         /// reads the counter name
118         /// \param makeNew whether to make a new counter if one 
119         ///        doesn't already exist
120         /// \return true on success
121         bool read(Lexer & lex, docstring const & name, bool makenew);
122         ///
123         void set(docstring const & ctr, int val);
124         ///
125         void addto(docstring const & ctr, int val);
126         ///
127         int value(docstring const & ctr) const;
128         /// Increment by one counter named by arg, and zeroes slave
129         /// counter(s) for which it is the master.
130         /// Sub-slaves are not zeroed! That happens at slave's first 
131         /// step 0->1. Seems to be sufficient.
132         /// \param utype determines whether we track the counters.
133         void step(docstring const & ctr, UpdateType utype);
134         /// Reset all counters, and all the internal data structures
135         /// used for keeping track of their values.
136         void reset();
137         /// Reset counters matched by match string.
138         void reset(docstring const & match);
139         /// Remove counter \p cnt.
140         bool remove(docstring const & cnt);
141         /// Copy counters whose name matches match from the &from to
142         /// the &to array of counters. Empty string matches all.
143         void copy(Counters & from, Counters & to,
144                   docstring const & match = docstring());
145         /** returns the expanded string representation of counter \c
146          *  c. The \c lang code is used to translate the string.
147          */
148         docstring theCounter(docstring const & c,
149                              std::string const & lang) const;
150         /** Replace in \c format all the LaTeX-like macros that depend
151          * on counters. The \c lang code is used to translate the
152          * string.
153          */
154         docstring counterLabel(docstring const & format,
155                                std::string const & lang) const;
156         /// returns a formatted version of the counter, using the 
157         /// format given by Counter::prettyFormat().
158         docstring prettyCounter(docstring const & cntr,
159                                std::string const & lang) const;
160         /// Are we in appendix?
161         bool appendix() const { return appendix_; }
162         /// Set the state variable indicating whether we are in appendix.
163         void appendix(bool a) { appendix_ = a; }
164         /// Returns the current enclosing float.
165         std::string const & current_float() const { return current_float_; }
166         /// Sets the current enclosing float.
167         void current_float(std::string const & f) { current_float_ = f; }
168         /// Are we in a subfloat?
169         bool isSubfloat() const { return subfloat_; }
170         /// Set the state variable indicating whether we are in a subfloat.
171         void isSubfloat(bool s) { subfloat_ = s; }
172         
173         /// \name refstepcounter        
174         // @{
175         /// The currently active counter, so far as references go.
176         /// We're trying to track \refstepcounter in LaTeX, more or less.
177         /// Note that this may be empty.
178         docstring currentCounter() const;
179         /// Called during updateBuffer() as we go through various paragraphs,
180         /// to track the layouts as we go through.
181         void setActiveLayout(Layout const & lay);
182         /// Also for updateBuffer().
183         /// Call this when entering things like footnotes, where there is now
184         /// no "last layout" and we want to restore the "last layout" on exit.
185         void clearLastLayout() { layout_stack_.push_back(0); }
186         /// Call this when exiting things like footnotes.
187         void restoreLastLayout() { layout_stack_.pop_back(); }
188         /// 
189         void saveLastCounter()
190                 { counter_stack_.push_back(counter_stack_.back()); }
191         /// 
192         void restoreLastCounter() { counter_stack_.pop_back(); }
193         // @}
194 private:
195         /** expands recursively any \\the<counter> macro in the
196          *  labelstring of \c counter.  The \c lang code is used to
197          *  translate the string.
198          */
199         docstring flattenLabelString(docstring const & counter, bool in_appendix,
200                                      std::string const &lang,
201                                      std::vector<docstring> & callers) const;
202         /// Returns the value of the counter according to the
203         /// numbering scheme numbertype.
204         /** Available numbering schemes are arabic (1, 2,...), roman
205          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
206          *  B,...) and hebrew.
207          */
208         docstring labelItem(docstring const & ctr,
209                             docstring const & numbertype) const;
210         /// Used for managing the counter_stack_.
211         // @{
212         void beginEnvironment();
213         void endEnvironment();
214         // @}
215         /// Maps counter (layout) names to actual counters.
216         typedef std::map<docstring, Counter> CounterList;
217         /// Instantiate.
218         CounterList counterList_;
219         /// Are we in an appendix?
220         bool appendix_;
221         /// The current enclosing float.
222         std::string current_float_;
223         /// Are we in a subfloat?
224         bool subfloat_;
225         /// Used to keep track of active counters.
226         std::vector<docstring> counter_stack_;
227         /// Same, but for last layout.
228         std::vector<Layout const *> layout_stack_;
229 };
230
231 } // namespace lyx
232
233 #endif