Function: ses-load

ses-load is a byte-compiled function defined in ses.el.gz.

Signature

(ses-load)

Documentation

Parse the current buffer and set up buffer-local variables.

Does not execute cell formulas or print functions.

Source Code

;; Defined in /usr/src/emacs/lisp/ses.el.gz
;;----------------------------------------------------------------------------
;; Startup for major mode
;;----------------------------------------------------------------------------

(defun ses-load ()
  "Parse the current buffer and set up buffer-local variables.
Does not execute cell formulas or print functions."
  (widen)
  ;; Read our global parameters, which should be a 3-element list.
  (goto-char (point-max))
  (search-backward ";; Local Variables:\n" nil t)
  (backward-list 1)
  (setq ses--params-marker (point-marker))
  (let* ((params (ignore-errors (read (current-buffer))))
	 (params-len (safe-length params)))
    (or (and (>=  params-len 3)
	     (<=  params-len 4)
	     (numberp (car params))
	     (numberp (cadr params))
	     (>= (cadr params) 0)
	     (numberp (nth 2 params))
	     (> (nth 2 params) 0)
	     (or (<= params-len 3)
		 (let ((numlocprn (nth 3 params)))
		   (and (integerp numlocprn) (>= numlocprn 0)))))
	(error "Invalid SES file"))
    (setq ses--file-format (car params)
	  ses--numrows     (cadr params)
	  ses--numcols     (nth 2 params)
	  ses--numlocprn (or (nth 3 params) 0))
    (when (= ses--file-format 1)
      (let (buffer-undo-list) ; This is not undoable.
	(ses-goto-data 'ses--header-row)
	(insert "(ses-header-row 0)\n")
	(ses-set-parameter 'ses--file-format 3)
	(message "Upgrading from SES-1 to SES-2 file format")))
    (or (<= ses--file-format 3)
	(error "This file needs a newer version of the SES library code"))
    ;; Initialize cell array.
    (setq ses--cells (make-vector ses--numrows nil))
    (dotimes (row ses--numrows)
      (aset ses--cells row (make-vector ses--numcols nil)))
    ;; initialize local printer map.
    (clrhash ses--local-printer-hashmap))

  ;; Skip over print area, which we assume is correct.
  (goto-char (point-min))
  (forward-line ses--numrows)
  (or (looking-at-p ses-print-data-boundary)
      (error "Missing marker between print and data areas"))
  (forward-char 1)
  (setq ses--data-marker (point-marker))
  (forward-char (1- (length ses-print-data-boundary)))
  ;; Initialize printer and symbol lists.
  (mapc #'ses-printer-record ses-standard-printer-functions)
  (setq ses--symbolic-formulas                   nil)

  ;; Load local printer definitions.
  ;; This must be loaded *BEFORE* cells and column printers because the latter
  ;; may call them.
  (save-excursion
    (forward-line (* ses--numrows (1+ ses--numcols)))
    (let ((numlocprn ses--numlocprn))
      (setq ses--numlocprn 0)
      (dotimes (_ numlocprn)
	(let ((x      (read (current-buffer))))
	  (or (and (= (following-char) ?\n)
		   (eq (car-safe x) 'ses-local-printer)
		   (apply #'ses--local-printer (cdr x)))
              (error "Local printer-def error"))
	  (setq ses--numlocprn (1+ ses--numlocprn))))))
  ;; Load cell definitions.
  (dotimes (row ses--numrows)
    (dotimes (col ses--numcols)
      (let* ((x      (read (current-buffer)))
	     (sym  (car-safe (cdr-safe x))))
	(or (and (= (following-char) ?\n)
		 (eq (car-safe x) 'ses-cell)
		 (ses-create-cell-variable sym row col))
	    (error "Cell-def error"))
	(apply #'ses--cell (cdr x))))
    (or (looking-at-p "\n\n")
	(error "Missing blank line between rows")))
  ;; Skip local printer function declaration --- that were already loaded.
  (forward-line (+ 2 ses--numlocprn))
  ;; Load global parameters.
  (let ((widths      (read (current-buffer)))
	(n1          (char-after (point)))
	(printers    (read (current-buffer)))
	(n2          (char-after (point)))
	(def-printer (read (current-buffer)))
	(n3          (char-after (point)))
	(head-row    (read (current-buffer)))
	(n4          (char-after (point))))
    (or (and (eq (car-safe widths) 'ses-column-widths)
	     (= n1 ?\n)
	     (eq (car-safe printers) 'ses-column-printers)
	     (= n2 ?\n)
	     (eq (car-safe def-printer) 'ses-default-printer)
	     (= n3 ?\n)
	     (eq (car-safe head-row) 'ses-header-row)
	     (= n4 ?\n))
	(error "Invalid SES global parameters"))
    (1value (eval widths t))
    (1value (eval def-printer t))
    (1value (eval printers t))
    (1value (eval head-row t)))
  ;; Should be back at global-params.
  (forward-char 1)
  (or (looking-at-p ses-initial-global-parameters-re)
      (error "Problem with column-defs or global-params"))
  ;; Check for overall newline count in definitions area.
  (forward-line 3)
  (let ((start (point)))
    (ses-goto-data 'ses--numrows)
    (or (= (point) start)
	(error "Extraneous newlines someplace?"))))