A capture template can contain arbitrary elisp code using the %(...)
syntax. We just need a function that takes a string (which will be the region you are capturing) and strips the comment characters. The only fussy thing is learning what the comment start character is. This function runs in the capture buffer, not the original one so we have to jump back and lookup comment-start
.
(defun capture-comment-line (&optional line)
(let ((c
(save-excursion
(save-window-excursion
(switch-to-buffer (plist-get org-capture-plist :original-buffer))
comment-start)
)))
(while (string-prefix-p c line)
(setq line (string-remove-prefix c line)))
(comment-string-strip line t t)
))
Then we make a simple capture template:
(add-to-list 'org-capture-templates
'("C" "TODO comment" entry
(file+headline "code.org" "Tasks")
"* %(capture-comment-line \"%i\")\n %a"
))