1 ;;; calc-keypd.el --- mouse-capable keypad input for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
33 (defvar calc-keypad-buffer nil
)
34 (defvar calc-keypad-menu
0)
35 (defvar calc-keypad-full-layout nil
)
36 (defvar calc-keypad-input nil
)
37 (defvar calc-keypad-prev-input nil
)
38 (defvar calc-keypad-said-hello nil
)
40 ;;; |----+----+----+----+----+----|
41 ;;; | ENTER |+/- |EEX |UNDO| <- |
42 ;;; |-----+---+-+--+--+-+---++----|
43 ;;; | INV | 7 | 8 | 9 | / |
44 ;;; |-----+-----+-----+-----+-----|
45 ;;; | HYP | 4 | 5 | 6 | * |
46 ;;; |-----+-----+-----+-----+-----|
47 ;;; |EXEC | 1 | 2 | 3 | - |
48 ;;; |-----+-----+-----+-----+-----|
49 ;;; | OFF | 0 | . | PI | + |
50 ;;; |-----+-----+-----+-----+-----|
51 (defvar calc-keypad-layout
52 '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over
)
53 ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over
)
54 ( "+/-" calc-change-sign calc-inv
(progn -
4 calc-pack
) )
55 ( "EEX" ("e") (progn calc-num-prefix calc-pack-interval
)
56 (progn -
5 calc-pack
) )
57 ( "UNDO" calc-undo calc-redo calc-last-args
)
58 ( "<-" calc-pop
(progn 0 calc-pop
)
59 (progn calc-num-prefix calc-pop
) ) )
60 ( ( "INV" calc-inverse
)
61 ( "7" ("7") calc-round
)
62 ( "8" ("8") (progn 2 calc-clean-num
) )
63 ( "9" ("9") calc-float
)
64 ( "/" calc-divide
(progn calc-inverse calc-power
) ) )
65 ( ( "HYP" calc-hyperbolic
)
66 ( "4" ("4") calc-ln calc-log10
)
67 ( "5" ("5") calc-exp calc-exp10
)
68 ( "6" ("6") calc-abs
)
69 ( "*" calc-times calc-power
) )
70 ( ( "EXEC" calc-keypad-execute
)
71 ( "1" ("1") calc-arcsin calc-sin
)
72 ( "2" ("2") calc-arccos calc-cos
)
73 ( "3" ("3") calc-arctan calc-tan
)
74 ( "-" calc-minus calc-conj
) )
75 ( ( "OFF" calc-keypad-off
)
76 ( "0" ("0") calc-imaginary
)
77 ( "." (".") calc-precision
)
79 ( "+" calc-plus calc-sqrt
) ) ))
81 (defvar calc-keypad-menus
'( calc-keypad-math-menu
82 calc-keypad-funcs-menu
83 calc-keypad-binary-menu
84 calc-keypad-vector-menu
85 calc-keypad-modes-menu
86 calc-keypad-user-menu
) )
88 ;;; |----+----+----+----+----+----|
89 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
90 ;;; |----+----+----+----+----+----|
91 ;;; | LN |EXP | |ABS |IDIV|MOD |
92 ;;; |----+----+----+----+----+----|
93 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
95 (defvar calc-keypad-math-menu
96 '( ( ( "FLR" calc-floor
)
97 ( "CEIL" calc-ceiling
)
100 ( "CLN2" (progn 2 calc-clean-num
) )
101 ( "FLT" calc-float
) )
113 ( "1/x" calc-inv
) ) ))
115 ;;; |----+----+----+----+----+----|
116 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
117 ;;; |----+----+----+----+----+----|
118 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
119 ;;; |----+----+----+----+----+----|
120 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
122 (defvar calc-keypad-funcs-menu
123 '( ( ( "IGAM" calc-inc-gamma
)
125 ( "IBET" calc-inc-beta
)
127 ( "BESJ" calc-bessel-J
)
128 ( "BESY" calc-bessel-Y
) )
129 ( ( "IMAG" calc-imaginary
)
131 ( "RE" calc-re calc-im
)
132 ( "ATN2" calc-arctan2
)
133 ( "RAND" calc-random
)
134 ( "RAGN" calc-random-again
) )
135 ( ( "GCD" calc-gcd calc-lcm
)
136 ( "FACT" calc-factorial calc-gamma
)
137 ( "DFCT" calc-double-factorial
)
138 ( "BNOM" calc-choose
)
140 ( "NXTP" calc-next-prime calc-prev-prime
) ) ))
142 ;;; |----+----+----+----+----+----|
143 ;;; |AND | OR |XOR |NOT |LSH |RSH |
144 ;;; |----+----+----+----+----+----|
145 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
146 ;;; |----+----+----+----+----+----|
147 ;;; | A | B | C | D | E | F |
149 (defvar calc-keypad-binary-menu
150 '( ( ( "AND" calc-and calc-diff
)
153 ( "NOT" calc-not calc-clip
)
154 ( "LSH" calc-lshift-binary calc-rotate-binary
)
155 ( "RSH" calc-rshift-binary
) )
156 ( ( "DEC" calc-decimal-radix
)
157 ( "HEX" calc-hex-radix
)
158 ( "OCT" calc-octal-radix
)
159 ( "BIN" calc-binary-radix
)
160 ( "WSIZ" calc-word-size
)
161 ( "ARSH" calc-rshift-arith
) )
169 ;;; |----+----+----+----+----+----|
170 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
171 ;;; |----+----+----+----+----+----|
172 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
173 ;;; |----+----+----+----+----+----|
174 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
176 (defvar calc-keypad-vector-menu
177 '( ( ( "SUM" calc-vector-sum calc-vector-alt-sum calc-vector-mean
)
178 ( "PROD" calc-vector-product nil calc-vector-sdev
)
179 ( "MAX" calc-vector-max calc-vector-min calc-vector-median
)
180 ( "MAP*" (lambda () (interactive)
181 (calc-map '(2 calcFunc-mul
"*"))) )
182 ( "MAP^" (lambda () (interactive)
183 (calc-map '(2 calcFunc-pow
"^"))) )
184 ( "MAP$" calc-map-stack
) )
185 ( ( "MINV" calc-inv
)
187 ( "MTRN" calc-transpose calc-conj-transpose
)
188 ( "IDNT" (progn calc-num-prefix calc-ident
) )
189 ( "CRSS" calc-cross
)
190 ( "\"x\"" "\excalc-algebraic-entry\rx\r"
191 "\excalc-algebraic-entry\ry\r"
192 "\excalc-algebraic-entry\rz\r"
193 "\excalc-algebraic-entry\rt\r") )
194 ( ( "PACK" calc-pack
)
195 ( "UNPK" calc-unpack
)
196 ( "INDX" (progn calc-num-prefix calc-index
) "\C-u\excalc-index\r" )
197 ( "BLD" (progn calc-num-prefix calc-build-vector
) )
198 ( "LEN" calc-vlength
)
199 ( "..." calc-full-vectors
) ) ))
201 ;;; |----+----+----+----+----+----|
202 ;;; |FLT |FIX |SCI |ENG |GRP | |
203 ;;; |----+----+----+----+----+----|
204 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
205 ;;; |----+----+----+----+----+----|
206 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
208 (defvar calc-keypad-modes-menu
209 '( ( ( "FLT" calc-normal-notation
210 (progn calc-num-prefix calc-normal-notation
) )
211 ( "FIX" (progn 2 calc-fix-notation
)
212 (progn calc-num-prefix calc-fix-notation
) )
213 ( "SCI" calc-sci-notation
214 (progn calc-num-prefix calc-sci-notation
) )
215 ( "ENG" calc-eng-notation
216 (progn calc-num-prefix calc-eng-notation
) )
217 ( "GRP" calc-group-digits
"\C-u-3\excalc-group-digits\r" )
219 ( ( "RAD" calc-radians-mode
)
220 ( "DEG" calc-degrees-mode
)
221 ( "FRAC" calc-frac-mode
)
222 ( "POLR" calc-polar-mode
)
223 ( "SYMB" calc-symbolic-mode
)
224 ( "PREC" calc-precision
) )
225 ( ( "SWAP" calc-roll-down
)
226 ( "RLL3" (progn 3 calc-roll-up
) (progn 3 calc-roll-down
) )
227 ( "RLL4" (progn 4 calc-roll-up
) (progn 4 calc-roll-down
) )
229 ( "STO" calc-keypad-store
)
230 ( "RCL" calc-keypad-recall
) ) ))
232 (define-derived-mode calc-keypad-mode fundamental-mode
"Calculator"
233 "Major mode for Calc keypad input."
234 (define-key calc-keypad-mode-map
" " 'calc-keypad-press
)
235 (define-key calc-keypad-mode-map
(kbd "RET") 'calc-keypad-press
)
236 (define-key calc-keypad-mode-map
(kbd "TAB") 'calc-keypad-menu
)
237 (define-key calc-keypad-mode-map
"q" 'calc-keypad-off
)
238 (define-key calc-keypad-mode-map
[down-mouse-1
] 'ignore
)
239 (define-key calc-keypad-mode-map
[drag-mouse-1
] 'ignore
)
240 (define-key calc-keypad-mode-map
[double-mouse-1
] 'ignore
)
241 (define-key calc-keypad-mode-map
[triple-mouse-1
] 'ignore
)
242 (define-key calc-keypad-mode-map
[down-mouse-2
] 'ignore
)
243 (define-key calc-keypad-mode-map
[drag-mouse-2
] 'ignore
)
244 (define-key calc-keypad-mode-map
[double-mouse-2
] 'ignore
)
245 (define-key calc-keypad-mode-map
[triple-mouse-2
] 'ignore
)
246 (define-key calc-keypad-mode-map
[down-mouse-3
] 'ignore
)
247 (define-key calc-keypad-mode-map
[drag-mouse-3
] 'ignore
)
248 (define-key calc-keypad-mode-map
[double-mouse-3
] 'ignore
)
249 (define-key calc-keypad-mode-map
[triple-mouse-3
] 'ignore
)
250 (define-key calc-keypad-mode-map
[mouse-3
] 'calc-keypad-right-click
)
251 (define-key calc-keypad-mode-map
[mouse-2
] 'calc-keypad-middle-click
)
252 (define-key calc-keypad-mode-map
[mouse-1
] 'calc-keypad-left-click
)
253 (put 'calc-keypad-mode
'mode-class
'special
)
254 (make-local-variable 'calc-main-buffer
))
256 (defun calc-do-keypad (&optional full-display interactive
)
258 (let ((calcbuf (current-buffer)))
259 (unless (bufferp calc-keypad-buffer
)
260 (set-buffer (setq calc-keypad-buffer
(get-buffer-create "*Calc Keypad*")))
262 (setq calc-main-buffer calcbuf
)
268 (if (setq win
(get-buffer-window "*Calculator*"))
270 (if (setq win
(get-buffer-window "*Calc Trail*"))
272 (switch-to-buffer (other-buffer))
273 (delete-window win
)))
274 (if (setq win
(get-buffer-window calc-keypad-buffer
))
276 (bury-buffer "*Calculator*")
277 (bury-buffer "*Calc Trail*")
278 (bury-buffer calc-keypad-buffer
)
280 (switch-to-buffer (other-buffer))
281 (delete-window win
)))
282 (setq calc-was-keypad-mode t
283 old-win
(get-largest-window))
284 (if (or (< (window-height old-win
) (+ height
6))
285 (< (window-width old-win
) (+ width
15))
287 (delete-other-windows old-win
))
288 (if (< (window-height old-win
) (+ height
4))
289 (error "Screen is not tall enough for this mode"))
292 (setq win
(split-window old-win
(- (window-height old-win
)
294 (set-window-buffer old-win
(calc-trail-buffer))
295 (set-window-buffer win calc-keypad-buffer
)
296 (set-window-start win
1)
297 (setq win
(split-window win
(+ width
7) t
))
298 (set-window-buffer win calcbuf
))
299 (if (or t
; left-side keypad not yet fully implemented
301 (set-buffer (window-buffer old-win
))
303 (/ (window-width) 2)))
304 (setq win
(split-window old-win
(- (window-width old-win
)
307 (setq old-win
(split-window old-win
(+ width
2) t
)))
308 (set-window-buffer win calc-keypad-buffer
)
309 (set-window-start win
1)
310 (split-window win
(- (window-height win
) height
1))
311 (set-window-buffer win calcbuf
))
312 (select-window old-win
)
313 (message "Welcome to GNU Emacs Calc! Use the left and right mouse buttons")
314 (run-hooks 'calc-keypad-start-hook
)
315 (and calc-keypad-said-hello interactive
319 (setq calc-keypad-said-hello t
)))
320 (setq calc-keypad-input nil
)))
322 (defun calc-keypad-off ()
324 (if calc-standalone-flag
325 (save-buffers-kill-emacs nil
)
328 (defun calc-keypad-redraw ()
329 (set-buffer calc-keypad-buffer
)
330 (setq buffer-read-only t
)
331 (setq calc-keypad-full-layout
(append (symbol-value (nth calc-keypad-menu
334 (let ((buffer-read-only nil
)
335 (row calc-keypad-full-layout
)
340 (let ((col (car row
)))
342 (let* ((key (car col
))
345 (if (and (= y
3) (eq col
(car row
)))
346 (progn (setq col
(cdr col
)) 9)
348 (name (if (and calc-standalone-flag
349 (eq (nth 1 key
) 'calc-keypad-off
))
351 (if (> (length (car key
)) cwid
)
352 (substring (car key
) 0 cwid
)
355 (pad (- cwid
(/ wid
2))))
356 (insert (make-string (/ (- cwid wid
) 2) 32)
358 (make-string (/ (- cwid wid -
1) 2) 32)
359 (if (equal name
"MENU")
360 (int-to-string (1+ calc-keypad-menu
))
362 (or (setq col
(cdr col
))
365 "-----+-----+-----+-----+-----"
367 "-----+---+-+--+--+-+---++----"
368 "----+----+----+----+----+----"))
369 (if (= y
7) "+\n" "|\n"))
372 (setq calc-keypad-prev-input t
)
373 (calc-keypad-show-input)
374 (goto-char (point-min)))
376 (defun calc-keypad-show-input ()
377 (or (equal calc-keypad-input calc-keypad-prev-input
)
378 (let ((buffer-read-only nil
))
380 (goto-char (point-min))
382 (delete-region (point-min) (point))
383 (if calc-keypad-input
384 (insert "Calc: " calc-keypad-input
"\n")
385 (insert "----+-----Calc " calc-version
" -----+----"
386 (int-to-string (1+ calc-keypad-menu
))
388 (setq calc-keypad-prev-input calc-keypad-input
))
390 (defun calc-keypad-press ()
392 (unless (eq major-mode
'calc-keypad-mode
)
393 (error "Must be in *Calc Keypad* buffer for this command"))
394 (let* ((row (save-excursion
396 (count-lines (point-min) (point))))
398 (x (/ (current-column) (if (>= y
4) 6 5)))
400 (hyp (with-current-buffer calc-main-buffer
401 (setq radix calc-number-radix
402 frac calc-prefer-frac
403 inv calc-inverse-flag
)
404 calc-hyperbolic-flag
))
406 (menu (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))
407 (input calc-keypad-input
)
409 (or (string-match "\\*[0-9]+\\.\\^" input
)
410 (and (<= radix
14) (string-match "e" input
)))
412 (key (nth x
(nth y calc-keypad-full-layout
)))
413 (cmd (or (nth (if inv
(if hyp
4 2) (if hyp
3 99)) key
)
416 (isstring (and (consp cmd
) (stringp (car cmd
))))
417 (calc-is-keypad-press t
))
418 (if invhyp
(calc-wrapper)) ; clear Inv and Hyp flags
420 (cond ((or (null cmd
)
423 ((and (> (minibuffer-depth) 0))
425 (push (aref (car cmd
) 0) unread-command-events
))
427 (push ?
\177 unread-command-events
))
428 ((eq cmd
'calc-enter
)
429 (push 13 unread-command-events
))
431 (push 7 unread-command-events
))
434 ((and input
(string-match "STO\\|RCL" input
))
435 (cond ((and isstring
(string-match "[0-9]" (car cmd
)))
436 (setq calc-keypad-input nil
)
437 (let ((var (intern (concat "var-q" (car cmd
)))))
438 (cond ((equal input
"STO+") (calc-store-plus var
))
439 ((equal input
"STO-") (calc-store-minus var
))
440 ((equal input
"STO*") (calc-store-times var
))
441 ((equal input
"STO/") (calc-store-div var
))
442 ((equal input
"STO^") (calc-store-power var
))
443 ((equal input
"STOn") (calc-store-neg 1 var
))
444 ((equal input
"STO&") (calc-store-inv 1 var
))
445 ((equal input
"STO") (calc-store-into var
))
446 (t (calc-recall var
)))))
447 ((memq cmd
'(calc-pop calc-undo
))
448 (setq calc-keypad-input nil
))
449 ((and (equal input
"STO")
450 (setq frac
(assq cmd
'( ( calc-plus .
"+" )
453 ( calc-divide .
"/" )
455 ( calc-change-sign .
"n")
456 ( calc-inv .
"&") ))))
457 (setq calc-keypad-input
(concat input
(cdr frac
))))
462 (if (or (and (equal cmd
".")
464 (string-match "[.:e^]" input
))
467 (or (and (<= radix
14) (string-match "e" input
))
468 (string-match "\\^\\|[-.:]\\'" input
)))
469 (and (not (equal cmd
"."))
470 (let ((case-fold-search nil
))
471 (string-match cmd
"0123456789ABCDEF"
473 "[e^]" (or input
""))
476 (setq calc-keypad-input
(concat
480 (format "%d#" radix
))
483 (or (and (equal cmd
"e") "1")
487 (if (and (equal cmd
".") frac
)
489 (if (and (equal cmd
"e")
494 (format "*%d.^" radix
)
496 ((and (eq cmd
'calc-change-sign
)
498 (let* ((epos (or iexpon
0))
499 (suffix (substring input epos
)))
500 (setq calc-keypad-input
(concat
501 (substring input
0 epos
)
502 (if (string-match "\\`-" suffix
)
504 (concat "-" suffix
))))))
505 ((and (eq cmd
'calc-pop
)
509 (setq calc-keypad-input
(substring input
0
514 ((and (eq cmd
'calc-undo
)
516 (setq calc-keypad-input nil
))
519 (let ((val (math-read-number input
)))
520 (setq calc-keypad-input nil
)
523 (calc-push-list (list (calc-record
524 (calc-normalize val
)))))
528 (if (eq cmd
'calc-enter
) (setq cmd nil
))))
529 (setq prefix-arg current-prefix-arg
)
531 (if (and (consp cmd
) (eq (car cmd
) 'progn
))
532 (while (setq cmd
(cdr cmd
))
533 (if (integerp (car cmd
))
534 (setq prefix-arg
(car cmd
))
535 (command-execute (car cmd
))))
536 (command-execute cmd
)))))
537 (set-buffer calc-keypad-buffer
)
538 (calc-keypad-show-input))))
540 (defun calc-keypad-left-click (event)
541 "Handle a left-button mouse click in Calc Keypad window."
543 (with-current-buffer calc-keypad-buffer
544 (goto-char (posn-point (event-start event
)))
545 (calc-keypad-press)))
547 (defun calc-keypad-right-click (event)
548 "Handle a right-button mouse click in Calc Keypad window."
551 (set-buffer calc-keypad-buffer
)
554 (defun calc-keypad-middle-click (event)
555 "Handle a middle-button mouse click in Calc Keypad window."
557 (with-current-buffer calc-keypad-buffer
558 (calc-keypad-menu-back)))
560 (defun calc-keypad-menu ()
562 (unless (eq major-mode
'calc-keypad-mode
)
563 (error "Must be in *Calc Keypad* buffer for this command"))
564 (while (progn (setq calc-keypad-menu
(%
(1+ calc-keypad-menu
)
565 (length calc-keypad-menus
)))
566 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
567 (calc-keypad-redraw))
569 (defun calc-keypad-menu-back ()
571 (or (eq major-mode
'calc-keypad-mode
)
572 (error "Must be in *Calc Keypad* buffer for this command"))
573 (while (progn (setq calc-keypad-menu
(%
(1- (+ calc-keypad-menu
574 (length calc-keypad-menus
)))
575 (length calc-keypad-menus
)))
576 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus
)))))
577 (calc-keypad-redraw))
579 (defun calc-keypad-store ()
581 (setq calc-keypad-input
"STO"))
583 (defun calc-keypad-recall ()
585 (setq calc-keypad-input
"RCL"))
587 (defun calc-pack-interval (mode)
589 (if (or (< mode
0) (> mode
3))
590 (error "Open/close code should be in the range from 0 to 3"))
591 (calc-pack (- -
6 mode
)))
593 (defun calc-keypad-execute ()
595 (let* ((prompt "Calc keystrokes: ")
596 (flush 'x-flush-mouse-queue
)
602 (setq keys
(read-key-sequence prompt
))
603 (setq cmd
(key-binding keys
))
604 (if (or (memq cmd
'(calc-inverse
609 (and prefix
(string-match "\\`\e?[-0-9]\\'" keys
)))
611 (setq last-command-char
(aref keys
(1- (length keys
))))
612 (command-execute cmd
)
613 (setq flush
'not-any-more
615 prompt
(concat prompt
(key-description keys
) " ")))
616 (eq cmd flush
))))) ; skip mouse-up event
619 (command-execute cmd
)
620 (error "Not a Calc command: %s" (key-description keys
)))))
622 (provide 'calc-keypd
)
624 ;; arch-tag: 4ba0d360-2bb6-40b8-adfa-eb373765b3f9
625 ;;; calc-keypd.el ends here