calc-keypd.el 21 KB

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