]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
The speed patch: redraw only rows that have changed
[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 #include "insetcommandparams.h"
14
15 #include "debug.h"
16 #include "lyxlex.h"
17
18
19 using std::string;
20 using std::endl;
21 using std::ostream;
22
23
24 InsetCommandParams::InsetCommandParams()
25 {}
26
27
28 InsetCommandParams::InsetCommandParams(string const & n,
29                                         string const & c,
30                                         string const & o,
31                                         string const & s)
32         : cmdname(n), contents(c), options(o), sec_options(s),
33         preview_(false)
34 {}
35
36
37 void InsetCommandParams::scanCommand(string const & cmd)
38 {
39         string tcmdname, toptions, tsecoptions, tcontents;
40
41         if (cmd.empty()) return;
42
43         enum { WS, CMDNAME, OPTION, SECOPTION, CONTENT } state = WS;
44
45         // Used to handle things like \command[foo[bar]]{foo{bar}}
46         int nestdepth = 0;
47
48         for (string::size_type i = 0; i < cmd.length(); ++i) {
49                 char const c = cmd[i];
50                 if ((state == CMDNAME && c == ' ') ||
51                     (state == CMDNAME && c == '[') ||
52                     (state == CMDNAME && c == '{')) {
53                         state = WS;
54                 }
55                 if ((state == OPTION  && c == ']') ||
56                     (state == SECOPTION  && c == ']') ||
57                     (state == CONTENT && c == '}')) {
58                         if (nestdepth == 0) {
59                                 state = WS;
60                         } else {
61                                 --nestdepth;
62                         }
63                 }
64                 if ((state == OPTION  && c == '[') ||
65                     (state == SECOPTION  && c == '[') ||
66                     (state == CONTENT && c == '{')) {
67                         ++nestdepth;
68                 }
69                 switch (state) {
70                 case CMDNAME:   tcmdname += c; break;
71                 case OPTION:    toptions += c; break;
72                 case SECOPTION: tsecoptions += c; break;
73                 case CONTENT:   tcontents += c; break;
74                 case WS: {
75                         char const b = i? cmd[i-1]: 0;
76                         if (c == '\\') {
77                                 state = CMDNAME;
78                         } else if (c == '[' && b != ']') {
79                                 state = OPTION;
80                                 nestdepth = 0; // Just to be sure
81                         } else if (c == '[' && b == ']') {
82                                 state = SECOPTION;
83                                 nestdepth = 0; // Just to be sure
84                         } else if (c == '{') {
85                                 state = CONTENT;
86                                 nestdepth = 0; // Just to be sure
87                         }
88                         break;
89                 }
90                 }
91         }
92
93         // Don't mess with this.
94         if (!tcmdname.empty())  setCmdName(tcmdname);
95         if (!toptions.empty())  setOptions(toptions);
96         if (!tsecoptions.empty())  setSecOptions(tsecoptions);
97         if (!tcontents.empty()) setContents(tcontents);
98
99         if (lyxerr.debugging(Debug::PARSER))
100                 lyxerr << "Command <" <<  cmd
101                        << "> == <" << getCommand()
102                        << "> == <" << getCmdName()
103                        << '|' << getContents()
104                        << '|' << getOptions()
105                        << '|' << getSecOptions() << '>' << endl;
106 }
107
108
109 void InsetCommandParams::read(LyXLex & lex)
110 {
111         string token;
112
113         if (lex.eatLine()) {
114                 token = lex.getString();
115                 scanCommand(token);
116         } else {
117                 lex.printError("InsetCommand: Parse error: `$$Token'");
118         }
119
120         while (lex.isOK()) {
121                 lex.next();
122                 token = lex.getString();
123                 if (token == "\\end_inset")
124                         break;
125                 if (token == "preview") {
126                         lex.next();
127                         preview_ = lex.getBool();
128                 }
129         }
130         if (token != "\\end_inset") {
131                 lex.printError("Missing \\end_inset at this point. "
132                                "Read: `$$Token'");
133         }
134 }
135
136
137 void InsetCommandParams::write(ostream & os) const
138 {
139         os << "LatexCommand " << getCommand() << "\n";
140 }
141
142
143 string const InsetCommandParams::getCommand() const
144 {
145         string s;
146         if (!getCmdName().empty()) s += '\\' + getCmdName();
147         if (!getOptions().empty()) s += '[' + getOptions() + ']';
148         if (!getSecOptions().empty()) {
149                 // for cases like \command[][sec_option]{arg}
150                 if (getOptions().empty()) s += "[]";
151         s += '[' + getSecOptions() + ']';
152         }
153         s += '{' + getContents() + '}';
154         return s;
155 }
156
157
158 bool operator==(InsetCommandParams const & o1,
159                 InsetCommandParams const & o2)
160 {
161         return o1.getCmdName() == o2.getCmdName()
162                 && o1.getContents() == o2.getContents()
163                 && o1.getOptions() == o2.getOptions()
164                 && o1.getSecOptions() == o2.getSecOptions()
165                 && o1.preview() == o2.preview();
166 }
167
168
169 bool operator!=(InsetCommandParams const & o1,
170                 InsetCommandParams const & o2)
171 {
172         return !(o1 == o2);
173 }