]> code.delx.au - gnu-emacs/blobdiff - lisp/calc/calc-sel.el
* view.el (view-recenter): Allow recenter to compute window height
[gnu-emacs] / lisp / calc / calc-sel.el
index 07820a44b229b2ffbcdc2c729f2dcf89ab8b95f3..42d64475447e53145213ea7c61e97c6ba5bf466f 100644 (file)
@@ -1,44 +1,45 @@
 ;;; calc-sel.el --- data selection functions for Calc
 
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
 ;; Author: David Gillespie <daveg@synaptics.com>
-;; Maintainers: D. Goel <deego@gnufans.org>
-;;              Colin Walters <walters@debian.org>
+;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
 
 ;; This file is part of GNU Emacs.
 
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
 ;; GNU Emacs is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY.  No author or distributor
-;; accepts responsibility to anyone for the consequences of using it
-;; or for whether it serves any particular purpose or works at all,
-;; unless he says so in writing.  Refer to the GNU Emacs General Public
-;; License for full details.
-
-;; Everyone is granted permission to copy, modify and redistribute
-;; GNU Emacs, but only under the conditions described in the
-;; GNU Emacs General Public License.   A copy of this license is
-;; supposed to have been given to you along with GNU Emacs so you
-;; can know your rights and responsibilities.  It should be in a
-;; file named COPYING.  Among other things, the copyright notice
-;; and this notice must be preserved on all copies.
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; 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)
 
+(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")
   (calc-wrapper
 
 (defun calc-select-part (num)
   (interactive "P")
-  (or num (setq num (- last-command-char ?0)))
+  (or num (setq num (- last-command-event ?0)))
   (calc-wrapper
    (calc-prepare-selection)
    (let ((sel (calc-find-nth-part (or (nth 2 calc-selection-cache-entry)
         (calc-change-current-selection sel)
        (error "%d is not a valid sub-formula index" num)))))
 
-(defun calc-find-nth-part (expr 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 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)
+       (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
             (nth 2 expr)))))
 
 (defun calc-select-next (num)
             (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)))))))))
 
                "Displaying only 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+\\'")
       (progn
                "Selection treats a+b+c as a sum of three terms"
              "Selection treats a+b+c as (a+b)+c"))))
 
-(defvar calc-selection-cache-entry nil)
 (defun calc-prepare-selection (&optional num)
   (or num (setq num (calc-locate-cursor-element (point))))
   (setq calc-selection-true-num num
              (setcar x (list 'cplx (car x) 0))
            (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))
         toppt
             (setq top (cdr top)))
           sel))))
 
-(defun calc-replace-sub-formula (expr old new)
-  (setq new (calc-encase-atoms new))
+;; 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))))))
 
 (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-top-list m (- n m -1) 'sel))))
     (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)))))
 
      (setcar (nthcdr 2 entry) (and (not (eq sel (car entry))) sel))
      (calc-delete-selection num))))
 
+(defvar calc-selection-history nil
+  "History for calc selections.")
+
 (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))
          alg)
      (let ((calc-dollar-values (list sel))
           (calc-dollar-used 0))
-       (setq alg (calc-do-alg-entry "" "Replace selection with: "))
+       (setq alg (calc-do-alg-entry "" "Replace selection with: " nil 
+                                    'calc-selection-history))
        (and alg
            (progn
              (setq alg (calc-encase-atoms (car alg)))
                                         (list (calc-replace-sub-formula
                                                expr sel alg))
                                         num
-                                        (list (and reselect alg))))))
+                                        (list (and calc-sel-reselect alg))))))
      (calc-handle-whys))))
 
 (defun calc-edit-selection ()
   (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))
 
+(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)))
   (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))))))
+                                    (list (and calc-sel-reselect val))))))
      (calc-handle-whys))))
 
 (defun calc-sel-expand-formula (arg)
   (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))))))
+                                    (list (and calc-sel-reselect val))))))
      (calc-handle-whys))))
 
-(defun calc-sel-mult-both-sides (no-simp &optional divide)
+(defun calc-sel-mult-both-sides (arg &optional divide)
   (interactive "P")
   (calc-wrapper
    (calc-preserve-point)
-   (let* ((num (max 1 (calc-locate-cursor-element (point))))
-         (reselect calc-keep-selection)
+   (let* ((no-simp (consp arg))
+          (num (max 1 (calc-locate-cursor-element (point))))
+         (calc-sel-reselect calc-keep-selection)
          (entry (calc-top num 'entry))
          (expr (car entry))
          (sel (or (calc-auto-selection entry) expr))
                (car (calc-do-alg-entry ""
                                        (if divide
                                            "Divide both sides by: "
-                                         "Multiply both sides by: ")))))
+                                         "Multiply both sides by: ")
+                                        nil 'calc-selection-history))))
      (and alg
          (progn
            (if (and (or (eq func '/)
                              rhs (math-simplify rhs))
                        (and (eq func '/)
                             (or (Math-equal (nth 1 sel) 1)
-                                (Math-equal (nth 1 sel) -1)
-                                (and (memq (car-safe (nth 2 sel)) '(+ -))
-                                     (memq (car-safe alg) '(+ -))))
-                            (setq rhs (math-expand-term rhs)))))
+                                (Math-equal (nth 1 sel) -1))
+;                               (and (memq (car-safe (nth 2 sel)) '(+ -))
+;                                    (memq (car-safe alg) '(+ -))))
+                             (unless arg
+                               (setq rhs (math-expand-term rhs))))))
+                  (if (and arg (not no-simp))
+                      (setq rhs (math-simplify
+                                 (calcFunc-expand rhs (unless (= arg 0) arg)))))
                  (setq alg (calc-encase-atoms
                             (calc-normalize (list func lhs rhs)))))
              (setq rhs (list (if divide '* '/) sel alg))
                                       (list (calc-replace-sub-formula
                                              expr sel alg))
                                       num
-                                      (list (and reselect alg)))))
+                                      (list (and calc-sel-reselect alg)))))
      (calc-handle-whys))))
 
 (defun calc-sel-div-both-sides (no-simp)
   (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))
                (car (calc-do-alg-entry ""
                                        (if subtract
                                            "Subtract from both sides: "
-                                         "Add to both sides: ")))))
+                                         "Add to both sides: ")
+                                        nil 'calc-selection-history))))
      (and alg
          (progn
            (if (and (assq func calc-tweak-eqn-table)
                                       (list (calc-replace-sub-formula
                                              expr sel alg))
                                       num
-                                      (list (and reselect alg)))))
+                                      (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))
 
+(provide 'calc-sel)
+
+;; arch-tag: e5169792-777d-428f-bff5-acca66813fa2
 ;;; calc-sel.el ends here