]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
use stream-like syntax for LaTeX output
[lyx.git] / src / mathed / formula.C
1 /*
2 *  File:        formula.C
3 *  Purpose:     Implementation of formula inset
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *  Description: Allows the edition of math paragraphs inside Lyx.
7 *
8 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
9 *
10 *  Version: 0.4, Lyx project.
11 *
12 *   You are free to use and modify this code under the terms of
13 *   the GNU General Public Licence version 2 or later.
14 */
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include <config.h>
21 #include <fstream>
22
23 #include "formula.h"
24 #include "commandtags.h"
25 #include "math_cursor.h"
26 #include "math_parser.h"
27 #include "math_charinset.h"
28 #include "lyx_main.h"
29 #include "BufferView.h"
30 #include "gettext.h"
31 #include "debug.h"
32 #include "lyx_gui_misc.h"
33 #include "support/LOstream.h"
34 #include "support/LAssert.h"
35 #include "support/lyxlib.h"
36 #include "support/syscall.h"
37 #include "support/lstrings.h"
38 #include "LyXView.h"
39 #include "Painter.h"
40 #include "lyxrc.h"
41 #include "math_matrixinset.h"
42 #include "mathed/support.h"
43
44 using std::ostream;
45 using std::ifstream;
46 using std::istream;
47 using std::pair;
48 using std::endl;
49 using std::vector;
50
51
52
53 InsetFormula::InsetFormula()
54         : par_(MathAtom(new MathMatrixInset))
55 {}
56
57
58 InsetFormula::InsetFormula(MathInsetTypes t)
59         : par_(MathAtom(new MathMatrixInset(t)))
60 {}
61
62
63 InsetFormula::InsetFormula(string const & s) 
64 {
65         if (s.size()) {
66                 bool res = mathed_parse_normal(par_, s);
67
68                 if (!res)
69                         res = mathed_parse_normal(par_, "$" + s + "$");
70
71                 if (!res) {
72                         lyxerr << "cannot interpret '" << s << "' as math\n";
73                         par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
74                 }
75         }
76         metrics();
77 }
78
79
80 Inset * InsetFormula::clone(Buffer const &, bool) const
81 {
82         return new InsetFormula(*this);
83 }
84
85
86 void InsetFormula::write(Buffer const * buf, ostream & os) const
87 {
88         os << "Formula ";
89         latex(buf, os, false, false);
90 }
91
92
93 int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
94         const
95 {
96         MathWriteInfo wi(buf, os, fragil);
97         par_->write(wi);
98         return 1;
99 }
100
101
102 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
103 {
104         MathWriteInfo wi(buf, os, false);
105         par_->write(wi);
106         return 1;
107 }
108
109
110 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
111 {
112         return ascii(buf, os, 0);
113 }
114
115
116 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
117 {
118         return ascii(buf, os, 0);
119 }
120
121
122 void InsetFormula::read(Buffer const *, LyXLex & lex)
123 {
124         mathed_parse_normal(par_, lex);
125         metrics();
126 }
127
128
129 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
130                         int y, float & xx, bool) const
131 {
132         int x = int(xx) - 1;
133         y -= 2;
134
135         MathInset::workwidth = bv->workWidth();
136         Painter & pain = bv->painter();
137
138         metrics(bv, &font);
139         int w = par_->width();
140         int h = par_->height();
141         int a = par_->ascent();
142         pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
143
144         if (mathcursor && mathcursor->formula() == this) {
145                 mathcursor->drawSelection(pain);
146                 pain.rectangle(x, y - a, w, h, LColor::mathframe);
147         }
148
149         par_->draw(pain, x, y);
150         xx += par_->width();
151
152         setCursorVisible(false);
153 }
154
155
156 vector<string> const InsetFormula::getLabelList() const
157 {
158         return mat()->getLabelList();
159 }
160
161
162 UpdatableInset::RESULT
163 InsetFormula::localDispatch(BufferView * bv, kb_action action,
164          string const & arg)
165 {
166         RESULT result = DISPATCHED;
167
168         switch (action) {
169
170                 case LFUN_BREAKLINE: 
171                         bv->lockedInsetStoreUndo(Undo::INSERT);
172                         mathcursor->breakLine();
173                         mathcursor->normalize();
174                         updateLocal(bv, true);
175                         break;
176
177                 case LFUN_MATH_NUMBER:
178                 {
179                         //lyxerr << "toggling all numbers\n";
180                         if (display()) {
181                                 bv->lockedInsetStoreUndo(Undo::INSERT);
182                                 bool old = mat()->numberedType();
183                                 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
184                                         mat()->numbered(row, !old);
185                                 bv->owner()->message(old ? _("No number") : _("Number"));
186                                 updateLocal(bv, true);
187                         }
188                         break;
189                 }
190
191                 case LFUN_MATH_NONUMBER:
192                 {
193                         //lyxerr << "toggling line number\n";
194                         if (display()) {
195                                 bv->lockedInsetStoreUndo(Undo::INSERT);
196                                 MathCursor::row_type row = mathcursor->row();
197                                 bool old = mat()->numbered(row);
198                                 bv->owner()->message(old ? _("No number") : _("Number"));
199                                 mat()->numbered(row, !old);
200                                 updateLocal(bv, true);
201                         }
202                         break;
203                 }
204
205                 case LFUN_INSERT_LABEL:
206                 {
207                         bv->lockedInsetStoreUndo(Undo::INSERT);
208
209                         MathCursor::row_type row = mathcursor->row();
210                         string old_label = mat()->label(row);
211                         string new_label = arg;
212
213                         if (new_label.empty()) {
214                                 string const default_label =
215                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
216                                 pair<bool, string> const res = old_label.empty()
217                                         ? askForText(_("Enter new label to insert:"), default_label)
218                                         : askForText(_("Enter label:"), old_label);
219                                 
220                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
221                                 if (!res.first)
222                                         break;
223                                 new_label = frontStrip(strip(res.second));
224                         }
225
226                         //if (new_label == old_label)
227                         //      break;  // Nothing to do
228
229                         if (!new_label.empty()) {
230                                 lyxerr << "setting label to '" << new_label << "'\n";
231                                 mat()->numbered(row, true);
232                         }
233
234                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
235                                 bv->redraw();
236
237                         mat()->label(row, new_label);
238
239                         updateLocal(bv, true);
240                         break;
241                 }
242
243                 case LFUN_MATH_EXTERN:
244                         bv->lockedInsetStoreUndo(Undo::EDIT);
245                         handleExtern(arg, bv);
246                         updateLocal(bv, true);
247                         break;
248
249                 case LFUN_MATH_MUTATE:
250                 {
251                         bv->lockedInsetStoreUndo(Undo::EDIT);
252                         int x;
253                         int y;
254                         mathcursor->getPos(x, y);
255                         mat()->mutate(arg);
256                         mathcursor->setPos(x, y);
257                         mathcursor->normalize();
258                         updateLocal(bv, true);
259                         break;
260                 }
261
262                 case LFUN_MATH_DISPLAY:
263                 {
264                         int x;
265                         int y;
266                         mathcursor->getPos(x, y);
267                         if (mat()->getType() == LM_OT_SIMPLE)
268                                 mat()->mutate(LM_OT_EQUATION);
269                         else
270                                 mat()->mutate(LM_OT_SIMPLE);
271                         mathcursor->setPos(x, y);
272                         mathcursor->normalize();
273                         updateLocal(bv, true);
274                         break;
275                 }
276                 
277                 case LFUN_PASTESELECTION:
278                 {
279                         string const clip = bv->getClipboard();
280                 if (!clip.empty())
281                                 mathed_parse_normal(par_, clip);
282                         break;
283                 }
284
285                 case LFUN_MATH_COLUMN_INSERT:
286                 {
287                         if (mat()->getType() == LM_OT_ALIGN)
288                                 mat()->mutate(LM_OT_ALIGNAT);
289                         mat()->addCol(mat()->ncols());
290                         mathcursor->normalize();
291                         updateLocal(bv, true);
292                 }
293
294                 default:
295                         result = InsetFormulaBase::localDispatch(bv, action, arg);
296         }
297
298         return result;
299 }
300
301
302 void InsetFormula::handleExtern(const string & arg, BufferView * bv)
303 {
304         // where are we?
305         MathArray & ar = mathcursor->cursor().cell();
306
307         // find position of last '=' in the array for handleExtern
308         MathArray::size_type pos = ar.size();
309         for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
310                 if ((*it)->getChar() == '=')
311                         pos = it - ar.begin();
312
313         // delete everything behind this position
314         ar.erase(pos, ar.size());
315
316         // create normalized expression
317         //string outfile = lyx::tempName("maple.out");
318         string outfile = "/tmp/lyx2" + arg + ".out";
319         ostringstream os;
320         ar.writeNormal(os); 
321         string code = os.str().c_str();
322
323         // run external sript
324         string script = "lyx2" + arg + " '" + code + "' " + outfile;
325         lyxerr << "calling: " << script << endl;
326         Systemcalls cmd(Systemcalls::System, script, 0);
327
328         // append a '='
329         ar.push_back(MathAtom(new MathCharInset('=')));
330         
331         // append result
332         ifstream is(outfile.c_str());
333         mathed_parse_cell(ar, is);
334         mathcursor->end();
335
336         // re-compute inset dimension
337         metrics(bv);
338 }
339
340
341 bool InsetFormula::display() const
342 {
343         return mat()->getType() != LM_OT_SIMPLE;
344 }
345
346
347 MathMatrixInset const * InsetFormula::mat() const
348 {
349         lyx::Assert(par_->asMatrixInset());
350         return par_->asMatrixInset();
351 }
352
353
354 MathMatrixInset * InsetFormula::mat()
355 {
356         lyx::Assert(par_->asMatrixInset());
357         return par_->asMatrixInset();
358 }
359
360
361 Inset::Code InsetFormula::lyxCode() const
362 {
363         return Inset::MATH_CODE;
364 }
365
366
367 void InsetFormula::validate(LaTeXFeatures & features) const
368 {
369         par_->validate(features);
370 }
371
372
373 bool InsetFormula::insetAllowed(Inset::Code code) const
374 {
375         return code == Inset::LABEL_CODE && display(); 
376 }
377
378
379 int InsetFormula::ascent(BufferView *, LyXFont const &) const
380 {
381         return par_->ascent() + 2;
382 }
383
384
385 int InsetFormula::descent(BufferView *, LyXFont const &) const
386 {
387         return par_->descent() - 2;
388 }
389
390
391 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
392 {
393         metrics(bv, &font);
394         return par_->width();
395 }
396
397
398 MathInsetTypes InsetFormula::getType() const
399 {
400         return mat()->getType();
401 }