1 ;;; calc-keypd.el --- mouse-capable keypad input for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
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.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-keypd () nil)
38 (defvar calc-keypad-buffer nil)
39 (defvar calc-keypad-menu 0)
40 (defvar calc-keypad-full-layout nil)
41 (defvar calc-keypad-input nil)
42 (defvar calc-keypad-prev-input nil)
43 (defvar calc-keypad-said-hello nil)
45 ;;; |----+----+----+----+----+----|
46 ;;; | ENTER |+/- |EEX |UNDO| <- |
47 ;;; |-----+---+-+--+--+-+---++----|
48 ;;; | INV | 7 | 8 | 9 | / |
49 ;;; |-----+-----+-----+-----+-----|
50 ;;; | HYP | 4 | 5 | 6 | * |
51 ;;; |-----+-----+-----+-----+-----|
52 ;;; |EXEC | 1 | 2 | 3 | - |
53 ;;; |-----+-----+-----+-----+-----|
54 ;;; | OFF | 0 | . | PI | + |
55 ;;; |-----+-----+-----+-----+-----|
56 (defvar calc-keypad-layout
57 '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
58 ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
59 ( "+/-" calc-change-sign calc-inv (progn -4 calc-pack) )
60 ( "EEX" ("e") (progn calc-num-prefix calc-pack-interval)
61 (progn -5 calc-pack) )
62 ( "UNDO" calc-undo calc-redo calc-last-args )
63 ( "<-" calc-pop (progn 0 calc-pop)
64 (progn calc-num-prefix calc-pop) ) )
65 ( ( "INV" calc-inverse )
66 ( "7" ("7") calc-round )
67 ( "8" ("8") (progn 2 calc-clean-num) )
68 ( "9" ("9") calc-float )
69 ( "/" calc-divide (progn calc-inverse calc-power) ) )
70 ( ( "HYP" calc-hyperbolic )
71 ( "4" ("4") calc-ln calc-log10 )
72 ( "5" ("5") calc-exp calc-exp10 )
73 ( "6" ("6") calc-abs )
74 ( "*" calc-times calc-power ) )
75 ( ( "EXEC" calc-keypad-execute )
76 ( "1" ("1") calc-arcsin calc-sin )
77 ( "2" ("2") calc-arccos calc-cos )
78 ( "3" ("3") calc-arctan calc-tan )
79 ( "-" calc-minus calc-conj ) )
80 ( ( "OFF" calc-keypad-off )
81 ( "0" ("0") calc-imaginary )
82 ( "." (".") calc-precision )
84 ( "+" calc-plus calc-sqrt ) ) ))
86 (defvar calc-keypad-menus '( calc-keypad-math-menu
87 calc-keypad-funcs-menu
88 calc-keypad-binary-menu
89 calc-keypad-vector-menu
90 calc-keypad-modes-menu
91 calc-keypad-user-menu ) )
93 ;;; |----+----+----+----+----+----|
94 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
95 ;;; |----+----+----+----+----+----|
96 ;;; | LN |EXP | |ABS |IDIV|MOD |
97 ;;; |----+----+----+----+----+----|
98 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
100 (defvar calc-keypad-math-menu
101 '( ( ( "FLR" calc-floor )
102 ( "CEIL" calc-ceiling )
104 ( "TRNC" calc-trunc )
105 ( "CLN2" (progn 2 calc-clean-num) )
106 ( "FLT" calc-float ) )
118 ( "1/x" calc-inv ) ) ))
120 ;;; |----+----+----+----+----+----|
121 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
122 ;;; |----+----+----+----+----+----|
123 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
124 ;;; |----+----+----+----+----+----|
125 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
127 (defvar calc-keypad-funcs-menu
128 '( ( ( "IGAM" calc-inc-gamma )
130 ( "IBET" calc-inc-beta )
132 ( "BESJ" calc-bessel-J )
133 ( "BESY" calc-bessel-Y ) )
134 ( ( "IMAG" calc-imaginary )
136 ( "RE" calc-re calc-im )
137 ( "ATN2" calc-arctan2 )
138 ( "RAND" calc-random )
139 ( "RAGN" calc-random-again ) )
140 ( ( "GCD" calc-gcd calc-lcm )
141 ( "FACT" calc-factorial calc-gamma )
142 ( "DFCT" calc-double-factorial )
143 ( "BNOM" calc-choose )
145 ( "NXTP" calc-next-prime calc-prev-prime ) ) ))
147 ;;; |----+----+----+----+----+----|
148 ;;; |AND | OR |XOR |NOT |LSH |RSH |
149 ;;; |----+----+----+----+----+----|
150 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
151 ;;; |----+----+----+----+----+----|
152 ;;; | A | B | C | D | E | F |
154 (defvar calc-keypad-binary-menu
155 '( ( ( "AND" calc-and calc-diff )
158 ( "NOT" calc-not calc-clip )
159 ( "LSH" calc-lshift-binary calc-rotate-binary )
160 ( "RSH" calc-rshift-binary ) )
161 ( ( "DEC" calc-decimal-radix )
162 ( "HEX" calc-hex-radix )
163 ( "OCT" calc-octal-radix )
164 ( "BIN" calc-binary-radix )
165 ( "WSIZ" calc-word-size )
166 ( "ARSH" calc-rshift-arith ) )
174 ;;; |----+----+----+----+----+----|
175 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
176 ;;; |----+----+----+----+----+----|
177 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
178 ;;; |----+----+----+----+----+----|
179 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
181 (defvar calc-keypad-vector-menu
182 '( ( ( "SUM" calc-vector-sum calc-vector-alt-sum calc-vector-mean )
183 ( "PROD" calc-vector-product nil calc-vector-sdev )
184 ( "MAX" calc-vector-max calc-vector-min calc-vector-median )
185 ( "MAP*" (lambda () (interactive)
186 (calc-map '(2 calcFunc-mul "*"))) )
187 ( "MAP^" (lambda () (interactive)
188 (calc-map '(2 calcFunc-pow "^"))) )
189 ( "MAP$" calc-map-stack ) )
190 ( ( "MINV" calc-inv )
192 ( "MTRN" calc-transpose calc-conj-transpose )
193 ( "IDNT" (progn calc-num-prefix calc-ident) )
194 ( "CRSS" calc-cross )
195 ( "\"x\"" "\excalc-algebraic-entry\rx\r"
196 "\excalc-algebraic-entry\ry\r"
197 "\excalc-algebraic-entry\rz\r"
198 "\excalc-algebraic-entry\rt\r") )
199 ( ( "PACK" calc-pack )
200 ( "UNPK" calc-unpack )
201 ( "INDX" (progn calc-num-prefix calc-index) "\C-u\excalc-index\r" )
202 ( "BLD" (progn calc-num-prefix calc-build-vector) )
203 ( "LEN" calc-vlength )
204 ( "..." calc-full-vectors ) ) ))
206 ;;; |----+----+----+----+----+----|
207 ;;; |FLT |FIX |SCI |ENG |GRP | |
208 ;;; |----+----+----+----+----+----|
209 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
210 ;;; |----+----+----+----+----+----|
211 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
213 (defvar calc-keypad-modes-menu
214 '( ( ( "FLT" calc-normal-notation
215 (progn calc-num-prefix calc-normal-notation) )
216 ( "FIX" (progn 2 calc-fix-notation)
217 (progn calc-num-prefix calc-fix-notation) )
218 ( "SCI" calc-sci-notation
219 (progn calc-num-prefix calc-sci-notation) )
220 ( "ENG" calc-eng-notation
221 (progn calc-num-prefix calc-eng-notation) )
222 ( "GRP" calc-group-digits "\C-u-3\excalc-group-digits\r" )
224 ( ( "RAD" calc-radians-mode )
225 ( "DEG" calc-degrees-mode )
226 ( "FRAC" calc-frac-mode )
227 ( "POLR" calc-polar-mode )
228 ( "SYMB" calc-symbolic-mode )
229 ( "PREC" calc-precision ) )
230 ( ( "SWAP" calc-roll-down )
231 ( "RLL3" (progn 3 calc-roll-up) (progn 3 calc-roll-down) )
232 ( "RLL4" (progn 4 calc-roll-up) (progn 4 calc-roll-down) )
234 ( "STO" calc-keypad-store )
235 ( "RCL" calc-keypad-recall ) ) ))
237 (define-derived-mode calc-keypad-mode fundamental-mode "Calculator"
238 "Major mode for Calc keypad input."
239 (define-key calc-keypad-mode-map " " 'calc-keypad-press)
240 (define-key calc-keypad-mode-map (kbd "RET") 'calc-keypad-press)
241 (define-key calc-keypad-mode-map (kbd "TAB") 'calc-keypad-menu)
242 (define-key calc-keypad-mode-map "q" 'calc-keypad-off)
243 (define-key calc-keypad-mode-map [(mouse-3)] 'calc-keypad-right-click)
244 (define-key calc-keypad-mode-map [(mouse-2)] 'calc-keypad-middle-click)
245 (define-key calc-keypad-mode-map [(mouse-1)] 'calc-keypad-left-click)
246 (put 'calc-keypad-mode 'mode-class 'special)
247 (make-local-variable 'calc-main-buffer))
249 (defun calc-do-keypad (&optional full-display interactive)
251 (let ((calcbuf (current-buffer)))
252 (unless (bufferp calc-keypad-buffer)
253 (set-buffer (setq calc-keypad-buffer (get-buffer-create "*Calc Keypad*")))
255 (setq calc-main-buffer calcbuf)
261 (if (setq win (get-buffer-window "*Calculator*"))
263 (if (setq win (get-buffer-window "*Calc Trail*"))
265 (switch-to-buffer (other-buffer))
266 (delete-window win)))
267 (if (setq win (get-buffer-window calc-keypad-buffer))
269 (bury-buffer "*Calculator*")
270 (bury-buffer "*Calc Trail*")
271 (bury-buffer calc-keypad-buffer)
273 (switch-to-buffer (other-buffer))
274 (delete-window win)))
275 (setq calc-was-keypad-mode t
276 old-win (get-largest-window))
277 (if (or (< (window-height old-win) (+ height 6))
278 (< (window-width old-win) (+ width 15))
280 (delete-other-windows old-win))
281 (if (< (window-height old-win) (+ height 4))
282 (error "Screen is not tall enough for this mode"))
285 (setq win (split-window old-win (- (window-height old-win)
287 (set-window-buffer old-win (calc-trail-buffer))
288 (set-window-buffer win calc-keypad-buffer)
289 (set-window-start win 1)
290 (setq win (split-window win (+ width 3) t))
291 (set-window-buffer win calcbuf))
292 (if (or t ; left-side keypad not yet fully implemented
294 (set-buffer (window-buffer old-win))
296 (/ (window-width) 2)))
297 (setq win (split-window old-win (- (window-width old-win)
300 (setq old-win (split-window old-win (+ width 2) t)))
301 (set-window-buffer win calc-keypad-buffer)
302 (set-window-start win 1)
303 (split-window win (- (window-height win) height 1))
304 (set-window-buffer win calcbuf))
305 (select-window old-win)
306 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons")
307 (run-hooks 'calc-keypad-start-hook)
308 (and calc-keypad-said-hello interactive
312 (setq calc-keypad-said-hello t)))
313 (setq calc-keypad-input nil)))
315 (defun calc-keypad-off ()
317 (if calc-standalone-flag
318 (save-buffers-kill-emacs nil)
321 (defun calc-keypad-redraw ()
322 (set-buffer calc-keypad-buffer)
323 (setq buffer-read-only t)
324 (setq calc-keypad-full-layout (append (symbol-value (nth calc-keypad-menu
327 (let ((buffer-read-only nil)
328 (row calc-keypad-full-layout)
333 (let ((col (car row)))
335 (let* ((key (car col))
338 (if (and (= y 3) (eq col (car row)))
339 (progn (setq col (cdr col)) 9)
341 (name (if (and calc-standalone-flag
342 (eq (nth 1 key) 'calc-keypad-off))
344 (if (> (length (car key)) cwid)
345 (substring (car key) 0 cwid)
348 (pad (- cwid (/ wid 2))))
349 (insert (make-string (/ (- cwid wid) 2) 32)
351 (make-string (/ (- cwid wid -1) 2) 32)
352 (if (equal name "MENU")
353 (int-to-string (1+ calc-keypad-menu))
355 (or (setq col (cdr col))
358 "-----+-----+-----+-----+-----"
360 "-----+---+-+--+--+-+---++----"
361 "----+----+----+----+----+----"))
362 (if (= y 7) "+\n" "|\n"))
365 (setq calc-keypad-prev-input t)
366 (calc-keypad-show-input)
367 (goto-char (point-min)))
369 (defun calc-keypad-show-input ()
370 (or (equal calc-keypad-input calc-keypad-prev-input)
371 (let ((buffer-read-only nil))
373 (goto-char (point-min))
375 (delete-region (point-min) (point))
376 (if calc-keypad-input
377 (insert "Calc: " calc-keypad-input "\n")
378 (insert "----+-----Calc " calc-version "-----+----"
379 (int-to-string (1+ calc-keypad-menu))
381 (setq calc-keypad-prev-input calc-keypad-input))
383 (defun calc-keypad-press ()
385 (unless (eq major-mode 'calc-keypad-mode)
386 (error "Must be in *Calc Keypad* buffer for this command"))
387 (let* ((row (save-excursion
389 (count-lines (point-min) (point))))
391 (x (/ (current-column) (if (>= y 4) 6 5)))
393 (hyp (with-current-buffer calc-main-buffer
394 (setq radix calc-number-radix
395 frac calc-prefer-frac
396 inv calc-inverse-flag)
397 calc-hyperbolic-flag))
399 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus)))
400 (input calc-keypad-input)
402 (or (string-match "\\*[0-9]+\\.\\^" input)
403 (and (<= radix 14) (string-match "e" input)))
405 (key (nth x (nth y calc-keypad-full-layout)))
406 (cmd (or (nth (if inv (if hyp 4 2) (if hyp 3 99)) key)
409 (isstring (and (consp cmd) (stringp (car cmd))))
410 (calc-is-keypad-press t))
411 (if invhyp (calc-wrapper)) ; clear Inv and Hyp flags
413 (cond ((or (null cmd)
416 ((and (> (minibuffer-depth) 0))
418 (push (aref (car cmd) 0) unread-command-events))
420 (push ?\177 unread-command-events))
421 ((eq cmd 'calc-enter)
422 (push 13 unread-command-events))
424 (push 7 unread-command-events))
427 ((and input (string-match "STO\\|RCL" input))
428 (cond ((and isstring (string-match "[0-9]" (car cmd)))
429 (setq calc-keypad-input nil)
430 (let ((var (intern (concat "var-q" (car cmd)))))
431 (cond ((equal input "STO+") (calc-store-plus var))
432 ((equal input "STO-") (calc-store-minus var))
433 ((equal input "STO*") (calc-store-times var))
434 ((equal input "STO/") (calc-store-div var))
435 ((equal input "STO^") (calc-store-power var))
436 ((equal input "STOn") (calc-store-neg 1 var))
437 ((equal input "STO&") (calc-store-inv 1 var))
438 ((equal input "STO") (calc-store-into var))
439 (t (calc-recall var)))))
440 ((memq cmd '(calc-pop calc-undo))
441 (setq calc-keypad-input nil))
442 ((and (equal input "STO")
443 (setq frac (assq cmd '( ( calc-plus . "+" )
446 ( calc-divide . "/" )
448 ( calc-change-sign . "n")
449 ( calc-inv . "&") ))))
450 (setq calc-keypad-input (concat input (cdr frac))))
455 (if (or (and (equal cmd ".")
457 (string-match "[.:e^]" input))
460 (or (and (<= radix 14) (string-match "e" input))
461 (string-match "\\^\\|[-.:]\\'" input)))
462 (and (not (equal cmd "."))
463 (let ((case-fold-search nil))
464 (string-match cmd "0123456789ABCDEF"
466 "[e^]" (or input ""))
469 (setq calc-keypad-input (concat
473 (format "%d#" radix))
476 (or (and (equal cmd "e") "1")
480 (if (and (equal cmd ".") frac)
482 (if (and (equal cmd "e")
487 (format "*%d.^" radix)
489 ((and (eq cmd 'calc-change-sign)
491 (let* ((epos (or iexpon 0))
492 (suffix (substring input epos)))
493 (setq calc-keypad-input (concat
494 (substring input 0 epos)
495 (if (string-match "\\`-" suffix)
497 (concat "-" suffix))))))
498 ((and (eq cmd 'calc-pop)
502 (setq calc-keypad-input (substring input 0
507 ((and (eq cmd 'calc-undo)
509 (setq calc-keypad-input nil))
512 (let ((val (math-read-number input)))
513 (setq calc-keypad-input nil)
516 (calc-push-list (list (calc-record
517 (calc-normalize val)))))
521 (if (eq cmd 'calc-enter) (setq cmd nil))))
522 (setq prefix-arg current-prefix-arg)
524 (if (and (consp cmd) (eq (car cmd) 'progn))
525 (while (setq cmd (cdr cmd))
526 (if (integerp (car cmd))
527 (setq prefix-arg (car cmd))
528 (command-execute (car cmd))))
529 (command-execute cmd)))))
530 (set-buffer calc-keypad-buffer)
531 (calc-keypad-show-input))))
533 (defun calc-keypad-left-click (event)
534 "Handle a left-button mouse click in Calc Keypad window."
536 (goto-char (posn-point (event-start event)))
539 (defun calc-keypad-right-click (event)
540 "Handle a right-button mouse click in Calc Keypad window."
543 (set-buffer calc-keypad-buffer)
546 (defun calc-keypad-middle-click (event)
547 "Handle a middle-button mouse click in Calc Keypad window."
549 (with-current-buffer calc-keypad-buffer
550 (calc-keypad-menu-back)))
552 (defun calc-keypad-menu ()
554 (unless (eq major-mode 'calc-keypad-mode)
555 (error "Must be in *Calc Keypad* buffer for this command"))
556 (while (progn (setq calc-keypad-menu (% (1+ calc-keypad-menu)
557 (length calc-keypad-menus)))
558 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
559 (calc-keypad-redraw))
561 (defun calc-keypad-menu-back ()
563 (or (eq major-mode 'calc-keypad-mode)
564 (error "Must be in *Calc Keypad* buffer for this command"))
565 (while (progn (setq calc-keypad-menu (% (1- (+ calc-keypad-menu
566 (length calc-keypad-menus)))
567 (length calc-keypad-menus)))
568 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
569 (calc-keypad-redraw))
571 (defun calc-keypad-store ()
573 (setq calc-keypad-input "STO"))
575 (defun calc-keypad-recall ()
577 (setq calc-keypad-input "RCL"))
579 (defun calc-pack-interval (mode)
581 (if (or (< mode 0) (> mode 3))
582 (error "Open/close code should be in the range from 0 to 3"))
583 (calc-pack (- -6 mode)))
585 (defun calc-keypad-execute ()
587 (let* ((prompt "Calc keystrokes: ")
588 (flush 'x-flush-mouse-queue)
594 (setq keys (read-key-sequence prompt))
595 (setq cmd (key-binding keys))
596 (if (or (memq cmd '(calc-inverse
601 (and prefix (string-match "\\`\e?[-0-9]\\'" keys)))
603 (setq last-command-char (aref keys (1- (length keys))))
604 (command-execute cmd)
605 (setq flush 'not-any-more
607 prompt (concat prompt (key-description keys) " ")))
608 (eq cmd flush))))) ; skip mouse-up event
611 (command-execute cmd)
612 (error "Not a Calc command: %s" (key-description keys)))))
615 ;;; calc-keypd.el ends here