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