1 ;;; calc-graph.el --- graph output functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
36 (defvar calc-gnuplot-name "gnuplot"
37 "*Name of GNUPLOT program, for calc-graph features.")
39 (defvar calc-gnuplot-plot-command nil
40 "*Name of command for displaying GNUPLOT output; %s = file name to print.")
42 (defvar calc-gnuplot-print-command "lp %s"
43 "*Name of command for printing GNUPLOT output; %s = file name to print.")
45 (defvar calc-gnuplot-tempfile "calc")
47 (defvar calc-gnuplot-default-device)
48 (defvar calc-gnuplot-default-output)
49 (defvar calc-gnuplot-print-device)
50 (defvar calc-gnuplot-print-output)
51 (defvar calc-gnuplot-keep-outfile nil)
52 (defvar calc-gnuplot-version nil)
54 (defvar calc-gnuplot-display (getenv "DISPLAY"))
55 (defvar calc-gnuplot-geometry)
57 (defvar calc-graph-default-resolution)
58 (defvar calc-graph-default-resolution-3d)
59 (defvar calc-graph-default-precision 5)
61 (defvar calc-gnuplot-buffer nil)
62 (defvar calc-gnuplot-input nil)
64 (defvar calc-gnuplot-last-error-pos 1)
65 (defvar calc-graph-last-device nil)
66 (defvar calc-graph-last-output nil)
67 (defvar calc-graph-file-cache nil)
68 (defvar calc-graph-var-cache nil)
69 (defvar calc-graph-data-cache nil)
70 (defvar calc-graph-data-cache-limit 10)
71 (defvar calc-graph-no-auto-view nil)
72 (defvar calc-graph-no-wait nil)
73 (defvar calc-gnuplot-trail-mark)
75 (defun calc-graph-fast (many)
77 (let ((calc-graph-no-auto-view t))
80 (calc-graph-plot nil)))
82 (defun calc-graph-fast-3d (many)
84 (let ((calc-graph-no-auto-view t))
86 (calc-graph-add-3d many)
87 (calc-graph-plot nil)))
89 (defun calc-graph-delete (all)
94 (set-buffer calc-gnuplot-input)
95 (and (calc-graph-find-plot t all)
97 (if (looking-at "s?plot")
99 (setq calc-graph-var-cache nil)
100 (delete-region (point) (point-max)))
101 (delete-region (point) (1- (point-max)))))))
102 (calc-graph-view-commands)))
104 (defun calc-graph-find-plot (&optional before all)
105 (goto-char (point-min))
106 (and (re-search-forward "^s?plot[ \t]+" nil t)
108 (goto-char (point-max))
110 (not (search-backward "," nil t))
115 (beginning-of-line)))
117 (re-search-forward ",[ \t]+")))
120 (defun calc-graph-add (many)
125 (calc-graph-add-curve (calc-graph-lookup (calc-top-n 2))
126 (calc-graph-lookup (calc-top-n 1))))
127 ((or (consp many) (eq many 0))
128 (let ((xdata (calc-graph-lookup (calc-top-n 2)))
129 (ylist (calc-top-n 1)))
130 (or (eq (car-safe ylist) 'vec)
131 (error "Y argument must be a vector"))
132 (while (setq ylist (cdr ylist))
133 (calc-graph-add-curve xdata (calc-graph-lookup (car ylist))))))
134 ((> (setq many (prefix-numeric-value many)) 0)
135 (let ((xdata (calc-graph-lookup (calc-top-n (1+ many)))))
137 (calc-graph-add-curve xdata
138 (calc-graph-lookup (calc-top-n many)))
139 (setq many (1- many)))))
144 (setq pair (calc-top-n many))
145 (or (and (eq (car-safe pair) 'vec)
147 (error "Argument must be an [x,y] vector"))
148 (calc-graph-add-curve (calc-graph-lookup (nth 1 pair))
149 (calc-graph-lookup (nth 2 pair)))
150 (setq many (1- many))))))
151 (calc-graph-view-commands)))
153 (defun calc-graph-add-3d (many)
158 (calc-graph-add-curve (calc-graph-lookup (calc-top-n 3))
159 (calc-graph-lookup (calc-top-n 2))
160 (calc-graph-lookup (calc-top-n 1))))
161 ((or (consp many) (eq many 0))
162 (let ((xdata (calc-graph-lookup (calc-top-n 3)))
163 (ydata (calc-graph-lookup (calc-top-n 2)))
164 (zlist (calc-top-n 1)))
165 (or (eq (car-safe zlist) 'vec)
166 (error "Z argument must be a vector"))
167 (while (setq zlist (cdr zlist))
168 (calc-graph-add-curve xdata ydata
169 (calc-graph-lookup (car zlist))))))
170 ((> (setq many (prefix-numeric-value many)) 0)
171 (let ((xdata (calc-graph-lookup (calc-top-n (+ many 2))))
172 (ydata (calc-graph-lookup (calc-top-n (+ many 1)))))
174 (calc-graph-add-curve xdata ydata
175 (calc-graph-lookup (calc-top-n many)))
176 (setq many (1- many)))))
181 (setq curve (calc-top-n many))
182 (or (and (eq (car-safe curve) 'vec)
183 (= (length curve) 4))
184 (error "Argument must be an [x,y,z] vector"))
185 (calc-graph-add-curve (calc-graph-lookup (nth 1 curve))
186 (calc-graph-lookup (nth 2 curve))
187 (calc-graph-lookup (nth 3 curve)))
188 (setq many (1- many))))))
189 (calc-graph-view-commands)))
191 (defun calc-graph-add-curve (xdata ydata &optional zdata)
192 (let ((num (calc-graph-count-curves))
193 (pstyle (calc-var-value 'var-PointStyles))
194 (lstyle (calc-var-value 'var-LineStyles)))
196 (set-buffer calc-gnuplot-input)
197 (goto-char (point-min))
198 (if (re-search-forward (if zdata "^plot[ \t]" "^splot[ \t]")
200 (error "Can't mix 2d and 3d curves on one graph"))
201 (if (re-search-forward "^s?plot[ \t]" nil t)
205 (goto-char (point-max))
206 (or (eq (preceding-char) ?\n)
208 (insert (if zdata "splot" "plot") " \n")
210 (insert "{" (symbol-name (nth 1 xdata))
211 ":" (symbol-name (nth 1 ydata)))
213 (insert ":" (symbol-name (nth 1 zdata))))
215 "title \"" (symbol-name (nth 1 ydata)) "\" "
217 (setq pstyle (and (eq (car-safe pstyle) 'vec) (nth (1+ num) pstyle)))
218 (setq lstyle (and (eq (car-safe lstyle) 'vec) (nth (1+ num) lstyle))))
219 (calc-graph-set-styles
220 (or (and (Math-num-integerp lstyle) (math-trunc lstyle))
222 (or (and (Math-num-integerp pstyle) (math-trunc pstyle))
223 (if (eq (car-safe (calc-var-value (nth 2 ydata))) 'vec)
226 (defun calc-graph-lookup (thing)
227 (if (and (eq (car-safe thing) 'var)
228 (calc-var-value (nth 2 thing)))
230 (let ((found (assoc thing calc-graph-var-cache)))
232 (let ((varname (concat "PlotData"
234 (1+ (length calc-graph-var-cache))))))
235 (setq var (list 'var (intern varname)
236 (intern (concat "var-" varname)))
237 found (cons thing var)
238 calc-graph-var-cache (cons found calc-graph-var-cache))
239 (set (nth 2 var) thing)))
242 (defun calc-graph-juggle (arg)
246 (set-buffer calc-gnuplot-input)
248 (let ((num (calc-graph-count-curves)))
251 (setq arg (+ arg num))))))
252 (while (>= (setq arg (1- arg)) 0)
253 (calc-graph-do-juggle))))
255 (defun calc-graph-count-curves ()
257 (set-buffer calc-gnuplot-input)
258 (if (re-search-forward "^s?plot[ \t]" nil t)
260 (goto-char (point-min))
261 (while (search-forward "," nil t)
266 (defun calc-graph-do-juggle ()
268 (and (calc-graph-find-plot t t)
271 (calc-graph-find-plot t nil)
272 (or (eq base (point))
273 (let ((str (buffer-substring (+ (point) 2) (1- (point-max)))))
274 (delete-region (point) (1- (point-max)))
275 (goto-char (+ base 5))
276 (insert str ", ")))))))
278 (defun calc-graph-print (flag)
280 (calc-graph-plot flag t))
284 (defvar var-PlotRejects)
286 ;; The following variables are local to calc-graph-plot, but are
287 ;; used in the functions calc-graph-compute-2d, calc-graph-refine-2d,
288 ;; calc-graph-recompute-2d, calc-graph-compute-3d and
289 ;; calc-graph-format-data, which are called by calc-graph-plot.
290 (defvar calc-graph-yvalue)
291 (defvar calc-graph-yvec)
292 (defvar calc-graph-numsteps)
293 (defvar calc-graph-numsteps3)
294 (defvar calc-graph-xvalue)
295 (defvar calc-graph-xvec)
296 (defvar calc-graph-xname)
297 (defvar calc-graph-yname)
298 (defvar calc-graph-xstep)
299 (defvar calc-graph-ycache)
300 (defvar calc-graph-ycacheptr)
301 (defvar calc-graph-refine)
302 (defvar calc-graph-keep-file)
303 (defvar calc-graph-xval)
304 (defvar calc-graph-xlow)
305 (defvar calc-graph-xhigh)
306 (defvar calc-graph-yval)
307 (defvar calc-graph-yp)
308 (defvar calc-graph-xp)
309 (defvar calc-graph-zp)
310 (defvar calc-graph-yvector)
311 (defvar calc-graph-resolution)
312 (defvar calc-graph-y3value)
313 (defvar calc-graph-y3name)
314 (defvar calc-graph-y3step)
315 (defvar calc-graph-zval)
316 (defvar calc-graph-stepcount)
317 (defvar calc-graph-is-splot)
318 (defvar calc-graph-surprise-splot)
319 (defvar calc-graph-blank)
320 (defvar calc-graph-non-blank)
321 (defvar calc-graph-curve-num)
323 (defun calc-graph-plot (flag &optional printing)
326 (let ((calcbuf (current-buffer))
327 (tempbuf (get-buffer-create "*Gnuplot Temp-2*"))
330 (calc-graph-curve-num 0)
331 (calc-graph-refine (and flag (> (prefix-numeric-value flag) 0)))
332 (recompute (and flag (< (prefix-numeric-value flag) 0)))
333 (calc-graph-surprise-splot nil)
335 cache-env calc-graph-is-splot device output calc-graph-resolution precision samples-pos)
336 (add-hook 'kill-emacs-hook 'calc-graph-kill-hook)
341 (set-buffer calc-gnuplot-input)
342 (goto-char (point-min))
343 (setq calc-graph-is-splot (re-search-forward "^splot[ \t]" nil t))
344 (let ((str (buffer-string))
345 (ver calc-gnuplot-version))
346 (set-buffer (get-buffer-create "*Gnuplot Temp*"))
348 (insert "# (Note: This is a temporary copy---do not edit!)\n")
350 (insert "set noarrow\nset nolabel\n"
351 "set autoscale xy\nset nologscale xy\n"
352 "set xlabel\nset ylabel\nset title\n"
353 "set noclip points\nset clip one\nset clip two\n"
354 "set format \"%g\"\nset tics\nset xtics\nset ytics\n"
355 "set data style linespoints\n"
356 "set nogrid\nset nokey\nset nopolar\n"))
358 (insert "set surface\nset nocontour\n"
359 "set " (if calc-graph-is-splot "" "no") "parametric\n"
360 "set notime\nset border\nset ztics\nset zeroaxis\n"
361 "set view 60,30,1,1\nset offsets 0,0,0,0\n"))
362 (setq samples-pos (point))
364 (goto-char (point-min))
365 (if calc-graph-is-splot
366 (if calc-graph-refine
367 (error "This option works only for 2d plots")
369 (let ((calc-gnuplot-input (current-buffer))
370 (calc-graph-no-auto-view t))
372 (setq device calc-gnuplot-print-device
373 output calc-gnuplot-print-output)
374 (setq device (calc-graph-find-command "terminal")
375 output (calc-graph-find-command "output"))
377 (setq device calc-gnuplot-default-device))
379 (setq output (car (read-from-string output)))
380 (setq output calc-gnuplot-default-output)))
381 (if (or (equal device "") (equal device "default"))
382 (setq device (if printing
384 (if (or (eq window-system 'x) (getenv "DISPLAY"))
386 (if (>= calc-gnuplot-version 3)
387 "dumb" "postscript")))))
388 (if (equal device "dumb")
389 (setq device (format "dumb %d %d"
390 (1- (frame-width)) (1- (frame-height)))))
391 (if (equal device "big")
392 (setq device (format "dumb %d %d"
393 (* 4 (- (frame-width) 3))
394 (* 4 (- (frame-height) 3)))))
396 (if (or (equal output "auto")
397 (and (equal output "tty") (setq tty-output t)))
398 (setq tempoutfile (calc-temp-file-name -1)
400 (setq output (eval output)))
401 (or (equal device calc-graph-last-device)
403 (setq calc-graph-last-device device)
404 (calc-gnuplot-command "set terminal" device)))
405 (or (equal output calc-graph-last-output)
407 (setq calc-graph-last-output output)
408 (calc-gnuplot-command "set output"
409 (if (equal output "STDOUT")
411 (prin1-to-string output)))))
412 (setq calc-graph-resolution (calc-graph-find-command "samples"))
413 (if calc-graph-resolution
414 (setq calc-graph-resolution (string-to-int calc-graph-resolution))
415 (setq calc-graph-resolution (if calc-graph-is-splot
416 calc-graph-default-resolution-3d
417 calc-graph-default-resolution)))
418 (setq precision (calc-graph-find-command "precision"))
420 (setq precision (string-to-int precision))
421 (setq precision calc-graph-default-precision))
422 (calc-graph-set-command "terminal")
423 (calc-graph-set-command "output")
424 (calc-graph-set-command "samples")
425 (calc-graph-set-command "precision"))
426 (goto-char samples-pos)
427 (insert "set samples " (int-to-string (max (if calc-graph-is-splot 20 200)
428 (+ 5 calc-graph-resolution))) "\n")
429 (while (re-search-forward "{\\*[^}]+}[^,\n]*" nil t)
430 (delete-region (match-beginning 0) (match-end 0))
433 (while (memq (preceding-char) '(?\s ?\t))
435 (if (eq (preceding-char) ?\,)
436 (delete-backward-char 1))))
439 (setq cache-env (list calc-angle-mode
444 precision calc-graph-is-splot))
445 (if (and (not recompute)
446 (equal (cdr (car calc-graph-data-cache)) cache-env))
447 (while (> (length calc-graph-data-cache)
448 calc-graph-data-cache-limit)
449 (setcdr calc-graph-data-cache
450 (cdr (cdr calc-graph-data-cache))))
451 (setq calc-graph-data-cache (list (cons nil cache-env)))))
452 (calc-graph-find-plot t t)
453 (while (re-search-forward
454 (if calc-graph-is-splot
455 "{\\([^{}:\n]+\\):\\([^{}:\n]+\\):\\([^{}:\n]+\\)}"
456 "{\\([^{}:\n]+\\)\\(:\\)\\([^{}:\n]+\\)}")
458 (setq calc-graph-curve-num (1+ calc-graph-curve-num))
459 (let* ((calc-graph-xname (buffer-substring (match-beginning 1) (match-end 1)))
460 (xvar (intern (concat "var-" calc-graph-xname)))
461 (calc-graph-xvalue (math-evaluate-expr (calc-var-value xvar)))
462 (calc-graph-y3name (and calc-graph-is-splot
463 (buffer-substring (match-beginning 2)
465 (y3var (and calc-graph-is-splot (intern (concat "var-" calc-graph-y3name))))
466 (calc-graph-y3value (and calc-graph-is-splot (calc-var-value y3var)))
467 (calc-graph-yname (buffer-substring (match-beginning 3) (match-end 3)))
468 (yvar (intern (concat "var-" calc-graph-yname)))
469 (calc-graph-yvalue (calc-var-value yvar))
471 (delete-region (match-beginning 0) (match-end 0))
472 (setq filename (calc-temp-file-name calc-graph-curve-num))
476 (calc-graph-xp calc-graph-xvalue)
477 (calc-graph-yp calc-graph-yvalue)
479 (calc-graph-xlow nil) (calc-graph-xhigh nil) (y3low nil) (y3high nil)
480 calc-graph-xvec calc-graph-xval calc-graph-xstep var-DUMMY
481 y3val calc-graph-y3step var-DUMMY2 (calc-graph-zval nil)
482 calc-graph-yvec calc-graph-yval calc-graph-ycache calc-graph-ycacheptr calc-graph-yvector
483 calc-graph-numsteps calc-graph-numsteps3
484 (calc-graph-keep-file (and (not calc-graph-is-splot) (file-exists-p filename)))
485 (calc-graph-stepcount 0)
486 (calc-symbolic-mode nil)
487 (calc-prefer-frac nil)
488 (calc-internal-prec (max 3 precision))
489 (calc-simplify-mode (and (not (memq calc-simplify-mode
493 (calc-graph-non-blank nil)
494 (math-working-step 0)
495 (math-working-step-2 nil))
497 (if calc-graph-is-splot
498 (calc-graph-compute-3d)
499 (calc-graph-compute-2d))
501 (goto-char (point-max))
502 (insert "\n" calc-graph-xname)
503 (if calc-graph-is-splot
504 (insert ":" calc-graph-y3name))
505 (insert ":" calc-graph-yname "\n\n")
506 (setq tempbuftop (point))
507 (let ((calc-group-digits nil)
508 (calc-leading-zeros nil)
509 (calc-number-radix 10)
510 (entry (and (not calc-graph-is-splot)
511 (list calc-graph-xp calc-graph-yp calc-graph-xhigh calc-graph-numsteps))))
513 (nth 1 (nth (1+ calc-graph-curve-num)
514 calc-graph-file-cache)))
515 (setq calc-graph-keep-file nil))
516 (setcar (cdr (nth (1+ calc-graph-curve-num) calc-graph-file-cache))
518 (or calc-graph-keep-file
519 (calc-graph-format-data)))
520 (or calc-graph-keep-file
522 (or calc-graph-non-blank
523 (error "No valid data points for %s:%s"
524 calc-graph-xname calc-graph-yname))
525 (write-region tempbuftop (point-max) filename
527 (insert (prin1-to-string filename))))
528 (if calc-graph-surprise-splot
529 (setcdr cache-env nil))
530 (if (= calc-graph-curve-num 0)
532 (calc-gnuplot-command "clear")
533 (calc-clear-command-flag 'clear-message)
534 (message "No data to plot!"))
535 (setq calc-graph-data-cache-limit (max calc-graph-curve-num
536 calc-graph-data-cache-limit)
537 filename (calc-temp-file-name 0))
538 (write-region (point-min) (point-max) filename nil 'quiet)
539 (calc-gnuplot-command "load" (prin1-to-string filename))
540 (or (equal output "STDOUT")
541 calc-gnuplot-keep-outfile
542 (progn ; need to close the output file before printing/plotting
543 (setq calc-graph-last-output "STDOUT")
544 (calc-gnuplot-command "set output")))
545 (let ((command (if printing
546 calc-gnuplot-print-command
547 (or calc-gnuplot-plot-command
548 (and (string-match "^dumb" device)
549 'calc-graph-show-dumb)
551 'calc-graph-show-tty)))))
553 (if (stringp command)
554 (calc-gnuplot-command
557 calc-gnuplot-print-output)))
558 (if (symbolp command)
559 (funcall command output)
560 (eval command))))))))))
562 (defun calc-graph-compute-2d ()
563 (if (setq calc-graph-yvec (eq (car-safe calc-graph-yvalue) 'vec))
564 (if (= (setq calc-graph-numsteps (1- (length calc-graph-yvalue))) 0)
565 (error "Can't plot an empty vector")
566 (if (setq calc-graph-xvec (eq (car-safe calc-graph-xvalue) 'vec))
567 (or (= (1- (length calc-graph-xvalue)) calc-graph-numsteps)
568 (error "%s and %s have different lengths" calc-graph-xname calc-graph-yname))
569 (if (and (eq (car-safe calc-graph-xvalue) 'intv)
570 (math-constp calc-graph-xvalue))
571 (setq calc-graph-xstep (math-div (math-sub (nth 3 calc-graph-xvalue)
572 (nth 2 calc-graph-xvalue))
573 (1- calc-graph-numsteps))
574 calc-graph-xvalue (nth 2 calc-graph-xvalue))
575 (if (math-realp calc-graph-xvalue)
576 (setq calc-graph-xstep 1)
577 (error "%s is not a suitable basis for %s" calc-graph-xname calc-graph-yname)))))
578 (or (math-realp calc-graph-yvalue)
580 (setq calc-graph-yvalue (math-evaluate-expr calc-graph-yvalue))
581 (calc-default-formula-arglist calc-graph-yvalue)
583 (error "%s does not contain any unassigned variables" calc-graph-yname))
585 (error "%s contains more than one variable: %s"
586 calc-graph-yname arglist))
587 (setq calc-graph-yvalue (math-expr-subst calc-graph-yvalue
588 (math-build-var-name (car arglist))
589 '(var DUMMY var-DUMMY)))))
590 (setq calc-graph-ycache (assoc calc-graph-yvalue calc-graph-data-cache))
591 (delq calc-graph-ycache calc-graph-data-cache)
592 (nconc calc-graph-data-cache
593 (list (or calc-graph-ycache (setq calc-graph-ycache (list calc-graph-yvalue)))))
594 (if (and (not (setq calc-graph-xvec (eq (car-safe calc-graph-xvalue) 'vec)))
595 calc-graph-refine (cdr (cdr calc-graph-ycache)))
596 (calc-graph-refine-2d)
597 (calc-graph-recompute-2d))))
599 (defun calc-graph-refine-2d ()
600 (setq calc-graph-keep-file nil
601 calc-graph-ycacheptr (cdr calc-graph-ycache))
602 (if (and (setq calc-graph-xval (calc-graph-find-command "xrange"))
603 (string-match "\\`\\[\\([0-9.eE+-]*\\):\\([0-9.eE+-]*\\)\\]\\'"
605 (let ((b2 (match-beginning 2))
607 (setq calc-graph-xlow (math-read-number (substring calc-graph-xval
610 calc-graph-xhigh (math-read-number (substring calc-graph-xval b2 e2))))
612 (while (and (cdr calc-graph-ycacheptr)
613 (Math-lessp (car (nth 1 calc-graph-ycacheptr)) calc-graph-xlow))
614 (setq calc-graph-ycacheptr (cdr calc-graph-ycacheptr)))))
615 (setq math-working-step-2 (1- (length calc-graph-ycacheptr)))
616 (while (and (cdr calc-graph-ycacheptr)
617 (or (not calc-graph-xhigh)
618 (Math-lessp (car (car calc-graph-ycacheptr)) calc-graph-xhigh)))
619 (setq var-DUMMY (math-div (math-add (car (car calc-graph-ycacheptr))
620 (car (nth 1 calc-graph-ycacheptr)))
622 math-working-step (1+ math-working-step)
623 calc-graph-yval (math-evaluate-expr calc-graph-yvalue))
624 (setcdr calc-graph-ycacheptr (cons (cons var-DUMMY calc-graph-yval)
625 (cdr calc-graph-ycacheptr)))
626 (setq calc-graph-ycacheptr (cdr (cdr calc-graph-ycacheptr))))
627 (setq calc-graph-yp calc-graph-ycache
628 calc-graph-numsteps 1000000))
630 (defun calc-graph-recompute-2d ()
631 (setq calc-graph-ycacheptr calc-graph-ycache)
633 (setq calc-graph-numsteps (1- (length calc-graph-xvalue))
634 calc-graph-yvector nil)
635 (if (and (eq (car-safe calc-graph-xvalue) 'intv)
636 (math-constp calc-graph-xvalue))
637 (setq calc-graph-numsteps calc-graph-resolution
639 calc-graph-xlow (nth 2 calc-graph-xvalue)
640 calc-graph-xhigh (nth 3 calc-graph-xvalue)
641 calc-graph-xstep (math-div (math-sub calc-graph-xhigh calc-graph-xlow)
642 (1- calc-graph-numsteps))
643 calc-graph-xvalue (nth 2 calc-graph-xvalue))
644 (error "%s is not a suitable basis for %s"
645 calc-graph-xname calc-graph-yname)))
646 (setq math-working-step-2 calc-graph-numsteps)
647 (while (>= (setq calc-graph-numsteps (1- calc-graph-numsteps)) 0)
648 (setq math-working-step (1+ math-working-step))
651 (setq calc-graph-xp (cdr calc-graph-xp)
652 calc-graph-xval (car calc-graph-xp))
653 (and (not (eq calc-graph-ycacheptr calc-graph-ycache))
654 (consp (car calc-graph-ycacheptr))
655 (not (Math-lessp (car (car calc-graph-ycacheptr)) calc-graph-xval))
656 (setq calc-graph-ycacheptr calc-graph-ycache)))
657 (if (= calc-graph-numsteps 0)
658 (setq calc-graph-xval calc-graph-xhigh) ; avoid cumulative roundoff
659 (setq calc-graph-xval calc-graph-xvalue
660 calc-graph-xvalue (math-add calc-graph-xvalue calc-graph-xstep))))
661 (while (and (cdr calc-graph-ycacheptr)
662 (Math-lessp (car (nth 1 calc-graph-ycacheptr)) calc-graph-xval))
663 (setq calc-graph-ycacheptr (cdr calc-graph-ycacheptr)))
664 (or (and (cdr calc-graph-ycacheptr)
665 (Math-equal (car (nth 1 calc-graph-ycacheptr)) calc-graph-xval))
667 (setq calc-graph-keep-file nil
668 var-DUMMY calc-graph-xval)
669 (setcdr calc-graph-ycacheptr (cons (cons calc-graph-xval (math-evaluate-expr calc-graph-yvalue))
670 (cdr calc-graph-ycacheptr)))))
671 (setq calc-graph-ycacheptr (cdr calc-graph-ycacheptr))
673 (setq calc-graph-yvector (cons (cdr (car calc-graph-ycacheptr)) calc-graph-yvector))
674 (or calc-graph-yp (setq calc-graph-yp calc-graph-ycacheptr))))
676 (setq calc-graph-xp calc-graph-xvalue
678 calc-graph-yp (cons 'vec (nreverse calc-graph-yvector))
679 calc-graph-numsteps (1- (length calc-graph-xp)))
680 (setq calc-graph-numsteps 1000000)))
682 (defun calc-graph-compute-3d ()
683 (if (setq calc-graph-yvec (eq (car-safe calc-graph-yvalue) 'vec))
684 (if (math-matrixp calc-graph-yvalue)
686 (setq calc-graph-numsteps (1- (length calc-graph-yvalue))
687 calc-graph-numsteps3 (1- (length (nth 1 calc-graph-yvalue))))
688 (if (eq (car-safe calc-graph-xvalue) 'vec)
689 (or (= (1- (length calc-graph-xvalue)) calc-graph-numsteps)
690 (error "%s has wrong length" calc-graph-xname))
691 (if (and (eq (car-safe calc-graph-xvalue) 'intv)
692 (math-constp calc-graph-xvalue))
693 (setq calc-graph-xvalue (calcFunc-index calc-graph-numsteps
694 (nth 2 calc-graph-xvalue)
696 (math-sub (nth 3 calc-graph-xvalue)
697 (nth 2 calc-graph-xvalue))
698 (1- calc-graph-numsteps))))
699 (if (math-realp calc-graph-xvalue)
700 (setq calc-graph-xvalue (calcFunc-index calc-graph-numsteps calc-graph-xvalue 1))
701 (error "%s is not a suitable basis for %s" calc-graph-xname calc-graph-yname))))
702 (if (eq (car-safe calc-graph-y3value) 'vec)
703 (or (= (1- (length calc-graph-y3value)) calc-graph-numsteps3)
704 (error "%s has wrong length" calc-graph-y3name))
705 (if (and (eq (car-safe calc-graph-y3value) 'intv)
706 (math-constp calc-graph-y3value))
707 (setq calc-graph-y3value (calcFunc-index calc-graph-numsteps3
708 (nth 2 calc-graph-y3value)
710 (math-sub (nth 3 calc-graph-y3value)
711 (nth 2 calc-graph-y3value))
712 (1- calc-graph-numsteps3))))
713 (if (math-realp calc-graph-y3value)
714 (setq calc-graph-y3value (calcFunc-index calc-graph-numsteps3 calc-graph-y3value 1))
715 (error "%s is not a suitable basis for %s" calc-graph-y3name calc-graph-yname))))
716 (setq calc-graph-xp nil
720 (while (setq calc-graph-xvalue (cdr calc-graph-xvalue) calc-graph-yvalue (cdr calc-graph-yvalue))
721 (setq calc-graph-xp (nconc calc-graph-xp (make-list (1+ calc-graph-numsteps3) (car calc-graph-xvalue)))
722 calc-graph-yp (nconc calc-graph-yp (cons 0 (copy-sequence (cdr calc-graph-y3value))))
723 calc-graph-zp (nconc calc-graph-zp (cons '(skip)
724 (copy-sequence (cdr (car calc-graph-yvalue)))))))
725 (setq calc-graph-numsteps (1- (* calc-graph-numsteps
726 (1+ calc-graph-numsteps3)))))
727 (if (= (setq calc-graph-numsteps (1- (length calc-graph-yvalue))) 0)
728 (error "Can't plot an empty vector"))
729 (or (and (eq (car-safe calc-graph-xvalue) 'vec)
730 (= (1- (length calc-graph-xvalue)) calc-graph-numsteps))
731 (error "%s is not a suitable basis for %s" calc-graph-xname calc-graph-yname))
732 (or (and (eq (car-safe calc-graph-y3value) 'vec)
733 (= (1- (length calc-graph-y3value)) calc-graph-numsteps))
734 (error "%s is not a suitable basis for %s" calc-graph-y3name calc-graph-yname))
735 (setq calc-graph-xp calc-graph-xvalue
736 calc-graph-yp calc-graph-y3value
737 calc-graph-zp calc-graph-yvalue
739 (or (math-realp calc-graph-yvalue)
741 (setq calc-graph-yvalue (math-evaluate-expr calc-graph-yvalue))
742 (calc-default-formula-arglist calc-graph-yvalue)
743 (setq arglist (sort arglist 'string-lessp))
745 (error "%s does not contain enough unassigned variables" calc-graph-yname))
746 (and (cdr (cdr arglist))
747 (error "%s contains too many variables: %s" calc-graph-yname arglist))
748 (setq calc-graph-yvalue (math-multi-subst calc-graph-yvalue
749 (mapcar 'math-build-var-name
751 '((var DUMMY var-DUMMY)
752 (var DUMMY2 var-DUMMY2))))))
753 (if (setq calc-graph-xvec (eq (car-safe calc-graph-xvalue) 'vec))
754 (setq calc-graph-numsteps (1- (length calc-graph-xvalue)))
755 (if (and (eq (car-safe calc-graph-xvalue) 'intv)
756 (math-constp calc-graph-xvalue))
757 (setq calc-graph-numsteps calc-graph-resolution
758 calc-graph-xvalue (calcFunc-index calc-graph-numsteps
759 (nth 2 calc-graph-xvalue)
760 (math-div (math-sub (nth 3 calc-graph-xvalue)
761 (nth 2 calc-graph-xvalue))
762 (1- calc-graph-numsteps))))
763 (error "%s is not a suitable basis for %s"
764 calc-graph-xname calc-graph-yname)))
765 (if (eq (car-safe calc-graph-y3value) 'vec)
766 (setq calc-graph-numsteps3 (1- (length calc-graph-y3value)))
767 (if (and (eq (car-safe calc-graph-y3value) 'intv)
768 (math-constp calc-graph-y3value))
769 (setq calc-graph-numsteps3 calc-graph-resolution
770 calc-graph-y3value (calcFunc-index calc-graph-numsteps3
771 (nth 2 calc-graph-y3value)
772 (math-div (math-sub (nth 3 calc-graph-y3value)
773 (nth 2 calc-graph-y3value))
774 (1- calc-graph-numsteps3))))
775 (error "%s is not a suitable basis for %s"
776 calc-graph-y3name calc-graph-yname)))
777 (setq calc-graph-xp nil
781 (setq math-working-step 0)
782 (while (setq calc-graph-xvalue (cdr calc-graph-xvalue))
783 (setq calc-graph-xp (nconc calc-graph-xp (make-list (1+ calc-graph-numsteps3) (car calc-graph-xvalue)))
784 calc-graph-yp (nconc calc-graph-yp (cons 0 (copy-sequence (cdr calc-graph-y3value))))
785 calc-graph-zp (cons '(skip) calc-graph-zp)
786 calc-graph-y3step calc-graph-y3value
787 var-DUMMY (car calc-graph-xvalue)
788 math-working-step-2 0
789 math-working-step (1+ math-working-step))
790 (while (setq calc-graph-y3step (cdr calc-graph-y3step))
791 (setq math-working-step-2 (1+ math-working-step-2)
792 var-DUMMY2 (car calc-graph-y3step)
793 calc-graph-zp (cons (math-evaluate-expr calc-graph-yvalue) calc-graph-zp))))
794 (setq calc-graph-zp (nreverse calc-graph-zp)
795 calc-graph-numsteps (1- (* calc-graph-numsteps (1+ calc-graph-numsteps3))))))
797 (defun calc-graph-format-data ()
798 (while (<= (setq calc-graph-stepcount (1+ calc-graph-stepcount)) calc-graph-numsteps)
800 (setq calc-graph-xp (cdr calc-graph-xp)
801 calc-graph-xval (car calc-graph-xp)
802 calc-graph-yp (cdr calc-graph-yp)
803 calc-graph-yval (car calc-graph-yp)
804 calc-graph-zp (cdr calc-graph-zp)
805 calc-graph-zval (car calc-graph-zp))
807 (setq calc-graph-xval calc-graph-xvalue
808 calc-graph-xvalue (math-add calc-graph-xvalue calc-graph-xstep)
809 calc-graph-yp (cdr calc-graph-yp)
810 calc-graph-yval (car calc-graph-yp))
811 (setq calc-graph-xval (car (car calc-graph-yp))
812 calc-graph-yval (cdr (car calc-graph-yp))
813 calc-graph-yp (cdr calc-graph-yp))
814 (if (or (not calc-graph-yp)
815 (and calc-graph-xhigh (equal calc-graph-xval calc-graph-xhigh)))
816 (setq calc-graph-numsteps 0))))
817 (if calc-graph-is-splot
818 (if (and (eq (car-safe calc-graph-zval) 'calcFunc-xyz)
819 (= (length calc-graph-zval) 4))
820 (setq calc-graph-xval (nth 1 calc-graph-zval)
821 calc-graph-yval (nth 2 calc-graph-zval)
822 calc-graph-zval (nth 3 calc-graph-zval)))
823 (if (and (eq (car-safe calc-graph-yval) 'calcFunc-xyz)
824 (= (length calc-graph-yval) 4))
826 (or calc-graph-surprise-splot
828 (set-buffer (get-buffer-create "*Gnuplot Temp*"))
830 (goto-char (point-max))
831 (re-search-backward "^plot[ \t]")
832 (insert "set parametric\ns")
833 (setq calc-graph-surprise-splot t))))
834 (setq calc-graph-xval (nth 1 calc-graph-yval)
835 calc-graph-zval (nth 3 calc-graph-yval)
836 calc-graph-yval (nth 2 calc-graph-yval)))
837 (if (and (eq (car-safe calc-graph-yval) 'calcFunc-xy)
838 (= (length calc-graph-yval) 3))
839 (setq calc-graph-xval (nth 1 calc-graph-yval)
840 calc-graph-yval (nth 2 calc-graph-yval)))))
841 (if (and (Math-realp calc-graph-xval)
842 (Math-realp calc-graph-yval)
843 (or (not calc-graph-zval) (Math-realp calc-graph-zval)))
845 (setq calc-graph-blank nil
846 calc-graph-non-blank t)
847 (if (Math-integerp calc-graph-xval)
848 (insert (math-format-number calc-graph-xval))
849 (if (eq (car calc-graph-xval) 'frac)
850 (setq calc-graph-xval (math-float calc-graph-xval)))
851 (insert (math-format-number (nth 1 calc-graph-xval))
852 "e" (int-to-string (nth 2 calc-graph-xval))))
854 (if (Math-integerp calc-graph-yval)
855 (insert (math-format-number calc-graph-yval))
856 (if (eq (car calc-graph-yval) 'frac)
857 (setq calc-graph-yval (math-float calc-graph-yval)))
858 (insert (math-format-number (nth 1 calc-graph-yval))
859 "e" (int-to-string (nth 2 calc-graph-yval))))
863 (if (Math-integerp calc-graph-zval)
864 (insert (math-format-number calc-graph-zval))
865 (if (eq (car calc-graph-zval) 'frac)
866 (setq calc-graph-zval (math-float calc-graph-zval)))
867 (insert (math-format-number (nth 1 calc-graph-zval))
868 "e" (int-to-string (nth 2 calc-graph-zval))))))
870 (and (not (equal calc-graph-zval '(skip)))
871 (eq (car-safe var-PlotRejects) 'vec)
872 (nconc var-PlotRejects
876 calc-graph-xval calc-graph-yval)))
877 (calc-refresh-evaltos 'var-PlotRejects))
881 (setq calc-graph-blank t))))))
883 (defun calc-temp-file-name (num)
884 (while (<= (length calc-graph-file-cache) (1+ num))
885 (setq calc-graph-file-cache (nconc calc-graph-file-cache (list nil))))
886 (car (or (nth (1+ num) calc-graph-file-cache)
887 (setcar (nthcdr (1+ num) calc-graph-file-cache)
888 (list (make-temp-file
889 (concat calc-gnuplot-tempfile
891 (char-to-string (- ?A num))
892 (int-to-string num))))
895 (defun calc-graph-delete-temps ()
896 (while calc-graph-file-cache
897 (and (car calc-graph-file-cache)
898 (file-exists-p (car (car calc-graph-file-cache)))
900 (delete-file (car (car calc-graph-file-cache)))
902 (setq calc-graph-file-cache (cdr calc-graph-file-cache))))
904 (defun calc-graph-kill-hook ()
905 (calc-graph-delete-temps))
907 (defun calc-graph-show-tty (output)
908 "Default calc-gnuplot-plot-command for \"tty\" output mode.
909 This is useful for tek40xx and other graphics-terminal types."
910 (call-process-region 1 1 shell-file-name
911 nil calc-gnuplot-buffer nil
912 "-c" (format "cat %s >/dev/tty; rm %s" output output)))
914 (defvar calc-dumb-map nil
915 "The keymap for the \"dumb\" terminal plot.")
917 (defun calc-graph-show-dumb (&optional output)
918 "Default calc-gnuplot-plot-command for Pinard's \"dumb\" terminal type.
919 This \"dumb\" driver will be present in Gnuplot 3.0."
921 (save-window-excursion
922 (switch-to-buffer calc-gnuplot-buffer)
923 (delete-other-windows)
924 (goto-char calc-gnuplot-trail-mark)
925 (or (search-forward "\f" nil t)
927 (goto-char (point-max))
928 (re-search-backward "\f\\|^[ \t]+\\^$\\|G N U P L O T")
929 (if (looking-at "\f")
932 (if (eolp) (forward-line 1))
933 (or (calc-graph-find-command "time")
934 (calc-graph-find-command "title")
935 (calc-graph-find-command "ylabel")
937 (insert-before-markers (format "(%s)" (current-time-string)))
939 (set-window-start (selected-window) (point))
940 (goto-char (point-max)))
946 (setq calc-dumb-map (make-sparse-keymap))
947 (define-key calc-dumb-map "\n" 'scroll-up)
948 (define-key calc-dumb-map " " 'scroll-up)
949 (define-key calc-dumb-map "\177" 'scroll-down)
950 (define-key calc-dumb-map "<" 'scroll-left)
951 (define-key calc-dumb-map ">" 'scroll-right)
952 (define-key calc-dumb-map "{" 'scroll-down)
953 (define-key calc-dumb-map "}" 'scroll-up)
954 (define-key calc-dumb-map "q" 'exit-recursive-edit)
955 (define-key calc-dumb-map "\C-c\C-c" 'exit-recursive-edit)))
956 (use-local-map calc-dumb-map)
957 (setq truncate-lines t)
958 (message "Type `q' or `C-c C-c' to return to Calc")
960 (bury-buffer "*Gnuplot Trail*")))
962 (defun calc-graph-clear ()
964 (if calc-graph-last-device
965 (if (or (equal calc-graph-last-device "x11")
966 (equal calc-graph-last-device "X11"))
967 (calc-gnuplot-command "set output"
968 (if (equal calc-graph-last-output "STDOUT")
970 (prin1-to-string calc-graph-last-output)))
971 (calc-gnuplot-command "clear"))))
973 (defun calc-graph-title-x (title)
974 (interactive "sX axis title: ")
975 (calc-graph-set-command "xlabel" (if (not (equal title ""))
976 (prin1-to-string title))))
978 (defun calc-graph-title-y (title)
979 (interactive "sY axis title: ")
980 (calc-graph-set-command "ylabel" (if (not (equal title ""))
981 (prin1-to-string title))))
983 (defun calc-graph-title-z (title)
984 (interactive "sZ axis title: ")
985 (calc-graph-set-command "zlabel" (if (not (equal title ""))
986 (prin1-to-string title))))
988 (defun calc-graph-range-x (range)
989 (interactive "sX axis range: ")
990 (calc-graph-set-range "xrange" range))
992 (defun calc-graph-range-y (range)
993 (interactive "sY axis range: ")
994 (calc-graph-set-range "yrange" range))
996 (defun calc-graph-range-z (range)
997 (interactive "sZ axis range: ")
998 (calc-graph-set-range "zrange" range))
1000 (defun calc-graph-set-range (cmd range)
1001 (if (equal range "$")
1003 (let ((val (calc-top-n 1)))
1004 (if (and (eq (car-safe val) 'intv) (math-constp val))
1006 (math-format-number (math-float (nth 2 val))) ":"
1007 (math-format-number (math-float (nth 3 val)))))
1008 (if (and (eq (car-safe val) 'vec)
1011 (math-format-number (math-float (nth 1 val))) ":"
1012 (math-format-number (math-float (nth 2 val)))))
1013 (error "Range specification must be an interval or 2-vector")))
1014 (calc-pop-stack 1))))
1015 (if (string-match "\\[.+\\]" range)
1016 (setq range (substring range 1 -1)))
1017 (if (and (not (string-match ":" range))
1018 (or (string-match "," range)
1019 (string-match " " range)))
1020 (aset range (match-beginning 0) ?\:))
1021 (calc-graph-set-command cmd (if (not (equal range ""))
1022 (concat "[" range "]"))))
1024 (defun calc-graph-log-x (flag)
1026 (calc-graph-set-log flag 0 0))
1028 (defun calc-graph-log-y (flag)
1030 (calc-graph-set-log 0 flag 0))
1032 (defun calc-graph-log-z (flag)
1034 (calc-graph-set-log 0 0 flag))
1036 (defun calc-graph-set-log (xflag yflag zflag)
1037 (let* ((old (or (calc-graph-find-command "logscale") ""))
1038 (xold (string-match "x" old))
1039 (yold (string-match "y" old))
1040 (zold (string-match "z" old))
1042 (setq str (concat (if (if xflag
1043 (if (eq xflag 0) xold
1044 (> (prefix-numeric-value xflag) 0))
1047 (if (eq yflag 0) yold
1048 (> (prefix-numeric-value yflag) 0))
1051 (if (eq zflag 0) zold
1052 (> (prefix-numeric-value zflag) 0))
1053 (not zold)) "z" "")))
1054 (calc-graph-set-command "logscale" (if (not (equal str "")) str))))
1056 (defun calc-graph-line-style (style)
1058 (calc-graph-set-styles (and style (prefix-numeric-value style)) t))
1060 (defun calc-graph-point-style (style)
1062 (calc-graph-set-styles t (and style (prefix-numeric-value style))))
1064 (defun calc-graph-set-styles (lines points)
1067 (set-buffer calc-gnuplot-input)
1068 (or (calc-graph-find-plot nil nil)
1069 (error "No data points have been set!"))
1070 (let ((base (point))
1071 (mode nil) (lstyle nil) (pstyle nil)
1072 start end lenbl penbl)
1073 (re-search-forward "[,\n]")
1075 (setq end (point) start end)
1077 (if (looking-at "[^,\n]*[^,\n \t]\\([ \t]+with\\)")
1079 (setq start (match-beginning 1))
1080 (goto-char (match-end 0))
1081 (if (looking-at "[ \t]+\\([a-z]+\\)")
1082 (setq mode (buffer-substring (match-beginning 1)
1084 (if (looking-at "[ \ta-z]+\\([0-9]+\\)")
1085 (setq lstyle (string-to-int
1086 (buffer-substring (match-beginning 1)
1088 (if (looking-at "[ \ta-z]+[0-9]+[ \t]+\\([0-9]+\\)")
1089 (setq pstyle (string-to-int
1090 (buffer-substring (match-beginning 1)
1092 (setq lenbl (or (equal mode "lines") (equal mode "linespoints"))
1093 penbl (or (equal mode "points") (equal mode "linespoints")))
1097 lenbl (>= lines 0)))
1098 (setq lenbl (not lenbl)))
1102 penbl (>= points 0)))
1103 (setq penbl (not penbl)))
1104 (delete-region start end)
1108 (if penbl "linespoints" "lines")
1109 (if penbl "points" "dots")))
1110 (if (and pstyle (> pstyle 0))
1111 (insert " " (if (and lstyle (> lstyle 0)) (int-to-string lstyle) "1")
1112 " " (int-to-string pstyle))
1113 (if (and lstyle (> lstyle 0))
1114 (insert " " (int-to-string lstyle))))))
1115 (calc-graph-view-commands))
1117 (defun calc-graph-zero-x (flag)
1119 (calc-graph-set-command "noxzeroaxis"
1121 (<= (prefix-numeric-value flag) 0)
1122 (not (calc-graph-find-command "noxzeroaxis")))
1125 (defun calc-graph-zero-y (flag)
1127 (calc-graph-set-command "noyzeroaxis"
1129 (<= (prefix-numeric-value flag) 0)
1130 (not (calc-graph-find-command "noyzeroaxis")))
1133 (defun calc-graph-name (name)
1134 (interactive "sTitle for current curve: ")
1137 (set-buffer calc-gnuplot-input)
1138 (or (calc-graph-find-plot nil nil)
1139 (error "No data points have been set!"))
1140 (let ((base (point))
1143 (re-search-forward "[,\n]\\|[ \t]+with")
1144 (setq end (match-beginning 0))
1146 (if (looking-at "[^,\n]*[^,\n \t]\\([ \t]+title\\)")
1148 (goto-char (match-beginning 1))
1149 (delete-region (point) end))
1151 (insert " title " (prin1-to-string name))))
1152 (calc-graph-view-commands))
1154 (defun calc-graph-hide (flag)
1157 (and (calc-graph-find-plot nil nil)
1159 (or (looking-at "{")
1160 (error "Can't hide this curve (wrong format)"))
1162 (if (looking-at "*")
1163 (if (or (null flag) (<= (prefix-numeric-value flag) 0))
1165 (if (or (null flag) (> (prefix-numeric-value flag) 0))
1168 (defun calc-graph-header (title)
1169 (interactive "sTitle for entire graph: ")
1170 (calc-graph-set-command "title" (if (not (equal title ""))
1171 (prin1-to-string title))))
1173 (defun calc-graph-border (flag)
1175 (calc-graph-set-command "noborder"
1177 (<= (prefix-numeric-value flag) 0)
1178 (not (calc-graph-find-command "noborder")))
1181 (defun calc-graph-grid (flag)
1183 (calc-graph-set-command "grid" (and (if flag
1184 (> (prefix-numeric-value flag) 0)
1185 (not (calc-graph-find-command "grid")))
1188 (defun calc-graph-key (flag)
1190 (calc-graph-set-command "key" (and (if flag
1191 (> (prefix-numeric-value flag) 0)
1192 (not (calc-graph-find-command "key")))
1195 (defun calc-graph-num-points (res flag)
1196 (interactive "sNumber of data points: \nP")
1198 (if (> (prefix-numeric-value flag) 0)
1200 (message "Default resolution is %d"
1201 calc-graph-default-resolution)
1202 (setq calc-graph-default-resolution (string-to-int res)))
1204 (message "Default 3D resolution is %d"
1205 calc-graph-default-resolution-3d)
1206 (setq calc-graph-default-resolution-3d (string-to-int res))))
1207 (calc-graph-set-command "samples" (if (not (equal res "")) res))))
1209 (defun calc-graph-device (name flag)
1210 (interactive "sDevice name: \nP")
1211 (if (equal name "?")
1213 (calc-gnuplot-command "set terminal")
1214 (calc-graph-view-trail))
1216 (if (> (prefix-numeric-value flag) 0)
1218 (message "Default GNUPLOT device is \"%s\""
1219 calc-gnuplot-default-device)
1220 (setq calc-gnuplot-default-device name))
1222 (message "GNUPLOT device for Print command is \"%s\""
1223 calc-gnuplot-print-device)
1224 (setq calc-gnuplot-print-device name)))
1225 (calc-graph-set-command "terminal" (if (not (equal name ""))
1228 (defun calc-graph-output (name flag)
1229 (interactive "FOutput file name: \np")
1230 (cond ((string-match "\\<[aA][uU][tT][oO]$" name)
1232 ((string-match "\\<[tT][tT][yY]$" name)
1234 ((string-match "\\<[sS][tT][dD][oO][uU][tT]$" name)
1235 (setq name "STDOUT"))
1236 ((equal (file-name-nondirectory name) "")
1238 (t (setq name (expand-file-name name))))
1240 (if (> (prefix-numeric-value flag) 0)
1242 (message "Default GNUPLOT output file is \"%s\""
1243 calc-gnuplot-default-output)
1244 (setq calc-gnuplot-default-output name))
1246 (message "GNUPLOT output file for Print command is \"%s\""
1247 calc-gnuplot-print-output)
1248 (setq calc-gnuplot-print-output name)))
1249 (calc-graph-set-command "output" (if (not (equal name ""))
1250 (prin1-to-string name)))))
1252 (defun calc-graph-display (name)
1253 (interactive "sX display name: ")
1255 (message "Current X display is \"%s\""
1256 (or calc-gnuplot-display "<none>"))
1257 (setq calc-gnuplot-display name)
1258 (if (calc-gnuplot-alive)
1259 (calc-gnuplot-command "exit"))))
1261 (defun calc-graph-geometry (name)
1262 (interactive "sX geometry spec (or \"default\"): ")
1264 (message "Current X geometry is \"%s\""
1265 (or calc-gnuplot-geometry "default"))
1266 (setq calc-gnuplot-geometry (and (not (equal name "default")) name))
1267 (if (calc-gnuplot-alive)
1268 (calc-gnuplot-command "exit"))))
1270 (defun calc-graph-find-command (cmd)
1273 (set-buffer calc-gnuplot-input)
1274 (goto-char (point-min))
1275 (if (re-search-forward (concat "^set[ \t]+" cmd "[ \t]*\\(.*\\)$") nil t)
1276 (buffer-substring (match-beginning 1) (match-end 1)))))
1278 (defun calc-graph-set-command (cmd &rest args)
1281 (set-buffer calc-gnuplot-input)
1282 (goto-char (point-min))
1283 (if (re-search-forward (concat "^set[ \t]+" cmd "[ \t\n]") nil t)
1287 (let ((end (point)))
1289 (delete-region (point) (1+ end))))
1290 (if (calc-graph-find-plot t t)
1291 (if (eq (preceding-char) ?\n)
1293 (goto-char (1- (point-max)))))
1294 (if (and args (car args))
1298 (insert "set " (mapconcat 'identity (cons cmd args) " ") "\n"))))
1299 (calc-graph-view-commands))
1301 (defun calc-graph-command (cmd)
1302 (interactive "sGNUPLOT command: ")
1305 (calc-graph-view-trail)
1306 (calc-gnuplot-command cmd)
1307 (accept-process-output)
1308 (calc-graph-view-trail)))
1310 (defun calc-graph-kill (&optional no-view)
1312 (calc-graph-delete-temps)
1313 (if (calc-gnuplot-alive)
1315 (or no-view (calc-graph-view-trail))
1316 (let ((calc-graph-no-wait t))
1317 (calc-gnuplot-command "exit"))
1319 (if (process-status calc-gnuplot-process)
1320 (delete-process calc-gnuplot-process))
1321 (setq calc-gnuplot-process nil))))
1323 (defun calc-graph-quit ()
1325 (if (get-buffer-window calc-gnuplot-input)
1326 (calc-graph-view-commands t))
1327 (if (get-buffer-window calc-gnuplot-buffer)
1328 (calc-graph-view-trail t))
1329 (calc-graph-kill t))
1331 (defun calc-graph-view-commands (&optional no-need)
1333 (or calc-graph-no-auto-view (calc-graph-init-buffers))
1334 (calc-graph-view calc-gnuplot-input calc-gnuplot-buffer (null no-need)))
1336 (defun calc-graph-view-trail (&optional no-need)
1338 (or calc-graph-no-auto-view (calc-graph-init-buffers))
1339 (calc-graph-view calc-gnuplot-buffer calc-gnuplot-input (null no-need)))
1341 (defun calc-graph-view (buf other-buf need)
1343 (or calc-graph-no-auto-view
1344 (if (setq win (get-buffer-window buf))
1346 (and (eq buf calc-gnuplot-buffer)
1349 (not (pos-visible-in-window-p (point-max) win))))
1352 (bury-buffer other-buf)
1353 (let ((curwin (selected-window)))
1355 (switch-to-buffer nil)
1356 (select-window curwin))))
1357 (if (setq win (get-buffer-window other-buf))
1358 (set-window-buffer win buf)
1359 (if (eq major-mode 'calc-mode)
1361 (< (window-height) (1- (frame-height))))
1362 (display-buffer buf))
1363 (switch-to-buffer buf)))))
1366 (if (and (eq buf calc-gnuplot-buffer)
1367 (setq win (get-buffer-window buf))
1368 (not (pos-visible-in-window-p (point-max) win)))
1370 (goto-char (point-max))
1371 (vertical-motion (- 6 (window-height win)))
1372 (set-window-start win (point))
1373 (goto-char (point-max)))))
1374 (or calc-graph-no-auto-view (sit-for 0))))
1376 (defun calc-gnuplot-check-for-errors ()
1380 (set-buffer calc-gnuplot-buffer)
1381 (goto-char calc-gnuplot-last-error-pos))
1382 (re-search-forward "^[ \t]+\\^$" nil t)
1383 (goto-char (point-max))
1384 (setq calc-gnuplot-last-error-pos (point-max))))
1385 (calc-graph-view-trail)))
1387 (defun calc-gnuplot-command (&rest args)
1389 (let ((cmd (concat (mapconcat 'identity args " ") "\n")))
1390 (accept-process-output)
1392 (set-buffer calc-gnuplot-buffer)
1393 (calc-gnuplot-check-for-errors)
1394 (goto-char (point-max))
1395 (setq calc-gnuplot-trail-mark (point))
1396 (or (>= calc-gnuplot-version 3)
1398 (set-marker (process-mark calc-gnuplot-process) (point))
1399 (process-send-string calc-gnuplot-process cmd)
1400 (if (get-buffer-window calc-gnuplot-buffer)
1401 (calc-graph-view-trail))
1402 (accept-process-output (and (not calc-graph-no-wait)
1403 calc-gnuplot-process))
1404 (calc-gnuplot-check-for-errors)
1405 (if (get-buffer-window calc-gnuplot-buffer)
1406 (calc-graph-view-trail)))))
1408 (defun calc-graph-init-buffers ()
1409 (or (and calc-gnuplot-buffer
1410 (buffer-name calc-gnuplot-buffer))
1411 (setq calc-gnuplot-buffer (get-buffer-create "*Gnuplot Trail*")))
1412 (or (and calc-gnuplot-input
1413 (buffer-name calc-gnuplot-input))
1414 (setq calc-gnuplot-input (get-buffer-create "*Gnuplot Commands*"))))
1416 (defun calc-graph-init ()
1417 (or (calc-gnuplot-alive)
1418 (let ((process-connection-type t)
1420 (if calc-gnuplot-process
1422 (delete-process calc-gnuplot-process)
1423 (setq calc-gnuplot-process nil)))
1424 (calc-graph-init-buffers)
1426 (set-buffer calc-gnuplot-buffer)
1427 (insert "\nStarting gnuplot...\n")
1428 (setq origin (point)))
1429 (setq calc-graph-last-device nil)
1430 (setq calc-graph-last-output nil)
1432 (let ((args (append (and calc-gnuplot-display
1433 (not (equal calc-gnuplot-display
1434 (getenv "DISPLAY")))
1436 calc-gnuplot-display))
1437 (and calc-gnuplot-geometry
1439 calc-gnuplot-geometry)))))
1440 (setq calc-gnuplot-process
1441 (apply 'start-process
1446 (set-process-query-on-exit-flag calc-gnuplot-process nil))
1448 (error "Sorry, can't find \"%s\" on your system"
1449 calc-gnuplot-name)))
1451 (set-buffer calc-gnuplot-buffer)
1452 (while (and (not (save-excursion
1454 (search-forward "gnuplot> " nil t)))
1455 (memq (process-status calc-gnuplot-process) '(run stop)))
1456 (accept-process-output calc-gnuplot-process))
1457 (or (memq (process-status calc-gnuplot-process) '(run stop))
1458 (error "Unable to start GNUPLOT process"))
1462 "G N U P L O T.*\n.*version \\([0-9]+\\)\\." nil t))
1463 (setq calc-gnuplot-version (string-to-int (buffer-substring
1466 (setq calc-gnuplot-version 1))
1467 (goto-char (point-max)))))
1469 (set-buffer calc-gnuplot-input)
1470 (if (= (buffer-size) 0)
1471 (insert "# Commands for running gnuplot\n\n\n")
1472 (or calc-graph-no-auto-view
1473 (eq (char-after (1- (point-max))) ?\n)
1475 (goto-char (point-max))
1478 (provide 'calc-graph)
1480 ;;; arch-tag: e4b06a52-c386-4d54-a2bb-7c0a0ef533c2
1481 ;;; calc-graph.el ends here