Function: calc-graph-plot

calc-graph-plot is an autoloaded, interactive and byte-compiled function defined in calc-graph.el.gz.

Signature

(calc-graph-plot FLAG &optional PRINTING)

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-graph.el.gz
(defun calc-graph-plot (flag &optional printing)
  (interactive "P")
  (calc-slow-wrapper
   (let ((calcbuf (current-buffer))
	 (tempbuf (get-buffer-create "*Gnuplot Temp-2*"))
	 (tempoutfile nil)
	 (calc-graph-curve-num 0)
	 (calc-graph-refine (and flag (> (prefix-numeric-value flag) 0)))
	 (recompute (and flag (< (prefix-numeric-value flag) 0)))
	 (calc-graph-surprise-splot nil)
	 (tty-output nil)
	 cache-env calc-graph-is-splot device output calc-graph-resolution precision samples-pos)
     (add-hook 'kill-emacs-hook 'calc-graph-kill-hook)
     (save-excursion
       (calc-graph-init)
       (set-buffer tempbuf)
       (erase-buffer)
       (set-buffer calc-gnuplot-input)
       (goto-char (point-min))
       (setq calc-graph-is-splot (re-search-forward "^splot[ \t]" nil t))
       (let ((str (buffer-string))
	     (ver calc-gnuplot-version))
	 (set-buffer (get-buffer-create "*Gnuplot Temp*"))
	 (erase-buffer)
	 (insert "# (Note: This is a temporary copy---do not edit!)\n")
	 (if (>= ver 2)
	     (insert "set noarrow\nset nolabel\n"
		     "set autoscale xy\nset nologscale xy\n"
		     "set xlabel\nset ylabel\nset title\n"
		     "set noclip points\nset clip one\nset clip two\n"
		     "set format \"%g\"\nset tics\nset xtics\nset ytics\n"
		     "set style data linespoints\n"
		     "set nogrid\nset nokey\nset nopolar\n"))
	 (if (>= ver 3)
	     (insert "set surface\nset nocontour\n"
		     "set " (if calc-graph-is-splot "" "no") "parametric\n"
		     "set notimestamp\nset border\nset ztics\nset zeroaxis\n"
		     "set view 60,30,1,1\nset offsets 0,0,0,0\n"))
	 (setq samples-pos (point))
	 (insert "\n\n" str))
       (goto-char (point-min))
       (if calc-graph-is-splot
	   (if calc-graph-refine
	       (error "This option works only for 2d plots")
	     (setq recompute t)))
       (let ((calc-gnuplot-input (current-buffer))
	     (calc-graph-no-auto-view t))
	 (if printing
	     (setq device calc-gnuplot-print-device
		   output calc-gnuplot-print-output)
	   (setq device (calc-graph-find-command "terminal")
		 output (calc-graph-find-command "output"))
	   (or device
	       (setq device calc-gnuplot-default-device))
	   (if output
	       (setq output (car (read-from-string output)))
	     (setq output calc-gnuplot-default-output)))
	 (if (or (equal device "") (equal device "default"))
	     (setq device
		   (cond
		    (printing "postscript")
		    ;; Check MS-Windows before X, in case they have
		    ;; $DISPLAY set for some reason (e.g., Cygwin or
		    ;; whatever)
                    ((string= calc-gnuplot-name "pgnuplot")
                     "windows")
                    ;; Versions of gnuplot that come without pgnuplot
                    ;; only work on MS-Windows with "qt" as the
                    ;; terminal, for some reason.
		    ((calc-graph-w32-p)
		     "qt")
		    ((or (eq window-system 'x) (getenv "DISPLAY"))
		     "x11")
		    ((>= calc-gnuplot-version 3)
		     "dumb")
		    (t "postscript"))))
	 (unless (equal device calc-graph-last-device)
	   (setq calc-graph-last-device device)
	   (unless (calc-gnuplot-command "set terminal" device)
             ;; If gnuplot doesn't support the terminal, then set it
             ;; to "dumb".
             (calc-gnuplot-command "set terminal dumb")
             (setq device "dumb")))
	 (if (equal device "dumb")
	     (setq device (format "dumb %d %d"
				  (1- (frame-width)) (1- (frame-height)))))
	 (if (equal device "big")
	     (setq device (format "dumb %d %d"
				  (* 4 (- (frame-width) 3))
				  (* 4 (- (frame-height) 3)))))
	 (if (stringp output)
	     (if (or (equal output "auto")
		     (and (equal output "tty") (setq tty-output t)))
		 (setq tempoutfile (calc-temp-file-name -1)
		       output tempoutfile))
	   (setq output (eval output t)))
	 (or (equal output calc-graph-last-output)
	     (progn
	       (setq calc-graph-last-output output)
	       (calc-gnuplot-command "set output"
				     (if (equal output "STDOUT")
					 ""
				       (prin1-to-string output)))))
	 (setq calc-graph-resolution (calc-graph-find-command "samples"))
	 (if calc-graph-resolution
	     (setq calc-graph-resolution (string-to-number calc-graph-resolution))
	   (setq calc-graph-resolution (if calc-graph-is-splot
				calc-graph-default-resolution-3d
			      calc-graph-default-resolution)))
	 (setq precision (calc-graph-find-command "precision"))
	 (if precision
	     (setq precision (string-to-number precision))
	   (setq precision calc-graph-default-precision))
	 (calc-graph-set-command "terminal")
	 (calc-graph-set-command "output")
	 (calc-graph-set-command "samples")
	 (calc-graph-set-command "precision"))
       (goto-char samples-pos)
       (insert "set samples " (int-to-string (max (if calc-graph-is-splot 20 200)
						  (+ 5 calc-graph-resolution))) "\n")
       (while (re-search-forward "{\\*[^}]+}[^,\n]*" nil t)
	 (delete-region (match-beginning 0) (match-end 0))
	 (if (looking-at ",")
	     (delete-char 1)
	   (while (memq (preceding-char) '(?\s ?\t))
	     (forward-char -1))
	   (if (eq (preceding-char) ?\,)
	       (delete-char -1))))
       (with-current-buffer calcbuf
	 (setq cache-env (list calc-angle-mode
			       calc-complex-mode
			       calc-simplify-mode
			       calc-infinite-mode
			       calc-word-size
			       precision calc-graph-is-splot))
	 (if (and (not recompute)
		  (equal (cdr (car calc-graph-data-cache)) cache-env))
	     (while (> (length calc-graph-data-cache)
		       calc-graph-data-cache-limit)
	       (setcdr calc-graph-data-cache
		       (cdr (cdr calc-graph-data-cache))))
	   (setq calc-graph-data-cache (list (cons nil cache-env)))))
       (calc-graph-find-plot t t)
       (while (re-search-forward
	       (if calc-graph-is-splot
		   "{\\([^{}:\n]+\\):\\([^{}:\n]+\\):\\([^{}:\n]+\\)}"
		 "{\\([^{}:\n]+\\)\\(:\\)\\([^{}:\n]+\\)}")
	       nil t)
	 (setq calc-graph-curve-num (1+ calc-graph-curve-num))
	 (let* ((calc-graph-xname (buffer-substring (match-beginning 1) (match-end 1)))
		(xvar (intern (concat "var-" calc-graph-xname)))
		(calc-graph-xvalue (math-evaluate-expr (calc-var-value xvar)))
		(calc-graph-y3name (and calc-graph-is-splot
			     (buffer-substring (match-beginning 2)
					       (match-end 2))))
		(y3var (and calc-graph-is-splot (intern (concat "var-" calc-graph-y3name))))
		(calc-graph-y3value (and calc-graph-is-splot (calc-var-value y3var)))
		(calc-graph-yname (buffer-substring (match-beginning 3) (match-end 3)))
		(yvar (intern (concat "var-" calc-graph-yname)))
		(calc-graph-yvalue (calc-var-value yvar))
		filename)
	   (delete-region (match-beginning 0) (match-end 0))
	   (setq filename (calc-temp-file-name calc-graph-curve-num))
	   (with-current-buffer calcbuf
	     (let (tempbuftop
		   (calc-graph-xp calc-graph-xvalue)
		   (calc-graph-yp calc-graph-yvalue)
		   (calc-graph-zp nil)
		   (calc-graph-xlow nil) (calc-graph-xhigh nil)
		   ;; (y3low nil) (y3high nil)
		   calc-graph-xvec calc-graph-xval calc-graph-xstep var-DUMMY
		   ;; y3val
		   calc-graph-y3step var-DUMMY2 (calc-graph-zval nil)
		   calc-graph-yvec calc-graph-yval calc-graph-ycache calc-graph-ycacheptr calc-graph-yvector
		   calc-graph-numsteps calc-graph-numsteps3
		   (calc-graph-keep-file (and (not calc-graph-is-splot) (file-exists-p filename)))
		   (calc-graph-stepcount 0)
		   (calc-symbolic-mode nil)
		   (calc-prefer-frac nil)
		   (calc-internal-prec (max 3 precision))
		   (calc-simplify-mode (and (not (memq calc-simplify-mode
						       '(none num)))
					    calc-simplify-mode))
		   (calc-graph-blank t)
		   (calc-graph-non-blank nil)
		   (math-working-step 0)
		   (math-working-step-2 nil))
	       (save-excursion
		 (if calc-graph-is-splot
		     (calc-graph-compute-3d)
		   (calc-graph-compute-2d))
		 (set-buffer tempbuf)
		 (goto-char (point-max))
		 (insert "\n" calc-graph-xname)
		 (if calc-graph-is-splot
		     (insert ":" calc-graph-y3name))
		 (insert ":" calc-graph-yname "\n\n")
		 (setq tempbuftop (point))
		 (let ((calc-group-digits nil)
		       (calc-leading-zeros nil)
		       (calc-number-radix 10)
                       (calc-twos-complement-mode nil)
		       (entry (and (not calc-graph-is-splot)
				   (list calc-graph-xp calc-graph-yp calc-graph-xhigh calc-graph-numsteps))))
		   (or (equal entry
			      (nth 1 (nth (1+ calc-graph-curve-num)
					  calc-graph-file-cache)))
		       (setq calc-graph-keep-file nil))
		   (setcar (cdr (nth (1+ calc-graph-curve-num) calc-graph-file-cache))
			   entry)
		   (or calc-graph-keep-file
		       (calc-graph-format-data)))
		 (or calc-graph-keep-file
		     (progn
		       (or calc-graph-non-blank
			   (error "No valid data points for %s:%s"
				  calc-graph-xname calc-graph-yname))
		       (write-region tempbuftop (point-max) filename
				     nil 'quiet))))))
	   (insert (prin1-to-string filename))))
       (if calc-graph-surprise-splot
	   (setcdr cache-env nil))
       (if (= calc-graph-curve-num 0)
	   (progn
	     (calc-gnuplot-command "clear")
	     (calc-clear-command-flag 'clear-message)
	     (message "No data to plot!"))
	 (setq calc-graph-data-cache-limit (max calc-graph-curve-num
						calc-graph-data-cache-limit))
	 (let ((filename (calc-temp-file-name 0)))
	   (write-region (point-min) (point-max) filename nil 'quiet)
	   (calc-gnuplot-command "load" (prin1-to-string filename)))
	 (or (equal output "STDOUT")
	     calc-gnuplot-keep-outfile
	     (progn   ; need to close the output file before printing/plotting
	       (setq calc-graph-last-output "STDOUT")
	       (calc-gnuplot-command "set output")))
	 (let ((command (if printing
			    calc-gnuplot-print-command
			  (or calc-gnuplot-plot-command
			      (and (string-match "^dumb" device)
				   'calc-graph-show-dumb)
			      (and tty-output
				   'calc-graph-show-tty)))))
	   (if command
	       (if (stringp command)
		   (calc-gnuplot-command
		    "!" (format command
				(or tempoutfile
				    calc-gnuplot-print-output)))
		 (if (symbolp command)
		     (funcall command output)
		   (eval command t))))))))))