Function: org-element--cache-sync

org-element--cache-sync is a byte-compiled function defined in org-element.el.gz.

Signature

(org-element--cache-sync BUFFER &optional THRESHOLD FUTURE-CHANGE OFFSET FORCE)

Documentation

Synchronize cache with recent modification in BUFFER.

When optional argument THRESHOLD is non-nil, do the synchronization for all elements starting before or at threshold, then exit. Otherwise, synchronize cache for as long as org-element-cache-sync-duration or until Emacs leaves idle state.

FUTURE-CHANGE, when non-nil, is a buffer position where changes not registered yet in the cache are going to happen. OFFSET is the change offset. It is used in org-element--cache-submit-request, where cache is partially updated before current modification are actually submitted.

FORCE, when non-nil will force the synchronization even when org-element--cache-active-p returns nil.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-element.el.gz
(defun org-element--cache-sync (buffer &optional threshold future-change offset force)
  "Synchronize cache with recent modification in BUFFER.

When optional argument THRESHOLD is non-nil, do the
synchronization for all elements starting before or at threshold,
then exit.  Otherwise, synchronize cache for as long as
`org-element-cache-sync-duration' or until Emacs leaves idle
state.

FUTURE-CHANGE, when non-nil, is a buffer position where changes
not registered yet in the cache are going to happen.  OFFSET is the
change offset.  It is used in `org-element--cache-submit-request',
where cache is partially updated before current modification are
actually submitted.

FORCE, when non-nil will force the synchronization even when
`org-element--cache-active-p' returns nil."
  (when (buffer-live-p buffer)
    (org-with-base-buffer buffer
      ;; Do not sync when, for example, in the middle of
      ;; `combine-change-calls'.  See the commentary inside
      ;; `org-element--cache-active-p'.  Such situation may occur when
      ;; sync timer triggers in the middle of `combine-change-calls'.
      (when (and org-element--cache-sync-requests
                 (or force (org-element--cache-active-p)))
        ;; Check if the buffer have been changed outside visibility of
        ;; `org-element--cache-before-change' and `org-element--cache-after-change'.
        (if (/= org-element--cache-last-buffer-size (buffer-size))
            (progn
              (org-element--cache-warn
               "Unregistered buffer modifications detected (%S != %S). Resetting.
If this warning appears regularly, please report the warning text to Org mode mailing list (M-x org-submit-bug-report).
The buffer is: %s\n Current command: %S\n Backtrace:\n%S"
               org-element--cache-last-buffer-size
               (buffer-size)
               (buffer-name (current-buffer))
               this-command
               (when (and (fboundp 'backtrace-get-frames)
                          (fboundp 'backtrace-to-string))
                 (backtrace-to-string (backtrace-get-frames 'backtrace))))
              (org-element-cache-reset))
          (let ((inhibit-quit t) request next)
            (setq org-element--cache-interrupt-C-g-count 0)
	    (when org-element--cache-sync-timer
	      (cancel-timer org-element--cache-sync-timer))
            (let ((time-limit (time-add nil org-element-cache-sync-duration)))
	      (catch 'org-element--cache-interrupt
                (when org-element--cache-sync-requests
                  (org-element--cache-log-message "Syncing down to %S-%S" (or future-change threshold) threshold))
                (while org-element--cache-sync-requests
                  (setq request (car org-element--cache-sync-requests)
                        next (nth 1 org-element--cache-sync-requests))
                  (org-element--cache-process-request
                   request
                   (when next (org-element--request-key next))
                   threshold
                   (unless threshold time-limit)
                   future-change
                   offset)
                  ;; Re-assign current and next requests.  It could have
                  ;; been altered during phase 1.
                  (setq request (car org-element--cache-sync-requests)
                        next (nth 1 org-element--cache-sync-requests))
                  ;; Request processed.  Merge current and next offsets and
                  ;; transfer ending position.
                  (when next
                    ;; The following requests can only be either phase 1
                    ;; or phase 2 requests.  We need to let them know
                    ;; that additional shifting happened ahead of them.
                    (cl-incf (org-element--request-offset next) (org-element--request-offset request))
                    (org-element--cache-log-message
                     "Updating next request offset to %S: %s"
                     (org-element--request-offset next)
                     (let ((print-length 10) (print-level 3)) (prin1-to-string next)))
                    ;; FIXME: END part of the request only matters for
                    ;; phase 0 requests.  However, the only possible
                    ;; phase 0 request must be the first request in the
                    ;; list all the time.  END position should be
                    ;; unused.
                    (setf (org-element--request-end next) (org-element--request-end request)))
                  (setq org-element--cache-sync-requests
                        (cdr org-element--cache-sync-requests)))))
	    ;; If more requests are awaiting, set idle timer accordingly.
	    ;; Otherwise, reset keys.
	    (if org-element--cache-sync-requests
                (org-element--cache-set-timer buffer)
              ;; NOTE: We cannot reset
              ;; `org-element--cache-change-warning' here as it might
              ;; still be needed when synchronization is called by
              ;; `org-element--cache-submit-request' before
              ;; `org-element--cache-for-removal'.
              (setq org-element--cache-sync-keys-value (1+ org-element--cache-sync-keys-value)))))))))