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
[(mouse-3)] 'calc-keypad-right-click
)
240 (define-key calc-keypad-mode-map
[(mouse-2)] 'calc-keypad-middle-click
)
241 (define-key calc-keypad-mode-map
[(mouse-1)] 'calc-keypad-left-click
)
242 (put 'calc-keypad-mode
'mode-class
'special
)
243 (make-local-variable 'calc-main-buffer
))
245 (defun calc-do-keypad (&optional full-display interactive
)
247 (let ((calcbuf (current-buffer)))
248 (unless (bufferp calc-keypad-buffer
)
249 (set-buffer (setq calc-keypad-buffer
(get-buffer-create "*Calc Keypad*")))
251 (setq calc-main-buffer calcbuf
)
257 (if (setq win
(get-buffer-window "*Calculator*"))
259 (if (setq win
(get-buffer-window "*Calc Trail*"))
261 (switch-to-buffer (other-buffer))
262 (delete-window win
)))
263 (if (setq win
(get-buffer-window calc-keypad-buffer
))
265 (bury-buffer "*Calculator*")
266 (bury-buffer "*Calc Trail*")
267 (bury-buffer calc-keypad-buffer
)
269 (switch-to-buffer (other-buffer))
270 (delete-window win
)))
271 (setq calc-was-keypad-mode t
272 old-win
(get-largest-window))
273 (if (or (< (window-height old-win
) (+ height
6))
274 (< (window-width old-win
) (+ width
15))
276 (delete-other-windows old-win
))
277 (if (< (window-height old-win
) (+ height
4))
278 (error "Screen is not tall enough for this mode"))
281 (setq win
(split-window old-win
(- (window-height old-win
)
283 (set-window-buffer old-win
(calc-trail-buffer))
284 (set-window-buffer win calc-keypad-buffer
)
285 (set-window-start win
1)
286 (setq win
(split-window win
(+ width
3) t
))
287 (set-window-buffer win calcbuf
))
288 (if (or t
; left-side keypad not yet fully implemented
290 (set-buffer (window-buffer old-win
))
292 (/ (window-width) 2)))
293 (setq win
(split-window old-win
(- (window-width old-win
)
296 (setq old-win
(split-window old-win
(+ width
2) t
)))
297 (set-window-buffer win calc-keypad-buffer
)
298 (set-window-start win
1)
299 (split-window win
(- (window-height win
) height
1))
300 (set-window-buffer win calcbuf
))
301 (select-window old-win
)
302 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons")
303 (run-hooks 'calc-keypad-start-hook
)
304 (and calc-keypad-said-hello interactive
308 (setq calc-keypad-said-hello t
)))
309 (setq calc-keypad-input nil
)))
311 (defun calc-keypad-off ()
313 (if calc-standalone-flag
314 (save-buffers-kill-emacs nil
)
317 (defun calc-keypad-redraw ()
318 (set-buffer calc-keypad-buffer
)
319 (setq buffer-read-only t
)
320 (setq calc-keypad-full-layout
(append (symbol-value (nth calc-keypad-menu
323 (let ((buffer-read-only nil
)
324 (row calc-keypad-full-layout
)
329 (let ((col (car row
)))
331 (let* ((key (car col
))
334 (if (and (= y
3) (eq col
(car row
)))
335 (progn (setq col
(cdr col
)) 9)
337 (name (if (and calc-standalone-flag
338 (eq (nth 1 key
) 'calc-keypad-off
))
340 (if (> (length (car key
)) cwid
)
341 (substring (car key
) 0 cwid
)
344 (pad (- cwid
(/ wid
2))))
345 (insert (make-string (/ (- cwid wid
) 2) 32)
347 (make-string (/ (- cwid wid -
1) 2) 32)
348 (if (equal name
"MENU")
349 (int-to-string (1+ calc-keypad-menu
))
351 (or (setq col
(cdr col
))
354 "-----+-----+-----+-----+-----"
356 "-----+---+-+--+--+-+---++----"
357 "----+----+----+----+----+----"))
358 (if (= y
7) "+\n" "|\n"))
361 (setq calc-keypad-prev-input t
)
362 (calc-keypad-show-input)
363 (goto-char (point-min)))
365 (defun calc-keypad-show-input ()
366 (or (equal calc-keypad-input calc-keypad-prev-input
)
367 (let ((buffer-read-only nil
))
369 (goto-char (point-min))
371 (delete-region (point-min) (point))
372 (if calc-keypad-input
373 (insert "Calc: " calc-keypad-input
"\n")
374 (insert "----+-----Calc " calc-version
"-----+----"
375 (int-to-string (1+ calc-keypad-menu
))
377 (setq calc-keypad-prev-input calc-keypad-input
))
379 (defun calc-keypad-press ()
381 (unless (eq major-mode
'calc-keypad-mode
)
382 (error "Must be in *Calc Keypad* buffer for this command"))
383 (let* ((row (save-excursion
385 (count-lines (point-min) (point))))
387 (x (/ (current-column) (if (>= y
4) 6 5)))
389 (hyp (with-current-buffer calc-main-buffer
390 (setq radix calc-number-radix
391 frac calc-prefer-frac
392 inv calc-inverse-flag
)
393 calc-hyperbolic-flag
))
395 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))
396 (input calc-keypad-input
)
398 (or (string-match "\\*[0-9]+\\.\\^" input
)
399 (and (<= radix
14) (string-match "e" input
)))
401 (key (nth x
(nth y calc-keypad-full-layout
)))
402 (cmd (or (nth (if inv
(if hyp
4 2) (if hyp
3 99)) key
)
405 (isstring (and (consp cmd
) (stringp (car cmd
))))
406 (calc-is-keypad-press t
))
407 (if invhyp
(calc-wrapper)) ; clear Inv and Hyp flags
409 (cond ((or (null cmd
)
412 ((and (> (minibuffer-depth) 0))
414 (push (aref (car cmd
) 0) unread-command-events
))
416 (push ?
\177 unread-command-events
))
417 ((eq cmd
'calc-enter
)
418 (push 13 unread-command-events
))
420 (push 7 unread-command-events
))
423 ((and input
(string-match "STO\\|RCL" input
))
424 (cond ((and isstring
(string-match "[0-9]" (car cmd
)))
425 (setq calc-keypad-input nil
)
426 (let ((var (intern (concat "var-q" (car cmd
)))))
427 (cond ((equal input
"STO+") (calc-store-plus var
))
428 ((equal input
"STO-") (calc-store-minus var
))
429 ((equal input
"STO*") (calc-store-times var
))
430 ((equal input
"STO/") (calc-store-div var
))
431 ((equal input
"STO^") (calc-store-power var
))
432 ((equal input
"STOn") (calc-store-neg 1 var
))
433 ((equal input
"STO&") (calc-store-inv 1 var
))
434 ((equal input
"STO") (calc-store-into var
))
435 (t (calc-recall var
)))))
436 ((memq cmd
'(calc-pop calc-undo
))
437 (setq calc-keypad-input nil
))
438 ((and (equal input
"STO")
439 (setq frac
(assq cmd
'( ( calc-plus .
"+" )
442 ( calc-divide .
"/" )
444 ( calc-change-sign .
"n")
445 ( calc-inv .
"&") ))))
446 (setq calc-keypad-input
(concat input
(cdr frac
))))
451 (if (or (and (equal cmd
".")
453 (string-match "[.:e^]" input
))
456 (or (and (<= radix
14) (string-match "e" input
))
457 (string-match "\\^\\|[-.:]\\'" input
)))
458 (and (not (equal cmd
"."))
459 (let ((case-fold-search nil
))
460 (string-match cmd
"0123456789ABCDEF"
462 "[e^]" (or input
""))
465 (setq calc-keypad-input
(concat
469 (format "%d#" radix
))
472 (or (and (equal cmd
"e") "1")
476 (if (and (equal cmd
".") frac
)
478 (if (and (equal cmd
"e")
483 (format "*%d.^" radix
)
485 ((and (eq cmd
'calc-change-sign
)
487 (let* ((epos (or iexpon
0))
488 (suffix (substring input epos
)))
489 (setq calc-keypad-input
(concat
490 (substring input
0 epos
)
491 (if (string-match "\\`-" suffix
)
493 (concat "-" suffix
))))))
494 ((and (eq cmd
'calc-pop
)
498 (setq calc-keypad-input
(substring input
0
503 ((and (eq cmd
'calc-undo
)
505 (setq calc-keypad-input nil
))
508 (let ((val (math-read-number input
)))
509 (setq calc-keypad-input nil
)
512 (calc-push-list (list (calc-record
513 (calc-normalize val
)))))
517 (if (eq cmd
'calc-enter
) (setq cmd nil
))))
518 (setq prefix-arg current-prefix-arg
)
520 (if (and (consp cmd
) (eq (car cmd
) 'progn
))
521 (while (setq cmd
(cdr cmd
))
522 (if (integerp (car cmd
))
523 (setq prefix-arg
(car cmd
))
524 (command-execute (car cmd
))))
525 (command-execute cmd
)))))
526 (set-buffer calc-keypad-buffer
)
527 (calc-keypad-show-input))))
529 (defun calc-keypad-left-click (event)
530 "Handle a left-button mouse click in Calc Keypad window."
532 (goto-char (posn-point (event-start event
)))
535 (defun calc-keypad-right-click (event)
536 "Handle a right-button mouse click in Calc Keypad window."
539 (set-buffer calc-keypad-buffer
)
542 (defun calc-keypad-middle-click (event)
543 "Handle a middle-button mouse click in Calc Keypad window."
545 (with-current-buffer calc-keypad-buffer
546 (calc-keypad-menu-back)))
548 (defun calc-keypad-menu ()
550 (unless (eq major-mode
'calc-keypad-mode
)
551 (error "Must be in *Calc Keypad* buffer for this command"))
552 (while (progn (setq calc-keypad-menu
(%
(1+ calc-keypad-menu
)
553 (length calc-keypad-menus
)))
554 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
555 (calc-keypad-redraw))
557 (defun calc-keypad-menu-back ()
559 (or (eq major-mode
'calc-keypad-mode
)
560 (error "Must be in *Calc Keypad* buffer for this command"))
561 (while (progn (setq calc-keypad-menu
(%
(1- (+ calc-keypad-menu
562 (length calc-keypad-menus
)))
563 (length calc-keypad-menus
)))
564 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
565 (calc-keypad-redraw))
567 (defun calc-keypad-store ()
569 (setq calc-keypad-input
"STO"))
571 (defun calc-keypad-recall ()
573 (setq calc-keypad-input
"RCL"))
575 (defun calc-pack-interval (mode)
577 (if (or (< mode
0) (> mode
3))
578 (error "Open/close code should be in the range from 0 to 3"))
579 (calc-pack (- -
6 mode
)))
581 (defun calc-keypad-execute ()
583 (let* ((prompt "Calc keystrokes: ")
584 (flush 'x-flush-mouse-queue
)
590 (setq keys
(read-key-sequence prompt
))
591 (setq cmd
(key-binding keys
))
592 (if (or (memq cmd
'(calc-inverse
597 (and prefix
(string-match "\\`\e?[-0-9]\\'" keys
)))
599 (setq last-command-char
(aref keys
(1- (length keys
))))
600 (command-execute cmd
)
601 (setq flush
'not-any-more
603 prompt
(concat prompt
(key-description keys
) " ")))
604 (eq cmd flush
))))) ; skip mouse-up event
607 (command-execute cmd
)
608 (error "Not a Calc command: %s" (key-description keys
)))))
610 (provide 'calc-keypd
)
612 ;;; arch-tag: 4ba0d360-2bb6-40b8-adfa-eb373765b3f9
613 ;;; calc-keypd.el ends here