]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
Now generate previews when loading the buffer for insets in insets.
[lyx.git] / src / insets / insetcommandparams.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 2002-2002 The LyX Team.
7  *
8  * ====================================================== */
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "insetcommandparams.h"
16 #include "lyxlex.h"
17 #include "debug.h"
18
19 #include "support/LOstream.h"
20
21 using std::ostream;
22 using std::endl;
23
24
25 InsetCommandParams::InsetCommandParams()
26 {}
27
28
29 InsetCommandParams::InsetCommandParams(string const & n,
30                                         string const & c,
31                                         string const & o)
32         : cmdname(n), contents(c), options(o)
33 {}
34
35
36 string const InsetCommandParams::getAsString() const
37 {
38         return cmdname + "|++|" + contents + "|++|" + options;
39 }
40
41
42 void InsetCommandParams::setFromString(string const & b)
43 {
44         string::size_type idx = b.find("|++|");
45         if (idx == string::npos) {
46                 cmdname = b;
47                 contents = "";
48                 options = "";
49                 return;
50         }
51
52         cmdname = b.substr(0, idx);
53         string tmp = b.substr(idx+4);
54
55         idx = tmp.find("|++|");
56         if (idx == string::npos) {
57                 contents = tmp;
58                 options = "";
59         } else {
60                 contents  = tmp.substr(0, idx);
61                 options = tmp.substr(idx+4);
62         }
63 }
64
65
66 void InsetCommandParams::scanCommand(string const & cmd)
67 {
68         string tcmdname, toptions, tcontents;
69
70         if (cmd.empty()) return;
71
72         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
73
74         // Used to handle things like \command[foo[bar]]{foo{bar}}
75         int nestdepth = 0;
76
77         for (string::size_type i = 0; i < cmd.length(); ++i) {
78                 char c = cmd[i];
79                 if ((state == CMDNAME && c == ' ') ||
80                     (state == CMDNAME && c == '[') ||
81                     (state == CMDNAME && c == '{')) {
82                         state = WS;
83                 }
84                 if ((state == OPTION  && c == ']') ||
85                     (state == CONTENT && c == '}')) {
86                         if (nestdepth == 0) {
87                                 state = WS;
88                         } else {
89                                 --nestdepth;
90                         }
91                 }
92                 if ((state == OPTION  && c == '[') ||
93                     (state == CONTENT && c == '{')) {
94                         ++nestdepth;
95                 }
96                 switch (state) {
97                 case CMDNAME:   tcmdname += c; break;
98                 case OPTION:    toptions += c; break;
99                 case CONTENT:   tcontents += c; break;
100                 case WS:
101                         if (c == '\\') {
102                                 state = CMDNAME;
103                         } else if (c == '[') {
104                                 state = OPTION;
105                                 nestdepth = 0; // Just to be sure
106                         } else if (c == '{') {
107                                 state = CONTENT;
108                                 nestdepth = 0; // Just to be sure
109                         }
110                         break;
111                 }
112         }
113
114         // Don't mess with this.
115         if (!tcmdname.empty())  setCmdName(tcmdname);
116         if (!toptions.empty())  setOptions(toptions);
117         if (!tcontents.empty()) setContents(tcontents);
118
119         if (lyxerr.debugging(Debug::PARSER))
120                 lyxerr << "Command <" <<  cmd
121                        << "> == <" << getCommand()
122                        << "> == <" << getCmdName()
123                        << '|' << getContents()
124                        << '|' << getOptions() << '>' << endl;
125 }
126
127
128 void InsetCommandParams::read(LyXLex & lex)
129 {
130         string token;
131
132         if (lex.eatLine()) {
133                 token = lex.getString();
134                 scanCommand(token);
135         } else {
136                 lex.printError("InsetCommand: Parse error: `$$Token'");
137         }
138
139         while (lex.isOK()) {
140                 lex.nextToken();
141                 token = lex.getString();
142                 if (token == "\\end_inset")
143                         break;
144         }
145         if (token != "\\end_inset") {
146                 lex.printError("Missing \\end_inset at this point. "
147                                "Read: `$$Token'");
148         }
149 }
150
151
152 void InsetCommandParams::write(ostream & os) const
153 {
154         os << "LatexCommand " << getCommand() << "\n";
155 }
156
157
158 string const InsetCommandParams::getCommand() const
159 {
160         string s;
161         if (!getCmdName().empty()) s += "\\"+getCmdName();
162         if (!getOptions().empty()) s += "["+getOptions()+']';
163         s += "{"+getContents()+'}';
164         return s;
165 }
166
167
168 bool operator==(InsetCommandParams const & o1,
169                 InsetCommandParams const & o2)
170 {
171         return o1.getCmdName() == o2.getCmdName()
172                 && o1.getContents() == o2.getContents()
173                 && o1.getOptions() == o2.getOptions();
174 }
175
176
177 bool operator!=(InsetCommandParams const & o1,
178                 InsetCommandParams const & o2)
179 {
180         return !(o1 == o2);
181 }