calc-keypd.el 21 KB

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