-Assumes that BEG is at the beginning of a line."
- (let* ((inhibit-modification-hooks t)
- (inlinetaskp (featurep 'org-inlinetask))
- (get-real-level (lambda (pos lvl)
- (save-excursion
- (goto-char pos)
- (if (and inlinetaskp (org-inlinetask-in-task-p))
- (org-inlinetask-get-task-level)
- lvl))))
- (b beg)
- (e end)
- (level 0)
- (n 0)
- exit nstars)
- (with-silent-modifications
- (save-excursion
- (goto-char beg)
- (while (not exit)
- (setq e end)
- (if (not (re-search-forward org-indent-outline-re nil t))
- (setq e (point-max) exit t)
- (setq e (match-beginning 0))
- (if (>= e end) (setq exit t))
- (unless (and inlinetaskp (org-inlinetask-in-task-p))
- (setq level (- (match-end 0) (match-beginning 0) 1)))
- (setq nstars (* (1- (funcall get-real-level e level))
- (1- org-indent-indentation-per-level)))
- (add-text-properties
- (point-at-bol) (point-at-eol)
- (list 'line-prefix
- (aref org-indent-stars nstars)
- 'wrap-prefix
- (aref org-indent-strings
- (* (funcall get-real-level e level)
- org-indent-indentation-per-level)))))
- (when (> e b)
- (add-text-properties
- b e (list 'line-prefix (aref org-indent-strings n)
- 'wrap-prefix (aref org-indent-strings n))))
- (setq b (1+ (point-at-eol))
- n (* (funcall get-real-level b level)
- org-indent-indentation-per-level)))))))
-
-(defvar org-inlinetask-min-level)
-(defun org-indent-refresh-section ()
- "Refresh indentation properties in the current outline section.
-Point is assumed to be at the beginning of a headline."
- (interactive)
- (when org-indent-mode
- (let (beg end)
- (save-excursion
- (when (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
- (if (featurep 'org-inlinetask)
- (1- org-inlinetask-min-level)
- ""))))
- (org-back-to-heading)))
- (setq beg (point))
- (setq end (or (save-excursion (or (outline-next-heading) (point)))))
- (org-indent-remove-properties beg end)
- (org-indent-add-properties beg end))))))
-
-(defun org-indent-refresh-to (limit)
- "Refresh indentation properties in the current outline section.
-Point is assumed to be at the beginning of a headline."
- (interactive)
- (when org-indent-mode
- (let ((beg (point)) (end limit))
- (save-excursion
- (and (ignore-errors (let ((outline-regexp (format "\\*\\{1,%s\\}[ \t]+"
- (if (featurep 'org-inlinetask)
- (1- org-inlinetask-min-level)
- ""))))
- (org-back-to-heading)))
- (setq beg (point))))
- (org-indent-remove-properties beg end)
- (org-indent-add-properties beg end)))
- (goto-char limit))
-
-(defun org-indent-refresh-subtree ()
- "Refresh indentation properties in the current outline subtree.
-Point is assumed to be at the beginning of a headline."
- (interactive)
+
+When DELAY is non-nil, it must be a time value. In that case,
+the process is asynchronous and can be interrupted, either by
+user request, or after DELAY. This is done by throwing the
+`interrupt' tag along with the buffer position where the process
+stopped."
+ (save-match-data
+ (org-with-wide-buffer
+ (goto-char beg)
+ (beginning-of-line)
+ ;; 1. Initialize prefix at BEG. This is done by storing two
+ ;; variables: INLINE-PF and PF, representing respectively
+ ;; length of current `line-prefix' when line is inside an
+ ;; inline task or not.
+ (let* ((case-fold-search t)
+ (limited-re (org-get-limited-outline-regexp))
+ (added-ind-per-lvl (abs (1- org-indent-indentation-per-level)))
+ (pf (save-excursion
+ (and (ignore-errors (let ((outline-regexp limited-re))
+ (org-back-to-heading t)))
+ (+ (* org-indent-indentation-per-level
+ (- (match-end 0) (match-beginning 0) 2)) 2))))
+ (pf-inline (and (featurep 'org-inlinetask)
+ (org-inlinetask-in-task-p)
+ (+ (* org-indent-indentation-per-level
+ (1- (org-inlinetask-get-task-level))) 2)))
+ (time-limit (and delay (time-add (current-time) delay))))
+ ;; 2. For each line, set `line-prefix' and `wrap-prefix'
+ ;; properties depending on the type of line (headline,
+ ;; inline task, item or other).
+ (org-with-silent-modifications
+ (while (and (<= (point) end) (not (eobp)))
+ (cond
+ ;; When in asynchronous mode, check if interrupt is
+ ;; required.
+ ((and delay (input-pending-p)) (throw 'interrupt (point)))
+ ;; In asynchronous mode, take a break of
+ ;; `org-indent-agent-resume-delay' every DELAY to avoid
+ ;; blocking any other idle timer or process output.
+ ((and delay (time-less-p time-limit (current-time)))
+ (setq org-indent-agent-resume-timer
+ (run-with-idle-timer
+ (time-add (current-idle-time)
+ org-indent-agent-resume-delay)
+ nil #'org-indent-initialize-agent))
+ (throw 'interrupt (point)))
+ ;; Headline or inline task.
+ ((looking-at org-outline-regexp)
+ (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
+ (line (* added-ind-per-lvl (1- nstars)))
+ (wrap (+ line (1+ nstars))))
+ (cond
+ ;; Headline: new value for PF.
+ ((looking-at limited-re)
+ (org-indent-set-line-properties line wrap t)
+ (setq pf wrap))
+ ;; End of inline task: PF-INLINE is now nil.
+ ((looking-at "\\*+ end[ \t]*$")
+ (org-indent-set-line-properties line wrap 'inline)
+ (setq pf-inline nil))
+ ;; Start of inline task. Determine if it contains
+ ;; text, or if it is only one line long. Set
+ ;; PF-INLINE accordingly.
+ (t (org-indent-set-line-properties line wrap 'inline)
+ (setq pf-inline (and (org-inlinetask-in-task-p) wrap))))))
+ ;; List item: `wrap-prefix' is set where body starts.
+ ((org-at-item-p)
+ (let* ((line (or pf-inline pf 0))
+ (wrap (+ (org-list-item-body-column (point)) line)))
+ (org-indent-set-line-properties line wrap nil)))
+ ;; Normal line: use PF-INLINE, PF or nil as prefixes.
+ (t (let* ((line (or pf-inline pf 0))
+ (wrap (+ line (org-get-indentation))))
+ (org-indent-set-line-properties line wrap nil))))))))))
+
+(defun org-indent-notify-modified-headline (beg end)
+ "Set `org-indent-modified-headline-flag' depending on context.
+
+BEG and END are the positions of the beginning and end of the
+range of deleted text.
+
+This function is meant to be called by `before-change-functions'.
+Flag will be non-nil if command is going to modify or delete an
+headline."