1 ;;; calc-keypd.el --- mouse-capable keypad input for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 (defvar calc-keypad-buffer nil
)
33 (defvar calc-keypad-menu
0)
34 (defvar calc-keypad-full-layout nil
)
35 (defvar calc-keypad-input nil
)
36 (defvar calc-keypad-prev-input nil
)
37 (defvar calc-keypad-said-hello nil
)
39 ;;; |----+----+----+----+----+----|
40 ;;; | ENTER |+/- |EEX |UNDO| <- |
41 ;;; |-----+---+-+--+--+-+---++----|
42 ;;; | INV | 7 | 8 | 9 | / |
43 ;;; |-----+-----+-----+-----+-----|
44 ;;; | HYP | 4 | 5 | 6 | * |
45 ;;; |-----+-----+-----+-----+-----|
46 ;;; |EXEC | 1 | 2 | 3 | - |
47 ;;; |-----+-----+-----+-----+-----|
48 ;;; | OFF | 0 | . | PI | + |
49 ;;; |-----+-----+-----+-----+-----|
50 (defvar calc-keypad-layout
51 '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over
)
52 ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over
)
53 ( "+/-" calc-change-sign calc-inv
(progn -
4 calc-pack
) )
54 ( "EEX" ("e") (progn calc-num-prefix calc-pack-interval
)
55 (progn -
5 calc-pack
) )
56 ( "UNDO" calc-undo calc-redo calc-last-args
)
57 ( "<-" calc-pop
(progn 0 calc-pop
)
58 (progn calc-num-prefix calc-pop
) ) )
59 ( ( "INV" calc-inverse
)
60 ( "7" ("7") calc-round
)
61 ( "8" ("8") (progn 2 calc-clean-num
) )
62 ( "9" ("9") calc-float
)
63 ( "/" calc-divide
(progn calc-inverse calc-power
) ) )
64 ( ( "HYP" calc-hyperbolic
)
65 ( "4" ("4") calc-ln calc-log10
)
66 ( "5" ("5") calc-exp calc-exp10
)
67 ( "6" ("6") calc-abs
)
68 ( "*" calc-times calc-power
) )
69 ( ( "EXEC" calc-keypad-execute
)
70 ( "1" ("1") calc-arcsin calc-sin
)
71 ( "2" ("2") calc-arccos calc-cos
)
72 ( "3" ("3") calc-arctan calc-tan
)
73 ( "-" calc-minus calc-conj
) )
74 ( ( "OFF" calc-keypad-off
)
75 ( "0" ("0") calc-imaginary
)
76 ( "." (".") calc-precision
)
78 ( "+" calc-plus calc-sqrt
) ) ))
80 (defvar calc-keypad-menus
'( calc-keypad-math-menu
81 calc-keypad-funcs-menu
82 calc-keypad-binary-menu
83 calc-keypad-vector-menu
84 calc-keypad-modes-menu
85 calc-keypad-user-menu
) )
87 ;;; |----+----+----+----+----+----|
88 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
89 ;;; |----+----+----+----+----+----|
90 ;;; | LN |EXP | |ABS |IDIV|MOD |
91 ;;; |----+----+----+----+----+----|
92 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
94 (defvar calc-keypad-math-menu
95 '( ( ( "FLR" calc-floor
)
96 ( "CEIL" calc-ceiling
)
99 ( "CLN2" (progn 2 calc-clean-num
) )
100 ( "FLT" calc-float
) )
112 ( "1/x" calc-inv
) ) ))
114 ;;; |----+----+----+----+----+----|
115 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
116 ;;; |----+----+----+----+----+----|
117 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
118 ;;; |----+----+----+----+----+----|
119 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
121 (defvar calc-keypad-funcs-menu
122 '( ( ( "IGAM" calc-inc-gamma
)
124 ( "IBET" calc-inc-beta
)
126 ( "BESJ" calc-bessel-J
)
127 ( "BESY" calc-bessel-Y
) )
128 ( ( "IMAG" calc-imaginary
)
130 ( "RE" calc-re calc-im
)
131 ( "ATN2" calc-arctan2
)
132 ( "RAND" calc-random
)
133 ( "RAGN" calc-random-again
) )
134 ( ( "GCD" calc-gcd calc-lcm
)
135 ( "FACT" calc-factorial calc-gamma
)
136 ( "DFCT" calc-double-factorial
)
137 ( "BNOM" calc-choose
)
139 ( "NXTP" calc-next-prime calc-prev-prime
) ) ))
141 ;;; |----+----+----+----+----+----|
142 ;;; |AND | OR |XOR |NOT |LSH |RSH |
143 ;;; |----+----+----+----+----+----|
144 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
145 ;;; |----+----+----+----+----+----|
146 ;;; | A | B | C | D | E | F |
148 (defvar calc-keypad-binary-menu
149 '( ( ( "AND" calc-and calc-diff
)
152 ( "NOT" calc-not calc-clip
)
153 ( "LSH" calc-lshift-binary calc-rotate-binary
)
154 ( "RSH" calc-rshift-binary
) )
155 ( ( "DEC" calc-decimal-radix
)
156 ( "HEX" calc-hex-radix
)
157 ( "OCT" calc-octal-radix
)
158 ( "BIN" calc-binary-radix
)
159 ( "WSIZ" calc-word-size
)
160 ( "ARSH" calc-rshift-arith
) )
168 ;;; |----+----+----+----+----+----|
169 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
170 ;;; |----+----+----+----+----+----|
171 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
172 ;;; |----+----+----+----+----+----|
173 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
175 (defvar calc-keypad-vector-menu
176 '( ( ( "SUM" calc-vector-sum calc-vector-alt-sum calc-vector-mean
)
177 ( "PROD" calc-vector-product nil calc-vector-sdev
)
178 ( "MAX" calc-vector-max calc-vector-min calc-vector-median
)
179 ( "MAP*" (lambda () (interactive)
180 (calc-map '(2 calcFunc-mul
"*"))) )
181 ( "MAP^" (lambda () (interactive)
182 (calc-map '(2 calcFunc-pow
"^"))) )
183 ( "MAP$" calc-map-stack
) )
184 ( ( "MINV" calc-inv
)
186 ( "MTRN" calc-transpose calc-conj-transpose
)
187 ( "IDNT" (progn calc-num-prefix calc-ident
) )
188 ( "CRSS" calc-cross
)
189 ( "\"x\"" "\excalc-algebraic-entry\rx\r"
190 "\excalc-algebraic-entry\ry\r"
191 "\excalc-algebraic-entry\rz\r"
192 "\excalc-algebraic-entry\rt\r") )
193 ( ( "PACK" calc-pack
)
194 ( "UNPK" calc-unpack
)
195 ( "INDX" (progn calc-num-prefix calc-index
) "\C-u\excalc-index\r" )
196 ( "BLD" (progn calc-num-prefix calc-build-vector
) )
197 ( "LEN" calc-vlength
)
198 ( "..." calc-full-vectors
) ) ))
200 ;;; |----+----+----+----+----+----|
201 ;;; |FLT |FIX |SCI |ENG |GRP | |
202 ;;; |----+----+----+----+----+----|
203 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
204 ;;; |----+----+----+----+----+----|
205 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
207 (defvar calc-keypad-modes-menu
208 '( ( ( "FLT" calc-normal-notation
209 (progn calc-num-prefix calc-normal-notation
) )
210 ( "FIX" (progn 2 calc-fix-notation
)
211 (progn calc-num-prefix calc-fix-notation
) )
212 ( "SCI" calc-sci-notation
213 (progn calc-num-prefix calc-sci-notation
) )
214 ( "ENG" calc-eng-notation
215 (progn calc-num-prefix calc-eng-notation
) )
216 ( "GRP" calc-group-digits
"\C-u-3\excalc-group-digits\r" )
218 ( ( "RAD" calc-radians-mode
)
219 ( "DEG" calc-degrees-mode
)
220 ( "FRAC" calc-frac-mode
)
221 ( "POLR" calc-polar-mode
)
222 ( "SYMB" calc-symbolic-mode
)
223 ( "PREC" calc-precision
) )
224 ( ( "SWAP" calc-roll-down
)
225 ( "RLL3" (progn 3 calc-roll-up
) (progn 3 calc-roll-down
) )
226 ( "RLL4" (progn 4 calc-roll-up
) (progn 4 calc-roll-down
) )
228 ( "STO" calc-keypad-store
)
229 ( "RCL" calc-keypad-recall
) ) ))
231 (define-derived-mode calc-keypad-mode fundamental-mode
"Calculator"
232 "Major mode for Calc keypad input."
233 (define-key calc-keypad-mode-map
" " 'calc-keypad-press
)
234 (define-key calc-keypad-mode-map
(kbd "RET") 'calc-keypad-press
)
235 (define-key calc-keypad-mode-map
(kbd "TAB") 'calc-keypad-menu
)
236 (define-key calc-keypad-mode-map
"q" 'calc-keypad-off
)
237 (define-key calc-keypad-mode-map
[down-mouse-1
] 'ignore
)
238 (define-key calc-keypad-mode-map
[drag-mouse-1
] 'ignore
)
239 (define-key calc-keypad-mode-map
[double-mouse-1
] 'ignore
)
240 (define-key calc-keypad-mode-map
[triple-mouse-1
] 'ignore
)
241 (define-key calc-keypad-mode-map
[down-mouse-2
] 'ignore
)
242 (define-key calc-keypad-mode-map
[drag-mouse-2
] 'ignore
)
243 (define-key calc-keypad-mode-map
[double-mouse-2
] 'ignore
)
244 (define-key calc-keypad-mode-map
[triple-mouse-2
] 'ignore
)
245 (define-key calc-keypad-mode-map
[down-mouse-3
] 'ignore
)
246 (define-key calc-keypad-mode-map
[drag-mouse-3
] 'ignore
)
247 (define-key calc-keypad-mode-map
[double-mouse-3
] 'ignore
)
248 (define-key calc-keypad-mode-map
[triple-mouse-3
] 'ignore
)
249 (define-key calc-keypad-mode-map
[mouse-3
] 'calc-keypad-right-click
)
250 (define-key calc-keypad-mode-map
[mouse-2
] 'calc-keypad-middle-click
)
251 (define-key calc-keypad-mode-map
[mouse-1
] 'calc-keypad-left-click
)
252 (put 'calc-keypad-mode
'mode-class
'special
)
253 (make-local-variable 'calc-main-buffer
))
255 (defun calc-do-keypad (&optional full-display interactive
)
257 (let ((calcbuf (current-buffer)))
258 (unless (bufferp calc-keypad-buffer
)
259 (set-buffer (setq calc-keypad-buffer
(get-buffer-create "*Calc Keypad*")))
261 (setq calc-main-buffer calcbuf
)
267 (if (setq win
(get-buffer-window "*Calculator*"))
269 (if (setq win
(get-buffer-window "*Calc Trail*"))
271 (switch-to-buffer (other-buffer))
272 (delete-window win
)))
273 (if (setq win
(get-buffer-window calc-keypad-buffer
))
275 (bury-buffer "*Calculator*")
276 (bury-buffer "*Calc Trail*")
277 (bury-buffer calc-keypad-buffer
)
279 (switch-to-buffer (other-buffer))
280 (delete-window win
)))
281 (setq calc-was-keypad-mode t
282 old-win
(get-largest-window))
283 (if (or (< (window-height old-win
) (+ height
6))
284 (< (window-width old-win
) (+ width
15))
286 (delete-other-windows old-win
))
287 (if (< (window-height old-win
) (+ height
4))
288 (error "Screen is not tall enough for this mode"))
291 (setq win
(split-window old-win
(- (window-height old-win
)
293 (set-window-buffer old-win
(calc-trail-buffer))
294 (set-window-buffer win calc-keypad-buffer
)
295 (set-window-start win
1)
296 (setq win
(split-window win
(+ width
7) t
))
297 (set-window-buffer win calcbuf
))
298 (if (or t
; left-side keypad not yet fully implemented
299 (< (with-current-buffer (window-buffer old-win
)
301 (/ (window-width) 2)))
302 (setq win
(split-window old-win
(- (window-width old-win
)
305 (setq old-win
(split-window old-win
(+ width
2) t
)))
306 (set-window-buffer win calc-keypad-buffer
)
307 (set-window-start win
1)
308 (split-window win
(- (window-height win
) height
1))
309 (set-window-buffer win calcbuf
))
310 (select-window old-win
)
311 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons")
312 (run-hooks 'calc-keypad-start-hook
)
313 (and calc-keypad-said-hello interactive
317 (setq calc-keypad-said-hello t
)))
318 (setq calc-keypad-input nil
)))
320 (defun calc-keypad-off ()
322 (if calc-standalone-flag
323 (save-buffers-kill-emacs nil
)
326 (defun calc-keypad-redraw ()
327 (set-buffer calc-keypad-buffer
)
328 (setq buffer-read-only t
)
329 (setq calc-keypad-full-layout
(append (symbol-value (nth calc-keypad-menu
332 (let ((buffer-read-only nil
)
333 (row calc-keypad-full-layout
)
338 (let ((col (car row
)))
340 (let* ((key (car col
))
343 (if (and (= y
3) (eq col
(car row
)))
344 (progn (setq col
(cdr col
)) 9)
346 (name (if (and calc-standalone-flag
347 (eq (nth 1 key
) 'calc-keypad-off
))
349 (if (> (length (car key
)) cwid
)
350 (substring (car key
) 0 cwid
)
353 (insert (make-string (/ (- cwid wid
) 2) 32)
355 (make-string (/ (- cwid wid -
1) 2) 32)
356 (if (equal name
"MENU")
357 (int-to-string (1+ calc-keypad-menu
))
359 (or (setq col
(cdr col
))
362 "-----+-----+-----+-----+-----"
364 "-----+---+-+--+--+-+---++----"
365 "----+----+----+----+----+----"))
366 (if (= y
7) "+\n" "|\n"))
369 (setq calc-keypad-prev-input t
)
370 (calc-keypad-show-input)
371 (goto-char (point-min)))
373 (defun calc-keypad-show-input ()
374 (or (equal calc-keypad-input calc-keypad-prev-input
)
375 (let ((buffer-read-only nil
))
377 (goto-char (point-min))
379 (delete-region (point-min) (point))
380 (if calc-keypad-input
381 (insert "Calc: " calc-keypad-input
"\n")
382 (insert "----+----+--Calc---+----+----"
383 (int-to-string (1+ calc-keypad-menu
))
385 (setq calc-keypad-prev-input calc-keypad-input
))
387 (defun calc-keypad-press ()
389 (unless (eq major-mode
'calc-keypad-mode
)
390 (error "Must be in *Calc Keypad* buffer for this command"))
391 (let* ((row (count-lines (point-min) (point-at-bol)))
393 (x (/ (current-column) (if (>= y
4) 6 5)))
395 (hyp (with-current-buffer calc-main-buffer
396 (setq radix calc-number-radix
397 frac calc-prefer-frac
398 inv calc-inverse-flag
)
399 calc-hyperbolic-flag
))
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."
536 ;; FIXME: Why not use "@e" instead to select the buffer?
538 (with-current-buffer calc-keypad-buffer
539 (goto-char (posn-point (event-start event
)))
540 (calc-keypad-press)))
542 (defun calc-keypad-right-click (_event)
543 "Handle a right-button mouse click in Calc Keypad window."
544 ;; FIXME: Why not use "@e" instead to select the buffer?
546 (with-current-buffer calc-keypad-buffer
549 (defun calc-keypad-middle-click (_event)
550 "Handle a middle-button mouse click in Calc Keypad window."
551 ;; FIXME: Why not use "@e" instead to select the buffer?
553 (with-current-buffer calc-keypad-buffer
554 (calc-keypad-menu-back)))
556 (defun calc-keypad-menu ()
558 (unless (eq major-mode
'calc-keypad-mode
)
559 (error "Must be in *Calc Keypad* buffer for this command"))
560 (while (progn (setq calc-keypad-menu
(%
(1+ calc-keypad-menu
)
561 (length calc-keypad-menus
)))
562 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
563 (calc-keypad-redraw))
565 (defun calc-keypad-menu-back ()
567 (or (eq major-mode
'calc-keypad-mode
)
568 (error "Must be in *Calc Keypad* buffer for this command"))
569 (while (progn (setq calc-keypad-menu
(%
(1- (+ calc-keypad-menu
570 (length calc-keypad-menus
)))
571 (length calc-keypad-menus
)))
572 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
573 (calc-keypad-redraw))
575 (defun calc-keypad-store ()
577 (setq calc-keypad-input
"STO"))
579 (defun calc-keypad-recall ()
581 (setq calc-keypad-input
"RCL"))
583 (defun calc-pack-interval (mode)
585 (if (or (< mode
0) (> mode
3))
586 (error "Open/close code should be in the range from 0 to 3"))
587 (calc-pack (- -
6 mode
)))
589 (defun calc-keypad-execute ()
591 (let* ((prompt "Calc keystrokes: ")
597 (setq keys
(read-key-sequence prompt
))
598 (setq cmd
(key-binding keys
))
599 (if (or (memq cmd
'(calc-inverse
604 (and prefix
(string-match "\\`\e?[-0-9]\\'" keys
)))
606 (setq last-command-event
(aref keys
(1- (length keys
))))
607 (command-execute cmd
)
609 prompt
(concat prompt
(key-description keys
) " ")))
610 nil
)))) ; skip mouse-up event
613 (command-execute cmd
)
614 (error "Not a Calc command: %s" (key-description keys
)))))
616 (provide 'calc-keypd
)
618 ;;; calc-keypd.el ends here