]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
0c893240880ad0a74a6b4fa045e6fa3de774e214
[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         if (cmdname == o.cmdname && contents == o.contents && options == o.options) return true;
70         return false;
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         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 const 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, bool)
182         : p_( p.getCmdName(), p.getContents(), p.getOptions() )
183 {
184 }
185
186
187 void InsetCommand::setParams(InsetCommandParams const & p )
188 {
189         p_.setCmdName( p.getCmdName() );
190         p_.setContents( p.getContents() );
191         p_.setOptions( p.getOptions() );
192 }
193
194
195 int InsetCommand::latex(Buffer const *, ostream & os,
196                         bool /*fragile*/, bool/*fs*/) const
197 {
198         os << getCommand();
199         return 0;
200 }
201
202
203 int InsetCommand::ascii(Buffer const *, ostream &, int) const
204 {
205         return 0;
206 }
207
208
209 int InsetCommand::linuxdoc(Buffer const *, ostream &) const
210 {
211         return 0;
212 }
213
214
215 int InsetCommand::docBook(Buffer const *, ostream &) const
216 {
217         return 0;
218 }