Use `with-silent-modifications' instead of `org-unmodified' when it makes sense

* org-macs.el: Add a comment on when to use `org-unmodified'
and when to use `with-silent-modifications'.

* org-colview.el (org-columns-display-here)
(org-columns-remove-overlays, org-columns-quit)
(org-columns-edit-value, org-columns-compute-all)
(org-columns-compute, org-agenda-colview-compute):
* org-clock.el (org-clock-sum):
* org.el (org-refresh-category-properties)
(org-refresh-properties, org-entry-blocked-p)
(org-agenda-prepare-buffers): Use `with-silent-modifications'
instead of `org-unmodified'.

Thanks to Stefan Monnier for reminding me about `with-silent-modifications'!
This commit is contained in:
Bastien Guerry 2013-02-23 14:57:51 +01:00
parent 64aae2fd29
commit 43c8aa02cc
4 changed files with 176 additions and 176 deletions

View File

@ -1700,85 +1700,85 @@ each headline in the time range with point at the headline. Headlines for
which HEADLINE-FILTER returns nil are excluded from the clock summation. which HEADLINE-FILTER returns nil are excluded from the clock summation.
PROPNAME lets you set a custom text property instead of :org-clock-minutes." PROPNAME lets you set a custom text property instead of :org-clock-minutes."
(interactive) (interactive)
(org-unmodified (with-silent-modifications
(let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*" (let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
org-clock-string org-clock-string
"[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)")) "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
(lmax 30) (lmax 30)
(ltimes (make-vector lmax 0)) (ltimes (make-vector lmax 0))
(t1 0) (t1 0)
(level 0) (level 0)
ts te dt ts te dt
time) time)
(if (stringp tstart) (setq tstart (org-time-string-to-seconds tstart))) (if (stringp tstart) (setq tstart (org-time-string-to-seconds tstart)))
(if (stringp tend) (setq tend (org-time-string-to-seconds tend))) (if (stringp tend) (setq tend (org-time-string-to-seconds tend)))
(if (consp tstart) (setq tstart (org-float-time tstart))) (if (consp tstart) (setq tstart (org-float-time tstart)))
(if (consp tend) (setq tend (org-float-time tend))) (if (consp tend) (setq tend (org-float-time tend)))
(remove-text-properties (point-min) (point-max) (remove-text-properties (point-min) (point-max)
`(,(or propname :org-clock-minutes) t `(,(or propname :org-clock-minutes) t
:org-clock-force-headline-inclusion t)) :org-clock-force-headline-inclusion t))
(save-excursion (save-excursion
(goto-char (point-max)) (goto-char (point-max))
(while (re-search-backward re nil t) (while (re-search-backward re nil t)
(cond (cond
((match-end 2) ((match-end 2)
;; Two time stamps ;; Two time stamps
(setq ts (match-string 2) (setq ts (match-string 2)
te (match-string 3) te (match-string 3)
ts (org-float-time ts (org-float-time
(apply 'encode-time (org-parse-time-string ts))) (apply 'encode-time (org-parse-time-string ts)))
te (org-float-time te (org-float-time
(apply 'encode-time (org-parse-time-string te))) (apply 'encode-time (org-parse-time-string te)))
ts (if tstart (max ts tstart) ts) ts (if tstart (max ts tstart) ts)
te (if tend (min te tend) te) te (if tend (min te tend) te)
dt (- te ts) dt (- te ts)
t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))) t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
((match-end 4) ((match-end 4)
;; A naked time ;; A naked time
(setq t1 (+ t1 (string-to-number (match-string 5)) (setq t1 (+ t1 (string-to-number (match-string 5))
(* 60 (string-to-number (match-string 4)))))) (* 60 (string-to-number (match-string 4))))))
(t ;; A headline (t ;; A headline
;; Add the currently clocking item time to the total ;; Add the currently clocking item time to the total
(when (and org-clock-report-include-clocking-task (when (and org-clock-report-include-clocking-task
(equal (org-clocking-buffer) (current-buffer)) (equal (org-clocking-buffer) (current-buffer))
(equal (marker-position org-clock-hd-marker) (point)) (equal (marker-position org-clock-hd-marker) (point))
tstart tstart
tend tend
(>= (org-float-time org-clock-start-time) tstart) (>= (org-float-time org-clock-start-time) tstart)
(<= (org-float-time org-clock-start-time) tend)) (<= (org-float-time org-clock-start-time) tend))
(let ((time (floor (- (org-float-time) (let ((time (floor (- (org-float-time)
(org-float-time org-clock-start-time)) 60))) (org-float-time org-clock-start-time)) 60)))
(setq t1 (+ t1 time)))) (setq t1 (+ t1 time))))
(let* ((headline-forced (let* ((headline-forced
(get-text-property (point) (get-text-property (point)
:org-clock-force-headline-inclusion)) :org-clock-force-headline-inclusion))
(headline-included (headline-included
(or (null headline-filter) (or (null headline-filter)
(save-excursion (save-excursion
(save-match-data (funcall headline-filter)))))) (save-match-data (funcall headline-filter))))))
(setq level (- (match-end 1) (match-beginning 1))) (setq level (- (match-end 1) (match-beginning 1)))
(when (or (> t1 0) (> (aref ltimes level) 0)) (when (or (> t1 0) (> (aref ltimes level) 0))
(when (or headline-included headline-forced) (when (or headline-included headline-forced)
(if headline-included (if headline-included
(loop for l from 0 to level do (loop for l from 0 to level do
(aset ltimes l (+ (aref ltimes l) t1)))) (aset ltimes l (+ (aref ltimes l) t1))))
(setq time (aref ltimes level)) (setq time (aref ltimes level))
(goto-char (match-beginning 0)) (goto-char (match-beginning 0))
(put-text-property (point) (point-at-eol) (put-text-property (point) (point-at-eol)
(or propname :org-clock-minutes) time) (or propname :org-clock-minutes) time)
(if headline-filter (if headline-filter
(save-excursion (save-excursion
(save-match-data (save-match-data
(while (while
(> (funcall outline-level) 1) (> (funcall outline-level) 1)
(outline-up-heading 1 t) (outline-up-heading 1 t)
(put-text-property (put-text-property
(point) (point-at-eol) (point) (point-at-eol)
:org-clock-force-headline-inclusion t)))))) :org-clock-force-headline-inclusion t))))))
(setq t1 0) (setq t1 0)
(loop for l from level to (1- lmax) do (loop for l from level to (1- lmax) do
(aset ltimes l 0))))))) (aset ltimes l 0)))))))
(setq org-clock-file-total-minutes (aref ltimes 0)))))) (setq org-clock-file-total-minutes (aref ltimes 0))))))
(defun org-clock-sum-current-item (&optional tstart) (defun org-clock-sum-current-item (&optional tstart)
"Return time, clocked on current item in total." "Return time, clocked on current item in total."

View File

@ -223,17 +223,17 @@ This is the compiled version of the format.")
(setq s2 (org-columns-add-ellipses (or modval val) width)) (setq s2 (org-columns-add-ellipses (or modval val) width))
(setq string (format f s2)) (setq string (format f s2))
;; Create the overlay ;; Create the overlay
(org-unmodified (with-silent-modifications
(setq ov (org-columns-new-overlay (setq ov (org-columns-new-overlay
beg (setq beg (1+ beg)) string (if dateline face1 face))) beg (setq beg (1+ beg)) string (if dateline face1 face)))
(overlay-put ov 'keymap org-columns-map) (overlay-put ov 'keymap org-columns-map)
(overlay-put ov 'org-columns-key property) (overlay-put ov 'org-columns-key property)
(overlay-put ov 'org-columns-value (cdr ass)) (overlay-put ov 'org-columns-value (cdr ass))
(overlay-put ov 'org-columns-value-modified modval) (overlay-put ov 'org-columns-value-modified modval)
(overlay-put ov 'org-columns-pom pom) (overlay-put ov 'org-columns-pom pom)
(overlay-put ov 'org-columns-format f) (overlay-put ov 'org-columns-format f)
(overlay-put ov 'line-prefix "") (overlay-put ov 'line-prefix "")
(overlay-put ov 'wrap-prefix "")) (overlay-put ov 'wrap-prefix ""))
(if (or (not (char-after beg)) (if (or (not (char-after beg))
(equal (char-after beg) ?\n)) (equal (char-after beg) ?\n))
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
@ -332,11 +332,11 @@ for the duration of the command.")
(remove-hook 'post-command-hook 'org-columns-hscoll-title 'local)) (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
(move-marker org-columns-begin-marker nil) (move-marker org-columns-begin-marker nil)
(move-marker org-columns-top-level-marker nil) (move-marker org-columns-top-level-marker nil)
(org-unmodified (with-silent-modifications
(mapc 'delete-overlay org-columns-overlays) (mapc 'delete-overlay org-columns-overlays)
(setq org-columns-overlays nil) (setq org-columns-overlays nil)
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(remove-text-properties (point-min) (point-max) '(read-only t)))) (remove-text-properties (point-min) (point-max) '(read-only t))))
(when org-columns-flyspell-was-active (when org-columns-flyspell-was-active
(flyspell-mode 1)) (flyspell-mode 1))
(when (local-variable-p 'org-colview-initial-truncate-line-value) (when (local-variable-p 'org-colview-initial-truncate-line-value)
@ -384,10 +384,10 @@ CPHR is the complex heading regexp to use for parsing ITEM."
(defun org-columns-quit () (defun org-columns-quit ()
"Remove the column overlays and in this way exit column editing." "Remove the column overlays and in this way exit column editing."
(interactive) (interactive)
(org-unmodified (with-silent-modifications
(org-columns-remove-overlays) (org-columns-remove-overlays)
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(remove-text-properties (point-min) (point-max) '(read-only t)))) (remove-text-properties (point-min) (point-max) '(read-only t))))
(when (eq major-mode 'org-agenda-mode) (when (eq major-mode 'org-agenda-mode)
(setq org-agenda-columns-active nil) (setq org-agenda-columns-active nil)
(message (message
@ -488,9 +488,9 @@ Where possible, use the standard interface for changing this line."
(org-agenda-columns))) (org-agenda-columns)))
(t (t
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(org-unmodified (with-silent-modifications
(remove-text-properties (remove-text-properties
(max (point-min) (1- bol)) eol '(read-only t))) (max (point-min) (1- bol)) eol '(read-only t)))
(unwind-protect (unwind-protect
(progn (progn
(setq org-columns-overlays (setq org-columns-overlays
@ -920,8 +920,8 @@ Don't set this, this is meant for dynamic scoping.")
(defun org-columns-compute-all () (defun org-columns-compute-all ()
"Compute all columns that have operators defined." "Compute all columns that have operators defined."
(org-unmodified (with-silent-modifications
(remove-text-properties (point-min) (point-max) '(org-summaries t))) (remove-text-properties (point-min) (point-max) '(org-summaries t)))
(let ((columns org-columns-current-fmt-compiled) (let ((columns org-columns-current-fmt-compiled)
(org-columns-time (time-to-number-of-days (current-time))) (org-columns-time (time-to-number-of-days (current-time)))
col) col)
@ -996,9 +996,9 @@ Don't set this, this is meant for dynamic scoping.")
(if (assoc property sum-alist) (if (assoc property sum-alist)
(setcdr (assoc property sum-alist) useval) (setcdr (assoc property sum-alist) useval)
(push (cons property useval) sum-alist) (push (cons property useval) sum-alist)
(org-unmodified (with-silent-modifications
(add-text-properties sumpos (1+ sumpos) (add-text-properties sumpos (1+ sumpos)
(list 'org-summaries sum-alist)))) (list 'org-summaries sum-alist))))
(when (and val (not (equal val (if flag str val)))) (when (and val (not (equal val (if flag str val))))
(org-entry-put nil property (if flag str val))) (org-entry-put nil property (if flag str val)))
;; add current to current level accumulator ;; add current to current level accumulator
@ -1509,9 +1509,8 @@ This will add overlays to the date lines, to show the summary for each day."
(save-excursion (save-excursion
(save-restriction (save-restriction
(widen) (widen)
(org-unmodified (with-silent-modifications
(remove-text-properties (point-min) (point-max) (remove-text-properties (point-min) (point-max) '(org-summaries t)))
'(org-summaries t)))
(goto-char (point-min)) (goto-char (point-min))
(org-columns-get-format-and-top-level) (org-columns-get-format-and-top-level)
(while (setq fm (pop fmt)) (while (setq fm (pop fmt))

View File

@ -117,6 +117,8 @@ Otherwise return nil."
(def-edebug-spec org-preserve-lc (body)) (def-edebug-spec org-preserve-lc (body))
;; Copied from bookmark.el ;; Copied from bookmark.el
;; Use `org-unmodified' to ignore real modifications, otherwise
;; `with-silent-modifications' is enough to ignore cosmetic ones
(defmacro org-unmodified (&rest body) (defmacro org-unmodified (&rest body)
"Run BODY while preserving the buffer's `buffer-modified-p' state." "Run BODY while preserving the buffer's `buffer-modified-p' state."
(org-with-gensyms (was-modified) (org-with-gensyms (was-modified)

View File

@ -8948,24 +8948,24 @@ call CMD."
((symbolp org-category) (symbol-name org-category)) ((symbolp org-category) (symbol-name org-category))
(t org-category))) (t org-category)))
beg end cat pos optionp) beg end cat pos optionp)
(org-unmodified (with-silent-modifications
(save-excursion (save-excursion
(save-restriction (save-restriction
(widen) (widen)
(goto-char (point-min)) (goto-char (point-min))
(put-text-property (point) (point-max) 'org-category def-cat) (put-text-property (point) (point-max) 'org-category def-cat)
(while (re-search-forward (while (re-search-forward
"^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t) "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
(setq pos (match-end 0) (setq pos (match-end 0)
optionp (equal (char-after (match-beginning 0)) ?#) optionp (equal (char-after (match-beginning 0)) ?#)
cat (org-trim (match-string 2))) cat (org-trim (match-string 2)))
(if optionp (if optionp
(setq beg (point-at-bol) end (point-max)) (setq beg (point-at-bol) end (point-max))
(org-back-to-heading t) (org-back-to-heading t)
(setq beg (point) end (org-end-of-subtree t t))) (setq beg (point) end (org-end-of-subtree t t)))
(put-text-property beg end 'org-category cat) (put-text-property beg end 'org-category cat)
(put-text-property beg end 'org-category-position beg) (put-text-property beg end 'org-category-position beg)
(goto-char pos))))))) (goto-char pos)))))))
(defun org-refresh-properties (dprop tprop) (defun org-refresh-properties (dprop tprop)
"Refresh buffer text properties. "Refresh buffer text properties.
@ -8973,17 +8973,17 @@ DPROP is the drawer property and TPROP is the corresponding text
property to set." property to set."
(let ((case-fold-search t) (let ((case-fold-search t)
(inhibit-read-only t) p) (inhibit-read-only t) p)
(org-unmodified (with-silent-modifications
(save-excursion (save-excursion
(save-restriction (save-restriction
(widen) (widen)
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t) (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
(setq p (org-match-string-no-properties 1)) (setq p (org-match-string-no-properties 1))
(save-excursion (save-excursion
(org-back-to-heading t) (org-back-to-heading t)
(put-text-property (put-text-property
(point-at-bol) (point-at-eol) tprop p)))))))) (point-at-bol) (point-at-eol) tprop p))))))))
;;;; Link Stuff ;;;; Link Stuff
@ -12159,16 +12159,15 @@ changes because there are unchecked boxes in this entry."
(defun org-entry-blocked-p () (defun org-entry-blocked-p ()
"Is the current entry blocked?" "Is the current entry blocked?"
(org-unmodified (with-silent-modifications
(if (org-entry-get nil "NOBLOCKING") (if (org-entry-get nil "NOBLOCKING")
nil ;; Never block this entry nil ;; Never block this entry
(not (not (run-hook-with-args-until-failure
(run-hook-with-args-until-failure 'org-blocker-hook
'org-blocker-hook (list :type 'todo-state-change
(list :type 'todo-state-change :position (point)
:position (point) :from 'todo
:from 'todo :to 'done))))))
:to 'done))))))
(defun org-update-statistics-cookies (all) (defun org-update-statistics-cookies (all)
"Update the statistics cookie, either from TODO or from checkboxes. "Update the statistics cookie, either from TODO or from checkboxes.
@ -17601,34 +17600,34 @@ When a buffer is unmodified, it is just killed. When modified, it is saved
(org-check-agenda-file file) (org-check-agenda-file file)
(set-buffer (org-get-agenda-file-buffer file))) (set-buffer (org-get-agenda-file-buffer file)))
(widen) (widen)
(org-unmodified (with-silent-modifications
(org-refresh-category-properties) (org-refresh-category-properties)
(org-refresh-properties org-effort-property 'org-effort) (org-refresh-properties org-effort-property 'org-effort)
(org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime) (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
(setq org-todo-keywords-for-agenda (setq org-todo-keywords-for-agenda
(append org-todo-keywords-for-agenda org-todo-keywords-1)) (append org-todo-keywords-for-agenda org-todo-keywords-1))
(setq org-done-keywords-for-agenda (setq org-done-keywords-for-agenda
(append org-done-keywords-for-agenda org-done-keywords)) (append org-done-keywords-for-agenda org-done-keywords))
(setq org-todo-keyword-alist-for-agenda (setq org-todo-keyword-alist-for-agenda
(append org-todo-keyword-alist-for-agenda org-todo-key-alist)) (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
(setq org-drawers-for-agenda (setq org-drawers-for-agenda
(append org-drawers-for-agenda org-drawers)) (append org-drawers-for-agenda org-drawers))
(setq org-tag-alist-for-agenda (setq org-tag-alist-for-agenda
(append org-tag-alist-for-agenda org-tag-alist)) (append org-tag-alist-for-agenda org-tag-alist))
(save-excursion (save-excursion
(remove-text-properties (point-min) (point-max) pall) (remove-text-properties (point-min) (point-max) pall)
(when org-agenda-skip-archived-trees (when org-agenda-skip-archived-trees
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward rea nil t) (while (re-search-forward rea nil t)
(if (org-at-heading-p t) (if (org-at-heading-p t)
(add-text-properties (point-at-bol) (org-end-of-subtree t) pa)))) (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
(goto-char (point-min)) (goto-char (point-min))
(setq re (format org-heading-keyword-regexp-format (setq re (format org-heading-keyword-regexp-format
org-comment-string)) org-comment-string))
(while (re-search-forward re nil t) (while (re-search-forward re nil t)
(add-text-properties (add-text-properties
(match-beginning 0) (org-end-of-subtree t) pc)))))))) (match-beginning 0) (org-end-of-subtree t) pc))))))))
(setq org-todo-keywords-for-agenda (setq org-todo-keywords-for-agenda
(org-uniquify org-todo-keywords-for-agenda)) (org-uniquify org-todo-keywords-for-agenda))
(setq org-todo-keyword-alist-for-agenda (setq org-todo-keyword-alist-for-agenda