]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Another clean-up patch from Angus
[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 ) return;
48
49         cmdname = b.substr(0, idx);
50         string tmp = b.substr(idx+4);
51
52         idx = tmp.find("|++|");
53         if( idx == string::npos ) {
54                 options = tmp;
55         } else {
56                 options  = tmp.substr(0, idx);
57                 contents = tmp.substr(idx+4);
58         }
59 }
60
61
62 bool InsetCommandParams::operator==(InsetCommandParams const & o) const
63 {
64         if( cmdname  != o.cmdname )  return false;
65         if( contents != o.contents ) return false;
66         if( options  != o.options )  return false;
67         return true;
68 }
69
70
71 bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
72 {
73         if( cmdname  != o.cmdname )  return true;
74         if( contents != o.contents ) return true;
75         if( options  != o.options )  return true;
76         return false;
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         while (lex.IsOK()) {
153                 lex.nextToken();
154                 token = lex.GetString();
155                 if (token == "\\end_inset")
156                         break;
157         }
158         if (token != "\\end_inset") {
159                 lex.printError("Missing \\end_inset at this point. "
160                                "Read: `$$Token'");
161         }
162 }
163
164
165 void InsetCommandParams::Write(ostream & os) const
166 {
167         os << "LatexCommand " << getCommand() << "\n";
168 }
169
170
171 string InsetCommandParams::getCommand() const
172 {       
173         string s;
174         if (!getCmdName().empty()) s += "\\"+getCmdName();
175         if (!getOptions().empty()) s += "["+getOptions()+']';
176         s += "{"+getContents()+'}';
177         return s;
178 }
179
180
181 InsetCommand::InsetCommand( InsetCommandParams const & p )
182         : p_( p.getCmdName(), p.getContents(), p.getOptions() )
183 {}
184
185
186 void InsetCommand::setParams(InsetCommandParams const & p )
187 {
188         p_.setCmdName( p.getCmdName() );
189         p_.setContents( p.getContents() );
190         p_.setOptions( p.getOptions() );
191 }
192
193
194 int InsetCommand::Latex(Buffer const *, ostream & os,
195                         bool /*fragile*/, bool/*fs*/) const
196 {
197         os << getCommand();
198         return 0;
199 }
200
201
202 int InsetCommand::Ascii(Buffer const *, ostream &) const
203 {
204         return 0;
205 }
206
207
208 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
209 {
210         return 0;
211 }
212
213
214 int InsetCommand::DocBook(Buffer const *, ostream &) const
215 {
216         return 0;
217 }