Function: kfile:update

kfile:update is a byte-compiled function defined in kfile.el.

Signature

(kfile:update &optional VISIBLE-ONLY-P)

Documentation

Update kfile internal structure so that view is ready for saving to a file.

Leave outline file expanded with structure data showing unless optional VISIBLE-ONLY-P is non-nil. Signal an error if kotl is not attached to a file.

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/kotl/kfile.el
(defun kfile:update (&optional visible-only-p)
  "Update kfile internal structure so that view is ready for saving to a file.
Leave outline file expanded with structure data showing unless optional
VISIBLE-ONLY-P is non-nil.  Signal an error if kotl is not attached to a file."
  (let* ((top (kview:top-cell kotl-kview))
	 (file (hypb:buffer-file-name))
	 (label-type (kview:label-type kotl-kview))
	 (label-min-width (kview:label-min-width kotl-kview))
	 (label-separator (kview:label-separator kotl-kview))
	 (level-indent (kview:level-indent kotl-kview))
	 ;; If this happens to be non-nil, it is virtually impossible to save
	 ;; a file, so ensure it is nil.
	 (debug-on-error))
    (cond ((null file)
	   (error "(kfile:update): Current outline is not attached to a file"))
	  ((not (file-writable-p file))
	   (error "(kfile:update): File \"%s\" is not writable" file)))
    (let* ((buffer-read-only)
	   (id-counter (kcell:get-attr top 'id-counter))
	   (kcell-data (make-vector (1+ id-counter) nil))
	   (standard-output (current-buffer))
	   (opoint (set-marker (make-marker) (point)))
	   (kcell-num 1)
	   cell)
      ;;
      ;; Prepare cell data for saving.
      (kfile:narrow-to-kcells)
      (kview:map-tree
        (lambda (_view)
	  (setq cell (kcell-view:cell))
	  (aset kcell-data
		kcell-num
		(kcell-data:create cell (kcell-view:idstamp-integer)))
	  (setq kcell-num (1+ kcell-num)))
	kotl-kview t)
      ;; Save top cell, 0, last since above loop may increment the total
      ;; number of cells counter stored in it, if any invalid cells are
      ;; encountered.
      (aset kcell-data 0 (kcell-data:create top 0))
      (setq id-counter (kcell:get-attr top 'id-counter))
      ;;
      (widen)
      (goto-char (point-min))
      (when (search-forward "\n\^_\n" nil t)
	(delete-region (point-min) (match-end 0)))
      (princ ";; -*- Mode: kotl -*- \n")
      (prin1 kfile:version)
      (princ " ;; file-format\n\^_\n")
      ;; Skip past cells.
      (if (search-forward "\n\^_\n" nil t)
	  ;; Get rid of excess blank lines after last cell.
	  (progn (goto-char (match-beginning 0))
		 (skip-chars-backward "\n")
		 (delete-region (point) (point-max)))
	(goto-char (point-max)))
      ;; Ensure that last cell has two newlines after it so that
      ;; kfile:insert-attributes finds it.
      (princ "\n\n\^_\n")
      (princ (format (concat
		      "%S ;; kvspec:current\n%d ;; id-counter\n"
		      "%S ;; label-type\n%d ;; label-min-width\n"
		      "%S ;; label-separator\n%d ;; level-indent\n")
		     kvspec:current id-counter label-type label-min-width
		     label-separator level-indent))
      (princ "\^_\n;; depth-first kcell attributes\n")
      (kfile:pretty-print kcell-data)
      ;;
      ;; Don't re-narrow buffer by default since this is used in
      ;; write-contents-hooks after save-buffer has widened buffer.  If
      ;; buffer is narrowed here, only the narrowed portion will be saved to
      ;; the file.  Narrow as an option since saving only the portion of the
      ;; file visible in a view may be useful in some situations.
      (when visible-only-p (kfile:narrow-to-kcells))
      ;;
      ;; Return point to its original position as given by the opoint marker.
      (goto-char opoint)
      (set-marker opoint nil)
      nil)))