imaxima.el: inlined plots from postscript files



Dear Yasuaki Honda and Emacs imaxima.el users,

I modified Emacs "imath" mode to display EPS files (besides currently
implemented Latex and Maxima formulas) using {eps myplot.ps eps} form.

Thus "imath-to-html" function in "imath" mode can create HTML file
including plots and formulas.

I believe it can simplify generation of HTML documents including plots
and formulas from Maxima by Emacs. The patches for imath.el and
imaxima.el are attached. Please let me know what you think about it.

Thanks,
-Bohumir

> Hi Yevgeny Zajcev,
>
> So, your idea is to generate plotted graphics and render it
> inlined within Emacs buffer. Sounds really great!!
>
> I am sure there is no such thing today. Here are my 2 cents:
> 1) You could start with imaxima-imath-0.94 as the base for your project.
> 2) The function imaxima-make-image in imaxima.el could be modified
> to generate a graph image when passed a command of plot2d() or plot3d().
>
>
> >
> > Hello, I'm new to Maxima and I'm very excited it.  I use `imaxima' to
> > interact with Maxima.  One thing that annoys me a little is that
> > Maxima uses external gnuplot for drawing.  So my question is it
> > possible to make Maxima use PSTricks's pst-plot and pst-3dplot plugins
> > to performe plotting, so imaxima will be able to generate inlined
> > plots.  If there no such thing, could you please provide
> > implementation hints for me to try to implement it ..
> >
> > Thanks for great software!
> >
> > --
> > lg
-------------- next part --------------
--- old/imath.el	2007-02-12 16:29:35.000000000 -0600
+++ new/imath.el	2007-02-12 16:49:24.000000000 -0600
@@ -143,6 +143,8 @@
 (defvar maxima-end " maxima}")
 (defvar latex-start "{latex ")
 (defvar latex-end " latex}")
+(defvar eps-start "{eps ")
+(defvar eps-end " eps}")
 
 (defun compose-maxima-formula ()
   "Insert maxima form template at the current position."
@@ -164,6 +166,9 @@
 	  ((eql ftype 'latex)
 	   (setq start-symbol latex-start
 		 end-symbol latex-end))
+          ((eql ftype 'eps)
+	   (setq start-symbol eps-start
+		 end-symbol eps-end))
 	  ((eql ftype 'both)
 	   (save-excursion
 	     (multiple-value-bind (la-start la-end la-type)
@@ -187,6 +192,8 @@
 		  (return-from find-formula tmpresult))
 		 ((setq tmpresult (find-formula 'maxima))
 		  (return-from find-formula tmpresult))
+		 ((setq tmpresult (find-formula 'eps))
+		  (return-from find-formula tmpresult))
 		 (t 
 		  (return-from find-formula nil))))
 	  (t (return-from find-formula nil)))
@@ -227,9 +234,16 @@
     (if (and start end ftype)
 	(cond ((eql ftype 'maxima)
 	       (maxima-to-latex))
+	      ((eql ftype 'eps)
+	       (get-image-from-imaxima 'eps))
 	      ((or (eql ftype 'both)
 		   (eql ftype 'latex))
-	       (get-image-from-imaxima))))))
+	       (get-image-from-imaxima 'latex)
+	       )
+	      )
+      )
+    )
+  )
 
 (defun maxima-to-latex ()
   "Transform maxima form which is placed just before current point or
@@ -252,20 +266,30 @@
       (setq continuation (list #'get-image-from-imaxima-1
 			       (current-buffer) begin end)))))
 
-(defun* get-image-from-imaxima ()
+(defun* get-image-from-imaxima (eps-or-latex)
   "Converts a both form or a latex form into a formula image when
 placed right after the form."
   (interactive "")
   (let (la-start la-end la-ftype entire-start entire-end entire-ftype
-		 latex-string entire-string latex-formula)
+		 entire-string latex-formula-or-epsfile)
+
     (multiple-value-bind (la-start la-end la-ftype)
-	(find-formula 'latex)
+	(find-formula eps-or-latex)
       (when (and la-start la-end la-ftype)
-	(setq latex-string (buffer-substring la-start la-end))
-	(setq latex-formula (substring latex-string
-				       (length latex-start)
-				       (- (length latex-string)
-					  (length latex-end))))
+	(setq entire-string (buffer-substring la-start la-end))
+	(cond ((eql eps-or-latex 'latex)
+	       (setq latex-formula-or-epsfile (substring entire-string
+							 (length latex-start)
+							 (- (length entire-string)
+							    (length latex-end))))
+	       )
+	      ((eql eps-or-latex 'eps)
+	       (setq latex-formula-or-epsfile (substring entire-string
+							 (length eps-start)
+							 (- (length entire-string)
+							    (length eps-end))))
+	       )
+	      )
 	(multiple-value-bind (entire-start entire-end entire-ftype)
 	    (find-formula 'any)
 	  (when (and entire-start entire-end entire-ftype)
@@ -274,15 +298,26 @@
 		  (let ((ext (extent-at entire-start)))
 		    (if ext
 			(return-from get-image-from-imaxima nil)))
-		  (let ((ext (extent-at 0 (imaxima-make-image latex-formula t))))
-		    (if ext
-			(insert-extent ext entire-start entire-end t (current-buffer)))))
+		  
+		  (cond ((eql eps-or-latex 'latex)
+			 (let ((ext (extent-at 0 (imaxima-make-image latex-formula-or-epsfile eps-or-latex  t))))
+			   (if ext
+			       (insert-extent ext entire-start entire-end t (current-buffer))))
+			 )
+			((eql eps-or-latex 'eps)
+			 (let ((ext (extent-at 0 (imaxima-make-image latex-formula-or-epsfile eps-or-latex  t))))
+			   (if ext
+			       (insert-extent ext entire-start entire-end t (current-buffer))))
+			 )
+			)
+		  )
+
 	      ;; FSF Emacs
 	      (add-text-properties entire-start entire-end
 				   (list 'display
 					 (get-text-property 1
 							    'display
-							    (imaxima-make-image latex-formula t)))
+							    (imaxima-make-image latex-formula-or-epsfile eps-or-latex t)))
 				   (current-buffer)))))))))
 
 (defun get-image-from-imaxima-1 (latex-string)
@@ -303,14 +338,14 @@
 	  (setq continuation nil)
 	  (let ((str-to-insert (concat maxima-string "&{latex " latex-string " latex}")))
 	    (if (featurep 'xemacs)
-		(let ((ext (extent-at 0 (imaxima-make-image latex-string t))))
+		(let ((ext (extent-at 0 (imaxima-make-image latex-string 'latex t))))
 		  (if ext
 		      (insert-extent ext 0 (length str-to-insert) t str-to-insert)))
 	      (add-text-properties 0 (length str-to-insert)
 				   (list 'display
 					 (get-text-property 1
 							    'display
-							    (imaxima-make-image latex-string t)))
+							    (imaxima-make-image latex-string 'latex  t)))
 				   str-to-insert))
 	    (insert str-to-insert))))))
 
@@ -320,10 +355,17 @@
 formula images." 
   (interactive "")
   (beginning-of-buffer)
-  (let ((msg "Converting"))
+  (let ((msg "Converting latex"))
     (message msg)
     (while (search-forward latex-end nil 1)
-      (get-image-from-imaxima)
+      (get-image-from-imaxima 'latex)
+      (setq msg (concat msg "."))
+      (message msg))
+    (message (concat msg ".done.")))
+  (beginning-of-buffer)
+  (let ((msg "Converting eps"))
+    (while (search-forward eps-end nil 1)
+      (get-image-from-imaxima 'eps)
       (setq msg (concat msg "."))
       (message msg))
     (message (concat msg ".done.")))
@@ -457,12 +499,16 @@
    If search failed, error search-failed is signaled."
   (interactive "")
   (let (start-pos end-pos tmp found-string ftype)
-    (re-search-forward (concat maxima-start "\\|" latex-start))
+    (re-search-forward (concat maxima-start "\\|" latex-start "\\|" eps-start))
     (setq found-string (match-string 0))
     (cond ((string= found-string latex-start)
 	   (setq start-pos (- (point) (length latex-start)))
 	   (search-forward latex-end)
 	   (setq end-pos (point) ftype 'latex))
+	  ((string= found-string eps-start)
+	   (setq start-pos (- (point) (length eps-start)))
+	   (search-forward eps-end)
+	   (setq end-pos (point) ftype 'eps))
 	  ((string= found-string maxima-start)
 	   (setq start-pos (- (point) (length maxima-start)))
 	   (search-forward maxima-end)
-------------- next part --------------
--- old/imaxima.el	2007-02-12 16:29:24.000000000 -0600
+++ new/imaxima.el	2007-02-12 16:35:24.000000000 -0600
@@ -294,6 +294,11 @@
   :group 'imaxima
   :type '(string))
 
+(defcustom imaxima-cp-program "cp"
+  "cp executable."
+  :group 'imaxima
+  :type '(string))
+
 (defcustom imaxima-dvips-options '("-E" "-R")
   "Options passed to dvips for conversion from DVI to EPS."
   :group 'imaxima
@@ -695,20 +700,29 @@
 			psfilename))
   (imaxima-gs-wait))
 
-(defun imaxima-make-image (str &optional no-label-p)
+(defun imaxima-make-image (str eps-or-latex &optional no-label-p)
   "Make image from STR. If no-label-p is specified t,
    label becomes nil and passed to imaxima-tex-to-dvi."
+
   (let* ((filename (expand-file-name
 		    (number-to-string (incf imaxima-file-counter))
 		    imaxima-tmp-subdir))
 	 (psfilename (concat filename ".ps"))
 	 (label "*"))
-    (when (string-match "\\(\\([^]*\\)\\)" str)
-      (setq label (match-string 2 str))
-      (setq str (replace-match "" t t str 1)))
-    (if no-label-p (setq label nil)) ;; override label to nil if no-label-p
-    (imaxima-tex-to-dvi str label (concat filename ".tex"))
-    (imaxima-dvi-to-ps filename)
+
+    (cond ((eql eps-or-latex 'latex)
+	   (when (string-match "\\(\\([^]*\\)\\)" str)
+	      (setq label (match-string 2 str))
+	      (setq str (replace-match "" t t str 1)))
+	   (if no-label-p (setq label nil)) ;; override label to nil if no-label-p
+	   (imaxima-tex-to-dvi str label (concat filename ".tex"))
+	   (imaxima-dvi-to-ps filename)
+	   )
+	  ((eql eps-or-latex 'eps)
+	   (copy-file str psfilename)
+	   (setq label nil)
+	   )
+	  )
     (if (not (file-exists-p psfilename))
 	(imaxima-latex-error str filename)
       (multiple-value-bind (bb  width height)
@@ -937,7 +951,7 @@
 	      (setq text (match-string 2 rest))
 	      (setq match (match-string 3 rest))
 	      (setq rest (replace-match "" t t rest 1))
-	      (setq output (concat output (if (equal output "") "" newline-char) text (imaxima-make-image match))))
+	      (setq output (concat output (if (equal output "") "" newline-char) text (imaxima-make-image match 'latex))))
 	    (setq imaxima-output "")
 	    (message "Processing Maxima output...done")
 	    (if continuation
@@ -957,7 +971,7 @@
 	      (setq text (match-string 2 rest))
 	      (setq match (match-string 3 rest))
 	      (setq rest (replace-match "" t t rest 1))
-	      (setq output (concat output (if (equal output "") "" newline-char) text (imaxima-make-image match))))
+	      (setq output (concat output (if (equal output "") "" newline-char) text (imaxima-make-image match 'latex))))
 	    (setq imaxima-output "")
 	    (message "Processing Maxima output...done")
 	    (if continuation
@@ -966,7 +980,7 @@
 	 ;; Special prompt, question.
 	 ((char-equal lastchar ?)
 	  (string-match "\\([^]*\\)" imaxima-output)
-	  (prog1 (imaxima-make-image (match-string 1 imaxima-output))
+	  (prog1 (imaxima-make-image (match-string 1 imaxima-output) 'latex)
 	    (setq imaxima-output "")))
 	 (t ""))))))