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)
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.
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
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
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(),
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
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.
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
)
151 ;; Imath requires *imaxima* running.
153 (let ((cur-buf (current-buffer)))
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."
168 (insert "{maxima maxima}")
171 (defun compose-latex-formula ()
172 "Insert maxima form template at the current position."
174 (insert "{latex latex}")
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
))
183 (setq start-symbol latex-start
184 end-symbol latex-end
))
186 (setq start-symbol eps-start
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)))
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
)))))))
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
)))
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
)
226 (defun remove-maxima-formula-image (arg)
229 (cl-multiple-value-bind (begin end ftype
)
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"
248 (cl-multiple-value-bind (start end ftype
)
250 (if (and start end ftype
)
251 (cond ((eql ftype
'maxima
)
254 (get-image-from-imaxima 'eps
))
255 ((or (eql ftype
'both
)
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."
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*")
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
)
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."
292 (if (not (re-search-forward "maxima}[^&]" (point-max) 0))
294 (setq imaxima-continuation nil
)
295 (cl-return-from maxima-to-image-all nil
)))
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*")
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
)
312 (let ((cont imaxima-continuation
))
313 (funcall #'get-inline-graph arg
)
315 (set-buffer (nth 1 cont
))
316 (maxima-to-image-all))))
318 (let ((cont imaxima-continuation
))
319 (funcall #'get-image-from-imaxima-1 arg
)
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."
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
340 (- (length entire-string
)
341 (length latex-end
))))
343 ((eql eps-or-latex
'eps
)
344 (setq latex-formula-or-epsfile
(substring entire-string
346 (- (length entire-string
)
350 (cl-multiple-value-bind (entire-start entire-end entire-ftype
)
352 (when (and entire-start entire-end entire-ftype
)
353 (if (featurep 'xemacs
)
355 (let ((ext (extent-at entire-start
)))
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
))))
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
))))
367 (insert-extent ext entire-start entire-end t
(current-buffer))))
373 (add-text-properties entire-start entire-end
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
))))
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
))))
402 (insert-extent ext
0 (length str-to-insert
) t str-to-insert
)))
403 (add-text-properties 0 (length str-to-insert
)
407 (imaxima-make-image latex-string
'latex t
)))
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
)))))
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
))))
432 (insert-extent ext
0 (length str-to-insert
) t str-to-insert
)))
433 (add-text-properties 0 (length str-to-insert
)
435 (get-text-property 1 'display latex-string
))
437 (insert str-to-insert
))))))
440 (defun buffer-formula-to-image ()
441 "Transform all the latex forms and maxima&latex forms into
444 (goto-char (point-min))
445 (let ((msg "Converting latex"))
447 (while (search-forward latex-end nil
1)
448 (get-image-from-imaxima 'latex
)
449 (setq msg
(concat 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
"."))
458 (message (concat msg
".done.")))
459 (goto-char (point-min))
460 (let ((msg "Converting maxima..."))
462 (maxima-to-image-all)
463 (message (concat msg
"done.")))
464 (set-buffer-modified-p nil
))
469 ;;; imath.el ends here