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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
31 ;; This file is autoloaded from calc-ext.el.
36 (defun calc-Need-calc-keypd () nil
)
39 (defvar calc-keypad-buffer nil
)
40 (defvar calc-keypad-menu
0)
41 (defvar calc-keypad-full-layout nil
)
42 (defvar calc-keypad-input nil
)
43 (defvar calc-keypad-prev-input nil
)
44 (defvar calc-keypad-said-hello nil
)
46 ;;; |----+----+----+----+----+----|
47 ;;; | ENTER |+/- |EEX |UNDO| <- |
48 ;;; |-----+---+-+--+--+-+---++----|
49 ;;; | INV | 7 | 8 | 9 | / |
50 ;;; |-----+-----+-----+-----+-----|
51 ;;; | HYP | 4 | 5 | 6 | * |
52 ;;; |-----+-----+-----+-----+-----|
53 ;;; |EXEC | 1 | 2 | 3 | - |
54 ;;; |-----+-----+-----+-----+-----|
55 ;;; | OFF | 0 | . | PI | + |
56 ;;; |-----+-----+-----+-----+-----|
57 (defvar calc-keypad-layout
58 '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over
)
59 ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over
)
60 ( "+/-" calc-change-sign calc-inv
(progn -
4 calc-pack
) )
61 ( "EEX" ("e") (progn calc-num-prefix calc-pack-interval
)
62 (progn -
5 calc-pack
) )
63 ( "UNDO" calc-undo calc-redo calc-last-args
)
64 ( "<-" calc-pop
(progn 0 calc-pop
)
65 (progn calc-num-prefix calc-pop
) ) )
66 ( ( "INV" calc-inverse
)
67 ( "7" ("7") calc-round
)
68 ( "8" ("8") (progn 2 calc-clean-num
) )
69 ( "9" ("9") calc-float
)
70 ( "/" calc-divide
(progn calc-inverse calc-power
) ) )
71 ( ( "HYP" calc-hyperbolic
)
72 ( "4" ("4") calc-ln calc-log10
)
73 ( "5" ("5") calc-exp calc-exp10
)
74 ( "6" ("6") calc-abs
)
75 ( "*" calc-times calc-power
) )
76 ( ( "EXEC" calc-keypad-execute
)
77 ( "1" ("1") calc-arcsin calc-sin
)
78 ( "2" ("2") calc-arccos calc-cos
)
79 ( "3" ("3") calc-arctan calc-tan
)
80 ( "-" calc-minus calc-conj
) )
81 ( ( "OFF" calc-keypad-off
)
82 ( "0" ("0") calc-imaginary
)
83 ( "." (".") calc-precision
)
85 ( "+" calc-plus calc-sqrt
) ) ))
87 (defvar calc-keypad-menus
'( calc-keypad-math-menu
88 calc-keypad-funcs-menu
89 calc-keypad-binary-menu
90 calc-keypad-vector-menu
91 calc-keypad-modes-menu
92 calc-keypad-user-menu
) )
94 ;;; |----+----+----+----+----+----|
95 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
96 ;;; |----+----+----+----+----+----|
97 ;;; | LN |EXP | |ABS |IDIV|MOD |
98 ;;; |----+----+----+----+----+----|
99 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
101 (defvar calc-keypad-math-menu
102 '( ( ( "FLR" calc-floor
)
103 ( "CEIL" calc-ceiling
)
105 ( "TRNC" calc-trunc
)
106 ( "CLN2" (progn 2 calc-clean-num
) )
107 ( "FLT" calc-float
) )
119 ( "1/x" calc-inv
) ) ))
121 ;;; |----+----+----+----+----+----|
122 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
123 ;;; |----+----+----+----+----+----|
124 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
125 ;;; |----+----+----+----+----+----|
126 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
128 (defvar calc-keypad-funcs-menu
129 '( ( ( "IGAM" calc-inc-gamma
)
131 ( "IBET" calc-inc-beta
)
133 ( "BESJ" calc-bessel-J
)
134 ( "BESY" calc-bessel-Y
) )
135 ( ( "IMAG" calc-imaginary
)
137 ( "RE" calc-re calc-im
)
138 ( "ATN2" calc-arctan2
)
139 ( "RAND" calc-random
)
140 ( "RAGN" calc-random-again
) )
141 ( ( "GCD" calc-gcd calc-lcm
)
142 ( "FACT" calc-factorial calc-gamma
)
143 ( "DFCT" calc-double-factorial
)
144 ( "BNOM" calc-choose
)
146 ( "NXTP" calc-next-prime calc-prev-prime
) ) ))
148 ;;; |----+----+----+----+----+----|
149 ;;; |AND | OR |XOR |NOT |LSH |RSH |
150 ;;; |----+----+----+----+----+----|
151 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
152 ;;; |----+----+----+----+----+----|
153 ;;; | A | B | C | D | E | F |
155 (defvar calc-keypad-binary-menu
156 '( ( ( "AND" calc-and calc-diff
)
159 ( "NOT" calc-not calc-clip
)
160 ( "LSH" calc-lshift-binary calc-rotate-binary
)
161 ( "RSH" calc-rshift-binary
) )
162 ( ( "DEC" calc-decimal-radix
)
163 ( "HEX" calc-hex-radix
)
164 ( "OCT" calc-octal-radix
)
165 ( "BIN" calc-binary-radix
)
166 ( "WSIZ" calc-word-size
)
167 ( "ARSH" calc-rshift-arith
) )
175 ;;; |----+----+----+----+----+----|
176 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
177 ;;; |----+----+----+----+----+----|
178 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
179 ;;; |----+----+----+----+----+----|
180 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
182 (defvar calc-keypad-vector-menu
183 '( ( ( "SUM" calc-vector-sum calc-vector-alt-sum calc-vector-mean
)
184 ( "PROD" calc-vector-product nil calc-vector-sdev
)
185 ( "MAX" calc-vector-max calc-vector-min calc-vector-median
)
186 ( "MAP*" (lambda () (interactive)
187 (calc-map '(2 calcFunc-mul
"*"))) )
188 ( "MAP^" (lambda () (interactive)
189 (calc-map '(2 calcFunc-pow
"^"))) )
190 ( "MAP$" calc-map-stack
) )
191 ( ( "MINV" calc-inv
)
193 ( "MTRN" calc-transpose calc-conj-transpose
)
194 ( "IDNT" (progn calc-num-prefix calc-ident
) )
195 ( "CRSS" calc-cross
)
196 ( "\"x\"" "\excalc-algebraic-entry\rx\r"
197 "\excalc-algebraic-entry\ry\r"
198 "\excalc-algebraic-entry\rz\r"
199 "\excalc-algebraic-entry\rt\r") )
200 ( ( "PACK" calc-pack
)
201 ( "UNPK" calc-unpack
)
202 ( "INDX" (progn calc-num-prefix calc-index
) "\C-u\excalc-index\r" )
203 ( "BLD" (progn calc-num-prefix calc-build-vector
) )
204 ( "LEN" calc-vlength
)
205 ( "..." calc-full-vectors
) ) ))
207 ;;; |----+----+----+----+----+----|
208 ;;; |FLT |FIX |SCI |ENG |GRP | |
209 ;;; |----+----+----+----+----+----|
210 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
211 ;;; |----+----+----+----+----+----|
212 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
214 (defvar calc-keypad-modes-menu
215 '( ( ( "FLT" calc-normal-notation
216 (progn calc-num-prefix calc-normal-notation
) )
217 ( "FIX" (progn 2 calc-fix-notation
)
218 (progn calc-num-prefix calc-fix-notation
) )
219 ( "SCI" calc-sci-notation
220 (progn calc-num-prefix calc-sci-notation
) )
221 ( "ENG" calc-eng-notation
222 (progn calc-num-prefix calc-eng-notation
) )
223 ( "GRP" calc-group-digits
"\C-u-3\excalc-group-digits\r" )
225 ( ( "RAD" calc-radians-mode
)
226 ( "DEG" calc-degrees-mode
)
227 ( "FRAC" calc-frac-mode
)
228 ( "POLR" calc-polar-mode
)
229 ( "SYMB" calc-symbolic-mode
)
230 ( "PREC" calc-precision
) )
231 ( ( "SWAP" calc-roll-down
)
232 ( "RLL3" (progn 3 calc-roll-up
) (progn 3 calc-roll-down
) )
233 ( "RLL4" (progn 4 calc-roll-up
) (progn 4 calc-roll-down
) )
235 ( "STO" calc-keypad-store
)
236 ( "RCL" calc-keypad-recall
) ) ))
238 (define-derived-mode calc-keypad-mode fundamental-mode
"Calculator"
239 "Major mode for Calc keypad input."
240 (define-key calc-keypad-mode-map
" " 'calc-keypad-press
)
241 (define-key calc-keypad-mode-map
(kbd "RET") 'calc-keypad-press
)
242 (define-key calc-keypad-mode-map
(kbd "TAB") 'calc-keypad-menu
)
243 (define-key calc-keypad-mode-map
"q" 'calc-keypad-off
)
244 (define-key calc-keypad-mode-map
[(mouse-3)] 'calc-keypad-right-click
)
245 (define-key calc-keypad-mode-map
[(mouse-2)] 'calc-keypad-middle-click
)
246 (define-key calc-keypad-mode-map
[(mouse-1)] 'calc-keypad-left-click
)
247 (put 'calc-keypad-mode
'mode-class
'special
)
248 (make-local-variable 'calc-main-buffer
))
250 (defun calc-do-keypad (&optional full-display interactive
)
252 (let ((calcbuf (current-buffer)))
253 (unless (bufferp calc-keypad-buffer
)
254 (set-buffer (setq calc-keypad-buffer
(get-buffer-create "*Calc Keypad*")))
256 (setq calc-main-buffer calcbuf
)
262 (if (setq win
(get-buffer-window "*Calculator*"))
264 (if (setq win
(get-buffer-window "*Calc Trail*"))
266 (switch-to-buffer (other-buffer))
267 (delete-window win
)))
268 (if (setq win
(get-buffer-window calc-keypad-buffer
))
270 (bury-buffer "*Calculator*")
271 (bury-buffer "*Calc Trail*")
272 (bury-buffer calc-keypad-buffer
)
274 (switch-to-buffer (other-buffer))
275 (delete-window win
)))
276 (setq calc-was-keypad-mode t
277 old-win
(get-largest-window))
278 (if (or (< (window-height old-win
) (+ height
6))
279 (< (window-width old-win
) (+ width
15))
281 (delete-other-windows old-win
))
282 (if (< (window-height old-win
) (+ height
4))
283 (error "Screen is not tall enough for this mode"))
286 (setq win
(split-window old-win
(- (window-height old-win
)
288 (set-window-buffer old-win
(calc-trail-buffer))
289 (set-window-buffer win calc-keypad-buffer
)
290 (set-window-start win
1)
291 (setq win
(split-window win
(+ width
3) t
))
292 (set-window-buffer win calcbuf
))
293 (if (or t
; left-side keypad not yet fully implemented
295 (set-buffer (window-buffer old-win
))
297 (/ (window-width) 2)))
298 (setq win
(split-window old-win
(- (window-width old-win
)
301 (setq old-win
(split-window old-win
(+ width
2) t
)))
302 (set-window-buffer win calc-keypad-buffer
)
303 (set-window-start win
1)
304 (split-window win
(- (window-height win
) height
1))
305 (set-window-buffer win calcbuf
))
306 (select-window old-win
)
307 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons")
308 (run-hooks 'calc-keypad-start-hook
)
309 (and calc-keypad-said-hello interactive
313 (setq calc-keypad-said-hello t
)))
314 (setq calc-keypad-input nil
)))
316 (defun calc-keypad-off ()
318 (if calc-standalone-flag
319 (save-buffers-kill-emacs nil
)
322 (defun calc-keypad-redraw ()
323 (set-buffer calc-keypad-buffer
)
324 (setq buffer-read-only t
)
325 (setq calc-keypad-full-layout
(append (symbol-value (nth calc-keypad-menu
328 (let ((buffer-read-only nil
)
329 (row calc-keypad-full-layout
)
334 (let ((col (car row
)))
336 (let* ((key (car col
))
339 (if (and (= y
3) (eq col
(car row
)))
340 (progn (setq col
(cdr col
)) 9)
342 (name (if (and calc-standalone-flag
343 (eq (nth 1 key
) 'calc-keypad-off
))
345 (if (> (length (car key
)) cwid
)
346 (substring (car key
) 0 cwid
)
349 (pad (- cwid
(/ wid
2))))
350 (insert (make-string (/ (- cwid wid
) 2) 32)
352 (make-string (/ (- cwid wid -
1) 2) 32)
353 (if (equal name
"MENU")
354 (int-to-string (1+ calc-keypad-menu
))
356 (or (setq col
(cdr col
))
359 "-----+-----+-----+-----+-----"
361 "-----+---+-+--+--+-+---++----"
362 "----+----+----+----+----+----"))
363 (if (= y
7) "+\n" "|\n"))
366 (setq calc-keypad-prev-input t
)
367 (calc-keypad-show-input)
368 (goto-char (point-min)))
370 (defun calc-keypad-show-input ()
371 (or (equal calc-keypad-input calc-keypad-prev-input
)
372 (let ((buffer-read-only nil
))
374 (goto-char (point-min))
376 (delete-region (point-min) (point))
377 (if calc-keypad-input
378 (insert "Calc: " calc-keypad-input
"\n")
379 (insert "----+-----Calc " calc-version
"-----+----"
380 (int-to-string (1+ calc-keypad-menu
))
382 (setq calc-keypad-prev-input calc-keypad-input
))
384 (defun calc-keypad-press ()
386 (unless (eq major-mode
'calc-keypad-mode
)
387 (error "Must be in *Calc Keypad* buffer for this command"))
388 (let* ((row (save-excursion
390 (count-lines (point-min) (point))))
392 (x (/ (current-column) (if (>= y
4) 6 5)))
394 (hyp (with-current-buffer calc-main-buffer
395 (setq radix calc-number-radix
396 frac calc-prefer-frac
397 inv calc-inverse-flag
)
398 calc-hyperbolic-flag
))
400 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))
401 (input calc-keypad-input
)
403 (or (string-match "\\*[0-9]+\\.\\^" input
)
404 (and (<= radix
14) (string-match "e" input
)))
406 (key (nth x
(nth y calc-keypad-full-layout
)))
407 (cmd (or (nth (if inv
(if hyp
4 2) (if hyp
3 99)) key
)
410 (isstring (and (consp cmd
) (stringp (car cmd
))))
411 (calc-is-keypad-press t
))
412 (if invhyp
(calc-wrapper)) ; clear Inv and Hyp flags
414 (cond ((or (null cmd
)
417 ((and (> (minibuffer-depth) 0))
419 (push (aref (car cmd
) 0) unread-command-events
))
421 (push ?
\177 unread-command-events
))
422 ((eq cmd
'calc-enter
)
423 (push 13 unread-command-events
))
425 (push 7 unread-command-events
))
428 ((and input
(string-match "STO\\|RCL" input
))
429 (cond ((and isstring
(string-match "[0-9]" (car cmd
)))
430 (setq calc-keypad-input nil
)
431 (let ((var (intern (concat "var-q" (car cmd
)))))
432 (cond ((equal input
"STO+") (calc-store-plus var
))
433 ((equal input
"STO-") (calc-store-minus var
))
434 ((equal input
"STO*") (calc-store-times var
))
435 ((equal input
"STO/") (calc-store-div var
))
436 ((equal input
"STO^") (calc-store-power var
))
437 ((equal input
"STOn") (calc-store-neg 1 var
))
438 ((equal input
"STO&") (calc-store-inv 1 var
))
439 ((equal input
"STO") (calc-store-into var
))
440 (t (calc-recall var
)))))
441 ((memq cmd
'(calc-pop calc-undo
))
442 (setq calc-keypad-input nil
))
443 ((and (equal input
"STO")
444 (setq frac
(assq cmd
'( ( calc-plus .
"+" )
447 ( calc-divide .
"/" )
449 ( calc-change-sign .
"n")
450 ( calc-inv .
"&") ))))
451 (setq calc-keypad-input
(concat input
(cdr frac
))))
456 (if (or (and (equal cmd
".")
458 (string-match "[.:e^]" input
))
461 (or (and (<= radix
14) (string-match "e" input
))
462 (string-match "\\^\\|[-.:]\\'" input
)))
463 (and (not (equal cmd
"."))
464 (let ((case-fold-search nil
))
465 (string-match cmd
"0123456789ABCDEF"
467 "[e^]" (or input
""))
470 (setq calc-keypad-input
(concat
474 (format "%d#" radix
))
477 (or (and (equal cmd
"e") "1")
481 (if (and (equal cmd
".") frac
)
483 (if (and (equal cmd
"e")
488 (format "*%d.^" radix
)
490 ((and (eq cmd
'calc-change-sign
)
492 (let* ((epos (or iexpon
0))
493 (suffix (substring input epos
)))
494 (setq calc-keypad-input
(concat
495 (substring input
0 epos
)
496 (if (string-match "\\`-" suffix
)
498 (concat "-" suffix
))))))
499 ((and (eq cmd
'calc-pop
)
503 (setq calc-keypad-input
(substring input
0
508 ((and (eq cmd
'calc-undo
)
510 (setq calc-keypad-input nil
))
513 (let ((val (math-read-number input
)))
514 (setq calc-keypad-input nil
)
517 (calc-push-list (list (calc-record
518 (calc-normalize val
)))))
522 (if (eq cmd
'calc-enter
) (setq cmd nil
))))
523 (setq prefix-arg current-prefix-arg
)
525 (if (and (consp cmd
) (eq (car cmd
) 'progn
))
526 (while (setq cmd
(cdr cmd
))
527 (if (integerp (car cmd
))
528 (setq prefix-arg
(car cmd
))
529 (command-execute (car cmd
))))
530 (command-execute cmd
)))))
531 (set-buffer calc-keypad-buffer
)
532 (calc-keypad-show-input))))
534 (defun calc-keypad-left-click (event)
535 "Handle a left-button mouse click in Calc Keypad window."
537 (goto-char (posn-point (event-start event
)))
540 (defun calc-keypad-right-click (event)
541 "Handle a right-button mouse click in Calc Keypad window."
544 (set-buffer calc-keypad-buffer
)
547 (defun calc-keypad-middle-click (event)
548 "Handle a middle-button mouse click in Calc Keypad window."
550 (with-current-buffer calc-keypad-buffer
551 (calc-keypad-menu-back)))
553 (defun calc-keypad-menu ()
555 (unless (eq major-mode
'calc-keypad-mode
)
556 (error "Must be in *Calc Keypad* buffer for this command"))
557 (while (progn (setq calc-keypad-menu
(%
(1+ calc-keypad-menu
)
558 (length calc-keypad-menus
)))
559 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
560 (calc-keypad-redraw))
562 (defun calc-keypad-menu-back ()
564 (or (eq major-mode
'calc-keypad-mode
)
565 (error "Must be in *Calc Keypad* buffer for this command"))
566 (while (progn (setq calc-keypad-menu
(%
(1- (+ calc-keypad-menu
567 (length calc-keypad-menus
)))
568 (length calc-keypad-menus
)))
569 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
570 (calc-keypad-redraw))
572 (defun calc-keypad-store ()
574 (setq calc-keypad-input
"STO"))
576 (defun calc-keypad-recall ()
578 (setq calc-keypad-input
"RCL"))
580 (defun calc-pack-interval (mode)
582 (if (or (< mode
0) (> mode
3))
583 (error "Open/close code should be in the range from 0 to 3"))
584 (calc-pack (- -
6 mode
)))
586 (defun calc-keypad-execute ()
588 (let* ((prompt "Calc keystrokes: ")
589 (flush 'x-flush-mouse-queue
)
595 (setq keys
(read-key-sequence prompt
))
596 (setq cmd
(key-binding keys
))
597 (if (or (memq cmd
'(calc-inverse
602 (and prefix
(string-match "\\`\e?[-0-9]\\'" keys
)))
604 (setq last-command-char
(aref keys
(1- (length keys
))))
605 (command-execute cmd
)
606 (setq flush
'not-any-more
608 prompt
(concat prompt
(key-description keys
) " ")))
609 (eq cmd flush
))))) ; skip mouse-up event
612 (command-execute cmd
)
613 (error "Not a Calc command: %s" (key-description keys
)))))
616 ;;; arch-tag: 4ba0d360-2bb6-40b8-adfa-eb373765b3f9
617 ;;; calc-keypd.el ends here