 (defun pjs-org-capture-to-heading ()
   ;; :annotation is the link
   (let* ((link (plist-get org-capture-plist :annotation))
-         (heading (org-find-exact-headline-in-buffer link (current-buffer) nil)))
+         (heading (org-find-exact-headline-in-buffer link (current-buffer) t)))
     (if heading
-        (goto-char (marker-position heading))
+        (goto-char heading)
       (progn
-        (org-insert-heading)
-        (insert link)
-        (org-find-exact-headline-in-buffer link nil nil)))))
+        (goto-char (point-max))
+        (org-insert-heading nil nil t)
+        (insert link)))))
 
 (defun pjs-ensure-ending-newline ()
   "Add a newline at the end of the buffer if there isn't any."

         ("C-c C-l" . 'tc/insert-clubhouse-story-url)
         ("C-c a" . 'tc/insert-co-authored-by)
         ("C-c C-a" . 'tc/insert-co-authored-by)))

(defun tc/insert-clubhouse-story-url ()
  "Looks forward in the buffer for a story branch, and uses the
story id to generate and insert a url to the story."
  (interactive)
  (insert
   (save-excursion
     (when (re-search-forward "branch .*/ch\\([0-9]+\\)/")
       (format "https://app.clubhouse.io/internal/story/%s/" (match-string 1))))))

(defun tc/get-recent-git-authors (n)
  "Returns an alpha sorted list of the unique authors of the last
  n commits for the current git repo."
  (let ((default-directory (file-name-directory (buffer-file-name))))
    (split-string
     (shell-command-to-string
      (format "git log HEAD~%s.. --pretty='%%an <%%ae>' | sort | uniq" n))
     "\n")))

(defun tc/insert-co-authored-by ()
  "Inserts a Co-authored-by header from a selection from authors
  for the 50 most recent commits."
  (interactive)
  (insert (format "Co-authored-by: %s"
                  (completing-read "co-author: "
                                   (save-excursion
                                     (tc/get-recent-git-authors 50))))))

(use-package pinentry
  :ensure t

(use-package gnu-elpa-keyring-update
  :ensure t
  :config (gnu-elpa-keyring-update))

(defun tc/insert-clubhouse-story-url ()
  "Looks forward in the buffer for a story branch, and uses the
story id to generate and insert a url to the story."
  (interactive)
  (insert
   (save-excursion
     (when (re-search-forward "branch .*/ch\\([0-9]+\\)/")
       (format "https://app.clubhouse.io/internal/story/%s/" (match-string 1))))))

(defun tc/get-recent-git-authors (n)
  "Returns an alpha sorted list of the unique authors of the last
  n commits for the current git repo."
  (let ((default-directory (file-name-directory (buffer-file-name))))
    (split-string
     (shell-command-to-string
      (format "git log HEAD~%s.. --pretty='%%an <%%ae>' | sort | uniq" n))
     "\n")))

(defun tc/insert-co-authored-by ()
  "Inserts a Co-authored-by header from a selection from authors
  for the 50 most recent commits."
  (interactive)
  (insert (format "Co-authored-by: %s"
                  (completing-read "co-author: "
                                   (save-excursion
                                     (tc/get-recent-git-authors 50))))))

(use-package magit
  :ensure t
  :bind (("C-c g" . magit-status)
