Browse Source

Use org-with-wide-buffer where appropriate

* lisp/org-agenda.el (org-agenda-get-some-entry-text):
(org-agenda-refile, org-agenda-open-link):
(org-agenda-change-all-lines, org-agenda-clock-out):
* lisp/org-capture.el (org-capture-refile):
(org-capture-bookmark-last-stored-position):
* lisp/org-clock.el (org-with-clock-position):
(org-clock-in):
* lisp/org-table.el (org-table-recalculate-buffer-tables):
(org-table-iterate-buffer-tables):
* lisp/org.el (org-table-map-tables):
(org-outline-overlay-data, org-refresh-properties):
(org-refresh-stats-properties, org-offer-links-in-entry):
(org-refile, org-refile-check-position):
(org-refile-new-child, org-map-entries):
(org-find-exact-headline-in-buffer):
(org-reftex-citation, org-imenu-get-tree):
Use `org-with-wide-buffer'.
Aaron Ecay 3 years ago
parent
commit
ab04bab4d6
5 changed files with 422 additions and 468 deletions
  1. 92 102
      lisp/org-agenda.el
  2. 11 15
      lisp/org-capture.el
  3. 113 117
      lisp/org-clock.el
  4. 13 17
      lisp/org-table.el
  5. 193 217
      lisp/org.el

+ 92 - 102
lisp/org-agenda.el

@@ -3454,79 +3454,77 @@ removed from the entry content.  Currently only `planning' is allowed here."
       (with-current-buffer (marker-buffer marker)
 	(if (not (derived-mode-p 'org-mode))
 	    (setq txt "")
-	  (save-excursion
-	    (save-restriction
-	      (widen)
-	      (goto-char marker)
-	      (end-of-line 1)
-	      (setq txt (buffer-substring
-			 (min (1+ (point)) (point-max))
-			 (progn (outline-next-heading) (point)))
-		    drawer-re org-drawer-regexp
-		    kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
-					".*\n?"))
-	      (with-temp-buffer
-		(insert txt)
-		(when org-agenda-add-entry-text-descriptive-links
-		  (goto-char (point-min))
-		  (while (org-activate-bracket-links (point-max))
-		    (add-text-properties (match-beginning 0) (match-end 0)
-					 '(face org-link))))
-		(goto-char (point-min))
-		(while (re-search-forward org-bracket-link-regexp (point-max) t)
-		  (set-text-properties (match-beginning 0) (match-end 0)
-				       nil))
-		(goto-char (point-min))
-		(while (re-search-forward drawer-re nil t)
-		  (delete-region
-		   (match-beginning 0)
-		   (progn (re-search-forward
-			   "^[ \t]*:END:.*\n?" nil 'move)
-			  (point))))
-		(unless (member 'planning keep)
-		  (goto-char (point-min))
-		  (while (re-search-forward kwd-time-re nil t)
-		    (replace-match "")))
-		(goto-char (point-min))
-		(when org-agenda-entry-text-exclude-regexps
-		  (let ((re-list org-agenda-entry-text-exclude-regexps)	re)
-		    (while (setq re (pop re-list))
-		      (goto-char (point-min))
-		      (while (re-search-forward re nil t)
-			(replace-match "")))))
-		(goto-char (point-max))
-		(skip-chars-backward " \t\n")
-		(if (looking-at "[ \t\n]+\\'") (replace-match ""))
-
-		;; find and remove min common indentation
-		(goto-char (point-min))
-		(untabify (point-min) (point-max))
-		(setq ind (org-get-indentation))
-		(while (not (eobp))
-		  (unless (looking-at "[ \t]*$")
-		    (setq ind (min ind (org-get-indentation))))
-		  (beginning-of-line 2))
-		(goto-char (point-min))
-		(while (not (eobp))
-		  (unless (looking-at "[ \t]*$")
-		    (move-to-column ind)
-		    (delete-region (point-at-bol) (point)))
-		  (beginning-of-line 2))
-
-		(run-hooks 'org-agenda-entry-text-cleanup-hook)
-
-		(goto-char (point-min))
-		(when indent
-		  (while (and (not (eobp)) (re-search-forward "^" nil t))
-		    (replace-match indent t t)))
-		(goto-char (point-min))
-		(while (looking-at "[ \t]*\n") (replace-match ""))
-		(goto-char (point-max))
-		(when (> (org-current-line)
-			 n-lines)
-		  (org-goto-line (1+ n-lines))
-		  (backward-char 1))
-		(setq txt (buffer-substring (point-min) (point)))))))))
+	  (org-with-wide-buffer
+	   (goto-char marker)
+	   (end-of-line 1)
+	   (setq txt (buffer-substring
+		      (min (1+ (point)) (point-max))
+		      (progn (outline-next-heading) (point)))
+		 drawer-re org-drawer-regexp
+		 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
+				     ".*\n?"))
+	   (with-temp-buffer
+	     (insert txt)
+	     (when org-agenda-add-entry-text-descriptive-links
+	       (goto-char (point-min))
+	       (while (org-activate-bracket-links (point-max))
+		 (add-text-properties (match-beginning 0) (match-end 0)
+				      '(face org-link))))
+	     (goto-char (point-min))
+	     (while (re-search-forward org-bracket-link-regexp (point-max) t)
+	       (set-text-properties (match-beginning 0) (match-end 0)
+				    nil))
+	     (goto-char (point-min))
+	     (while (re-search-forward drawer-re nil t)
+	       (delete-region
+		(match-beginning 0)
+		(progn (re-search-forward
+			"^[ \t]*:END:.*\n?" nil 'move)
+		       (point))))
+	     (unless (member 'planning keep)
+	       (goto-char (point-min))
+	       (while (re-search-forward kwd-time-re nil t)
+		 (replace-match "")))
+	     (goto-char (point-min))
+	     (when org-agenda-entry-text-exclude-regexps
+	       (let ((re-list org-agenda-entry-text-exclude-regexps)	re)
+		 (while (setq re (pop re-list))
+		   (goto-char (point-min))
+		   (while (re-search-forward re nil t)
+		     (replace-match "")))))
+	     (goto-char (point-max))
+	     (skip-chars-backward " \t\n")
+	     (if (looking-at "[ \t\n]+\\'") (replace-match ""))
+
+	     ;; find and remove min common indentation
+	     (goto-char (point-min))
+	     (untabify (point-min) (point-max))
+	     (setq ind (org-get-indentation))
+	     (while (not (eobp))
+	       (unless (looking-at "[ \t]*$")
+		 (setq ind (min ind (org-get-indentation))))
+	       (beginning-of-line 2))
+	     (goto-char (point-min))
+	     (while (not (eobp))
+	       (unless (looking-at "[ \t]*$")
+		 (move-to-column ind)
+		 (delete-region (point-at-bol) (point)))
+	       (beginning-of-line 2))
+
+	     (run-hooks 'org-agenda-entry-text-cleanup-hook)
+
+	     (goto-char (point-min))
+	     (when indent
+	       (while (and (not (eobp)) (re-search-forward "^" nil t))
+		 (replace-match indent t t)))
+	     (goto-char (point-min))
+	     (while (looking-at "[ \t]*\n") (replace-match ""))
+	     (goto-char (point-max))
+	     (when (> (org-current-line)
+		      n-lines)
+	       (org-goto-line (1+ n-lines))
+	       (backward-char 1))
+	     (setq txt (buffer-substring (point-min) (point))))))))
     txt))
 
 (defun org-check-for-org-mode ()
@@ -8527,13 +8525,11 @@ When NO-UPDATE is non-nil, don't redo the agenda buffer."
 		       (if goto "Goto" "Refile to") buffer
 		       org-refile-allow-creating-parent-nodes))))
       (with-current-buffer buffer
-	(save-excursion
-	  (save-restriction
-	    (widen)
-	    (goto-char marker)
-	    (let ((org-agenda-buffer-name buffer-orig))
-	      (org-remove-subtree-entries-from-agenda))
-	    (org-refile goto buffer rfloc)))))
+	(org-with-wide-buffer
+	 (goto-char marker)
+	 (let ((org-agenda-buffer-name buffer-orig))
+	   (org-remove-subtree-entries-from-agenda))
+	 (org-refile goto buffer rfloc))))
     (unless no-update (org-agenda-redo)))))
 
 (defun org-agenda-open-link (&optional arg)
@@ -8558,13 +8554,11 @@ It also looks at the text of the entry itself."
 		  (setq trg (and (string-match org-bracket-link-regexp l)
 				 (match-string 1 l)))
 		  (if (or (not trg) (string-match org-any-link-re trg))
-		      (save-excursion
-			(save-restriction
-			  (widen)
-			  (goto-char marker)
-			  (when (search-forward l nil lkend)
-			    (goto-char (match-beginning 0))
-			    (org-open-at-point))))
+		      (org-with-wide-buffer
+		       (goto-char marker)
+		       (when (search-forward l nil lkend)
+			 (goto-char (match-beginning 0))
+			 (org-open-at-point)))
 		    ;; This is an internal link, widen the buffer
 		    (switch-to-buffer-other-window buffer)
 		    (widen)
@@ -8880,9 +8874,9 @@ If FORCE-TAGS is non nil, the car of it returns the new tags."
 	 (line (org-current-line))
 	 (org-agenda-buffer (current-buffer))
 	 (thetags (with-current-buffer (marker-buffer hdmarker)
-		    (save-excursion (save-restriction (widen)
-						      (goto-char hdmarker)
-						      (org-get-tags-at)))))
+		    (org-with-wide-buffer
+		     (goto-char hdmarker)
+		     (org-get-tags-at))))
 	 props m pl undone-face done-face finish new dotime level cat tags)
     (save-excursion
       (goto-char (point-max))
@@ -8903,10 +8897,8 @@ If FORCE-TAGS is non nil, the car of it returns the new tags."
 			   org-prefix-format-compiled))
 		      (extra (org-get-at-bol 'extra)))
 		  (with-current-buffer (marker-buffer hdmarker)
-		    (save-excursion
-		      (save-restriction
-			(widen)
-			(org-agenda-format-item extra newhead level cat tags dotime)))))
+		    (org-with-wide-buffer
+		     (org-agenda-format-item extra newhead level cat tags dotime))))
 		pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
 		undone-face (org-get-at-bol 'undone-face)
 		done-face (org-get-at-bol 'done-face))
@@ -9297,14 +9289,12 @@ ARG is passed through to `org-deadline'."
   (let ((marker (make-marker)) (col (current-column)) newhead)
     (org-with-remote-undo (marker-buffer org-clock-marker)
       (with-current-buffer (marker-buffer org-clock-marker)
-	(save-excursion
-	  (save-restriction
-	    (widen)
-	    (goto-char org-clock-marker)
-	    (org-back-to-heading t)
-	    (move-marker marker (point))
-	    (org-clock-out)
-	    (setq newhead (org-get-heading))))))
+	(org-with-wide-buffer
+	 (goto-char org-clock-marker)
+	 (org-back-to-heading t)
+	 (move-marker marker (point))
+	 (org-clock-out)
+	 (setq newhead (org-get-heading)))))
     (org-agenda-change-all-lines newhead marker)
     (move-marker marker nil)
     (org-move-to-column col)

+ 11 - 15
lisp/org-capture.el

@@ -825,11 +825,9 @@ already gone.  Any prefix argument will be passed to the refile command."
     (org-capture-finalize)
     (save-window-excursion
       (with-current-buffer (or base (current-buffer))
-	(save-excursion
-	  (save-restriction
-	    (widen)
-	    (goto-char pos)
-	    (call-interactively 'org-refile)))))
+	(org-with-wide-buffer
+	 (goto-char pos)
+	 (call-interactively 'org-refile))))
     (when kill-buffer (kill-buffer base))))
 
 (defun org-capture-kill ()
@@ -1305,16 +1303,14 @@ Of course, if exact position has been required, just put it there."
 		      (point-at-bol))
 		  (point))))))
     (with-current-buffer (buffer-base-buffer (current-buffer))
-      (save-excursion
-	(save-restriction
-	  (widen)
-	  (goto-char pos)
-	  (let ((bookmark-name (plist-get org-bookmark-names-plist
-					  :last-capture)))
-	    (when bookmark-name
-	      (with-demoted-errors
-		(bookmark-set bookmark-name))))
-	  (move-marker org-capture-last-stored-marker (point)))))))
+      (org-with-wide-buffer
+       (goto-char pos)
+       (let ((bookmark-name (plist-get org-bookmark-names-plist
+				       :last-capture)))
+	 (when bookmark-name
+	   (with-demoted-errors
+	       (bookmark-set bookmark-name))))
+       (move-marker org-capture-last-stored-marker (point))))))
 
 (defun org-capture-narrow (beg end)
   "Narrow, unless configuration says not to narrow."

+ 113 - 117
lisp/org-clock.el

@@ -852,12 +852,10 @@ If CLOCK-SOUND is non-nil, it overrides `org-clock-sound'."
 (defmacro org-with-clock-position (clock &rest forms)
   "Evaluate FORMS with CLOCK as the current active clock."
   `(with-current-buffer (marker-buffer (car ,clock))
-     (save-excursion
-       (save-restriction
-	 (widen)
-	 (goto-char (car ,clock))
-	 (beginning-of-line)
-	 ,@forms))))
+     (org-with-wide-buffer
+      (goto-char (car ,clock))
+      (beginning-of-line)
+      ,@forms)))
 (def-edebug-spec org-with-clock-position (form body))
 (put 'org-with-clock-position 'lisp-indent-function 1)
 
@@ -1252,117 +1250,115 @@ make this the default behavior.)"
 	  (set-buffer (org-base-buffer (marker-buffer selected-task)))
 	  (setq target-pos (marker-position selected-task))
 	  (move-marker selected-task nil))
-	(save-excursion
-	  (save-restriction
-	    (widen)
-	    (goto-char target-pos)
-	    (org-back-to-heading t)
-	    (or interrupting (move-marker org-clock-interrupted-task nil))
-	    (run-hooks 'org-clock-in-prepare-hook)
-	    (org-clock-history-push)
-	    (setq org-clock-current-task (nth 4 (org-heading-components)))
-	    (cond ((functionp org-clock-in-switch-to-state)
-		   (looking-at org-complex-heading-regexp)
-		   (let ((newstate (funcall org-clock-in-switch-to-state
-					    (match-string 2))))
-		     (if newstate (org-todo newstate))))
-		  ((and org-clock-in-switch-to-state
-			(not (looking-at (concat org-outline-regexp "[ \t]*"
-						 org-clock-in-switch-to-state
-						 "\\>"))))
-		   (org-todo org-clock-in-switch-to-state)))
-	    (setq org-clock-heading
-		  (cond ((and org-clock-heading-function
-			      (functionp org-clock-heading-function))
-			 (funcall org-clock-heading-function))
-			((nth 4 (org-heading-components))
-			 (replace-regexp-in-string
-			  "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
-			  (match-string-no-properties 4)))
-			(t "???")))
-	    (org-clock-find-position org-clock-in-resume)
-	    (cond
-	     ((and org-clock-in-resume
-		   (looking-at
-		    (concat "^[ \t]*" org-clock-string
-			    " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
-			    " *\\sw+.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
-	      (message "Matched %s" (match-string 1))
-	      (setq ts (concat "[" (match-string 1) "]"))
-	      (goto-char (match-end 1))
-	      (setq org-clock-start-time
-		    (apply 'encode-time
-			   (org-parse-time-string (match-string 1))))
-	      (setq org-clock-effort (org-entry-get (point) org-effort-property))
-	      (setq org-clock-total-time (org-clock-sum-current-item
-					  (org-clock-get-sum-start))))
-	     ((eq org-clock-in-resume 'auto-restart)
-	      ;; called from org-clock-load during startup,
-	      ;; do not interrupt, but warn!
-	      (message "Cannot restart clock because task does not contain unfinished clock")
-	      (ding)
-	      (sit-for 2)
-	      (throw 'abort nil))
-	     (t
-	      (insert-before-markers "\n")
-	      (backward-char 1)
-	      (org-indent-line)
-	      (when (and (save-excursion
-			   (end-of-line 0)
-			   (org-in-item-p)))
-		(beginning-of-line 1)
-		(indent-line-to (- (org-get-indentation) 2)))
-	      (insert org-clock-string " ")
-	      (setq org-clock-effort (org-entry-get (point) org-effort-property))
-	      (setq org-clock-total-time (org-clock-sum-current-item
-					  (org-clock-get-sum-start)))
-	      (setq org-clock-start-time
-		    (or (and org-clock-continuously org-clock-out-time)
-			(and leftover
-			     (y-or-n-p
-			      (format
-			       "You stopped another clock %d mins ago; start this one from then? "
-			       (/ (- (float-time
-				      (org-current-time org-clock-rounding-minutes t))
-				     (float-time leftover))
-				  60)))
-			     leftover)
-			start-time
-			(org-current-time org-clock-rounding-minutes t)))
-	      (setq ts (org-insert-time-stamp org-clock-start-time
-					      'with-hm 'inactive))))
-	    (move-marker org-clock-marker (point) (buffer-base-buffer))
-	    (move-marker org-clock-hd-marker
-			 (save-excursion (org-back-to-heading t) (point))
-			 (buffer-base-buffer))
-	    (setq org-clock-has-been-used t)
-	    ;; add to mode line
-	    (when (or (eq org-clock-clocked-in-display 'mode-line)
-		      (eq org-clock-clocked-in-display 'both))
-	      (or global-mode-string (setq global-mode-string '("")))
-	      (or (memq 'org-mode-line-string global-mode-string)
-		  (setq global-mode-string
-			(append global-mode-string '(org-mode-line-string)))))
-	    ;; add to frame title
-	    (when (or (eq org-clock-clocked-in-display 'frame-title)
-		      (eq org-clock-clocked-in-display 'both))
-	      (setq frame-title-format org-clock-frame-title-format))
-	    (org-clock-update-mode-line)
-	    (when org-clock-mode-line-timer
-	      (cancel-timer org-clock-mode-line-timer)
-	      (setq org-clock-mode-line-timer nil))
-	    (when org-clock-clocked-in-display
-	      (setq org-clock-mode-line-timer
-		    (run-with-timer org-clock-update-period
-				    org-clock-update-period
-				    'org-clock-update-mode-line)))
-	    (when org-clock-idle-timer
-	      (cancel-timer org-clock-idle-timer)
-	      (setq org-clock-idle-timer nil))
-	    (setq org-clock-idle-timer
-		  (run-with-timer 60 60 'org-resolve-clocks-if-idle))
-	    (message "Clock starts at %s - %s" ts org--msg-extra)
-	    (run-hooks 'org-clock-in-hook)))))))
+	(org-with-wide-buffer
+	 (goto-char target-pos)
+	 (org-back-to-heading t)
+	 (or interrupting (move-marker org-clock-interrupted-task nil))
+	 (run-hooks 'org-clock-in-prepare-hook)
+	 (org-clock-history-push)
+	 (setq org-clock-current-task (nth 4 (org-heading-components)))
+	 (cond ((functionp org-clock-in-switch-to-state)
+		(looking-at org-complex-heading-regexp)
+		(let ((newstate (funcall org-clock-in-switch-to-state
+					 (match-string 2))))
+		  (if newstate (org-todo newstate))))
+	       ((and org-clock-in-switch-to-state
+		     (not (looking-at (concat org-outline-regexp "[ \t]*"
+					      org-clock-in-switch-to-state
+					      "\\>"))))
+		(org-todo org-clock-in-switch-to-state)))
+	 (setq org-clock-heading
+	       (cond ((and org-clock-heading-function
+			   (functionp org-clock-heading-function))
+		      (funcall org-clock-heading-function))
+		     ((nth 4 (org-heading-components))
+		      (replace-regexp-in-string
+		       "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
+		       (match-string-no-properties 4)))
+		     (t "???")))
+	 (org-clock-find-position org-clock-in-resume)
+	 (cond
+	  ((and org-clock-in-resume
+		(looking-at
+		 (concat "^[ \t]*" org-clock-string
+			 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
+			 " *\\sw+.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
+	   (message "Matched %s" (match-string 1))
+	   (setq ts (concat "[" (match-string 1) "]"))
+	   (goto-char (match-end 1))
+	   (setq org-clock-start-time
+		 (apply 'encode-time
+			(org-parse-time-string (match-string 1))))
+	   (setq org-clock-effort (org-entry-get (point) org-effort-property))
+	   (setq org-clock-total-time (org-clock-sum-current-item
+				       (org-clock-get-sum-start))))
+	  ((eq org-clock-in-resume 'auto-restart)
+	   ;; called from org-clock-load during startup,
+	   ;; do not interrupt, but warn!
+	   (message "Cannot restart clock because task does not contain unfinished clock")
+	   (ding)
+	   (sit-for 2)
+	   (throw 'abort nil))
+	  (t
+	   (insert-before-markers "\n")
+	   (backward-char 1)
+	   (org-indent-line)
+	   (when (and (save-excursion
+			(end-of-line 0)
+			(org-in-item-p)))
+	     (beginning-of-line 1)
+	     (indent-line-to (- (org-get-indentation) 2)))
+	   (insert org-clock-string " ")
+	   (setq org-clock-effort (org-entry-get (point) org-effort-property))
+	   (setq org-clock-total-time (org-clock-sum-current-item
+				       (org-clock-get-sum-start)))
+	   (setq org-clock-start-time
+		 (or (and org-clock-continuously org-clock-out-time)
+		     (and leftover
+			  (y-or-n-p
+			   (format
+			    "You stopped another clock %d mins ago; start this one from then? "
+			    (/ (- (float-time
+				   (org-current-time org-clock-rounding-minutes t))
+				  (float-time leftover))
+			       60)))
+			  leftover)
+		     start-time
+		     (org-current-time org-clock-rounding-minutes t)))
+	   (setq ts (org-insert-time-stamp org-clock-start-time
+					   'with-hm 'inactive))))
+	 (move-marker org-clock-marker (point) (buffer-base-buffer))
+	 (move-marker org-clock-hd-marker
+		      (save-excursion (org-back-to-heading t) (point))
+		      (buffer-base-buffer))
+	 (setq org-clock-has-been-used t)
+	 ;; add to mode line
+	 (when (or (eq org-clock-clocked-in-display 'mode-line)
+		   (eq org-clock-clocked-in-display 'both))
+	   (or global-mode-string (setq global-mode-string '("")))
+	   (or (memq 'org-mode-line-string global-mode-string)
+	       (setq global-mode-string
+		     (append global-mode-string '(org-mode-line-string)))))
+	 ;; add to frame title
+	 (when (or (eq org-clock-clocked-in-display 'frame-title)
+		   (eq org-clock-clocked-in-display 'both))
+	   (setq frame-title-format org-clock-frame-title-format))
+	 (org-clock-update-mode-line)
+	 (when org-clock-mode-line-timer
+	   (cancel-timer org-clock-mode-line-timer)
+	   (setq org-clock-mode-line-timer nil))
+	 (when org-clock-clocked-in-display
+	   (setq org-clock-mode-line-timer
+		 (run-with-timer org-clock-update-period
+				 org-clock-update-period
+				 'org-clock-update-mode-line)))
+	 (when org-clock-idle-timer
+	   (cancel-timer org-clock-idle-timer)
+	   (setq org-clock-idle-timer nil))
+	 (setq org-clock-idle-timer
+	       (run-with-timer 60 60 'org-resolve-clocks-if-idle))
+	 (message "Clock starts at %s - %s" ts org--msg-extra)
+	 (run-hooks 'org-clock-in-hook))))))
 
 ;;;###autoload
 (defun org-clock-in-last (&optional arg)

+ 13 - 17
lisp/org-table.el

@@ -3317,10 +3317,8 @@ with the prefix ARG."
 (defun org-table-recalculate-buffer-tables ()
   "Recalculate all tables in the current buffer."
   (interactive)
-  (save-excursion
-    (save-restriction
-      (widen)
-      (org-table-map-tables (lambda () (org-table-recalculate t)) t))))
+  (org-with-wide-buffer
+   (org-table-map-tables (lambda () (org-table-recalculate t)) t)))
 
 ;;;###autoload
 (defun org-table-iterate-buffer-tables ()
@@ -3330,19 +3328,17 @@ with the prefix ARG."
 	 (i imax)
 	 (checksum (md5 (buffer-string)))
 	 c1)
-    (save-excursion
-      (save-restriction
-	(widen)
-	(catch 'exit
-	  (while (> i 0)
-	    (setq i (1- i))
-	    (org-table-map-tables (lambda () (org-table-recalculate t)) t)
-	    (if (equal checksum (setq c1 (md5 (buffer-string))))
-		(progn
-		  (message "Convergence after %d iterations" (- imax i))
-		  (throw 'exit t))
-	      (setq checksum c1)))
-	  (user-error "No convergence after %d iterations" imax))))))
+    (org-with-wide-buffer
+     (catch 'exit
+       (while (> i 0)
+	 (setq i (1- i))
+	 (org-table-map-tables (lambda () (org-table-recalculate t)) t)
+	 (if (equal checksum (setq c1 (md5 (buffer-string))))
+	     (progn
+	       (message "Convergence after %d iterations" (- imax i))
+	       (throw 'exit t))
+	   (setq checksum c1)))
+       (user-error "No convergence after %d iterations" imax)))))
 
 (defun org-table-calc-current-TBLFM (&optional arg)
   "Apply the #+TBLFM in the line at point to the table."

+ 193 - 217
lisp/org.el

@@ -4664,22 +4664,20 @@ is nil, return nil unconditionally."
 
 (defun org-table-map-tables (function &optional quietly)
   "Apply FUNCTION to the start of all tables in the buffer."
-  (save-excursion
-    (save-restriction
-      (widen)
-      (goto-char (point-min))
-      (while (re-search-forward org-table-any-line-regexp nil t)
-	(unless quietly
-	  (message "Mapping tables: %d%%"
-		   (floor (* 100.0 (point)) (buffer-size))))
-	(beginning-of-line 1)
-	(when (and (looking-at org-table-line-regexp)
-		   ;; Exclude tables in src/example/verbatim/clocktable blocks
-		   (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
-	  (save-excursion (funcall function))
-	  (or (looking-at org-table-line-regexp)
-	      (forward-char 1)))
-	(re-search-forward org-table-any-border-regexp nil 1))))
+  (org-with-wide-buffer
+   (goto-char (point-min))
+   (while (re-search-forward org-table-any-line-regexp nil t)
+     (unless quietly
+       (message "Mapping tables: %d%%"
+		(floor (* 100.0 (point)) (buffer-size))))
+     (beginning-of-line 1)
+     (when (and (looking-at org-table-line-regexp)
+		;; Exclude tables in src/example/verbatim/clocktable blocks
+		(not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
+       (save-excursion (funcall function))
+       (or (looking-at org-table-line-regexp)
+	   (forward-char 1)))
+     (re-search-forward org-table-any-border-regexp nil 1)))
   (unless quietly (message "Mapping tables: done")))
 
 (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
@@ -7441,20 +7439,18 @@ The return value is a list of cons cells, with start and stop
 positions for each overlay.
 If USE-MARKERS is set, return the positions as markers."
   (let (beg end)
-    (save-excursion
-      (save-restriction
-	(widen)
-	(delq nil
-	      (mapcar (lambda (o)
-			(when (eq (overlay-get o 'invisible) 'outline)
-			  (setq beg (overlay-start o)
-				end (overlay-end o))
-			  (and beg end (> end beg)
-			       (if use-markers
-				   (cons (copy-marker beg)
-					 (copy-marker end t))
-				 (cons beg end)))))
-		      (overlays-in (point-min) (point-max))))))))
+    (org-with-wide-buffer
+     (delq nil
+	   (mapcar (lambda (o)
+		     (when (eq (overlay-get o 'invisible) 'outline)
+		       (setq beg (overlay-start o)
+			     end (overlay-end o))
+		       (and beg end (> end beg)
+			    (if use-markers
+				(cons (copy-marker beg)
+				      (copy-marker end t))
+			      (cons beg end)))))
+		   (overlays-in (point-min) (point-max)))))))
 
 (defun org-set-outline-overlay-data (data)
   "Create visibility overlays for all positions in DATA.
@@ -9707,12 +9703,10 @@ the value of the drawer property."
   (let ((case-fold-search t)
 	(inhibit-read-only t))
     (org-with-silent-modifications
-     (save-excursion
-       (save-restriction
-	 (widen)
-	 (goto-char (point-min))
-	 (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
-	   (org-refresh-property tprop (match-string-no-properties 1))))))))
+     (org-with-wide-buffer
+      (goto-char (point-min))
+      (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
+	(org-refresh-property tprop (match-string-no-properties 1)))))))
 
 (defun org-refresh-property (tprop p)
   "Refresh the buffer text property TPROP from the drawer property P.
@@ -9776,22 +9770,20 @@ The refresh happens only for the current tree (not subtree)."
   "Refresh stats text properties in the buffer."
   (let (stats)
     (org-with-silent-modifications
-     (save-excursion
-       (save-restriction
-	 (widen)
-	 (goto-char (point-min))
-	 (while (re-search-forward
-		 (concat org-outline-regexp-bol ".*"
-			 "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
-		 nil t)
-	   (setq stats (cond ((equal (match-string 3) "0") 0)
-			     ((match-string 2)
-			      (/ (* (string-to-number (match-string 2)) 100)
-				 (string-to-number (match-string 3))))
-			     (t (string-to-number (match-string 1)))))
-	   (org-back-to-heading t)
-	   (put-text-property (point) (progn (org-end-of-subtree t t) (point))
-			      'org-stats stats)))))))
+     (org-with-wide-buffer
+      (goto-char (point-min))
+      (while (re-search-forward
+	      (concat org-outline-regexp-bol ".*"
+		      "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
+	      nil t)
+	(setq stats (cond ((equal (match-string 3) "0") 0)
+			  ((match-string 2)
+			   (/ (* (string-to-number (match-string 2)) 100)
+			      (string-to-number (match-string 3))))
+			  (t (string-to-number (match-string 1)))))
+	(org-back-to-heading t)
+	(put-text-property (point) (progn (org-end-of-subtree t t) (point))
+			   'org-stats stats))))))
 
 (defun org-refresh-effort-properties ()
   "Refresh effort properties"
@@ -10975,56 +10967,54 @@ If NTH is an integer, return the NTH link found.
 If ZERO is a string, check also this string for a link, and if
 there is one, return it."
   (with-current-buffer buffer
-    (save-excursion
-      (save-restriction
-	(widen)
-	(goto-char marker)
-	(let ((cnt ?0)
-	      have-zero end links link c)
-	  (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
-	    (push (match-string 0 zero) links)
-	    (setq cnt (1- cnt) have-zero t))
-	  (save-excursion
-	    (org-back-to-heading t)
-	    (setq end (save-excursion (outline-next-heading) (point)))
-	    (while (re-search-forward org-any-link-re end t)
-	      (push (match-string 0) links))
-	    (setq links (org-uniquify (reverse links))))
-	  (cond
-	   ((null links)
-	    (message "No links"))
-	   ((equal (length links) 1)
-	    (setq link (car links)))
-	   ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
-	    (setq link (nth (if have-zero nth (1- nth)) links)))
-	   (t				; we have to select a link
-	    (save-excursion
-	      (save-window-excursion
-		(delete-other-windows)
-		(with-output-to-temp-buffer "*Select Link*"
-		  (dolist (l links)
-		    (cond
-		     ((not (string-match org-bracket-link-regexp l))
-		      (princ (format "[%c]  %s\n" (cl-incf cnt)
-				     (org-remove-angle-brackets l))))
-		     ((match-end 3)
-		      (princ (format "[%c]  %s (%s)\n" (cl-incf cnt)
-				     (match-string 3 l) (match-string 1 l))))
-		     (t (princ (format "[%c]  %s\n" (cl-incf cnt)
-				       (match-string 1 l)))))))
-		(org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
-		(message "Select link to open, RET to open all:")
-		(setq c (read-char-exclusive))
-		(and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
-	    (when (equal c ?q) (user-error "Abort"))
-	    (if (equal c ?\C-m)
-		(setq link links)
-	      (setq nth (- c ?0))
-	      (when have-zero (setq nth (1+ nth)))
-	      (unless (and (integerp nth) (>= (length links) nth))
-		(user-error "Invalid link selection"))
-	      (setq link (nth (1- nth) links)))))
-	  (cons link end))))))
+    (org-with-wide-buffer
+     (goto-char marker)
+     (let ((cnt ?0)
+	   have-zero end links link c)
+       (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
+	 (push (match-string 0 zero) links)
+	 (setq cnt (1- cnt) have-zero t))
+       (save-excursion
+	 (org-back-to-heading t)
+	 (setq end (save-excursion (outline-next-heading) (point)))
+	 (while (re-search-forward org-any-link-re end t)
+	   (push (match-string 0) links))
+	 (setq links (org-uniquify (reverse links))))
+       (cond
+	((null links)
+	 (message "No links"))
+	((equal (length links) 1)
+	 (setq link (car links)))
+	((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
+	 (setq link (nth (if have-zero nth (1- nth)) links)))
+	(t				; we have to select a link
+	 (save-excursion
+	   (save-window-excursion
+	     (delete-other-windows)
+	     (with-output-to-temp-buffer "*Select Link*"
+	       (dolist (l links)
+		 (cond
+		  ((not (string-match org-bracket-link-regexp l))
+		   (princ (format "[%c]  %s\n" (cl-incf cnt)
+				  (org-remove-angle-brackets l))))
+		  ((match-end 3)
+		   (princ (format "[%c]  %s (%s)\n" (cl-incf cnt)
+				  (match-string 3 l) (match-string 1 l))))
+		  (t (princ (format "[%c]  %s\n" (cl-incf cnt)
+				    (match-string 1 l)))))))
+	     (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
+	     (message "Select link to open, RET to open all:")
+	     (setq c (read-char-exclusive))
+	     (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
+	 (when (equal c ?q) (user-error "Abort"))
+	 (if (equal c ?\C-m)
+	     (setq link links)
+	   (setq nth (- c ?0))
+	   (when have-zero (setq nth (1+ nth)))
+	   (unless (and (integerp nth) (>= (length links) nth))
+	     (user-error "Invalid link selection"))
+	   (setq link (nth (1- nth) links)))))
+       (cons link end)))))
 
 ;; TODO: These functions are deprecated since `org-open-at-point'
 ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
@@ -12044,50 +12034,48 @@ prefix argument (`C-u C-u C-u C-c C-w')."
 	    (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
 						(find-file-noselect file)))
 	      (setq reversed (org-notes-order-reversed-p))
-	      (save-excursion
-		(save-restriction
-		  (widen)
-		  (if pos
-		      (progn
-			(goto-char pos)
-			(looking-at org-outline-regexp)
-			(setq level (org-get-valid-level (funcall outline-level) 1))
-			(goto-char
-			 (if reversed
-			     (or (outline-next-heading) (point-max))
-			   (or (save-excursion (org-get-next-sibling))
-			       (org-end-of-subtree t t)
-			       (point-max)))))
-		    (setq level 1)
-		    (if (not reversed)
-			(goto-char (point-max))
-		      (goto-char (point-min))
-		      (or (outline-next-heading) (goto-char (point-max)))))
-		  (unless (bolp) (newline))
-		  (org-paste-subtree level nil nil t)
-		  (when org-log-refile
-		    (org-add-log-setup 'refile nil nil org-log-refile)
-		    (unless (eq org-log-refile 'note)
-		      (save-excursion (org-add-log-note))))
-		  (and org-auto-align-tags
-		       (let ((org-loop-over-headlines-in-active-region nil))
-			 (org-set-tags nil t)))
-		  (let ((bookmark-name (plist-get org-bookmark-names-plist
-						  :last-refile)))
-		    (when bookmark-name
-		      (with-demoted-errors
-			  (bookmark-set bookmark-name))))
-		  ;; If we are refiling for capture, make sure that the
-		  ;; last-capture pointers point here
-		  (when (bound-and-true-p org-capture-is-refiling)
-		    (let ((bookmark-name (plist-get org-bookmark-names-plist
-						    :last-capture-marker)))
-		      (when bookmark-name
-			(with-demoted-errors
-			    (bookmark-set bookmark-name))))
-		    (move-marker org-capture-last-stored-marker (point)))
-		  (when (fboundp 'deactivate-mark) (deactivate-mark))
-		  (run-hooks 'org-after-refile-insert-hook))))
+	      (org-with-wide-buffer
+	       (if pos
+		   (progn
+		     (goto-char pos)
+		     (looking-at org-outline-regexp)
+		     (setq level (org-get-valid-level (funcall outline-level) 1))
+		     (goto-char
+		      (if reversed
+			  (or (outline-next-heading) (point-max))
+			(or (save-excursion (org-get-next-sibling))
+			    (org-end-of-subtree t t)
+			    (point-max)))))
+		 (setq level 1)
+		 (if (not reversed)
+		     (goto-char (point-max))
+		   (goto-char (point-min))
+		   (or (outline-next-heading) (goto-char (point-max)))))
+	       (unless (bolp) (newline))
+	       (org-paste-subtree level nil nil t)
+	       (when org-log-refile
+		 (org-add-log-setup 'refile nil nil org-log-refile)
+		 (unless (eq org-log-refile 'note)
+		   (save-excursion (org-add-log-note))))
+	       (and org-auto-align-tags
+		    (let ((org-loop-over-headlines-in-active-region nil))
+		      (org-set-tags nil t)))
+	       (let ((bookmark-name (plist-get org-bookmark-names-plist
+					       :last-refile)))
+		 (when bookmark-name
+		   (with-demoted-errors
+		       (bookmark-set bookmark-name))))
+	       ;; If we are refiling for capture, make sure that the
+	       ;; last-capture pointers point here
+	       (when (bound-and-true-p org-capture-is-refiling)
+		 (let ((bookmark-name (plist-get org-bookmark-names-plist
+						 :last-capture-marker)))
+		   (when bookmark-name
+		     (with-demoted-errors
+			 (bookmark-set bookmark-name))))
+		 (move-marker org-capture-last-stored-marker (point)))
+	       (when (fboundp 'deactivate-mark) (deactivate-mark))
+	       (run-hooks 'org-after-refile-insert-hook)))
 	    (unless org-refile-keep
 	      (if regionp
 		  (delete-region (point) (+ (point) (- region-end region-start)))
@@ -12197,13 +12185,11 @@ this function appends the default value from
 		       (or (find-buffer-visiting file)
 			   (find-file-noselect file))))
 	(with-current-buffer buffer
-	  (save-excursion
-	    (save-restriction
-	      (widen)
-	      (goto-char pos)
-	      (beginning-of-line 1)
-	      (unless (looking-at-p re)
-		(user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
+	  (org-with-wide-buffer
+	   (goto-char pos)
+	   (beginning-of-line 1)
+	   (unless (looking-at-p re)
+	     (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
 
 (defun org-refile-new-child (parent-target child)
   "Use refile target PARENT-TARGET to add new CHILD below it."
@@ -12214,22 +12200,20 @@ this function appends the default value from
 	level)
     (with-current-buffer (or (find-buffer-visiting file)
 			     (find-file-noselect file))
-      (save-excursion
-	(save-restriction
-	  (widen)
-	  (if pos
-	      (goto-char pos)
-	    (goto-char (point-max))
-	    (unless (bolp) (newline)))
-	  (when (looking-at org-outline-regexp)
-	    (setq level (funcall outline-level))
-	    (org-end-of-subtree t t))
-	  (org-back-over-empty-lines)
-	  (insert "\n" (make-string
-			(if pos (org-get-valid-level level 1) 1) ?*)
-		  " " child "\n")
-	  (beginning-of-line 0)
-	  (list (concat (car parent-target) "/" child) file "" (point)))))))
+      (org-with-wide-buffer
+       (if pos
+	   (goto-char pos)
+	 (goto-char (point-max))
+	 (unless (bolp) (newline)))
+       (when (looking-at org-outline-regexp)
+	 (setq level (funcall outline-level))
+	 (org-end-of-subtree t t))
+       (org-back-over-empty-lines)
+       (insert "\n" (make-string
+		     (if pos (org-get-valid-level level 1) 1) ?*)
+	       " " child "\n")
+       (beginning-of-line 0)
+       (list (concat (car parent-target) "/" child) file "" (point))))))
 
 (defun org-olpath-completing-read (prompt collection &rest args)
   "Read an outline path like a file name."
@@ -15642,15 +15626,13 @@ a *different* entry, you cannot use these techniques."
 	    (org-agenda-prepare-buffers scope)
 	    (dolist (file scope)
 	      (with-current-buffer (org-find-base-buffer-visiting file)
-		(save-excursion
-		  (save-restriction
-		    (widen)
-		    (goto-char (point-min))
-		    (setq res
-			  (append
-			   res
-			   (org-scan-tags
-			    func matcher org--matcher-tags-todo-only))))))))))
+		(org-with-wide-buffer
+		 (goto-char (point-min))
+		 (setq res
+		       (append
+			res
+			(org-scan-tags
+			 func matcher org--matcher-tags-todo-only)))))))))
       res)))
 
 ;;; Properties API
@@ -16725,17 +16707,15 @@ If POS-ONLY is set, return just the position instead of a marker.
 The heading text must match exact, but it may have a TODO keyword,
 a priority cookie and tags in the standard locations."
   (with-current-buffer (or buffer (current-buffer))
-    (save-excursion
-      (save-restriction
-	(widen)
-	(goto-char (point-min))
-	(let (case-fold-search)
-	  (when (re-search-forward
-		 (format org-complex-heading-regexp-format
-			 (regexp-quote heading)) nil t)
-	    (if pos-only
-		(match-beginning 0)
-	      (move-marker (make-marker) (match-beginning 0)))))))))
+    (org-with-wide-buffer
+     (goto-char (point-min))
+     (let (case-fold-search)
+       (when (re-search-forward
+	      (format org-complex-heading-regexp-format
+		      (regexp-quote heading)) nil t)
+	 (if pos-only
+	     (match-beginning 0)
+	   (move-marker (make-marker) (match-beginning 0))))))))
 
 (defun org-find-exact-heading-in-directory (heading &optional dir)
   "Find Org node headline HEADING in all .org files in directory DIR.
@@ -23784,17 +23764,15 @@ package ox-bibtex by Taru Karttunen."
   (let ((reftex-docstruct-symbol 'org--rds)
 	(reftex-cite-format "\\cite{%l}")
 	org--rds bib)
-    (save-excursion
-      (save-restriction
-	(widen)
-	(let ((case-fold-search t)
-	      (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
-	  (if (not (save-excursion
-		     (or (re-search-forward re nil t)
-			 (re-search-backward re nil t))))
-	      (user-error "No bibliography defined in file")
-	    (setq bib (concat (match-string 1) ".bib")
-		  org--rds (list (list 'bib bib)))))))
+    (org-with-wide-buffer
+     (let ((case-fold-search t)
+	   (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
+       (if (not (save-excursion
+		  (or (re-search-forward re nil t)
+		      (re-search-backward re nil t))))
+	   (user-error "No bibliography defined in file")
+	 (setq bib (concat (match-string 1) ".bib")
+	       org--rds (list (list 'bib bib))))))
     (call-interactively 'reftex-citation)))
 
 ;;;; Functions extending outline functionality
@@ -24909,23 +24887,21 @@ when non-nil, is a regexp matching keywords names."
 	 (subs (make-vector (1+ n) nil))
 	 (last-level 0)
 	 m level head0 head)
-    (save-excursion
-      (save-restriction
-	(widen)
-	(goto-char (point-max))
-	(while (re-search-backward re nil t)
-	  (setq level (org-reduced-level (funcall outline-level)))
-	  (when (and (<= level n)
-		     (looking-at org-complex-heading-regexp)
-		     (setq head0 (match-string-no-properties 4)))
-	    (setq head (org-link-display-format head0)
-		  m (org-imenu-new-marker))
-	    (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
-	    (if (>= level last-level)
-		(push (cons head m) (aref subs level))
-	      (push (cons head (aref subs (1+ level))) (aref subs level))
-	      (cl-loop for i from (1+ level) to n do (aset subs i nil)))
-	    (setq last-level level)))))
+    (org-with-wide-buffer
+     (goto-char (point-max))
+     (while (re-search-backward re nil t)
+       (setq level (org-reduced-level (funcall outline-level)))
+       (when (and (<= level n)
+		  (looking-at org-complex-heading-regexp)
+		  (setq head0 (match-string-no-properties 4)))
+	 (setq head (org-link-display-format head0)
+	       m (org-imenu-new-marker))
+	 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
+	 (if (>= level last-level)
+	     (push (cons head m) (aref subs level))
+	   (push (cons head (aref subs (1+ level))) (aref subs level))
+	   (cl-loop for i from (1+ level) to n do (aset subs i nil)))
+	 (setq last-level level))))
     (aref subs 1)))
 
 (eval-after-load "imenu"