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