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