]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/calc: Fix a few issues introduced by lexical scoping
authorStefan Monnier <monnier@iro.umontreal.ca>
Wed, 14 Oct 2020 22:03:52 +0000 (18:03 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Wed, 14 Oct 2020 22:03:52 +0000 (18:03 -0400)
Fix a few places I missed, where we incorrectly used lexical scoping on a var
that needed dynamic scoping.
These were detected thanks to a bit of footwork by Mattias EngdegĂ„rd!

* lisp/calc/calc-ext.el (math-read-big-lines): Declare as dynbound.
(math-read-big-bigp): Bind it inside a `let`.
* lisp/calc/calc-graph.el (math-arglist): Declare as dynbound.
* lisp/calc/calc-map.el (math-arglist): Declare as dynbound.
* lisp/calc/calc-misc.el (math-trunc-prec): Declare as dynbound.
(math-trunc): Bind it inside a `let`.
(math-floor-prec): Declare as dynbound.
(math-floor): Bind it inside a `let`.
* lisp/calc/calc-nlfit.el (calc-curve-varnames, calc-curve-coefnames):
Declare as dynbound.
* lisp/calc/calc-sel.el (math-comp-sel-tag): Declare as dynbound.
* lisp/calc/calcsel2.el (calc-sel-reselect): Declare as dynbound.

lisp/calc/calc-ext.el
lisp/calc/calc-graph.el
lisp/calc/calc-map.el
lisp/calc/calc-misc.el
lisp/calc/calc-nlfit.el
lisp/calc/calc-sel.el
lisp/calc/calcalg2.el
lisp/calc/calcsel2.el

index fc0a2c88fea6735c333fa18cff9e861fcfd52012..c48d159582205c6c18efdcadd4814baf22611728 100644 (file)
@@ -3094,6 +3094,7 @@ If X is not an error form, return 1."
 (defvar math-read-big-baseline)
 (defvar math-read-big-h2)
 (defvar math-read-big-err-msg)
+(defvar math-read-big-lines)
 
 (defun math-read-big-expr (str)
   (and (> (length calc-left-label) 0)
@@ -3138,41 +3139,42 @@ If X is not an error form, return 1."
 
 (defvar math-rb-h2)
 
-(defun math-read-big-bigp (math-read-big-lines)
-  (and (cdr math-read-big-lines)
-       (let ((matrix nil)
-            (v 0)
-            (height (if (> (length (car math-read-big-lines)) 0) 1 0)))
-        (while (and (cdr math-read-big-lines)
-                    (let* ((i 0)
-                           j
-                           (l1 (car math-read-big-lines))
-                           (l2 (nth 1 math-read-big-lines))
-                           (len (min (length l1) (length l2))))
-                      (if (> (length l2) 0)
-                          (setq height (1+ height)))
-                      (while (and (< i len)
-                                  (or (memq (aref l1 i) '(?\  ?\- ?\_))
-                                      (memq (aref l2 i) '(?\  ?\-))
-                                      (and (memq (aref l1 i) '(?\| ?\,))
-                                           (= (aref l2 i) (aref l1 i)))
-                                      (and (eq (aref l1 i) ?\[)
-                                           (eq (aref l2 i) ?\[)
-                                           (let ((math-rb-h2 (length l1)))
-                                             (setq j (math-read-big-balance
-                                                      (1+ i) v "[")))
-                                           (setq i (1- j)))))
-                        (setq i (1+ i)))
-                      (or (= i len)
-                          (and (eq (aref l1 i) ?\[)
-                               (eq (aref l2 i) ?\[)
-                               (setq matrix t)
-                               nil))))
-          (setq math-read-big-lines (cdr math-read-big-lines)
-                v (1+ v)))
-        (or (and (> height 1)
-                 (not (cdr math-read-big-lines)))
-            matrix))))
+(defun math-read-big-bigp (read-big-lines)
+  (when (cdr read-big-lines)
+    (let ((math-read-big-lines read-big-lines)
+         (matrix nil)
+         (v 0)
+         (height (if (> (length (car read-big-lines)) 0) 1 0)))
+      (while (and (cdr math-read-big-lines)
+                 (let* ((i 0)
+                        j
+                        (l1 (car math-read-big-lines))
+                        (l2 (nth 1 math-read-big-lines))
+                        (len (min (length l1) (length l2))))
+                   (if (> (length l2) 0)
+                       (setq height (1+ height)))
+                   (while (and (< i len)
+                               (or (memq (aref l1 i) '(?\  ?\- ?\_))
+                                   (memq (aref l2 i) '(?\  ?\-))
+                                   (and (memq (aref l1 i) '(?\| ?\,))
+                                        (= (aref l2 i) (aref l1 i)))
+                                   (and (eq (aref l1 i) ?\[)
+                                        (eq (aref l2 i) ?\[)
+                                        (let ((math-rb-h2 (length l1)))
+                                          (setq j (math-read-big-balance
+                                                   (1+ i) v "[")))
+                                        (setq i (1- j)))))
+                     (setq i (1+ i)))
+                   (or (= i len)
+                       (and (eq (aref l1 i) ?\[)
+                            (eq (aref l2 i) ?\[)
+                            (setq matrix t)
+                            nil))))
+       (setq math-read-big-lines (cdr math-read-big-lines)
+             v (1+ v)))
+      (or (and (> height 1)
+              (not (cdr math-read-big-lines)))
+         matrix))))
 
 ;;; Nontrivial "flat" formatting.
 
index 82e93357164d429bb31dc61b1323a44cc3272489..829fa44ca4f1d5afcdab025e8c42074cd593956b 100644 (file)
 (defvar calc-graph-blank)
 (defvar calc-graph-non-blank)
 (defvar calc-graph-curve-num)
+(defvar math-arglist)
 
 (defun calc-graph-plot (flag &optional printing)
   (interactive "P")
index 57483fc6590293ff9cc43ae1067fa162683239ca..0ee8282692724c83ed9f873205c5a2194cdfb671 100644 (file)
 (defvar calc-get-operator-history nil
   "History for calc-get-operator.")
 
+(defvar math-arglist)
+
 (defun calc-get-operator (msg &optional nargs)
   (setq calc-aborted-prefix nil)
   (let ((inv nil) (hyp nil) (prefix nil) (forcenargs nil)
index 7c97dc6a9a08032e882209386ff24046ef37e6ef..2db09e2b677e5ece08ca187434475c0465a85099 100644 (file)
@@ -757,19 +757,21 @@ loaded and the keystroke automatically re-typed."
 
 ;; The variable math-trunc-prec is local to math-trunc, but used by
 ;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
+(defvar math-trunc-prec)
 
 ;;;###autoload
-(defun math-trunc (a &optional math-trunc-prec)
-  (cond (math-trunc-prec
+(defun math-trunc (a &optional trunc-prec)
+  (cond (trunc-prec
         (require 'calc-ext)
-        (math-trunc-special a math-trunc-prec))
+        (math-trunc-special a trunc-prec))
        ((Math-integerp a) a)
        ((Math-looks-negp a)
         (math-neg (math-trunc (math-neg a))))
        ((eq (car a) 'float)
         (math-scale-int (nth 1 a) (nth 2 a)))
        (t (require 'calc-ext)
-          (math-trunc-fancy a))))
+          (let ((math-trunc-prec trunc-prec))
+            (math-trunc-fancy a)))))
 ;;;###autoload
 (defalias 'calcFunc-trunc 'math-trunc)
 
@@ -777,12 +779,13 @@ loaded and the keystroke automatically re-typed."
 
 ;; The variable math-floor-prec is local to math-floor, but used by
 ;; math-floor-fancy in calc-arith.el, which is called by math-floor.
+(defvar math-floor-prec)
 
 ;;;###autoload
-(defun math-floor (a &optional math-floor-prec)    ;  [Public]
-  (cond (math-floor-prec
+(defun math-floor (a &optional floor-prec)    ;  [Public]
+  (cond (floor-prec
         (require 'calc-ext)
-        (math-floor-special a math-floor-prec))
+        (math-floor-special a floor-prec))
        ((Math-integerp a) a)
        ((Math-messy-integerp a) (math-trunc a))
        ((Math-realp a)
@@ -790,7 +793,9 @@ loaded and the keystroke automatically re-typed."
             (math-add (math-trunc a) -1)
           (math-trunc a)))
        (t (require 'calc-ext)
-          (math-floor-fancy a))))
+          (let ((math-floor-prec floor-prec))
+            (math-floor-fancy a)))))
+
 ;;;###autoload
 (defalias 'calcFunc-floor 'math-floor)
 
index 6f2a601cd9983dbecc1b00acb8d6c10b35ae7441..5ed85fe7cae7eb7a1446cadaded251e0740c83c2 100644 (file)
   (calc-handle-whys))
 
 (defvar calc-curve-nvars)
+(defvar calc-curve-varnames)
+(defvar calc-curve-coefnames)
 
 (defun math-nlfit-fit-curve (fn grad solnexpr initparms &optional sdv)
   (calc-slow-wrapper
index d2944488870357c17687b4685a9440536ae68b30..23c0e01b52702433ed841acae310013b3d3385bf 100644 (file)
 ;; The variable math-comp-sel-tag is local to calc-find-selected-part,
 ;; but is used by math-comp-sel-flat-term and math-comp-add-string-sel
 ;; in calccomp.el, which are called (indirectly) by calc-find-selected-part.
+(defvar math-comp-sel-tag)
 
 (defun calc-find-selected-part ()
   (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset))
                                                   (current-indentation))
                                         lcount (1+ lcount)))
                                 (- lcount (math-comp-ascent
-                                           calc-selection-cache-comp) -1))))
+                                           calc-selection-cache-comp)
+                                   -1))))
         (math-comp-sel-cpos (- (point) toppt calc-selection-cache-offset
                                spaces lcount))
         (math-comp-sel-tag nil))
index 99d0549ca88c1b626eda6332cd7639eaf5cf2ef8..7894bd93015a8a530059f26debe76a756628dab7 100644 (file)
       (let ((math-solve-simplifying t))
        (math-solve-system-rec exprs math-solve-vars nil)))))
 
-;;; The following backtracking solver works by choosing a variable
-;;; and equation, and trying to solve the equation for the variable.
-;;; If it succeeds it calls itself recursively with that variable and
-;;; equation removed from their respective lists, and with the solution
-;;; added to solns as well as being substituted into all existing
-;;; equations.  The algorithm terminates when any solution path
-;;; manages to remove all the variables from var-list.
-
-;;; To support calcFunc-roots, entries in eqn-list and solns are
-;;; actually lists of equations.
+;; The following backtracking solver works by choosing a variable
+;; and equation, and trying to solve the equation for the variable.
+;; If it succeeds it calls itself recursively with that variable and
+;; equation removed from their respective lists, and with the solution
+;; added to solns as well as being substituted into all existing
+;; equations.  The algorithm terminates when any solution path
+;; manages to remove all the variables from `var-list'.
+
+;; To support calcFunc-roots, entries in eqn-list and solns are
+;; actually lists of equations.
 
 ;; The variables math-solve-system-res and math-solve-system-vv are
 ;; local to math-solve-system-rec, but are used by math-solve-system-subst.
index faec2309394fe16364ebc39668617a617ca9ff08..d6842aa7eeea20bc78deaa32936476a02f2cf92e 100644 (file)
@@ -34,6 +34,7 @@
 ;; The variable calc-sel-reselect is local to the methods below,
 ;; but is used by some functions in calc-sel.el which are called
 ;; by the functions below.
+(defvar calc-sel-reselect)
 
 (defun calc-commute-left (arg)
   (interactive "p")