Texinfo compatibility fix
[maxima.git] / interfaces / emacs / imaxima / imath.el
blobec559b49017ab8a7b0ca11ddbe947e79978a98dc
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 ;; How to install imath minor mode
28 ;; 1. You need to install imaxima properly. (./configure;make;make install)
29 ;; 2. Put (autoload 'imath-mode "imath" "Interactive Math minor mode." t)
30 ;; in the .emacs.el file.
32 ;; How to use imath minor node
33 ;; 1. In a buffer, M-x imath-mode to enable imath minor mode.
34 ;; 2. C-c [ to insert a template maxima form.
35 ;; C-c ] to insert a template latex form.
36 ;; 3. Type any Maxima command input in the maxima template form:
37 ;; Ex: {maxima diff(f(x),x) maxima}
38 ;; Type any LaTeX command in the latex template form:
39 ;; Ex: {latex \ifracd{d}{d\*x}\*f\left(x\right) latex}
40 ;; 4. Type C-c ! to obtain an image of the formula.
41 ;; 5. If you want to edit the maxima or latex command, place cursor
42 ;; right after the image and type C-c & to obtain the original
43 ;; maxima or latex form.
44 ;; 6. You can save the buffer, however discarding all then formula
45 ;; images. Formulas are saved as maxima forms or latex forms or
46 ;; both. On visiting the saved file, you can restore all the
47 ;; images by enabling the imath minor mode and then type C-c $.
48 ;; 7. Imath mode can be automatically set by having the first line
49 ;; of the file like this:
50 ;; ;; -*- mode: imath -*-
51 ;; 8. Imath mode enables you to export the buffer contents to
52 ;; HTML document. You can do so by typing M-x imath-to-html .
53 ;; A buffer is created to convert imath text to HTML. C-x C-s
54 ;; saves the buffer to the file whose name is the same as the
55 ;; original file but file extension being .html in the same
56 ;; folder as the original file.
57 ;; A folder is created to store all the formula images. They
58 ;; are referenced from the HTML document by using <IMG> tag.
60 (require 'cl-lib)
61 (require 'imaxima)
63 (if (featurep 'xemacs)
64 (require 'atomic-extents))
66 (define-minor-mode imath-mode
67 "Toggle MathEdit mode.
68 With no argument, this command toggles the mode.
69 Non-null prefix argument turns on the mode.
70 Null prefix argument turns off the mode.
72 The imath minor mode provides a small set of functions to aid
73 insert math formulas into plain text.
75 A math formula is written using a Maxima form whose syntax is
76 {maxima a formula maxima} where a formula is a string which can
77 be accepted as Maxima command input. C-c [ inserts a template
78 for a maxima form.
80 The other way to write a math formula is to use LaTeX form
81 whose syntax is {latex a formula latex} where a formula is
82 a valid LaTeX commands. C-c ] inserts a template for a latex
83 form.
85 Example maxima and latex forms are:
86 {maxima integrate(f(x),x) maxima}
87 {maxima sum(a[n],n,0,i) maxima}
88 {latex \\int {f\\left(x\\right)}{\\;dx} latex}
89 {latex \\sum_{n=0}^{i}{a_{n}} latex}
91 Version 1.0 beta and later supports inline graph. You can use
92 the following six maxima commands in maxima forms:
93 wxplot2d(), wxplot3d(), wxdraw2d(), wxdraw3d(), wximplicit_plot(),
94 wxcontour_plot().
96 Example maxima forms for inline graph are:
97 {maxima wxplot2d(sin(5*x)/x,[x,-5,5]) maxima}
98 {maxima wxplot3d(sin(x)*y,[x,-5,5],[y,-5,5]) maxima}
99 {maxima wxdraw2d(implicit(x^2+y^3=5,x,-5,5,y,-5,5)) maxima}
101 Removing the prefix \"wx\" from the commands above, you obtain
102 command names that are described in Maxima manual. The documentation
103 applies to the \"wx\" version of the commands.
105 Version 1.0 beta and later supports inline EPS image. You can
106 insert arbitrary image files in EPS format using the following
107 syntax:
108 {eps filename eps}
110 An example is:
111 {eps /home/george/image1.eps eps}
113 Assuming the cursor position is right after a form or in the
114 middle, C-c ! transforms the form into the formula image using
115 the Imaxima functionality.
117 If the resulting image is not what you want, you may want to edit
118 the formula again. To do this, place the cursor right after the
119 image and C-c &. Then the image is removed and original form
120 appears at the position.
122 When saving the buffer into a file, images are
123 discarded. However, maxima forms and their corresponding latex
124 forms are kept there in the text. If the text is loaded again
125 into Emacs and imath minor mode is enabled, you can type C-c $ to
126 restore all the images for the forms in the buffer.
128 Imath mode enables you to export the buffer contents to
129 HTML document. You can do so by typing M-x imath-to-html .
130 A buffer is created to convert imath text to HTML. C-x C-s
131 saves the buffer to the file whose name is the same as the
132 original file but file extension being .html in the same
133 folder as the original file.
135 A folder is created to store all the formula images. They
136 are referenced from the HTML document by using <IMG> tag.
139 ;; The initial value.
141 ;; The indicator for the mode line.
142 " iMath"
143 ;; The minor mode bindings.
144 '(("\C-c[" . compose-maxima-formula)
145 ("\C-c]" . compose-latex-formula)
146 ("\C-c!" . form-to-image)
147 ("\C-c$" . buffer-formula-to-image)
148 ("\C-c&" . remove-maxima-formula-image)
150 ;; body
151 ;; Imath requires *imaxima* running.
152 (progn
153 (let ((cur-buf (current-buffer)))
154 (save-current-buffer
155 (imaxima))
156 (switch-to-buffer cur-buf))))
158 (defvar maxima-start "{maxima ")
159 (defvar maxima-end " maxima}")
160 (defvar latex-start "{latex ")
161 (defvar latex-end " latex}")
162 (defvar eps-start "{eps ")
163 (defvar eps-end " eps}")
165 (defun compose-maxima-formula ()
166 "Insert maxima form template at the current position."
167 (interactive "")
168 (insert "{maxima maxima}")
169 (backward-char 8))
171 (defun compose-latex-formula ()
172 "Insert maxima form template at the current position."
173 (interactive "")
174 (insert "{latex latex}")
175 (backward-char 7))
177 (cl-defun find-formula (ftype)
178 (let (start-symbol end-symbol tmpresult)
179 (cond ((eql ftype 'maxima)
180 (setq start-symbol maxima-start
181 end-symbol maxima-end))
182 ((eql ftype 'latex)
183 (setq start-symbol latex-start
184 end-symbol latex-end))
185 ((eql ftype 'eps)
186 (setq start-symbol eps-start
187 end-symbol eps-end))
188 ((eql ftype 'both)
189 (save-excursion
190 (cl-multiple-value-bind (la-start la-end la-type)
191 (find-formula 'latex)
192 (if (not (and la-start la-end la-type))
193 (cl-return-from find-formula nil)
194 (goto-char (1- la-start))
195 (if (not (string= (buffer-substring (point) (1+ (point)))
196 "&"))
197 (cl-return-from find-formula nil))
198 (cl-multiple-value-bind (mx-start mx-end mx-type)
199 (find-formula 'maxima)
200 (if (not (and mx-start mx-end mx-type))
201 (cl-return-from find-formula nil))
202 (cl-return-from find-formula
203 (cl-values mx-start la-end 'both)))))))
204 ((eql ftype 'any)
205 (cond ((setq tmpresult (find-formula 'both))
206 (cl-return-from find-formula tmpresult))
207 ((setq tmpresult (find-formula 'latex))
208 (cl-return-from find-formula tmpresult))
209 ((setq tmpresult (find-formula 'maxima))
210 (cl-return-from find-formula tmpresult))
211 ((setq tmpresult (find-formula 'eps))
212 (cl-return-from find-formula tmpresult))
214 (cl-return-from find-formula nil))))
215 (t (cl-return-from find-formula nil)))
216 (save-excursion
217 (let (begin end (curpos (point)))
218 (setq begin (search-backward start-symbol (point-min) t))
219 (setq end (search-forward end-symbol (point-max) t))
220 (if (and (numberp begin) (numberp end) ;; {start-symbol ... end-symbol} is found.
221 (or (and (> end curpos) (> curpos begin)) ;; {start-symbol ... curpos ... end-symbol}
222 (= curpos end))) ;; {start-symbol ... end-symbol}}curpos
223 (cl-values begin end ftype)
224 nil)))))
226 (defun remove-maxima-formula-image (arg)
227 (interactive "P")
228 (save-excursion
229 (cl-multiple-value-bind (begin end ftype)
230 (find-formula 'any)
231 (when (and begin end ftype)
232 (if (featurep 'xemacs)
233 (let ((ext (extent-at begin)))
234 (if ext (delete-extent ext)))
235 (remove-text-properties begin end '(display) (current-buffer)))
236 (if (eql ftype 'both)
237 (cl-multiple-value-bind (la-begin la-end la-ftype)
238 (find-formula 'latex)
239 (when (and (not arg) la-begin la-end la-ftype)
240 ;; remove & between the maxima formula and latex formula
241 ;; if that is the case.
242 (if (eql ftype 'both) (cl-decf la-begin))
243 (delete-region la-begin la-end))))))))
245 (defun form-to-image ()
246 "Convert any form to image based on form types"
247 (interactive "")
248 (cl-multiple-value-bind (start end ftype)
249 (find-formula 'any)
250 (if (and start end ftype)
251 (cond ((eql ftype 'maxima)
252 (maxima-to-image))
253 ((eql ftype 'eps)
254 (get-image-from-imaxima 'eps))
255 ((or (eql ftype 'both)
256 (eql ftype 'latex))
257 (get-image-from-imaxima 'latex)
264 (cl-defun maxima-to-image (&aux maxcmd)
265 "Transform a maxima form which is placed just before current point or
266 is surrounding the current point into a formula image."
267 (interactive "")
268 (save-excursion
269 (cl-multiple-value-bind (begin end)
270 (find-formula 'maxima)
271 (let (curpos (point))
272 (when (and begin end)
273 (setq maxcmd (buffer-substring (+ begin (length maxima-start))
274 (- end (length maxima-end))))
275 (kill-new (buffer-substring (+ begin (length maxima-start))
276 (- end (length maxima-end))))
277 (with-current-buffer (if imaxima-use-maxima-mode-flag "*maxima*" "*imaxima*")
278 (yank)
279 (insert ";")
280 (comint-send-input))))
281 (if imaxima-continuation
282 (debug-imaxima-filter "imaxima-continuation exits"))
283 (setq imaxima-continuation (list (if (string-match "[ ]*wx\\(plot2d\\|plot3d\\|draw\\|draw2d\\|draw3d\\|implicit_plot\\|contour_plot\\).*" maxcmd)
284 #'get-inline-graph
285 #'get-image-from-imaxima-1)
286 (current-buffer) begin end)))))
288 (cl-defun maxima-to-image-all (&aux maxcmd)
289 "Transform a maxima form which is placed just before current point or
290 is surrounding the current point into a formula image."
291 (interactive "")
292 (if (not (re-search-forward "maxima}[^&]" (point-max) 0))
293 (progn
294 (setq imaxima-continuation nil)
295 (cl-return-from maxima-to-image-all nil)))
296 (backward-char)
297 (cl-multiple-value-bind (begin end)
298 (find-formula 'maxima)
299 (when (and begin end)
300 (setq maxcmd (buffer-substring (+ begin (length maxima-start))
301 (- end (length maxima-end))))
302 (kill-new (buffer-substring (+ begin (length maxima-start))
303 (- end (length maxima-end))))
304 (with-current-buffer (if imaxima-use-maxima-mode-flag "*maxima*" "*imaxima*")
305 (yank)
306 (insert ";")
307 (comint-send-input))
308 (if imaxima-continuation
309 (debug-imaxima-filter "imaxima-continuation exits"))
310 (setq imaxima-continuation (list (if (string-match "[ ]*wx\\(plot2d\\|plot3d\\|draw\\|draw2d\\|draw3d\\|implicit_plot\\|contour_plot\\).*" maxcmd)
311 #'(lambda (arg)
312 (let ((cont imaxima-continuation))
313 (funcall #'get-inline-graph arg)
314 (save-current-buffer
315 (set-buffer (nth 1 cont))
316 (maxima-to-image-all))))
317 #'(lambda (arg)
318 (let ((cont imaxima-continuation))
319 (funcall #'get-image-from-imaxima-1 arg)
320 (save-current-buffer
321 (set-buffer (nth 1 cont))
322 (maxima-to-image-all)))))
323 (current-buffer) begin end)))))
326 (cl-defun get-image-from-imaxima (eps-or-latex)
327 "Converts a both form or a latex form into a formula image when
328 placed right after the form."
329 (interactive "")
330 (let (la-start la-end la-ftype entire-start entire-end entire-ftype
331 entire-string latex-formula-or-epsfile)
333 (cl-multiple-value-bind (la-start la-end la-ftype)
334 (find-formula eps-or-latex)
335 (when (and la-start la-end la-ftype)
336 (setq entire-string (buffer-substring la-start la-end))
337 (cond ((eql eps-or-latex 'latex)
338 (setq latex-formula-or-epsfile (substring entire-string
339 (length latex-start)
340 (- (length entire-string)
341 (length latex-end))))
343 ((eql eps-or-latex 'eps)
344 (setq latex-formula-or-epsfile (substring entire-string
345 (length eps-start)
346 (- (length entire-string)
347 (length eps-end))))
350 (cl-multiple-value-bind (entire-start entire-end entire-ftype)
351 (find-formula 'any)
352 (when (and entire-start entire-end entire-ftype)
353 (if (featurep 'xemacs)
354 (progn
355 (let ((ext (extent-at entire-start)))
356 (if ext
357 (cl-return-from get-image-from-imaxima nil)))
359 (cond ((eql eps-or-latex 'latex)
360 (let ((ext (extent-at 0 (imaxima-make-image latex-formula-or-epsfile eps-or-latex t))))
361 (if ext
362 (insert-extent ext entire-start entire-end t (current-buffer))))
364 ((eql eps-or-latex 'eps)
365 (let ((ext (extent-at 0 (imaxima-make-image latex-formula-or-epsfile eps-or-latex t))))
366 (if ext
367 (insert-extent ext entire-start entire-end t (current-buffer))))
372 ;; FSF Emacs
373 (add-text-properties entire-start entire-end
374 (list 'display
375 (get-text-property 1
376 'display
377 (imaxima-make-image latex-formula-or-epsfile eps-or-latex t)))
378 (current-buffer)))))))))
380 (defun get-image-from-imaxima-1 (latex-string)
381 (setq latex-string (copy-sequence latex-string))
382 (if (featurep 'xemacs)
383 (let ((ext (extent-at 0 latex-string)))
384 (if ext (set-extent-property ext 'duplicable nil))))
385 (while (string-match "^([\\%a-zA-Z0-9]+)" latex-string)
386 (setq latex-string (replace-match "" t t latex-string)))
387 (if imaxima-continuation
388 (let ((maxima-string (save-current-buffer
389 (set-buffer (nth 1 imaxima-continuation))
390 (buffer-substring (nth 2 imaxima-continuation)
391 (nth 3 imaxima-continuation))))
392 pos)
393 (save-current-buffer
394 (set-buffer (nth 1 imaxima-continuation))
395 (setq pos (copy-marker (nth 2 imaxima-continuation) t))
396 (delete-region (nth 2 imaxima-continuation) (nth 3 imaxima-continuation))
397 (setq imaxima-continuation nil)
398 (let ((str-to-insert (concat maxima-string "&{latex " latex-string " latex}")))
399 (if (featurep 'xemacs)
400 (let ((ext (extent-at 0 (imaxima-make-image latex-string 'latex t))))
401 (if ext
402 (insert-extent ext 0 (length str-to-insert) t str-to-insert)))
403 (add-text-properties 0 (length str-to-insert)
404 (list 'display
405 (get-text-property 1
406 'display
407 (imaxima-make-image latex-string 'latex t)))
408 str-to-insert))
409 (goto-char pos)
410 (insert str-to-insert))))))
412 (defun get-inline-graph (latex-string)
413 (setq latex-string (copy-sequence latex-string))
414 (if (featurep 'xemacs)
415 (let ((ext (extent-at 0 latex-string)))
416 (if ext (set-extent-property ext 'duplicable nil))))
417 (when (string-match "^([\\%a-zA-Z0-9]+)" latex-string)
418 (setq latex-string (replace-match "" t t latex-string)))
419 (if imaxima-continuation
420 (let ((maxima-string (save-current-buffer
421 (set-buffer (nth 1 imaxima-continuation))
422 (buffer-substring (nth 2 imaxima-continuation)
423 (nth 3 imaxima-continuation)))))
424 (save-current-buffer
425 (set-buffer (nth 1 imaxima-continuation))
426 (delete-region (nth 2 imaxima-continuation) (nth 3 imaxima-continuation))
427 (setq imaxima-continuation nil)
428 (let ((str-to-insert maxima-string))
429 (if (featurep 'xemacs)
430 (let ((ext (extent-at 0 (imaxima-make-image latex-string 'latex t))))
431 (if ext
432 (insert-extent ext 0 (length str-to-insert) t str-to-insert)))
433 (add-text-properties 0 (length str-to-insert)
434 (list 'display
435 (get-text-property 1 'display latex-string))
436 str-to-insert))
437 (insert str-to-insert))))))
440 (defun buffer-formula-to-image ()
441 "Transform all the latex forms and maxima&latex forms into
442 formula images."
443 (interactive "")
444 (goto-char (point-min))
445 (let ((msg "Converting latex"))
446 (message msg)
447 (while (search-forward latex-end nil 1)
448 (get-image-from-imaxima 'latex)
449 (setq msg (concat msg "."))
450 (message msg))
451 (message (concat msg ".done.")))
452 (goto-char (point-min))
453 (let ((msg "Converting eps"))
454 (while (search-forward eps-end nil 1)
455 (get-image-from-imaxima 'eps)
456 (setq msg (concat msg "."))
457 (message msg))
458 (message (concat msg ".done.")))
459 (goto-char (point-min))
460 (let ((msg "Converting maxima..."))
461 (message msg)
462 (maxima-to-image-all)
463 (message (concat msg "done.")))
464 (set-buffer-modified-p nil))
467 (provide 'imath)
469 ;;; imath.el ends here