1 ;; Calculator for GNU Emacs, part II [calc-keypd.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-keypd () nil)
33 ;;; Pictorial interface to Calc using the X window system mouse.
35 (defvar calc-keypad-buffer nil)
36 (defvar calc-keypad-menu 0)
37 (defvar calc-keypad-full-layout nil)
38 (defvar calc-keypad-input nil)
39 (defvar calc-keypad-prev-input nil)
40 (defvar calc-keypad-prev-x-left-click nil)
41 (defvar calc-keypad-prev-x-middle-click nil)
42 (defvar calc-keypad-prev-x-right-click nil)
43 (defvar calc-keypad-said-hello nil)
45 (defvar calc-keypad-map nil)
48 (setq calc-keypad-map (make-sparse-keymap))
49 (define-key calc-keypad-map " " 'calc-keypad-press)
50 (define-key calc-keypad-map "\r" 'calc-keypad-press)
51 (define-key calc-keypad-map "\t" 'calc-keypad-menu)
52 (define-key calc-keypad-map "q" 'calc-keypad-off))
54 (defun calc-do-keypad (&optional full-display interactive)
55 (if (string-match "^19" emacs-version)
56 (error "Sorry, calc-keypad not yet implemented for Emacs 19"))
58 (let ((calcbuf (current-buffer)))
59 (or (and calc-keypad-buffer
60 (buffer-name calc-keypad-buffer))
62 (setq calc-keypad-buffer (get-buffer-create "*Calc Keypad*"))
63 (set-buffer calc-keypad-buffer)
64 (use-local-map calc-keypad-map)
65 (setq major-mode 'calc-keypad)
66 (setq mode-name "Calculator")
67 (put 'calc-keypad 'mode-class 'special)
68 (make-local-variable 'calc-main-buffer)
69 (setq calc-main-buffer calcbuf)
75 (if (setq win (get-buffer-window "*Calculator*"))
77 (if (setq win (get-buffer-window "*Calc Trail*"))
79 (switch-to-buffer (other-buffer))
81 (if (setq win (get-buffer-window calc-keypad-buffer))
83 (bury-buffer "*Calculator*")
84 (bury-buffer "*Calc Trail*")
85 (bury-buffer calc-keypad-buffer)
87 (switch-to-buffer (other-buffer))
89 (if (and calc-keypad-prev-x-left-click
90 (eq (aref mouse-map 0) 'calc-keypad-x-right-click)
91 (eq (aref mouse-map 1) 'calc-keypad-x-middle-click)
92 (eq (aref mouse-map 2) 'calc-keypad-x-left-click))
94 (aset mouse-map 0 calc-keypad-prev-x-right-click)
95 (aset mouse-map 1 calc-keypad-prev-x-middle-click)
96 (aset mouse-map 2 calc-keypad-prev-x-left-click)
97 (setq calc-keypad-prev-x-left-click nil))))
98 (setq calc-was-keypad-mode t
99 old-win (get-largest-window))
100 (if (or (< (window-height old-win) (+ height 6))
101 (< (window-width old-win) (+ width 15))
103 (delete-other-windows old-win))
104 (if (< (window-height old-win) (+ height 4))
105 (error "Screen is not tall enough for this mode"))
108 (setq win (split-window old-win (- (window-height old-win)
110 (set-window-buffer old-win (calc-trail-buffer))
111 (set-window-buffer win calc-keypad-buffer)
112 (set-window-start win 1)
113 (setq win (split-window win (+ width 3) t))
114 (set-window-buffer win calcbuf))
115 (if (or t ; left-side keypad not yet fully implemented
117 (set-buffer (window-buffer old-win))
119 (/ (window-width) 2)))
120 (setq win (split-window old-win (- (window-width old-win)
123 (setq old-win (split-window old-win (+ width 2) t)))
124 (set-window-buffer win calc-keypad-buffer)
125 (set-window-start win 1)
126 (split-window win (- (window-height win) height 1))
127 (set-window-buffer win calcbuf))
128 (select-window old-win)
129 (if (and (eq window-system 'x)
130 (not calc-keypad-prev-x-left-click))
132 (setq calc-keypad-prev-x-right-click (aref mouse-map 0)
133 calc-keypad-prev-x-middle-click (aref mouse-map 1)
134 calc-keypad-prev-x-left-click (aref mouse-map 2))
135 (aset mouse-map 0 'calc-keypad-x-right-click)
136 (aset mouse-map 1 'calc-keypad-x-middle-click)
137 (aset mouse-map 2 'calc-keypad-x-left-click)))
138 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons.")
139 (run-hooks 'calc-keypad-start-hook)
140 (and calc-keypad-said-hello interactive
144 (setq calc-keypad-said-hello t))))
145 (setq calc-keypad-input nil)
148 (defun calc-keypad-off ()
150 (if calc-standalone-flag
151 (save-buffers-kill-emacs nil)
155 (defun calc-keypad-redraw ()
156 (set-buffer calc-keypad-buffer)
157 (setq buffer-read-only t)
158 (setq calc-keypad-full-layout (append (symbol-value (nth calc-keypad-menu
161 (let ((buffer-read-only nil)
162 (row calc-keypad-full-layout)
167 (let ((col (car row)))
169 (let* ((key (car col))
172 (if (and (= y 3) (eq col (car row)))
173 (progn (setq col (cdr col)) 9)
175 (name (if (and calc-standalone-flag
176 (eq (nth 1 key) 'calc-keypad-off))
178 (if (> (length (car key)) cwid)
179 (substring (car key) 0 cwid)
182 (pad (- cwid (/ wid 2))))
183 (insert (make-string (/ (- cwid wid) 2) 32)
185 (make-string (/ (- cwid wid -1) 2) 32)
186 (if (equal name "MENU")
187 (int-to-string (1+ calc-keypad-menu))
189 (or (setq col (cdr col))
192 "-----+-----+-----+-----+-----"
194 "-----+---+-+--+--+-+---++----"
195 "----+----+----+----+----+----"))
196 (if (= y 7) "+\n" "|\n"))
199 (setq calc-keypad-prev-input t)
200 (calc-keypad-show-input)
201 (goto-char (point-min))
204 (defun calc-keypad-show-input ()
205 (or (equal calc-keypad-input calc-keypad-prev-input)
206 (let ((buffer-read-only nil))
208 (goto-char (point-min))
210 (delete-region (point-min) (point))
211 (if calc-keypad-input
212 (insert "Calc: " calc-keypad-input "\n")
213 (insert "----+-----Calc " calc-version "-----+----"
214 (int-to-string (1+ calc-keypad-menu))
216 (setq calc-keypad-prev-input calc-keypad-input)
219 (defun calc-keypad-press ()
221 (or (eq major-mode 'calc-keypad)
222 (error "Must be in *Calc Keypad* buffer for this command"))
223 (let* ((row (save-excursion
225 (count-lines (point-min) (point))))
227 (x (/ (current-column) (if (>= y 4) 6 5)))
230 (set-buffer calc-main-buffer)
231 (setq radix calc-number-radix
232 frac calc-prefer-frac
233 inv calc-inverse-flag)
234 calc-hyperbolic-flag))
236 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus)))
237 (input calc-keypad-input)
239 (or (string-match "\\*[0-9]+\\.\\^" input)
240 (and (<= radix 14) (string-match "e" input)))
242 (key (nth x (nth y calc-keypad-full-layout)))
243 (cmd (or (nth (if inv (if hyp 4 2) (if hyp 3 99)) key)
246 (isstring (and (consp cmd) (stringp (car cmd))))
247 (calc-is-keypad-press t))
248 (if invhyp (calc-wrapper)) ; clear Inv and Hyp flags
250 (cond ((or (null cmd)
253 ((and (> (minibuffer-depth) 0))
255 (setq unread-command-char (aref (car cmd) 0)))
257 (setq unread-command-char ?\177))
258 ((eq cmd 'calc-enter)
259 (setq unread-command-char ?\r))
261 (setq unread-command-char ?\a))
264 ((and input (string-match "STO\\|RCL" input))
265 (cond ((and isstring (string-match "[0-9]" (car cmd)))
266 (setq calc-keypad-input nil)
267 (let ((var (intern (concat "var-q" (car cmd)))))
268 (cond ((equal input "STO+") (calc-store-plus var))
269 ((equal input "STO-") (calc-store-minus var))
270 ((equal input "STO*") (calc-store-times var))
271 ((equal input "STO/") (calc-store-div var))
272 ((equal input "STO^") (calc-store-power var))
273 ((equal input "STOn") (calc-store-neg 1 var))
274 ((equal input "STO&") (calc-store-inv 1 var))
275 ((equal input "STO") (calc-store-into var))
276 (t (calc-recall var)))))
277 ((memq cmd '(calc-pop calc-undo))
278 (setq calc-keypad-input nil))
279 ((and (equal input "STO")
280 (setq frac (assq cmd '( ( calc-plus . "+" )
283 ( calc-divide . "/" )
285 ( calc-change-sign . "n")
286 ( calc-inv . "&") ))))
287 (setq calc-keypad-input (concat input (cdr frac))))
292 (if (or (and (equal cmd ".")
294 (string-match "[.:e^]" input))
297 (or (and (<= radix 14) (string-match "e" input))
298 (string-match "\\^\\|[-.:]\\'" input)))
299 (and (not (equal cmd "."))
300 (let ((case-fold-search nil))
301 (string-match cmd "0123456789ABCDEF"
303 "[e^]" (or input ""))
306 (setq calc-keypad-input (concat
310 (format "%d#" radix))
313 (or (and (equal cmd "e") "1")
317 (if (and (equal cmd ".") frac)
319 (if (and (equal cmd "e")
324 (format "*%d.^" radix)
326 ((and (eq cmd 'calc-change-sign)
328 (let* ((epos (or iexpon 0))
329 (suffix (substring input epos)))
330 (setq calc-keypad-input (concat
331 (substring input 0 epos)
332 (if (string-match "\\`-" suffix)
334 (concat "-" suffix))))))
335 ((and (eq cmd 'calc-pop)
339 (setq calc-keypad-input (substring input 0
344 ((and (eq cmd 'calc-undo)
346 (setq calc-keypad-input nil))
349 (let ((val (math-read-number input)))
350 (setq calc-keypad-input nil)
353 (calc-push-list (list (calc-record
354 (calc-normalize val)))))
358 (if (eq cmd 'calc-enter) (setq cmd nil))))
359 (setq prefix-arg current-prefix-arg)
361 (if (and (consp cmd) (eq (car cmd) 'progn))
362 (while (setq cmd (cdr cmd))
363 (if (integerp (car cmd))
364 (setq prefix-arg (car cmd))
365 (command-execute (car cmd))))
366 (command-execute cmd)))))
367 (set-buffer calc-keypad-buffer)
368 (calc-keypad-show-input)))
371 (defun calc-keypad-x-left-click (arg)
372 "Handle a left-button mouse click in Calc Keypad window."
374 (if (and calc-keypad-buffer
375 (buffer-name calc-keypad-buffer)
376 (get-buffer-window calc-keypad-buffer)
377 (setq coords (coordinates-in-window-p
378 arg (get-buffer-window calc-keypad-buffer))))
379 (let ((win (selected-window)))
382 (x-mouse-set-point arg)
384 (and (window-point win)
385 (select-window win))))
386 (funcall calc-keypad-prev-x-left-click arg)))
389 (defun calc-keypad-x-right-click (arg)
390 "Handle a right-button mouse click in Calc Keypad window."
391 (if (and calc-keypad-buffer
392 (buffer-name calc-keypad-buffer)
393 (get-buffer-window calc-keypad-buffer)
394 (coordinates-in-window-p
395 arg (get-buffer-window calc-keypad-buffer)))
397 (set-buffer calc-keypad-buffer)
399 (funcall calc-keypad-prev-x-right-click arg))
402 (defun calc-keypad-x-middle-click (arg)
403 "Handle a middle-button mouse click in Calc Keypad window."
404 (if (and calc-keypad-buffer
405 (buffer-name calc-keypad-buffer)
406 (get-buffer-window calc-keypad-buffer)
407 (coordinates-in-window-p
408 arg (get-buffer-window calc-keypad-buffer)))
410 (set-buffer calc-keypad-buffer)
411 (calc-keypad-menu-back))
412 (funcall calc-keypad-prev-x-middle-click arg))
415 (defun calc-keypad-menu ()
417 (or (eq major-mode 'calc-keypad)
418 (error "Must be in *Calc Keypad* buffer for this command"))
419 (while (progn (setq calc-keypad-menu (% (1+ calc-keypad-menu)
420 (length calc-keypad-menus)))
421 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
425 (defun calc-keypad-menu-back ()
427 (or (eq major-mode 'calc-keypad)
428 (error "Must be in *Calc Keypad* buffer for this command"))
429 (while (progn (setq calc-keypad-menu (% (1- (+ calc-keypad-menu
430 (length calc-keypad-menus)))
431 (length calc-keypad-menus)))
432 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
436 (defun calc-keypad-store ()
438 (setq calc-keypad-input "STO")
441 (defun calc-keypad-recall ()
443 (setq calc-keypad-input "RCL")
446 (defun calc-pack-interval (mode)
448 (if (or (< mode 0) (> mode 3))
449 (error "Open/close code should be in the range from 0 to 3."))
450 (calc-pack (- -6 mode))
453 (defun calc-keypad-execute ()
455 (let* ((prompt "Calc keystrokes: ")
456 (flush 'x-flush-mouse-queue)
462 (setq keys (read-key-sequence prompt))
463 (setq cmd (key-binding keys))
464 (if (or (memq cmd '(calc-inverse
469 (and prefix (string-match "\\`\e?[-0-9]\\'" keys)))
471 (setq last-command-char (aref keys (1- (length keys))))
472 (command-execute cmd)
473 (setq flush 'not-any-more
475 prompt (concat prompt (key-description keys) " ")))
476 (eq cmd flush))))) ; skip mouse-up event
479 (command-execute cmd)
480 (error "Not a Calc command: %s" (key-description keys))))
484 ;;; |----+----+----+----+----+----|
485 ;;; | ENTER |+/- |EEX |UNDO| <- |
486 ;;; |-----+---+-+--+--+-+---++----|
487 ;;; | INV | 7 | 8 | 9 | / |
488 ;;; |-----+-----+-----+-----+-----|
489 ;;; | HYP | 4 | 5 | 6 | * |
490 ;;; |-----+-----+-----+-----+-----|
491 ;;; |EXEC | 1 | 2 | 3 | - |
492 ;;; |-----+-----+-----+-----+-----|
493 ;;; | OFF | 0 | . | PI | + |
494 ;;; |-----+-----+-----+-----+-----|
496 (defvar calc-keypad-layout
497 '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
498 ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
499 ( "+/-" calc-change-sign calc-inv (progn -4 calc-pack) )
500 ( "EEX" ("e") (progn calc-num-prefix calc-pack-interval)
501 (progn -5 calc-pack) )
502 ( "UNDO" calc-undo calc-redo calc-last-args )
503 ( "<-" calc-pop (progn 0 calc-pop)
504 (progn calc-num-prefix calc-pop) ) )
505 ( ( "INV" calc-inverse )
506 ( "7" ("7") calc-round )
507 ( "8" ("8") (progn 2 calc-clean-num) )
508 ( "9" ("9") calc-float )
509 ( "/" calc-divide (progn calc-inverse calc-power) ) )
510 ( ( "HYP" calc-hyperbolic )
511 ( "4" ("4") calc-ln calc-log10 )
512 ( "5" ("5") calc-exp calc-exp10 )
513 ( "6" ("6") calc-abs )
514 ( "*" calc-times calc-power ) )
515 ( ( "EXEC" calc-keypad-execute )
516 ( "1" ("1") calc-arcsin calc-sin )
517 ( "2" ("2") calc-arccos calc-cos )
518 ( "3" ("3") calc-arctan calc-tan )
519 ( "-" calc-minus calc-conj ) )
520 ( ( "OFF" calc-keypad-off )
521 ( "0" ("0") calc-imaginary )
522 ( "." (".") calc-precision )
524 ( "+" calc-plus calc-sqrt ) ) )
527 (defvar calc-keypad-menus '( calc-keypad-math-menu
528 calc-keypad-funcs-menu
529 calc-keypad-binary-menu
530 calc-keypad-vector-menu
531 calc-keypad-modes-menu
532 calc-keypad-user-menu ) )
534 ;;; |----+----+----+----+----+----|
535 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
536 ;;; |----+----+----+----+----+----|
537 ;;; | LN |EXP | |ABS |IDIV|MOD |
538 ;;; |----+----+----+----+----+----|
539 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
541 (defvar calc-keypad-math-menu
542 '( ( ( "FLR" calc-floor )
543 ( "CEIL" calc-ceiling )
545 ( "TRNC" calc-trunc )
546 ( "CLN2" (progn 2 calc-clean-num) )
547 ( "FLT" calc-float ) )
559 ( "1/x" calc-inv ) ) )
562 ;;; |----+----+----+----+----+----|
563 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
564 ;;; |----+----+----+----+----+----|
565 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
566 ;;; |----+----+----+----+----+----|
567 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
569 (defvar calc-keypad-funcs-menu
570 '( ( ( "IGAM" calc-inc-gamma )
572 ( "IBET" calc-inc-beta )
574 ( "BESJ" calc-bessel-J )
575 ( "BESY" calc-bessel-Y ) )
576 ( ( "IMAG" calc-imaginary )
578 ( "RE" calc-re calc-im )
579 ( "ATN2" calc-arctan2 )
580 ( "RAND" calc-random )
581 ( "RAGN" calc-random-again ) )
582 ( ( "GCD" calc-gcd calc-lcm )
583 ( "FACT" calc-factorial calc-gamma )
584 ( "DFCT" calc-double-factorial )
585 ( "BNOM" calc-choose )
587 ( "NXTP" calc-next-prime calc-prev-prime ) ) )
590 ;;; |----+----+----+----+----+----|
591 ;;; |AND | OR |XOR |NOT |LSH |RSH |
592 ;;; |----+----+----+----+----+----|
593 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
594 ;;; |----+----+----+----+----+----|
595 ;;; | A | B | C | D | E | F |
597 (defvar calc-keypad-binary-menu
598 '( ( ( "AND" calc-and calc-diff )
601 ( "NOT" calc-not calc-clip )
602 ( "LSH" calc-lshift-binary calc-rotate-binary )
603 ( "RSH" calc-rshift-binary ) )
604 ( ( "DEC" calc-decimal-radix )
605 ( "HEX" calc-hex-radix )
606 ( "OCT" calc-octal-radix )
607 ( "BIN" calc-binary-radix )
608 ( "WSIZ" calc-word-size )
609 ( "ARSH" calc-rshift-arith ) )
618 ;;; |----+----+----+----+----+----|
619 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
620 ;;; |----+----+----+----+----+----|
621 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
622 ;;; |----+----+----+----+----+----|
623 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
625 (defvar calc-keypad-vector-menu
626 '( ( ( "SUM" calc-vector-sum calc-vector-alt-sum calc-vector-mean )
627 ( "PROD" calc-vector-product nil calc-vector-sdev )
628 ( "MAX" calc-vector-max calc-vector-min calc-vector-median )
629 ( "MAP*" (lambda () (interactive)
630 (calc-map '(2 calcFunc-mul "*"))) )
631 ( "MAP^" (lambda () (interactive)
632 (calc-map '(2 calcFunc-pow "^"))) )
633 ( "MAP$" calc-map-stack ) )
634 ( ( "MINV" calc-inv )
636 ( "MTRN" calc-transpose calc-conj-transpose )
637 ( "IDNT" (progn calc-num-prefix calc-ident) )
638 ( "CRSS" calc-cross )
639 ( "\"x\"" "\excalc-algebraic-entry\rx\r"
640 "\excalc-algebraic-entry\ry\r"
641 "\excalc-algebraic-entry\rz\r"
642 "\excalc-algebraic-entry\rt\r") )
643 ( ( "PACK" calc-pack )
644 ( "UNPK" calc-unpack )
645 ( "INDX" (progn calc-num-prefix calc-index) "\C-u\excalc-index\r" )
646 ( "BLD" (progn calc-num-prefix calc-build-vector) )
647 ( "LEN" calc-vlength )
648 ( "..." calc-full-vectors ) ) )
651 ;;; |----+----+----+----+----+----|
652 ;;; |FLT |FIX |SCI |ENG |GRP | |
653 ;;; |----+----+----+----+----+----|
654 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
655 ;;; |----+----+----+----+----+----|
656 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
658 (defvar calc-keypad-modes-menu
659 '( ( ( "FLT" calc-normal-notation
660 (progn calc-num-prefix calc-normal-notation) )
661 ( "FIX" (progn 2 calc-fix-notation)
662 (progn calc-num-prefix calc-fix-notation) )
663 ( "SCI" calc-sci-notation
664 (progn calc-num-prefix calc-sci-notation) )
665 ( "ENG" calc-eng-notation
666 (progn calc-num-prefix calc-eng-notation) )
667 ( "GRP" calc-group-digits "\C-u-3\excalc-group-digits\r" )
669 ( ( "RAD" calc-radians-mode )
670 ( "DEG" calc-degrees-mode )
671 ( "FRAC" calc-frac-mode )
672 ( "POLR" calc-polar-mode )
673 ( "SYMB" calc-symbolic-mode )
674 ( "PREC" calc-precision ) )
675 ( ( "SWAP" calc-roll-down )
676 ( "RLL3" (progn 3 calc-roll-up) (progn 3 calc-roll-down) )
677 ( "RLL4" (progn 4 calc-roll-up) (progn 4 calc-roll-down) )
679 ( "STO" calc-keypad-store )
680 ( "RCL" calc-keypad-recall ) ) )