Function: org-num--verify

org-num--verify is a byte-compiled function defined in org-num.el.gz.

Signature

(org-num--verify BEG END _)

Documentation

Check numbering integrity; update it if necessary.

This function is meant to be used in after-change-functions. See this variable for the meaning of BEG and END.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-num.el.gz
(defun org-num--verify (beg end _)
  "Check numbering integrity; update it if necessary.
This function is meant to be used in `after-change-functions'.
See this variable for the meaning of BEG and END."
  (setq org-num--missing-overlay nil)
  (save-match-data
    (org-with-point-at beg
      (let ((regexp (org-num--headline-regexp)))
        ;; At this point, directly altered overlays between BEG and
        ;; END are marked as invalid and will trigger a full update.
        ;; However, there are still two cases to handle.
        ;;
        ;; First, some valid overlays may need to be invalidated, due
        ;; to an indirect change.  That happens when the skip value --
        ;; see `org-num--skip-value' -- of the heading BEG belongs to
        ;; is altered, or when deleting the newline character right
        ;; before the next headline.
        (save-excursion
          ;; Bail out if we're before first headline or within
          ;; a headline too deep to be numbered.
          (when (and (org-with-limited-levels
                      (ignore-errors (org-back-to-heading t)))
                     (looking-at regexp))
            (pcase (get-char-property-and-overlay (point) 'org-num)
              (`(nil)
               ;; At a headline, without a numbering overlay: change
               ;; just created one.  Mark it for parsing.
               (setq org-num--missing-overlay (point)))
              (`(t . ,o)
               ;; Check if skip value changed.  Invalidate overlay
               ;; accordingly.
               (unless (eq (org-num--skip-value) (overlay-get o 'skip))
                 (org-num--invalidate-overlay o)))
              (_ nil))))
        ;; Deleting the newline character before a numbering overlay
        ;; doesn't invalidate it, even though it could land in the
        ;; middle of a line.  Be sure to catch this case.
        (when (and (= beg end) (not (bolp)))
          (pcase (get-char-property-and-overlay (point) 'org-num)
            (`(t . ,o) (org-num--invalidate-overlay o))
            (_ nil)))
        ;; Second, if nothing is marked as invalid, and therefore if
        ;; no full update is due so far, changes may still have
        ;; created new headlines, at BEG -- which is actually handled
        ;; by the previous phase --, or, in case of a multi-line
        ;; insertion, at END, or in-between.
        (unless (or org-num--invalid-flag
                    org-num--missing-overlay
                    (<= end (line-end-position))) ;single line change
          (forward-line)
          (when (or (re-search-forward regexp end 'move)
                    ;; Check if change created a headline after END.
                    (progn (skip-chars-backward "*") (looking-at regexp)))
            (setq org-num--missing-overlay (line-beginning-position))))))
    ;; Update numbering only if a headline was altered or created.
    (when (or org-num--missing-overlay org-num--invalid-flag)
      (org-num--update))))