1 ;;; calc-keypd.el --- mouse-capable keypad input for Calc
3 ;; Copyright (C) 1990-1993, 2001-2012 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 (pad (- cwid
(/ wid
2))))
354 (insert (make-string (/ (- cwid wid
) 2) 32)
356 (make-string (/ (- cwid wid -
1) 2) 32)
357 (if (equal name
"MENU")
358 (int-to-string (1+ calc-keypad-menu
))
360 (or (setq col
(cdr col
))
363 "-----+-----+-----+-----+-----"
365 "-----+---+-+--+--+-+---++----"
366 "----+----+----+----+----+----"))
367 (if (= y
7) "+\n" "|\n"))
370 (setq calc-keypad-prev-input t
)
371 (calc-keypad-show-input)
372 (goto-char (point-min)))
374 (defun calc-keypad-show-input ()
375 (or (equal calc-keypad-input calc-keypad-prev-input
)
376 (let ((buffer-read-only nil
))
378 (goto-char (point-min))
380 (delete-region (point-min) (point))
381 (if calc-keypad-input
382 (insert "Calc: " calc-keypad-input
"\n")
383 (insert "----+----+--Calc---+----+----"
384 (int-to-string (1+ calc-keypad-menu
))
386 (setq calc-keypad-prev-input calc-keypad-input
))
388 (defun calc-keypad-press ()
390 (unless (eq major-mode
'calc-keypad-mode
)
391 (error "Must be in *Calc Keypad* buffer for this command"))
392 (let* ((row (count-lines (point-min) (point-at-bol)))
394 (x (/ (current-column) (if (>= y
4) 6 5)))
396 (hyp (with-current-buffer calc-main-buffer
397 (setq radix calc-number-radix
398 frac calc-prefer-frac
399 inv calc-inverse-flag
)
400 calc-hyperbolic-flag
))
402 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))
403 (input calc-keypad-input
)
405 (or (string-match "\\*[0-9]+\\.\\^" input
)
406 (and (<= radix
14) (string-match "e" input
)))
408 (key (nth x
(nth y calc-keypad-full-layout
)))
409 (cmd (or (nth (if inv
(if hyp
4 2) (if hyp
3 99)) key
)
412 (isstring (and (consp cmd
) (stringp (car cmd
))))
413 (calc-is-keypad-press t
))
414 (if invhyp
(calc-wrapper)) ; clear Inv and Hyp flags
416 (cond ((or (null cmd
)
419 ((and (> (minibuffer-depth) 0))
421 (push (aref (car cmd
) 0) unread-command-events
))
423 (push ?
\177 unread-command-events
))
424 ((eq cmd
'calc-enter
)
425 (push 13 unread-command-events
))
427 (push 7 unread-command-events
))
430 ((and input
(string-match "STO\\|RCL" input
))
431 (cond ((and isstring
(string-match "[0-9]" (car cmd
)))
432 (setq calc-keypad-input nil
)
433 (let ((var (intern (concat "var-q" (car cmd
)))))
434 (cond ((equal input
"STO+") (calc-store-plus var
))
435 ((equal input
"STO-") (calc-store-minus var
))
436 ((equal input
"STO*") (calc-store-times var
))
437 ((equal input
"STO/") (calc-store-div var
))
438 ((equal input
"STO^") (calc-store-power var
))
439 ((equal input
"STOn") (calc-store-neg 1 var
))
440 ((equal input
"STO&") (calc-store-inv 1 var
))
441 ((equal input
"STO") (calc-store-into var
))
442 (t (calc-recall var
)))))
443 ((memq cmd
'(calc-pop calc-undo
))
444 (setq calc-keypad-input nil
))
445 ((and (equal input
"STO")
446 (setq frac
(assq cmd
'( ( calc-plus .
"+" )
449 ( calc-divide .
"/" )
451 ( calc-change-sign .
"n")
452 ( calc-inv .
"&") ))))
453 (setq calc-keypad-input
(concat input
(cdr frac
))))
458 (if (or (and (equal cmd
".")
460 (string-match "[.:e^]" input
))
463 (or (and (<= radix
14) (string-match "e" input
))
464 (string-match "\\^\\|[-.:]\\'" input
)))
465 (and (not (equal cmd
"."))
466 (let ((case-fold-search nil
))
467 (string-match cmd
"0123456789ABCDEF"
469 "[e^]" (or input
""))
472 (setq calc-keypad-input
(concat
476 (format "%d#" radix
))
479 (or (and (equal cmd
"e") "1")
483 (if (and (equal cmd
".") frac
)
485 (if (and (equal cmd
"e")
490 (format "*%d.^" radix
)
492 ((and (eq cmd
'calc-change-sign
)
494 (let* ((epos (or iexpon
0))
495 (suffix (substring input epos
)))
496 (setq calc-keypad-input
(concat
497 (substring input
0 epos
)
498 (if (string-match "\\`-" suffix
)
500 (concat "-" suffix
))))))
501 ((and (eq cmd
'calc-pop
)
505 (setq calc-keypad-input
(substring input
0
510 ((and (eq cmd
'calc-undo
)
512 (setq calc-keypad-input nil
))
515 (let ((val (math-read-number input
)))
516 (setq calc-keypad-input nil
)
519 (calc-push-list (list (calc-record
520 (calc-normalize val
)))))
524 (if (eq cmd
'calc-enter
) (setq cmd nil
))))
525 (setq prefix-arg current-prefix-arg
)
527 (if (and (consp cmd
) (eq (car cmd
) 'progn
))
528 (while (setq cmd
(cdr cmd
))
529 (if (integerp (car cmd
))
530 (setq prefix-arg
(car cmd
))
531 (command-execute (car cmd
))))
532 (command-execute cmd
)))))
533 (set-buffer calc-keypad-buffer
)
534 (calc-keypad-show-input))))
536 (defun calc-keypad-left-click (event)
537 "Handle a left-button mouse click in Calc Keypad window."
539 (with-current-buffer calc-keypad-buffer
540 (goto-char (posn-point (event-start event
)))
541 (calc-keypad-press)))
543 (defun calc-keypad-right-click (event)
544 "Handle a right-button mouse click in Calc Keypad window."
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."
552 (with-current-buffer calc-keypad-buffer
553 (calc-keypad-menu-back)))
555 (defun calc-keypad-menu ()
557 (unless (eq major-mode
'calc-keypad-mode
)
558 (error "Must be in *Calc Keypad* buffer for this command"))
559 (while (progn (setq calc-keypad-menu
(%
(1+ calc-keypad-menu
)
560 (length calc-keypad-menus
)))
561 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
562 (calc-keypad-redraw))
564 (defun calc-keypad-menu-back ()
566 (or (eq major-mode
'calc-keypad-mode
)
567 (error "Must be in *Calc Keypad* buffer for this command"))
568 (while (progn (setq calc-keypad-menu
(%
(1- (+ calc-keypad-menu
569 (length calc-keypad-menus
)))
570 (length calc-keypad-menus
)))
571 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
572 (calc-keypad-redraw))
574 (defun calc-keypad-store ()
576 (setq calc-keypad-input
"STO"))
578 (defun calc-keypad-recall ()
580 (setq calc-keypad-input
"RCL"))
582 (defun calc-pack-interval (mode)
584 (if (or (< mode
0) (> mode
3))
585 (error "Open/close code should be in the range from 0 to 3"))
586 (calc-pack (- -
6 mode
)))
588 (defun calc-keypad-execute ()
590 (let* ((prompt "Calc keystrokes: ")
591 (flush 'x-flush-mouse-queue
)
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
)
608 (setq flush
'not-any-more
610 prompt
(concat prompt
(key-description keys
) " ")))
611 (eq cmd flush
))))) ; skip mouse-up event
614 (command-execute cmd
)
615 (error "Not a Calc command: %s" (key-description keys
)))))
617 (provide 'calc-keypd
)
619 ;;; calc-keypd.el ends here