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: 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.
34 (defvar calc-keypad-buffer nil)
35 (defvar calc-keypad-menu 0)
36 (defvar calc-keypad-full-layout nil)
37 (defvar calc-keypad-input nil)
38 (defvar calc-keypad-prev-input nil)
39 (defvar calc-keypad-said-hello nil)
41 ;;; |----+----+----+----+----+----|
42 ;;; | ENTER |+/- |EEX |UNDO| <- |
43 ;;; |-----+---+-+--+--+-+---++----|
44 ;;; | INV | 7 | 8 | 9 | / |
45 ;;; |-----+-----+-----+-----+-----|
46 ;;; | HYP | 4 | 5 | 6 | * |
47 ;;; |-----+-----+-----+-----+-----|
48 ;;; |EXEC | 1 | 2 | 3 | - |
49 ;;; |-----+-----+-----+-----+-----|
50 ;;; | OFF | 0 | . | PI | + |
51 ;;; |-----+-----+-----+-----+-----|
52 (defvar calc-keypad-layout
53 '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
54 ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
55 ( "+/-" calc-change-sign calc-inv (progn -4 calc-pack) )
56 ( "EEX" ("e") (progn calc-num-prefix calc-pack-interval)
57 (progn -5 calc-pack) )
58 ( "UNDO" calc-undo calc-redo calc-last-args )
59 ( "<-" calc-pop (progn 0 calc-pop)
60 (progn calc-num-prefix calc-pop) ) )
61 ( ( "INV" calc-inverse )
62 ( "7" ("7") calc-round )
63 ( "8" ("8") (progn 2 calc-clean-num) )
64 ( "9" ("9") calc-float )
65 ( "/" calc-divide (progn calc-inverse calc-power) ) )
66 ( ( "HYP" calc-hyperbolic )
67 ( "4" ("4") calc-ln calc-log10 )
68 ( "5" ("5") calc-exp calc-exp10 )
69 ( "6" ("6") calc-abs )
70 ( "*" calc-times calc-power ) )
71 ( ( "EXEC" calc-keypad-execute )
72 ( "1" ("1") calc-arcsin calc-sin )
73 ( "2" ("2") calc-arccos calc-cos )
74 ( "3" ("3") calc-arctan calc-tan )
75 ( "-" calc-minus calc-conj ) )
76 ( ( "OFF" calc-keypad-off )
77 ( "0" ("0") calc-imaginary )
78 ( "." (".") calc-precision )
80 ( "+" calc-plus calc-sqrt ) ) ))
82 (defvar calc-keypad-menus '( calc-keypad-math-menu
83 calc-keypad-funcs-menu
84 calc-keypad-binary-menu
85 calc-keypad-vector-menu
86 calc-keypad-modes-menu
87 calc-keypad-user-menu ) )
89 ;;; |----+----+----+----+----+----|
90 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
91 ;;; |----+----+----+----+----+----|
92 ;;; | LN |EXP | |ABS |IDIV|MOD |
93 ;;; |----+----+----+----+----+----|
94 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
96 (defvar calc-keypad-math-menu
97 '( ( ( "FLR" calc-floor )
98 ( "CEIL" calc-ceiling )
100 ( "TRNC" calc-trunc )
101 ( "CLN2" (progn 2 calc-clean-num) )
102 ( "FLT" calc-float ) )
114 ( "1/x" calc-inv ) ) ))
116 ;;; |----+----+----+----+----+----|
117 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
118 ;;; |----+----+----+----+----+----|
119 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
120 ;;; |----+----+----+----+----+----|
121 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
123 (defvar calc-keypad-funcs-menu
124 '( ( ( "IGAM" calc-inc-gamma )
126 ( "IBET" calc-inc-beta )
128 ( "BESJ" calc-bessel-J )
129 ( "BESY" calc-bessel-Y ) )
130 ( ( "IMAG" calc-imaginary )
132 ( "RE" calc-re calc-im )
133 ( "ATN2" calc-arctan2 )
134 ( "RAND" calc-random )
135 ( "RAGN" calc-random-again ) )
136 ( ( "GCD" calc-gcd calc-lcm )
137 ( "FACT" calc-factorial calc-gamma )
138 ( "DFCT" calc-double-factorial )
139 ( "BNOM" calc-choose )
141 ( "NXTP" calc-next-prime calc-prev-prime ) ) ))
143 ;;; |----+----+----+----+----+----|
144 ;;; |AND | OR |XOR |NOT |LSH |RSH |
145 ;;; |----+----+----+----+----+----|
146 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
147 ;;; |----+----+----+----+----+----|
148 ;;; | A | B | C | D | E | F |
150 (defvar calc-keypad-binary-menu
151 '( ( ( "AND" calc-and calc-diff )
154 ( "NOT" calc-not calc-clip )
155 ( "LSH" calc-lshift-binary calc-rotate-binary )
156 ( "RSH" calc-rshift-binary ) )
157 ( ( "DEC" calc-decimal-radix )
158 ( "HEX" calc-hex-radix )
159 ( "OCT" calc-octal-radix )
160 ( "BIN" calc-binary-radix )
161 ( "WSIZ" calc-word-size )
162 ( "ARSH" calc-rshift-arith ) )
170 ;;; |----+----+----+----+----+----|
171 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
172 ;;; |----+----+----+----+----+----|
173 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
174 ;;; |----+----+----+----+----+----|
175 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
177 (defvar calc-keypad-vector-menu
178 '( ( ( "SUM" calc-vector-sum calc-vector-alt-sum calc-vector-mean )
179 ( "PROD" calc-vector-product nil calc-vector-sdev )
180 ( "MAX" calc-vector-max calc-vector-min calc-vector-median )
181 ( "MAP*" (lambda () (interactive)
182 (calc-map '(2 calcFunc-mul "*"))) )
183 ( "MAP^" (lambda () (interactive)
184 (calc-map '(2 calcFunc-pow "^"))) )
185 ( "MAP$" calc-map-stack ) )
186 ( ( "MINV" calc-inv )
188 ( "MTRN" calc-transpose calc-conj-transpose )
189 ( "IDNT" (progn calc-num-prefix calc-ident) )
190 ( "CRSS" calc-cross )
191 ( "\"x\"" "\excalc-algebraic-entry\rx\r"
192 "\excalc-algebraic-entry\ry\r"
193 "\excalc-algebraic-entry\rz\r"
194 "\excalc-algebraic-entry\rt\r") )
195 ( ( "PACK" calc-pack )
196 ( "UNPK" calc-unpack )
197 ( "INDX" (progn calc-num-prefix calc-index) "\C-u\excalc-index\r" )
198 ( "BLD" (progn calc-num-prefix calc-build-vector) )
199 ( "LEN" calc-vlength )
200 ( "..." calc-full-vectors ) ) ))
202 ;;; |----+----+----+----+----+----|
203 ;;; |FLT |FIX |SCI |ENG |GRP | |
204 ;;; |----+----+----+----+----+----|
205 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
206 ;;; |----+----+----+----+----+----|
207 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
209 (defvar calc-keypad-modes-menu
210 '( ( ( "FLT" calc-normal-notation
211 (progn calc-num-prefix calc-normal-notation) )
212 ( "FIX" (progn 2 calc-fix-notation)
213 (progn calc-num-prefix calc-fix-notation) )
214 ( "SCI" calc-sci-notation
215 (progn calc-num-prefix calc-sci-notation) )
216 ( "ENG" calc-eng-notation
217 (progn calc-num-prefix calc-eng-notation) )
218 ( "GRP" calc-group-digits "\C-u-3\excalc-group-digits\r" )
220 ( ( "RAD" calc-radians-mode )
221 ( "DEG" calc-degrees-mode )
222 ( "FRAC" calc-frac-mode )
223 ( "POLR" calc-polar-mode )
224 ( "SYMB" calc-symbolic-mode )
225 ( "PREC" calc-precision ) )
226 ( ( "SWAP" calc-roll-down )
227 ( "RLL3" (progn 3 calc-roll-up) (progn 3 calc-roll-down) )
228 ( "RLL4" (progn 4 calc-roll-up) (progn 4 calc-roll-down) )
230 ( "STO" calc-keypad-store )
231 ( "RCL" calc-keypad-recall ) ) ))
233 (define-derived-mode calc-keypad-mode fundamental-mode "Calculator"
234 "Major mode for Calc keypad input."
235 (define-key calc-keypad-mode-map " " 'calc-keypad-press)
236 (define-key calc-keypad-mode-map (kbd "RET") 'calc-keypad-press)
237 (define-key calc-keypad-mode-map (kbd "TAB") 'calc-keypad-menu)
238 (define-key calc-keypad-mode-map "q" 'calc-keypad-off)
239 (define-key calc-keypad-mode-map [down-mouse-1] 'ignore)
240 (define-key calc-keypad-mode-map [drag-mouse-1] 'ignore)
241 (define-key calc-keypad-mode-map [double-mouse-1] 'ignore)
242 (define-key calc-keypad-mode-map [triple-mouse-1] 'ignore)
243 (define-key calc-keypad-mode-map [down-mouse-2] 'ignore)
244 (define-key calc-keypad-mode-map [drag-mouse-2] 'ignore)
245 (define-key calc-keypad-mode-map [double-mouse-2] 'ignore)
246 (define-key calc-keypad-mode-map [triple-mouse-2] 'ignore)
247 (define-key calc-keypad-mode-map [down-mouse-3] 'ignore)
248 (define-key calc-keypad-mode-map [drag-mouse-3] 'ignore)
249 (define-key calc-keypad-mode-map [double-mouse-3] 'ignore)
250 (define-key calc-keypad-mode-map [triple-mouse-3] 'ignore)
251 (define-key calc-keypad-mode-map [mouse-3] 'calc-keypad-right-click)
252 (define-key calc-keypad-mode-map [mouse-2] 'calc-keypad-middle-click)
253 (define-key calc-keypad-mode-map [mouse-1] 'calc-keypad-left-click)
254 (put 'calc-keypad-mode 'mode-class 'special)
255 (make-local-variable 'calc-main-buffer))
257 (defun calc-do-keypad (&optional full-display interactive)
259 (let ((calcbuf (current-buffer)))
260 (unless (bufferp calc-keypad-buffer)
261 (set-buffer (setq calc-keypad-buffer (get-buffer-create "*Calc Keypad*")))
263 (setq calc-main-buffer calcbuf)
269 (if (setq win (get-buffer-window "*Calculator*"))
271 (if (setq win (get-buffer-window "*Calc Trail*"))
273 (switch-to-buffer (other-buffer))
274 (delete-window win)))
275 (if (setq win (get-buffer-window calc-keypad-buffer))
277 (bury-buffer "*Calculator*")
278 (bury-buffer "*Calc Trail*")
279 (bury-buffer calc-keypad-buffer)
281 (switch-to-buffer (other-buffer))
282 (delete-window win)))
283 (setq calc-was-keypad-mode t
284 old-win (get-largest-window))
285 (if (or (< (window-height old-win) (+ height 6))
286 (< (window-width old-win) (+ width 15))
288 (delete-other-windows old-win))
289 (if (< (window-height old-win) (+ height 4))
290 (error "Screen is not tall enough for this mode"))
293 (setq win (split-window old-win (- (window-height old-win)
295 (set-window-buffer old-win (calc-trail-buffer))
296 (set-window-buffer win calc-keypad-buffer)
297 (set-window-start win 1)
298 (setq win (split-window win (+ width 3) t))
299 (set-window-buffer win calcbuf))
300 (if (or t ; left-side keypad not yet fully implemented
302 (set-buffer (window-buffer old-win))
304 (/ (window-width) 2)))
305 (setq win (split-window old-win (- (window-width old-win)
308 (setq old-win (split-window old-win (+ width 2) t)))
309 (set-window-buffer win calc-keypad-buffer)
310 (set-window-start win 1)
311 (split-window win (- (window-height win) height 1))
312 (set-window-buffer win calcbuf))
313 (select-window old-win)
314 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons")
315 (run-hooks 'calc-keypad-start-hook)
316 (and calc-keypad-said-hello interactive
320 (setq calc-keypad-said-hello t)))
321 (setq calc-keypad-input nil)))
323 (defun calc-keypad-off ()
325 (if calc-standalone-flag
326 (save-buffers-kill-emacs nil)
329 (defun calc-keypad-redraw ()
330 (set-buffer calc-keypad-buffer)
331 (setq buffer-read-only t)
332 (setq calc-keypad-full-layout (append (symbol-value (nth calc-keypad-menu
335 (let ((buffer-read-only nil)
336 (row calc-keypad-full-layout)
341 (let ((col (car row)))
343 (let* ((key (car col))
346 (if (and (= y 3) (eq col (car row)))
347 (progn (setq col (cdr col)) 9)
349 (name (if (and calc-standalone-flag
350 (eq (nth 1 key) 'calc-keypad-off))
352 (if (> (length (car key)) cwid)
353 (substring (car key) 0 cwid)
356 (pad (- cwid (/ wid 2))))
357 (insert (make-string (/ (- cwid wid) 2) 32)
359 (make-string (/ (- cwid wid -1) 2) 32)
360 (if (equal name "MENU")
361 (int-to-string (1+ calc-keypad-menu))
363 (or (setq col (cdr col))
366 "-----+-----+-----+-----+-----"
368 "-----+---+-+--+--+-+---++----"
369 "----+----+----+----+----+----"))
370 (if (= y 7) "+\n" "|\n"))
373 (setq calc-keypad-prev-input t)
374 (calc-keypad-show-input)
375 (goto-char (point-min)))
377 (defun calc-keypad-show-input ()
378 (or (equal calc-keypad-input calc-keypad-prev-input)
379 (let ((buffer-read-only nil))
381 (goto-char (point-min))
383 (delete-region (point-min) (point))
384 (if calc-keypad-input
385 (insert "Calc: " calc-keypad-input "\n")
386 (insert "----+-----Calc " calc-version "-----+----"
387 (int-to-string (1+ calc-keypad-menu))
389 (setq calc-keypad-prev-input calc-keypad-input))
391 (defun calc-keypad-press ()
393 (unless (eq major-mode 'calc-keypad-mode)
394 (error "Must be in *Calc Keypad* buffer for this command"))
395 (let* ((row (save-excursion
397 (count-lines (point-min) (point))))
399 (x (/ (current-column) (if (>= y 4) 6 5)))
401 (hyp (with-current-buffer calc-main-buffer
402 (setq radix calc-number-radix
403 frac calc-prefer-frac
404 inv calc-inverse-flag)
405 calc-hyperbolic-flag))
407 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus)))
408 (input calc-keypad-input)
410 (or (string-match "\\*[0-9]+\\.\\^" input)
411 (and (<= radix 14) (string-match "e" input)))
413 (key (nth x (nth y calc-keypad-full-layout)))
414 (cmd (or (nth (if inv (if hyp 4 2) (if hyp 3 99)) key)
417 (isstring (and (consp cmd) (stringp (car cmd))))
418 (calc-is-keypad-press t))
419 (if invhyp (calc-wrapper)) ; clear Inv and Hyp flags
421 (cond ((or (null cmd)
424 ((and (> (minibuffer-depth) 0))
426 (push (aref (car cmd) 0) unread-command-events))
428 (push ?\177 unread-command-events))
429 ((eq cmd 'calc-enter)
430 (push 13 unread-command-events))
432 (push 7 unread-command-events))
435 ((and input (string-match "STO\\|RCL" input))
436 (cond ((and isstring (string-match "[0-9]" (car cmd)))
437 (setq calc-keypad-input nil)
438 (let ((var (intern (concat "var-q" (car cmd)))))
439 (cond ((equal input "STO+") (calc-store-plus var))
440 ((equal input "STO-") (calc-store-minus var))
441 ((equal input "STO*") (calc-store-times var))
442 ((equal input "STO/") (calc-store-div var))
443 ((equal input "STO^") (calc-store-power var))
444 ((equal input "STOn") (calc-store-neg 1 var))
445 ((equal input "STO&") (calc-store-inv 1 var))
446 ((equal input "STO") (calc-store-into var))
447 (t (calc-recall var)))))
448 ((memq cmd '(calc-pop calc-undo))
449 (setq calc-keypad-input nil))
450 ((and (equal input "STO")
451 (setq frac (assq cmd '( ( calc-plus . "+" )
454 ( calc-divide . "/" )
456 ( calc-change-sign . "n")
457 ( calc-inv . "&") ))))
458 (setq calc-keypad-input (concat input (cdr frac))))
463 (if (or (and (equal cmd ".")
465 (string-match "[.:e^]" input))
468 (or (and (<= radix 14) (string-match "e" input))
469 (string-match "\\^\\|[-.:]\\'" input)))
470 (and (not (equal cmd "."))
471 (let ((case-fold-search nil))
472 (string-match cmd "0123456789ABCDEF"
474 "[e^]" (or input ""))
477 (setq calc-keypad-input (concat
481 (format "%d#" radix))
484 (or (and (equal cmd "e") "1")
488 (if (and (equal cmd ".") frac)
490 (if (and (equal cmd "e")
495 (format "*%d.^" radix)
497 ((and (eq cmd 'calc-change-sign)
499 (let* ((epos (or iexpon 0))
500 (suffix (substring input epos)))
501 (setq calc-keypad-input (concat
502 (substring input 0 epos)
503 (if (string-match "\\`-" suffix)
505 (concat "-" suffix))))))
506 ((and (eq cmd 'calc-pop)
510 (setq calc-keypad-input (substring input 0
515 ((and (eq cmd 'calc-undo)
517 (setq calc-keypad-input nil))
520 (let ((val (math-read-number input)))
521 (setq calc-keypad-input nil)
524 (calc-push-list (list (calc-record
525 (calc-normalize val)))))
529 (if (eq cmd 'calc-enter) (setq cmd nil))))
530 (setq prefix-arg current-prefix-arg)
532 (if (and (consp cmd) (eq (car cmd) 'progn))
533 (while (setq cmd (cdr cmd))
534 (if (integerp (car cmd))
535 (setq prefix-arg (car cmd))
536 (command-execute (car cmd))))
537 (command-execute cmd)))))
538 (set-buffer calc-keypad-buffer)
539 (calc-keypad-show-input))))
541 (defun calc-keypad-left-click (event)
542 "Handle a left-button mouse click in Calc Keypad window."
544 (with-current-buffer calc-keypad-buffer
545 (goto-char (posn-point (event-start event)))
546 (calc-keypad-press)))
548 (defun calc-keypad-right-click (event)
549 "Handle a right-button mouse click in Calc Keypad window."
552 (set-buffer calc-keypad-buffer)
555 (defun calc-keypad-middle-click (event)
556 "Handle a middle-button mouse click in Calc Keypad window."
558 (with-current-buffer calc-keypad-buffer
559 (calc-keypad-menu-back)))
561 (defun calc-keypad-menu ()
563 (unless (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 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
568 (calc-keypad-redraw))
570 (defun calc-keypad-menu-back ()
572 (or (eq major-mode 'calc-keypad-mode)
573 (error "Must be in *Calc Keypad* buffer for this command"))
574 (while (progn (setq calc-keypad-menu (% (1- (+ calc-keypad-menu
575 (length calc-keypad-menus)))
576 (length calc-keypad-menus)))
577 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
578 (calc-keypad-redraw))
580 (defun calc-keypad-store ()
582 (setq calc-keypad-input "STO"))
584 (defun calc-keypad-recall ()
586 (setq calc-keypad-input "RCL"))
588 (defun calc-pack-interval (mode)
590 (if (or (< mode 0) (> mode 3))
591 (error "Open/close code should be in the range from 0 to 3"))
592 (calc-pack (- -6 mode)))
594 (defun calc-keypad-execute ()
596 (let* ((prompt "Calc keystrokes: ")
597 (flush 'x-flush-mouse-queue)
603 (setq keys (read-key-sequence prompt))
604 (setq cmd (key-binding keys))
605 (if (or (memq cmd '(calc-inverse
610 (and prefix (string-match "\\`\e?[-0-9]\\'" keys)))
612 (setq last-command-char (aref keys (1- (length keys))))
613 (command-execute cmd)
614 (setq flush 'not-any-more
616 prompt (concat prompt (key-description keys) " ")))
617 (eq cmd flush))))) ; skip mouse-up event
620 (command-execute cmd)
621 (error "Not a Calc command: %s" (key-description keys)))))
623 (provide 'calc-keypd)
625 ;;; arch-tag: 4ba0d360-2bb6-40b8-adfa-eb373765b3f9
626 ;;; calc-keypd.el ends here