]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
fix #832
[lyx.git] / src / insets / insetcommandparams.C
1 /**
2  * \file insetcommandparams.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "insetcommandparams.h"
15 #include "lyxlex.h"
16 #include "debug.h"
17
18 #include "support/LOstream.h"
19
20 using std::ostream;
21 using std::endl;
22
23
24 InsetCommandParams::InsetCommandParams()
25 {}
26
27
28 InsetCommandParams::InsetCommandParams(string const & n,
29                                         string const & c,
30                                         string const & o)
31         : cmdname(n), contents(c), options(o), preview_(false)
32 {}
33
34
35 void InsetCommandParams::scanCommand(string const & cmd)
36 {
37         string tcmdname, toptions, tcontents;
38
39         if (cmd.empty()) return;
40
41         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
42
43         // Used to handle things like \command[foo[bar]]{foo{bar}}
44         int nestdepth = 0;
45
46         for (string::size_type i = 0; i < cmd.length(); ++i) {
47                 char c = cmd[i];
48                 if ((state == CMDNAME && c == ' ') ||
49                     (state == CMDNAME && c == '[') ||
50                     (state == CMDNAME && c == '{')) {
51                         state = WS;
52                 }
53                 if ((state == OPTION  && c == ']') ||
54                     (state == CONTENT && c == '}')) {
55                         if (nestdepth == 0) {
56                                 state = WS;
57                         } else {
58                                 --nestdepth;
59                         }
60                 }
61                 if ((state == OPTION  && c == '[') ||
62                     (state == CONTENT && c == '{')) {
63                         ++nestdepth;
64                 }
65                 switch (state) {
66                 case CMDNAME:   tcmdname += c; break;
67                 case OPTION:    toptions += c; break;
68                 case CONTENT:   tcontents += c; break;
69                 case WS:
70                         if (c == '\\') {
71                                 state = CMDNAME;
72                         } else if (c == '[') {
73                                 state = OPTION;
74                                 nestdepth = 0; // Just to be sure
75                         } else if (c == '{') {
76                                 state = CONTENT;
77                                 nestdepth = 0; // Just to be sure
78                         }
79                         break;
80                 }
81         }
82
83         // Don't mess with this.
84         if (!tcmdname.empty())  setCmdName(tcmdname);
85         if (!toptions.empty())  setOptions(toptions);
86         if (!tcontents.empty()) setContents(tcontents);
87
88         if (lyxerr.debugging(Debug::PARSER))
89                 lyxerr << "Command <" <<  cmd
90                        << "> == <" << getCommand()
91                        << "> == <" << getCmdName()
92                        << '|' << getContents()
93                        << '|' << getOptions() << '>' << endl;
94 }
95
96
97 void InsetCommandParams::read(LyXLex & lex)
98 {
99         string token;
100
101         if (lex.eatLine()) {
102                 token = lex.getString();
103                 scanCommand(token);
104         } else {
105                 lex.printError("InsetCommand: Parse error: `$$Token'");
106         }
107
108         while (lex.isOK()) {
109                 lex.next();
110                 token = lex.getString();
111                 if (token == "\\end_inset")
112                         break;
113                 if (token == "preview") {
114                         lex.next();
115                         preview_ = lex.getBool();
116                 }
117         }
118         if (token != "\\end_inset") {
119                 lex.printError("Missing \\end_inset at this point. "
120                                "Read: `$$Token'");
121         }
122 }
123
124
125 void InsetCommandParams::write(ostream & os) const
126 {
127         os << "LatexCommand " << getCommand() << "\n";
128 }
129
130
131 string const InsetCommandParams::getCommand() const
132 {
133         string s;
134         if (!getCmdName().empty()) s += '\\' + getCmdName();
135         if (!getOptions().empty()) s += '[' + getOptions() + ']';
136         s += '{' + getContents() + '}';
137         return s;
138 }
139
140
141 bool operator==(InsetCommandParams const & o1,
142                 InsetCommandParams const & o2)
143 {
144         return o1.getCmdName() == o2.getCmdName()
145                 && o1.getContents() == o2.getContents()
146                 && o1.getOptions() == o2.getOptions()
147                 && o1.preview() == o2.preview();
148 }
149
150
151 bool operator!=(InsetCommandParams const & o1,
152                 InsetCommandParams const & o2)
153 {
154         return !(o1 == o2);
155 }