Function: vtable-update-object

vtable-update-object is a byte-compiled function defined in vtable.el.gz.

Signature

(vtable-update-object TABLE OBJECT &optional OLD-OBJECT)

Documentation

Update OBJECT's representation in TABLE.

If OLD-OBJECT is non-nil, replace OLD-OBJECT with OBJECT and display it. In either case, if the existing object is not found in the table (being compared with equal), signal an error. Note a limitation: if TABLE's buffer is not in a visible window, or if its window has changed width since it was updated, updating the TABLE is not possible, and an error is signaled.

Probably introduced at or before Emacs version 30.1.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/vtable.el.gz
(defun vtable-update-object (table object &optional old-object)
  "Update OBJECT's representation in TABLE.
If OLD-OBJECT is non-nil, replace OLD-OBJECT with OBJECT and display it.
In either case, if the existing object is not found in the table (being
compared with `equal'), signal an error.  Note a limitation: if TABLE's
buffer is not in a visible window, or if its window has changed width
since it was updated, updating the TABLE is not possible, and an error
is signaled."
  (unless old-object
    (setq old-object object))
  (let* ((objects (vtable-objects table))
         (inhibit-read-only t))
    ;; First replace the object in the object storage.
    (if (eq old-object (car objects))
        ;; It's at the head, so replace it there.
        (setf (vtable-objects table)
              (cons object (cdr objects)))
      ;; Otherwise splice into the list.
      (while (and (cdr objects)
                  (not (eq (cadr objects) old-object)))
        (setq objects (cdr objects)))
      (unless objects
        (error "Can't find the old object"))
      (setcar (cdr objects) object))
    ;; Then update the cache...
    ;; FIXME: If the table's buffer has no visible window, or if its
    ;; width has changed since the table was updated, the cache key will
    ;; not match and the object can't be updated.  (Bug #69837).
    (if-let ((line-number (seq-position (car (vtable--cache table)) old-object
                                        (lambda (a b)
                                          (equal (car a) b))))
             (line (elt (car (vtable--cache table)) line-number)))
        (progn
          (setcar line object)
          (setcdr line (vtable--compute-cached-line table object))
          ;; ... and redisplay the line in question.
          (save-excursion
            (vtable-goto-object old-object)
            (let ((keymap (get-text-property (point) 'keymap))
                  (start (point)))
              (delete-line)
              (vtable--insert-line table line line-number
                                   (nth 1 (vtable--cache table))
                                   (vtable--spacer table))
              (add-text-properties start (point) (list 'keymap keymap
                                                       'vtable table))))
          ;; We may have inserted a non-numerical value into a previously
          ;; all-numerical table, so recompute.
          (vtable--recompute-numerical table (cdr line)))
      (error "Can't find cached object in vtable"))))