2000-07-17 18:27:53 +00:00
|
|
|
// -*- C++ -*-
|
2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Counters.h
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author John Levon
|
|
|
|
* \author Martin Vermeer
|
2000-07-24 13:53:19 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
#ifndef COUNTERS_H
|
2001-02-12 14:09:09 +00:00
|
|
|
#define COUNTERS_H
|
2000-07-17 18:27:53 +00:00
|
|
|
|
2010-01-20 19:42:12 +00:00
|
|
|
#include "OutputEnums.h"
|
|
|
|
|
2020-10-23 12:59:54 +00:00
|
|
|
#include "support/docstring.h"
|
2006-10-20 20:30:00 +00:00
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include <map>
|
2010-12-03 22:42:22 +00:00
|
|
|
#include <vector>
|
2002-09-05 12:58:10 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2010-01-20 19:03:17 +00:00
|
|
|
class Layout;
|
2008-10-06 14:37:28 +00:00
|
|
|
class Lexer;
|
|
|
|
|
2002-08-07 14:15:06 +00:00
|
|
|
/// This represents a single counter.
|
2002-08-06 22:40:59 +00:00
|
|
|
class Counter {
|
2000-07-17 18:27:53 +00:00
|
|
|
public:
|
|
|
|
///
|
|
|
|
Counter();
|
|
|
|
///
|
2017-07-03 17:45:58 +00:00
|
|
|
Counter(docstring const & mc, docstring const & ls,
|
2020-05-03 03:11:53 +00:00
|
|
|
docstring const & lsa, docstring const & guiname);
|
2008-10-06 14:37:28 +00:00
|
|
|
/// \return true on success
|
|
|
|
bool read(Lexer & lex);
|
2007-08-16 11:07:00 +00:00
|
|
|
///
|
2000-07-17 18:27:53 +00:00
|
|
|
void set(int v);
|
|
|
|
///
|
|
|
|
void addto(int v);
|
|
|
|
///
|
|
|
|
int value() const;
|
|
|
|
///
|
2020-05-01 01:56:58 +00:00
|
|
|
void saveValue();
|
|
|
|
///
|
|
|
|
void restoreValue();
|
|
|
|
///
|
2000-07-17 18:27:53 +00:00
|
|
|
void step();
|
|
|
|
///
|
|
|
|
void reset();
|
2020-11-02 22:03:42 +00:00
|
|
|
/// Returns the parent counter of this counter.
|
|
|
|
docstring const & parent() const;
|
|
|
|
/// Checks if the parent counter is cnt, and if so removes
|
2011-01-13 01:47:41 +00:00
|
|
|
/// it. This is used when a counter is deleted.
|
2020-11-02 22:03:42 +00:00
|
|
|
/// \return whether we removed the parent.
|
|
|
|
bool checkAndRemoveParent(docstring const & cnt);
|
2017-07-03 17:45:58 +00:00
|
|
|
/// Returns a LaTeX-like string to format the counter.
|
2008-10-06 12:16:17 +00:00
|
|
|
/** This is similar to what one gets in LaTeX when using
|
2009-07-12 20:09:53 +00:00
|
|
|
* "\the<counter>". The \c in_appendix bool tells whether we
|
|
|
|
* want the version shown in an appendix.
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2009-04-19 20:05:26 +00:00
|
|
|
docstring const & labelString(bool in_appendix) const;
|
2010-01-20 20:33:36 +00:00
|
|
|
/// Similar, but used for formatted references in XHTML output.
|
|
|
|
/// E.g., for a section counter it might be "section \thesection"
|
|
|
|
docstring const & prettyFormat() const { return prettyformat_; }
|
2020-05-03 03:00:17 +00:00
|
|
|
///
|
|
|
|
docstring const & guiName() const { return guiname_; }
|
2017-07-03 17:45:58 +00:00
|
|
|
|
|
|
|
/// Returns a map of LaTeX-like strings to format the counter.
|
2009-07-12 20:09:53 +00:00
|
|
|
/** For each language, the string is similar to what one gets
|
|
|
|
* in LaTeX when using "\the<counter>". The \c in_appendix
|
|
|
|
* bool tells whether we want the version shown in an
|
|
|
|
* appendix. This version does not contain any \\the<counter>
|
|
|
|
* expression.
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2009-07-12 20:09:53 +00:00
|
|
|
typedef std::map<std::string, docstring> StringMap;
|
|
|
|
StringMap & flatLabelStrings(bool in_appendix) const;
|
2002-08-06 22:40:59 +00:00
|
|
|
private:
|
2002-09-05 12:58:10 +00:00
|
|
|
///
|
2000-07-17 18:27:53 +00:00
|
|
|
int value_;
|
2013-06-01 21:42:08 +00:00
|
|
|
/// This is actually one less than the initial value, since the
|
|
|
|
/// counter is always stepped before being used.
|
|
|
|
int initial_value_;
|
2020-05-01 01:56:58 +00:00
|
|
|
///
|
|
|
|
int saved_value_;
|
2020-11-02 22:03:42 +00:00
|
|
|
/// contains parent counter name.
|
|
|
|
/** The parent counter is the counter that, if stepped
|
2008-10-06 12:16:17 +00:00
|
|
|
* (incremented) zeroes this counter. E.g. "subsection"'s
|
2020-11-02 22:03:42 +00:00
|
|
|
* parent is "section".
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2020-11-02 22:03:42 +00:00
|
|
|
docstring parent_;
|
2008-10-06 12:16:17 +00:00
|
|
|
/// Contains a LaTeX-like string to format the counter.
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring labelstring_;
|
2008-10-06 12:16:17 +00:00
|
|
|
/// The same as labelstring_, but in appendices.
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring labelstringappendix_;
|
2010-01-20 20:33:36 +00:00
|
|
|
/// Similar, but used for formatted references in XHTML output
|
|
|
|
docstring prettyformat_;
|
2020-05-03 03:00:17 +00:00
|
|
|
///
|
|
|
|
docstring guiname_;
|
2017-07-03 17:45:58 +00:00
|
|
|
/// Cache of the labelstring with \\the<counter> expressions expanded,
|
2009-07-12 20:09:53 +00:00
|
|
|
/// indexed by language
|
|
|
|
mutable StringMap flatlabelstring_;
|
2017-07-03 17:45:58 +00:00
|
|
|
/// Cache of the appendix labelstring with \\the<counter> expressions expanded,
|
2009-07-12 20:09:53 +00:00
|
|
|
/// indexed by language
|
|
|
|
mutable StringMap flatlabelstringappendix_;
|
2000-07-17 18:27:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-11 20:34:20 +00:00
|
|
|
/// This is a class of (La)TeX type counters.
|
2002-08-07 14:15:06 +00:00
|
|
|
/// Every instantiation is an array of counters of type Counter.
|
2000-07-17 18:27:53 +00:00
|
|
|
class Counters {
|
|
|
|
public:
|
2010-12-09 18:10:44 +00:00
|
|
|
/// NOTE Do not call this in an attempt to clear the counters.
|
|
|
|
/// That will wipe out all the information we have about them
|
|
|
|
/// from the document class (e.g., which ones are defined).
|
|
|
|
/// Instead, call Counters::reset().
|
2010-12-02 20:22:10 +00:00
|
|
|
Counters();
|
2020-11-02 22:03:42 +00:00
|
|
|
/// Add new counter newc having parentc its parent,
|
2008-10-06 12:24:31 +00:00
|
|
|
/// ls as its label, and lsa as its appendix label.
|
2007-08-11 18:23:30 +00:00
|
|
|
void newCounter(docstring const & newc,
|
2020-11-02 22:03:42 +00:00
|
|
|
docstring const & parentc,
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring const & ls,
|
2020-05-03 03:11:53 +00:00
|
|
|
docstring const & lsa,
|
|
|
|
docstring const & guiname);
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Checks whether the given counter exists.
|
2007-08-11 18:23:30 +00:00
|
|
|
bool hasCounter(docstring const & c) const;
|
2008-10-06 14:37:28 +00:00
|
|
|
/// reads the counter name
|
2017-07-03 17:45:58 +00:00
|
|
|
/// \param makeNew whether to make a new counter if one
|
2009-08-12 14:50:01 +00:00
|
|
|
/// doesn't already exist
|
2008-10-06 14:37:28 +00:00
|
|
|
/// \return true on success
|
2009-08-12 14:50:01 +00:00
|
|
|
bool read(Lexer & lex, docstring const & name, bool makenew);
|
2000-07-17 18:27:53 +00:00
|
|
|
///
|
2007-08-11 18:23:30 +00:00
|
|
|
void set(docstring const & ctr, int val);
|
2000-07-17 18:27:53 +00:00
|
|
|
///
|
2007-08-11 18:23:30 +00:00
|
|
|
void addto(docstring const & ctr, int val);
|
|
|
|
///
|
|
|
|
int value(docstring const & ctr) const;
|
2020-05-01 01:56:58 +00:00
|
|
|
///
|
|
|
|
void saveValue(docstring const & ctr) const;
|
|
|
|
///
|
|
|
|
void restoreValue(docstring const & ctr) const;
|
2020-11-02 22:03:42 +00:00
|
|
|
/// Reset recursively all the counters that are children of the one named by \c ctr.
|
|
|
|
void resetChildren(docstring const & ctr);
|
|
|
|
/// Increment by one the parent of counter named by \c ctr.
|
2016-10-14 18:08:12 +00:00
|
|
|
/// This also resets the counter named by \c ctr.
|
|
|
|
/// \param utype determines whether we track the counters.
|
2020-11-02 22:03:42 +00:00
|
|
|
void stepParent(docstring const & ctr, UpdateType utype);
|
|
|
|
/// Increment by one counter named by \c ctr, and zeroes child
|
|
|
|
/// counter(s) for which it is the parent.
|
2010-01-20 19:47:27 +00:00
|
|
|
/// \param utype determines whether we track the counters.
|
|
|
|
void step(docstring const & ctr, UpdateType utype);
|
2010-12-09 18:10:44 +00:00
|
|
|
/// Reset all counters, and all the internal data structures
|
|
|
|
/// used for keeping track of their values.
|
2002-08-11 20:34:20 +00:00
|
|
|
void reset();
|
|
|
|
/// Reset counters matched by match string.
|
2007-08-11 18:23:30 +00:00
|
|
|
void reset(docstring const & match);
|
2011-01-12 22:53:41 +00:00
|
|
|
/// Remove counter \p cnt.
|
|
|
|
bool remove(docstring const & cnt);
|
2009-07-12 20:09:53 +00:00
|
|
|
/** returns the expanded string representation of counter \c
|
|
|
|
* c. The \c lang code is used to translate the string.
|
|
|
|
*/
|
|
|
|
docstring theCounter(docstring const & c,
|
|
|
|
std::string const & lang) const;
|
|
|
|
/** Replace in \c format all the LaTeX-like macros that depend
|
|
|
|
* on counters. The \c lang code is used to translate the
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
docstring counterLabel(docstring const & format,
|
|
|
|
std::string const & lang) const;
|
2017-07-03 17:45:58 +00:00
|
|
|
/// returns a formatted version of the counter, using the
|
2010-01-20 20:33:36 +00:00
|
|
|
/// format given by Counter::prettyFormat().
|
|
|
|
docstring prettyCounter(docstring const & cntr,
|
|
|
|
std::string const & lang) const;
|
2020-05-03 03:00:17 +00:00
|
|
|
///
|
|
|
|
docstring const & guiName(docstring const & cntr) const;
|
2009-04-19 20:05:26 +00:00
|
|
|
/// Are we in appendix?
|
2009-11-15 01:18:13 +00:00
|
|
|
bool appendix() const { return appendix_; }
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Set the state variable indicating whether we are in appendix.
|
2009-11-15 01:18:13 +00:00
|
|
|
void appendix(bool a) { appendix_ = a; }
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Returns the current enclosing float.
|
2007-08-11 18:23:30 +00:00
|
|
|
std::string const & current_float() const { return current_float_; }
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Sets the current enclosing float.
|
2007-08-11 18:23:30 +00:00
|
|
|
void current_float(std::string const & f) { current_float_ = f; }
|
2008-03-02 11:30:50 +00:00
|
|
|
/// Are we in a subfloat?
|
|
|
|
bool isSubfloat() const { return subfloat_; }
|
|
|
|
/// Set the state variable indicating whether we are in a subfloat.
|
2009-11-15 01:18:13 +00:00
|
|
|
void isSubfloat(bool s) { subfloat_ = s; }
|
2014-02-25 07:00:43 +00:00
|
|
|
/// Are we in a longtable?
|
|
|
|
bool isLongtable() const { return longtable_; }
|
|
|
|
/// Set the state variable indicating whether we are in a longtable.
|
|
|
|
void isLongtable(bool s) { longtable_ = s; }
|
2017-07-03 17:45:58 +00:00
|
|
|
|
|
|
|
/// \name refstepcounter
|
2010-01-20 19:03:17 +00:00
|
|
|
// @{
|
|
|
|
/// The currently active counter, so far as references go.
|
|
|
|
/// We're trying to track \refstepcounter in LaTeX, more or less.
|
|
|
|
/// Note that this may be empty.
|
|
|
|
docstring currentCounter() const;
|
2010-03-03 22:13:45 +00:00
|
|
|
/// Called during updateBuffer() as we go through various paragraphs,
|
2010-01-20 19:03:17 +00:00
|
|
|
/// to track the layouts as we go through.
|
|
|
|
void setActiveLayout(Layout const & lay);
|
2010-03-03 22:13:45 +00:00
|
|
|
/// Also for updateBuffer().
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Call this when entering things like footnotes, where there is now
|
|
|
|
/// no "last layout" and we want to restore the "last layout" on exit.
|
2020-02-28 06:48:10 +00:00
|
|
|
void clearLastLayout() { layout_stack_.push_back(nullptr); }
|
2010-12-02 21:05:45 +00:00
|
|
|
/// Call this when exiting things like footnotes.
|
2010-01-20 19:03:17 +00:00
|
|
|
void restoreLastLayout() { layout_stack_.pop_back(); }
|
2017-07-03 17:45:58 +00:00
|
|
|
///
|
2010-01-20 19:03:17 +00:00
|
|
|
void saveLastCounter()
|
|
|
|
{ counter_stack_.push_back(counter_stack_.back()); }
|
2017-07-03 17:45:58 +00:00
|
|
|
///
|
2010-01-20 19:03:17 +00:00
|
|
|
void restoreLastCounter() { counter_stack_.pop_back(); }
|
|
|
|
// @}
|
2020-05-01 01:56:58 +00:00
|
|
|
///
|
|
|
|
std::vector<docstring> listOfCounters() const;
|
2000-07-17 18:27:53 +00:00
|
|
|
private:
|
2010-01-20 19:03:17 +00:00
|
|
|
/** expands recursively any \\the<counter> macro in the
|
2009-07-12 20:09:53 +00:00
|
|
|
* labelstring of \c counter. The \c lang code is used to
|
|
|
|
* translate the string.
|
|
|
|
*/
|
|
|
|
docstring flattenLabelString(docstring const & counter, bool in_appendix,
|
|
|
|
std::string const &lang,
|
2010-12-03 22:42:22 +00:00
|
|
|
std::vector<docstring> & callers) const;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Returns the value of the counter according to the
|
|
|
|
/// numbering scheme numbertype.
|
2008-10-06 12:16:17 +00:00
|
|
|
/** Available numbering schemes are arabic (1, 2,...), roman
|
|
|
|
* (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
|
|
|
|
* B,...) and hebrew.
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2007-08-11 18:23:30 +00:00
|
|
|
docstring labelItem(docstring const & ctr,
|
2009-04-19 10:06:17 +00:00
|
|
|
docstring const & numbertype) const;
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Used for managing the counter_stack_.
|
|
|
|
// @{
|
|
|
|
void beginEnvironment();
|
|
|
|
void endEnvironment();
|
|
|
|
// @}
|
2002-08-07 14:15:06 +00:00
|
|
|
/// Maps counter (layout) names to actual counters.
|
2007-08-11 18:23:30 +00:00
|
|
|
typedef std::map<docstring, Counter> CounterList;
|
2002-08-07 14:15:06 +00:00
|
|
|
/// Instantiate.
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList counterList_;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Are we in an appendix?
|
2007-07-11 12:52:50 +00:00
|
|
|
bool appendix_;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// The current enclosing float.
|
2007-08-11 18:23:30 +00:00
|
|
|
std::string current_float_;
|
2008-03-02 11:30:50 +00:00
|
|
|
/// Are we in a subfloat?
|
|
|
|
bool subfloat_;
|
2014-02-25 07:00:43 +00:00
|
|
|
/// Are we in a longtable?
|
|
|
|
bool longtable_;
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Used to keep track of active counters.
|
2010-12-03 22:42:22 +00:00
|
|
|
std::vector<docstring> counter_stack_;
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Same, but for last layout.
|
2010-12-03 22:42:22 +00:00
|
|
|
std::vector<Layout const *> layout_stack_;
|
2000-07-17 18:27:53 +00:00
|
|
|
};
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
#endif
|