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