Browse Source

Let `org-agenda-set-restriction-lock' remove restriction at point

* lisp/org-compat.el (org-speedbar-set-agenda-restriction):
* lisp/org-agenda.el (org-agenda-set-restriction-lock):
If there is an agenda restriction at point, remove it.

* doc/org-manual.org:
* etc/ORG-NEWS: Document the new feature.
Bastien 6 years ago
parent
commit
011f1c6181
4 changed files with 72 additions and 51 deletions
  1. 15 14
      doc/org-manual.org
  2. 7 0
      etc/ORG-NEWS
  3. 43 36
      lisp/org-agenda.el
  4. 7 1
      lisp/org-compat.el

+ 15 - 14
doc/org-manual.org

@@ -8338,20 +8338,20 @@ scope for an extended period, use the following commands:
 
      #+kindex: C-c C-x <
      #+findex: org-agenda-set-restriction-lock
-     Permanently restrict the agenda to the current subtree.  When
-     called with a prefix argument, or with the cursor before the
-     first headline in a file, set the agenda scope to the entire
-     file.  This restriction remains in effect until removed with
-     {{{kbd(C-c C-x >)}}}, or by typing either {{{kbd(<)}}} or
-     {{{kbd(>)}}} in the agenda dispatcher.  If there is a window
-     displaying an agenda view, the new restriction takes effect
-     immediately.
+     Restrict the agenda to the current subtree.  If there already is
+     a restriction at point, remove it.  When called with a universal
+     prefix argument or with the cursor before the first headline in a
+     file, set the agenda scope to the entire file.  This restriction
+     remains in effect until removed with {{{kbd(C-c C-x >)}}}, or by typing
+     either {{{kbd(<)}}} or {{{kbd(>)}}} in the agenda dispatcher.  If there is a
+     window displaying an agenda view, the new restriction takes
+     effect immediately.
 
 - {{{kbd(C-c C-x >)}}} (~org-agenda-remove-restriction-lock~) ::
 
      #+kindex: C-c C-x >
      #+findex: org-agenda-remove-restriction-lock
-     Remove the permanent restriction created by {{{kbd(C-c C-x <)}}}.
+     Remove the restriction created by {{{kbd(C-c C-x <)}}}.
 
 #+texinfo: @noindent
 When working with =speedbar.el=, you can use the following commands in
@@ -8360,15 +8360,16 @@ the Speedbar frame:
 - {{{kbd(<)}}} (~org-speedbar-set-agenda-restriction~) ::
 
      #+findex: org-speedbar-set-agenda-restriction
-     Permanently restrict the agenda to the item---either an Org file
-     or a subtree in such a file---at the cursor in the Speedbar
-     frame.  If there is a window displaying an agenda view, the new
-     restriction takes effect immediately.
+     Restrict the agenda to the item---either an Org file or a subtree
+     in such a file---at the cursor in the Speedbar frame.  If agenda
+     is already restricted there, remove the restriction.  If there is
+     a window displaying an agenda view, the new restriction takes
+     effect immediately.
 
 - {{{kbd(>)}}} (~org-agenda-remove-restriction-lock~) ::
 
      #+findex: org-agenda-remove-restriction-lock
-     Lift the restriction.
+     Remove the restriction.
 
 ** The Agenda Dispatcher
 :PROPERTIES:

+ 7 - 0
etc/ORG-NEWS

@@ -298,6 +298,13 @@ parameters.  See example bellow.
   ,#+END_SRC
 #+END_SRC
 
+*** ~org-agenda-set-restriction-lock~ toggle agenda restriction at point
+
+You can set an agenda restriction lock with =C-x C-x <= or with =<= at the
+beginning of a headline when using Org speed commands.  Now, if there
+is already a restriction at point, hitting =<= again (or =C-x C-x <=) will
+remove it.
+
 ** New functions
 
 *** ~org-insert-structure-template~

+ 43 - 36
lisp/org-agenda.el

@@ -7124,43 +7124,50 @@ Argument ARG is the prefix argument."
 
 ;;;###autoload
 (defun org-agenda-set-restriction-lock (&optional type)
-  "Set restriction lock for agenda, to current subtree or file.
-Restriction will be the file if TYPE is `file', or if type is the
-universal prefix \\='(4), or if the cursor is before the first headline
-in the file.  Otherwise, restriction will be to the current subtree."
+  "Set restriction lock for agenda to current subtree or file.
+When in a restricted subtree, remove it.
+
+The restriction will span over the entire file if TYPE is `file',
+or if type is '(4), or if the cursor is before the first headline
+in the file. Otherwise, only apply the restriction to the current
+subtree."
   (interactive "P")
-  (org-agenda-remove-restriction-lock 'noupdate)
-  (and (equal type '(4)) (setq type 'file))
-  (setq type (cond
-	      (type type)
-	      ((org-at-heading-p) 'subtree)
-	      ((condition-case nil (org-back-to-heading t) (error nil))
-	       'subtree)
-	      (t 'file)))
-  (if (eq type 'subtree)
-      (progn
-	(setq org-agenda-restrict (current-buffer))
-	(setq org-agenda-overriding-restriction 'subtree)
-	(put 'org-agenda-files 'org-restrict
-	     (list (buffer-file-name (buffer-base-buffer))))
-	(org-back-to-heading t)
-	(move-overlay org-agenda-restriction-lock-overlay
-		      (point)
-		      (if org-agenda-restriction-lock-highlight-subtree
-			  (save-excursion (org-end-of-subtree t t) (point))
-			(point-at-eol)))
-	(move-marker org-agenda-restrict-begin (point))
-	(move-marker org-agenda-restrict-end
-		     (save-excursion (org-end-of-subtree t t)))
-	(message "Locking agenda restriction to subtree"))
-    (put 'org-agenda-files 'org-restrict
-	 (list (buffer-file-name (buffer-base-buffer))))
-    (setq org-agenda-restrict nil)
-    (setq org-agenda-overriding-restriction 'file)
-    (move-marker org-agenda-restrict-begin nil)
-    (move-marker org-agenda-restrict-end nil)
-    (message "Locking agenda restriction to file"))
-  (setq current-prefix-arg nil)
+  (if (and org-agenda-overriding-restriction
+	   (member org-agenda-restriction-lock-overlay
+		   (overlays-at (point))))
+      (org-agenda-remove-restriction-lock 'noupdate)
+    (org-agenda-remove-restriction-lock 'noupdate)
+    (and (equal type '(4)) (setq type 'file))
+    (setq type (cond
+		(type type)
+		((org-at-heading-p) 'subtree)
+		((condition-case nil (org-back-to-heading t) (error nil))
+		 'subtree)
+		(t 'file)))
+    (if (eq type 'subtree)
+	(progn
+	  (setq org-agenda-restrict (current-buffer))
+	  (setq org-agenda-overriding-restriction 'subtree)
+	  (put 'org-agenda-files 'org-restrict
+	       (list (buffer-file-name (buffer-base-buffer))))
+	  (org-back-to-heading t)
+	  (move-overlay org-agenda-restriction-lock-overlay
+			(point)
+			(if org-agenda-restriction-lock-highlight-subtree
+			    (save-excursion (org-end-of-subtree t t) (point))
+			  (point-at-eol)))
+	  (move-marker org-agenda-restrict-begin (point))
+	  (move-marker org-agenda-restrict-end
+		       (save-excursion (org-end-of-subtree t t)))
+	  (message "Locking agenda restriction to subtree"))
+      (put 'org-agenda-files 'org-restrict
+	   (list (buffer-file-name (buffer-base-buffer))))
+      (setq org-agenda-restrict nil)
+      (setq org-agenda-overriding-restriction 'file)
+      (move-marker org-agenda-restrict-begin nil)
+      (move-marker org-agenda-restrict-end nil)
+      (message "Locking agenda restriction to file"))
+    (setq current-prefix-arg nil))
   (org-agenda-maybe-redo))
 
 (defun org-agenda-remove-restriction-lock (&optional noupdate)

+ 7 - 1
lisp/org-compat.el

@@ -688,6 +688,8 @@ This also applied for speedbar access."
 
 (defun org-speedbar-set-agenda-restriction ()
   "Restrict future agenda commands to the location at point in speedbar.
+If there is already a restriction lock at the location, remove it.
+
 To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
   (interactive)
   (require 'org-agenda)
@@ -698,7 +700,11 @@ To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
       (setq m (get-text-property p 'org-imenu-marker))
       (with-current-buffer (marker-buffer m)
 	(goto-char m)
-	(org-agenda-set-restriction-lock 'subtree)))
+	(if (and org-agenda-overriding-restriction
+		 (member org-agenda-restriction-lock-overlay
+			 (overlays-at (point))))
+	    (org-agenda-remove-restriction-lock 'noupdate)
+	  (org-agenda-set-restriction-lock 'subtree))))
      ((setq p (text-property-any (point-at-bol) (point-at-eol)
 				 'speedbar-function 'speedbar-find-file))
       (setq tp (previous-single-property-change