annotate lisp/ses.el @ 51696:ca4a1b3128f7

(vc-default-show-log-entry): Use with-no-warnings.
author Richard M. Stallman <rms@gnu.org>
date Mon, 30 Jun 2003 10:34:26 +0000
parents 3261228348b4
children c195b419ef52 d7ddb3e565de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47667
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1 ;;;; ses.el -- Simple Emacs Spreadsheet
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
4
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
5 ;; Author: Jonathan Yavner <jyavner@engineer.com>
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
6 ;; Maintainer: Jonathan Yavner <jyavner@engineer.com>
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
7 ;; Keywords: spreadsheet
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
8
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
10
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
14 ;; any later version.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
15
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
20
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
24 ;; Boston, MA 02111-1307, USA.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
25
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
26 ;;; To-do list:
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
27 ;; * Do something about control characters & octal codes in cell print
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
28 ;; areas. Currently they distort the columnar appearance, but fixing them
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
29 ;; seems like too much work? Use text-char-description?
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
30 ;; * Input validation functions. How specified?
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
31 ;; * Menubar and popup menus.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
32 ;; * Faces (colors & styles) in print cells.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
33 ;; * Move a column by dragging its letter in the header line.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
34 ;; * Left-margin column for row number.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
35 ;; * Move a row by dragging its number in the left-margin.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
36
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
37 (require 'unsafep)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
38
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
39
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
40 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
41 ;;;; User-customizable variables
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
42 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
43
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
44 (defgroup ses nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
45 "Simple Emacs Spreadsheet"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
46 :group 'applications
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
47 :prefix "ses-"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
48 :version "21.1")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
49
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
50 (defcustom ses-initial-size '(1 . 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
51 "Initial size of a new spreadsheet, as a cons (NUMROWS . NUMCOLS)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
52 :group 'ses
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
53 :type '(cons (integer :tag "numrows") (integer :tag "numcols")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
54
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
55 (defcustom ses-initial-column-width 7
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
56 "Initial width of columns in a new spreadsheet."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
57 :group 'ses
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
58 :type '(integer :match (lambda (widget value) (> value 0))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
59
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
60 (defcustom ses-initial-default-printer "%.7g"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
61 "Initial default printer for a new spreadsheet."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
62 :group 'ses
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
63 :type '(choice string
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
64 (list :tag "Parenthesized string" string)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
65 function))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
66
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
67 (defcustom ses-after-entry-functions '(forward-char)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
68 "Things to do after entering a value into a cell. An abnormal hook that
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
69 usually runs a cursor-movement function. Each function is called with ARG=1."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
70 :group 'ses
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
71 :type 'hook
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
72 :options '(forward-char backward-char next-line previous-line))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
73
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
74 (defcustom ses-mode-hook nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
75 "Hook functions to be run upon entering SES mode."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
76 :group 'ses
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
77 :type 'hook)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
78
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
79
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
80 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
81 ;;;; Global variables and constants
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
82 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
83
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
84 (defvar ses-read-cell-history nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
85 "List of formulas that have been typed in.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
86
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
87 (defvar ses-read-printer-history nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
88 "List of printer functions that have been typed in.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
89
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
90 (defvar ses-mode-map nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
91 "Local keymap for Simple Emacs Spreadsheet.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
92
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
93 (defvar ses-mode-print-map nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
94 "Local keymap for SES print area.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
95
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
96 (defvar ses-mode-edit-map nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
97 "Local keymap for SES minibuffer cell-editing.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
98
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
99 ;Key map used for 'x' key.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
100 (defalias 'ses-export-keymap
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
101 (let ((map (make-sparse-keymap "SES export")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
102 (define-key map "T" (cons " tab-formulas" 'ses-export-tsf))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
103 (define-key map "t" (cons " tab-values" 'ses-export-tsv))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
104 map))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
105
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
106 (defconst ses-print-data-boundary "\n\014\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
107 "Marker string denoting the boundary between print area and data area")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
108
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
109 (defconst ses-initial-global-parameters
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
110 "\n( ;Global parameters (these are read first)\n 2 ;SES file-format\n 1 ;numrows\n 1 ;numcols\n)\n\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
111 "Initial contents for the three-element list at the bottom of the data area")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
112
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
113 (defconst ses-initial-file-trailer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
114 ";;; Local Variables:\n;;; mode: ses\n;;; End:\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
115 "Initial contents for the file-trailer area at the bottom of the file.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
116
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
117 (defconst ses-initial-file-contents
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
118 (concat " \n" ;One blank cell in print area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
119 ses-print-data-boundary
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
120 "(ses-cell A1 nil nil nil nil)\n" ;One blank cell in data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
121 "\n" ;End-of-row terminator for the one row in data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
122 "(ses-column-widths [7])\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
123 "(ses-column-printers [nil])\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
124 "(ses-default-printer \"%.7g\")\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
125 "(ses-header-row 0)\n"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
126 ses-initial-global-parameters
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
127 ses-initial-file-trailer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
128 "The initial contents of an empty spreadsheet.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
129
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
130 (defconst ses-cell-size 4
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
131 "A cell consists of a SYMBOL, a FORMULA, a PRINTER-function, and a list of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
132 REFERENCES.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
133
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
134 (defconst ses-paramlines-plist
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
135 '(column-widths 2 col-printers 3 default-printer 4 header-row 5
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
136 file-format 8 numrows 9 numcols 10)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
137 "Offsets from last cell line to various parameter lines in the data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
138 of a spreadsheet.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
139
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
140 (defconst ses-box-prop '(:box (:line-width 2 :style released-button))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
141 "Display properties to create a raised box for cells in the header line.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
142
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
143 (defconst ses-standard-printer-functions
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
144 '(ses-center ses-center-span ses-dashfill ses-dashfill-span
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
145 ses-tildefill-span)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
146 "List of print functions to be included in initial history of printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
147 functions. None of these standard-printer functions is suitable for use as a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
148 column printer or a global-default printer because they invoke the column or
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
149 default printer and then modify its output.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
150
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
151 (eval-and-compile
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
152 (defconst ses-localvars
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
153 '(blank-line cells col-printers column-widths curcell curcell-overlay
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
154 default-printer deferred-narrow deferred-recalc deferred-write
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
155 file-format header-hscroll header-row header-string linewidth
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
156 mode-line-process next-line-add-newlines numcols numrows
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
157 symbolic-formulas transient-mark-mode)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
158 "Buffer-local variables used by SES."))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
159
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
160 ;;When compiling, create all the buffer locals and give them values
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
161 (eval-when-compile
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
162 (dolist (x ses-localvars)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
163 (make-local-variable x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
164 (set x nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
165
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
166
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
167 ;;;
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
168 ;;; "Side-effect variables". They are set in one function, altered in
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
169 ;;; another as a side effect, then read back by the first, as a way of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
170 ;;; passing back more than one value. These declarations are just to make
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
171 ;;; the compiler happy, and to conform to standard Emacs-Lisp practice (I
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
172 ;;; think the make-local-variable trick above is cleaner).
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
173 ;;;
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
174
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
175 (defvar ses-relocate-return nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
176 "Set by `ses-relocate-formula' and `ses-relocate-range', read by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
177 `ses-relocate-all'. Set to 'delete if a cell-reference was deleted from a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
178 formula--so the formula needs recalculation. Set to 'range if the size of a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
179 `ses-range' was changed--so both the formula's value and list of dependents
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
180 need to be recalculated.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
181
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
182 (defvar ses-call-printer-return nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
183 "Set to t if last cell printer invoked by `ses-call-printer' requested
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
184 left-justification of the result. Set to error-signal if ses-call-printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
185 encountered an error during printing. Nil otherwise.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
186
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
187 (defvar ses-start-time nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
188 "Time when current operation started. Used by `ses-time-check' to decide
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
189 when to emit a progress message.")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
190
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
191
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
192 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
193 ;;;; Macros
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
194 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
195
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
196 (defmacro ses-get-cell (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
197 "Return the cell structure that stores information about cell (ROW,COL)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
198 `(aref (aref cells ,row) ,col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
199
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
200 (defmacro ses-cell-symbol (row &optional col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
201 "From a CELL or a pair (ROW,COL), get the symbol that names the local-variable holding its value. (0,0) => A1."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
202 `(aref ,(if col `(ses-get-cell ,row ,col) row) 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
203
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
204 (defmacro ses-cell-formula (row &optional col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
205 "From a CELL or a pair (ROW,COL), get the function that computes its value."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
206 `(aref ,(if col `(ses-get-cell ,row ,col) row) 1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
207
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
208 (defmacro ses-cell-printer (row &optional col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
209 "From a CELL or a pair (ROW,COL), get the function that prints its value."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
210 `(aref ,(if col `(ses-get-cell ,row ,col) row) 2))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
211
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
212 (defmacro ses-cell-references (row &optional col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
213 "From a CELL or a pair (ROW,COL), get the list of symbols for cells whose
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
214 functions refer to its value."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
215 `(aref ,(if col `(ses-get-cell ,row ,col) row) 3))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
216
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
217 (defmacro ses-cell-value (row &optional col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
218 "From a CELL or a pair (ROW,COL), get the current value for that cell."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
219 `(symbol-value (ses-cell-symbol ,row ,col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
220
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
221 (defmacro ses-col-width (col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
222 "Return the width for column COL."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
223 `(aref column-widths ,col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
224
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
225 (defmacro ses-col-printer (col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
226 "Return the default printer for column COL."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
227 `(aref col-printers ,col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
228
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
229 (defmacro ses-sym-rowcol (sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
230 "From a cell-symbol SYM, gets the cons (row . col). A1 => (0 . 0). Result
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
231 is nil if SYM is not a symbol that names a cell."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
232 `(and (symbolp ,sym) (get ,sym 'ses-cell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
233
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
234 (defmacro ses-cell (sym value formula printer references)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
235 "Load a cell SYM from the spreadsheet file. Does not recompute VALUE from
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
236 FORMULA, does not reprint using PRINTER, does not check REFERENCES. This is a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
237 macro to prevent propagate-on-load viruses. Safety-checking for FORMULA and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
238 PRINTER are deferred until first use."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
239 (let ((rowcol (ses-sym-rowcol sym)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
240 (ses-formula-record formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
241 (ses-printer-record printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
242 (or (atom formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
243 (eq safe-functions t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
244 (setq formula `(ses-safe-formula ,formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
245 (or (not printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
246 (stringp printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
247 (eq safe-functions t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
248 (setq printer `(ses-safe-printer ,printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
249 (aset (aref cells (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
250 (cdr rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
251 (vector sym formula printer references)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
252 (set sym value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
253 sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
254
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
255 (defmacro ses-column-widths (widths)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
256 "Load the vector of column widths from the spreadsheet file. This is a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
257 macro to prevent propagate-on-load viruses."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
258 (or (and (vectorp widths) (= (length widths) numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
259 (error "Bad column-width vector"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
260 ;;To save time later, we also calculate the total width of each line in the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
261 ;;print area (excluding the terminating newline)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
262 (setq column-widths widths
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
263 linewidth (apply '+ -1 (mapcar '1+ widths))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
264 blank-line (concat (make-string linewidth ? ) "\n"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
265 t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
266
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
267 (defmacro ses-column-printers (printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
268 "Load the vector of column printers from the spreadsheet file and checks
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
269 them for safety. This is a macro to prevent propagate-on-load viruses."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
270 (or (and (vectorp printers) (= (length printers) numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
271 (error "Bad column-printers vector"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
272 (dotimes (x numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
273 (aset printers x (ses-safe-printer (aref printers x))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
274 (setq col-printers printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
275 (mapc 'ses-printer-record printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
276 t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
277
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
278 (defmacro ses-default-printer (def)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
279 "Load the global default printer from the spreadsheet file and checks it
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
280 for safety. This is a macro to prevent propagate-on-load viruses."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
281 (setq default-printer (ses-safe-printer def))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
282 (ses-printer-record def)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
283 t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
284
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
285 (defmacro ses-header-row (row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
286 "Load the header row from the spreadsheet file and checks it
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
287 for safety. This is a macro to prevent propagate-on-load viruses."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
288 (or (and (wholenump row) (< row numrows))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
289 (error "Bad header-row"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
290 (setq header-row row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
291 t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
292
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
293 (defmacro ses-dotimes-msg (spec msg &rest body)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
294 "(ses-dotimes-msg (VAR LIMIT) MSG BODY...): Like `dotimes', but
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
295 a message is emitted using MSG every second or so during the loop."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
296 (let ((msgvar (make-symbol "msg"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
297 (limitvar (make-symbol "limit"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
298 (var (car spec))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
299 (limit (cadr spec)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
300 `(let ((,limitvar ,limit)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
301 (,msgvar ,msg))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
302 (setq ses-start-time (float-time))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
303 (message ,msgvar)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
304 (setq ,msgvar (concat ,msgvar " (%d%%)"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
305 (dotimes (,var ,limitvar)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
306 (ses-time-check ,msgvar '(/ (* ,var 100) ,limitvar))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
307 ,@body)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
308 (message nil))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
309
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
310 (put 'ses-dotimes-msg 'lisp-indent-function 2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
311 (def-edebug-spec ses-dotimes-msg ((symbolp form) form body))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
312
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
313 (defmacro ses-dorange (curcell &rest body)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
314 "Execute BODY repeatedly, with the variables `row' and `col' set to each
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
315 cell in the range specified by CURCELL. The range is available in the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
316 variables `minrow', `maxrow', `mincol', and `maxcol'."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
317 (let ((cur (make-symbol "cur"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
318 (min (make-symbol "min"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
319 (max (make-symbol "max"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
320 (r (make-symbol "r"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
321 (c (make-symbol "c")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
322 `(let* ((,cur ,curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
323 (,min (ses-sym-rowcol (if (consp ,cur) (car ,cur) ,cur)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
324 (,max (ses-sym-rowcol (if (consp ,cur) (cdr ,cur) ,cur))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
325 (let ((minrow (car ,min))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
326 (maxrow (car ,max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
327 (mincol (cdr ,min))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
328 (maxcol (cdr ,max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
329 row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
330 (if (or (> minrow maxrow) (> mincol maxcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
331 (error "Empty range"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
332 (dotimes (,r (- maxrow minrow -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
333 (setq row (+ ,r minrow))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
334 (dotimes (,c (- maxcol mincol -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
335 (setq col (+ ,c mincol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
336 ,@body))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
337
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
338 (put 'ses-dorange 'lisp-indent-function 'defun)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
339 (def-edebug-spec ses-dorange (form body))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
340
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
341 ;;Support for coverage testing.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
342 (defmacro 1value (form)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
343 "For code-coverage testing, indicate that FORM is expected to always have
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
344 the same value."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
345 form)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
346 (defmacro noreturn (form)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
347 "For code-coverage testing, indicate that FORM will always signal an error."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
348 form)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
349
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
350
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
351 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
352 ;;;; Utility functions
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
353 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
354
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
355 (defun ses-vector-insert (array idx new)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
356 "Create a new vector which is one larger than ARRAY and has NEW inserted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
357 before element IDX."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
358 (let* ((len (length array))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
359 (result (make-vector (1+ len) new)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
360 (dotimes (x len)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
361 (aset result
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
362 (if (< x idx) x (1+ x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
363 (aref array x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
364 result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
365
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
366 ;;Allow ARRAY to be a symbol for use in buffer-undo-list
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
367 (defun ses-vector-delete (array idx count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
368 "Create a new vector which is a copy of ARRAY with COUNT objects removed
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
369 starting at element IDX. ARRAY is either a vector or a symbol whose value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
370 is a vector--if a symbol, the new vector is assigned as the symbol's value."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
371 (let* ((a (if (arrayp array) array (symbol-value array)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
372 (len (- (length a) count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
373 (result (make-vector len nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
374 (dotimes (x len)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
375 (aset result x (aref a (if (< x idx) x (+ x count)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
376 (if (symbolp array)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
377 (set array result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
378 result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
379
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
380 (defun ses-delete-line (count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
381 "Like `kill-line', but no kill ring."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
382 (let ((pos (point)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
383 (forward-line count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
384 (delete-region pos (point))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
385
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
386 (defun ses-printer-validate (printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
387 "Signals an error if PRINTER is not a valid SES cell printer."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
388 (or (not printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
389 (stringp printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
390 (functionp printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
391 (and (stringp (car-safe printer)) (not (cdr printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
392 (error "Invalid printer function"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
393 printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
394
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
395 (defun ses-printer-record (printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
396 "Add PRINTER to `ses-read-printer-history' if not already there, after first
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
397 checking that it is a valid printer function."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
398 (ses-printer-validate printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
399 ;;To speed things up, we avoid calling prin1 for the very common "nil" case.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
400 (if printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
401 (add-to-list 'ses-read-printer-history (prin1-to-string printer))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
402
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
403 (defun ses-formula-record (formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
404 "If FORMULA is of the form 'symbol, adds it to the list of symbolic formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
405 for this spreadsheet."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
406 (when (and (eq (car-safe formula) 'quote)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
407 (symbolp (cadr formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
408 (add-to-list 'symbolic-formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
409 (list (symbol-name (cadr formula))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
410
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
411 (defun ses-column-letter (col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
412 "Converts a column number to A..Z or AA..ZZ"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
413 (if (< col 26)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
414 (char-to-string (+ ?A col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
415 (string (+ ?@ (/ col 26)) (+ ?A (% col 26)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
416
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
417 (defun ses-create-cell-symbol (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
418 "Produce a symbol that names the cell (ROW,COL). (0,0) => 'A1."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
419 (intern (concat (ses-column-letter col) (number-to-string (1+ row)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
420
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
421 (defun ses-create-cell-variable-range (minrow maxrow mincol maxcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
422 "Create buffer-local variables for cells. This is undoable."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
423 (push `(ses-destroy-cell-variable-range ,minrow ,maxrow ,mincol ,maxcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
424 buffer-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
425 (let (sym xrow xcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
426 (dotimes (row (1+ (- maxrow minrow)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
427 (dotimes (col (1+ (- maxcol mincol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
428 (setq xrow (+ row minrow)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
429 xcol (+ col mincol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
430 sym (ses-create-cell-symbol xrow xcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
431 (put sym 'ses-cell (cons xrow xcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
432 (make-local-variable sym)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
433
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
434 ;;;We do not delete the ses-cell properties for the cell-variables, in case a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
435 ;;;formula that refers to this cell is in the kill-ring and is later pasted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
436 ;;;back in.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
437 (defun ses-destroy-cell-variable-range (minrow maxrow mincol maxcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
438 "Destroy buffer-local variables for cells. This is undoable."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
439 (let (sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
440 (dotimes (row (1+ (- maxrow minrow)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
441 (dotimes (col (1+ (- maxcol mincol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
442 (setq sym (ses-create-cell-symbol (+ row minrow) (+ col mincol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
443 (if (boundp sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
444 (push `(ses-set-with-undo ,sym ,(symbol-value sym))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
445 buffer-undo-list))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
446 (kill-local-variable sym))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
447 (push `(ses-create-cell-variable-range ,minrow ,maxrow ,mincol ,maxcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
448 buffer-undo-list))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
449
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
450 (defun ses-reset-header-string ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
451 "Flags the header string for update. Upon undo, the header string will be
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
452 updated again."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
453 (push '(ses-reset-header-string) buffer-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
454 (setq header-hscroll -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
455
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
456 ;;Split this code off into a function to avoid coverage-testing difficulties
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
457 (defun ses-time-check (format arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
458 "If `ses-start-time' is more than a second ago, call `message' with FORMAT
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
459 and (eval ARG) and reset `ses-start-time' to the current time."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
460 (when (> (- (float-time) ses-start-time) 1.0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
461 (message format (eval arg))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
462 (setq ses-start-time (float-time)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
463 nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
464
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
465
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
466 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
467 ;;;; The cells
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
468 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
469
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
470 (defun ses-set-cell (row col field val)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
471 "Install VAL as the contents for field FIELD (named by a quoted symbol) of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
472 cell (ROW,COL). This is undoable. The cell's data will be updated through
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
473 `post-command-hook'."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
474 (let ((cell (ses-get-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
475 (elt (plist-get '(value t symbol 0 formula 1 printer 2 references 3)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
476 field))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
477 change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
478 (or elt (signal 'args-out-of-range nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
479 (setq change (if (eq elt t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
480 (ses-set-with-undo (ses-cell-symbol cell) val)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
481 (ses-aset-with-undo cell elt val)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
482 (if change
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
483 (add-to-list 'deferred-write (cons row col))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
484 nil) ;Make coverage-tester happy
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
485
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
486 (defun ses-cell-set-formula (row col formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
487 "Store a new formula for (ROW . COL) and enqueues the cell for
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
488 recalculation via `post-command-hook'. Updates the reference lists for the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
489 cells that this cell refers to. Does not update cell value or reprint the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
490 cell. To avoid inconsistencies, this function is not interruptible, which
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
491 means Emacs will crash if FORMULA contains a circular list."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
492 (let* ((cell (ses-get-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
493 (old (ses-cell-formula cell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
494 (let ((sym (ses-cell-symbol cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
495 (oldref (ses-formula-references old))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
496 (newref (ses-formula-references formula))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
497 (inhibit-quit t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
498 x xrow xcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
499 (add-to-list 'deferred-recalc sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
500 ;;Delete old references from this cell. Skip the ones that are also
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
501 ;;in the new list.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
502 (dolist (ref oldref)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
503 (unless (memq ref newref)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
504 (setq x (ses-sym-rowcol ref)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
505 xrow (car x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
506 xcol (cdr x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
507 (ses-set-cell xrow xcol 'references
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
508 (delq sym (ses-cell-references xrow xcol)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
509 ;;Add new ones. Skip ones left over from old list
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
510 (dolist (ref newref)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
511 (setq x (ses-sym-rowcol ref)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
512 xrow (car x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
513 xcol (cdr x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
514 x (ses-cell-references xrow xcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
515 (or (memq sym x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
516 (ses-set-cell xrow xcol 'references (cons sym x))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
517 (ses-formula-record formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
518 (ses-set-cell row col 'formula formula))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
519
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
520 (defun ses-calculate-cell (row col force)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
521 "Calculate and print the value for cell (ROW,COL) using the cell's formula
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
522 function and print functions, if any. Result is nil for normal operation, or
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
523 the error signal if the formula or print function failed. The old value is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
524 left unchanged if it was *skip* and the new value is nil.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
525 Any cells that depend on this cell are queued for update after the end of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
526 processing for the current keystroke, unless the new value is the same as
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
527 the old and FORCE is nil."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
528 (let ((cell (ses-get-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
529 formula-error printer-error)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
530 (let ((symbol (ses-cell-symbol cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
531 (oldval (ses-cell-value cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
532 (formula (ses-cell-formula cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
533 newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
534 (if (eq (car-safe formula) 'ses-safe-formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
535 (ses-set-cell row col 'formula (ses-safe-formula (cadr formula))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
536 (condition-case sig
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
537 (setq newval (eval formula))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
538 (error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
539 (setq formula-error sig
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
540 newval '*error*)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
541 (if (and (not newval) (eq oldval '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
542 ;;Don't lose the *skip* - previous field spans this one
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
543 (setq newval '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
544 (when (or force (not (eq newval oldval)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
545 (add-to-list 'deferred-write (cons row col)) ;In case force=t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
546 (ses-set-cell row col 'value newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
547 (dolist (ref (ses-cell-references cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
548 (add-to-list 'deferred-recalc ref))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
549 (setq printer-error (ses-print-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
550 (or formula-error printer-error)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
551
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
552 (defun ses-clear-cell (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
553 "Delete formula and printer for cell (ROW,COL)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
554 (ses-set-cell row col 'printer nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
555 (ses-cell-set-formula row col nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
556
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
557 (defun ses-update-cells (list &optional force)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
558 "Recalculate cells in LIST, checking for dependency loops. Prints
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
559 progress messages every second. Dependent cells are not recalculated
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
560 if the cell's value is unchanged if FORCE is nil."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
561 (let ((deferred-recalc list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
562 (nextlist list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
563 (pos (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
564 curlist prevlist rowcol formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
565 (with-temp-message " "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
566 (while (and deferred-recalc (not (equal nextlist prevlist)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
567 ;;In each loop, recalculate cells that refer only to other cells that
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
568 ;;have already been recalculated or aren't in the recalculation
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
569 ;;region. Repeat until all cells have been processed or until the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
570 ;;set of cells being worked on stops changing.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
571 (if prevlist
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
572 (message "Recalculating... (%d cells left)"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
573 (length deferred-recalc)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
574 (setq curlist deferred-recalc
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
575 deferred-recalc nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
576 prevlist nextlist)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
577 (while curlist
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
578 (setq rowcol (ses-sym-rowcol (car curlist))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
579 formula (ses-cell-formula (car rowcol) (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
580 (or (catch 'ref
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
581 (dolist (ref (ses-formula-references formula))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
582 (when (or (memq ref curlist)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
583 (memq ref deferred-recalc))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
584 ;;This cell refers to another that isn't done yet
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
585 (add-to-list 'deferred-recalc (car curlist))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
586 (throw 'ref t))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
587 ;;ses-update-cells is called from post-command-hook, so
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
588 ;;inhibit-quit is implicitly bound to t.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
589 (when quit-flag
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
590 ;;Abort the recalculation. User will probably undo now.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
591 (error "Quit"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
592 (ses-calculate-cell (car rowcol) (cdr rowcol) force))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
593 (setq curlist (cdr curlist)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
594 (dolist (ref deferred-recalc)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
595 (add-to-list 'nextlist ref))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
596 (setq nextlist (sort (copy-sequence nextlist) 'string<))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
597 (if (equal nextlist prevlist)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
598 ;;We'll go around the loop one more time.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
599 (add-to-list 'nextlist t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
600 (when deferred-recalc
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
601 ;;Just couldn't finish these
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
602 (dolist (x deferred-recalc)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
603 (let ((rowcol (ses-sym-rowcol x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
604 (ses-set-cell (car rowcol) (cdr rowcol) 'value '*error*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
605 (1value (ses-print-cell (car rowcol) (cdr rowcol)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
606 (error "Circular references: %s" deferred-recalc))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
607 (message " "))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
608 ;;Can't use save-excursion here: if the cell under point is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
609 ;;updated, save-excusion's marker will move past the cell.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
610 (goto-char pos)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
611
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
612
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
613 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
614 ;;;; The print area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
615 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
616
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
617 ;;;We turn off point-motion-hooks and explicitly position the cursor, in case
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
618 ;;;the intangible properties have gotten screwed up (e.g., when
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
619 ;;;ses-goto-print is called during a recursive ses-print-cell).
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
620 (defun ses-goto-print (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
621 "Move point to print area for cell (ROW,COL)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
622 (let ((inhibit-point-motion-hooks t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
623 (goto-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
624 (forward-line row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
625 (dotimes (c col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
626 (forward-char (1+ (ses-col-width c))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
627
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
628 (defun ses-set-curcell ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
629 "Sets `curcell' to the current cell symbol, or a cons (BEG,END) for a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
630 region, or nil if cursor is not at a cell."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
631 (if (or (not mark-active)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
632 deactivate-mark
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
633 (= (region-beginning) (region-end)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
634 ;;Single cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
635 (setq curcell (get-text-property (point) 'intangible))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
636 ;;Range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
637 (let ((bcell (get-text-property (region-beginning) 'intangible))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
638 (ecell (get-text-property (1- (region-end)) 'intangible)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
639 (setq curcell (if (and bcell ecell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
640 (cons bcell ecell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
641 nil))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
642 nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
643
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
644 (defun ses-check-curcell (&rest args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
645 "Signal an error if curcell is inappropriate. The end marker is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
646 appropriate if some argument is 'end. A range is appropriate if some
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
647 argument is 'range. A single cell is appropriate unless some argument is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
648 'needrange."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
649 (if (eq curcell t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
650 ;;curcell recalculation was postponed, but user typed ahead
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
651 (ses-set-curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
652 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
653 ((not curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
654 (or (memq 'end args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
655 (error "Not at cell")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
656 ((consp curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
657 (or (memq 'range args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
658 (memq 'needrange args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
659 (error "Can't use a range")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
660 ((memq 'needrange args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
661 (error "Need a range"))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
662
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
663 (defun ses-print-cell (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
664 "Format and print the value of cell (ROW,COL) to the print area, using the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
665 cell's printer function. If the cell's new print form is too wide, it will
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
666 spill over into the following cell, but will not run off the end of the row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
667 or overwrite the next non-nil field. Result is nil for normal operation, or
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
668 the error signal if the printer function failed and the cell was formatted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
669 with \"%s\". If the cell's value is *skip*, nothing is printed because the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
670 preceding cell has spilled over."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
671 (catch 'ses-print-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
672 (let* ((cell (ses-get-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
673 (value (ses-cell-value cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
674 (printer (ses-cell-printer cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
675 (maxcol (1+ col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
676 text sig startpos x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
677 ;;Create the string to print
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
678 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
679 ((eq value '*skip*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
680 ;;Don't print anything
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
681 (throw 'ses-print-cell nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
682 ((eq value '*error*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
683 (setq text (make-string (ses-col-width col) ?#)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
684 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
685 ;;Deferred safety-check on printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
686 (if (eq (car-safe printer) 'ses-safe-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
687 (ses-set-cell row col 'printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
688 (setq printer (ses-safe-printer (cadr printer)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
689 ;;Print the value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
690 (setq text (ses-call-printer (or printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
691 (ses-col-printer col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
692 default-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
693 value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
694 (if (consp ses-call-printer-return)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
695 ;;Printer returned an error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
696 (setq sig ses-call-printer-return))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
697 ;;Adjust print width to match column width
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
698 (let ((width (ses-col-width col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
699 (len (length text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
700 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
701 ((< len width)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
702 ;;Fill field to length with spaces
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
703 (setq len (make-string (- width len) ? )
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
704 text (if (eq ses-call-printer-return t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
705 (concat text len)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
706 (concat len text))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
707 ((> len width)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
708 ;;Spill over into following cells, if possible
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
709 (let ((maxwidth width))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
710 (while (and (> len maxwidth)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
711 (< maxcol numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
712 (or (not (setq x (ses-cell-value row maxcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
713 (eq x '*skip*)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
714 (unless x
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
715 ;;Set this cell to '*skip* so it won't overwrite our spillover
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
716 (ses-set-cell row maxcol 'value '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
717 (setq maxwidth (+ maxwidth (ses-col-width maxcol) 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
718 maxcol (1+ maxcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
719 (if (<= len maxwidth)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
720 ;;Fill to complete width of all the fields spanned
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
721 (setq text (concat text (make-string (- maxwidth len) ? )))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
722 ;;Not enough room to end of line or next non-nil field. Truncate
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
723 ;;if string; otherwise fill with error indicator
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
724 (setq sig `(error "Too wide" ,text))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
725 (if (stringp value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
726 (setq text (substring text 0 maxwidth))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
727 (setq text (make-string maxwidth ?#))))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
728 ;;Substitute question marks for tabs and newlines. Newlines are
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
729 ;;used as row-separators; tabs could confuse the reimport logic.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
730 (setq text (replace-regexp-in-string "[\t\n]" "?" text))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
731 (ses-goto-print row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
732 (setq startpos (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
733 ;;Install the printed result. This is not interruptible.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
734 (let ((inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
735 (inhibit-quit t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
736 (delete-char (1+ (length text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
737 ;;We use concat instead of inserting separate strings in order to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
738 ;;reduce the number of cells in the undo list.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
739 (setq x (concat text (if (< maxcol numcols) " " "\n")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
740 ;;We use set-text-properties to prevent a wacky print function
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
741 ;;from inserting rogue properties, and to ensure that the keymap
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
742 ;;property is inherited (is it a bug that only unpropertied strings
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
743 ;;actually inherit from surrounding text?)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
744 (set-text-properties 0 (length x) nil x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
745 (insert-and-inherit x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
746 (put-text-property startpos (point) 'intangible
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
747 (ses-cell-symbol cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
748 (when (and (zerop row) (zerop col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
749 ;;Reconstruct special beginning-of-buffer attributes
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
750 (put-text-property 1 (point) 'keymap 'ses-mode-print-map)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
751 (put-text-property 1 (point) 'read-only 'ses)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
752 (put-text-property 1 2 'front-sticky t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
753 (if (= row (1- header-row))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
754 ;;This line is part of the header - force recalc
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
755 (ses-reset-header-string))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
756 ;;If this cell (or a preceding one on the line) previously spilled over
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
757 ;;and has gotten shorter, redraw following cells on line recursively.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
758 (when (and (< maxcol numcols) (eq (ses-cell-value row maxcol) '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
759 (ses-set-cell row maxcol 'value nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
760 (ses-print-cell row maxcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
761 ;;Return to start of cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
762 (goto-char startpos)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
763 sig)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
764
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
765 (defun ses-call-printer (printer &optional value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
766 "Invokes PRINTER (a string or parenthesized string or function-symbol or
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
767 lambda of one argument) on VALUE. Result is the the printed cell as a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
768 string. The variable `ses-call-printer-return' is set to t if the printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
769 used parenthesis to request left-justification, or the error-signal if the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
770 printer signalled one (and \"%s\" is used as the default printer), else nil."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
771 (setq ses-call-printer-return nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
772 (unless value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
773 (setq value ""))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
774 (condition-case signal
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
775 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
776 ((stringp printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
777 (format printer value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
778 ((stringp (car-safe printer))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
779 (setq ses-call-printer-return t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
780 (format (car printer) value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
781 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
782 (setq value (funcall printer value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
783 (if (stringp value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
784 value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
785 (or (stringp (car-safe value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
786 (error "Printer should return \"string\" or (\"string\")"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
787 (setq ses-call-printer-return t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
788 (car value))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
789 (error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
790 (setq ses-call-printer-return signal)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
791 (prin1-to-string value t))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
792
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
793 (defun ses-adjust-print-width (col change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
794 "Insert CHANGE spaces in front of column COL, or at end of line if
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
795 COL=NUMCOLS. Deletes characters if CHANGE < 0. Caller should bind
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
796 inhibit-quit to t."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
797 (let ((inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
798 (blank (if (> change 0) (make-string change ? )))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
799 (at-end (= col numcols)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
800 (ses-set-with-undo 'linewidth (+ linewidth change))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
801 ;;ses-set-with-undo always returns t for strings.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
802 (1value (ses-set-with-undo 'blank-line
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
803 (concat (make-string linewidth ? ) "\n")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
804 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
805 (ses-goto-print row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
806 (when at-end
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
807 ;;Insert new columns before newline
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
808 (let ((inhibit-point-motion-hooks t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
809 (backward-char 1)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
810 (if blank
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
811 (insert blank)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
812 (delete-char (- change))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
813
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
814 (defun ses-print-cell-new-width (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
815 "Same as ses-print-cell, except if the cell's value is *skip*, the preceding
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
816 nonskipped cell is reprinted. This function is used when the width of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
817 cell (ROW,COL) has changed."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
818 (if (not (eq (ses-cell-value row col) '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
819 (ses-print-cell row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
820 ;;Cell was skipped over - reprint previous
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
821 (ses-goto-print row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
822 (backward-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
823 (let ((rowcol (ses-sym-rowcol (get-text-property (point) 'intangible))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
824 (ses-print-cell (car rowcol) (cdr rowcol)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
825
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
826
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
827 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
828 ;;;; The data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
829 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
830
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
831 (defun ses-goto-data (def &optional col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
832 "Move point to data area for (DEF,COL). If DEF is a row number, COL is the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
833 column number for a data cell -- otherwise DEF is one of the symbols
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
834 column-widths, col-printers, default-printer, numrows, or numcols."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
835 (if (< (point-max) (buffer-size))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
836 (setq deferred-narrow t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
837 (widen)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
838 (let ((inhibit-point-motion-hooks t)) ;In case intangible attrs are wrong
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
839 (goto-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
840 (if col
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
841 ;;It's a cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
842 (forward-line (+ numrows 2 (* def (1+ numcols)) col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
843 ;;Convert def-symbol to offset
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
844 (setq def (plist-get ses-paramlines-plist def))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
845 (or def (signal 'args-out-of-range nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
846 (forward-line (+ (* numrows (+ numcols 2)) def)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
847
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
848 (defun ses-set-parameter (def value &optional elem)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
849 "Sets parameter DEF to VALUE (with undo) and writes the value to the data
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
850 area. See `ses-goto-data' for meaning of DEF. Newlines in the data
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
851 are escaped. If ELEM is specified, it is the array subscript within DEF to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
852 be set to VALUE."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
853 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
854 ;;We call ses-goto-data early, using the old values of numrows and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
855 ;;numcols in case one of them is being changed.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
856 (ses-goto-data def)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
857 (if elem
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
858 (ses-aset-with-undo (symbol-value def) elem value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
859 (ses-set-with-undo def value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
860 (let ((inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
861 (fmt (plist-get '(column-widths "(ses-column-widths %S)"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
862 col-printers "(ses-column-printers %S)"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
863 default-printer "(ses-default-printer %S)"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
864 header-row "(ses-header-row %S)"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
865 file-format " %S ;SES file-format"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
866 numrows " %S ;numrows"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
867 numcols " %S ;numcols")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
868 def)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
869 (delete-region (point) (line-end-position))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
870 (insert (format fmt (symbol-value def))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
871
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
872 (defun ses-write-cells ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
873 "`deferred-write' is a list of (ROW,COL) for cells to be written from
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
874 buffer-local variables to data area. Newlines in the data are escaped."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
875 (let* ((inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
876 (print-escape-newlines t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
877 rowcol row col cell sym formula printer text)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
878 (setq ses-start-time (float-time))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
879 (with-temp-message " "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
880 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
881 (while deferred-write
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
882 (ses-time-check "Writing... (%d cells left)"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
883 '(length deferred-write))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
884 (setq rowcol (pop deferred-write)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
885 row (car rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
886 col (cdr rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
887 cell (ses-get-cell row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
888 sym (ses-cell-symbol cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
889 formula (ses-cell-formula cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
890 printer (ses-cell-printer cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
891 (if (eq (car-safe formula) 'ses-safe-formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
892 (setq formula (cadr formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
893 (if (eq (car-safe printer) 'ses-safe-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
894 (setq printer (cadr printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
895 ;;This is noticably faster than (format "%S %S %S %S %S")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
896 (setq text (concat "(ses-cell "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
897 (symbol-name sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
898 " "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
899 (prin1-to-string (symbol-value sym))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
900 " "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
901 (prin1-to-string formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
902 " "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
903 (prin1-to-string printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
904 " "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
905 (if (atom (ses-cell-references cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
906 "nil"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
907 (concat "("
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
908 (mapconcat 'symbol-name
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
909 (ses-cell-references cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
910 " ")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
911 ")"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
912 ")"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
913 (ses-goto-data row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
914 (delete-region (point) (line-end-position))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
915 (insert text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
916 (message " "))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
917
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
918
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
919 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
920 ;;;; Formula relocation
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
921 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
922
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
923 (defun ses-formula-references (formula &optional result-so-far)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
924 "Produce a list of symbols for cells that this formula's value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
925 refers to. For recursive calls, RESULT-SO-FAR is the list being constructed,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
926 or t to get a wrong-type-argument error when the first reference is found."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
927 (if (atom formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
928 (if (ses-sym-rowcol formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
929 ;;Entire formula is one symbol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
930 (add-to-list 'result-so-far formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
931 ) ;;Ignore other atoms
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
932 (dolist (cur formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
933 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
934 ((ses-sym-rowcol cur)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
935 ;;Save this reference
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
936 (add-to-list 'result-so-far cur))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
937 ((eq (car-safe cur) 'ses-range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
938 ;;All symbols in range are referenced
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
939 (dolist (x (cdr (macroexpand cur)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
940 (add-to-list 'result-so-far x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
941 ((and (consp cur) (not (eq (car cur) 'quote)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
942 ;;Recursive call for subformulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
943 (setq result-so-far (ses-formula-references cur result-so-far)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
944 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
945 ;;Ignore other stuff
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
946 ))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
947 result-so-far)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
948
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
949 (defun ses-relocate-formula (formula startrow startcol rowincr colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
950 "Produce a copy of FORMULA where all symbols that refer to cells in row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
951 STARTROW or above and col STARTCOL or above are altered by adding ROWINCR
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
952 and COLINCR. STARTROW and STARTCOL are 0-based. Example:
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
953 (ses-relocate-formula '(+ A1 B2 D3) 1 2 1 -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
954 => (+ A1 B2 C4)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
955 If ROWINCR or COLINCR is negative, references to cells being deleted are
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
956 removed. Example:
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
957 (ses-relocate-formula '(+ A1 B2 D3) 0 1 0 -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
958 => (+ A1 C3)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
959 Sets `ses-relocate-return' to 'delete if cell-references were removed."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
960 (let (rowcol result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
961 (if (or (atom formula) (eq (car formula) 'quote))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
962 (if (setq rowcol (ses-sym-rowcol formula))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
963 (ses-relocate-symbol formula rowcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
964 startrow startcol rowincr colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
965 formula) ;Pass through as-is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
966 (dolist (cur formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
967 (setq rowcol (ses-sym-rowcol cur))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
968 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
969 (rowcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
970 (setq cur (ses-relocate-symbol cur rowcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
971 startrow startcol rowincr colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
972 (if cur
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
973 (push cur result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
974 ;;Reference to a deleted cell. Set a flag in ses-relocate-return.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
975 ;;don't change the flag if it's already 'range, since range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
976 ;;implies 'delete.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
977 (unless ses-relocate-return
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
978 (setq ses-relocate-return 'delete))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
979 ((eq (car-safe cur) 'ses-range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
980 (setq cur (ses-relocate-range cur startrow startcol rowincr colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
981 (if cur
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
982 (push cur result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
983 ((or (atom cur) (eq (car cur) 'quote))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
984 ;;Constants pass through unchanged
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
985 (push cur result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
986 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
987 ;;Recursively copy and alter subformulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
988 (push (ses-relocate-formula cur startrow startcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
989 rowincr colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
990 result))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
991 (nreverse result))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
992
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
993 (defun ses-relocate-symbol (sym rowcol startrow startcol rowincr colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
994 "Relocate one symbol SYM, whichs corresponds to ROWCOL (a cons of ROW and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
995 COL). Cells starting at (STARTROW,STARTCOL) are being shifted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
996 by (ROWINCR,COLINCR)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
997 (let ((row (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
998 (col (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
999 (if (or (< row startrow) (< col startcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1000 sym
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1001 (setq row (+ row rowincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1002 col (+ col colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1003 (if (and (>= row startrow) (>= col startcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1004 (< row numrows) (< col numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1005 ;;Relocate this variable
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1006 (ses-create-cell-symbol row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1007 ;;Delete reference to a deleted cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1008 nil))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1009
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1010 (defun ses-relocate-range (range startrow startcol rowincr colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1011 "Relocate one RANGE, of the form '(ses-range min max). Cells starting
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1012 at (STARTROW,STARTCOL) are being shifted by (ROWINCR,COLINCR). Result is the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1013 new range, or nil if the entire range is deleted. If new rows are being added
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1014 just beyond the end of a row range, or new columns just beyond a column range,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1015 the new rows/columns will be added to the range. Sets `ses-relocate-return'
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1016 if the range was altered."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1017 (let* ((minorig (cadr range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1018 (minrowcol (ses-sym-rowcol minorig))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1019 (min (ses-relocate-symbol minorig minrowcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1020 startrow startcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1021 rowincr colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1022 (maxorig (nth 2 range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1023 (maxrowcol (ses-sym-rowcol maxorig))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1024 (max (ses-relocate-symbol maxorig maxrowcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1025 startrow startcol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1026 rowincr colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1027 field)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1028 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1029 ((and (not min) (not max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1030 (setq range nil)) ;;The entire range is deleted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1031 ((zerop colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1032 ;;Inserting or deleting rows
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1033 (setq field 'car)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1034 (if (not min)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1035 ;;Chopped off beginning of range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1036 (setq min (ses-create-cell-symbol startrow (cdr minrowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1037 ses-relocate-return 'range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1038 (if (not max)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1039 (if (> rowincr 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1040 ;;Trying to insert a nonexistent row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1041 (setq max (ses-create-cell-symbol (1- numrows) (cdr minrowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1042 ;;End of range is being deleted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1043 (setq max (ses-create-cell-symbol (1- startrow) (cdr minrowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1044 ses-relocate-return 'range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1045 (and (> rowincr 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1046 (= (car maxrowcol) (1- startrow))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1047 (= (cdr minrowcol) (cdr maxrowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1048 ;;Insert after ending row of vertical range - include it
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1049 (setq max (ses-create-cell-symbol (+ startrow rowincr -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1050 (cdr maxrowcol))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1051 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1052 ;;Inserting or deleting columns
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1053 (setq field 'cdr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1054 (if (not min)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1055 ;;Chopped off beginning of range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1056 (setq min (ses-create-cell-symbol (car minrowcol) startcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1057 ses-relocate-return 'range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1058 (if (not max)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1059 (if (> colincr 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1060 ;;Trying to insert a nonexistent column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1061 (setq max (ses-create-cell-symbol (car maxrowcol) (1- numcols)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1062 ;;End of range is being deleted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1063 (setq max (ses-create-cell-symbol (car maxrowcol) (1- startcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1064 ses-relocate-return 'range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1065 (and (> colincr 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1066 (= (cdr maxrowcol) (1- startcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1067 (= (car minrowcol) (car maxrowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1068 ;;Insert after ending column of horizontal range - include it
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1069 (setq max (ses-create-cell-symbol (car maxrowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1070 (+ startcol colincr -1)))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1071 (when range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1072 (if (/= (- (funcall field maxrowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1073 (funcall field minrowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1074 (- (funcall field (ses-sym-rowcol max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1075 (funcall field (ses-sym-rowcol min))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1076 ;;This range has changed size
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1077 (setq ses-relocate-return 'range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1078 (list 'ses-range min max))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1079
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1080 (defun ses-relocate-all (minrow mincol rowincr colincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1081 "Alter all cell values, symbols, formulas, and reference-lists to relocate
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1082 the rectangle (MINROW,MINCOL)..(NUMROWS,NUMCOLS) by adding ROWINCR and COLINCR
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1083 to each symbol."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1084 (let (reform)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1085 (let (mycell newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1086 (ses-dotimes-msg (row numrows) "Relocating formulas..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1087 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1088 (setq ses-relocate-return nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1089 mycell (ses-get-cell row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1090 newval (ses-relocate-formula (ses-cell-formula mycell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1091 minrow mincol rowincr colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1092 (ses-set-cell row col 'formula newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1093 (if (eq ses-relocate-return 'range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1094 ;;This cell contains a (ses-range X Y) where a cell has been
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1095 ;;inserted or deleted in the middle of the range.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1096 (push (cons row col) reform))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1097 (if ses-relocate-return
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1098 ;;This cell referred to a cell that's been deleted or is no
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1099 ;;longer part of the range. We can't fix that now because
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1100 ;;reference lists cells have been partially updated.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1101 (add-to-list 'deferred-recalc
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1102 (ses-create-cell-symbol row col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1103 (setq newval (ses-relocate-formula (ses-cell-references mycell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1104 minrow mincol rowincr colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1105 (ses-set-cell row col 'references newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1106 (and (>= row minrow) (>= col mincol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1107 (ses-set-cell row col 'symbol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1108 (ses-create-cell-symbol row col))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1109 ;;Relocate the cell values
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1110 (let (oldval myrow mycol xrow xcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1111 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1112 ((and (<= rowincr 0) (<= colincr 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1113 ;;Deletion of rows and/or columns
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1114 (ses-dotimes-msg (row (- numrows minrow)) "Relocating variables..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1115 (setq myrow (+ row minrow))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1116 (dotimes (col (- numcols mincol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1117 (setq mycol (+ col mincol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1118 xrow (- myrow rowincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1119 xcol (- mycol colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1120 (if (and (< xrow numrows) (< xcol numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1121 (setq oldval (ses-cell-value xrow xcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1122 ;;Cell is off the end of the array
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1123 (setq oldval (symbol-value (ses-create-cell-symbol xrow xcol))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1124 (ses-set-cell myrow mycol 'value oldval))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1125 ((and (wholenump rowincr) (wholenump colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1126 ;;Insertion of rows and/or columns. Run the loop backwards.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1127 (let ((disty (1- numrows))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1128 (distx (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1129 myrow mycol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1130 (ses-dotimes-msg (row (- numrows minrow)) "Relocating variables..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1131 (setq myrow (- disty row))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1132 (dotimes (col (- numcols mincol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1133 (setq mycol (- distx col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1134 xrow (- myrow rowincr)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1135 xcol (- mycol colincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1136 (if (or (< xrow minrow) (< xcol mincol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1137 ;;Newly-inserted value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1138 (setq oldval nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1139 ;;Transfer old value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1140 (setq oldval (ses-cell-value xrow xcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1141 (ses-set-cell myrow mycol 'value oldval)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1142 t)) ;Make testcover happy by returning non-nil here
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1143 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1144 (error "ROWINCR and COLINCR must have the same sign"))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1145 ;;Reconstruct reference lists for cells that contain ses-ranges that
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1146 ;;have changed size.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1147 (when reform
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1148 (message "Fixing ses-ranges...")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1149 (let (row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1150 (setq ses-start-time (float-time))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1151 (while reform
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1152 (ses-time-check "Fixing ses-ranges... (%d left)" '(length reform))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1153 (setq row (caar reform)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1154 col (cdar reform)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1155 reform (cdr reform))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1156 (ses-cell-set-formula row col (ses-cell-formula row col))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1157 (message nil))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1158
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1159
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1160 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1161 ;;;; Undo control
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1162 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1163
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1164 (defadvice undo-more (around ses-undo-more activate preactivate)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1165 "Define a meaning for conses in buffer-undo-list whose car is a symbol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1166 other than t or nil. To undo these, apply the car--a function--to the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1167 cdr--its arglist."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1168 (let ((ses-count (ad-get-arg 0)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1169 (catch 'undo
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1170 (dolist (ses-x pending-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1171 (unless ses-x
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1172 ;;End of undo boundary
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1173 (setq ses-count (1- ses-count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1174 (if (<= ses-count 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1175 ;;We've seen enough boundaries - stop undoing
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1176 (throw 'undo nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1177 (and (consp ses-x) (symbolp (car ses-x)) (fboundp (car ses-x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1178 ;;Undo using apply
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1179 (apply (car ses-x) (cdr ses-x)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1180 (if (not (eq major-mode 'ses-mode))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1181 ad-do-it
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1182 ;;Here is some extra code for SES mode.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1183 (setq deferred-narrow (or deferred-narrow (< (point-max) (buffer-size))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1184 (widen)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1185 (condition-case x
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1186 ad-do-it
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1187 (error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1188 ;;Restore narrow if appropriate
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1189 (ses-command-hook)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1190 (signal (car x) (cdr x)))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1191
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1192 (defun ses-begin-change ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1193 "For undo, remember current buffer-position before we start changing hidden
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1194 stuff."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1195 (let ((inhibit-read-only t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1196 (insert-and-inherit "X")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1197 (delete-region (1- (point)) (point))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1198
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1199 (defun ses-set-with-undo (sym newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1200 "Like set, but undoable. Result is t if value has changed."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1201 ;;We avoid adding redundant entries to the undo list, but this is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1202 ;;unavoidable for strings because equal ignores text properties and there's
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1203 ;;no easy way to get the whole property list to see if it's different!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1204 (unless (and (boundp sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1205 (equal (symbol-value sym) newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1206 (not (stringp newval)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1207 (push (if (boundp sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1208 `(ses-set-with-undo ,sym ,(symbol-value sym))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1209 `(ses-unset-with-undo ,sym))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1210 buffer-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1211 (set sym newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1212 t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1213
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1214 (defun ses-unset-with-undo (sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1215 "Set SYM to be unbound. This is undoable."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1216 (when (1value (boundp sym)) ;;Always bound, except after a programming error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1217 (push `(ses-set-with-undo ,sym ,(symbol-value sym)) buffer-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1218 (makunbound sym)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1219
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1220 (defun ses-aset-with-undo (array idx newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1221 "Like aset, but undoable. Result is t if element has changed"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1222 (unless (equal (aref array idx) newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1223 (push `(ses-aset-with-undo ,array ,idx ,(aref array idx)) buffer-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1224 (aset array idx newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1225 t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1226
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1227
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1228 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1229 ;;;; Startup for major mode
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1230 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1231
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1232 (defun ses-build-mode-map ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1233 "Set up `ses-mode-map', `ses-mode-print-map', and `ses-mode-edit-map' with
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1234 standard keymap bindings for SES."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1235 (message "Building mode map...")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1236 ;;;Define ses-mode-map
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1237 (let ((keys '("\C-c\M-\C-l" ses-reconstruct-all
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1238 "\C-c\C-l" ses-recalculate-all
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1239 "\C-c\C-n" ses-renarrow-buffer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1240 "\C-c\C-c" ses-recalculate-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1241 "\C-c\M-\C-s" ses-sort-column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1242 "\C-c\M-\C-h" ses-read-header-row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1243 "\C-c\C-t" ses-truncate-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1244 "\C-c\C-j" ses-jump
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1245 "\C-c\C-p" ses-read-default-printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1246 "\M-\C-l" ses-reprint-all
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1247 [?\S-\C-l] ses-reprint-all
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1248 [header-line mouse-2] ses-sort-column-click))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1249 (newmap (make-sparse-keymap)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1250 (while keys
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1251 (define-key (1value newmap) (car keys) (cadr keys))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1252 (setq keys (cddr keys)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1253 (setq ses-mode-map (1value newmap)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1254 ;;;Define ses-mode-print-map
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1255 (let ((keys '(;;At least three ways to define shift-tab--and some PC systems
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1256 ;;won't generate it at all!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1257 [S-tab] backward-char
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1258 [backtab] backward-char
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1259 [S-iso-backtab] backward-char
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1260 [S-iso-lefttab] backward-char
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1261 [tab] ses-forward-or-insert
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1262 "\C-i" ses-forward-or-insert ;Needed for ses-coverage.el?
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1263 "\M-o" ses-insert-column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1264 "\C-o" ses-insert-row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1265 "\C-m" ses-edit-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1266 "\M-k" ses-delete-column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1267 "\M-y" ses-yank-pop
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1268 "\C-k" ses-delete-row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1269 "\C-j" ses-append-row-jump-first-column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1270 "\M-h" ses-mark-row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1271 "\M-H" ses-mark-column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1272 "\C-d" ses-clear-cell-forward
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1273 "\C-?" ses-clear-cell-backward
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1274 "(" ses-read-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1275 "\"" ses-read-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1276 "'" ses-read-symbol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1277 "=" ses-edit-cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1278 "j" ses-jump
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1279 "p" ses-read-cell-printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1280 "w" ses-set-column-width
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1281 "x" ses-export-keymap
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1282 "\M-p" ses-read-column-printer))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1283 (repl '(;;We'll replace these wherever they appear in the keymap
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1284 clipboard-kill-region ses-kill-override
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1285 end-of-line ses-end-of-line
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1286 kill-line ses-delete-row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1287 kill-region ses-kill-override
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1288 open-line ses-insert-row))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1289 (numeric "0123456789.-")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1290 (newmap (make-keymap)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1291 ;;Get rid of printables
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1292 (suppress-keymap (1value newmap) t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1293 ;;These keys insert themselves as the beginning of a numeric value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1294 (dotimes (x (length (1value numeric)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1295 (define-key (1value newmap)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1296 (substring (1value numeric) x (1+ x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1297 'ses-read-cell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1298 ;;Override these global functions wherever they're bound
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1299 (while repl
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1300 (substitute-key-definition (car repl) (cadr repl)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1301 (1value newmap)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1302 (current-global-map))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1303 (setq repl (cddr repl)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1304 ;;Apparently substitute-key-definition doesn't catch this?
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1305 (define-key (1value newmap) [(menu-bar) edit cut] 'ses-kill-override)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1306 ;;Define our other local keys
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1307 (while keys
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1308 (define-key (1value newmap) (car keys) (cadr keys))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1309 (setq keys (cddr keys)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1310 ;;Keymap property wants the map as a function, not a variable
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1311 (fset 'ses-mode-print-map (1value newmap))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1312 (setq ses-mode-print-map (1value newmap)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1313 ;;;Define ses-mode-edit-map
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1314 (let ((keys '("\C-c\C-r" ses-insert-range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1315 "\C-c\C-s" ses-insert-ses-range
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1316 [S-mouse-3] ses-insert-range-click
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1317 [C-S-mouse-3] ses-insert-ses-range-click
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1318 "\M-\C-i" lisp-complete-symbol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1319 (newmap (make-sparse-keymap)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1320 (1value (set-keymap-parent (1value newmap) (1value minibuffer-local-map)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1321 (while keys
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1322 (define-key (1value newmap) (car keys) (cadr keys))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1323 (setq keys (cddr keys)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1324 (setq ses-mode-edit-map (1value newmap)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1325 (message nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1326
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1327 (defun ses-load ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1328 "Parse the current buffer and sets up buffer-local variables. Does not
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1329 execute cell formulas or print functions."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1330 (widen)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1331 ;;Read our global parameters, which should be a 3-element list
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1332 (goto-char (point-max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1333 (search-backward ";;; Local Variables:\n" nil t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1334 (backward-list 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1335 (let ((params (condition-case nil (read (current-buffer)) (error nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1336 sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1337 (or (and (= (safe-length params) 3)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1338 (numberp (car params))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1339 (numberp (cadr params))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1340 (> (cadr params) 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1341 (numberp (nth 2 params))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1342 (> (nth 2 params) 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1343 (error "Invalid SES file"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1344 (setq file-format (car params)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1345 numrows (cadr params)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1346 numcols (nth 2 params))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1347 (when (= file-format 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1348 (let (buffer-undo-list) ;This is not undoable
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1349 (ses-goto-data 'header-row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1350 (insert "(ses-header-row 0)\n")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1351 (ses-set-parameter 'file-format 2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1352 (message "Upgrading from SES-1 file format")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1353 (or (= file-format 2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1354 (error "This file needs a newer version of the SES library code."))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1355 (ses-create-cell-variable-range 0 (1- numrows) 0 (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1356 ;;Initialize cell array
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1357 (setq cells (make-vector numrows nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1358 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1359 (aset cells row (make-vector numcols nil))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1360 ;;Skip over print area, which we assume is correct
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1361 (goto-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1362 (forward-line numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1363 (or (looking-at ses-print-data-boundary)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1364 (error "Missing marker between print and data areas"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1365 (forward-char (length ses-print-data-boundary))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1366 ;;Initialize printer and symbol lists
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1367 (mapc 'ses-printer-record ses-standard-printer-functions)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1368 (setq symbolic-formulas nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1369 ;;Load cell definitions
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1370 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1371 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1372 (let* ((x (read (current-buffer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1373 (rowcol (ses-sym-rowcol (car-safe (cdr-safe x)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1374 (or (and (looking-at "\n")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1375 (eq (car-safe x) 'ses-cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1376 (eq row (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1377 (eq col (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1378 (error "Cell-def error"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1379 (eval x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1380 (or (looking-at "\n\n")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1381 (error "Missing blank line between rows")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1382 ;;Load global parameters
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1383 (let ((widths (read (current-buffer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1384 (n1 (char-after (point)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1385 (printers (read (current-buffer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1386 (n2 (char-after (point)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1387 (def-printer (read (current-buffer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1388 (n3 (char-after (point)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1389 (head-row (read (current-buffer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1390 (n4 (char-after (point))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1391 (or (and (eq (car-safe widths) 'ses-column-widths)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1392 (= n1 ?\n)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1393 (eq (car-safe printers) 'ses-column-printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1394 (= n2 ?\n)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1395 (eq (car-safe def-printer) 'ses-default-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1396 (= n3 ?\n)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1397 (eq (car-safe head-row) 'ses-header-row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1398 (= n4 ?\n))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1399 (error "Invalid SES global parameters"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1400 (1value (eval widths))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1401 (1value (eval def-printer))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1402 (1value (eval printers))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1403 (1value (eval head-row)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1404 ;;Should be back at global-params
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1405 (forward-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1406 (or (looking-at (replace-regexp-in-string "1" "[0-9]+"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1407 ses-initial-global-parameters))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1408 (error "Problem with column-defs or global-params"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1409 ;;Check for overall newline count in definitions area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1410 (forward-line 3)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1411 (let ((start (point)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1412 (ses-goto-data 'numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1413 (or (= (point) start)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1414 (error "Extraneous newlines someplace?"))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1415
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1416 (defun ses-setup ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1417 "Set up for display of only the printed cell values.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1418
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1419 Narrows the buffer to show only the print area. Gives it `read-only' and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1420 `intangible' properties. Sets up highlighting for current cell."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1421 (interactive)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1422 (let ((end 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1423 (inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1424 (was-modified (buffer-modified-p))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1425 pos sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1426 (ses-goto-data 0 0) ;;Include marker between print-area and data-area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1427 (set-text-properties (point) (buffer-size) nil) ;Delete garbage props
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1428 (mapc 'delete-overlay (overlays-in 1 (buffer-size)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1429 ;;The print area is read-only (except for our special commands) and uses a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1430 ;;special keymap.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1431 (put-text-property 1 (1- (point)) 'read-only 'ses)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1432 (put-text-property 1 (1- (point)) 'keymap 'ses-mode-print-map)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1433 ;;For the beginning of the buffer, we want the read-only and keymap
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1434 ;;attributes to be inherited from the first character
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1435 (put-text-property 1 2 'front-sticky t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1436 ;;Create intangible properties, which also indicate which cell the text
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1437 ;;came from.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1438 (ses-dotimes-msg (row numrows) "Finding cells..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1439 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1440 (setq pos end
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1441 sym (ses-cell-symbol row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1442 ;;Include skipped cells following this one
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1443 (while (and (< col (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1444 (eq (ses-cell-value row (1+ col)) '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1445 (setq end (+ end (ses-col-width col) 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1446 col (1+ col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1447 (setq end (+ end (ses-col-width col) 1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1448 (put-text-property pos end 'intangible sym)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1449 ;;Adding these properties did not actually alter the text
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1450 (unless was-modified
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1451 (set-buffer-modified-p nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1452 (buffer-disable-undo)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1453 (buffer-enable-undo)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1454 ;;Create the underlining overlay. It's impossible for (point) to be 2,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1455 ;;because column A must be at least 1 column wide.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1456 (setq curcell-overlay (make-overlay 2 2))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1457 (overlay-put curcell-overlay 'face 'underline))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1458
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1459 (defun ses-cleanup ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1460 "Cleanup when changing a buffer from SES mode to something else. Delete
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1461 overlay, remove special text properties."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1462 (widen)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1463 (let ((inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1464 (was-modified (buffer-modified-p))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1465 end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1466 ;;Delete read-only, keymap, and intangible properties
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1467 (set-text-properties 1 (point-max) nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1468 ;;Delete overlay
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1469 (mapc 'delete-overlay (overlays-in 1 (point-max)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1470 (unless was-modified
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1471 (set-buffer-modified-p nil))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1472
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1473 ;;;###autoload
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1474 (defun ses-mode ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1475 "Major mode for Simple Emacs Spreadsheet. See \"ses-readme.txt\" for more info.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1476
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1477 Key definitions:
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1478 \\{ses-mode-map}
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1479 These key definitions are active only in the print area (the visible part):
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1480 \\{ses-mode-print-map}
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1481 These are active only in the minibuffer, when entering or editing a formula:
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1482 \\{ses-mode-edit-map}"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1483 (interactive)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1484 (unless (and (boundp 'deferred-narrow)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1485 (eq deferred-narrow 'ses-mode))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1486 (kill-all-local-variables)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1487 (mapc 'make-local-variable ses-localvars)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1488 (setq major-mode 'ses-mode
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1489 mode-name "SES"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1490 next-line-add-newlines nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1491 truncate-lines t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1492 ;;SES deliberately puts lots of trailing whitespace in its buffer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1493 show-trailing-whitespace nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1494 ;;Cell ranges do not work reasonably without this
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1495 transient-mark-mode t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1496 (unless (and ses-mode-map ses-mode-print-map ses-mode-edit-map)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1497 (ses-build-mode-map))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1498 (1value (add-hook 'change-major-mode-hook 'ses-cleanup nil t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1499 (1value (add-hook 'before-revert-hook 'ses-cleanup nil t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1500 (setq curcell nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1501 deferred-recalc nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1502 deferred-write nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1503 header-hscroll -1 ;Flag for "initial recalc needed"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1504 header-line-format '(:eval (progn
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1505 (when (/= (window-hscroll)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1506 header-hscroll)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1507 ;;Reset header-hscroll first, to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1508 ;;avoid recursion problems when
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1509 ;;debugging ses-create-header-string
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1510 (setq header-hscroll (window-hscroll))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1511 (ses-create-header-string))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1512 header-string)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1513 (let ((was-empty (zerop (buffer-size)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1514 (was-modified (buffer-modified-p)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1515 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1516 (if was-empty
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1517 ;;Initialize buffer to contain one cell, for now
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1518 (insert ses-initial-file-contents))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1519 (ses-load)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1520 (ses-setup))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1521 (when was-empty
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1522 (unless (equal ses-initial-default-printer (1value default-printer))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1523 (1value (ses-read-default-printer ses-initial-default-printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1524 (unless (= ses-initial-column-width (1value (ses-col-width 0)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1525 (1value (ses-set-column-width 0 ses-initial-column-width)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1526 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1527 (if (> (car ses-initial-size) (1value numrows))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1528 (1value (ses-insert-row (1- (car ses-initial-size)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1529 (if (> (cdr ses-initial-size) (1value numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1530 (1value (ses-insert-column (1- (cdr ses-initial-size)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1531 (ses-write-cells)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1532 (set-buffer-modified-p was-modified)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1533 (buffer-disable-undo)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1534 (buffer-enable-undo)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1535 (goto-char 1)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1536 (use-local-map ses-mode-map)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1537 ;;Set the deferred narrowing flag (we can't narrow until after
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1538 ;;after-find-file completes). If .ses is on the auto-load alist and the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1539 ;;file has "mode: ses", our ses-mode function will be called twice! Use
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1540 ;;a special flag to detect this (will be reset by ses-command-hook).
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1541 ;;For find-alternate-file, post-command-hook doesn't get run for some
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1542 ;;reason, so use an idle timer to make sure.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1543 (setq deferred-narrow 'ses-mode)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1544 (1value (add-hook 'post-command-hook 'ses-command-hook nil t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1545 (run-with-idle-timer 0.01 nil 'ses-command-hook)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1546 (run-hooks 'ses-mode-hook)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1547
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1548 (put 'ses-mode 'mode-class 'special)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1549
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1550 (defun ses-command-hook ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1551 "Invoked from `post-command-hook'. If point has moved to a different cell,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1552 moves the underlining overlay. Performs any recalculations or cell-data
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1553 writes that have been deferred. If buffer-narrowing has been deferred,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1554 narrows the buffer now."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1555 (condition-case err
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1556 (when (eq major-mode 'ses-mode) ;Otherwise, not our buffer anymore
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1557 (when deferred-recalc
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1558 ;;We reset the deferred list before starting on the recalc -- in case
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1559 ;;of error, we don't want to retry the recalc after every keystroke!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1560 (let ((old deferred-recalc))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1561 (setq deferred-recalc nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1562 (ses-update-cells old)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1563 (if deferred-write
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1564 ;;We don't reset the deferred list before starting -- the most
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1565 ;;likely error is keyboard-quit, and we do want to keep trying
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1566 ;;these writes after a quit.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1567 (ses-write-cells))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1568 (when deferred-narrow
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1569 ;;We're not allowed to narrow the buffer until after-find-file has
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1570 ;;read the local variables at the end of the file. Now it's safe to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1571 ;;do the narrowing.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1572 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1573 (goto-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1574 (forward-line numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1575 (narrow-to-region 1 (point)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1576 (setq deferred-narrow nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1577 ;;Update the modeline
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1578 (let ((oldcell curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1579 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1580 (unless (eq curcell oldcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1581 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1582 ((not curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1583 (setq mode-line-process nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1584 ((atom curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1585 (setq mode-line-process (list " cell " (symbol-name curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1586 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1587 (setq mode-line-process (list " range "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1588 (symbol-name (car curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1589 "-"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1590 (symbol-name (cdr curcell))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1591 (force-mode-line-update)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1592 ;;Use underline overlay for single-cells only, turn off otherwise
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1593 (if (listp curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1594 (move-overlay curcell-overlay 2 2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1595 (let ((next (next-single-property-change (point) 'intangible)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1596 (move-overlay curcell-overlay (point) (1- next))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1597 (when (not (pos-visible-in-window-p))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1598 ;;Scrolling will happen later
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1599 (run-with-idle-timer 0.01 nil 'ses-command-hook)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1600 (setq curcell t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1601 ;;Prevent errors in this post-command-hook from silently erasing the hook!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1602 (error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1603 (unless executing-kbd-macro
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1604 (ding))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1605 (message (error-message-string err))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1606 nil) ;Make coverage-tester happy
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1607
49573
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1608 (defun ses-header-string-left-offset ()
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1609 "Number of characters in left fringe and left scrollbar (if any)."
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1610 (let ((left-fringe (round (or (frame-parameter nil 'left-fringe) 0)
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1611 (frame-char-width)))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1612 (left-scrollbar (if (not (eq (frame-parameter nil
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1613 'vertical-scroll-bars)
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1614 'left))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1615 0
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1616 (let ((x (frame-parameter nil 'scroll-bar-width)))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1617 ;;Non-toolkil bar is always 14 pixels?
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1618 (unless x (setq x 14))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1619 ;;Always round up
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1620 (ceiling x (frame-char-width))))))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1621 (+ left-fringe left-scrollbar)))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1622
47667
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1623 (defun ses-create-header-string ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1624 "Sets up `header-string' as the buffer's header line, based on the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1625 current set of columns and window-scroll position."
49573
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1626 (let* ((left-offset (ses-header-string-left-offset))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1627 (totwidth (- left-offset (window-hscroll)))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1628 result width result x)
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1629 ;;Leave room for the left-side fringe and scrollbar
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1630 (push (make-string left-offset ? ) result)
47667
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1631 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1632 (setq width (ses-col-width col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1633 totwidth (+ totwidth width 1))
49573
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1634 (if (= totwidth (+ left-offset 1))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1635 ;;Scrolled so intercolumn space is leftmost
47667
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1636 (push " " result))
49573
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1637 (when (> totwidth (+ left-offset 1))
47667
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1638 (if (> header-row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1639 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1640 (ses-goto-print (1- header-row) col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1641 (setq x (buffer-substring-no-properties (point)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1642 (+ (point) width)))
49573
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1643 (if (>= width (- totwidth left-offset))
3261228348b4 Fix header-line when scrollbar is on left and/or fringe isn't one char wide.
Jonathan Yavner <jyavner@member.fsf.org>
parents: 47667
diff changeset
1644 (setq x (substring x (- width totwidth left-offset -1))))
47667
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1645 (push (propertize x 'face ses-box-prop) result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1646 (setq x (ses-column-letter col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1647 (push (propertize x 'face ses-box-prop) result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1648 (push (propertize (make-string (- width (length x)) ?.)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1649 'display `((space :align-to ,(1- totwidth)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1650 'face ses-box-prop)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1651 result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1652 ;;Allow the following space to be squished to make room for the 3-D box
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1653 ;;Coverage test ignores properties, thinks this is always a space!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1654 (push (1value (propertize " " 'display `((space :align-to ,totwidth))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1655 result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1656 (if (> header-row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1657 (push (propertize (format " [row %d]" header-row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1658 'display '((height (- 1))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1659 result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1660 (setq header-string (apply 'concat (nreverse result)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1661
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1662
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1663 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1664 ;;;; Redisplay and recalculation
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1665 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1666
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1667 (defun ses-jump (sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1668 "Move point to cell SYM."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1669 (interactive "SJump to cell: ")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1670 (let ((rowcol (ses-sym-rowcol sym)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1671 (or rowcol (error "Invalid cell name"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1672 (if (eq (symbol-value sym) '*skip*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1673 (error "Cell is covered by preceding cell"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1674 (ses-goto-print (car rowcol) (cdr rowcol))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1675
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1676 (defun ses-jump-safe (cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1677 "Like `ses-jump', but no error if invalid cell."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1678 (condition-case nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1679 (ses-jump cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1680 (error)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1681
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1682 (defun ses-reprint-all (&optional nonarrow)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1683 "Recreate the display area. Calls all printer functions. Narrows to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1684 print area if NONARROW is nil."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1685 (interactive "*P")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1686 (widen)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1687 (unless nonarrow
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1688 (setq deferred-narrow t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1689 (let ((startcell (get-text-property (point) 'intangible))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1690 (inhibit-read-only t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1691 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1692 (goto-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1693 (search-forward ses-print-data-boundary)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1694 (backward-char (length ses-print-data-boundary))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1695 (delete-region 1 (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1696 ;;Insert all blank lines before printing anything, so ses-print-cell can
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1697 ;;find the data area when inserting or deleting *skip* values for cells
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1698 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1699 (insert-and-inherit blank-line))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1700 (ses-dotimes-msg (row numrows) "Reprinting..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1701 (if (eq (ses-cell-value row 0) '*skip*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1702 ;;Column deletion left a dangling skip
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1703 (ses-set-cell row 0 'value nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1704 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1705 (ses-print-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1706 (beginning-of-line 2))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1707 (ses-jump-safe startcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1708
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1709 (defun ses-recalculate-cell ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1710 "Recalculate and reprint the current cell or range.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1711
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1712 For an individual cell, shows the error if the formula or printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1713 signals one, or otherwise shows the cell's complete value. For a range, the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1714 cells are recalculated in \"natural\" order, so cells that other cells refer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1715 to are recalculated first."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1716 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1717 (ses-check-curcell 'range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1718 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1719 (let (sig)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1720 (setq ses-start-time (float-time))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1721 (if (atom curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1722 (setq sig (ses-sym-rowcol curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1723 sig (ses-calculate-cell (car sig) (cdr sig) t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1724 ;;First, recalculate all cells that don't refer to other cells and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1725 ;;produce a list of cells with references.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1726 (ses-dorange curcell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1727 (ses-time-check "Recalculating... %s" '(ses-cell-symbol row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1728 (condition-case nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1729 (progn
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1730 ;;The t causes an error if the cell has references.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1731 ;;If no references, the t will be the result value.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1732 (1value (ses-formula-references (ses-cell-formula row col) t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1733 (setq sig (ses-calculate-cell row col t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1734 (wrong-type-argument
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1735 ;;The formula contains a reference
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1736 (add-to-list 'deferred-recalc (ses-cell-symbol row col))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1737 ;;Do the update now, so we can force recalculation
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1738 (let ((x deferred-recalc))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1739 (setq deferred-recalc nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1740 (condition-case hold
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1741 (ses-update-cells x t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1742 (error (setq sig hold))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1743 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1744 (sig
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1745 (message (error-message-string sig)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1746 ((consp curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1747 (message " "))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1748 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1749 (princ (symbol-value curcell))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1750
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1751 (defun ses-recalculate-all ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1752 "Recalculate and reprint all cells."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1753 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1754 (let ((startcell (get-text-property (point) 'intangible))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1755 (curcell (cons 'A1 (ses-cell-symbol (1- numrows) (1- numcols)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1756 (ses-recalculate-cell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1757 (ses-jump-safe startcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1758
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1759 (defun ses-truncate-cell ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1760 "Reprint current cell, but without spillover into any following blank
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1761 cells."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1762 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1763 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1764 (let* ((rowcol (ses-sym-rowcol curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1765 (row (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1766 (col (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1767 (when (and (< col (1- numcols)) ;;Last column can't spill over, anyway
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1768 (eq (ses-cell-value row (1+ col)) '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1769 ;;This cell has spill-over. We'll momentarily pretend the following
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1770 ;;cell has a `t' in it.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1771 (eval `(let ((,(ses-cell-symbol row (1+ col)) t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1772 (ses-print-cell row col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1773 ;;Now remove the *skip*. ses-print-cell is always nil here
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1774 (ses-set-cell row (1+ col) 'value nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1775 (1value (ses-print-cell row (1+ col))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1776
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1777 (defun ses-reconstruct-all ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1778 "Reconstruct buffer based on cell data stored in Emacs variables."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1779 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1780 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1781 ;;Reconstruct reference lists.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1782 (let (refs x yrow ycol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1783 ;;Delete old reference lists
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1784 (ses-dotimes-msg (row numrows) "Deleting references..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1785 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1786 (ses-set-cell row col 'references nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1787 ;;Create new reference lists
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1788 (ses-dotimes-msg (row numrows) "Computing references..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1789 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1790 (dolist (ref (ses-formula-references (ses-cell-formula row col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1791 (setq x (ses-sym-rowcol ref)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1792 yrow (car x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1793 ycol (cdr x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1794 (ses-set-cell yrow ycol 'references
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1795 (cons (ses-cell-symbol row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1796 (ses-cell-references yrow ycol)))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1797 ;;Delete everything and reconstruct basic data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1798 (if (< (point-max) (buffer-size))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1799 (setq deferred-narrow t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1800 (widen)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1801 (let ((inhibit-read-only t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1802 (goto-char (point-max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1803 (if (search-backward ";;; Local Variables:\n" nil t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1804 (delete-region 1 (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1805 ;;Buffer is quite screwed up - can't even save the user-specified locals
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1806 (delete-region 1 (point-max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1807 (insert ses-initial-file-trailer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1808 (goto-char 1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1809 ;;Create a blank display area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1810 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1811 (insert blank-line))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1812 (insert ses-print-data-boundary)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1813 ;;Placeholders for cell data
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1814 (insert (make-string (* numrows (1+ numcols)) ?\n))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1815 ;;Placeholders for col-widths, col-printers, default-printer, header-row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1816 (insert "\n\n\n\n")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1817 (insert ses-initial-global-parameters))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1818 (ses-set-parameter 'column-widths column-widths)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1819 (ses-set-parameter 'col-printers col-printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1820 (ses-set-parameter 'default-printer default-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1821 (ses-set-parameter 'header-row header-row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1822 (ses-set-parameter 'numrows numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1823 (ses-set-parameter 'numcols numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1824 ;;Keep our old narrowing
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1825 (ses-setup)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1826 (ses-recalculate-all)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1827 (goto-char 1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1828
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1829
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1830 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1831 ;;;; Input of cell formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1832 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1833
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1834 (defun ses-edit-cell (row col newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1835 "Display current cell contents in minibuffer, for editing. Returns nil if
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1836 cell formula was unsafe and user declined confirmation."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1837 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1838 (progn
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1839 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1840 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1841 (let* ((rowcol (ses-sym-rowcol curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1842 (row (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1843 (col (cdr rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1844 (formula (ses-cell-formula row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1845 initial)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1846 (if (eq (car-safe formula) 'ses-safe-formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1847 (setq formula (cadr formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1848 (if (eq (car-safe formula) 'quote)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1849 (setq initial (format "'%S" (cadr formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1850 (setq initial (prin1-to-string formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1851 (if (stringp formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1852 ;;Position cursor inside close-quote
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1853 (setq initial (cons initial (length initial))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1854 (list row col
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1855 (read-from-minibuffer (format "Cell %s: " curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1856 initial
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1857 ses-mode-edit-map
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1858 t ;Convert to Lisp object
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1859 'ses-read-cell-history)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1860 (when (ses-warn-unsafe newval 'unsafep)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1861 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1862 (ses-cell-set-formula row col newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1863 t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1864
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1865 (defun ses-read-cell (row col newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1866 "Self-insert for initial character of cell function."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1867 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1868 (let ((initial (this-command-keys))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1869 (rowcol (progn (ses-check-curcell) (ses-sym-rowcol curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1870 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1871 (if (string= initial "\"")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1872 (setq initial "\"\"") ;Enter a string
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1873 (if (string= initial "(")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1874 (setq initial "()"))) ;Enter a formula list
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1875 (list (car rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1876 (cdr rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1877 (read-from-minibuffer (format "Cell %s: " curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1878 (cons initial 2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1879 ses-mode-edit-map
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1880 t ;Convert to Lisp object
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1881 'ses-read-cell-history))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1882 (when (ses-edit-cell row col newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1883 (ses-command-hook) ;Update cell widths before movement
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1884 (dolist (x ses-after-entry-functions)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1885 (funcall x 1))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1886
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1887 (defun ses-read-symbol (row col symb)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1888 "Self-insert for a symbol as a cell formula. The set of all symbols that
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1889 have been used as formulas in this spreadsheet is available for completions."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1890 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1891 (let ((rowcol (progn (ses-check-curcell) (ses-sym-rowcol curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1892 newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1893 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1894 (setq newval (completing-read (format "Cell %s ': " curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1895 symbolic-formulas))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1896 (list (car rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1897 (cdr rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1898 (if (string= newval "")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1899 nil ;Don't create zero-length symbols!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1900 (list 'quote (intern newval))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1901 (when (ses-edit-cell row col symb)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1902 (ses-command-hook) ;Update cell widths before movement
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1903 (dolist (x ses-after-entry-functions)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1904 (funcall x 1))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1905
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1906 (defun ses-clear-cell-forward (count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1907 "Delete formula and printer for current cell and then move to next cell.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1908 With prefix, deletes several cells."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1909 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1910 (if (< count 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1911 (1value (ses-clear-cell-backward (- count)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1912 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1913 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1914 (dotimes (x count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1915 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1916 (let ((rowcol (ses-sym-rowcol curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1917 (or rowcol (signal 'end-of-buffer nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1918 (ses-clear-cell (car rowcol) (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1919 (forward-char 1))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1920
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1921 (defun ses-clear-cell-backward (count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1922 "Move to previous cell and then delete it. With prefix, deletes several
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1923 cells."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1924 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1925 (if (< count 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1926 (1value (ses-clear-cell-forward (- count)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1927 (ses-check-curcell 'end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1928 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1929 (dotimes (x count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1930 (backward-char 1) ;Will signal 'beginning-of-buffer if appropriate
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1931 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1932 (let ((rowcol (ses-sym-rowcol curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1933 (ses-clear-cell (car rowcol) (cdr rowcol))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1934
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1935
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1936 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1937 ;;;; Input of cell-printer functions
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1938 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1939
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1940 (defun ses-read-printer (prompt default)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1941 "Common code for `ses-read-cell-printer', `ses-read-column-printer', and `ses-read-default-printer'.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1942 PROMPT should end with \": \". Result is t if operation was cancelled."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1943 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1944 (if (eq default t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1945 (setq default "")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1946 (setq prompt (format "%s [currently %S]: "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1947 (substring prompt 0 -2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1948 default)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1949 (let ((new (read-from-minibuffer prompt
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1950 nil ;Initial contents
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1951 ses-mode-edit-map
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1952 t ;Evaluate the result
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1953 'ses-read-printer-history
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1954 (prin1-to-string default))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1955 (if (equal new default)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1956 ;;User changed mind, decided not to change printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1957 (setq new t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1958 (ses-printer-validate new)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1959 (or (not new)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1960 (stringp new)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1961 (stringp (car-safe new))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1962 (ses-warn-unsafe new 'unsafep-function)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1963 (setq new t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1964 new))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1965
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1966 (defun ses-read-cell-printer (newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1967 "Set the printer function for the current cell or range.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1968
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1969 A printer function is either a string (a format control-string with one
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1970 %-sequence -- result from format will be right-justified), or a list of one
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1971 string (result from format will be left-justified), or a lambda-expression of
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1972 one argument, or a symbol that names a function of one argument. In the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1973 latter two cases, the function's result should be either a string (will be
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1974 right-justified) or a list of one string (will be left-justified)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1975 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1976 (let ((default t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1977 prompt)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1978 (ses-check-curcell 'range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1979 ;;Default is none if not all cells in range have same printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1980 (catch 'ses-read-cell-printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1981 (ses-dorange curcell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1982 (setq x (ses-cell-printer row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1983 (if (eq (car-safe x) 'ses-safe-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1984 (setq x (cadr x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1985 (if (eq default t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1986 (setq default x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1987 (unless (equal default x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1988 ;;Range contains differing printer functions
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1989 (setq default t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1990 (throw 'ses-read-cell-printer t)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1991 (list (ses-read-printer (format "Cell %S printer: " curcell) default))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1992 (unless (eq newval t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1993 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1994 (ses-dorange curcell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1995 (ses-set-cell row col 'printer newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1996 (ses-print-cell row col))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1997
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1998 (defun ses-read-column-printer (col newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
1999 "Set the printer function for the current column. See
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2000 `ses-read-cell-printer' for input forms."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2001 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2002 (let ((col (cdr (ses-sym-rowcol curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2003 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2004 (list col (ses-read-printer (format "Column %s printer: "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2005 (ses-column-letter col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2006 (ses-col-printer col)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2007
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2008 (unless (eq newval t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2009 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2010 (ses-set-parameter 'col-printers newval col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2011 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2012 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2013 (ses-print-cell row col)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2014
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2015 (defun ses-read-default-printer (newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2016 "Set the default printer function for cells that have no other. See
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2017 `ses-read-cell-printer' for input forms."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2018 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2019 (list (ses-read-printer "Default printer: " default-printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2020 (unless (eq newval t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2021 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2022 (ses-set-parameter 'default-printer newval)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2023 (ses-reprint-all t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2024
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2025
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2026 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2027 ;;;; Spreadsheet size adjustments
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2028 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2029
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2030 (defun ses-insert-row (count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2031 "Insert a new row before the current one. With prefix, insert COUNT rows
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2032 before current one."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2033 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2034 (ses-check-curcell 'end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2035 (or (> count 0) (signal 'args-out-of-range nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2036 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2037 (let ((inhibit-quit t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2038 (inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2039 (row (or (car (ses-sym-rowcol curcell)) numrows))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2040 newrow)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2041 ;;Create a new set of cell-variables
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2042 (ses-create-cell-variable-range numrows (+ numrows count -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2043 0 (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2044 (ses-set-parameter 'numrows (+ numrows count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2045 ;;Insert each row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2046 (ses-goto-print row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2047 (ses-dotimes-msg (x count) "Inserting row..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2048 ;;Create a row of empty cells. The `symbol' fields will be set by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2049 ;;the call to ses-relocate-all.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2050 (setq newrow (make-vector numcols nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2051 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2052 (aset newrow col (make-vector ses-cell-size nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2053 (setq cells (ses-vector-insert cells row newrow))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2054 (push `(ses-vector-delete cells ,row 1) buffer-undo-list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2055 (insert blank-line))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2056 ;;Insert empty lines in cell data area (will be replaced by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2057 ;;ses-relocate-all)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2058 (ses-goto-data row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2059 (insert (make-string (* (1+ numcols) count) ?\n))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2060 (ses-relocate-all row 0 count 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2061 ;;If any cell printers insert constant text, insert that text
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2062 ;;into the line.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2063 (let ((cols (mapconcat #'ses-call-printer col-printers nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2064 (global (ses-call-printer default-printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2065 (if (or (> (length cols) 0) (> (length global) 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2066 (dotimes (x count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2067 (dotimes (col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2068 ;;These cells are always nil, only constant formatting printed
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2069 (1value (ses-print-cell (+ x row) col))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2070 (when (> header-row row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2071 ;;Inserting before header
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2072 (ses-set-parameter 'header-row (+ header-row count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2073 (ses-reset-header-string)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2074 ;;Reconstruct text attributes
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2075 (ses-setup)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2076 ;;Return to current cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2077 (if curcell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2078 (ses-jump-safe curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2079 (ses-goto-print (1- numrows) 0)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2080
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2081 (defun ses-delete-row (count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2082 "Delete the current row. With prefix, Deletes COUNT rows starting from the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2083 current one."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2084 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2085 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2086 (or (> count 0) (signal 'args-out-of-range nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2087 (let ((inhibit-quit t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2088 (inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2089 (row (car (ses-sym-rowcol curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2090 pos)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2091 (setq count (min count (- numrows row)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2092 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2093 (ses-set-parameter 'numrows (- numrows count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2094 ;;Delete lines from print area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2095 (ses-goto-print row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2096 (ses-delete-line count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2097 ;;Delete lines from cell data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2098 (ses-goto-data row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2099 (ses-delete-line (* count (1+ numcols)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2100 ;;Relocate variables and formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2101 (ses-set-with-undo 'cells (ses-vector-delete cells row count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2102 (ses-relocate-all row 0 (- count) 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2103 (ses-destroy-cell-variable-range numrows (+ numrows count -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2104 0 (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2105 (when (> header-row row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2106 (if (<= header-row (+ row count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2107 ;;Deleting the header row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2108 (ses-set-parameter 'header-row 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2109 (ses-set-parameter 'header-row (- header-row count)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2110 (ses-reset-header-string)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2111 ;;Reconstruct attributes
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2112 (ses-setup)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2113 (ses-jump-safe curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2114
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2115 (defun ses-insert-column (count &optional col width printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2116 "Insert a new column before COL (default is the current one). With prefix,
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2117 insert COUNT columns before current one. If COL is specified, the new
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2118 column(s) get the specified WIDTH and PRINTER (otherwise they're taken from
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2119 the current column)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2120 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2121 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2122 (or (> count 0) (signal 'args-out-of-range nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2123 (or col
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2124 (setq col (cdr (ses-sym-rowcol curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2125 width (ses-col-width col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2126 printer (ses-col-printer col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2127 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2128 (let ((inhibit-quit t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2129 (inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2130 (widths column-widths)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2131 (printers col-printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2132 has-skip)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2133 ;;Create a new set of cell-variables
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2134 (ses-create-cell-variable-range 0 (1- numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2135 numcols (+ numcols count -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2136 ;;Insert each column.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2137 (ses-dotimes-msg (x count) "Inserting column..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2138 ;;Create a column of empty cells. The `symbol' fields will be set by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2139 ;;the call to ses-relocate-all.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2140 (ses-adjust-print-width col (1+ width))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2141 (ses-set-parameter 'numcols (1+ numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2142 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2143 (and (< (1+ col) numcols) (eq (ses-cell-value row col) '*skip*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2144 ;;Inserting in the middle of a spill-over
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2145 (setq has-skip t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2146 (ses-aset-with-undo cells row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2147 (ses-vector-insert (aref cells row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2148 col
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2149 (make-vector ses-cell-size nil)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2150 ;;Insert empty lines in cell data area (will be replaced by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2151 ;;ses-relocate-all)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2152 (ses-goto-data row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2153 (insert ?\n))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2154 ;;Insert column width and printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2155 (setq widths (ses-vector-insert widths col width)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2156 printers (ses-vector-insert printers col printer)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2157 (ses-set-parameter 'column-widths widths)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2158 (ses-set-parameter 'col-printers printers)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2159 (ses-reset-header-string)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2160 (ses-relocate-all 0 col 0 count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2161 (if has-skip
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2162 (ses-reprint-all t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2163 (when (or (> (length (ses-call-printer printer)) 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2164 (> (length (ses-call-printer default-printer)) 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2165 ;;Either column printer or global printer inserts some constant text
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2166 ;;Reprint the new columns to insert that text.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2167 (dotimes (x numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2168 (dotimes (y count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2169 ;Always nil here - this is a blank column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2170 (1value (ses-print-cell-new-width x (+ y col))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2171 (ses-setup)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2172 (ses-jump-safe curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2173
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2174 (defun ses-delete-column (count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2175 "Delete the current column. With prefix, Deletes COUNT columns starting
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2176 from the current one."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2177 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2178 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2179 (or (> count 0) (signal 'args-out-of-range nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2180 (let ((inhibit-quit t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2181 (inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2182 (rowcol (ses-sym-rowcol curcell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2183 (width 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2184 new col origrow has-skip)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2185 (setq origrow (car rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2186 col (cdr rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2187 count (min count (- numcols col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2188 (if (= count numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2189 (error "Can't delete all columns!"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2190 ;;Determine width of column(s) being deleted
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2191 (dotimes (x count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2192 (setq width (+ width (ses-col-width (+ col x)) 1)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2193 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2194 (ses-set-parameter 'numcols (- numcols count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2195 (ses-adjust-print-width col (- width))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2196 (ses-dotimes-msg (row numrows) "Deleting column..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2197 ;;Delete lines from cell data area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2198 (ses-goto-data row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2199 (ses-delete-line count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2200 ;;Delete cells. Check if deletion area begins or ends with a skip.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2201 (if (or (eq (ses-cell-value row col) '*skip*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2202 (and (< col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2203 (eq (ses-cell-value row (+ col count)) '*skip*)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2204 (setq has-skip t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2205 (ses-aset-with-undo cells row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2206 (ses-vector-delete (aref cells row) col count)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2207 ;;Update globals
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2208 (ses-set-parameter 'column-widths
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2209 (ses-vector-delete column-widths col count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2210 (ses-set-parameter 'col-printers
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2211 (ses-vector-delete col-printers col count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2212 (ses-reset-header-string)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2213 ;;Relocate variables and formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2214 (ses-relocate-all 0 col 0 (- count))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2215 (ses-destroy-cell-variable-range 0 (1- numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2216 numcols (+ numcols count -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2217 (if has-skip
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2218 (ses-reprint-all t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2219 (ses-setup))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2220 (if (>= col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2221 (setq col (1- col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2222 (ses-goto-print origrow col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2223
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2224 (defun ses-forward-or-insert (&optional count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2225 "Move to next cell in row, or inserts a new cell if already in last one, or
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2226 inserts a new row if at bottom of print area. Repeat COUNT times."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2227 (interactive "p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2228 (ses-check-curcell 'end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2229 (setq deactivate-mark t) ;Doesn't combine well with ranges
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2230 (dotimes (x count)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2231 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2232 (if (not curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2233 (progn ;At bottom of print area
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2234 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2235 (ses-insert-row 1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2236 (let ((col (cdr (ses-sym-rowcol curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2237 (when (/= 32
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2238 (char-before (next-single-property-change (point)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2239 'intangible)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2240 ;;We're already in last nonskipped cell on line. Need to create a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2241 ;;new column.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2242 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2243 (ses-insert-column (- count x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2244 numcols
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2245 (ses-col-width col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2246 (ses-col-printer col)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2247 (forward-char)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2248
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2249 (defun ses-append-row-jump-first-column ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2250 "Insert a new row after current one and jumps to its first column."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2251 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2252 (ses-check-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2253 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2254 (beginning-of-line 2)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2255 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2256 (ses-insert-row 1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2257
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2258 (defun ses-set-column-width (col newwidth)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2259 "Set the width of the current column."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2260 (interactive
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2261 (let ((col (cdr (progn (ses-check-curcell) (ses-sym-rowcol curcell)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2262 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2263 (list col
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2264 (if current-prefix-arg
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2265 (prefix-numeric-value current-prefix-arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2266 (read-from-minibuffer (format "Column %s width [currently %d]: "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2267 (ses-column-letter col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2268 (ses-col-width col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2269 nil ;No initial contents
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2270 nil ;No override keymap
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2271 t ;Convert to Lisp object
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2272 nil ;No history
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2273 (number-to-string
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2274 (ses-col-width col))))))) ;Default value
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2275 (if (< newwidth 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2276 (error "Invalid column width"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2277 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2278 (ses-reset-header-string)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2279 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2280 (let ((inhibit-quit t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2281 (ses-adjust-print-width col (- newwidth (ses-col-width col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2282 (ses-set-parameter 'column-widths newwidth col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2283 (dotimes (row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2284 (ses-print-cell-new-width row col))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2285
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2286
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2287 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2288 ;;;; Cut and paste, import and export
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2289 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2290
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2291 (defadvice copy-region-as-kill (around ses-copy-region-as-kill
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2292 activate preactivate)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2293 "It doesn't make sense to copy read-only or intangible attributes into the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2294 kill ring. It probably doesn't make sense to copy keymap properties.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2295 We'll assume copying front-sticky properties doesn't make sense, either.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2296
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2297 This advice also includes some SES-specific code because otherwise it's too
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2298 hard to override how mouse-1 works."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2299 (when (> beg end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2300 (let ((temp beg))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2301 (setq beg end
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2302 end temp)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2303 (if (not (and (eq major-mode 'ses-mode)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2304 (eq (get-text-property beg 'read-only) 'ses)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2305 (eq (get-text-property (1- end) 'read-only) 'ses)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2306 ad-do-it ;Normal copy-region-as-kill
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2307 (kill-new (ses-copy-region beg end))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2308
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2309 (defun ses-copy-region (beg end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2310 "Treat the region as rectangular. Convert the intangible attributes to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2311 SES attributes recording the contents of the cell as of the time of copying."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2312 (let* ((inhibit-point-motion-hooks t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2313 (x (mapconcat 'ses-copy-region-helper
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2314 (extract-rectangle beg (1- end)) "\n")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2315 (remove-text-properties 0 (length x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2316 '(read-only t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2317 intangible t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2318 keymap t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2319 front-sticky t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2320 x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2321 x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2322
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2323 (defun ses-copy-region-helper (line)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2324 "Converts one line (of a rectangle being extracted from a spreadsheet) to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2325 external form by attaching to each print cell a 'ses attribute that records
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2326 the corresponding data cell."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2327 (or (> (length line) 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2328 (error "Empty range"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2329 (let ((inhibit-read-only t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2330 (pos 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2331 mycell next sym rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2332 (while pos
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2333 (setq sym (get-text-property pos 'intangible line)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2334 next (next-single-property-change pos 'intangible line)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2335 rowcol (ses-sym-rowcol sym)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2336 mycell (ses-get-cell (car rowcol) (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2337 (put-text-property pos (or next (length line))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2338 'ses
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2339 (list (ses-cell-symbol mycell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2340 (ses-cell-formula mycell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2341 (ses-cell-printer mycell))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2342 line)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2343 (setq pos next)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2344 line)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2345
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2346 (defun ses-kill-override (beg end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2347 "Generic override for any commands that kill text. We clear the killed
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2348 cells instead of deleting them."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2349 (interactive "r")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2350 (ses-check-curcell 'needrange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2351 ;;For some reason, the text-read-only error is not caught by
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2352 ;;`delete-region', so we have to use subterfuge.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2353 (let ((buffer-read-only t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2354 (1value (condition-case x
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2355 (noreturn (funcall (lookup-key (current-global-map)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2356 (this-command-keys))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2357 beg end))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2358 (buffer-read-only nil)))) ;The expected error
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2359 ;;Because the buffer was marked read-only, the kill command turned itself
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2360 ;;into a copy. Now we clear the cells or signal the error. First we
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2361 ;;check whether the buffer really is read-only.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2362 (barf-if-buffer-read-only)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2363 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2364 (ses-dorange curcell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2365 (ses-clear-cell row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2366 (ses-jump (car curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2367
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2368 (defadvice yank (around ses-yank activate preactivate)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2369 "In SES mode, the yanked text is inserted as cells.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2370
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2371 If the text contains 'ses attributes (meaning it went to the kill-ring from a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2372 SES buffer), the formulas and print functions are restored for the cells. If
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2373 the text contains tabs, this is an insertion of tab-separated formulas.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2374 Otherwise the text is inserted as the formula for the current cell.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2375
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2376 When inserting cells, the formulas are usually relocated to keep the same
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2377 relative references to neighboring cells. This is best if the formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2378 generally refer to other cells within the yanked text. You can use the C-u
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2379 prefix to specify insertion without relocation, which is best when the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2380 formulas refer to cells outsite the yanked text.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2381
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2382 When inserting formulas, the text is treated as a string constant if it doesn't
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2383 make sense as a sexp or would otherwise be considered a symbol. Use 'sym to
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2384 explicitly insert a symbol, or use the C-u prefix to treat all unmarked words
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2385 as symbols."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2386 (if (not (and (eq major-mode 'ses-mode)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2387 (eq (get-text-property (point) 'keymap) 'ses-mode-print-map)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2388 ad-do-it ;Normal non-SES yank
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2389 (ses-check-curcell 'end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2390 (push-mark (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2391 (let ((text (current-kill (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2392 ((listp arg) 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2393 ((eq arg '-) -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2394 (t (1- arg))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2395 (or (ses-yank-cells text arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2396 (ses-yank-tsf text arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2397 (ses-yank-one (ses-yank-resize 1 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2398 text
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2399 0
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2400 (if (memq (aref text (1- (length text))) '(?\t ?\n))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2401 ;;Just one cell - delete final tab or newline
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2402 (1- (length text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2403 arg)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2404 (if (consp arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2405 (exchange-point-and-mark))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2406
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2407 (defun ses-yank-pop (arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2408 "Replace just-yanked stretch of killed text with a different stretch.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2409 This command is allowed only immediately after a `yank' or a `yank-pop', when
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2410 the region contains a stretch of reinserted previously-killed text. We
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2411 replace it with a different stretch of killed text.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2412 Unlike standard `yank-pop', this function uses `undo' to delete the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2413 previous insertion."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2414 (interactive "*p")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2415 (or (eq last-command 'yank)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2416 ;;Use noreturn here just to avoid a "poor-coverage" warning in its
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2417 ;;macro definition.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2418 (noreturn (error "Previous command was not a yank")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2419 (undo)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2420 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2421 (yank (1+ (or arg 1)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2422 (setq this-command 'yank))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2423
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2424 (defun ses-yank-cells (text arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2425 "If the TEXT has a proper set of 'ses attributes, inserts the text as
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2426 cells, else return nil. The cells are reprinted--the supplied text is
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2427 ignored because the column widths, default printer, etc. at yank time might
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2428 be different from those at kill-time. ARG is a list to indicate that
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2429 formulas are to be inserted without relocation."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2430 (let ((first (get-text-property 0 'ses text))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2431 (last (get-text-property (1- (length text)) 'ses text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2432 (when (and first last) ;;Otherwise not proper set of attributes
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2433 (setq first (ses-sym-rowcol (car first))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2434 last (ses-sym-rowcol (car last)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2435 (let* ((needrows (- (car last) (car first) -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2436 (needcols (- (cdr last) (cdr first) -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2437 (rowcol (ses-yank-resize needrows needcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2438 (rowincr (- (car rowcol) (car first)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2439 (colincr (- (cdr rowcol) (cdr first)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2440 (pos 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2441 myrow mycol x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2442 (ses-dotimes-msg (row needrows) "Yanking..."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2443 (setq myrow (+ row (car rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2444 (dotimes (col needcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2445 (setq mycol (+ col (cdr rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2446 last (get-text-property pos 'ses text)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2447 pos (next-single-property-change pos 'ses text)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2448 x (ses-sym-rowcol (car last)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2449 (if (not last)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2450 ;;Newline - all remaining cells on row are skipped
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2451 (setq x (cons (- myrow rowincr) (+ needcols colincr -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2452 last (list nil nil nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2453 pos (1- pos)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2454 (if (/= (car x) (- myrow rowincr))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2455 (error "Cell row error"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2456 (if (< (- mycol colincr) (cdr x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2457 ;;Some columns were skipped
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2458 (let ((oldcol mycol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2459 (while (< (- mycol colincr) (cdr x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2460 (ses-clear-cell myrow mycol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2461 (setq col (1+ col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2462 mycol (1+ mycol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2463 (ses-print-cell myrow (1- oldcol)))) ;;This inserts *skip*
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2464 (when (car last) ;Skip this for *skip* cells
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2465 (setq x (nth 2 last))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2466 (unless (equal x (ses-cell-printer myrow mycol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2467 (or (not x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2468 (stringp x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2469 (eq (car-safe x) 'ses-safe-printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2470 (setq x `(ses-safe-printer ,x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2471 (ses-set-cell myrow mycol 'printer x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2472 (setq x (cadr last))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2473 (if (atom arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2474 (setq x (ses-relocate-formula x 0 0 rowincr colincr)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2475 (or (atom x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2476 (eq (car-safe x) 'ses-safe-formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2477 (setq x `(ses-safe-formula ,x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2478 (ses-cell-set-formula myrow mycol x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2479 (when pos
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2480 (if (get-text-property pos 'ses text)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2481 (error "Missing newline between rows"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2482 (setq pos (next-single-property-change pos 'ses text))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2483 t))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2484
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2485 (defun ses-yank-one (rowcol text from to arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2486 "Insert the substring [FROM,TO] of TEXT as the formula for cell ROWCOL (a
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2487 cons of ROW and COL). Treat plain symbols as strings unless ARG is a list."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2488 (let ((val (condition-case nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2489 (read-from-string text from to)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2490 (error (cons nil from)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2491 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2492 ((< (cdr val) (or to (length text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2493 ;;Invalid sexp - leave it as a string
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2494 (setq val (substring text from to)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2495 ((and (car val) (symbolp (car val)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2496 (if (consp arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2497 (setq val (list 'quote (car val))) ;Keep symbol
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2498 (setq val (substring text from to)))) ;Treat symbol as text
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2499 (t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2500 (setq val (car val))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2501 (let ((row (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2502 (col (cdr rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2503 (or (atom val)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2504 (setq val `(ses-safe-formula ,val)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2505 (ses-cell-set-formula row col val))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2506
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2507 (defun ses-yank-tsf (text arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2508 "If TEXT contains tabs and/or newlines, treats the tabs as
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2509 column-separators and the newlines as row-separators and inserts the text as
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2510 cell formulas--else return nil. Treat plain symbols as strings unless ARG
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2511 is a list. Ignore a final newline."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2512 (if (or (not (string-match "[\t\n]" text))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2513 (= (match-end 0) (length text)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2514 ;;Not TSF format
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2515 nil
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2516 (if (/= (aref text (1- (length text))) ?\n)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2517 (setq text (concat text "\n")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2518 (let ((pos -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2519 (spots (list -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2520 (cols 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2521 (needrows 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2522 needcols rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2523 ;;Find all the tabs and newlines
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2524 (while (setq pos (string-match "[\t\n]" text (1+ pos)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2525 (push pos spots)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2526 (setq cols (1+ cols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2527 (when (eq (aref text pos) ?\n)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2528 (if (not needcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2529 (setq needcols cols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2530 (or (= needcols cols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2531 (error "Inconsistent row lengths")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2532 (setq cols 0
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2533 needrows (1+ needrows))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2534 ;;Insert the formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2535 (setq rowcol (ses-yank-resize needrows needcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2536 (dotimes (row needrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2537 (dotimes (col needcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2538 (ses-yank-one (cons (+ (car rowcol) needrows (- row) -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2539 (+ (cdr rowcol) needcols (- col) -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2540 text (1+ (cadr spots)) (car spots) arg)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2541 (setq spots (cdr spots))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2542 (ses-goto-print (+ (car rowcol) needrows -1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2543 (+ (cdr rowcol) needcols -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2544 t)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2545
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2546 (defun ses-yank-resize (needrows needcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2547 "If this yank will require inserting rows and/or columns, asks for
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2548 confirmation and then inserts them. Result is (row,col) for top left of yank
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2549 spot, or error signal if user requests cancel."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2550 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2551 (let ((rowcol (if curcell (ses-sym-rowcol curcell) (cons numrows 0)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2552 rowbool colbool)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2553 (setq needrows (- (+ (car rowcol) needrows) numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2554 needcols (- (+ (cdr rowcol) needcols) numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2555 rowbool (> needrows 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2556 colbool (> needcols 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2557 (when (or rowbool colbool)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2558 ;;Need to insert. Get confirm
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2559 (or (y-or-n-p (format "Yank will insert %s%s%s. Continue "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2560 (if rowbool (format "%d rows" needrows) "")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2561 (if (and rowbool colbool) " and " "")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2562 (if colbool (format "%d columns" needcols) "")))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2563 (error "Cancelled"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2564 (when rowbool
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2565 (let (curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2566 (save-excursion
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2567 (ses-goto-print numrows 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2568 (ses-insert-row needrows))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2569 (when colbool
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2570 (ses-insert-column needcols
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2571 numcols
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2572 (ses-col-width (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2573 (ses-col-printer (1- numcols)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2574 rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2575
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2576 (defun ses-export-tsv (beg end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2577 "Export values from the current range, with tabs between columns and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2578 newlines between rows. Result is placed in kill ring."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2579 (interactive "r")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2580 (ses-export-tab nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2581
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2582 (defun ses-export-tsf (beg end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2583 "Export formulas from the current range, with tabs between columns and
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2584 newlines between rows. Result is placed in kill ring."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2585 (interactive "r")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2586 (ses-export-tab t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2587
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2588 (defun ses-export-tab (want-formulas)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2589 "Export the current range with tabs between columns and newlines between
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2590 rows. Result is placed in kill ring. The export is values unless
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2591 WANT-FORMULAS is non-nil. Newlines and tabs in the export text are escaped."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2592 (ses-check-curcell 'needrange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2593 (let ((print-escape-newlines t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2594 result item)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2595 (ses-dorange curcell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2596 (setq item (if want-formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2597 (ses-cell-formula row col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2598 (ses-cell-value row col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2599 (if (eq (car-safe item) 'ses-safe-formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2600 ;;Hide our deferred safety-check marker
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2601 (setq item (cadr item)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2602 (if (or (not item) (eq item '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2603 (setq item ""))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2604 (when (eq (car-safe item) 'quote)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2605 (push "'" result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2606 (setq item (cadr item)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2607 (setq item (prin1-to-string item t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2608 (setq item (replace-regexp-in-string "\t" "\\\\t" item))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2609 (push item result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2610 (cond
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2611 ((< col maxcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2612 (push "\t" result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2613 ((< row maxrow)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2614 (push "\n" result))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2615 (setq result (apply 'concat (nreverse result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2616 (kill-new result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2617
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2618
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2619 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2620 ;;;; Other user commands
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2621 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2622
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2623 (defun ses-read-header-row (row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2624 (interactive "NHeader row: ")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2625 (if (or (< row 0) (> row numrows))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2626 (error "Invalid header-row"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2627 (ses-begin-change)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2628 (ses-set-parameter 'header-row row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2629 (ses-reset-header-string))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2630
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2631 (defun ses-mark-row ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2632 "Marks the entirety of current row as a range."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2633 (interactive)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2634 (ses-check-curcell 'range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2635 (let ((row (car (ses-sym-rowcol (or (car-safe curcell) curcell)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2636 (push-mark (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2637 (ses-goto-print (1+ row) 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2638 (push-mark (point) nil t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2639 (ses-goto-print row 0)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2640
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2641 (defun ses-mark-column ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2642 "Marks the entirety of current column as a range."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2643 (interactive)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2644 (ses-check-curcell 'range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2645 (let ((col (cdr (ses-sym-rowcol (or (car-safe curcell) curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2646 (row 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2647 (push-mark (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2648 (ses-goto-print (1- numrows) col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2649 (forward-char 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2650 (push-mark (point) nil t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2651 (while (eq '*skip* (ses-cell-value row col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2652 ;;Skip over initial cells in column that can't be selected
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2653 (setq row (1+ row)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2654 (ses-goto-print row col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2655
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2656 (defun ses-end-of-line ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2657 "Move point to last cell on line."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2658 (interactive)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2659 (ses-check-curcell 'end 'range)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2660 (when curcell ;Otherwise we're at the bottom row, which is empty anyway
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2661 (let ((col (1- numcols))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2662 row rowcol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2663 (if (symbolp curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2664 ;;Single cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2665 (setq row (car (ses-sym-rowcol curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2666 ;;Range - use whichever end of the range the point is at
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2667 (setq rowcol (ses-sym-rowcol (if (< (point) (mark))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2668 (car curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2669 (cdr curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2670 ;;If range already includes the last cell in a row, point is actually
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2671 ;;in the following row
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2672 (if (<= (cdr rowcol) (1- col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2673 (setq row (car rowcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2674 (setq row (1+ (car rowcol)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2675 (if (= row numrows)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2676 ;;Already at end - can't go anywhere
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2677 (setq col 0))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2678 (when (< row numrows) ;Otherwise it's a range that includes last cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2679 (while (eq (ses-cell-value row col) '*skip*)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2680 ;;Back to beginning of multi-column cell
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2681 (setq col (1- col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2682 (ses-goto-print row col)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2683
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2684 (defun ses-renarrow-buffer ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2685 "Narrow the buffer so only the print area is visible. Use after \\[widen]."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2686 (interactive)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2687 (setq deferred-narrow t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2688
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2689 (defun ses-sort-column (sorter &optional reverse)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2690 "Sorts the range by a specified column. With prefix, sorts in
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2691 REVERSE order."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2692 (interactive "*sSort column: \nP")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2693 (ses-check-curcell 'needrange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2694 (let ((min (ses-sym-rowcol (car curcell)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2695 (max (ses-sym-rowcol (cdr curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2696 (let ((minrow (car min))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2697 (mincol (cdr min))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2698 (maxrow (car max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2699 (maxcol (cdr max))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2700 keys extracts end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2701 (setq sorter (cdr (ses-sym-rowcol (intern (concat sorter "1")))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2702 (or (and sorter (>= sorter mincol) (<= sorter maxcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2703 (error "Invalid sort column"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2704 ;;Get key columns and sort them
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2705 (dotimes (x (- maxrow minrow -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2706 (ses-goto-print (+ minrow x) sorter)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2707 (setq end (next-single-property-change (point) 'intangible))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2708 (push (cons (buffer-substring-no-properties (point) end)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2709 (+ minrow x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2710 keys))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2711 (setq keys (sort keys #'(lambda (x y) (string< (car x) (car y)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2712 ;;Extract the lines in reverse sorted order
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2713 (or reverse
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2714 (setq keys (nreverse keys)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2715 (dolist (x keys)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2716 (ses-goto-print (cdr x) (1+ maxcol))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2717 (setq end (point))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2718 (ses-goto-print (cdr x) mincol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2719 (push (ses-copy-region (point) end) extracts))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2720 (deactivate-mark)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2721 ;;Paste the lines sequentially
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2722 (dotimes (x (- maxrow minrow -1))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2723 (ses-goto-print (+ minrow x) mincol)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2724 (ses-set-curcell)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2725 (ses-yank-cells (pop extracts) nil)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2726
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2727 (defun ses-sort-column-click (event reverse)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2728 (interactive "*e\nP")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2729 (setq event (event-end event))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2730 (select-window (posn-window event))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2731 (setq event (car (posn-col-row event))) ;Click column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2732 (let ((col 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2733 (while (and (< col numcols) (> event (ses-col-width col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2734 (setq event (- event (ses-col-width col) 1)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2735 col (1+ col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2736 (if (>= col numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2737 (ding)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2738 (ses-sort-column (ses-column-letter col) reverse))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2739
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2740 (defun ses-insert-range ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2741 "Inserts into minibuffer the list of cells currently highlighted in the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2742 spreadsheet."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2743 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2744 (let (x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2745 (with-current-buffer (window-buffer minibuffer-scroll-window)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2746 (ses-command-hook) ;For ses-coverage
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2747 (ses-check-curcell 'needrange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2748 (setq x (cdr (macroexpand `(ses-range ,(car curcell) ,(cdr curcell))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2749 (insert (substring (prin1-to-string (nreverse x)) 1 -1))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2750
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2751 (defun ses-insert-ses-range ()
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2752 "Inserts \"(ses-range x y)\" in the minibuffer to represent the currently
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2753 highlighted range in the spreadsheet."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2754 (interactive "*")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2755 (let (x)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2756 (with-current-buffer (window-buffer minibuffer-scroll-window)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2757 (ses-command-hook) ;For ses-coverage
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2758 (ses-check-curcell 'needrange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2759 (setq x (format "(ses-range %S %S)" (car curcell) (cdr curcell))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2760 (insert x)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2761
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2762 (defun ses-insert-range-click (event)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2763 "Mouse version of `ses-insert-range'."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2764 (interactive "*e")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2765 (mouse-set-point event)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2766 (ses-insert-range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2767
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2768 (defun ses-insert-ses-range-click (event)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2769 "Mouse version of `ses-insert-ses-range'."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2770 (interactive "*e")
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2771 (mouse-set-point event)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2772 (ses-insert-ses-range))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2773
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2774
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2775 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2776 ;;;; Checking formulas for safety
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2777 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2778
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2779 (defun ses-safe-printer (printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2780 "Returns PRINTER if safe, or the substitute printer `ses-unsafe' otherwise."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2781 (if (or (stringp printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2782 (stringp (car-safe printer))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2783 (not printer)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2784 (ses-warn-unsafe printer 'unsafep-function))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2785 printer
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2786 'ses-unsafe))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2787
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2788 (defun ses-safe-formula (formula)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2789 "Returns FORMULA if safe, or the substitute formula *unsafe* otherwise."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2790 (if (ses-warn-unsafe formula 'unsafep)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2791 formula
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2792 `(ses-unsafe ',formula)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2793
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2794 (defun ses-warn-unsafe (formula checker)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2795 "Applies CHECKER to FORMULA. If result is non-nil, asks user for
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2796 confirmation about FORMULA, which might be unsafe. Returns t if formula
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2797 is safe or user allows execution anyway. Always returns t if
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2798 `safe-functions' is t."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2799 (if (eq safe-functions t)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2800 t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2801 (setq checker (funcall checker formula))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2802 (if (not checker)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2803 t
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2804 (y-or-n-p (format "Formula %S\nmight be unsafe %S. Process it? "
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2805 formula checker)))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2806
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2807
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2808 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2809 ;;;; Standard formulas
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2810 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2811
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2812 (defmacro ses-range (from to)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2813 "Expands to a list of cell-symbols for the range. The range automatically
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2814 expands to include any new row or column inserted into its middle. The SES
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2815 library code specifically looks for the symbol `ses-range', so don't create an
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2816 alias for this macro!"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2817 (let (result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2818 (ses-dorange (cons from to)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2819 (push (ses-cell-symbol row col) result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2820 (cons 'list result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2821
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2822 (defun ses-delete-blanks (&rest args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2823 "Return ARGS reversed, with the blank elements (nil and *skip*) removed."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2824 (let (result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2825 (dolist (cur args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2826 (and cur (not (eq cur '*skip*))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2827 (push cur result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2828 result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2829
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2830 (defun ses+ (&rest args)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2831 "Compute the sum of the arguments, ignoring blanks."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2832 (apply '+ (apply 'ses-delete-blanks args)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2833
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2834 (defun ses-average (list)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2835 "Computes the sum of the numbers in LIST, divided by their length. Blanks
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2836 are ignored. Result is always floating-point, even if all args are integers."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2837 (setq list (apply 'ses-delete-blanks list))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2838 (/ (float (apply '+ list)) (length list)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2839
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2840 (defmacro ses-select (fromrange test torange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2841 "Select cells in FROMRANGE that are `equal' to TEST. For each match, return
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2842 the corresponding cell from TORANGE. The ranges are macroexpanded but not
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2843 evaluated so they should be either (ses-range BEG END) or (list ...). The
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2844 TEST is evaluated."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2845 (setq fromrange (cdr (macroexpand fromrange))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2846 torange (cdr (macroexpand torange))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2847 test (eval test))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2848 (or (= (length fromrange) (length torange))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2849 (error "ses-select: Ranges not same length"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2850 (let (result)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2851 (dolist (x fromrange)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2852 (if (equal test (symbol-value x))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2853 (push (car torange) result))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2854 (setq torange (cdr torange)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2855 (cons 'list result)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2856
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2857 ;;All standard formulas are safe
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2858 (dolist (x '(ses-range ses-delete-blanks ses+ ses-average ses-select))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2859 (put x 'side-effect-free t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2860
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2861
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2862 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2863 ;;;; Standard print functions
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2864 ;;;----------------------------------------------------------------------------
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2865
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2866 ;;These functions use the variables 'row' and 'col' that are
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2867 ;;dynamically bound by ses-print-cell. We define these varables at
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2868 ;;compile-time to make the compiler happy.
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2869 (eval-when-compile
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2870 (make-local-variable 'row)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2871 (make-local-variable 'col)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2872 ;;Don't use setq -- that gives a "free variable" compiler warning
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2873 (set 'row nil)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2874 (set 'col nil))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2875
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2876 (defun ses-center (value &optional span fill)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2877 "Print VALUE, centered within column. FILL is the fill character for
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2878 centering (default = space). SPAN indicates how many additional rightward
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2879 columns to include in width (default = 0)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2880 (let ((printer (or (ses-col-printer col) default-printer))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2881 (width (ses-col-width col))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2882 half)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2883 (or fill (setq fill ? ))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2884 (or span (setq span 0))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2885 (setq value (ses-call-printer printer value))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2886 (dotimes (x span)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2887 (setq width (+ width 1 (ses-col-width (+ col span (- x))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2888 (setq width (- width (length value)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2889 (if (<= width 0)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2890 value ;Too large for field, anyway
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2891 (setq half (make-string (/ width 2) fill))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2892 (concat half value half
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2893 (if (> (% width 2) 0) (char-to-string fill))))))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2894
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2895 (defun ses-center-span (value &optional fill)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2896 "Print VALUE, centered within the span that starts in the current column
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2897 and continues until the next nonblank column. FILL specifies the fill
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2898 character (default = space)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2899 (let ((end (1+ col)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2900 (while (and (< end numcols)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2901 (memq (ses-cell-value row end) '(nil *skip*)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2902 (setq end (1+ end)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2903 (ses-center value (- end col 1) fill)))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2904
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2905 (defun ses-dashfill (value &optional span)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2906 "Print VALUE centered using dashes. SPAN indicates how many rightward
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2907 columns to include in width (default = 0)."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2908 (ses-center value span ?-))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2909
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2910 (defun ses-dashfill-span (value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2911 "Print VALUE, centered using dashes within the span that starts in the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2912 current column and continues until the next nonblank column."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2913 (ses-center-span value ?-))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2914
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2915 (defun ses-tildefill-span (value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2916 "Print VALUE, centered using tildes within the span that starts in the
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2917 current column and continues until the next nonblank column."
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2918 (ses-center-span value ?~))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2919
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2920 (defun ses-unsafe (value)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2921 "Substitute for an unsafe formula or printer"
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2922 (error "Unsafe formula or printer"))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2923
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2924 ;;All standard printers are safe, including ses-unsafe!
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2925 (dolist (x (cons 'ses-unsafe ses-standard-printer-functions))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2926 (put x 'side-effect-free t))
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2927
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2928 (provide 'ses)
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2929
96b260e0ff3d New major mode "SES" for spreadsheets.
Jonathan Yavner <jyavner@member.fsf.org>
parents:
diff changeset
2930 ;; ses.el ends here.