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-mailingl...@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
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: signature.asc
Description: This is a digitally signed message part

Reply via email to