]> code.delx.au - gnu-emacs/blobdiff - lisp/calc/calc-sel.el
Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-13
[gnu-emacs] / lisp / calc / calc-sel.el
index 16e7fe9cc90caff97ec634f0a91cd4adcc2a472e..bf18fa968c512ffb7df1fd9ecbd11db4c4fa2fea 100644 (file)
@@ -1,6 +1,10 @@
-;; Calculator for GNU Emacs, part II [calc-sel.el]
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
-;; Written by Dave Gillespie, daveg@synaptics.com.
+;;; calc-sel.el --- data selection functions for Calc
+
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
+;;   2005, 2006 Free Software Foundation, Inc.
+
+;; Author: David Gillespie <daveg@synaptics.com>
+;; Maintainer: Jay Belanger <belanger@truman.edu>
 
 ;; This file is part of GNU Emacs.
 
 ;; file named COPYING.  Among other things, the copyright notice
 ;; and this notice must be preserved on all copies.
 
+;;; Commentary:
 
+;;; Code:
 
 ;; This file is autoloaded from calc-ext.el.
-(require 'calc-ext)
 
+(require 'calc-ext)
 (require 'calc-macs)
 
-(defun calc-Need-calc-sel () nil)
+;;; Selection commands.
 
+(defvar calc-keep-selection t)
 
-;;; Selection commands.
+(defvar calc-selection-cache-entry nil)
+(defvar calc-selection-cache-num)
+(defvar calc-selection-cache-comp)
+(defvar calc-selection-cache-offset)
+(defvar calc-selection-true-num)
 
 (defun calc-select-here (num &optional once keep)
   (interactive "P")
@@ -42,7 +53,7 @@
           (if once (progn
                      (setq calc-keep-selection nil)
                      (message "(Selection will apply to next command only)")))
-          (calc-change-current-selection 
+          (calc-change-current-selection
            (if found
                (if (and num (> (setq num (prefix-numeric-value num)) 0))
                    (progn
                                     (car entry) found)))
                      found)
                  (calc-grow-assoc-formula (car entry) found))
-             (car entry)))))))
-)
+             (car entry))))))))
 
 (defun calc-select-once (num)
   (interactive "P")
-  (calc-select-here num t)
-)
+  (calc-select-here num t))
 
 (defun calc-select-here-maybe (num)
   (interactive "P")
-  (calc-select-here num nil t)
-)
+  (calc-select-here num nil t))
 
 (defun calc-select-once-maybe (num)
   (interactive "P")
-  (calc-select-here num t t)
-)
+  (calc-select-here num t t))
 
 (defun calc-select-additional ()
   (interactive)
@@ -88,8 +95,7 @@
                               (car entry) sel)))
                  sel)
              (calc-grow-assoc-formula (car entry) found)))
-       (car entry)))))
-)
+       (car entry))))))
 
 (defun calc-select-more (num)
   (interactive "P")
                       (>= (setq num (1- (prefix-numeric-value num))) 0))
             (setq sel (calc-find-assoc-parent-formula (car entry) sel)))
           (calc-change-current-selection sel))
-       (calc-select-here num))))
-)
+       (calc-select-here num)))))
 
 (defun calc-select-less (num)
   (interactive "p")
    (calc-prepare-selection)
    (let ((found (calc-find-selected-part))
         (entry calc-selection-cache-entry))
-     (calc-change-current-selection 
+     (calc-change-current-selection
       (and found
           (let ((sel (nth 2 entry))
                 old index op)
                    (setq op (assq (car-safe sel) calc-assoc-ops))
                    (memq (car old) (nth index op))
                    (setq num (1+ num))))
-            sel)))))
-)
+            sel))))))
 
 (defun calc-select-part (num)
   (interactive "P")
                                  num)))
      (if sel
         (calc-change-current-selection sel)
-       (error "%d is not a valid sub-formula index" num))))
-)
+       (error "%d is not a valid sub-formula index" num)))))
+
+;; The variables calc-fnp-op and calc-fnp-num are local to 
+;; calc-find-nth-part (and calc-select-previous) but used by 
+;; calc-find-nth-part-rec, which is called by them.
+(defvar calc-fnp-op)
+(defvar calc-fnp-num)
 
-(defun calc-find-nth-part (expr num)
+(defun calc-find-nth-part (expr calc-fnp-num)
   (if (and calc-assoc-selections
           (assq (car-safe expr) calc-assoc-ops))
-      (let (op)
+      (let (calc-fnp-op)
        (calc-find-nth-part-rec expr))
     (if (eq (car-safe expr) 'intv)
-       (and (>= num 1) (<= num 2) (nth (1+ num) expr))
-      (and (not (Math-primp expr)) (>= num 1) (< num (length expr))
-          (nth num expr))))
-)
+       (and (>= calc-fnp-num 1) (<= calc-fnp-num 2) (nth (1+ calc-fnp-num) expr))
+      (and (not (Math-primp expr)) (>= calc-fnp-num 1) (< calc-fnp-num (length expr))
+          (nth calc-fnp-num expr)))))
 
 (defun calc-find-nth-part-rec (expr)   ; uses num, op
-  (or (if (and (setq op (assq (car-safe (nth 1 expr)) calc-assoc-ops))
-              (memq (car expr) (nth 1 op)))
+  (or (if (and (setq calc-fnp-op (assq (car-safe (nth 1 expr)) calc-assoc-ops))
+              (memq (car expr) (nth 1 calc-fnp-op)))
          (calc-find-nth-part-rec (nth 1 expr))
-       (and (= (setq num (1- num)) 0)
+       (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
             (nth 1 expr)))
-      (if (and (setq op (assq (car-safe (nth 2 expr)) calc-assoc-ops))
-              (memq (car expr) (nth 2 op)))
+      (if (and (setq calc-fnp-op (assq (car-safe (nth 2 expr)) calc-assoc-ops))
+              (memq (car expr) (nth 2 calc-fnp-op)))
          (calc-find-nth-part-rec (nth 2 expr))
-       (and (= (setq num (1- num)) 0)
-            (nth 2 expr))))
-)
+       (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
+            (nth 2 expr)))))
 
 (defun calc-select-next (num)
   (interactive "p")
             (calc-change-current-selection sel))
         (if (Math-primp (car entry))
             (calc-change-current-selection (car entry))
-          (calc-select-part num))))))
-)
+          (calc-select-part num)))))))
 
 (defun calc-select-previous (num)
   (interactive "p")
             (calc-change-current-selection (car entry))
           (let ((len (if (and calc-assoc-selections
                               (assq (car (car entry)) calc-assoc-ops))
-                         (let (op (num 0))
+                         (let (calc-fnp-op (calc-fnp-num 0))
                            (calc-find-nth-part-rec (car entry))
-                           (- 1 num))
+                           (- 1 calc-fnp-num))
                        (length (car entry)))))
-            (calc-select-part (- len num))))))))
-)
+            (calc-select-part (- len num)))))))))
 
 (defun calc-find-parent-formula (expr part)
   (cond ((eq expr part) t)
                       (not (setq res (calc-find-parent-formula
                                       (car p) part)))))
           (and p
-               (if (eq res t) expr res)))))
-)
+               (if (eq res t) expr res))))))
 
 
 (defun calc-find-assoc-parent-formula (expr part)
-  (calc-grow-assoc-formula expr (calc-find-parent-formula expr part))
-)
+  (calc-grow-assoc-formula expr (calc-find-parent-formula expr part)))
 
 (defun calc-grow-assoc-formula (expr part)
   (if calc-assoc-selections
                                (nth (calc-find-sub-formula new part) op)))
                (setq part new))))
        part)
-    part)
-)
+    part))
 
 (defun calc-find-sub-formula (expr part)
   (cond ((eq expr part) t)
           (while (and (setq expr (cdr expr))
                       (not (calc-find-sub-formula (car expr) part)))
             (setq num (1+ num)))
-          (and expr num))))
-)
+          (and expr num)))))
 
 (defun calc-unselect (num)
   (interactive "P")
   (calc-wrapper
    (calc-prepare-selection num)
-   (calc-change-current-selection nil))
-)
+   (calc-change-current-selection nil)))
 
 (defun calc-clear-selections ()
   (interactive)
             (calc-prepare-selection n)
             (calc-change-current-selection nil)))
        (setq n (1+ n))))
-   (calc-clear-command-flag 'position-point))
-)
+   (calc-clear-command-flag 'position-point)))
 
 (defun calc-show-selections (arg)
   (interactive "P")
                (calc-change-current-selection sel)))))
    (message (if calc-show-selections
                "Displaying only selected part of formulas"
-             "Displaying all but selected part of formulas")))
-)
+             "Displaying all but selected part of formulas"))))
+
+;; The variables calc-final-point-line and calc-final-point-column
+;; are declared in calc.el, and are used throughout.
+(defvar calc-final-point-line)
+(defvar calc-final-point-column)
 
 (defun calc-preserve-point ()
   (or (looking-at "\\.\n+\\'")
        (setq calc-final-point-line (+ (count-lines (point-min) (point))
                                       (if (bolp) 1 0))
              calc-final-point-column (current-column))
-       (calc-set-command-flag 'position-point)))
-)
+       (calc-set-command-flag 'position-point))))
 
 (defun calc-enable-selections (arg)
   (interactive "P")
    (calc-set-command-flag 'renum-stack)
    (message (if calc-use-selections
                "Commands operate only on selected sub-formulas"
-             "Selections of sub-formulas have no effect")))
-)
+             "Selections of sub-formulas have no effect"))))
 
 (defun calc-break-selections (arg)
   (interactive "P")
                                 (not calc-assoc-selections)))
    (message (if calc-assoc-selections
                "Selection treats a+b+c as a sum of three terms"
-             "Selection treats a+b+c as (a+b)+c")))
-)
+             "Selection treats a+b+c as (a+b)+c"))))
 
 (defun calc-prepare-selection (&optional num)
   (or num (setq num (calc-locate-cursor-element (point))))
                (+ (car (math-stack-value-offset calc-selection-cache-comp))
                   (length calc-left-label)
                   (if calc-line-numbering 4 0))))))
-  (calc-preserve-point)
-)
-(setq calc-selection-cache-entry nil)
+  (calc-preserve-point))
 
 ;;; The following ensures that no two subformulas will be "eq" to each other!
 (defun calc-encase-atoms (x)
          (equal x '(float 0 0)))
       (list 'cplx x 0)
     (calc-encase-atoms-rec x)
-    x)
-)
+    x))
 
 (defun calc-encase-atoms-rec (x)
   (or (Math-primp x)
          (if (or (not (consp (car x)))
                  (equal (car x) '(float 0 0)))
              (setcar x (list 'cplx (car x) 0))
-           (calc-encase-atoms-rec (car x))))))
-)
+           (calc-encase-atoms-rec (car x)))))))
+
+;; 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.
 
 (defun calc-find-selected-part ()
   (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset))
     (and (>= math-comp-sel-hpos 0)
         (> calc-selection-true-num 0)
         (math-composition-to-string calc-selection-cache-comp 1000000))
-    (nth 1 math-comp-sel-tag))
-)
+    (nth 1 math-comp-sel-tag)))
 
 (defun calc-change-current-selection (sub-expr)
   (or (eq sub-expr (nth 2 calc-selection-cache-entry))
        (delete-region top (point))
        (let ((calc-selection-cache-default-entry calc-selection-cache-entry))
          (insert (math-format-stack-value calc-selection-cache-entry)
-                 "\n"))))
-)
+                 "\n")))))
 
 (defun calc-top-selected (&optional n m)
   (and calc-any-selections
             (if (nth 2 (car top))
                 (setq sel (if sel t (nth 2 (car top)))))
             (setq top (cdr top)))
-          sel)))
-)
+          sel))))
 
-(defun calc-replace-sub-formula (expr old new)
-  (setq new (calc-encase-atoms new))
-  (calc-replace-sub-formula-rec expr)
-)
+;; The variables calc-rsf-old and calc-rsf-new are local to
+;; calc-replace-sub-formula, but used by calc-replace-sub-formula-rec,
+;; which is called by calc-replace-sub-formula.
+(defvar calc-rsf-old)
+(defvar calc-rsf-new)
+
+(defun calc-replace-sub-formula (expr calc-rsf-old calc-rsf-new)
+  (setq calc-rsf-new (calc-encase-atoms calc-rsf-new))
+  (calc-replace-sub-formula-rec expr))
 
 (defun calc-replace-sub-formula-rec (expr)
-  (cond ((eq expr old) new)
+  (cond ((eq expr calc-rsf-old) calc-rsf-new)
        ((Math-primp expr) expr)
        (t
         (cons (car expr)
-              (mapcar 'calc-replace-sub-formula-rec (cdr expr)))))
-)
+              (mapcar 'calc-replace-sub-formula-rec (cdr expr))))))
 
 (defun calc-sel-error ()
-  (error "Illegal operation on sub-formulas")
-)
+  (error "Invalid operation on sub-formulas"))
 
 (defun calc-replace-selections (n vals m)
   (if (calc-top-selected n m)
              (calc-push-list vals))))
         (t (calc-sel-error))))
     (calc-pop-stack n m t)
-    (calc-push-list vals m))
-)
-(setq calc-keep-selection t)
+    (calc-push-list vals m)))
 
 (defun calc-delete-selection (n)
   (let ((entry (calc-top n 'entry)))
                                                                (copy-sequence
                                                                 parent)))))
                              n)))))
-      (calc-pop-stack 1 n t)))
-)
+      (calc-pop-stack 1 n t))))
 
 (defun calc-roll-down-with-selections (n m)
   (let ((vals (append (calc-top-list m 1)
                      (calc-top-list (- n m) (1+ m))))
        (sels (append (calc-top-list m 1 'sel)
                      (calc-top-list (- n m) (1+ m) 'sel))))
-    (calc-pop-push-list n vals 1 sels))
-)
+    (calc-pop-push-list n vals 1 sels)))
 
 (defun calc-roll-up-with-selections (n m)
   (let ((vals (append (calc-top-list (- n m) 1)
                      (calc-top-list m (- n m -1))))
        (sels (append (calc-top-list (- n m) 1 'sel)
                      (calc-top-list m (- n m -1) 'sel))))
-    (calc-pop-push-list n vals 1 sels))
-)
+    (calc-pop-push-list n vals 1 sels)))
+
+;; The variable calc-sel-reselect is local to several functions
+;; which call calc-auto-selection.
+(defvar calc-sel-reselect)
 
 (defun calc-auto-selection (entry)
   (or (nth 2 entry)
       (progn
-       (and (boundp 'reselect) (setq reselect nil))
+       (setq calc-sel-reselect nil)
        (calc-prepare-selection)
-       (calc-grow-assoc-formula (car entry) (calc-find-selected-part))))
-)
+       (calc-grow-assoc-formula (car entry) (calc-find-selected-part)))))
 
 (defun calc-copy-selection ()
   (interactive)
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
          (entry (calc-top num 'entry)))
-     (calc-push (or (calc-auto-selection entry) (car entry)))))
-)
+     (calc-push (or (calc-auto-selection entry) (car entry))))))
 
 (defun calc-del-selection ()
   (interactive)
          (entry (calc-top num 'entry))
          (sel (calc-auto-selection entry)))
      (setcar (nthcdr 2 entry) (and (not (eq sel (car entry))) sel))
-     (calc-delete-selection num)))
-)
+     (calc-delete-selection num))))
 
 (defun calc-enter-selection ()
   (interactive)
   (calc-wrapper
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (expr (car entry))
          (sel (or (calc-auto-selection entry) expr))
                                         (list (calc-replace-sub-formula
                                                expr sel alg))
                                         num
-                                        (list (and reselect alg))))))
-     (calc-handle-whys)))
-)
+                                        (list (and calc-sel-reselect alg))))))
+     (calc-handle-whys))))
 
 (defun calc-edit-selection ()
   (interactive)
   (calc-wrapper
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (expr (car entry))
          (sel (or (calc-auto-selection entry) expr))
      (let ((str (math-showing-full-precision
                 (math-format-nice-expr sel (frame-width)))))
        (calc-edit-mode (list 'calc-finish-selection-edit
-                            num (list 'quote sel) reselect))
+                            num (list 'quote sel) calc-sel-reselect))
        (insert str "\n"))))
-  (calc-show-edit-buffer)
-)
+  (calc-show-edit-buffer))
+
+(defvar calc-original-buffer)
+
+;; The variable calc-edit-disp-trail is local to calc-edit-finish,
+;; in calc-yank.el.
+(defvar calc-edit-disp-trail)
+(defvar calc-edit-top)
 
 (defun calc-finish-selection-edit (num sel reselect)
   (let ((buf (current-buffer))
-       (str (buffer-substring (point) (point-max)))
+       (str (buffer-substring calc-edit-top (point-max)))
        (start (point)))
     (switch-to-buffer calc-original-buffer)
     (let ((val (math-read-expr str)))
            (error (nth 2 val))))
       (calc-wrapper
        (calc-preserve-point)
-       (if disp-trail
+       (if calc-edit-disp-trail
           (calc-trail-display 1 t))
        (setq val (calc-encase-atoms (calc-normalize val)))
        (let ((expr (calc-top num 'full)))
                                        num
                                        (list (and reselect val)))
           (calc-push val)
-          (error "Original selection has been lost"))))))
-)
+          (error "Original selection has been lost")))))))
 
 (defun calc-sel-evaluate (arg)
   (interactive "p")
   (calc-slow-wrapper
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (sel (or (calc-auto-selection entry) (car entry))))
      (calc-with-default-simplification
                                     (list (calc-replace-sub-formula
                                            (car entry) sel val))
                                     num
-                                    (list (and reselect val))))))
-     (calc-handle-whys)))
-)
+                                    (list (and calc-sel-reselect val))))))
+     (calc-handle-whys))))
 
 (defun calc-sel-expand-formula (arg)
   (interactive "p")
   (calc-slow-wrapper
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (sel (or (calc-auto-selection entry) (car entry))))
      (calc-with-default-simplification
                                     (list (calc-replace-sub-formula
                                            (car entry) sel val))
                                     num
-                                    (list (and reselect val))))))
-     (calc-handle-whys)))
-)
+                                    (list (and calc-sel-reselect val))))))
+     (calc-handle-whys))))
 
 (defun calc-sel-mult-both-sides (no-simp &optional divide)
   (interactive "P")
   (calc-wrapper
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (expr (car entry))
          (sel (or (calc-auto-selection entry) expr))
                                       (list (calc-replace-sub-formula
                                              expr sel alg))
                                       num
-                                      (list (and reselect alg)))))
-     (calc-handle-whys)))
-)
+                                      (list (and calc-sel-reselect alg)))))
+     (calc-handle-whys))))
 
 (defun calc-sel-div-both-sides (no-simp)
   (interactive "P")
-  (calc-sel-mult-both-sides no-simp t)
-)
+  (calc-sel-mult-both-sides no-simp t))
 
 (defun calc-sel-add-both-sides (no-simp &optional subtract)
   (interactive "P")
   (calc-wrapper
    (calc-preserve-point)
    (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (expr (car entry))
          (sel (or (calc-auto-selection entry) expr))
                                       (list (calc-replace-sub-formula
                                              expr sel alg))
                                       num
-                                      (list (and reselect alg)))))
-     (calc-handle-whys)))
-)
+                                      (list (and calc-sel-reselect alg)))))
+     (calc-handle-whys))))
 
 (defun calc-sel-sub-both-sides (no-simp)
   (interactive "P")
-  (calc-sel-add-both-sides no-simp t)
-)
+  (calc-sel-add-both-sides no-simp t))
+
+(provide 'calc-sel)
 
+;;; arch-tag: e5169792-777d-428f-bff5-acca66813fa2
+;;; calc-sel.el ends here