]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
small changes to ButtonController usage
[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-2000 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
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 InsetCommandParams::getAsString() const
37 {
38         string b(cmdname);
39         b += "|++|" + options + "|++|" + contents;
40         return b;
41 }
42
43
44 void InsetCommandParams::setFromString( string const & b )
45 {
46         string::size_type idx = b.find("|++|");
47         if( idx == string::npos ) {
48                 cmdname = "";
49                 options  = "";
50                 contents = "";
51                 return;
52         }
53
54         cmdname = b.substr(0, idx);
55         string tmp = b.substr(idx+4);
56
57         idx = tmp.find("|++|");
58         if( idx == string::npos ) {
59                 options = tmp;
60         } else {
61                 options  = tmp.substr(0, idx);
62                 contents = tmp.substr(idx+4);
63         }
64 }
65
66
67 bool InsetCommandParams::operator==(InsetCommandParams const & o) const
68 {
69         if( cmdname  != o.cmdname )  return false;
70         if( contents != o.contents ) return false;
71         if( options  != o.options )  return false;
72         return true;
73 }
74
75
76 bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
77 {
78         if( cmdname  != o.cmdname )  return true;
79         if( contents != o.contents ) return true;
80         if( options  != o.options )  return true;
81         return false;
82 }
83
84
85 void InsetCommandParams::scanCommand(string const & cmd)
86 {
87         string tcmdname, toptions, tcontents;
88
89         if (cmd.empty()) return;
90
91         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
92         
93         // Used to handle things like \command[foo[bar]]{foo{bar}}
94         int nestdepth = 0;
95
96         for (string::size_type i = 0; i < cmd.length(); ++i) {
97                 char c = cmd[i];
98                 if ((state == CMDNAME && c == ' ') ||
99                     (state == CMDNAME && c == '[') ||
100                     (state == CMDNAME && c == '{')) {
101                         state = WS;
102                 }
103                 if ((state == OPTION  && c == ']') ||
104                     (state == CONTENT && c == '}')) {
105                         if (nestdepth == 0) {
106                                 state = WS;
107                         } else {
108                                 --nestdepth;
109                         }
110                 }
111                 if ((state == OPTION  && c == '[') ||
112                     (state == CONTENT && c == '{')) {
113                         ++nestdepth;
114                 }
115                 switch (state) {
116                 case CMDNAME:   tcmdname += c; break;
117                 case OPTION:    toptions += c; break;
118                 case CONTENT:   tcontents += c; break;
119                 case WS:
120                         if (c == '\\') {
121                                 state = CMDNAME;
122                         } else if (c == '[') {
123                                 state = OPTION;
124                                 nestdepth = 0; // Just to be sure
125                         } else if (c == '{') {
126                                 state = CONTENT;
127                                 nestdepth = 0; // Just to be sure
128                         }
129                         break;
130                 }
131         }
132
133         // Don't mess with this.
134         if (!tcmdname.empty())  setCmdName( tcmdname );
135         if (!toptions.empty())  setOptions( toptions );
136         if (!tcontents.empty()) setContents( tcontents ); 
137
138         if (lyxerr.debugging(Debug::PARSER))
139                 lyxerr << "Command <" <<  cmd
140                        << "> == <" << getCommand()
141                        << "> == <" << getCmdName()
142                        << '|' << getContents()
143                        << '|' << getOptions() << '>' << endl;
144 }
145
146
147 // This function will not be necessary when lyx3
148 void InsetCommandParams::Read(LyXLex & lex)
149 {    
150         string token;
151
152         if (lex.EatLine()) {
153                 token = lex.GetString();
154                 scanCommand(token);
155         } else
156                 lex.printError("InsetCommand: Parse error: `$$Token'");
157         while (lex.IsOK()) {
158                 lex.nextToken();
159                 token = lex.GetString();
160                 if (token == "\\end_inset")
161                         break;
162         }
163         if (token != "\\end_inset") {
164                 lex.printError("Missing \\end_inset at this point. "
165                                "Read: `$$Token'");
166         }
167 }
168
169
170 void InsetCommandParams::Write(ostream & os) const
171 {
172         os << "LatexCommand " << getCommand() << "\n";
173 }
174
175
176 string InsetCommandParams::getCommand() const
177 {       
178         string s;
179         if (!getCmdName().empty()) s += "\\"+getCmdName();
180         if (!getOptions().empty()) s += "["+getOptions()+']';
181         s += "{"+getContents()+'}';
182         return s;
183 }
184
185
186 InsetCommand::InsetCommand( InsetCommandParams const & p )
187         : p_( p.getCmdName(), p.getContents(), p.getOptions() )
188 {}
189
190
191 void InsetCommand::setParams(InsetCommandParams const & p )
192 {
193         p_.setCmdName( p.getCmdName() );
194         p_.setContents( p.getContents() );
195         p_.setOptions( p.getOptions() );
196 }
197
198
199 int InsetCommand::Latex(Buffer const *, ostream & os,
200                         bool /*fragile*/, bool/*fs*/) const
201 {
202         os << getCommand();
203         return 0;
204 }
205
206
207 int InsetCommand::Ascii(Buffer const *, ostream &) const
208 {
209         return 0;
210 }
211
212
213 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
214 {
215         return 0;
216 }
217
218
219 int InsetCommand::DocBook(Buffer const *, ostream &) const
220 {
221         return 0;
222 }