Merge from trunk.
[bpt/emacs.git] / lisp / ses.el
index 4036516..8765374 100644 (file)
@@ -1,6 +1,6 @@
 ;;; ses.el -- Simple Emacs Spreadsheet  -*- coding: utf-8 -*-
 
-;; Copyright (C) 2002-2011  Free Software Foundation, Inc.
+;; Copyright (C) 2002-2012  Free Software Foundation, Inc.
 
 ;; Author: Jonathan Yavner <jyavner@member.fsf.org>
 ;; Maintainer: Vincent Belaïche  <vincentb1@users.sourceforge.net>
@@ -65,6 +65,7 @@
 
 (defgroup ses nil
   "Simple Emacs Spreadsheet."
+  :tag "SES"
   :group  'applications
   :prefix "ses-"
   :version "21.1")
@@ -282,6 +283,9 @@ default printer and then modify its output.")
       ses--numcols ses--numrows ses--symbolic-formulas
       ses--data-marker ses--params-marker (ses--Dijkstra-attempt-nb . 0)
       ses--Dijkstra-weight-bound
+      ;; This list is useful to speed-up clean-up of symbols when
+      ;; an area containing renamed cell is deleted.
+      ses--renamed-cell-symb-list
       ;; Global variables that we override
       mode-line-process next-line-add-newlines transient-mark-mode)
     "Buffer-local variables used by SES.")
@@ -675,8 +679,12 @@ for this spreadsheet."
        (make-local-variable sym)))))
 
 (defun ses-create-cell-variable (sym row col)
-  "Create a buffer-local variable for cell with symbol
-SYM at position ROW COL.  Return nil in case of failure."
+  "Create a buffer-local variable `SYM' for cell at position (ROW, COL).
+
+SYM is the symbol for that variable, ROW and COL are integers for
+row and column of the cell, with numbering starting from 0.
+
+Return nil in case of failure."
   (unless (local-variable-p sym)
     (make-local-variable  sym)
     (put sym 'ses-cell (cons row col))))
@@ -689,7 +697,10 @@ SYM at position ROW COL.  Return nil in case of failure."
   (let (sym)
     (dotimes (row (1+ (- maxrow minrow)))
       (dotimes (col (1+ (- maxcol mincol)))
-       (setq sym (ses-create-cell-symbol (+ row minrow) (+ col mincol)))
+       (let ((xrow  (+ row minrow)) (xcol (+ col mincol)))
+         (setq sym (if (and (< xrow ses--numrows) (< xcol ses--numcols))
+                       (ses-cell-symbol xrow xcol)
+                       (ses-create-cell-symbol xrow xcol))))
        (if (boundp sym)
            (push `(apply ses-set-with-undo ,sym ,(symbol-value sym))
                  buffer-undo-list))
@@ -930,6 +941,7 @@ the old and FORCE is nil."
 
 (defcustom ses-self-reference-early-detection nil
   "True if cycle detection is early for cells that refer to themselves."
+  :version "24.1"
   :type 'boolean
   :group 'ses)
 
@@ -3188,6 +3200,9 @@ highlighted range in the spreadsheet."
   (or
    (and  (local-variable-p new-name)
         (ses-sym-rowcol new-name)
+        ;; this test is needed because ses-cell property of deleted cells
+        ;; is not deleted in case of subsequent undo
+        (memq new-name ses--renamed-cell-symb-list)
         (error "Already a cell name"))
    (and (boundp new-name)
        (null (yes-or-no-p (format "`%S' is already bound outside this buffer, continue? "
@@ -3195,6 +3210,7 @@ highlighted range in the spreadsheet."
        (error "Already a bound cell name")))
   (let* ((rowcol (ses-sym-rowcol ses--curcell))
         (cell (ses-get-cell (car rowcol) (cdr rowcol))))
+    (put new-name 'ses-cell rowcol)
     (dolist (reference (ses-cell-references (car rowcol) (cdr rowcol)))
       (let* ((rowcol (ses-sym-rowcol reference))
             (cell  (ses-get-cell (car rowcol) (cdr rowcol))))
@@ -3204,7 +3220,7 @@ highlighted range in the spreadsheet."
                               (ses-cell-formula cell)
                               ses--curcell
                               new-name))))
-    (put new-name 'ses-cell rowcol)
+    (push new-name ses--renamed-cell-symb-list)
     (set new-name (symbol-value ses--curcell))
     (aset cell 0 new-name)
     (put ses--curcell 'ses-cell nil)
@@ -3220,7 +3236,6 @@ highlighted range in the spreadsheet."
                    (point)))))
       (put-text-property pos end 'intangible new-name))) )
 
-
 ;;----------------------------------------------------------------------------
 ;; Checking formulas for safety
 ;;----------------------------------------------------------------------------