Windows installer: update Gnuplot
[maxima.git] / interfaces / emacs / imaxima / imath.el
blob700660767859e6c0725eab94b13e6d79b6dc3621
1 ;;;; imath.el --- Imath minor mode
3 ;; Copyright (C) 2004, 2008 Yasuaki Honda
4 ;; Copyright (C) 2007 bohumir for the {eps eps} formula support
6 ;; Author: Yasuaki Honda (yhonda@mac.com)
7 ;; Created: 7 Nov 2004
8 ;; Version: 1.0b
9 ;; Keywords: maxima
11 ;; $Id: imath.el,v 1.5 2009-02-22 09:18:27 yasu-honda Exp $
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2 of
16 ;; the License, or (at your option) any later version.
18 ;; This program is distributed in the hope that it will be
19 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
20 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 ;; PURPOSE. See the GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public
24 ;; License along with this program; if not, write to the Free
25 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 ;; MA 02111-1307 USA
28 ;; How to install imath minor mode
29 ;; 1. You need to install imaxima properly. (./configure;make;make install)
30 ;; 2. Put (autoload 'imath-mode "imath" "Interactive Math minor mode." t)
31 ;; in the .emacs.el file.
33 ;; How to use imath minor node
34 ;; 1. In a buffer, M-x imath-mode to enable imath minor mode.
35 ;; 2. C-c [ to insert a template maxima form.
36 ;; C-c ] to insert a template latex form.
37 ;; 3. Type any Maxima command input in the maxima template form:
38 ;; Ex: {maxima diff(f(x),x) maxima}
39 ;; Type any LaTeX command in the latex template form:
40 ;; Ex: {latex \ifracd{d}{d\*x}\*f\left(x\right) latex}
41 ;; 4. Type C-c ! to obtain an image of the formula.
42 ;; 5. If you want to edit the maxima or latex command, place cursor
43 ;; right after the image and type C-c & to obtain the original
44 ;; maxima or latex form.
45 ;; 6. You can save the buffer, however discarding all then formula
46 ;; images. Formulas are saved as maxima forms or latex forms or
47 ;; both. On visiting the saved file, you can restore all the
48 ;; images by enabling the imath minor mode and then type C-c $.
49 ;; 7. Imath mode can be automatically set by having the first line
50 ;; of the file like this:
51 ;; ;; -*- mode: imath -*-
52 ;; 8. Imath mode enables you to export the buffer contents to
53 ;; HTML document. You can do so by typing M-x imath-to-html .
54 ;; A buffer is created to convert imath text to HTML. C-x C-s
55 ;; saves the buffer to the file whose name is the same as the
56 ;; original file but file extension being .html in the same
57 ;; folder as the original file.
58 ;; A folder is created to store all the formula images. They
59 ;; are referenced from the HTML document by using <IMG> tag.
61 (require 'cl)
62 (require 'imaxima)
64 (if (featurep 'xemacs)
65 (require 'atomic-extents))
67 (define-minor-mode imath-mode
68 "Toggle MathEdit mode.
69 With no argument, this command toggles the mode.
70 Non-null prefix argument turns on the mode.
71 Null prefix argument turns off the mode.
73 The imath minor mode provides a small set of functions to aid
74 insert math formulas into plain text.
76 A math formula is written using a Maxima form whose syntax is
77 {maxima a formula maxima} where a formula is a string which can
78 be accepted as Maxima command input. C-c [ inserts a template
79 for a maxima form.
81 The other way to write a math formula is to use LaTeX form
82 whose syntax is {latex a formula latex} where a formula is
83 a valid LaTeX commands. C-c ] inserts a template for a latex
84 form.
86 Example maxima and latex forms are:
87 {maxima integrate(f(x),x) maxima}
88 {maxima sum(a[n],n,0,i) maxima}
89 {latex \\int {f\\left(x\\right)}{\\;dx} latex}
90 {latex \\sum_{n=0}^{i}{a_{n}} latex}
92 Version 1.0 beta and later supports inline graph. You can use
93 the following six maxima commands in maxima forms:
94 wxplot2d(), wxplot3d(), wxdraw2d(), wxdraw3d(), wximplicit_plot(),
95 wxcontour_plot().
97 Example maxima forms for inline graph are:
98 {maxima wxplot2d(sin(5*x)/x,[x,-5,5]) maxima}
99 {maxima wxplot3d(sin(x)*y,[x,-5,5],[y,-5,5]) maxima}
100 {maxima wxdraw2d(implicit(x^2+y^3=5,x,-5,5,y,-5,5)) maxima}
102 Removing the prefix \"wx\" from the commands above, you obtain
103 command names that are described in Maxima manual. The documentation
104 applies to the \"wx\" version of the commands.
106 Version 1.0 beta and later supports inline EPS image. You can
107 insert arbitrary image files in EPS format using the following
108 syntax:
109 {eps filename eps}
111 An example is:
112 {eps /home/george/image1.eps eps}
114 Assuming the cursor position is right after a form or in the
115 middle, C-c ! transforms the form into the formula image using
116 the Imaxima functionality.
118 If the resulting image is not what you want, you may want to edit
119 the formula again. To do this, place the cursor right after the
120 image and C-c &. Then the image is removed and original form
121 appears at the position.
123 When saving the buffer into a file, images are
124 discarded. However, maxima forms and their correspoding latex
125 forms are kept there in the text. If the text is loaded again
126 into Emacs and imath minor mode is enabled, you can type C-c $ to
127 restore all the images for the forms in the buffer.
129 Imath mode enables you to export the buffer contents to
130 HTML document. You can do so by typing M-x imath-to-html .
131 A buffer is created to convert imath text to HTML. C-x C-s
132 saves the buffer to the file whose name is the same as the
133 original file but file extension being .html in the same
134 folder as the original file.
136 A folder is created to store all the formula images. They
137 are referenced from the HTML document by using <IMG> tag.
140 ;; The initial value.
142 ;; The indicator for the mode line.
143 " iMath"
144 ;; The minor mode bindings.
145 '(("\C-c[" . compose-maxima-formula)
146 ("\C-c]" . compose-latex-formula)
147 ("\C-c!" . form-to-image)
148 ("\C-c$" . buffer-formula-to-image)
149 ("\C-c&" . remove-maxima-formula-image)
151 ;; body
152 ;; Imath requires *imaxima* running.
153 (progn
154 (let ((cur-buf (current-buffer)))
155 (save-current-buffer
156 (imaxima))
157 (switch-to-buffer cur-buf))))
159 ;;; Continuation is used between maxima-to-image function and
160 ;;; get-image-from-imaxima. The value is either nil or a list of
161 ;;; buffer, pos1, and pos2, where pos1 and pos2 are the beginning and
162 ;;; end of current maxima formula.
163 ;; (func buffer pos1 pos2)
165 (defvar continuation nil)
166 (defvar maxima-start "{maxima ")
167 (defvar maxima-end " maxima}")
168 (defvar latex-start "{latex ")
169 (defvar latex-end " latex}")
170 (defvar eps-start "{eps ")
171 (defvar eps-end " eps}")
173 (defun compose-maxima-formula ()
174 "Insert maxima form template at the current position."
175 (interactive "")
176 (insert "{maxima maxima}")
177 (backward-char 8))
179 (defun compose-latex-formula ()
180 "Insert maxima form template at the current position."
181 (interactive "")
182 (insert "{latex latex}")
183 (backward-char 7))
185 (defun* find-formula (ftype)
186 (let (start-symbol end-symbol tmpresult)
187 (cond ((eql ftype 'maxima)
188 (setq start-symbol maxima-start
189 end-symbol maxima-end))
190 ((eql ftype 'latex)
191 (setq start-symbol latex-start
192 end-symbol latex-end))
193 ((eql ftype 'eps)
194 (setq start-symbol eps-start
195 end-symbol eps-end))
196 ((eql ftype 'both)
197 (save-excursion
198 (multiple-value-bind (la-start la-end la-type)
199 (find-formula 'latex)
200 (if (not (and la-start la-end la-type))
201 (return-from find-formula nil)
202 (goto-char (1- la-start))
203 (if (not (string= (buffer-substring (point) (1+ (point)))
204 "&"))
205 (return-from find-formula nil))
206 (multiple-value-bind (mx-start mx-end mx-type)
207 (find-formula 'maxima)
208 (if (not (and mx-start mx-end mx-type))
209 (return-from find-formula nil))
210 (return-from find-formula
211 (values mx-start la-end 'both)))))))
212 ((eql ftype 'any)
213 (cond ((setq tmpresult (find-formula 'both))
214 (return-from find-formula tmpresult))
215 ((setq tmpresult (find-formula 'latex))
216 (return-from find-formula tmpresult))
217 ((setq tmpresult (find-formula 'maxima))
218 (return-from find-formula tmpresult))
219 ((setq tmpresult (find-formula 'eps))
220 (return-from find-formula tmpresult))
222 (return-from find-formula nil))))
223 (t (return-from find-formula nil)))
224 (save-excursion
225 (let (begin end (curpos (point)))
226 (setq begin (search-backward start-symbol (point-min) t))
227 (setq end (search-forward end-symbol (point-max) t))
228 (if (and (numberp begin) (numberp end) ;; {start-symbol ... end-symbol} is found.
229 (or (and (> end curpos) (> curpos begin)) ;; {start-symbol ... curpos ... end-symbol}
230 (= curpos end))) ;; {start-symbol ... end-symbol}}curpos
231 (values begin end ftype)
232 nil)))))
234 (defun remove-maxima-formula-image (arg)
235 (interactive "P")
236 (save-excursion
237 (multiple-value-bind (begin end ftype)
238 (find-formula 'any)
239 (when (and begin end ftype)
240 (if (featurep 'xemacs)
241 (let ((ext (extent-at begin)))
242 (if ext (delete-extent ext)))
243 (remove-text-properties begin end '(display) (current-buffer)))
244 (if (eql ftype 'both)
245 (multiple-value-bind (la-begin la-end la-ftype)
246 (find-formula 'latex)
247 (when (and (not arg) la-begin la-end la-ftype)
248 ;; remove & between the maxima formula and latex formula
249 ;; if that is the case.
250 (if (eql ftype 'both) (decf la-begin))
251 (delete-region la-begin la-end))))))))
253 (defun form-to-image ()
254 "Convert any form to image based on form types"
255 (interactive "")
256 (multiple-value-bind (start end ftype)
257 (find-formula 'any)
258 (if (and start end ftype)
259 (cond ((eql ftype 'maxima)
260 (maxima-to-image))
261 ((eql ftype 'eps)
262 (get-image-from-imaxima 'eps))
263 ((or (eql ftype 'both)
264 (eql ftype 'latex))
265 (get-image-from-imaxima 'latex)
272 (defun* maxima-to-image (&aux maxcmd)
273 "Transform a maxima form which is placed just before current point or
274 is surrounding the current point into a formula image."
275 (interactive "")
276 (save-excursion
277 (multiple-value-bind (begin end)
278 (find-formula 'maxima)
279 (let (curpos (point))
280 (when (and begin end)
281 (setq maxcmd (buffer-substring (+ begin (length maxima-start))
282 (- end (length maxima-end))))
283 (kill-new (buffer-substring (+ begin (length maxima-start))
284 (- end (length maxima-end))))
285 (save-excursion
286 (set-buffer (if imaxima-use-maxima-mode-flag
287 "*maxima*"
288 "*imaxima*"))
289 (yank)
290 (insert ";")
291 (comint-send-input))))
292 (if continuation
293 (debug-imaxima-filter "continuation exits"))
294 (setq continuation (list (if (string-match "[ ]*wx\\(plot2d\\|plot3d\\|draw\\|draw2d\\|draw3d\\|implicit_plot\\|contour_plot\\).*" maxcmd)
295 #'get-inline-graph
296 #'get-image-from-imaxima-1)
297 (current-buffer) begin end)))))
299 (defun* maxima-to-image-all (&aux maxcmd)
300 "Transform a maxima form which is placed just before current point or
301 is surrounding the current point into a formula image."
302 (interactive "")
303 (if (not (re-search-forward "maxima}[^&]" (point-max) 0))
304 (progn
305 (setq continuation nil)
306 (return-from maxima-to-image-all nil)))
307 (backward-char)
308 (multiple-value-bind (begin end)
309 (find-formula 'maxima)
310 (when (and begin end)
311 (setq maxcmd (buffer-substring (+ begin (length maxima-start))
312 (- end (length maxima-end))))
313 (kill-new (buffer-substring (+ begin (length maxima-start))
314 (- end (length maxima-end))))
315 (save-excursion
316 (set-buffer (if imaxima-use-maxima-mode-flag
317 "*maxima*"
318 "*imaxima*"))
319 (yank)
320 (insert ";")
321 (comint-send-input))
322 (if continuation
323 (debug-imaxima-filter "continuation exits"))
324 (setq continuation (list (if (string-match "[ ]*wx\\(plot2d\\|plot3d\\|draw\\|draw2d\\|draw3d\\|implicit_plot\\|contour_plot\\).*" maxcmd)
325 #'(lambda (arg)
326 (let ((cont continuation))
327 (funcall #'get-inline-graph arg)
328 (save-current-buffer
329 (set-buffer (nth 1 cont))
330 (maxima-to-image-all))))
331 #'(lambda (arg)
332 (let ((cont continuation))
333 (funcall #'get-image-from-imaxima-1 arg)
334 (save-current-buffer
335 (set-buffer (nth 1 cont))
336 (maxima-to-image-all)))))
337 (current-buffer) begin end)))))
340 (defun* get-image-from-imaxima (eps-or-latex)
341 "Converts a both form or a latex form into a formula image when
342 placed right after the form."
343 (interactive "")
344 (let (la-start la-end la-ftype entire-start entire-end entire-ftype
345 entire-string latex-formula-or-epsfile)
347 (multiple-value-bind (la-start la-end la-ftype)
348 (find-formula eps-or-latex)
349 (when (and la-start la-end la-ftype)
350 (setq entire-string (buffer-substring la-start la-end))
351 (cond ((eql eps-or-latex 'latex)
352 (setq latex-formula-or-epsfile (substring entire-string
353 (length latex-start)
354 (- (length entire-string)
355 (length latex-end))))
357 ((eql eps-or-latex 'eps)
358 (setq latex-formula-or-epsfile (substring entire-string
359 (length eps-start)
360 (- (length entire-string)
361 (length eps-end))))
364 (multiple-value-bind (entire-start entire-end entire-ftype)
365 (find-formula 'any)
366 (when (and entire-start entire-end entire-ftype)
367 (if (featurep 'xemacs)
368 (progn
369 (let ((ext (extent-at entire-start)))
370 (if ext
371 (return-from get-image-from-imaxima nil)))
373 (cond ((eql eps-or-latex 'latex)
374 (let ((ext (extent-at 0 (imaxima-make-image latex-formula-or-epsfile eps-or-latex t))))
375 (if ext
376 (insert-extent ext entire-start entire-end t (current-buffer))))
378 ((eql eps-or-latex 'eps)
379 (let ((ext (extent-at 0 (imaxima-make-image latex-formula-or-epsfile eps-or-latex t))))
380 (if ext
381 (insert-extent ext entire-start entire-end t (current-buffer))))
386 ;; FSF Emacs
387 (add-text-properties entire-start entire-end
388 (list 'display
389 (get-text-property 1
390 'display
391 (imaxima-make-image latex-formula-or-epsfile eps-or-latex t)))
392 (current-buffer)))))))))
394 (defun get-image-from-imaxima-1 (latex-string)
395 (setq latex-string (copy-sequence latex-string))
396 (if (featurep 'xemacs)
397 (let ((ext (extent-at 0 latex-string)))
398 (if ext (set-extent-property ext 'duplicable nil))))
399 (while (string-match "^([\\%a-zA-Z0-9]+)" latex-string)
400 (setq latex-string (replace-match "" t t latex-string)))
401 (if continuation
402 (let ((maxima-string (save-current-buffer
403 (set-buffer (nth 1 continuation))
404 (buffer-substring (nth 2 continuation)
405 (nth 3 continuation))))
406 pos)
407 (save-current-buffer
408 (set-buffer (nth 1 continuation))
409 (setq pos (copy-marker (nth 2 continuation) t))
410 (delete-region (nth 2 continuation) (nth 3 continuation))
411 (setq continuation nil)
412 (let ((str-to-insert (concat maxima-string "&{latex " latex-string " latex}")))
413 (if (featurep 'xemacs)
414 (let ((ext (extent-at 0 (imaxima-make-image latex-string 'latex t))))
415 (if ext
416 (insert-extent ext 0 (length str-to-insert) t str-to-insert)))
417 (add-text-properties 0 (length str-to-insert)
418 (list 'display
419 (get-text-property 1
420 'display
421 (imaxima-make-image latex-string 'latex t)))
422 str-to-insert))
423 (goto-char pos)
424 (insert str-to-insert))))))
426 (defun get-inline-graph (latex-string)
427 (setq latex-string (copy-sequence latex-string))
428 (if (featurep 'xemacs)
429 (let ((ext (extent-at 0 latex-string)))
430 (if ext (set-extent-property ext 'duplicable nil))))
431 (when (string-match "^([\\%a-zA-Z0-9]+)" latex-string)
432 (setq latex-string (replace-match "" t t latex-string)))
433 (if continuation
434 (let ((maxima-string (save-current-buffer
435 (set-buffer (nth 1 continuation))
436 (buffer-substring (nth 2 continuation)
437 (nth 3 continuation)))))
438 (save-current-buffer
439 (set-buffer (nth 1 continuation))
440 (delete-region (nth 2 continuation) (nth 3 continuation))
441 (setq continuation nil)
442 (let ((str-to-insert maxima-string))
443 (if (featurep 'xemacs)
444 (let ((ext (extent-at 0 (imaxima-make-image latex-string 'latex t))))
445 (if ext
446 (insert-extent ext 0 (length str-to-insert) t str-to-insert)))
447 (add-text-properties 0 (length str-to-insert)
448 (list 'display
449 (get-text-property 1 'display latex-string))
450 str-to-insert))
451 (insert str-to-insert))))))
454 (defun buffer-formula-to-image ()
455 "Transform all the latex forms and maxima&latex forms into
456 formula images."
457 (interactive "")
458 (beginning-of-buffer)
459 (let ((msg "Converting latex"))
460 (message msg)
461 (while (search-forward latex-end nil 1)
462 (get-image-from-imaxima 'latex)
463 (setq msg (concat msg "."))
464 (message msg))
465 (message (concat msg ".done.")))
466 (beginning-of-buffer)
467 (let ((msg "Converting eps"))
468 (while (search-forward eps-end nil 1)
469 (get-image-from-imaxima 'eps)
470 (setq msg (concat msg "."))
471 (message msg))
472 (message (concat msg ".done.")))
473 (beginning-of-buffer)
474 (let ((msg "Converting maxima..."))
475 (message msg)
476 (maxima-to-image-all)
477 (message (concat msg "done.")))
478 (set-buffer-modified-p nil))
481 (provide 'imath)
483 ;;; imath.el ends here