Skip to content

Commit c57b030

Browse files
ilupinyantar92
authored andcommitted
org-agenda: Make sure file restriction can be removed
* lisp/org-agenda.el: (org-agenda-restrict): (org-agenda-restrict-begin): (org-agenda-restrict-end): (org-agenda-overriding-restriction): add docstrings. (org-agenda): (org-agenda-set-restriction-lock): Set `org-agenda-restrict' non-nil during both temporary and extended file restriction. (org-agenda-remove-restriction-lock): Revert commit df0e96b. * testing/lisp/test-org-agenda.el (test-org-agenda/file-restriction): Add a test.
1 parent eb15341 commit c57b030

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

lisp/org-agenda.el

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,17 @@ string that it returns."
22022202
(org-remap org-agenda-mode-map 'move-end-of-line 'org-agenda-end-of-line)
22032203

22042204
(defvar org-agenda-menu) ; defined later in this file.
2205-
(defvar org-agenda-restrict nil)
2205+
(defvar org-agenda-restrict nil
2206+
"Non-nil means agenda restriction is active.
2207+
This is an internal flag indicating either temporary or extended
2208+
agenda restriction. Specifically, it is set to t if the agenda
2209+
is restricted to an entire file, and is set to the corresponding
2210+
buffer if the agenda is restricted to a part of a file, e.g. a
2211+
region or a substree. In the latter case,
2212+
`org-agenda-restrict-begin' and `org-agenda-restrict-end' are set
2213+
to the beginning and the end of the part.
2214+
2215+
See also `org-agenda-set-restriction-lock'.")
22062216
(defvar org-agenda-follow-mode nil)
22072217
(defvar org-agenda-entry-text-mode nil)
22082218
(defvar org-agenda-clockreport-mode nil)
@@ -2734,10 +2744,16 @@ that have been changed along."
27342744

27352745
;;; Agenda dispatch
27362746

2737-
(defvar org-agenda-restrict-begin (make-marker))
2738-
(defvar org-agenda-restrict-end (make-marker))
2747+
(defvar org-agenda-restrict-begin (make-marker)
2748+
"Internal variable used to mark the restriction beginning.
2749+
It is only relevant when `org-agenda-restrict' is a buffer.")
2750+
(defvar org-agenda-restrict-end (make-marker)
2751+
"Internal variable used to mark the restriction end.
2752+
It is only relevant when `org-agenda-restrict' is a buffer.")
27392753
(defvar org-agenda-last-dispatch-buffer nil)
2740-
(defvar org-agenda-overriding-restriction nil)
2754+
(defvar org-agenda-overriding-restriction nil
2755+
"Non-nil means extended agenda restriction is active.
2756+
This is an internal flag set by `org-agenda-set-restriction-lock'.")
27412757

27422758
(defcustom org-agenda-custom-commands-contexts nil
27432759
"Alist of custom agenda keys and contextual rules.
@@ -2962,12 +2978,12 @@ Pressing `<' twice means to restrict to the current subtree or region
29622978
(move-marker org-agenda-restrict-begin (point))
29632979
(move-marker org-agenda-restrict-end
29642980
(progn (org-end-of-subtree t)))))
2965-
((and (eq restriction 'buffer)
2966-
(or (< 1 (point-min))
2967-
(< (point-max) (1+ (buffer-size)))))
2968-
(setq org-agenda-restrict (current-buffer))
2969-
(move-marker org-agenda-restrict-begin (point-min))
2970-
(move-marker org-agenda-restrict-end (point-max)))))
2981+
((eq restriction 'buffer)
2982+
(if (not (buffer-narrowed-p))
2983+
(setq org-agenda-restrict t)
2984+
(setq org-agenda-restrict (current-buffer))
2985+
(move-marker org-agenda-restrict-begin (point-min))
2986+
(move-marker org-agenda-restrict-end (point-max))))))
29712987

29722988
;; For example the todo list should not need it (but does...)
29732989
(cond
@@ -7958,7 +7974,7 @@ subtree."
79587974
(message "Locking agenda restriction to subtree"))
79597975
(put 'org-agenda-files 'org-restrict
79607976
(list (buffer-file-name (buffer-base-buffer))))
7961-
(setq org-agenda-restrict nil)
7977+
(setq org-agenda-restrict t)
79627978
(setq org-agenda-overriding-restriction 'file)
79637979
(move-marker org-agenda-restrict-begin nil)
79647980
(move-marker org-agenda-restrict-end nil)
@@ -7969,14 +7985,11 @@ subtree."
79697985
(defun org-agenda-remove-restriction-lock (&optional noupdate)
79707986
"Remove agenda restriction lock."
79717987
(interactive "P")
7972-
(if (not (or org-agenda-restrict org-agenda-overriding-restriction))
7988+
(if (not org-agenda-restrict)
79737989
(message "No agenda restriction to remove.")
79747990
(delete-overlay org-agenda-restriction-lock-overlay)
79757991
(delete-overlay org-speedbar-restriction-lock-overlay)
79767992
(setq org-agenda-overriding-restriction nil)
7977-
(unless org-agenda-keep-restricted-file-list
7978-
;; There is a request to keep the file list in place
7979-
(put 'org-agenda-files 'org-restrict nil))
79807993
(setq org-agenda-restrict nil)
79817994
(put 'org-agenda-files 'org-restrict nil)
79827995
(move-marker org-agenda-restrict-begin nil)

testing/lisp/test-org-agenda.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,34 @@ See https://list.orgmode.org/06d301d83d9e$f8b44340$ea1cc9c0$@tomdavey.com"
255255
(get-text-property (point) 'day))))
256256
(org-test-agenda--kill-all-agendas)))
257257

258+
(ert-deftest test-org-agenda/file-restriction ()
259+
"Test file restriction for org agenda."
260+
(org-test-with-temp-text-in-file "* TODO Foo"
261+
(org-agenda-set-restriction-lock t)
262+
(org-agenda nil "t")
263+
(should (search-forward "Foo"))
264+
(should (org-agenda-files))
265+
(should-not (org-agenda-files t))
266+
(org-agenda-remove-restriction-lock)
267+
(goto-char (point-min))
268+
(should-not (search-forward "Foo" nil t))
269+
(should-not (org-agenda-files)))
270+
(org-test-with-temp-text-in-file "* TODO Bar"
271+
(org-agenda nil "t" 'buffer)
272+
(should (search-forward "Bar"))
273+
(should (org-agenda-files))
274+
(should-not (org-agenda-files t))
275+
(org-agenda-remove-restriction-lock)
276+
(goto-char (point-min))
277+
(should-not (search-forward "Bar" nil t))
278+
(should-not (org-agenda-files)))
279+
(org-test-with-temp-text-in-file "* TODO Bar"
280+
(org-agenda nil "t" 'buffer)
281+
(org-agenda nil "t")
282+
(should-not (search-forward "Bar" nil t))
283+
(should-not (org-agenda-files)))
284+
(org-test-agenda--kill-all-agendas))
285+
258286

259287
;; agenda redo
260288

0 commit comments

Comments
 (0)