Skip to content

Instantly share code, notes, and snippets.

@vherrmann
Created January 3, 2026 22:27
Show Gist options
  • Select an option

  • Save vherrmann/98bce5ef54243df1c5057ea2d256be84 to your computer and use it in GitHub Desktop.

Select an option

Save vherrmann/98bce5ef54243df1c5057ea2d256be84 to your computer and use it in GitHub Desktop.
Editing org-attach attachments at point
(cl-defun iko/run-process-async (name program &key stdin args on-exit)
"Run PROGRAM asynchronously with ARGS, outputting to BUFFER-NAME."
(with-current-buffer (get-buffer-create (concat "*" name "*"))
(let ((b (current-buffer))
(p (apply #'start-process name (current-buffer) program args)))
(set-process-sentinel
p
(lambda (proc event)
(when (eq (process-status proc) 'exit)
(when on-exit
(funcall on-exit
(process-exit-status proc)
(with-current-buffer b (buffer-string))
proc
event))
(kill-buffer b))))
(when stdin
(process-send-string p (if (string-suffix-p "\n" stdin)
stdin
(concat stdin "\n")))
(process-send-eof p))
p)))
(defun iko/org-attach-get-absolute-path-at-point ()
(let* ((ctx (org-element-context)))
(if (and (eq (org-element-type ctx) 'link)
(string= (org-element-property :type ctx) "attachment"))
(let* ((raw (org-element-property :raw-link ctx))
(file (string-remove-prefix "attachment:" raw))
(abs (org-attach-expand file)))
(message "%s" abs)
abs)
(user-error "Not on an attachment link."))))
(defun iko/edit-attachment-at-point ()
(interactive)
(let ((file (iko/org-attach-get-absolute-path-at-point)))
(iko/run-process-async "ksnip" "ksnip"
:args `(,file)
:on-exit
(lambda (status-code _output _proc _event)
(if (= status-code 0)
(progn
(org-redisplay-inline-images)
(message "Successfully edited %s" file))
(user-error "ksnip failed with code %s" status-code))))))
@vherrmann
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment