EasyPG 1.07 Released
[packages] / xemacs-packages / calc / calc-keypd.el
1 ;;; calc-keypd.el --- mouse-capable keypad input for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 ;; This file is autoloaded from calc-ext.el.
27
28 (require 'calc-ext)
29 (require 'calc-macs)
30
31 (defvar calc-keypad-buffer nil)
32 (defvar calc-keypad-menu 0)
33 (defvar calc-keypad-full-layout nil)
34 (defvar calc-keypad-input nil)
35 (defvar calc-keypad-prev-input nil)
36 (defvar calc-keypad-said-hello nil)
37
38 ;;; |----+----+----+----+----+----|
39 ;;; |  ENTER  |+/- |EEX |UNDO| <- |
40 ;;; |-----+---+-+--+--+-+---++----|
41 ;;; | INV |  7  |  8  |  9  |  /  |
42 ;;; |-----+-----+-----+-----+-----|
43 ;;; | HYP |  4  |  5  |  6  |  *  |
44 ;;; |-----+-----+-----+-----+-----|
45 ;;; |EXEC |  1  |  2  |  3  |  -  |
46 ;;; |-----+-----+-----+-----+-----|
47 ;;; | OFF |  0  |  .  | PI  |  +  |
48 ;;; |-----+-----+-----+-----+-----|
49 (defvar calc-keypad-layout
50   '( ( ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
51        ( "ENTER" calc-enter calc-roll-down calc-roll-up calc-over )
52        ( "+/-"   calc-change-sign calc-inv (progn -4 calc-pack) )
53        ( "EEX"   ("e") (progn calc-num-prefix calc-pack-interval)
54                  (progn -5 calc-pack)  )
55        ( "UNDO"  calc-undo calc-redo calc-last-args )
56        ( "<-"    calc-pop (progn 0 calc-pop)
57                  (progn calc-num-prefix calc-pop) ) )
58      ( ( "INV"   calc-inverse )
59        ( "7"     ("7") calc-round )
60        ( "8"     ("8") (progn 2 calc-clean-num) )
61        ( "9"     ("9") calc-float )
62        ( "/"     calc-divide (progn calc-inverse calc-power) ) )
63      ( ( "HYP"   calc-hyperbolic )
64        ( "4"     ("4") calc-ln calc-log10 )
65        ( "5"     ("5") calc-exp calc-exp10 )
66        ( "6"     ("6") calc-abs )
67        ( "*"     calc-times calc-power ) )
68      ( ( "EXEC"  calc-keypad-execute )
69        ( "1"     ("1") calc-arcsin calc-sin )
70        ( "2"     ("2") calc-arccos calc-cos )
71        ( "3"     ("3") calc-arctan calc-tan )
72        ( "-"     calc-minus calc-conj ) )
73      ( ( "OFF"   calc-keypad-off )
74        ( "0"     ("0") calc-imaginary )
75        ( "."     (".") calc-precision )
76        ( "PI"    calc-pi )
77        ( "+"     calc-plus calc-sqrt ) ) ))
78
79 (defvar calc-keypad-menus '( calc-keypad-math-menu
80                              calc-keypad-funcs-menu
81                              calc-keypad-binary-menu
82                              calc-keypad-vector-menu
83                              calc-keypad-modes-menu
84                              calc-keypad-user-menu ) )
85
86 ;;; |----+----+----+----+----+----|
87 ;;; |FLR |CEIL|RND |TRNC|CLN2|FLT |
88 ;;; |----+----+----+----+----+----|
89 ;;; | LN |EXP |    |ABS |IDIV|MOD |
90 ;;; |----+----+----+----+----+----|
91 ;;; |SIN |COS |TAN |SQRT|y^x |1/x |
92
93 (defvar calc-keypad-math-menu
94   '( ( ( "FLR"   calc-floor )
95        ( "CEIL"  calc-ceiling )
96        ( "RND"   calc-round )
97        ( "TRNC"  calc-trunc )
98        ( "CLN2"  (progn 2 calc-clean-num) )
99        ( "FLT"   calc-float ) )
100      ( ( "LN"    calc-ln )
101        ( "EXP"   calc-exp )
102        ( ""      nil )
103        ( "ABS"   calc-abs )
104        ( "IDIV"  calc-idiv )
105        ( "MOD"   calc-mod ) )
106      ( ( "SIN"   calc-sin )
107        ( "COS"   calc-cos )
108        ( "TAN"   calc-tan )
109        ( "SQRT"  calc-sqrt )
110        ( "y^x"   calc-power )
111        ( "1/x"   calc-inv ) ) ))
112
113 ;;; |----+----+----+----+----+----|
114 ;;; |IGAM|BETA|IBET|ERF |BESJ|BESY|
115 ;;; |----+----+----+----+----+----|
116 ;;; |IMAG|CONJ| RE |ATN2|RAND|RAGN|
117 ;;; |----+----+----+----+----+----|
118 ;;; |GCD |FACT|DFCT|BNOM|PERM|NXTP|
119
120 (defvar calc-keypad-funcs-menu
121   '( ( ( "IGAM"  calc-inc-gamma )
122        ( "BETA"  calc-beta )
123        ( "IBET"  calc-inc-beta )
124        ( "ERF"   calc-erf )
125        ( "BESJ"  calc-bessel-J )
126        ( "BESY"  calc-bessel-Y ) )
127      ( ( "IMAG"  calc-imaginary )
128        ( "CONJ"  calc-conj )
129        ( "RE"    calc-re calc-im )
130        ( "ATN2"  calc-arctan2 )
131        ( "RAND"  calc-random )
132        ( "RAGN"  calc-random-again ) )
133      ( ( "GCD"   calc-gcd calc-lcm )
134        ( "FACT"  calc-factorial calc-gamma )
135        ( "DFCT"  calc-double-factorial )
136        ( "BNOM"  calc-choose )
137        ( "PERM"  calc-perm )
138        ( "NXTP"  calc-next-prime calc-prev-prime ) ) ))
139
140 ;;; |----+----+----+----+----+----|
141 ;;; |AND | OR |XOR |NOT |LSH |RSH |
142 ;;; |----+----+----+----+----+----|
143 ;;; |DEC |HEX |OCT |BIN |WSIZ|ARSH|
144 ;;; |----+----+----+----+----+----|
145 ;;; | A  | B  | C  | D  | E  | F  |
146
147 (defvar calc-keypad-binary-menu
148   '( ( ( "AND"   calc-and calc-diff )
149        ( "OR"    calc-or )
150        ( "XOR"   calc-xor )
151        ( "NOT"   calc-not calc-clip )
152        ( "LSH"   calc-lshift-binary calc-rotate-binary )
153        ( "RSH"   calc-rshift-binary ) )
154      ( ( "DEC"   calc-decimal-radix )
155        ( "HEX"   calc-hex-radix )
156        ( "OCT"   calc-octal-radix )
157        ( "BIN"   calc-binary-radix )
158        ( "WSIZ"  calc-word-size )
159        ( "ARSH"  calc-rshift-arith ) )
160      ( ( "A"     ("A") )
161        ( "B"     ("B") )
162        ( "C"     ("C") )
163        ( "D"     ("D") )
164        ( "E"     ("E") )
165        ( "F"     ("F") ) ) ))
166
167 ;;; |----+----+----+----+----+----|
168 ;;; |SUM |PROD|MAX |MAP*|MAP^|MAP$|
169 ;;; |----+----+----+----+----+----|
170 ;;; |INV |DET |TRN |IDNT|CRSS|"x" |
171 ;;; |----+----+----+----+----+----|
172 ;;; |PACK|UNPK|INDX|BLD |LEN |... |
173
174 (defvar calc-keypad-vector-menu
175   '( ( ( "SUM"   calc-vector-sum calc-vector-alt-sum calc-vector-mean )
176        ( "PROD"  calc-vector-product nil calc-vector-sdev )
177        ( "MAX"   calc-vector-max calc-vector-min calc-vector-median )
178        ( "MAP*"  (lambda () (interactive)
179                    (calc-map '(2 calcFunc-mul "*"))) )
180        ( "MAP^"  (lambda () (interactive)
181                    (calc-map '(2 calcFunc-pow "^"))) )
182        ( "MAP$"  calc-map-stack ) )
183      ( ( "MINV"  calc-inv )
184        ( "MDET"  calc-mdet )
185        ( "MTRN"  calc-transpose calc-conj-transpose )
186        ( "IDNT"  (progn calc-num-prefix calc-ident) )
187        ( "CRSS"  calc-cross )
188        ( "\"x\"" "\excalc-algebraic-entry\rx\r"
189                  "\excalc-algebraic-entry\ry\r"
190                  "\excalc-algebraic-entry\rz\r"
191                  "\excalc-algebraic-entry\rt\r") )
192      ( ( "PACK"  calc-pack )
193        ( "UNPK"  calc-unpack )
194        ( "INDX"  (progn calc-num-prefix calc-index) "\C-u\excalc-index\r" )
195        ( "BLD"   (progn calc-num-prefix calc-build-vector) )
196        ( "LEN"   calc-vlength )
197        ( "..."   calc-full-vectors ) ) ))
198
199 ;;; |----+----+----+----+----+----|
200 ;;; |FLT |FIX |SCI |ENG |GRP |    |
201 ;;; |----+----+----+----+----+----|
202 ;;; |RAD |DEG |FRAC|POLR|SYMB|PREC|
203 ;;; |----+----+----+----+----+----|
204 ;;; |SWAP|RLL3|RLL4|OVER|STO |RCL |
205
206 (defvar calc-keypad-modes-menu
207   '( ( ( "FLT"   calc-normal-notation
208                  (progn calc-num-prefix calc-normal-notation) )
209        ( "FIX"   (progn 2 calc-fix-notation)
210                  (progn calc-num-prefix calc-fix-notation) )
211        ( "SCI"   calc-sci-notation
212                  (progn calc-num-prefix calc-sci-notation) )
213        ( "ENG"   calc-eng-notation
214                  (progn calc-num-prefix calc-eng-notation) )
215        ( "GRP"   calc-group-digits "\C-u-3\excalc-group-digits\r" )
216        ( ""      nil ) )
217      ( ( "RAD"   calc-radians-mode )
218        ( "DEG"   calc-degrees-mode )
219        ( "FRAC"  calc-frac-mode )
220        ( "POLR"  calc-polar-mode )
221        ( "SYMB"  calc-symbolic-mode )
222        ( "PREC"  calc-precision ) )
223      ( ( "SWAP"  calc-roll-down )
224        ( "RLL3"  (progn 3 calc-roll-up) (progn 3 calc-roll-down) )
225        ( "RLL4"  (progn 4 calc-roll-up) (progn 4 calc-roll-down) )
226        ( "OVER"  calc-over )
227        ( "STO"   calc-keypad-store )
228        ( "RCL"   calc-keypad-recall ) ) ))
229
230 (define-derived-mode calc-keypad-mode fundamental-mode "Calculator"
231   "Major mode for Calc keypad input."
232   (define-key calc-keypad-mode-map " " 'calc-keypad-press)
233   (define-key calc-keypad-mode-map (kbd "RET") 'calc-keypad-press)
234   (define-key calc-keypad-mode-map (kbd "TAB") 'calc-keypad-menu)
235   (define-key calc-keypad-mode-map "q" 'calc-keypad-off)
236   (define-key calc-keypad-mode-map [down-mouse-1] 'ignore)
237   (define-key calc-keypad-mode-map [drag-mouse-1] 'ignore)
238   (define-key calc-keypad-mode-map [double-mouse-1] 'ignore)
239   (define-key calc-keypad-mode-map [triple-mouse-1] 'ignore)
240   (define-key calc-keypad-mode-map [down-mouse-2] 'ignore)
241   (define-key calc-keypad-mode-map [drag-mouse-2] 'ignore)
242   (define-key calc-keypad-mode-map [double-mouse-2] 'ignore)
243   (define-key calc-keypad-mode-map [triple-mouse-2] 'ignore)
244   (define-key calc-keypad-mode-map [down-mouse-3] 'ignore)
245   (define-key calc-keypad-mode-map [drag-mouse-3] 'ignore)
246   (define-key calc-keypad-mode-map [double-mouse-3] 'ignore)
247   (define-key calc-keypad-mode-map [triple-mouse-3] 'ignore)
248   (define-key calc-keypad-mode-map [mouse-3] 'calc-keypad-press)
249   (define-key calc-keypad-mode-map [mouse-2] 'calc-keypad-press)
250   (define-key calc-keypad-mode-map [mouse-1] 'calc-keypad-press)
251   (define-key calc-keypad-mode-map [mouse-3] 'calc-keypad-right-click)
252   (define-key calc-keypad-mode-map [mouse-2] 'calc-keypad-middle-click)
253   (define-key calc-keypad-mode-map [mouse-1] 'calc-keypad-left-click)
254   (put 'calc-keypad-mode 'mode-class 'special)
255   (make-local-variable 'calc-main-buffer))
256
257 (defun calc-do-keypad (&optional full-display interactive)
258   (calc-create-buffer)
259   (let ((calcbuf (current-buffer)))
260     (unless (bufferp calc-keypad-buffer)
261       (set-buffer (setq calc-keypad-buffer (get-buffer-create "*Calc Keypad*")))
262       (calc-keypad-mode)
263       (setq calc-main-buffer calcbuf)
264       (calc-keypad-redraw)
265       (calc-trail-buffer))
266     (let ((width 29)
267           (height 17)
268           win old-win)
269       (if (setq win (get-buffer-window "*Calculator*"))
270           (delete-window win))
271       (if (setq win (get-buffer-window "*Calc Trail*"))
272           (if (one-window-p)
273               (switch-to-buffer (other-buffer))
274             (delete-window win)))
275       (if (setq win (get-buffer-window calc-keypad-buffer))
276           (progn
277             (bury-buffer "*Calculator*")
278             (bury-buffer "*Calc Trail*")
279             (bury-buffer calc-keypad-buffer)
280             (if (one-window-p)
281                 (switch-to-buffer (other-buffer))
282               (delete-window win)))
283         (setq calc-was-keypad-mode t
284               old-win (get-largest-window))
285         (if (or (< (window-height old-win) (+ height 6))
286                 (< (window-width old-win) (+ width 15))
287                 full-display)
288             (delete-other-windows old-win))
289         (if (< (window-height old-win) (+ height 4))
290             (error "Screen is not tall enough for this mode"))
291         (if full-display
292             (progn
293               (setq win (split-window old-win (- (window-height old-win)
294                                                  height 1)))
295               (set-window-buffer old-win (calc-trail-buffer))
296               (set-window-buffer win calc-keypad-buffer)
297               (set-window-start win 1)
298               (setq win (split-window win (+ width 7) t))
299               (set-window-buffer win calcbuf))
300           (if (or t  ; left-side keypad not yet fully implemented
301                   (< (with-current-buffer (window-buffer old-win)
302                        (current-column))
303                      (/ (window-width) 2)))
304               (setq win (split-window old-win (- (window-width old-win)
305                                                  width 2)
306                                       t))
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
316              (progn
317                (sit-for 2)
318                (message "")))
319         (setq calc-keypad-said-hello t)))
320     (setq calc-keypad-input nil)))
321
322 (defun calc-keypad-off ()
323   (interactive)
324   (if calc-standalone-flag
325       (save-buffers-kill-emacs nil)
326     (calc-keypad)))
327
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
332                                                            calc-keypad-menus))
333                                         calc-keypad-layout))
334   (let ((buffer-read-only nil)
335         (row calc-keypad-full-layout)
336         (y 0))
337     (erase-buffer)
338     (insert "\n")
339     (while row
340       (let ((col (car row)))
341         (while col
342           (let* ((key (car col))
343                  (cwid (if (>= y 4)
344                            5
345                          (if (and (= y 3) (eq col (car row)))
346                              (progn (setq col (cdr col)) 9)
347                            4)))
348                  (name (if (and calc-standalone-flag
349                                 (eq (nth 1 key) 'calc-keypad-off))
350                            "EXIT"
351                          (if (> (length (car key)) cwid)
352                              (substring (car key) 0 cwid)
353                            (car key))))
354                  (wid (length name)))
355             (insert (make-string (/ (- cwid wid) 2) 32)
356                     name
357                     (make-string (/ (- cwid wid -1) 2) 32)
358                     (if (equal name "MENU")
359                         (int-to-string (1+ calc-keypad-menu))
360                       "|")))
361           (or (setq col (cdr col))
362               (insert "\n")))
363         (insert (if (>= y 4)
364                     "-----+-----+-----+-----+-----"
365                   (if (= y 3)
366                       "-----+---+-+--+--+-+---++----"
367                     "----+----+----+----+----+----"))
368                 (if (= y 7) "+\n" "|\n"))
369         (setq y (1+ y)
370               row (cdr row)))))
371   (setq calc-keypad-prev-input t)
372   (calc-keypad-show-input)
373   (goto-char (point-min)))
374
375 (defun calc-keypad-show-input ()
376   (or (equal calc-keypad-input calc-keypad-prev-input)
377       (let ((buffer-read-only nil))
378         (save-excursion
379           (goto-char (point-min))
380           (forward-line 1)
381           (delete-region (point-min) (point))
382           (if calc-keypad-input
383               (insert "Calc: " calc-keypad-input "\n")
384             (insert "----+----+--Calc---+----+----"
385                     (int-to-string (1+ calc-keypad-menu))
386                     "\n")))))
387   (setq calc-keypad-prev-input calc-keypad-input))
388
389 (defun calc-keypad-press ()
390   (interactive)
391   (unless (eq major-mode 'calc-keypad-mode)
392     (error "Must be in *Calc Keypad* buffer for this command"))
393   (let* ((row (count-lines (point-min) (point-at-bol)))
394          (y (/ row 2))
395          (x (/ (current-column) (if (>= y 4) 6 5)))
396          radix frac inv
397          (hyp (with-current-buffer calc-main-buffer
398                 (setq radix calc-number-radix
399                       frac calc-prefer-frac
400                       inv calc-inverse-flag)
401                 calc-hyperbolic-flag))
402          (invhyp t)
403          (input calc-keypad-input)
404          (iexpon (and input
405                       (or (string-match "\\*[0-9]+\\.\\^" input)
406                           (and (<= radix 14) (string-match "e" input)))
407                       (match-end 0)))
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)
410                   (setq invhyp nil)
411                   (nth 1 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
415     (unwind-protect
416         (cond ((or (null cmd)
417                    (= (% row 2) 0))
418                (beep))
419               ((and (> (minibuffer-depth) 0))
420                (cond (isstring
421                       (push (aref (car cmd) 0) unread-command-events))
422                      ((eq cmd 'calc-pop)
423                       (push ?\177 unread-command-events))
424                      ((eq cmd 'calc-enter)
425                       (push 13 unread-command-events))
426                      ((eq cmd 'calc-undo)
427                       (push 7 unread-command-events))
428                      (t
429                       (beep))))
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 . "+" )
447                                                    ( calc-minus . "-" )
448                                                    ( calc-times . "*" )
449                                                    ( calc-divide . "/" )
450                                                    ( calc-power . "^")
451                                                    ( calc-change-sign . "n")
452                                                    ( calc-inv . "&") ))))
453                       (setq calc-keypad-input (concat input (cdr frac))))
454                      (t
455                       (beep))))
456               (isstring
457                (setq cmd (car cmd))
458                (if (or (and (equal cmd ".")
459                             input
460                             (string-match "[.:e^]" input))
461                        (and (equal cmd "e")
462                             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"
468                                             (if (string-match
469                                                  "[e^]" (or input ""))
470                                                 10 radix)))))
471                    (beep)
472                  (setq calc-keypad-input (concat
473                                           (and (/= radix 10)
474                                                (or (not input)
475                                                    (equal input "-"))
476                                                (format "%d#" radix))
477                                           (and (or (not input)
478                                                    (equal input "-"))
479                                                (or (and (equal cmd "e") "1")
480                                                    (and (equal cmd ".")
481                                                         (if frac "1" "0"))))
482                                           input
483                                           (if (and (equal cmd ".") frac)
484                                               ":"
485                                             (if (and (equal cmd "e")
486                                                      (or (not input)
487                                                          (string-match
488                                                           "#" input))
489                                                      (> radix 14))
490                                                 (format "*%d.^" radix)
491                                               cmd))))))
492               ((and (eq cmd 'calc-change-sign)
493                     input)
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)
499                                               (substring suffix 1)
500                                             (concat "-" suffix))))))
501               ((and (eq cmd 'calc-pop)
502                     input)
503                (if (equal input "")
504                    (beep)
505                  (setq calc-keypad-input (substring input 0
506                                                     (or (string-match
507                                                          "\\*[0-9]+\\.\\^\\'"
508                                                          input)
509                                                         -1)))))
510               ((and (eq cmd 'calc-undo)
511                     input)
512                (setq calc-keypad-input nil))
513               (t
514                (if input
515                    (let ((val (math-read-number input)))
516                      (setq calc-keypad-input nil)
517                      (if val
518                          (calc-wrapper
519                           (calc-push-list (list (calc-record
520                                                  (calc-normalize val)))))
521                        (or (equal input "")
522                            (beep))
523                        (setq cmd nil))
524                      (if (eq cmd 'calc-enter) (setq cmd nil))))
525                (setq prefix-arg current-prefix-arg)
526                (if cmd
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))))
535
536 (defun calc-keypad-left-click (event)
537   "Handle a left-button mouse click in Calc Keypad window."
538   ;; FIXME: Why not use "@e" instead to select the buffer?
539   (interactive "e")
540   (with-current-buffer calc-keypad-buffer
541     (goto-char (event-point event))
542     (calc-keypad-press)))
543
544 (defun calc-keypad-right-click (_event)
545   "Handle a right-button mouse click in Calc Keypad window."
546   ;; FIXME: Why not use "@e" instead to select the buffer?
547   (interactive "e")
548   (with-current-buffer calc-keypad-buffer
549     (calc-keypad-menu)))
550
551 (defun calc-keypad-middle-click (_event)
552   "Handle a middle-button mouse click in Calc Keypad window."
553   ;; FIXME: Why not use "@e" instead to select the buffer?
554   (interactive "e")
555   (with-current-buffer calc-keypad-buffer
556     (calc-keypad-menu-back)))
557
558 (defun calc-keypad-menu ()
559   (interactive)
560   (unless (eq major-mode 'calc-keypad-mode)
561     (error "Must be in *Calc Keypad* buffer for this command"))
562   (while (progn (setq calc-keypad-menu (% (1+ calc-keypad-menu)
563                                           (length calc-keypad-menus)))
564                 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
565   (calc-keypad-redraw))
566
567 (defun calc-keypad-menu-back ()
568   (interactive)
569   (or (eq major-mode 'calc-keypad-mode)
570       (error "Must be in *Calc Keypad* buffer for this command"))
571   (while (progn (setq calc-keypad-menu (% (1- (+ calc-keypad-menu
572                                                  (length calc-keypad-menus)))
573                                           (length calc-keypad-menus)))
574                 (not (symbol-value (nth calc-keypad-menu calc-keypad-menus)))))
575   (calc-keypad-redraw))
576
577 (defun calc-keypad-store ()
578   (interactive)
579   (setq calc-keypad-input "STO"))
580
581 (defun calc-keypad-recall ()
582   (interactive)
583   (setq calc-keypad-input "RCL"))
584
585 (defun calc-pack-interval (mode)
586   (interactive "p")
587   (if (or (< mode 0) (> mode 3))
588       (error "Open/close code should be in the range from 0 to 3"))
589   (calc-pack (- -6 mode)))
590
591 (defun calc-keypad-execute ()
592   (interactive)
593   (let* ((prompt "Calc keystrokes: ")
594          (prefix nil)
595          keys cmd)
596     (save-excursion
597       (calc-select-buffer)
598       (while (progn
599                (setq keys (read-key-sequence prompt))
600                (setq cmd (key-binding keys))
601                (if (or (memq cmd '(calc-inverse
602                                    calc-hyperbolic
603                                    universal-argument
604                                    digit-argument
605                                    negative-argument))
606                        (and prefix (string-match "\\`\e?[-0-9]\\'" keys)))
607                    (progn
608                      (setq last-command-char (aref keys (1- (length keys))))
609                      (command-execute cmd)
610                      (setq prefix t
611                            prompt (concat prompt (key-description keys) " ")))
612                  nil))))  ; skip mouse-up event
613     (message "")
614     (if (commandp cmd)
615         (command-execute cmd)
616       (error "Not a Calc command: %s" (key-description keys)))))
617
618 (provide 'calc-keypd)
619
620 ;;; calc-keypd.el ends here