4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * Full author contact details are available in file CREDITS.
15 #include "DocumentClassPtr.h"
16 #include "FloatList.h"
19 #include "LayoutEnums.h"
20 #include "LayoutModuleList.h"
22 #include "insets/InsetLayout.h"
24 #include "support/docstring.h"
25 #include "support/types.h"
39 namespace support { class FileName; }
47 /// Based upon ideas in boost::noncopyable, inheriting from this
48 /// class effectively makes the copy constructor protected but the
49 /// assignment constructor private.
55 ProtectCopy(const ProtectCopy &) {}
57 const ProtectCopy & operator=(const ProtectCopy &);
61 /// A TextClass represents a collection of layout information: At the
62 /// moment, this includes Layout's and InsetLayout's.
64 /// There are two major subclasses of TextClass: LayoutFile and
65 /// DocumentClass. These subclasses are what are actually used in LyX.
66 /// Simple TextClass objects are not directly constructed in the main
67 /// LyX code---the constructor is protected. (That said, in tex2lyx
68 /// there are what amount to simple TextClass objects.)
70 /// A LayoutFile (see LayoutFile.{h,cpp}) represents a *.layout file.
71 /// These are generally static objects---though they can be reloaded
72 /// from disk via LFUN_LAYOUT_RELOAD, so one should not assume that
73 /// they will never change.
75 /// A DocumentClass (see below) represents the layout information that
76 /// is associated with a given Buffer. These are static, in the sense
77 /// that they will not themselves change, but which DocumentClass is
78 /// associated with a Buffer can change, as modules are loaded and
79 /// unloaded, for example.
81 class TextClass : protected ProtectCopy {
84 virtual ~TextClass() {}
85 ///////////////////////////////////////////////////////////////////
87 ///////////////////////////////////////////////////////////////////
88 // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
89 // std::list<Layout *>. This will lead to problems. The reason is
90 // that DocumentClass objects are generally created by copying a
91 // LayoutFile, which serves as a base for the DocumentClass. If the
92 // LayoutList is a container of pointers, then every DocumentClass
93 // that derives from a given LayoutFile (e.g., article) will SHARE
94 // a basic set of layouts. So if one Buffer were to modify a layout
95 // (say, Standard), that would modify that layout for EVERY Buffer
96 // that was based upon the same DocumentClass.
98 // NOTE: Layout pointers are directly assigned to paragraphs so a
99 // container that does not invalidate these pointers after insertion
101 /// The individual paragraph layouts comprising the document class
102 typedef std::list<Layout> LayoutList;
103 /// The inset layouts available to this class
104 typedef std::map<docstring, InsetLayout> InsetLayouts;
106 typedef LayoutList::const_iterator const_iterator;
108 ///////////////////////////////////////////////////////////////////
110 ///////////////////////////////////////////////////////////////////
112 const_iterator begin() const { return layoutlist_.begin(); }
114 const_iterator end() const { return layoutlist_.end(); }
117 ///////////////////////////////////////////////////////////////////
119 ///////////////////////////////////////////////////////////////////
121 Layout const & defaultLayout() const;
123 docstring const & defaultLayoutName() const;
125 bool isDefaultLayout(Layout const &) const;
127 bool isPlainLayout(Layout const &) const;
128 /// returns a special layout for use when we don't really want one,
129 /// e.g., in table cells
130 Layout const & plainLayout() const
131 { return operator[](plain_layout_); }
132 /// the name of the plain layout
133 docstring const & plainLayoutName() const
134 { return plain_layout_; }
135 /// Enumerate the paragraph styles.
136 size_t layoutCount() const { return layoutlist_.size(); }
138 bool hasLayout(docstring const & name) const;
140 bool hasInsetLayout(docstring const & name) const;
142 Layout const & operator[](docstring const & vname) const;
143 /// Inset layouts of this doc class
144 InsetLayouts const & insetLayouts() const { return insetlayoutlist_; }
146 ///////////////////////////////////////////////////////////////////
148 ///////////////////////////////////////////////////////////////////
149 /// Enum used with TextClass::read
151 BASECLASS, //>This is a base class, i.e., top-level layout file
152 MERGE, //>This is a file included in a layout file
153 MODULE, //>This is a layout module
154 CITE_ENGINE, //>This is a cite engine
155 VALIDATION //>We're just validating
157 /// return values for read()
165 /// Performs the read of the layout file.
166 /// \return true on success.
167 // FIXME Should return ReturnValues....
168 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
170 ReturnValues read(std::string const & str, ReadType rt = MODULE);
172 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
173 /// validates the layout information passed in str
174 static ReturnValues validate(std::string const & str);
175 /// \return the conversion of \param str to the latest layout format
176 /// compatible with the lyx format.
177 static std::string convert(std::string const & str);
179 ///////////////////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////////////////
182 /// Sees to it the textclass structure has been loaded
183 /// This function will search for $classname.layout in default directories
184 /// and an optional path, but if path points to a file, it will be loaded
186 bool load(std::string const & path = std::string()) const;
187 /// Has this layout file been loaded yet?
188 /// Overridden by DocumentClass
189 virtual bool loaded() const { return loaded_; }
191 ///////////////////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////////////////
195 std::string const & name() const { return name_; }
197 std::string const & path() const { return path_; }
199 std::string const & category() const { return category_; }
201 std::string const & description() const { return description_; }
203 std::string const & latexname() const { return latexname_; }
205 std::string const & prerequisites(std::string const & sep = "\n\t") const;
206 /// Can be LaTeX, DocBook, etc.
207 OutputType outputType() const { return outputType_; }
208 /// Can be latex, docbook ... (the name of a format)
209 std::string outputFormat() const { return outputFormat_; }
210 /// Return the non-localised names for the toc types.
211 std::map<std::string, docstring> const &
212 outlinerNames() const { return outliner_names_; }
215 /// Protect construction
218 Layout & operator[](docstring const & name);
219 /** Create an new, very basic layout for this textclass. This is used for
220 the Plain Layout common to all TextClass objects and also, in
221 DocumentClass, for the creation of new layouts `on the fly' when
222 previously unknown layouts are encountered.
223 \param unknown Set to true if this layout is used to represent an
226 Layout createBasicLayout(docstring const & name, bool unknown = false) const;
228 ///////////////////////////////////////////////////////////////////
229 // non-const iterators
230 ///////////////////////////////////////////////////////////////////
232 typedef LayoutList::iterator iterator;
234 iterator begin() { return layoutlist_.begin(); }
236 iterator end() { return layoutlist_.end(); }
238 ///////////////////////////////////////////////////////////////////
240 ///////////////////////////////////////////////////////////////////
241 /// Paragraph styles used in this layout
242 /// This variable is mutable because unknown layouts can be added
243 /// to const textclass.
244 mutable LayoutList layoutlist_;
247 /// Layout file path (empty for system layout files)
250 std::string category_;
251 /// document class name
252 std::string latexname_;
253 /// document class description
254 std::string description_;
255 /// available types of float, eg. figure, algorithm.
256 mutable FloatList floatlist_;
257 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
258 mutable Counters counters_;
259 /// Has this layout file been loaded yet?
260 mutable bool loaded_;
261 /// Is the TeX class available?
262 bool tex_class_avail_;
263 /// document class prerequisites
264 mutable std::string prerequisites_;
265 /// The possible cite engine types
266 std::string opt_enginetype_;
267 /// The cite framework (bibtex, biblatex)
268 std::string citeframework_;
270 std::string opt_fontsize_;
272 std::string opt_pagestyle_;
273 /// Specific class options
274 std::string options_;
276 std::string pagestyle_;
278 std::string class_header_;
280 docstring defaultlayout_;
281 /// name of plain layout
282 static const docstring plain_layout_;
283 /// preamble text to support layout styles
285 /// same, but for HTML output
286 /// this is output as is to the header
287 docstring htmlpreamble_;
288 /// same, but specifically for CSS information
289 docstring htmlstyles_;
290 /// the paragraph style to use for TOCs, Bibliography, etc
291 mutable docstring html_toc_section_;
292 /// latex packages loaded by document class.
293 std::set<std::string> provides_;
294 /// latex packages requested by document class.
295 std::set<std::string> requires_;
297 std::map<std::string, std::string> package_options_;
298 /// default modules wanted by document class
299 LayoutModuleList default_modules_;
300 /// modules provided by document class
301 LayoutModuleList provided_modules_;
302 /// modules excluded by document class
303 LayoutModuleList excluded_modules_;
305 unsigned int columns_;
308 /// header depth to have numbering
310 /// header depth to appear in table of contents
312 /// Can be LaTeX, DocBook, etc.
313 OutputType outputType_;
314 /// Can be latex, docbook ... (the name of a format)
315 std::string outputFormat_;
316 /** Base font. The paragraph and layout fonts are resolved against
317 this font. This has to be fully instantiated. Attributes
318 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
321 FontInfo defaultfont_;
322 /// Text that dictates how wide the left margin is on the screen
323 docstring leftmargin_;
324 /// Text that dictates how wide the right margin is on the screen
325 docstring rightmargin_;
326 /// The type of command used to produce a title
327 TitleLatexType titletype_;
328 /// The name of the title command
329 std::string titlename_;
330 /// Input layouts available to this layout
331 InsetLayouts insetlayoutlist_;
332 /// The minimal TocLevel of sectioning layouts
334 /// The maximal TocLevel of sectioning layouts
336 /// Citation formatting information
337 std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
339 std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
340 /// The default BibTeX bibliography style file
341 std::map<std::string, std::string> cite_default_biblio_style_;
342 /// Citation command aliases
343 std::map<std::string, std::string> cite_command_aliases_;
344 /// The maximum number of citations before "et al."
345 size_t maxcitenames_;
346 /// Whether full author lists are supported
347 bool cite_full_author_list_;
348 /// The possible citation styles
349 std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
351 std::map<std::string, docstring> outliner_names_;
353 ///////////////////////////////////////////////////////////////////
354 // helper routines for reading layout files
355 ///////////////////////////////////////////////////////////////////
357 bool deleteLayout(docstring const &);
359 bool deleteInsetLayout(docstring const &);
361 bool convertLayoutFormat(support::FileName const &, ReadType);
362 /// Reads the layout file without running layout2layout.
363 ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
364 /// \return true for success.
365 bool readStyle(Lexer &, Layout &) const;
367 void readOutputType(Lexer &);
369 void readTitleType(Lexer &);
371 void readMaxCounter(Lexer &);
373 void readClassOptions(Lexer &);
375 void readCharStyle(Lexer &, std::string const &);
377 bool readFloat(Lexer &);
379 bool readCiteEngine(Lexer &);
381 int readCiteEngineType(Lexer &) const;
383 bool readCiteFormat(Lexer &);
385 bool readOutlinerName(Lexer &);
389 /// A DocumentClass represents the layout information associated with a
390 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
393 /// In that regard, DocumentClass objects are "dynamic". But this is really
394 /// an illusion, since DocumentClass objects are not (currently) changed
395 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
396 /// discarded---actually, it will be kept around if something on the cut
397 /// stack needs it---and a new one is created from scratch.
398 class DocumentClass : public TextClass {
401 virtual ~DocumentClass() {}
403 ///////////////////////////////////////////////////////////////////
405 ///////////////////////////////////////////////////////////////////
406 /// \return true if there is a Layout with latexname lay
407 bool hasLaTeXLayout(std::string const & lay) const;
408 /// A DocumentClass nevers count as loaded, since it is dynamic
409 virtual bool loaded() const { return false; }
410 /// \return the layout object of an inset given by name. If the name
411 /// is not found as such, the part after the ':' is stripped off, and
412 /// searched again. In this way, an error fallback can be provided:
413 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
414 /// will invoke the layout object defined by name = 'CharStyle'.
415 /// If that doesn't work either, an empty object returns (shouldn't
416 /// happen). -- Idea JMarc, comment MV
417 InsetLayout const & insetLayout(docstring const & name) const;
418 /// a plain inset layout for use as a default
419 static InsetLayout const & plainInsetLayout();
420 /// add a new layout \c name if it does not exist in layoutlist_
421 /// \return whether we had to add one.
422 bool addLayoutIfNeeded(docstring const & name) const;
423 /// Forced layouts in layout file syntax
424 std::string forcedLayouts() const;
426 ///////////////////////////////////////////////////////////////////
428 ///////////////////////////////////////////////////////////////////
429 /// the list of floats defined in the document class
430 FloatList const & floats() const { return floatlist_; }
432 Counters & counters() const { return counters_; }
434 std::string const & opt_enginetype() const { return opt_enginetype_; }
436 std::string const & citeFramework() const { return citeframework_; }
438 std::string const & opt_fontsize() const { return opt_fontsize_; }
440 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
442 std::string const & options() const { return options_; }
444 std::string const & class_header() const { return class_header_; }
446 std::string const & pagestyle() const { return pagestyle_; }
448 docstring const & preamble() const { return preamble_; }
450 docstring const & htmlpreamble() const { return htmlpreamble_; }
452 docstring const & htmlstyles() const { return htmlstyles_; }
453 /// Looks for the layout of "highest level", other than Part (or other
454 /// layouts with a negative toc number), for use in constructing TOCs and
455 /// similar information.
456 Layout const & getTOCLayout() const;
457 /// the paragraph style to use for TOCs, Bibliography, etc
458 /// we will attempt to calculate this if it was not given
459 Layout const & htmlTOCLayout() const;
460 /// is this feature already provided by the class?
461 bool provides(std::string const & p) const;
462 /// features required by the class?
463 std::set<std::string> const & requires() const { return requires_; }
464 /// package options to write to LaTeX file
465 std::map<std::string, std::string> const & packageOptions() const
466 { return package_options_; }
468 unsigned int columns() const { return columns_; }
470 PageSides sides() const { return sides_; }
472 int secnumdepth() const { return secnumdepth_; }
474 int tocdepth() const { return tocdepth_; }
476 FontInfo const & defaultfont() const { return defaultfont_; }
477 /// Text that dictates how wide the left margin is on the screen
478 docstring const & leftmargin() const { return leftmargin_; }
479 /// Text that dictates how wide the right margin is on the screen
480 docstring const & rightmargin() const { return rightmargin_; }
481 /// The type of command used to produce a title
482 TitleLatexType titletype() const { return titletype_; }
483 /// The name of the title command
484 std::string const & titlename() const { return titlename_; }
486 int size() const { return layoutlist_.size(); }
487 /// The minimal TocLevel of sectioning layouts
488 int min_toclevel() const { return min_toclevel_; }
489 /// The maximal TocLevel of sectioning layouts
490 int max_toclevel() const { return max_toclevel_; }
491 /// returns true if the class has a ToC structure
492 bool hasTocLevels() const;
494 std::string const getCiteFormat(CiteEngineType const & type,
495 std::string const & entry, bool const punct = true,
496 std::string const & fallback = "") const;
498 std::string const & getCiteMacro(CiteEngineType const & type,
499 std::string const & macro) const;
501 std::vector<std::string> const citeCommands(CiteEngineType const &) const;
503 std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
505 std::map<std::string, std::string> const & defaultBiblioStyle() const
506 { return cite_default_biblio_style_; }
508 std::map<std::string, std::string> const & citeCommandAliases() const
509 { return cite_command_aliases_; }
510 /// The maximum number of citations before "et al."
511 size_t max_citenames() const { return maxcitenames_; }
513 bool const & fullAuthorList() const { return cite_full_author_list_; }
515 /// Constructs a DocumentClass based upon a LayoutFile.
516 DocumentClass(LayoutFile const & tc);
517 /// Needed in tex2lyx
520 /// The only way to make a DocumentClass is to call this function.
521 friend DocumentClassPtr
522 getDocumentClass(LayoutFile const &, LayoutModuleList const &,
523 LayoutModuleList const &,
528 /// The only way to make a DocumentClass is to call this function.
529 /// The shared_ptr is needed because DocumentClass objects can be kept
530 /// in memory long after their associated Buffer is destroyed, mostly
532 DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
533 LayoutModuleList const & modlist,
534 LayoutModuleList const & celist,
535 bool const clone = false);
537 /// convert page sides option to text 1 or 2
538 std::ostream & operator<<(std::ostream & os, PageSides p);
540 /// current format of layout files
541 extern int const LAYOUT_FORMAT;
542 /// layout format for the current lyx file format (usually equal to
544 extern int const LYXFILE_LAYOUT_FORMAT;