feat: add markdown drag-and-drop for blog images

- Fix org DND flag: +download-dnd → +dragndrop (was silently no-op)
- New qwan-markdown-dnd.el: handles file drops in markdown buffers
  copies images to configurable attachment dir (default: attachments/)
  inserts markdown image links at point
  per-repo config via .dir-locals.el (qwan-markdown-attachment-dir)
- AGENTS.md: document the new module and DND setup
This commit is contained in:
Willem van den Ende
2026-06-28 13:23:59 +02:00
parent 717c358ce3
commit 6c2a08bee5
4 changed files with 206 additions and 3 deletions
+111
View File
@@ -0,0 +1,111 @@
# AGENTS.md — willem's .doom.d
This file provides context for LLM coding agents working on this Doom Emacs configuration repository.
## Repository Overview
This is a [Doom Emacs](https://github.com/doomemacs/doomemacs) configuration at `~/.doom.d/`. The Doom framework lives in `~/.emacs.d/` (managed via straight.el). This repo only contains user configuration:
- `init.el` — Doom module declarations (what features to enable)
- `config.el` — Main user configuration (editor settings, keybindings, org, latex, gptel, etc.)
- `packages.el` — Package declarations for `doom sync`
- `custom.el` — Auto-generated by Emacs' customize system
- `config-mac.el` — Mac-specific configuration (gptel backends, LLM tools)
- `config-local.example.el` — Example local overrides (not committed with secrets)
### Custom Package Directories
**`mostalive/`** — Personal extensions by Willem (@mostalive)
- `my-org-roam.el` — `org-roam-node-insert-immediate` (insert link without opening buffer), org-agenda file collection, log file opener
- `journelly-convert-entry-to-org-roam.el` — Convert Journelly app entries to org-roam notes
- `convert-chats-to-org.el` — Chat log formatting utilities
**`qwan/`** — QWAN partnership utilities
- `qwan-blog.el` — Blog publishing helpers (publish draft to `../_posts/`, org-to-markdown link conversion, image markdown link creation, URL stripping)
- `qwan-refactor.el` — Elisp refactoring utilities with ERT tests (rename function at point, remove tests, `as-function-name`)
- `qwan-markdown-dnd.el` — Drag-and-drop image attachment for markdown buffers (copies dropped files to `attachments/`, inserts markdown link)
**`coe-xx/`** — Cross-stitch pattern editor (from Craft of Emacs guide)
- `coe-xx.el` — Major mode for `.coe-xx` pattern files
- `coe-demo.el` — Demo functions
## User Identity
- **Willem van den Ende** — `redacted@example.invalid` — @mostalive
- **QWAN** — partnership entity (Willem's collaborative practice)
- **Org-roam directory**: `~/MyDocuments/qwan/essays/willem/`
- **Blog**: Static site, markdown files in `../_posts/` relative to org-roam dir
- **Mastodon**: @mostalive@mastodon.social
## Workflow Patterns
### Key Commands
| Action | Command |
|--------|---------|
| Reload config | `M-x doom/reload` |
| Sync packages | `doom sync` (terminal) |
| Run all ERT tests | `(ert t)` |
| Run matching tests | `(ert "test-pattern-*")` |
### Code Style Conventions
- **Naming**: Functions use namespace prefixes: `qwan/`, `my/`, `org-roam-node-insert-immediate` (no conflict needed).
- **Testing**: ERT tests follow the function being tested. Test names use `test-` prefix or describe the function. Tests live in the same file as the code.
- **Loading**: Custom packages loaded via `add-load-path!` + `require` in `config.el`.
- **Formatting**: `elisp-autofmt` installed for Elisp formatting.
## Markdown Drag-and-Drop
**Status**: Implemented in `qwan/qwan-markdown-dnd.el` (see above).
**How it works**:
1. When a file is dragged from Finder into a markdown buffer, Emacs' `dnd-protocol-alist` dispatches to `qwan-markdown-dnd-handler`
2. The handler copies the file into `qwan-markdown-attachment-dir` (default `"attachments"`, relative to the markdown file)
3. On name conflicts, appends a timestamp suffix
4. For images, inserts `![alt](path)`; for other files, inserts `[filename](path)`
5. Configure per-repo via `.dir-locals.el`:
```elisp
((markdown-mode . ((qwan-markdown-attachment-dir . "assets/images"))))
```
**Related existing code in `qwan/qwan-blog.el`**:
- `qwan/image-mode-copy-filename-as-markdown-image-link` — copies markdown image link to kill-ring from an image buffer
- `qwan/relative-path-to-attachments` — strips prefix to get relative path from `/attachments`
- `qwan/string-as-markdown-image-link` — wraps path in markdown image syntax
**Org DND**: The `+dragndrop` flag (fixed from `+download-dnd`) enables `org-download` for org-mode screenshots, clipboard images, and nicer inline previews.
## Org-mode Configuration Highlights
- **org-roam** enabled with heavy customization
- **org-pomodoro** + gnuplot + pretty bullets
- **Export**: LaTeX with xelatex (ET Bembo font, Tufte style), HTML without preamble
- **Elfeed** RSS with org integration, org-roam capture for saved articles
- **Publishing**: Drafts published to `../_posts/` with date prefix
## LLM Integration (gptel / pi)
- **Primary backend**: Local llama.cpp server at `localhost:8000`
- **Alternative**: Ollama at `localhost:11434`
- **Presets**: `explain`, `elixir`, `elisp`, `glm-fast`, `glm-think`, `glm-search`
- **Custom tools**: `projectile-search` (code search), `search_notes` (org-roam RIP grep)
- **pi-coding-agent**: Also installed for agentic coding assistance
## Key Editor Settings
- **Theme**: leuven
- **Line numbers**: relative
- **Evil mode**: `evil-move-cursor-back` nil, `evil-move-beyond-eol` t
- **Company**: idle delay disabled
- **Magit**: magit-delta enabled, diff-hl disabled in magit (to avoid refresh errors)
- **Spell check**: aspell (hunspell not used)
## Important Gotchas
- Changing `init.el` or `packages.el` requires `doom sync` then `M-x doom/reload`
- `load!` is Doom's version of `load` for files relative to `doom-user-dir`
- `after!` runs code after a package has loaded
- `use-package!` is Doom's wrapper around `use-package`
- `map!` for keybindings (especially leader keys)
- `add-load-path!` for adding directories to Emacs' `load-path`
+7
View File
@@ -318,6 +318,7 @@
(add-load-path! "~/.doom.d/qwan")
(require 'qwan-blog)
(require 'qwan-refactor)
(require 'qwan-markdown-dnd)
;; Elfeed RSS configuration
;; Feeds are defined in org-directory (same as org-roam-directory)
@@ -389,6 +390,12 @@
(setq gptel-default-mode 'org-mode) ;; LLM replies in org-mode format
(require 'gptel-integrations)) ;; <- for mcp
;; emacs mode for pi
(use-package! pi-coding-agent
:commands pi
:init (defalias 'pi 'pi-coding-agent)
:custom pi-coding-agent-executable '("devcontainer" "exec" "pi"))
;; Load machine-specific configuration (gptel backends, etc.)
(load! "config-mac.el" doom-user-dir t)
+3 -3
View File
@@ -26,8 +26,8 @@
company ; the ultimate code completion backend
;;helm ; the *other* search engine for love and life
;;ido ; the other *other* search engine...
;;ivy ; a search engine for love and life
vertico
ivy ; a search engine for love and life
;; vertico
:ui
;;deft ; notational velocity for Emacs
@@ -152,7 +152,7 @@
;;nim ; python + lisp at the speed of c
;;nix ; I hereby declare "nix geht mehr!"
;;ocaml ; an objective camel
(org +pomodoro +gnuplot +pretty +roam +download-dnd +present) ; organize your plain life in plain text
(org +pomodoro +gnuplot +pretty +roam +dragndrop +present) ; organize your plain life in plain text
;;php ; perl's insecure younger brother
;; plantuml ; diagrams for confusing people more
;; purescript ; javascript, but functional
+85
View File
@@ -0,0 +1,85 @@
;;; 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.")
;;;###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)
(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)
(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
(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)
;;; qwan-markdown-dnd.el ends here