]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Fix small bug in reading \set_color in lyxrc
[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 InsetCommand::InsetCommand()
26 {}
27
28
29 InsetCommand::InsetCommand(string const & cmd, string const & arg, 
30                            string const & opt)
31         : cmdname(cmd), options(opt), contents(arg)
32 {}
33
34
35 // In lyxf3 this will be just LaTeX
36 void InsetCommand::Write(Buffer const *, ostream & os) const
37 {
38         os << "LatexCommand " << getCommand() << "\n";
39 }
40
41
42 void InsetCommand::scanCommand(string const & cmd)
43 {
44         string tcmdname, toptions, tcontents;
45
46         if (cmd.empty()) return;
47
48         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
49         
50         // Used to handle things like \command[foo[bar]]{foo{bar}}
51         int nestdepth = 0;
52
53         for (string::size_type i = 0; i < cmd.length(); ++i) {
54                 char c = cmd[i];
55                 if ((state == CMDNAME && c == ' ') ||
56                     (state == CMDNAME && c == '[') ||
57                     (state == CMDNAME && c == '{')) {
58                         state = WS;
59                 }
60                 if ((state == OPTION  && c == ']') ||
61                     (state == CONTENT && c == '}')) {
62                         if (nestdepth == 0) {
63                                 state = WS;
64                         } else {
65                                 --nestdepth;
66                         }
67                 }
68                 if ((state == OPTION  && c == '[') ||
69                     (state == CONTENT && c == '{')) {
70                         ++nestdepth;
71                 }
72                 switch (state) {
73                 case CMDNAME:   tcmdname += c; break;
74                 case OPTION:    toptions += c; break;
75                 case CONTENT:   tcontents += c; break;
76                 case WS:
77                         if (c == '\\') {
78                                 state = CMDNAME;
79                         } else if (c == '[') {
80                                 state = OPTION;
81                                 nestdepth = 0; // Just to be sure
82                         } else if (c == '{') {
83                                 state = CONTENT;
84                                 nestdepth = 0; // Just to be sure
85                         }
86                         break;
87                 }
88         }
89
90         // Don't mess with this.
91         if (!tcmdname.empty()) cmdname = tcmdname;
92         if (!toptions.empty()) options = toptions;
93         if (!tcontents.empty()) setContents(tcontents); 
94                         // setContents is overloaded in InsetInclude
95
96         if (lyxerr.debugging(Debug::PARSER))
97                 lyxerr << "Command <" <<  cmd
98                        << "> == <" << getCommand()
99                        << "> == <" << getCmdName()
100                        << '|' << getContents()
101                        << '|' << getOptions() << '>' << endl;
102 }
103
104
105 // This function will not be necessary when lyx3
106 void InsetCommand::Read(Buffer const *, LyXLex & lex)
107 {    
108         string token;
109
110         if (lex.EatLine()) {
111                 token = lex.GetString();
112                 scanCommand(token);
113         } else
114                 lex.printError("InsetCommand: Parse error: `$$Token'");
115         while (lex.IsOK()) {
116                 lex.nextToken();
117                 token = lex.GetString();
118                 if (token == "\\end_inset")
119                         break;
120         }
121         if (token != "\\end_inset") {
122                 lex.printError("Missing \\end_inset at this point. "
123                                "Read: `$$Token'");
124         }
125 }
126
127
128 int InsetCommand::Latex(Buffer const *, ostream & os,
129                         bool /*fragile*/, bool/*fs*/) const
130 {
131         os << getCommand();
132         return 0;
133 }
134
135
136 int InsetCommand::Ascii(Buffer const *, ostream &) const
137 {
138         return 0;
139 }
140
141
142 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
143 {
144         return 0;
145 }
146
147
148 int InsetCommand::DocBook(Buffer const *, ostream &) const
149 {
150         return 0;
151 }
152
153
154 Inset * InsetCommand::Clone() const
155 {
156         return new InsetCommand(cmdname, contents, options);
157 }
158
159
160 string InsetCommand::getCommand() const
161 {       
162         string s;
163         if (!cmdname.empty()) s += "\\"+cmdname;
164         if (!options.empty()) s += "["+options+']';
165         s += "{"+contents+'}';
166         return s;
167 }