comparison lisp/calc/calc.el @ 41047:73f364fd8aaa

Style cleanup; don't put closing parens on their own line, add "foo.el ends here" to each file, and update copyright date.
author Colin Walters <walters@gnu.org>
date Wed, 14 Nov 2001 09:09:09 +0000
parents d5ccdce87268
children 711f18abaf57
comparison
equal deleted inserted replaced
41046:14b73d89514a 41047:73f364fd8aaa
920 calc-pop-above calc-power calc-roll-down calc-roll-up 920 calc-pop-above calc-power calc-roll-down calc-roll-up
921 calc-shift-Y-prefix-help calc-tutorial calcDigit-letter 921 calc-shift-Y-prefix-help calc-tutorial calcDigit-letter
922 report-calc-bug) 922 report-calc-bug)
923 923
924 )) 924 ))
925
926 ) 925 )
927 926
928 (calc-init-base) 927 (calc-init-base)
929 928
930 929
945 (symbolp 944 (symbolp
946 (lookup-key calc-dispatch-map (substring keys 0 1))) 945 (lookup-key calc-dispatch-map (substring keys 0 1)))
947 (define-key calc-dispatch-map (substring keys 0 1) nil)) 946 (define-key calc-dispatch-map (substring keys 0 1) nil))
948 (define-key calc-dispatch-map keys 'calc-same-interface)))) 947 (define-key calc-dispatch-map keys 'calc-same-interface))))
949 (error nil)) 948 (error nil))
950 (calc-do-dispatch arg) 949 (calc-do-dispatch arg))
951 )
952 950
953 (defun calc-do-dispatch (arg) 951 (defun calc-do-dispatch (arg)
954 (let ((key (calc-read-key-sequence 952 (let ((key (calc-read-key-sequence
955 (if calc-dispatch-help 953 (if calc-dispatch-help
956 "Calc options: Calc, Keypad, Quick, Embed; eXit; Info, Tutorial; Grab; ?=more" 954 "Calc options: Calc, Keypad, Quick, Embed; eXit; Info, Tutorial; Grab; ?=more"
961 (message "") 959 (message "")
962 (if key 960 (if key
963 (progn 961 (progn
964 (or (commandp key) (calc-extensions)) 962 (or (commandp key) (calc-extensions))
965 (call-interactively key)) 963 (call-interactively key))
966 (beep))) 964 (beep))))
967 )
968 (setq calc-dispatch-help nil) 965 (setq calc-dispatch-help nil)
969 966
970 (defun calc-read-key-sequence (prompt map) 967 (defun calc-read-key-sequence (prompt map)
971 (let ((prompt2 (format "%s " (key-description (this-command-keys)))) 968 (let ((prompt2 (format "%s " (key-description (this-command-keys))))
972 (glob (current-global-map)) 969 (glob (current-global-map))
982 (if (commandp (key-binding (if calc-emacs-type-19 979 (if (commandp (key-binding (if calc-emacs-type-19
983 (vector (cdr key)) 980 (vector (cdr key))
984 (char-to-string (cdr key))))) 981 (char-to-string (cdr key)))))
985 "" prompt2))) 982 "" prompt2)))
986 (use-global-map glob) 983 (use-global-map glob)
987 (use-local-map loc)))) 984 (use-local-map loc)))))
988 )
989 985
990 986
991 987
992 (defun calc-mode () 988 (defun calc-mode ()
993 "Calculator major mode. 989 "Calculator major mode.
1063 (message "Evaluating calc-defs...") 1059 (message "Evaluating calc-defs...")
1064 (calc-need-macros) 1060 (calc-need-macros)
1065 (eval (cons 'progn calc-defs)) 1061 (eval (cons 'progn calc-defs))
1066 (setq calc-defs nil) 1062 (setq calc-defs nil)
1067 (calc-set-mode-line))) 1063 (calc-set-mode-line)))
1068 (calc-check-defines) 1064 (calc-check-defines))
1069 )
1070 1065
1071 (defun calc-check-defines () 1066 (defun calc-check-defines ()
1072 (if (symbol-plist 'calc-define) 1067 (if (symbol-plist 'calc-define)
1073 (let ((plist (copy-sequence (symbol-plist 'calc-define)))) 1068 (let ((plist (copy-sequence (symbol-plist 'calc-define))))
1074 (while (and plist (null (nth 1 plist))) 1069 (while (and plist (null (nth 1 plist)))
1082 (put 'calc-define (car plist) nil) 1077 (put 'calc-define (car plist) nil)
1083 (eval (nth 1 plist)) 1078 (eval (nth 1 plist))
1084 (setq plist (cdr (cdr plist)))) 1079 (setq plist (cdr (cdr plist))))
1085 ;; See if this has added any more calc-define properties. 1080 ;; See if this has added any more calc-define properties.
1086 (calc-check-defines)) 1081 (calc-check-defines))
1087 (setplist 'calc-define nil)))) 1082 (setplist 'calc-define nil)))))
1088 )
1089 (setq calc-check-defines 'calc-check-defines) ; suitable for run-hooks 1083 (setq calc-check-defines 'calc-check-defines) ; suitable for run-hooks
1090 1084
1091 (defun calc-trail-mode (&optional buf) 1085 (defun calc-trail-mode (&optional buf)
1092 "Calc Trail mode. 1086 "Calc Trail mode.
1093 This mode is used by the *Calc Trail* buffer, which records all results 1087 This mode is used by the *Calc Trail* buffer, which records all results
1113 (setq calc-main-buffer buf))) 1107 (setq calc-main-buffer buf)))
1114 (if (= (buffer-size) 0) 1108 (if (= (buffer-size) 0)
1115 (let ((buffer-read-only nil)) 1109 (let ((buffer-read-only nil))
1116 (insert "Emacs Calculator v" calc-version " by Dave Gillespie, " 1110 (insert "Emacs Calculator v" calc-version " by Dave Gillespie, "
1117 "installed " calc-installed-date "\n"))) 1111 "installed " calc-installed-date "\n")))
1118 (run-hooks 'calc-trail-mode-hook) 1112 (run-hooks 'calc-trail-mode-hook))
1119 )
1120 1113
1121 (defun calc-create-buffer () 1114 (defun calc-create-buffer ()
1122 (set-buffer (get-buffer-create "*Calculator*")) 1115 (set-buffer (get-buffer-create "*Calculator*"))
1123 (or (eq major-mode 'calc-mode) 1116 (or (eq major-mode 'calc-mode)
1124 (calc-mode)) 1117 (calc-mode))
1126 (if calc-always-load-extensions 1119 (if calc-always-load-extensions
1127 (calc-extensions)) 1120 (calc-extensions))
1128 (if calc-language 1121 (if calc-language
1129 (progn 1122 (progn
1130 (calc-extensions) 1123 (calc-extensions)
1131 (calc-set-language calc-language calc-language-option t))) 1124 (calc-set-language calc-language calc-language-option t))))
1132 )
1133 1125
1134 ;;;###autoload 1126 ;;;###autoload
1135 (defun calc (&optional arg full-display interactive) 1127 (defun calc (&optional arg full-display interactive)
1136 "The Emacs Calculator. Full documentation is listed under \"calc-mode\"." 1128 "The Emacs Calculator. Full documentation is listed under \"calc-mode\"."
1137 (interactive "P") 1129 (interactive "P")
1189 (and calc-said-hello 1181 (and calc-said-hello
1190 (or (interactive-p) interactive) 1182 (or (interactive-p) interactive)
1191 (progn 1183 (progn
1192 (sit-for 2) 1184 (sit-for 2)
1193 (message ""))) 1185 (message "")))
1194 (setq calc-said-hello t)))) 1186 (setq calc-said-hello t)))))
1195 )
1196 1187
1197 ;;;###autoload 1188 ;;;###autoload
1198 (defun full-calc () 1189 (defun full-calc ()
1199 "Invoke the Calculator and give it a full-sized window." 1190 "Invoke the Calculator and give it a full-sized window."
1200 (interactive) 1191 (interactive)
1201 (calc nil t (interactive-p)) 1192 (calc nil t (interactive-p)))
1202 )
1203 1193
1204 (defun calc-same-interface (arg) 1194 (defun calc-same-interface (arg)
1205 "Invoke the Calculator using the most recent interface (calc or calc-keypad)." 1195 "Invoke the Calculator using the most recent interface (calc or calc-keypad)."
1206 (interactive "P") 1196 (interactive "P")
1207 (if (and (equal (buffer-name) "*Gnuplot Trail*") 1197 (if (and (equal (buffer-name) "*Gnuplot Trail*")
1211 (calc-edit-finish arg) 1201 (calc-edit-finish arg)
1212 (if (eq major-mode 'MacEdit-mode) 1202 (if (eq major-mode 'MacEdit-mode)
1213 (MacEdit-finish-edit) 1203 (MacEdit-finish-edit)
1214 (if calc-was-keypad-mode 1204 (if calc-was-keypad-mode
1215 (calc-keypad) 1205 (calc-keypad)
1216 (calc arg calc-full-mode t))))) 1206 (calc arg calc-full-mode t))))))
1217 )
1218 1207
1219 1208
1220 (defun calc-quit (&optional non-fatal) 1209 (defun calc-quit (&optional non-fatal)
1221 (interactive) 1210 (interactive)
1222 (and calc-standalone-flag (not non-fatal) 1211 (and calc-standalone-flag (not non-fatal)
1251 (calc-delete-windows-keep buf kbuf) 1240 (calc-delete-windows-keep buf kbuf)
1252 (delete-windows-on buf) 1241 (delete-windows-on buf)
1253 (delete-windows-on kbuf)) 1242 (delete-windows-on kbuf))
1254 (bury-buffer buf) 1243 (bury-buffer buf)
1255 (bury-buffer calc-trail-buffer) 1244 (bury-buffer calc-trail-buffer)
1256 (and kbuf (bury-buffer kbuf)))))) 1245 (and kbuf (bury-buffer kbuf)))))))
1257 )
1258 1246
1259 ;;;###autoload 1247 ;;;###autoload
1260 (defun quick-calc () 1248 (defun quick-calc ()
1261 "Do a quick calculation in the minibuffer without invoking full Calculator." 1249 "Do a quick calculation in the minibuffer without invoking full Calculator."
1262 (interactive) 1250 (interactive)
1263 (calc-do-quick-calc) 1251 (calc-do-quick-calc))
1264 )
1265 1252
1266 ;;;###autoload 1253 ;;;###autoload
1267 (defun calc-eval (str &optional separator &rest args) 1254 (defun calc-eval (str &optional separator &rest args)
1268 "Do a quick calculation and return the result as a string. 1255 "Do a quick calculation and return the result as a string.
1269 Return value will either be the formatted result in string form, 1256 Return value will either be the formatted result in string form,
1270 or a list containing a character position and an error message in string form." 1257 or a list containing a character position and an error message in string form."
1271 (calc-do-calc-eval str separator args) 1258 (calc-do-calc-eval str separator args))
1272 )
1273 1259
1274 ;;;###autoload 1260 ;;;###autoload
1275 (defun calc-keypad () 1261 (defun calc-keypad ()
1276 "Invoke the Calculator in \"visual keypad\" mode. 1262 "Invoke the Calculator in \"visual keypad\" mode.
1277 This is most useful in the X window system. 1263 This is most useful in the X window system.
1278 In this mode, click on the Calc \"buttons\" using the left mouse button. 1264 In this mode, click on the Calc \"buttons\" using the left mouse button.
1279 Or, position the cursor manually and do M-x calc-keypad-press." 1265 Or, position the cursor manually and do M-x calc-keypad-press."
1280 (interactive) 1266 (interactive)
1281 (calc-extensions) 1267 (calc-extensions)
1282 (calc-do-keypad calc-full-mode (interactive-p)) 1268 (calc-do-keypad calc-full-mode (interactive-p)))
1283 )
1284 1269
1285 ;;;###autoload 1270 ;;;###autoload
1286 (defun full-calc-keypad () 1271 (defun full-calc-keypad ()
1287 "Invoke the Calculator in full-screen \"visual keypad\" mode. 1272 "Invoke the Calculator in full-screen \"visual keypad\" mode.
1288 See calc-keypad for details." 1273 See calc-keypad for details."
1289 (interactive) 1274 (interactive)
1290 (calc-extensions) 1275 (calc-extensions)
1291 (calc-do-keypad t (interactive-p)) 1276 (calc-do-keypad t (interactive-p)))
1292 )
1293 1277
1294 1278
1295 ;;; Note that modifications to this function may break calc-pass-errors. 1279 ;;; Note that modifications to this function may break calc-pass-errors.
1296 (defun calc-do (do-body &optional do-slow) 1280 (defun calc-do (do-body &optional do-slow)
1297 (calc-check-defines) 1281 (calc-check-defines)
1365 (and (memq 'do-edit calc-command-flags) 1349 (and (memq 'do-edit calc-command-flags)
1366 (switch-to-buffer (get-buffer-create "*Calc Edit*"))) 1350 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
1367 (calc-set-mode-line) 1351 (calc-set-mode-line)
1368 (and calc-embedded-info 1352 (and calc-embedded-info
1369 (calc-embedded-finish-command)))) 1353 (calc-embedded-finish-command))))
1370 (identity nil) ; allow a GC after timing is done 1354 (identity nil)) ; allow a GC after timing is done
1371 ) 1355
1372 (setq calc-aborted-prefix nil) 1356 (setq calc-aborted-prefix nil)
1373 (setq calc-start-time nil) 1357 (setq calc-start-time nil)
1374 1358
1375 (defun calc-set-command-flag (f) 1359 (defun calc-set-command-flag (f)
1376 (if (not (memq f calc-command-flags)) 1360 (if (not (memq f calc-command-flags))
1377 (setq calc-command-flags (cons f calc-command-flags))) 1361 (setq calc-command-flags (cons f calc-command-flags))))
1378 )
1379 1362
1380 (defun calc-select-buffer () 1363 (defun calc-select-buffer ()
1381 (or (eq major-mode 'calc-mode) 1364 (or (eq major-mode 'calc-mode)
1382 (if calc-main-buffer 1365 (if calc-main-buffer
1383 (set-buffer calc-main-buffer) 1366 (set-buffer calc-main-buffer)
1384 (let ((buf (get-buffer "*Calculator*"))) 1367 (let ((buf (get-buffer "*Calculator*")))
1385 (if buf 1368 (if buf
1386 (set-buffer buf) 1369 (set-buffer buf)
1387 (error "Calculator buffer not available"))))) 1370 (error "Calculator buffer not available"))))))
1388 )
1389 1371
1390 (defun calc-cursor-stack-index (&optional index) 1372 (defun calc-cursor-stack-index (&optional index)
1391 (goto-char (point-max)) 1373 (goto-char (point-max))
1392 (forward-line (- (calc-substack-height (or index 1)))) 1374 (forward-line (- (calc-substack-height (or index 1)))))
1393 )
1394 1375
1395 (defun calc-stack-size () 1376 (defun calc-stack-size ()
1396 (- (length calc-stack) calc-stack-top) 1377 (- (length calc-stack) calc-stack-top))
1397 )
1398 1378
1399 (defun calc-substack-height (n) 1379 (defun calc-substack-height (n)
1400 (let ((sum 0) 1380 (let ((sum 0)
1401 (stack calc-stack)) 1381 (stack calc-stack))
1402 (setq n (+ n calc-stack-top)) 1382 (setq n (+ n calc-stack-top))
1403 (while (and (> n 0) stack) 1383 (while (and (> n 0) stack)
1404 (setq sum (+ sum (nth 1 (car stack))) 1384 (setq sum (+ sum (nth 1 (car stack)))
1405 n (1- n) 1385 n (1- n)
1406 stack (cdr stack))) 1386 stack (cdr stack)))
1407 sum) 1387 sum))
1408 )
1409 1388
1410 (defun calc-set-mode-line () 1389 (defun calc-set-mode-line ()
1411 (save-excursion 1390 (save-excursion
1412 (calc-select-buffer) 1391 (calc-select-buffer)
1413 (let* ((fmt (car calc-float-format)) 1392 (let* ((fmt (car calc-float-format))
1508 (apply 'concat calc-other-modes))))) 1487 (apply 'concat calc-other-modes)))))
1509 (if (equal new-mode-string mode-line-buffer-identification) 1488 (if (equal new-mode-string mode-line-buffer-identification)
1510 nil 1489 nil
1511 (setq mode-line-buffer-identification new-mode-string) 1490 (setq mode-line-buffer-identification new-mode-string)
1512 (set-buffer-modified-p (buffer-modified-p)) 1491 (set-buffer-modified-p (buffer-modified-p))
1513 (and calc-embedded-info (calc-embedded-mode-line-change))))) 1492 (and calc-embedded-info (calc-embedded-mode-line-change))))))
1514 )
1515 1493
1516 (defun calc-align-stack-window () 1494 (defun calc-align-stack-window ()
1517 (if (eq major-mode 'calc-mode) 1495 (if (eq major-mode 'calc-mode)
1518 (progn 1496 (progn
1519 (let ((win (get-buffer-window (current-buffer)))) 1497 (let ((win (get-buffer-window (current-buffer))))
1525 (calc-cursor-stack-index 0) 1503 (calc-cursor-stack-index 0)
1526 (if (looking-at " *\\.$") 1504 (if (looking-at " *\\.$")
1527 (goto-char (1- (match-end 0))))) 1505 (goto-char (1- (match-end 0)))))
1528 (save-excursion 1506 (save-excursion
1529 (calc-select-buffer) 1507 (calc-select-buffer)
1530 (calc-align-stack-window))) 1508 (calc-align-stack-window))))
1531 )
1532 1509
1533 (defun calc-check-stack (n) 1510 (defun calc-check-stack (n)
1534 (if (> n (calc-stack-size)) 1511 (if (> n (calc-stack-size))
1535 (error "Too few elements on stack")) 1512 (error "Too few elements on stack"))
1536 (if (< n 0) 1513 (if (< n 0)
1537 (error "Invalid argument")) 1514 (error "Invalid argument")))
1538 )
1539 1515
1540 (defun calc-push-list (vals &optional m sels) 1516 (defun calc-push-list (vals &optional m sels)
1541 (while vals 1517 (while vals
1542 (if calc-executing-macro 1518 (if calc-executing-macro
1543 (calc-push-list-in-macro vals m sels) 1519 (calc-push-list-in-macro vals m sels)
1554 (let ((buffer-read-only nil)) 1530 (let ((buffer-read-only nil))
1555 (insert (math-format-stack-value entry) "\n")) 1531 (insert (math-format-stack-value entry) "\n"))
1556 (calc-record-undo (list 'push mm)) 1532 (calc-record-undo (list 'push mm))
1557 (calc-set-command-flag 'renum-stack)))) 1533 (calc-set-command-flag 'renum-stack))))
1558 (setq vals (cdr vals) 1534 (setq vals (cdr vals)
1559 sels (cdr sels))) 1535 sels (cdr sels))))
1560 )
1561 1536
1562 (defun calc-pop-push-list (n vals &optional m sels) 1537 (defun calc-pop-push-list (n vals &optional m sels)
1563 (if (and calc-any-selections (null sels)) 1538 (if (and calc-any-selections (null sels))
1564 (calc-replace-selections n vals m) 1539 (calc-replace-selections n vals m)
1565 (calc-pop-stack n m sels) 1540 (calc-pop-stack n m sels)
1566 (calc-push-list vals m sels)) 1541 (calc-push-list vals m sels)))
1567 )
1568 1542
1569 (defun calc-pop-push-record-list (n prefix vals &optional m sels) 1543 (defun calc-pop-push-record-list (n prefix vals &optional m sels)
1570 (or (and (consp vals) 1544 (or (and (consp vals)
1571 (or (integerp (car vals)) 1545 (or (integerp (car vals))
1572 (consp (car vals)))) 1546 (consp (car vals))))
1575 (calc-check-stack (+ n (or m 1) -1)) 1549 (calc-check-stack (+ n (or m 1) -1))
1576 (if prefix 1550 (if prefix
1577 (if (cdr vals) 1551 (if (cdr vals)
1578 (calc-record-list vals prefix) 1552 (calc-record-list vals prefix)
1579 (calc-record (car vals) prefix))) 1553 (calc-record (car vals) prefix)))
1580 (calc-pop-push-list n vals m sels) 1554 (calc-pop-push-list n vals m sels))
1581 )
1582 1555
1583 (defun calc-enter-result (n prefix vals &optional m) 1556 (defun calc-enter-result (n prefix vals &optional m)
1584 (setq calc-aborted-prefix prefix) 1557 (setq calc-aborted-prefix prefix)
1585 (if (and (consp vals) 1558 (if (and (consp vals)
1586 (or (integerp (car vals)) 1559 (or (integerp (car vals))
1592 (consp (car vals)))) 1565 (consp (car vals))))
1593 (setq vals (list vals))) 1566 (setq vals (list vals)))
1594 (if (equal vals '((nil))) 1567 (if (equal vals '((nil)))
1595 (setq vals nil)) 1568 (setq vals nil))
1596 (calc-pop-push-record-list n prefix vals m) 1569 (calc-pop-push-record-list n prefix vals m)
1597 (calc-handle-whys) 1570 (calc-handle-whys))
1598 )
1599 1571
1600 (defun calc-normalize (val) 1572 (defun calc-normalize (val)
1601 (if (memq calc-simplify-mode '(nil none num)) 1573 (if (memq calc-simplify-mode '(nil none num))
1602 (math-normalize val) 1574 (math-normalize val)
1603 (calc-extensions) 1575 (calc-extensions)
1604 (calc-normalize-fancy val)) 1576 (calc-normalize-fancy val)))
1605 )
1606 1577
1607 (defun calc-handle-whys () 1578 (defun calc-handle-whys ()
1608 (if calc-next-why 1579 (if calc-next-why
1609 (calc-do-handle-whys)) 1580 (calc-do-handle-whys)))
1610 )
1611 1581
1612 1582
1613 (defun calc-pop-stack (&optional n m sel-ok) ; pop N objs at level M of stack. 1583 (defun calc-pop-stack (&optional n m sel-ok) ; pop N objs at level M of stack.
1614 (or n (setq n 1)) 1584 (or n (setq n 1))
1615 (or m (setq m 1)) 1585 (or m (setq m 1))
1633 (setcdr (nthcdr (- mm 2) calc-stack) 1603 (setcdr (nthcdr (- mm 2) calc-stack)
1634 (nthcdr (+ n mm -1) calc-stack))) 1604 (nthcdr (+ n mm -1) calc-stack)))
1635 (calc-cursor-stack-index n) 1605 (calc-cursor-stack-index n)
1636 (setq calc-stack (nthcdr n calc-stack)) 1606 (setq calc-stack (nthcdr n calc-stack))
1637 (delete-region (point) (point-max)))) 1607 (delete-region (point) (point-max))))
1638 (calc-set-command-flag 'renum-stack))))) 1608 (calc-set-command-flag 'renum-stack))))))
1639 )
1640 1609
1641 (defun calc-get-stack-element (x) 1610 (defun calc-get-stack-element (x)
1642 (cond ((eq sel-mode 'entry) 1611 (cond ((eq sel-mode 'entry)
1643 x) 1612 x)
1644 ((eq sel-mode 'sel) 1613 ((eq sel-mode 'sel)
1647 (eq sel-mode 'full) 1616 (eq sel-mode 'full)
1648 (not calc-use-selections)) 1617 (not calc-use-selections))
1649 (car x)) 1618 (car x))
1650 (sel-mode 1619 (sel-mode
1651 (calc-sel-error)) 1620 (calc-sel-error))
1652 (t (nth 2 x))) 1621 (t (nth 2 x))))
1653 )
1654 1622
1655 ;; Get the Nth element of the stack (N=1 is the top element). 1623 ;; Get the Nth element of the stack (N=1 is the top element).
1656 (defun calc-top (&optional n sel-mode) 1624 (defun calc-top (&optional n sel-mode)
1657 (or n (setq n 1)) 1625 (or n (setq n 1))
1658 (calc-check-stack n) 1626 (calc-check-stack n)
1659 (calc-get-stack-element (nth (+ n calc-stack-top -1) calc-stack)) 1627 (calc-get-stack-element (nth (+ n calc-stack-top -1) calc-stack)))
1660 )
1661 1628
1662 (defun calc-top-n (&optional n sel-mode) ; in case precision has changed 1629 (defun calc-top-n (&optional n sel-mode) ; in case precision has changed
1663 (math-check-complete (calc-normalize (calc-top n sel-mode))) 1630 (math-check-complete (calc-normalize (calc-top n sel-mode))))
1664 )
1665 1631
1666 (defun calc-top-list (&optional n m sel-mode) 1632 (defun calc-top-list (&optional n m sel-mode)
1667 (or n (setq n 1)) 1633 (or n (setq n 1))
1668 (or m (setq m 1)) 1634 (or m (setq m 1))
1669 (calc-check-stack (+ n m -1)) 1635 (calc-check-stack (+ n m -1))
1670 (and (> n 0) 1636 (and (> n 0)
1671 (let ((top (copy-sequence (nthcdr (+ m calc-stack-top -1) 1637 (let ((top (copy-sequence (nthcdr (+ m calc-stack-top -1)
1672 calc-stack)))) 1638 calc-stack))))
1673 (setcdr (nthcdr (1- n) top) nil) 1639 (setcdr (nthcdr (1- n) top) nil)
1674 (nreverse (mapcar 'calc-get-stack-element top)))) 1640 (nreverse (mapcar 'calc-get-stack-element top)))))
1675 )
1676 1641
1677 (defun calc-top-list-n (&optional n m sel-mode) 1642 (defun calc-top-list-n (&optional n m sel-mode)
1678 (mapcar 'math-check-complete 1643 (mapcar 'math-check-complete
1679 (mapcar 'calc-normalize (calc-top-list n m sel-mode))) 1644 (mapcar 'calc-normalize (calc-top-list n m sel-mode))))
1680 )
1681 1645
1682 1646
1683 (defun calc-renumber-stack () 1647 (defun calc-renumber-stack ()
1684 (if calc-line-numbering 1648 (if calc-line-numbering
1685 (save-excursion 1649 (save-excursion
1707 calc-use-selections) "*" ":") 1671 calc-use-selections) "*" ":")
1708 (make-string (- 3 (length prefix)) 32)))) 1672 (make-string (- 3 (length prefix)) 32))))
1709 (beginning-of-line) 1673 (beginning-of-line)
1710 (setq lnum (1+ lnum) 1674 (setq lnum (1+ lnum)
1711 stack (cdr stack)))))) 1675 stack (cdr stack))))))
1712 (and calc-embedded-info (calc-embedded-stack-change)) 1676 (and calc-embedded-info (calc-embedded-stack-change)))
1713 )
1714 1677
1715 (defun calc-refresh (&optional align) 1678 (defun calc-refresh (&optional align)
1716 (interactive) 1679 (interactive)
1717 (and (eq major-mode 'calc-mode) 1680 (and (eq major-mode 'calc-mode)
1718 (not calc-executing-macro) 1681 (not calc-executing-macro)
1741 (if save-mark (set-mark save-mark)))) 1704 (if save-mark (set-mark save-mark))))
1742 (and calc-embedded-info (not (eq major-mode 'calc-mode)) 1705 (and calc-embedded-info (not (eq major-mode 'calc-mode))
1743 (save-excursion 1706 (save-excursion
1744 (set-buffer (aref calc-embedded-info 1)) 1707 (set-buffer (aref calc-embedded-info 1))
1745 (calc-refresh align))) 1708 (calc-refresh align)))
1746 (setq calc-refresh-count (1+ calc-refresh-count)) 1709 (setq calc-refresh-count (1+ calc-refresh-count)))
1747 )
1748 1710
1749 1711
1750 (defun calc-x-paste-text (arg) 1712 (defun calc-x-paste-text (arg)
1751 "Move point to mouse position and insert window system cut buffer contents. 1713 "Move point to mouse position and insert window system cut buffer contents.
1752 If mouse is pressed in Calc window, push cut buffer contents onto the stack." 1714 If mouse is pressed in Calc window, push cut buffer contents onto the stack."
1761 (progn 1723 (progn
1762 (setq val (math-read-exprs buf)) 1724 (setq val (math-read-exprs buf))
1763 (if (eq (car-safe val) 'error) 1725 (if (eq (car-safe val) 'error)
1764 (error "%s in yanked data" (nth 2 val))))) 1726 (error "%s in yanked data" (nth 2 val)))))
1765 (calc-enter-result 0 "Xynk" val)))) 1727 (calc-enter-result 0 "Xynk" val))))
1766 (x-paste-text arg)) 1728 (x-paste-text arg)))
1767 )
1768 1729
1769 1730
1770 1731
1771 ;;;; The Calc Trail buffer. 1732 ;;;; The Calc Trail buffer.
1772 1733
1773 (defun calc-check-trail-aligned () 1734 (defun calc-check-trail-aligned ()
1774 (save-excursion 1735 (save-excursion
1775 (let ((win (get-buffer-window (current-buffer)))) 1736 (let ((win (get-buffer-window (current-buffer))))
1776 (and win 1737 (and win
1777 (pos-visible-in-window-p (1- (point-max)) win)))) 1738 (pos-visible-in-window-p (1- (point-max)) win)))))
1778 )
1779 1739
1780 (defun calc-trail-buffer () 1740 (defun calc-trail-buffer ()
1781 (and (or (null calc-trail-buffer) 1741 (and (or (null calc-trail-buffer)
1782 (null (buffer-name calc-trail-buffer))) 1742 (null (buffer-name calc-trail-buffer)))
1783 (save-excursion 1743 (save-excursion
1792 (eq (marker-buffer calc-trail-pointer) calc-trail-buffer)) 1752 (eq (marker-buffer calc-trail-pointer) calc-trail-buffer))
1793 (save-excursion 1753 (save-excursion
1794 (set-buffer calc-trail-buffer) 1754 (set-buffer calc-trail-buffer)
1795 (goto-line 2) 1755 (goto-line 2)
1796 (setq calc-trail-pointer (point-marker)))) 1756 (setq calc-trail-pointer (point-marker))))
1797 calc-trail-buffer 1757 calc-trail-buffer)
1798 )
1799 1758
1800 (defun calc-record (val &optional prefix) 1759 (defun calc-record (val &optional prefix)
1801 (setq calc-aborted-prefix nil) 1760 (setq calc-aborted-prefix nil)
1802 (or calc-executing-macro 1761 (or calc-executing-macro
1803 (let* ((mainbuf (current-buffer)) 1762 (let* ((mainbuf (current-buffer))
1823 (insert fval "\n") 1782 (insert fval "\n")
1824 (let ((win (get-buffer-window buf))) 1783 (let ((win (get-buffer-window buf)))
1825 (if (and aligned win (not (memq 'hold-trail calc-command-flags))) 1784 (if (and aligned win (not (memq 'hold-trail calc-command-flags)))
1826 (calc-trail-here)) 1785 (calc-trail-here))
1827 (goto-char (1- (point-max)))))))) 1786 (goto-char (1- (point-max))))))))
1828 val 1787 val)
1829 )
1830 1788
1831 1789
1832 (defun calc-trail-display (flag &optional no-refresh) 1790 (defun calc-trail-display (flag &optional no-refresh)
1833 (interactive "P") 1791 (interactive "P")
1834 (let ((win (get-buffer-window (calc-trail-buffer)))) 1792 (let ((win (get-buffer-window (calc-trail-buffer))))
1853 (calc-wrapper 1811 (calc-wrapper
1854 (or no-refresh 1812 (or no-refresh
1855 (if (interactive-p) 1813 (if (interactive-p)
1856 (calc-do-refresh) 1814 (calc-do-refresh)
1857 (calc-refresh)))))))) 1815 (calc-refresh))))))))
1858 calc-trail-buffer 1816 calc-trail-buffer)
1859 )
1860 1817
1861 (defun calc-trail-here () 1818 (defun calc-trail-here ()
1862 (interactive) 1819 (interactive)
1863 (if (eq major-mode 'calc-trail-mode) 1820 (if (eq major-mode 'calc-trail-mode)
1864 (progn 1821 (progn
1884 (set-window-start win (point)) 1841 (set-window-start win (point))
1885 (set-window-point win (+ calc-trail-pointer 4)) 1842 (set-window-point win (+ calc-trail-pointer 4))
1886 (set-buffer calc-main-buffer) 1843 (set-buffer calc-main-buffer)
1887 (setq overlay-arrow-string calc-trail-overlay 1844 (setq overlay-arrow-string calc-trail-overlay
1888 overlay-arrow-position calc-trail-pointer)))))) 1845 overlay-arrow-position calc-trail-pointer))))))
1889 (error "Not in Calc Trail buffer")) 1846 (error "Not in Calc Trail buffer")))
1890 )
1891 1847
1892 1848
1893 1849
1894 1850
1895 ;;;; The Undo list. 1851 ;;;; The Undo list.
1899 (if (memq 'undo calc-command-flags) 1855 (if (memq 'undo calc-command-flags)
1900 (setq calc-undo-list (cons (cons rec (car calc-undo-list)) 1856 (setq calc-undo-list (cons (cons rec (car calc-undo-list))
1901 (cdr calc-undo-list))) 1857 (cdr calc-undo-list)))
1902 (setq calc-undo-list (cons (list rec) calc-undo-list) 1858 (setq calc-undo-list (cons (list rec) calc-undo-list)
1903 calc-redo-list nil) 1859 calc-redo-list nil)
1904 (calc-set-command-flag 'undo))) 1860 (calc-set-command-flag 'undo))))
1905 )
1906 1861
1907 1862
1908 1863
1909 1864
1910 ;;; Arithmetic commands. 1865 ;;; Arithmetic commands.
1914 (if (null arg) 1869 (if (null arg)
1915 (calc-enter-result 2 name (cons (or func2 func) 1870 (calc-enter-result 2 name (cons (or func2 func)
1916 (mapcar 'math-check-complete 1871 (mapcar 'math-check-complete
1917 (calc-top-list 2)))) 1872 (calc-top-list 2))))
1918 (calc-extensions) 1873 (calc-extensions)
1919 (calc-binary-op-fancy name func arg ident unary)) 1874 (calc-binary-op-fancy name func arg ident unary)))
1920 )
1921 1875
1922 (defun calc-unary-op (name func arg &optional func2) 1876 (defun calc-unary-op (name func arg &optional func2)
1923 (setq calc-aborted-prefix name) 1877 (setq calc-aborted-prefix name)
1924 (if (null arg) 1878 (if (null arg)
1925 (calc-enter-result 1 name (list (or func2 func) 1879 (calc-enter-result 1 name (list (or func2 func)
1926 (math-check-complete (calc-top 1)))) 1880 (math-check-complete (calc-top 1))))
1927 (calc-extensions) 1881 (calc-extensions)
1928 (calc-unary-op-fancy name func arg)) 1882 (calc-unary-op-fancy name func arg)))
1929 )
1930 1883
1931 1884
1932 (defun calc-plus (arg) 1885 (defun calc-plus (arg)
1933 (interactive "P") 1886 (interactive "P")
1934 (calc-slow-wrapper 1887 (calc-slow-wrapper
1935 (calc-binary-op "+" 'calcFunc-add arg 0 nil '+)) 1888 (calc-binary-op "+" 'calcFunc-add arg 0 nil '+)))
1936 )
1937 1889
1938 (defun calc-minus (arg) 1890 (defun calc-minus (arg)
1939 (interactive "P") 1891 (interactive "P")
1940 (calc-slow-wrapper 1892 (calc-slow-wrapper
1941 (calc-binary-op "-" 'calcFunc-sub arg 0 'neg '-)) 1893 (calc-binary-op "-" 'calcFunc-sub arg 0 'neg '-)))
1942 )
1943 1894
1944 (defun calc-times (arg) 1895 (defun calc-times (arg)
1945 (interactive "P") 1896 (interactive "P")
1946 (calc-slow-wrapper 1897 (calc-slow-wrapper
1947 (calc-binary-op "*" 'calcFunc-mul arg 1 nil '*)) 1898 (calc-binary-op "*" 'calcFunc-mul arg 1 nil '*)))
1948 )
1949 1899
1950 (defun calc-divide (arg) 1900 (defun calc-divide (arg)
1951 (interactive "P") 1901 (interactive "P")
1952 (calc-slow-wrapper 1902 (calc-slow-wrapper
1953 (calc-binary-op "/" 'calcFunc-div arg 0 'calcFunc-inv '/)) 1903 (calc-binary-op "/" 'calcFunc-div arg 0 'calcFunc-inv '/)))
1954 )
1955 1904
1956 1905
1957 (defun calc-change-sign (arg) 1906 (defun calc-change-sign (arg)
1958 (interactive "P") 1907 (interactive "P")
1959 (calc-wrapper 1908 (calc-wrapper
1960 (calc-unary-op "chs" 'neg arg)) 1909 (calc-unary-op "chs" 'neg arg)))
1961 )
1962 1910
1963 1911
1964 1912
1965 ;;; Stack management commands. 1913 ;;; Stack management commands.
1966 1914
1970 (cond ((< n 0) 1918 (cond ((< n 0)
1971 (calc-push-list (calc-top-list 1 (- n)))) 1919 (calc-push-list (calc-top-list 1 (- n))))
1972 ((= n 0) 1920 ((= n 0)
1973 (calc-push-list (calc-top-list (calc-stack-size)))) 1921 (calc-push-list (calc-top-list (calc-stack-size))))
1974 (t 1922 (t
1975 (calc-push-list (calc-top-list n))))) 1923 (calc-push-list (calc-top-list n))))))
1976 )
1977 1924
1978 1925
1979 (defun calc-pop (n) 1926 (defun calc-pop (n)
1980 (interactive "P") 1927 (interactive "P")
1981 (calc-wrapper 1928 (calc-wrapper
1997 (t 1944 (t
1998 (if (and calc-any-selections 1945 (if (and calc-any-selections
1999 (= nn 1) 1946 (= nn 1)
2000 (calc-top-selected 1 1)) 1947 (calc-top-selected 1 1))
2001 (calc-delete-selection 1) 1948 (calc-delete-selection 1)
2002 (calc-pop-stack nn)))))) 1949 (calc-pop-stack nn)))))))
2003 )
2004 1950
2005 1951
2006 1952
2007 1953
2008 ;;;; Reading a number using the minibuffer. 1954 ;;;; Reading a number using the minibuffer.
2040 (calc-push-list (list (calc-record (calc-normalize 1986 (calc-push-list (list (calc-record (calc-normalize
2041 calc-digit-value)))))) 1987 calc-digit-value))))))
2042 (if (eq calc-prev-char 'dots) 1988 (if (eq calc-prev-char 'dots)
2043 (progn 1989 (progn
2044 (calc-extensions) 1990 (calc-extensions)
2045 (calc-dots)))))) 1991 (calc-dots)))))))
2046 )
2047 1992
2048 (defsubst calc-minibuffer-size () 1993 (defsubst calc-minibuffer-size ()
2049 (- (point-max) (minibuffer-prompt-end))) 1994 (- (point-max) (minibuffer-prompt-end)))
2050 1995
2051 (defun calcDigit-nondigit () 1996 (defun calcDigit-nondigit ()
2065 (progn (setq prefix-arg current-prefix-arg) 2010 (progn (setq prefix-arg current-prefix-arg)
2066 (calc-unread-command (if (and (eq last-command-char 27) 2011 (calc-unread-command (if (and (eq last-command-char 27)
2067 (>= last-input-char 128)) 2012 (>= last-input-char 128))
2068 last-input-char 2013 last-input-char
2069 nil)))) 2014 nil))))
2070 (exit-minibuffer)) 2015 (exit-minibuffer)))
2071 )
2072 2016
2073 2017
2074 (defun calc-minibuffer-contains (rex) 2018 (defun calc-minibuffer-contains (rex)
2075 (save-excursion 2019 (save-excursion
2076 (goto-char (minibuffer-prompt-end)) 2020 (goto-char (minibuffer-prompt-end))
2077 (looking-at rex)) 2021 (looking-at rex)))
2078 )
2079 2022
2080 (defun calcDigit-key () 2023 (defun calcDigit-key ()
2081 (interactive) 2024 (interactive)
2082 (goto-char (point-max)) 2025 (goto-char (point-max))
2083 (if (or (and (memq last-command-char '(?+ ?-)) 2026 (if (or (and (memq last-command-char '(?+ ?-))
2172 (calc-digit-dots)) 2115 (calc-digit-dots))
2173 (delete-backward-char 1) 2116 (delete-backward-char 1)
2174 (beep) 2117 (beep)
2175 (calc-temp-minibuffer-message " [Bad format]")))))) 2118 (calc-temp-minibuffer-message " [Bad format]"))))))
2176 (setq calc-prev-prev-char calc-prev-char 2119 (setq calc-prev-prev-char calc-prev-char
2177 calc-prev-char last-command-char) 2120 calc-prev-char last-command-char))
2178 )
2179 2121
2180 2122
2181 (defun calcDigit-backspace () 2123 (defun calcDigit-backspace ()
2182 (interactive) 2124 (interactive)
2183 (goto-char (point-max)) 2125 (goto-char (point-max))
2191 (erase-buffer)) 2133 (erase-buffer))
2192 (t (backward-delete-char 1))) 2134 (t (backward-delete-char 1)))
2193 (if (= (calc-minibuffer-size) 0) 2135 (if (= (calc-minibuffer-size) 0)
2194 (progn 2136 (progn
2195 (setq last-command-char 13) 2137 (setq last-command-char 13)
2196 (calcDigit-nondigit))) 2138 (calcDigit-nondigit))))
2197 )
2198 2139
2199 2140
2200 2141
2201 2142
2202 2143
2399 (setq var-EvalRules nil) 2340 (setq var-EvalRules nil)
2400 (math-normalize (cons (car a) args))) 2341 (math-normalize (cons (car a) args)))
2401 (calc-record-why "*Variable is void" (nth 1 err))))) 2342 (calc-record-why "*Variable is void" (nth 1 err)))))
2402 (if (consp (car a)) 2343 (if (consp (car a))
2403 (math-dimension-error) 2344 (math-dimension-error)
2404 (cons (car a) args))))))) 2345 (cons (car a) args))))))))
2405 )
2406 2346
2407 2347
2408 2348
2409 ;;; True if A is a floating-point real or complex number. [P x] [Public] 2349 ;;; True if A is a floating-point real or complex number. [P x] [Public]
2410 (defun math-floatp (a) 2350 (defun math-floatp (a)
2412 ((memq (car-safe a) '(cplx polar mod sdev intv)) 2352 ((memq (car-safe a) '(cplx polar mod sdev intv))
2413 (or (math-floatp (nth 1 a)) 2353 (or (math-floatp (nth 1 a))
2414 (math-floatp (nth 2 a)) 2354 (math-floatp (nth 2 a))
2415 (and (eq (car a) 'intv) (math-floatp (nth 3 a))))) 2355 (and (eq (car a) 'intv) (math-floatp (nth 3 a)))))
2416 ((eq (car-safe a) 'date) 2356 ((eq (car-safe a) 'date)
2417 (math-floatp (nth 1 a)))) 2357 (math-floatp (nth 1 a)))))
2418 )
2419 2358
2420 2359
2421 2360
2422 ;;; Verify that A is a complete object and return A. [x x] [Public] 2361 ;;; Verify that A is a complete object and return A. [x x] [Public]
2423 (defun math-check-complete (a) 2362 (defun math-check-complete (a)
2424 (cond ((integerp a) a) 2363 (cond ((integerp a) a)
2425 ((eq (car-safe a) 'incomplete) 2364 ((eq (car-safe a) 'incomplete)
2426 (calc-incomplete-error a)) 2365 (calc-incomplete-error a))
2427 ((consp a) a) 2366 ((consp a) a)
2428 (t (error "Invalid data object encountered"))) 2367 (t (error "Invalid data object encountered"))))
2429 )
2430 2368
2431 2369
2432 2370
2433 ;;; Coerce integer A to be a bignum. [B S] 2371 ;;; Coerce integer A to be a bignum. [B S]
2434 (defun math-bignum (a) 2372 (defun math-bignum (a)
2435 (if (>= a 0) 2373 (if (>= a 0)
2436 (cons 'bigpos (math-bignum-big a)) 2374 (cons 'bigpos (math-bignum-big a))
2437 (cons 'bigneg (math-bignum-big (- a)))) 2375 (cons 'bigneg (math-bignum-big (- a)))))
2438 )
2439 2376
2440 (defun math-bignum-big (a) ; [L s] 2377 (defun math-bignum-big (a) ; [L s]
2441 (if (= a 0) 2378 (if (= a 0)
2442 nil 2379 nil
2443 (cons (% a 1000) (math-bignum-big (/ a 1000)))) 2380 (cons (% a 1000) (math-bignum-big (/ a 1000)))))
2444 )
2445 2381
2446 2382
2447 ;;; Build a normalized floating-point number. [F I S] 2383 ;;; Build a normalized floating-point number. [F I S]
2448 (defun math-make-float (mant exp) 2384 (defun math-make-float (mant exp)
2449 (if (eq mant 0) 2385 (if (eq mant 0)
2470 (<= (+ exp (math-numdigs mant) -1) -4000000)) 2406 (<= (+ exp (math-numdigs mant) -1) -4000000))
2471 (signal 'math-underflow nil) 2407 (signal 'math-underflow nil)
2472 (if (and (>= exp 3000000) 2408 (if (and (>= exp 3000000)
2473 (>= (+ exp (math-numdigs mant) -1) 4000000)) 2409 (>= (+ exp (math-numdigs mant) -1) 4000000))
2474 (signal 'math-overflow nil) 2410 (signal 'math-overflow nil)
2475 (list 'float mant exp)))) 2411 (list 'float mant exp)))))
2476 )
2477 2412
2478 (defun math-div10-bignum (a) ; [l l] 2413 (defun math-div10-bignum (a) ; [l l]
2479 (if (cdr a) 2414 (if (cdr a)
2480 (cons (+ (/ (car a) 10) (* (% (nth 1 a) 10) 100)) 2415 (cons (+ (/ (car a) 10) (* (% (nth 1 a) 10) 100))
2481 (math-div10-bignum (cdr a))) 2416 (math-div10-bignum (cdr a)))
2482 (list (/ (car a) 10))) 2417 (list (/ (car a) 10))))
2483 )
2484 2418
2485 ;;; Coerce A to be a float. [F N; V V] [Public] 2419 ;;; Coerce A to be a float. [F N; V V] [Public]
2486 (defun math-float (a) 2420 (defun math-float (a)
2487 (cond ((Math-integerp a) (math-make-float a 0)) 2421 (cond ((Math-integerp a) (math-make-float a 0))
2488 ((eq (car a) 'frac) (math-div (math-float (nth 1 a)) (nth 2 a))) 2422 ((eq (car a) 'frac) (math-div (math-float (nth 1 a)) (nth 2 a)))
2489 ((eq (car a) 'float) a) 2423 ((eq (car a) 'float) a)
2490 ((memq (car a) '(cplx polar vec hms date sdev mod)) 2424 ((memq (car a) '(cplx polar vec hms date sdev mod))
2491 (cons (car a) (mapcar 'math-float (cdr a)))) 2425 (cons (car a) (mapcar 'math-float (cdr a))))
2492 (t (math-float-fancy a))) 2426 (t (math-float-fancy a))))
2493 )
2494 2427
2495 2428
2496 (defun math-neg (a) 2429 (defun math-neg (a)
2497 (cond ((not (consp a)) (- a)) 2430 (cond ((not (consp a)) (- a))
2498 ((eq (car a) 'bigpos) (cons 'bigneg (cdr a))) 2431 ((eq (car a) 'bigpos) (cons 'bigneg (cdr a)))
2499 ((eq (car a) 'bigneg) (cons 'bigpos (cdr a))) 2432 ((eq (car a) 'bigneg) (cons 'bigpos (cdr a)))
2500 ((memq (car a) '(frac float)) 2433 ((memq (car a) '(frac float))
2501 (list (car a) (Math-integer-neg (nth 1 a)) (nth 2 a))) 2434 (list (car a) (Math-integer-neg (nth 1 a)) (nth 2 a)))
2502 ((memq (car a) '(cplx vec hms date calcFunc-idn)) 2435 ((memq (car a) '(cplx vec hms date calcFunc-idn))
2503 (cons (car a) (mapcar 'math-neg (cdr a)))) 2436 (cons (car a) (mapcar 'math-neg (cdr a))))
2504 (t (math-neg-fancy a))) 2437 (t (math-neg-fancy a))))
2505 )
2506 2438
2507 2439
2508 ;;; Compute the number of decimal digits in integer A. [S I] 2440 ;;; Compute the number of decimal digits in integer A. [S I]
2509 (defun math-numdigs (a) 2441 (defun math-numdigs (a)
2510 (if (consp a) 2442 (if (consp a)
2517 ((>= a 10) 2) 2449 ((>= a 10) 2)
2518 ((>= a 1) 1) 2450 ((>= a 1) 1)
2519 ((= a 0) 0) 2451 ((= a 0) 0)
2520 ((> a -10) 1) 2452 ((> a -10) 1)
2521 ((> a -100) 2) 2453 ((> a -100) 2)
2522 (t (math-numdigs (- a))))) 2454 (t (math-numdigs (- a))))))
2523 )
2524 2455
2525 ;;; Multiply (with truncation toward 0) the integer A by 10^N. [I i S] 2456 ;;; Multiply (with truncation toward 0) the integer A by 10^N. [I i S]
2526 (defun math-scale-int (a n) 2457 (defun math-scale-int (a n)
2527 (cond ((= n 0) a) 2458 (cond ((= n 0) a)
2528 ((> n 0) (math-scale-left a n)) 2459 ((> n 0) (math-scale-left a n))
2529 (t (math-normalize (math-scale-right a (- n))))) 2460 (t (math-normalize (math-scale-right a (- n))))))
2530 )
2531 2461
2532 (defun math-scale-left (a n) ; [I I S] 2462 (defun math-scale-left (a n) ; [I I S]
2533 (if (= n 0) 2463 (if (= n 0)
2534 a 2464 a
2535 (if (consp a) 2465 (if (consp a)
2542 (if (or (>= a 10000) (<= a -10000)) 2472 (if (or (>= a 10000) (<= a -10000))
2543 (math-scale-left (math-bignum a) 2) 2473 (math-scale-left (math-bignum a) 2)
2544 (* a 100)) 2474 (* a 100))
2545 (if (or (>= a 100000) (<= a -100000)) 2475 (if (or (>= a 100000) (<= a -100000))
2546 (math-scale-left (math-bignum a) 1) 2476 (math-scale-left (math-bignum a) 1)
2547 (* a 10)))))) 2477 (* a 10)))))))
2548 )
2549 2478
2550 (defun math-scale-left-bignum (a n) 2479 (defun math-scale-left-bignum (a n)
2551 (if (>= n 3) 2480 (if (>= n 3)
2552 (while (>= (setq a (cons 0 a) 2481 (while (>= (setq a (cons 0 a)
2553 n (- n 3)) 3))) 2482 n (- n 3)) 3)))
2554 (if (> n 0) 2483 (if (> n 0)
2555 (math-mul-bignum-digit a (if (= n 2) 100 10) 0) 2484 (math-mul-bignum-digit a (if (= n 2) 100 10) 0)
2556 a) 2485 a))
2557 )
2558 2486
2559 (defun math-scale-right (a n) ; [i i S] 2487 (defun math-scale-right (a n) ; [i i S]
2560 (if (= n 0) 2488 (if (= n 0)
2561 a 2489 a
2562 (if (consp a) 2490 (if (consp a)
2570 (>= (setq n (- n 3)) 3)))) 2498 (>= (setq n (- n 3)) 3))))
2571 (if (= n 2) 2499 (if (= n 2)
2572 (/ a 100) 2500 (/ a 100)
2573 (if (= n 1) 2501 (if (= n 1)
2574 (/ a 10) 2502 (/ a 10)
2575 a))))) 2503 a))))))
2576 )
2577 2504
2578 (defun math-scale-right-bignum (a n) ; [L L S; l l S] 2505 (defun math-scale-right-bignum (a n) ; [L L S; l l S]
2579 (if (>= n 3) 2506 (if (>= n 3)
2580 (setq a (nthcdr (/ n 3) a) 2507 (setq a (nthcdr (/ n 3) a)
2581 n (% n 3))) 2508 n (% n 3)))
2582 (if (> n 0) 2509 (if (> n 0)
2583 (cdr (math-mul-bignum-digit a (if (= n 2) 10 100) 0)) 2510 (cdr (math-mul-bignum-digit a (if (= n 2) 10 100) 0))
2584 a) 2511 a))
2585 )
2586 2512
2587 ;;; Multiply (with rounding) the integer A by 10^N. [I i S] 2513 ;;; Multiply (with rounding) the integer A by 10^N. [I i S]
2588 (defun math-scale-rounding (a n) 2514 (defun math-scale-rounding (a n)
2589 (cond ((>= n 0) 2515 (cond ((>= n 0)
2590 (math-scale-left a n)) 2516 (math-scale-left a n))
2608 (t 2534 (t
2609 (if (< a 0) 2535 (if (< a 0)
2610 (- (math-scale-rounding (- a) n)) 2536 (- (math-scale-rounding (- a) n))
2611 (if (= n -1) 2537 (if (= n -1)
2612 (/ (+ a 5) 10) 2538 (/ (+ a 5) 10)
2613 (/ (+ (math-scale-right a (- -1 n)) 5) 10))))) 2539 (/ (+ (math-scale-right a (- -1 n)) 5) 10))))))
2614 )
2615 2540
2616 2541
2617 ;;; Compute the sum of A and B. [O O O] [Public] 2542 ;;; Compute the sum of A and B. [O O O] [Public]
2618 (defun math-add (a b) 2543 (defun math-add (a b)
2619 (or 2544 (or
2659 (setq b (math-float b))) 2584 (setq b (math-float b)))
2660 (math-add-float a b))) 2585 (math-add-float a b)))
2661 (and (calc-extensions) 2586 (and (calc-extensions)
2662 (math-add-objects-fancy a b)))) 2587 (math-add-objects-fancy a b))))
2663 (and (calc-extensions) 2588 (and (calc-extensions)
2664 (math-add-symb-fancy a b))) 2589 (math-add-symb-fancy a b))))
2665 )
2666 2590
2667 (defun math-add-bignum (a b) ; [L L L; l l l] 2591 (defun math-add-bignum (a b) ; [L L L; l l l]
2668 (if a 2592 (if a
2669 (if b 2593 (if b
2670 (let* ((a (copy-sequence a)) (aa a) (carry nil) sum) 2594 (let* ((a (copy-sequence a)) (aa a) (carry nil) sum)
2694 (nconc a '(1)))) 2618 (nconc a '(1))))
2695 (if b 2619 (if b
2696 (nconc a b) 2620 (nconc a b)
2697 a))) 2621 a)))
2698 a) 2622 a)
2699 b) 2623 b))
2700 )
2701 2624
2702 (defun math-sub-bignum (a b) ; [l l l] 2625 (defun math-sub-bignum (a b) ; [l l l]
2703 (if b 2626 (if b
2704 (if a 2627 (if a
2705 (let* ((a (copy-sequence a)) (aa a) (borrow nil) sum) 2628 (let* ((a (copy-sequence a)) (aa a) (borrow nil) sum)
2733 a))) 2656 a)))
2734 (while (eq (car b) 0) 2657 (while (eq (car b) 0)
2735 (setq b (cdr b))) 2658 (setq b (cdr b)))
2736 (and b 2659 (and b
2737 'neg)) 2660 'neg))
2738 a) 2661 a))
2739 )
2740 2662
2741 (defun math-add-float (a b) ; [F F F] 2663 (defun math-add-float (a b) ; [F F F]
2742 (let ((ediff (- (nth 2 a) (nth 2 b)))) 2664 (let ((ediff (- (nth 2 a) (nth 2 b))))
2743 (if (>= ediff 0) 2665 (if (>= ediff 0)
2744 (if (>= ediff (+ calc-internal-prec calc-internal-prec)) 2666 (if (>= ediff (+ calc-internal-prec calc-internal-prec))
2751 (if (>= (setq ediff (- ediff)) 2673 (if (>= (setq ediff (- ediff))
2752 (+ calc-internal-prec calc-internal-prec)) 2674 (+ calc-internal-prec calc-internal-prec))
2753 b 2675 b
2754 (math-make-float (math-add (nth 1 a) 2676 (math-make-float (math-add (nth 1 a)
2755 (math-scale-left (nth 1 b) ediff)) 2677 (math-scale-left (nth 1 b) ediff))
2756 (nth 2 a))))) 2678 (nth 2 a))))))
2757 )
2758 2679
2759 ;;; Compute the difference of A and B. [O O O] [Public] 2680 ;;; Compute the difference of A and B. [O O O] [Public]
2760 (defun math-sub (a b) 2681 (defun math-sub (a b)
2761 (if (or (consp a) (consp b)) 2682 (if (or (consp a) (consp b))
2762 (math-add a (math-neg b)) 2683 (math-add a (math-neg b))
2763 (setq a (- a b)) 2684 (setq a (- a b))
2764 (if (or (<= a -1000000) (>= a 1000000)) 2685 (if (or (<= a -1000000) (>= a 1000000))
2765 (math-bignum a) 2686 (math-bignum a)
2766 a)) 2687 a)))
2767 )
2768 2688
2769 (defun math-sub-float (a b) ; [F F F] 2689 (defun math-sub-float (a b) ; [F F F]
2770 (let ((ediff (- (nth 2 a) (nth 2 b)))) 2690 (let ((ediff (- (nth 2 a) (nth 2 b))))
2771 (if (>= ediff 0) 2691 (if (>= ediff 0)
2772 (if (>= ediff (+ calc-internal-prec calc-internal-prec)) 2692 (if (>= ediff (+ calc-internal-prec calc-internal-prec))
2780 (+ calc-internal-prec calc-internal-prec)) 2700 (+ calc-internal-prec calc-internal-prec))
2781 b 2701 b
2782 (math-make-float (math-add (nth 1 a) 2702 (math-make-float (math-add (nth 1 a)
2783 (Math-integer-neg 2703 (Math-integer-neg
2784 (math-scale-left (nth 1 b) ediff))) 2704 (math-scale-left (nth 1 b) ediff)))
2785 (nth 2 a))))) 2705 (nth 2 a))))))
2786 )
2787 2706
2788 2707
2789 ;;; Compute the product of A and B. [O O O] [Public] 2708 ;;; Compute the product of A and B. [O O O] [Public]
2790 (defun math-mul (a b) 2709 (defun math-mul (a b)
2791 (or 2710 (or
2827 (math-make-float (math-mul (nth 1 a) (nth 1 b)) 2746 (math-make-float (math-mul (nth 1 a) (nth 1 b))
2828 (+ (nth 2 a) (nth 2 b))))) 2747 (+ (nth 2 a) (nth 2 b)))))
2829 (and (calc-extensions) 2748 (and (calc-extensions)
2830 (math-mul-objects-fancy a b)))) 2749 (math-mul-objects-fancy a b))))
2831 (and (calc-extensions) 2750 (and (calc-extensions)
2832 (math-mul-symb-fancy a b))) 2751 (math-mul-symb-fancy a b))))
2833 )
2834 2752
2835 (defun math-infinitep (a &optional undir) 2753 (defun math-infinitep (a &optional undir)
2836 (while (and (consp a) (memq (car a) '(* / neg))) 2754 (while (and (consp a) (memq (car a) '(* / neg)))
2837 (if (or (not (eq (car a) '*)) (math-infinitep (nth 1 a))) 2755 (if (or (not (eq (car a) '*)) (math-infinitep (nth 1 a)))
2838 (setq a (nth 1 a)) 2756 (setq a (nth 1 a))
2840 (and (consp a) 2758 (and (consp a)
2841 (eq (car a) 'var) 2759 (eq (car a) 'var)
2842 (memq (nth 2 a) '(var-inf var-uinf var-nan)) 2760 (memq (nth 2 a) '(var-inf var-uinf var-nan))
2843 (if (and undir (eq (nth 2 a) 'var-inf)) 2761 (if (and undir (eq (nth 2 a) 'var-inf))
2844 '(var uinf var-uinf) 2762 '(var uinf var-uinf)
2845 a)) 2763 a)))
2846 )
2847 2764
2848 ;;; Multiply digit lists A and B. [L L L; l l l] 2765 ;;; Multiply digit lists A and B. [L L L; l l l]
2849 (defun math-mul-bignum (a b) 2766 (defun math-mul-bignum (a b)
2850 (and a b 2767 (and a b
2851 (let* ((sum (if (<= (car b) 1) 2768 (let* ((sum (if (<= (car b) 1)
2867 ss (or (cdr ss) (setcdr ss (list 0))))) 2784 ss (or (cdr ss) (setcdr ss (list 0)))))
2868 (if (>= prod 1000) 2785 (if (>= prod 1000)
2869 (if (cdr ss) 2786 (if (cdr ss)
2870 (setcar (cdr ss) (+ (/ prod 1000) (car (cdr ss)))) 2787 (setcar (cdr ss) (+ (/ prod 1000) (car (cdr ss))))
2871 (setcdr ss (list (/ prod 1000)))))) 2788 (setcdr ss (list (/ prod 1000))))))
2872 sum)) 2789 sum)))
2873 )
2874 2790
2875 ;;; Multiply digit list A by digit D. [L L D D; l l D D] 2791 ;;; Multiply digit list A by digit D. [L L D D; l l D D]
2876 (defun math-mul-bignum-digit (a d c) 2792 (defun math-mul-bignum-digit (a d c)
2877 (if a 2793 (if a
2878 (if (<= d 1) 2794 (if (<= d 1)
2885 c (/ prod 1000))) 2801 c (/ prod 1000)))
2886 (if (>= prod 1000) 2802 (if (>= prod 1000)
2887 (setcdr aa (list (/ prod 1000)))) 2803 (setcdr aa (list (/ prod 1000))))
2888 a)) 2804 a))
2889 (and (> c 0) 2805 (and (> c 0)
2890 (list c))) 2806 (list c))))
2891 )
2892 2807
2893 2808
2894 ;;; Compute the integer (quotient . remainder) of A and B, which may be 2809 ;;; Compute the integer (quotient . remainder) of A and B, which may be
2895 ;;; small or big integers. Type and consistency of truncation is undefined 2810 ;;; small or big integers. Type and consistency of truncation is undefined
2896 ;;; if A or B is negative. B must be nonzero. [I.I I I] [Public] 2811 ;;; if A or B is negative. B must be nonzero. [I.I I I] [Public]
2908 (let ((res (math-div-bignum (cdr a) (cdr b)))) 2823 (let ((res (math-div-bignum (cdr a) (cdr b))))
2909 (cons 2824 (cons
2910 (math-normalize (cons (if (eq (car a) (car b)) 'bigpos 'bigneg) 2825 (math-normalize (cons (if (eq (car a) (car b)) 'bigpos 'bigneg)
2911 (car res))) 2826 (car res)))
2912 (math-normalize (cons (car a) (cdr res)))))) 2827 (math-normalize (cons (car a) (cdr res))))))
2913 (cons (/ a b) (% a b))) 2828 (cons (/ a b) (% a b))))
2914 )
2915 2829
2916 (defun math-quotient (a b) ; [I I I] [Public] 2830 (defun math-quotient (a b) ; [I I I] [Public]
2917 (if (and (not (consp a)) (not (consp b))) 2831 (if (and (not (consp a)) (not (consp b)))
2918 (if (= b 0) 2832 (if (= b 0)
2919 (math-reject-arg a "*Division by zero") 2833 (math-reject-arg a "*Division by zero")
2930 (d (/ 1000 (1+ (nth (1- blen) (cdr b))))) 2844 (d (/ 1000 (1+ (nth (1- blen) (cdr b)))))
2931 (res (math-div-bignum-big (math-mul-bignum-digit (cdr a) d 0) 2845 (res (math-div-bignum-big (math-mul-bignum-digit (cdr a) d 0)
2932 (math-mul-bignum-digit (cdr b) d 0) 2846 (math-mul-bignum-digit (cdr b) d 0)
2933 alen blen))) 2847 alen blen)))
2934 (math-normalize (cons (if (eq (car a) (car b)) 'bigpos 'bigneg) 2848 (math-normalize (cons (if (eq (car a) (car b)) 'bigpos 'bigneg)
2935 (car res)))))) 2849 (car res)))))))
2936 )
2937 2850
2938 2851
2939 ;;; Divide a bignum digit list by another. [l.l l L] 2852 ;;; Divide a bignum digit list by another. [l.l l L]
2940 ;;; The following division algorithm is borrowed from Knuth vol. II, sec. 4.3.1 2853 ;;; The following division algorithm is borrowed from Knuth vol. II, sec. 4.3.1
2941 (defun math-div-bignum (a b) 2854 (defun math-div-bignum (a b)
2949 (if (= d 1) 2862 (if (= d 1)
2950 res 2863 res
2951 (cons (car res) 2864 (cons (car res)
2952 (car (math-div-bignum-digit (cdr res) d))))) 2865 (car (math-div-bignum-digit (cdr res) d)))))
2953 (let ((res (math-div-bignum-digit a (car b)))) 2866 (let ((res (math-div-bignum-digit a (car b))))
2954 (cons (car res) (list (cdr res))))) 2867 (cons (car res) (list (cdr res))))))
2955 )
2956 2868
2957 ;;; Divide a bignum digit list by a digit. [l.D l D] 2869 ;;; Divide a bignum digit list by a digit. [l.D l D]
2958 (defun math-div-bignum-digit (a b) 2870 (defun math-div-bignum-digit (a b)
2959 (if a 2871 (if a
2960 (let* ((res (math-div-bignum-digit (cdr a) b)) 2872 (let* ((res (math-div-bignum-digit (cdr a) b))
2961 (num (+ (* (cdr res) 1000) (car a)))) 2873 (num (+ (* (cdr res) 1000) (car a))))
2962 (cons 2874 (cons
2963 (cons (/ num b) (car res)) 2875 (cons (/ num b) (car res))
2964 (% num b))) 2876 (% num b)))
2965 '(nil . 0)) 2877 '(nil . 0)))
2966 )
2967 2878
2968 (defun math-div-bignum-big (a b alen blen) ; [l.l l L] 2879 (defun math-div-bignum-big (a b alen blen) ; [l.l l L]
2969 (if (< alen blen) 2880 (if (< alen blen)
2970 (cons nil a) 2881 (cons nil a)
2971 (let* ((res (math-div-bignum-big (cdr a) b (1- alen) blen)) 2882 (let* ((res (math-div-bignum-big (cdr a) b (1- alen) blen))
2972 (num (cons (car a) (cdr res))) 2883 (num (cons (car a) (cdr res)))
2973 (res2 (math-div-bignum-part num b blen))) 2884 (res2 (math-div-bignum-part num b blen)))
2974 (cons 2885 (cons
2975 (cons (car res2) (car res)) 2886 (cons (car res2) (car res))
2976 (cdr res2)))) 2887 (cdr res2)))))
2977 )
2978 2888
2979 (defun math-div-bignum-part (a b blen) ; a < b*1000 [D.l l L] 2889 (defun math-div-bignum-part (a b blen) ; a < b*1000 [D.l l L]
2980 (let* ((num (+ (* (or (nth blen a) 0) 1000) (or (nth (1- blen) a) 0))) 2890 (let* ((num (+ (* (or (nth blen a) 0) 1000) (or (nth (1- blen) a) 0)))
2981 (den (nth (1- blen) b)) 2891 (den (nth (1- blen) b))
2982 (guess (min (/ num den) 999))) 2892 (guess (min (/ num den) 999)))
2983 (math-div-bignum-try a b (math-mul-bignum-digit b guess 0) guess)) 2893 (math-div-bignum-try a b (math-mul-bignum-digit b guess 0) guess)))
2984 )
2985 2894
2986 (defun math-div-bignum-try (a b c guess) ; [D.l l l D] 2895 (defun math-div-bignum-try (a b c guess) ; [D.l l l D]
2987 (let ((rem (math-sub-bignum a c))) 2896 (let ((rem (math-sub-bignum a c)))
2988 (if (eq rem 'neg) 2897 (if (eq rem 'neg)
2989 (math-div-bignum-try a b (math-sub-bignum c b) (1- guess)) 2898 (math-div-bignum-try a b (math-sub-bignum c b) (1- guess))
2990 (cons guess rem))) 2899 (cons guess rem))))
2991 )
2992 2900
2993 2901
2994 ;;; Compute the quotient of A and B. [O O N] [Public] 2902 ;;; Compute the quotient of A and B. [O O N] [Public]
2995 (defun math-div (a b) 2903 (defun math-div (a b)
2996 (or 2904 (or
3025 (setq b (math-float b))) 2933 (setq b (math-float b)))
3026 (math-div-float a b))) 2934 (math-div-float a b)))
3027 (and (calc-extensions) 2935 (and (calc-extensions)
3028 (math-div-objects-fancy a b)))) 2936 (math-div-objects-fancy a b))))
3029 (and (calc-extensions) 2937 (and (calc-extensions)
3030 (math-div-symb-fancy a b))) 2938 (math-div-symb-fancy a b))))
3031 )
3032 2939
3033 (defun math-div-float (a b) ; [F F F] 2940 (defun math-div-float (a b) ; [F F F]
3034 (let ((ldiff (max (- (1+ calc-internal-prec) 2941 (let ((ldiff (max (- (1+ calc-internal-prec)
3035 (- (math-numdigs (nth 1 a)) (math-numdigs (nth 1 b)))) 2942 (- (math-numdigs (nth 1 a)) (math-numdigs (nth 1 b))))
3036 0))) 2943 0)))
3037 (math-make-float (math-quotient (math-scale-int (nth 1 a) ldiff) (nth 1 b)) 2944 (math-make-float (math-quotient (math-scale-int (nth 1 a) ldiff) (nth 1 b))
3038 (- (- (nth 2 a) (nth 2 b)) ldiff))) 2945 (- (- (nth 2 a) (nth 2 b)) ldiff))))
3039 )
3040 2946
3041 2947
3042 2948
3043 2949
3044 2950
3098 (if calc-line-numbering 3004 (if calc-line-numbering
3099 (progn 3005 (progn
3100 (aset s 0 ?1) 3006 (aset s 0 ?1)
3101 (aset s 1 ?:)))) 3007 (aset s 1 ?:))))
3102 (setcar (cdr entry) (calc-count-lines s)) 3008 (setcar (cdr entry) (calc-count-lines s))
3103 s) 3009 s))
3104 )
3105 3010
3106 (defun math-stack-value-offset (c) 3011 (defun math-stack-value-offset (c)
3107 (let* ((num (if calc-line-numbering 4 0)) 3012 (let* ((num (if calc-line-numbering 4 0))
3108 (wid (calc-window-width)) 3013 (wid (calc-window-width))
3109 off) 3014 off)
3113 (math-stack-value-offset-fancy)) 3018 (math-stack-value-offset-fancy))
3114 (setq off (or calc-display-origin 0)) 3019 (setq off (or calc-display-origin 0))
3115 (if (integerp calc-line-breaking) 3020 (if (integerp calc-line-breaking)
3116 (setq wid calc-line-breaking))) 3021 (setq wid calc-line-breaking)))
3117 (cons (max (- off (length calc-left-label)) 0) 3022 (cons (max (- off (length calc-left-label)) 0)
3118 (+ wid num))) 3023 (+ wid num))))
3119 )
3120 3024
3121 (defun calc-count-lines (s) 3025 (defun calc-count-lines (s)
3122 (let ((pos 0) 3026 (let ((pos 0)
3123 (num 1)) 3027 (num 1))
3124 (while (setq newpos (string-match "\n" s pos)) 3028 (while (setq newpos (string-match "\n" s pos))
3125 (setq pos (1+ newpos) 3029 (setq pos (1+ newpos)
3126 num (1+ num))) 3030 num (1+ num)))
3127 num) 3031 num))
3128 )
3129 3032
3130 (defun math-format-value (a &optional w) 3033 (defun math-format-value (a &optional w)
3131 (if (and (Math-scalarp a) 3034 (if (and (Math-scalarp a)
3132 (memq calc-language '(nil flat unform))) 3035 (memq calc-language '(nil flat unform)))
3133 (math-format-number a) 3036 (math-format-number a)
3134 (calc-extensions) 3037 (calc-extensions)
3135 (let ((calc-line-breaking nil)) 3038 (let ((calc-line-breaking nil))
3136 (math-composition-to-string (math-compose-expr a 0) w))) 3039 (math-composition-to-string (math-compose-expr a 0) w))))
3137 )
3138 3040
3139 (defun calc-window-width () 3041 (defun calc-window-width ()
3140 (if calc-embedded-info 3042 (if calc-embedded-info
3141 (let ((win (get-buffer-window (aref calc-embedded-info 0)))) 3043 (let ((win (get-buffer-window (aref calc-embedded-info 0))))
3142 (1- (if win (window-width win) (frame-width)))) 3044 (1- (if win (window-width win) (frame-width))))
3143 (- (window-width (get-buffer-window (current-buffer))) 3045 (- (window-width (get-buffer-window (current-buffer)))
3144 (if calc-line-numbering 5 1))) 3046 (if calc-line-numbering 5 1))))
3145 )
3146 3047
3147 (defun math-comp-concat (c1 c2) 3048 (defun math-comp-concat (c1 c2)
3148 (if (and (stringp c1) (stringp c2)) 3049 (if (and (stringp c1) (stringp c2))
3149 (concat c1 c2) 3050 (concat c1 c2)
3150 (list 'horiz c1 c2)) 3051 (list 'horiz c1 c2)))
3151 )
3152 3052
3153 3053
3154 3054
3155 ;;; Format an expression as a one-line string suitable for re-reading. 3055 ;;; Format an expression as a one-line string suitable for re-reading.
3156 3056
3169 (calc-hms-format "%s@ %s' %s\"") 3069 (calc-hms-format "%s@ %s' %s\"")
3170 (calc-language nil)) 3070 (calc-language nil))
3171 (math-format-number a))) 3071 (math-format-number a)))
3172 (t 3072 (t
3173 (calc-extensions) 3073 (calc-extensions)
3174 (math-format-flat-expr-fancy a prec))) 3074 (math-format-flat-expr-fancy a prec))))
3175 )
3176 3075
3177 3076
3178 3077
3179 ;;; Format a number as a string. 3078 ;;; Format a number as a string.
3180 (defun math-format-number (a &optional prec) ; [X N] [Public] 3079 (defun math-format-number (a &optional prec) ; [X N] [Public]
3280 "%se%d") 3179 "%se%d")
3281 str (- eadj scale))))))) 3180 str (- eadj scale)))))))
3282 str))) 3181 str)))
3283 (t 3182 (t
3284 (calc-extensions) 3183 (calc-extensions)
3285 (math-format-number-fancy a prec))) 3184 (math-format-number-fancy a prec))))
3286 )
3287 3185
3288 (defun math-format-bignum (a) ; [X L] 3186 (defun math-format-bignum (a) ; [X L]
3289 (if (and (= calc-number-radix 10) 3187 (if (and (= calc-number-radix 10)
3290 (not calc-leading-zeros) 3188 (not calc-leading-zeros)
3291 (not calc-group-digits)) 3189 (not calc-group-digits))
3292 (math-format-bignum-decimal a) 3190 (math-format-bignum-decimal a)
3293 (calc-extensions) 3191 (calc-extensions)
3294 (math-format-bignum-fancy a)) 3192 (math-format-bignum-fancy a)))
3295 )
3296 3193
3297 (defun math-format-bignum-decimal (a) ; [X L] 3194 (defun math-format-bignum-decimal (a) ; [X L]
3298 (if a 3195 (if a
3299 (let ((s "")) 3196 (let ((s ""))
3300 (while (cdr (cdr a)) 3197 (while (cdr (cdr a))
3301 (setq s (concat (format "%06d" (+ (* (nth 1 a) 1000) (car a))) s) 3198 (setq s (concat (format "%06d" (+ (* (nth 1 a) 1000) (car a))) s)
3302 a (cdr (cdr a)))) 3199 a (cdr (cdr a))))
3303 (concat (int-to-string (+ (* (or (nth 1 a) 0) 1000) (car a))) s)) 3200 (concat (int-to-string (+ (* (or (nth 1 a) 0) 1000) (car a))) s))
3304 "0") 3201 "0"))
3305 )
3306 3202
3307 3203
3308 3204
3309 ;;; Parse a simple number in string form. [N X] [Public] 3205 ;;; Parse a simple number in string form. [N X] [Public]
3310 (defun math-read-number (s) 3206 (defun math-read-number (s)
3360 (and mant exp (Math-realp mant) (> exp -4000000) (< exp 4000000) 3256 (and mant exp (Math-realp mant) (> exp -4000000) (< exp 4000000)
3361 (let ((mant (math-float mant))) 3257 (let ((mant (math-float mant)))
3362 (list 'float (nth 1 mant) (+ (nth 2 mant) exp))))))) 3258 (list 'float (nth 1 mant) (+ (nth 2 mant) exp)))))))
3363 3259
3364 ;; Syntax error! 3260 ;; Syntax error!
3365 (t nil))) 3261 (t nil))))
3366 )
3367 3262
3368 (defun math-match-substring (s n) 3263 (defun math-match-substring (s n)
3369 (if (match-beginning n) 3264 (if (match-beginning n)
3370 (substring s (match-beginning n) (match-end n)) 3265 (substring s (match-beginning n) (match-end n))
3371 "") 3266 ""))
3372 )
3373 3267
3374 (defun math-read-bignum (s) ; [l X] 3268 (defun math-read-bignum (s) ; [l X]
3375 (if (> (length s) 3) 3269 (if (> (length s) 3)
3376 (cons (string-to-int (substring s -3)) 3270 (cons (string-to-int (substring s -3))
3377 (math-read-bignum (substring s 0 -3))) 3271 (math-read-bignum (substring s 0 -3)))
3378 (list (string-to-int s))) 3272 (list (string-to-int s))))
3379 )
3380 3273
3381 3274
3382 (defconst math-tex-ignore-words 3275 (defconst math-tex-ignore-words
3383 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right") 3276 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right")
3384 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ") 3277 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ")
3447 ;;;###autoload 3340 ;;;###autoload
3448 (defun calc-grab-region (top bot arg) 3341 (defun calc-grab-region (top bot arg)
3449 "Parse the region as a vector of numbers and push it on the Calculator stack." 3342 "Parse the region as a vector of numbers and push it on the Calculator stack."
3450 (interactive "r\nP") 3343 (interactive "r\nP")
3451 (calc-extensions) 3344 (calc-extensions)
3452 (calc-do-grab-region top bot arg) 3345 (calc-do-grab-region top bot arg))
3453 )
3454 3346
3455 ;;;###autoload 3347 ;;;###autoload
3456 (defun calc-grab-rectangle (top bot arg) 3348 (defun calc-grab-rectangle (top bot arg)
3457 "Parse a rectangle as a matrix of numbers and push it on the Calculator stack." 3349 "Parse a rectangle as a matrix of numbers and push it on the Calculator stack."
3458 (interactive "r\nP") 3350 (interactive "r\nP")
3459 (calc-extensions) 3351 (calc-extensions)
3460 (calc-do-grab-rectangle top bot arg) 3352 (calc-do-grab-rectangle top bot arg))
3461 )
3462 3353
3463 (defun calc-grab-sum-down (top bot arg) 3354 (defun calc-grab-sum-down (top bot arg)
3464 "Parse a rectangle as a matrix of numbers and sum its columns." 3355 "Parse a rectangle as a matrix of numbers and sum its columns."
3465 (interactive "r\nP") 3356 (interactive "r\nP")
3466 (calc-extensions) 3357 (calc-extensions)
3467 (calc-do-grab-rectangle top bot arg 'calcFunc-reduced) 3358 (calc-do-grab-rectangle top bot arg 'calcFunc-reduced))
3468 )
3469 3359
3470 (defun calc-grab-sum-across (top bot arg) 3360 (defun calc-grab-sum-across (top bot arg)
3471 "Parse a rectangle as a matrix of numbers and sum its rows." 3361 "Parse a rectangle as a matrix of numbers and sum its rows."
3472 (interactive "r\nP") 3362 (interactive "r\nP")
3473 (calc-extensions) 3363 (calc-extensions)
3474 (calc-do-grab-rectangle top bot arg 'calcFunc-reducea) 3364 (calc-do-grab-rectangle top bot arg 'calcFunc-reducea))
3475 )
3476 3365
3477 3366
3478 ;;;###autoload 3367 ;;;###autoload
3479 (defun calc-embedded (arg &optional end obeg oend) 3368 (defun calc-embedded (arg &optional end obeg oend)
3480 "Start Calc Embedded mode on the formula surrounding point." 3369 "Start Calc Embedded mode on the formula surrounding point."
3481 (interactive "P") 3370 (interactive "P")
3482 (calc-extensions) 3371 (calc-extensions)
3483 (calc-do-embedded arg end obeg oend) 3372 (calc-do-embedded arg end obeg oend))
3484 )
3485 3373
3486 ;;;###autoload 3374 ;;;###autoload
3487 (defun calc-embedded-activate (&optional arg cbuf) 3375 (defun calc-embedded-activate (&optional arg cbuf)
3488 "Scan the current editing buffer for all embedded := and => formulas. 3376 "Scan the current editing buffer for all embedded := and => formulas.
3489 Also looks for the equivalent TeX words, \\gets and \\evalto." 3377 Also looks for the equivalent TeX words, \\gets and \\evalto."
3490 (interactive "P") 3378 (interactive "P")
3491 (calc-do-embedded-activate arg cbuf) 3379 (calc-do-embedded-activate arg cbuf))
3492 )
3493 3380
3494 3381
3495 (defun calc-user-invocation () 3382 (defun calc-user-invocation ()
3496 (interactive) 3383 (interactive)
3497 (or (stringp calc-invocation-macro) 3384 (or (stringp calc-invocation-macro)
3498 (error "Use `Z I' inside Calc to define a `M-# Z' keyboard macro")) 3385 (error "Use `Z I' inside Calc to define a `M-# Z' keyboard macro"))
3499 (execute-kbd-macro calc-invocation-macro nil) 3386 (execute-kbd-macro calc-invocation-macro nil))
3500 )
3501 3387
3502 3388
3503 3389
3504 3390
3505 ;;; User-programmability. 3391 ;;; User-programmability.
3506 3392
3507 ;;;###autoload 3393 ;;;###autoload
3508 (defmacro defmath (func args &rest body) ; [Public] 3394 (defmacro defmath (func args &rest body) ; [Public]
3509 (calc-extensions) 3395 (calc-extensions)
3510 (math-do-defmath func args body) 3396 (math-do-defmath func args body))
3511 )
3512 3397
3513 3398
3514 ;;; Functions needed for Lucid Emacs support. 3399 ;;; Functions needed for Lucid Emacs support.
3515 3400
3516 (defun calc-read-key (&optional optkey) 3401 (defun calc-read-key (&optional optkey)
3522 (calc-emacs-type-gnu19 3407 (calc-emacs-type-gnu19
3523 (let ((key (read-event))) 3408 (let ((key (read-event)))
3524 (cons key key))) 3409 (cons key key)))
3525 (t 3410 (t
3526 (let ((key (read-char))) 3411 (let ((key (read-char)))
3527 (cons key key)))) 3412 (cons key key)))))
3528 )
3529 3413
3530 (defun calc-unread-command (&optional input) 3414 (defun calc-unread-command (&optional input)
3531 (if (featurep 'xemacs) 3415 (if (featurep 'xemacs)
3532 (setq unread-command-event 3416 (setq unread-command-event
3533 (if (integerp input) (character-to-event input) 3417 (if (integerp input) (character-to-event input)
3540 (setq unread-command-events nil))) 3424 (setq unread-command-events nil)))
3541 3425
3542 (if calc-always-load-extensions 3426 (if calc-always-load-extensions
3543 (progn 3427 (progn
3544 (calc-extensions) 3428 (calc-extensions)
3545 (calc-load-everything)) 3429 (calc-load-everything)))
3546 )
3547 3430
3548 3431
3549 (run-hooks 'calc-load-hook) 3432 (run-hooks 'calc-load-hook)
3550 3433
3551 3434 ;;; calc.el ends here