;;; qwan/qwan-markdown-dnd.el -*- lexical-binding: t; -*- ;;; Commentary: ;; Adds drag-and-drop support for markdown buffers. ;; When you drag an image from Finder (or other file manager) into a markdown ;; buffer, it is copied to `qwan-markdown-attachment-dir' and a markdown image ;; link is inserted at point. ;; ;; Configure per-repo by setting `qwan-markdown-attachment-dir' in ;; .dir-locals.el. Example: ;; ;; ((markdown-mode . ((qwan-markdown-attachment-dir . "assets/images")))) ;; ;; Requires Emacs' built-in dnd mechanism (no extra packages). ;;; Code: (provide 'qwan-markdown-dnd) (defvar-local qwan-markdown-attachment-dir "attachments" "Directory for dropped attachments, relative to the markdown file. Set this buffer-locally (e.g. via .dir-locals.el) to customise per project.") (defun is-image (filename) (let* ((mime-type (when (fboundp 'mailcap-file-name-to-mime-type) (mailcap-file-name-to-mime-type filename)))) (and mime-type (string-prefix-p "image/" mime-type)))) (ert-deftest detect-image () (should (is-image "bla.png")) (should-not (is-image "book.md"))) ;to run test via SPC m e b (eval buffer), uncomment: ;(ert 'detect-image ) ;;;###autoload (defun qwan-markdown-dnd-handler (uri action) "Handle dropping a file URI into a markdown buffer. Copies the file referenced by URI into `qwan-markdown-attachment-dir' and inserts a markdown link at point. Images get an `![]()' link, other files a regular `[]()' link. ACTION is ignored (always copies)." (ignore action) ;; not sure what ignore action is. the copy does not actually happen ;; is-image is not a function (mime-type is only used for is-image) ;; not sure how 'unless' works here (let* ((filename (dnd-get-local-file-name uri 'must-exist)) (mime-type (when (fboundp 'mailcap-file-name-to-mime-type) (mailcap-file-name-to-mime-type filename))) (is-image (and mime-type (string-prefix-p "image/" mime-type)))) (unless filename (error "Cannot find local file for %s" uri)) (let* ((buffer-dir (or (when (buffer-file-name) (file-name-directory (buffer-file-name))) default-directory)) (dest-dir (expand-file-name qwan-markdown-attachment-dir buffer-dir)) (dest-name (file-name-nondirectory filename)) (dest-file (expand-file-name dest-name dest-dir))) ;; Create destination directory if needed (unless (file-directory-p dest-dir) (make-directory dest-dir t)) ;; Handle name conflicts by appending a timestamp (when (file-exists-p dest-file) (message "Destination file %s already exists." dest-file) (setq dest-file (expand-file-name (concat (file-name-base dest-name) "_" (format-time-string "%Y%m%d_%H%M%S") "." (file-name-extension dest-name)) dest-dir))) ;; Copy the file (message "copying %s to %s." filename dest-file) (copy-file filename dest-file) ;; Insert the appropriate markdown link (let ((relative-path (file-relative-name dest-file buffer-dir))) (if is-image (insert (format "![%s](%s)" "" relative-path)) (insert (format "[%s](%s)" dest-name relative-path)))) ;; Signal that we've handled the drop 'private))) ;;;###autoload (defun qwan-markdown-setup-dnd () "Register DND handler for the current markdown buffer. Add this to `markdown-mode-hook'." (setq-local dnd-protocol-alist (append (list (cons "^\\(?:https?\\|ftp\\|file\\|nfs\\):" #'qwan-markdown-dnd-handler)) dnd-protocol-alist))) ;;;###autoload (add-hook 'markdown-mode-hook #'qwan-markdown-setup-dnd) (add-hook 'gfm-mode-hook #'qwan-markdown-setup-dnd) ;; what is gfm-mode-hook ? ;;; qwan-markdown-dnd.el ends here