emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-capture and fast selection of tags
@ 2022-06-26  9:32 Christian Heinrich
  2022-07-11  2:02 ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Christian Heinrich @ 2022-06-26  9:32 UTC (permalink / raw)
  To: Emacs Orgmode

[-- Attachment #1: Type: text/plain, Size: 1138 bytes --]

Hi everyone,

I am trying to set up an org-capture template that presents me with the fast selection window for
tags instead of just the prompt one gets when using org-capture's %^g / %^G placeholders. I often
have project related tags that I need to apply to many but not all tasks, so this would help me a
lot.

I have my tags defined through #+TAGS in my org file. 

I first thought that using %(org-set-tags-command) should work; but the capture-buffer is narrowed
(i.e., no tags are available), so I used (setq org-complete-tags-always-offer-all-agenda-tags t) to
make my tags available there as well. They are available now, but the fast selection is not used, as
org-use-fast-tag-selection is set to 'auto'. When I set it explicitly to t, the fast selection is
shown but because of org-complete-tags-always-offer-all-agenda-tags, I have way too many options to
choose from. I only want the tags defined through #+TAGS to be offered through fast selection.

How can I achieve that? Is there a way to also make only the tags defined in the target file
available?

Thanks for any help!

Best regards
Christian

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-06-26  9:32 org-capture and fast selection of tags Christian Heinrich
@ 2022-07-11  2:02 ` Ihor Radchenko
  2022-07-14 10:12   ` Christian Heinrich
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2022-07-11  2:02 UTC (permalink / raw)
  To: Christian Heinrich; +Cc: Emacs Orgmode

Christian Heinrich <christian@gladbachcity.de> writes:

> Today, I went through your init.org on github (it's ... huge) and couldn't find anything there
> either.

That's because I rarely need to set standard tags when capturing staff.
So, I am content with entering tags manually.

> ... Do you have an idea
> on how to use fast selection of tags (using shortcuts) when capturing an entry?
> ...
> I first thought that using %(org-set-tags-command) should work; but the capture-buffer is narrowed
> (i.e., no tags are available),...

%(org-with-wide-buffer (org-set-tags-command)) may work.

Of course, we may also implement a proper capture selection for %^g
template. As usual, patches are welcome.

Best,
Ihor


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-07-11  2:02 ` Ihor Radchenko
@ 2022-07-14 10:12   ` Christian Heinrich
  2022-07-16  9:12     ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Christian Heinrich @ 2022-07-14 10:12 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3311 bytes --]

Hi Ihor,

thanks for your reply.

I looked at the code and came up with a patch that works for me (see below). However, this may
change behavior for others:

1. The original %^g will work on non-headlines, but if (org-set-tags-command) is called as I do,
this is no longer possible and would need to be checked (what would be a good way here?)

2. Can I really deduce from (org-use-fast-tag-selection) being non-nil that fast selection should be
used in capture templates as well? Does it actually make sense to incorporate this into %^g/G?

I am neither a lisp programmer nor acquainted with the org codebase; this is a draft I came up
with. If you can provide me with further feedback, I'm willing to make this more stable.


Best regards 
Christian



diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 9ef160d16..a2a05c69d 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -72,6 +72,7 @@
 (defvar crm-separator)
 (defvar org-end-time-was-given)
 (defvar org-keyword-properties)
+(defvar org-use-fast-tag-selection)
 (defvar org-remember-default-headline)
 (defvar org-remember-templates)
 (defvar org-store-link-plist)
@@ -1751,7 +1752,10 @@ Expansion occurs in a temporary Org mode buffer."
                             (org-global-tags-completion-table
                              (cond ((equal key "G") (org-agenda-files))
                                    (file (list file))
-                                   (t nil))))
+                                   (t nil)))))
+                (if (and (boundp 'org-use-fast-tag-selection) (not (null org-use-fast-tag-
selection)))
+                  (org-set-tags-command)
+                  (let* (
                            (org-add-colon-after-tag-completion t)
                            (ins (mapconcat
                                  #'identity
@@ -1765,7 +1769,7 @@ Expansion occurs in a temporary Org mode buffer."
                         (unless (eq (char-before) ?:) (insert ":"))
                         (insert ins)
                         (unless (eq (char-after) ?:) (insert ":"))
-                        (when (org-at-heading-p) (org-align-tags)))))
+                        (when (org-at-heading-p) (org-align-tags)))))))
                    ((or "C" "L")
                     (let ((insert-fun (if (equal key "C") #'insert
                                         (lambda (s) (org-insert-link 0 s)))))


On Mon, 2022-07-11 at 10:02 +0800, Ihor Radchenko wrote:
> Christian Heinrich <christian@gladbachcity.de> writes:
> 
> > Today, I went through your init.org on github (it's ... huge) and couldn't find anything there
> > either.
> 
> That's because I rarely need to set standard tags when capturing staff.
> So, I am content with entering tags manually.
> 
> > ... Do you have an idea
> > on how to use fast selection of tags (using shortcuts) when capturing an entry?
> > ...
> > I first thought that using %(org-set-tags-command) should work; but the capture-buffer is
> > narrowed
> > (i.e., no tags are available),...
> 
> %(org-with-wide-buffer (org-set-tags-command)) may work.
> 
> Of course, we may also implement a proper capture selection for %^g
> template. As usual, patches are welcome.
> 
> Best,
> Ihor
> 

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-07-14 10:12   ` Christian Heinrich
@ 2022-07-16  9:12     ` Ihor Radchenko
  2022-07-16 15:03       ` Christian Heinrich
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2022-07-16  9:12 UTC (permalink / raw)
  To: Christian Heinrich; +Cc: emacs-orgmode

Christian Heinrich <com-orgmode-mailinglist@gladbachcity.de> writes:

> I looked at the code and came up with a patch that works for me (see below). However, this may
> change behavior for others:

Thanks!

> 1. The original %^g will work on non-headlines, but if (org-set-tags-command) is called as I do,
> this is no longer possible and would need to be checked (what would be a good way here?)
>
> 2. Can I really deduce from (org-use-fast-tag-selection) being non-nil that fast selection should be
> used in capture templates as well? Does it actually make sense to incorporate this into %^g/G?
>
> I am neither a lisp programmer nor acquainted with the org codebase; this is a draft I came up
> with. If you can provide me with further feedback, I'm willing to make this more stable.

The idea is reasonable, but using org-set-tags-command is not ideal
indeed. Instead, you can take the relevant part of org-set-tags-command
(it is all but last line of the "t" cond clause) and put it into a
separate auxiliary function. Then, you can simply call that function
inside the org-set-tags-command and inside the capture template code.

Best,
Ihor


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-07-16  9:12     ` Ihor Radchenko
@ 2022-07-16 15:03       ` Christian Heinrich
  2022-07-17 10:14         ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Christian Heinrich @ 2022-07-16 15:03 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1841 bytes --]

Hi Ihor,

thanks for your feedback. I attached a patch, but I'm not sure why you said "all but the last line".
That last line for me was (org-set-tags tags), which proved important as otherwise the tags were not
set in the buffer.

Anyways, please find the patch attached. Feedback is welcome, particularly on naming the auxiliary
function (org-input-tags does not seem like a great name).

I didn't sign any papers yet, but as this is just a minor refactoring without new logic, I'm not
sure whether it would be required.

Best
Christian

On Sat, 2022-07-16 at 17:12 +0800, Ihor Radchenko wrote:
> Christian Heinrich <com-orgmode-mailinglist@gladbachcity.de> writes:
> 
> > I looked at the code and came up with a patch that works for me (see below). However, this may
> > change behavior for others:
> 
> Thanks!
> 
> > 1. The original %^g will work on non-headlines, but if (org-set-tags-command) is called as I do,
> > this is no longer possible and would need to be checked (what would be a good way here?)
> > 
> > 2. Can I really deduce from (org-use-fast-tag-selection) being non-nil that fast selection
> > should be
> > used in capture templates as well? Does it actually make sense to incorporate this into %^g/G?
> > 
> > I am neither a lisp programmer nor acquainted with the org codebase; this is a draft I came up
> > with. If you can provide me with further feedback, I'm willing to make this more stable.
> 
> The idea is reasonable, but using org-set-tags-command is not ideal
> indeed. Instead, you can take the relevant part of org-set-tags-command
> (it is all but last line of the "t" cond clause) and put it into a
> separate auxiliary function. Then, you can simply call that function
> inside the org-set-tags-command and inside the capture template code.
> 
> Best,
> Ihor

[-- Attachment #1.2: org-capture-add-fast-tag-select-support.diff --]
[-- Type: text/x-patch, Size: 5311 bytes --]

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 9ef160d16..4f7652ca3 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -72,6 +72,7 @@
 (defvar crm-separator)
 (defvar org-end-time-was-given)
 (defvar org-keyword-properties)
+(defvar org-use-fast-tag-selection)
 (defvar org-remember-default-headline)
 (defvar org-remember-templates)
 (defvar org-store-link-plist)
@@ -1751,21 +1752,8 @@ Expansion occurs in a temporary Org mode buffer."
 			     (org-global-tags-completion-table
 			      (cond ((equal key "G") (org-agenda-files))
 				    (file (list file))
-				    (t nil))))
-			    (org-add-colon-after-tag-completion t)
-			    (ins (mapconcat
-				  #'identity
-				  (let ((crm-separator "[ \t]*:[ \t]*"))
-                                    (completing-read-multiple
-				     (if prompt (concat prompt ": ") "Tags: ")
-				     org-last-tags-completion-table nil nil nil
-				     'org-tags-history))
-				  ":")))
-		       (when (org-string-nw-p ins)
-			 (unless (eq (char-before) ?:) (insert ":"))
-			 (insert ins)
-			 (unless (eq (char-after) ?:) (insert ":"))
-			 (when (org-at-heading-p) (org-align-tags)))))
+				    (t nil)))))
+               (org-input-tags)))
 		    ((or "C" "L")
 		     (let ((insert-fun (if (equal key "C") #'insert
 					 (lambda (s) (org-insert-link 0 s)))))
diff --git a/lisp/org.el b/lisp/org.el
index dd33028c6..6631d2a0a 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -11445,49 +11445,53 @@ in Lisp code use `org-set-tags' instead."
 	   'region)
 	 (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))))
      (t
-      (save-excursion
-	(org-back-to-heading)
-	(let* ((all-tags (org-get-tags))
-	       (table (setq org-last-tags-completion-table
-			    (org--tag-add-to-alist
-			     (and org-complete-tags-always-offer-all-agenda-tags
-				  (org-global-tags-completion-table
-				   (org-agenda-files)))
-			     (or org-current-tag-alist (org-get-buffer-tags)))))
-	       (current-tags
-		(cl-remove-if (lambda (tag) (get-text-property 0 'inherited tag))
-			      all-tags))
-	       (inherited-tags
-		(cl-remove-if-not (lambda (tag) (get-text-property 0 'inherited tag))
-				  all-tags))
-	       (tags
-		(replace-regexp-in-string
-		 ;; Ignore all forbidden characters in tags.
-		 "[^[:alnum:]_@#%]+" ":"
-		 (if (or (eq t org-use-fast-tag-selection)
-			 (and org-use-fast-tag-selection
-			      (delq nil (mapcar #'cdr table))))
-		     (org-fast-tag-selection
-		      current-tags
-		      inherited-tags
-		      table
-		      (and org-fast-tag-selection-include-todo org-todo-key-alist))
-		   (let ((org-add-colon-after-tag-completion (< 1 (length table)))
-                         (crm-separator "[ \t]*:[ \t]*"))
-		     (mapconcat #'identity
-                                (completing-read-multiple
-			         "Tags: "
-			         org-last-tags-completion-table
-			         nil nil (org-make-tag-string current-tags)
-			         'org-tags-history)
-                                ":"))))))
-	  (org-set-tags tags)))))
+      (org-input-tags)))
     ;; `save-excursion' may not replace the point at the right
     ;; position.
     (when (and (save-excursion (skip-chars-backward "*") (bolp))
 	       (looking-at-p " "))
       (forward-char))))
 
+(defun org-input-tags ()
+  "Test"
+  (save-excursion
+    (org-back-to-heading)
+    (let* ((all-tags (org-get-tags))
+           (table (setq org-last-tags-completion-table
+                        (org--tag-add-to-alist
+                          (and org-complete-tags-always-offer-all-agenda-tags
+                               (org-global-tags-completion-table
+                                 (org-agenda-files)))
+                          (or org-current-tag-alist (org-get-buffer-tags)))))
+           (current-tags
+             (cl-remove-if (lambda (tag) (get-text-property 0 'inherited tag))
+                           all-tags))
+           (inherited-tags
+             (cl-remove-if-not (lambda (tag) (get-text-property 0 'inherited tag))
+                               all-tags))
+           (tags
+             (replace-regexp-in-string
+               ;; Ignore all forbidden characters in tags.
+               "[^[:alnum:]_@#%]+" ":"
+               (if (or (eq t org-use-fast-tag-selection)
+                       (and org-use-fast-tag-selection
+                            (delq nil (mapcar #'cdr table))))
+                 (org-fast-tag-selection
+                   current-tags
+                   inherited-tags
+                   table
+                   (and org-fast-tag-selection-include-todo org-todo-key-alist))
+                 (let ((org-add-colon-after-tag-completion (< 1 (length table)))
+                       (crm-separator "[ \t]*:[ \t]*"))
+                   (mapconcat #'identity
+                              (completing-read-multiple
+                                "Tags: "
+                                org-last-tags-completion-table
+                                nil nil (org-make-tag-string current-tags)
+                                'org-tags-history)
+                              ":"))))))
+      (org-set-tags tags))))
+
 (defun org-align-tags (&optional all)
   "Align tags in current entry.
 When optional argument ALL is non-nil, align all tags in the

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-07-16 15:03       ` Christian Heinrich
@ 2022-07-17 10:14         ` Ihor Radchenko
  2022-08-23  2:17           ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2022-07-17 10:14 UTC (permalink / raw)
  To: Christian Heinrich; +Cc: emacs-orgmode

Christian Heinrich <com-orgmode-mailinglist@gladbachcity.de> writes:

> thanks for your feedback. I attached a patch, but I'm not sure why you said "all but the last line".
> That last line for me was (org-set-tags tags), which proved important as otherwise the tags were not
> set in the buffer.

By "all but last line" I implied that the part that reads the tags
should be factored out. That part should not modify the tags in file,
just read the tags using the preferred interface and return the selected
tag list. This return value can be used in org-set-tags-command to be
passed to org-set-tags. For org-capture the return value can be used to
insert the tags in place of %^g (rather than putting the tags under
headline unconditionally).

Now, after examining the code of org-set-tags-command more closely, I
found out that org-fast-tag-selection is actually tricky to use as I
described above. org-fast-tag-selection itself is changing the headline
tags even before the user finishes the selection process. So, an
appropriate patch would also need to alter org-fast-tag-selection in
such a way that it could be called without side effects (changing buffer
text) - it can be done by providing a new optional argument to
org-fast-tag-selection.

Best,
Ihor


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-07-17 10:14         ` Ihor Radchenko
@ 2022-08-23  2:17           ` Ihor Radchenko
  2022-09-28 18:40             ` Christian Heinrich
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2022-08-23  2:17 UTC (permalink / raw)
  To: Christian Heinrich; +Cc: emacs-orgmode


Christian, did you get a chance to take a look at my further comments on
the patch?

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-08-23  2:17           ` Ihor Radchenko
@ 2022-09-28 18:40             ` Christian Heinrich
  2022-09-29  3:57               ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Christian Heinrich @ 2022-09-28 18:40 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 450 bytes --]

Hi Ihor,

unfortunately, I didn't have any time to incorporate your comments but this is still on my todo
list. I hope I will have more time soon, but it will most likely take me a few weeks.

Thanks for checking in with me, I really appreciate your support!

Best regards
Christian

On Tue, 2022-08-23 at 10:17 +0800, Ihor Radchenko wrote:
> 
> Christian, did you get a chance to take a look at my further comments on
> the patch?
> 

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: org-capture and fast selection of tags
  2022-09-28 18:40             ` Christian Heinrich
@ 2022-09-29  3:57               ` Ihor Radchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Ihor Radchenko @ 2022-09-29  3:57 UTC (permalink / raw)
  To: Christian Heinrich; +Cc: emacs-orgmode

Christian Heinrich <com-orgmode-mailinglist@gladbachcity.de> writes:

> unfortunately, I didn't have any time to incorporate your comments but this is still on my todo
> list. I hope I will have more time soon, but it will most likely take me a few weeks.

No problem. It's not like we are in rush. Just wanted to make sure that
this email thread did not slip through the cracks.

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-09-29  3:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-26  9:32 org-capture and fast selection of tags Christian Heinrich
2022-07-11  2:02 ` Ihor Radchenko
2022-07-14 10:12   ` Christian Heinrich
2022-07-16  9:12     ` Ihor Radchenko
2022-07-16 15:03       ` Christian Heinrich
2022-07-17 10:14         ` Ihor Radchenko
2022-08-23  2:17           ` Ihor Radchenko
2022-09-28 18:40             ` Christian Heinrich
2022-09-29  3:57               ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).