adopt(linux): add live-dictate and firehose-blog from linux-adopt
Two files that exist only on the Linux side, taken verbatim:
lisp/live-dictate.el streaming dictation over the Lemonade
OpenAI-compatible API (linux repo 978aa86)
qwan/firehose-blog.el drag-and-drop images into a firehose blog post,
with year dirs, collision suffixes and 16 ERT tests
Both are inert until something requires them. Wiring lives in
config-linux.el (next commit), not in the shared config.el: live-dictate
depends on whisper.el plus a Lemonade server on :13305, and on macOS
neither exists yet. Getting live dictation working there is a feature,
not a port: ffmpeg capture, an installed whisper.el, and a reachable
backend.
firehose-blog.el is OS-independent by design and should later replace
qwan/qwan-markdown-dnd.el on both sides -- see the note in config.el.
This commit is contained in:
@@ -0,0 +1,340 @@
|
|||||||
|
;;; live-dictate.el --- Live dictation via Lemonade realtime WebSocket -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
|
;; Author: Willem (generated with pi)
|
||||||
|
;; Version: 1.0.0
|
||||||
|
;; Package-Requires: ((emacs "27.1") (websocket "1.13"))
|
||||||
|
;; URL: https://github.com/lemonade-sdk/lemonade
|
||||||
|
|
||||||
|
;;; Commentary:
|
||||||
|
|
||||||
|
;; Live speech-to-text dictation using Lemonade Server's OpenAI
|
||||||
|
;; Realtime-compatible WebSocket endpoint (/v1/realtime).
|
||||||
|
;;
|
||||||
|
;; `live-dictate' toggles dictation in the current buffer: microphone audio
|
||||||
|
;; is captured with FFmpeg as 16kHz PCM16, streamed to the server over a
|
||||||
|
;; WebSocket, and interim/final transcripts are inserted at point as you
|
||||||
|
;; speak. Finalized utterances replace their interim preview.
|
||||||
|
;;
|
||||||
|
;; Concurrency note: websocket.el can desync its frame parser if frames are
|
||||||
|
;; sent from within its own network process filter (or any process filter).
|
||||||
|
;; All sends are therefore queued to an outbox and flushed from a timer.
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
|
|
||||||
|
(require 'json)
|
||||||
|
(require 'cl-lib)
|
||||||
|
|
||||||
|
(defgroup live-dictate nil
|
||||||
|
"Live dictation via Lemonade's realtime transcription WebSocket."
|
||||||
|
:group 'external)
|
||||||
|
|
||||||
|
(defcustom live-dictate-server-url "http://localhost:13305"
|
||||||
|
"Base URL of the Lemonade server (WS upgrade happens on /v1/realtime)."
|
||||||
|
:type 'string
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-model "Whisper-Large-v3-Turbo"
|
||||||
|
"Realtime transcription model. Must support `realtime-transcription'."
|
||||||
|
:type 'string
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-follow-cursor t
|
||||||
|
"Whether point follows the incoming transcription."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-stop-delay 2.0
|
||||||
|
"Seconds to wait after committing the buffer before closing the WebSocket."
|
||||||
|
:type 'number
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-flush-interval 0.1
|
||||||
|
"How often to flush queued audio to the WebSocket, in seconds."
|
||||||
|
:type 'number
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-ffmpeg-input-format
|
||||||
|
(pcase system-type
|
||||||
|
('gnu/linux (if (or (executable-find "pulseaudio") (executable-find "pipewire-pulse"))
|
||||||
|
"pulse" "alsa"))
|
||||||
|
('darwin "avfoundation")
|
||||||
|
('windows-nt "dshow")
|
||||||
|
(_ nil))
|
||||||
|
"Value passed to FFmpeg's `-f' flag to record audio."
|
||||||
|
:type '(choice string (const nil))
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-ffmpeg-input-device
|
||||||
|
(pcase system-type
|
||||||
|
('gnu/linux "default")
|
||||||
|
(_ nil))
|
||||||
|
"Value passed to FFmpeg's `-i' flag to record audio."
|
||||||
|
:type '(choice string (const nil))
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defcustom live-dictate-debug nil
|
||||||
|
"Verbose logging to *Messages*."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'live-dictate)
|
||||||
|
|
||||||
|
(defvar live-dictate--ws nil)
|
||||||
|
(defvar live-dictate--ffmpeg nil)
|
||||||
|
(defvar live-dictate--active nil)
|
||||||
|
(defvar live-dictate--outbox nil)
|
||||||
|
(defvar live-dictate--flush-timer nil)
|
||||||
|
(defvar live-dictate--stop-timer nil)
|
||||||
|
(defvar live-dictate--target-buffer nil)
|
||||||
|
(defvar live-dictate--marker nil)
|
||||||
|
(defvar live-dictate--interim-start nil)
|
||||||
|
(defvar live-dictate--interim-end nil)
|
||||||
|
(defvar live-dictate--chunk-count 0)
|
||||||
|
(defvar live-dictate--chunk-bytes 0)
|
||||||
|
(defvar live-dictate--stderr-buffer " *live-dictate-stderr*")
|
||||||
|
|
||||||
|
(defvar live-dictate--mode-line-indicator
|
||||||
|
(propertize " 🎙" 'face 'font-lock-warning-face))
|
||||||
|
|
||||||
|
(defun live-dictate--dbg (fmt &rest args)
|
||||||
|
(when live-dictate-debug
|
||||||
|
(message "live-dictate: %s" (apply #'format fmt args))))
|
||||||
|
|
||||||
|
(defun live-dictate--ws-url ()
|
||||||
|
(format "ws://%s/v1/realtime?model=%s"
|
||||||
|
(replace-regexp-in-string "^https?://" "" live-dictate-server-url)
|
||||||
|
(url-hexify-string live-dictate-model)))
|
||||||
|
|
||||||
|
(defun live-dictate--queue-send (payload)
|
||||||
|
"Queue JSON-able PAYLOAD for sending. Safe from any context."
|
||||||
|
(push payload live-dictate--outbox))
|
||||||
|
|
||||||
|
(defun live-dictate--flush-outbox ()
|
||||||
|
"Send all queued payloads. Runs from a timer, never inside process filters."
|
||||||
|
(when live-dictate--outbox
|
||||||
|
(if (and live-dictate--ws (websocket-openp live-dictate--ws))
|
||||||
|
(let ((queue (nreverse live-dictate--outbox)))
|
||||||
|
(setq live-dictate--outbox nil)
|
||||||
|
(live-dictate--dbg "flush: %d payload(s), first is %s"
|
||||||
|
(length queue)
|
||||||
|
(alist-get 'type (car queue)))
|
||||||
|
(catch 'dead
|
||||||
|
(dolist (payload queue)
|
||||||
|
(condition-case err
|
||||||
|
(websocket-send-text live-dictate--ws (json-encode payload))
|
||||||
|
(error
|
||||||
|
(message "live-dictate: send failed: %s" err)
|
||||||
|
(live-dictate--cleanup)
|
||||||
|
(throw 'dead nil))))))
|
||||||
|
(live-dictate--dbg "flush withheld: %d queued, ws=%s openp=%s status=%s"
|
||||||
|
(length live-dictate--outbox)
|
||||||
|
(and live-dictate--ws t)
|
||||||
|
(and live-dictate--ws (websocket-openp live-dictate--ws))
|
||||||
|
(and live-dictate--ws
|
||||||
|
(process-status (websocket-conn live-dictate--ws)))))))
|
||||||
|
|
||||||
|
(defun live-dictate--mode-line (show)
|
||||||
|
(when show
|
||||||
|
(cl-pushnew (list t live-dictate--mode-line-indicator)
|
||||||
|
global-mode-string :test #'equal))
|
||||||
|
(setq global-mode-string
|
||||||
|
(remove (list t live-dictate--mode-line-indicator) global-mode-string))
|
||||||
|
(force-mode-line-update t))
|
||||||
|
|
||||||
|
(defun live-dictate--clear-interim ()
|
||||||
|
"Drop the interim region markers without touching text."
|
||||||
|
(setq live-dictate--interim-start nil
|
||||||
|
live-dictate--interim-end nil))
|
||||||
|
|
||||||
|
(defun live-dictate--insert (text)
|
||||||
|
"Insert TEXT into the target buffer at the marker, replacing any interim text."
|
||||||
|
(when (buffer-live-p live-dictate--target-buffer)
|
||||||
|
(with-current-buffer live-dictate--target-buffer
|
||||||
|
(let (inhibit-read-only)
|
||||||
|
(save-excursion
|
||||||
|
(cond
|
||||||
|
((and live-dictate--interim-start live-dictate--interim-end)
|
||||||
|
;; deltas carry the full interim text: replace the whole region
|
||||||
|
(delete-region live-dictate--interim-start live-dictate--interim-end)
|
||||||
|
(goto-char live-dictate--interim-start)
|
||||||
|
(insert text)
|
||||||
|
(set-marker live-dictate--interim-end (point)))
|
||||||
|
(t
|
||||||
|
(goto-char live-dictate--marker)
|
||||||
|
(setq live-dictate--interim-start (point-marker))
|
||||||
|
(insert text)
|
||||||
|
(setq live-dictate--interim-end (point-marker)))))
|
||||||
|
(when live-dictate-follow-cursor
|
||||||
|
(goto-char (marker-position live-dictate--interim-end)))))))
|
||||||
|
|
||||||
|
(defun live-dictate--handle-message (payload)
|
||||||
|
(let* ((msg (condition-case _ (json-read-from-string payload)
|
||||||
|
(error nil)))
|
||||||
|
(type (and msg (alist-get 'type msg))))
|
||||||
|
(live-dictate--dbg "<- %s%s" (or type "(unparsed)")
|
||||||
|
(let ((d (alist-get 'delta msg)))
|
||||||
|
(if (stringp d) (concat " | " d) "")))
|
||||||
|
(pcase type
|
||||||
|
("conversation.item.input_audio_transcription.delta"
|
||||||
|
(let ((delta (alist-get 'delta msg)))
|
||||||
|
(when (and (stringp delta) (not (equal delta "")))
|
||||||
|
(live-dictate--insert delta))))
|
||||||
|
("conversation.item.input_audio_transcription.completed"
|
||||||
|
(let ((transcript (alist-get 'transcript msg)))
|
||||||
|
(when (stringp transcript)
|
||||||
|
;; replace the interim preview with the final transcript
|
||||||
|
(live-dictate--insert (string-trim transcript))
|
||||||
|
(with-current-buffer live-dictate--target-buffer
|
||||||
|
(let (inhibit-read-only)
|
||||||
|
(save-excursion
|
||||||
|
(goto-char (marker-position live-dictate--interim-end))
|
||||||
|
(insert " ")
|
||||||
|
(set-marker live-dictate--interim-end (point))))
|
||||||
|
(set-marker live-dictate--marker (marker-position live-dictate--interim-end))
|
||||||
|
(live-dictate--clear-interim)))))
|
||||||
|
("error"
|
||||||
|
(message "live-dictate server error: %s"
|
||||||
|
(or (alist-get 'message (alist-get 'error msg)) payload)))
|
||||||
|
(_ nil))))
|
||||||
|
|
||||||
|
(defun live-dictate--on-message (_ws frame)
|
||||||
|
(when (eq (websocket-frame-opcode frame) 'text)
|
||||||
|
(live-dictate--handle-message (websocket-frame-payload frame))))
|
||||||
|
|
||||||
|
(defun live-dictate--audio-filter (_proc chunk)
|
||||||
|
"Queue raw PCM16 CHUNK from FFmpeg for sending."
|
||||||
|
(setq live-dictate--chunk-count (1+ live-dictate--chunk-count)
|
||||||
|
live-dictate--chunk-bytes (+ live-dictate--chunk-bytes (length chunk)))
|
||||||
|
(when (zerop (mod live-dictate--chunk-count 100))
|
||||||
|
(live-dictate--dbg "audio: %d chunks, %d bytes so far"
|
||||||
|
live-dictate--chunk-count live-dictate--chunk-bytes))
|
||||||
|
(live-dictate--queue-send
|
||||||
|
`((type . "input_audio_buffer.append")
|
||||||
|
(audio . ,(base64-encode-string chunk 'nolinebreaks)))))
|
||||||
|
|
||||||
|
(defun live-dictate--on-open (_ws)
|
||||||
|
(live-dictate--dbg "ws open")
|
||||||
|
(condition-case err
|
||||||
|
(progn
|
||||||
|
(live-dictate--queue-send
|
||||||
|
`((type . "session.update")
|
||||||
|
(session . ((model . ,live-dictate-model)))))
|
||||||
|
;; start flush timer (sending must not happen inside filter callbacks)
|
||||||
|
(when live-dictate--flush-timer (cancel-timer live-dictate--flush-timer))
|
||||||
|
(setq live-dictate--flush-timer
|
||||||
|
(run-with-timer live-dictate-flush-interval
|
||||||
|
live-dictate-flush-interval
|
||||||
|
#'live-dictate--flush-outbox))
|
||||||
|
(unless live-dictate--ffmpeg
|
||||||
|
(setq live-dictate--ffmpeg
|
||||||
|
(make-process
|
||||||
|
:name "live-dictate-ffmpeg"
|
||||||
|
:command `("ffmpeg" "-nostdin" "-hide_banner" "-loglevel" "error"
|
||||||
|
"-f" ,live-dictate-ffmpeg-input-format
|
||||||
|
"-i" ,live-dictate-ffmpeg-input-device
|
||||||
|
"-ac" "1" "-ar" "16000" "-f" "s16le" "pipe:1")
|
||||||
|
:connection-type 'pipe
|
||||||
|
:filter #'live-dictate--audio-filter
|
||||||
|
:stderr (get-buffer-create live-dictate--stderr-buffer)
|
||||||
|
:sentinel (lambda (_proc event)
|
||||||
|
(when (and live-dictate--active
|
||||||
|
(not (string-prefix-p "finished" event)))
|
||||||
|
(message "live-dictate: FFmpeg stopped (%s)"
|
||||||
|
(string-trim event)))))))
|
||||||
|
(live-dictate--dbg "on-open done"))
|
||||||
|
(error
|
||||||
|
(message "live-dictate: ERROR in on-open (was being swallowed): %S" err))))
|
||||||
|
|
||||||
|
(defun live-dictate--on-close (_ws)
|
||||||
|
(live-dictate--dbg "ws closed")
|
||||||
|
(setq live-dictate--ws nil))
|
||||||
|
|
||||||
|
(defun live-dictate--on-error (_ws _type err)
|
||||||
|
(message "live-dictate WebSocket error: %s" err)
|
||||||
|
(live-dictate--cleanup))
|
||||||
|
|
||||||
|
(defun live-dictate--cleanup ()
|
||||||
|
"Tear down all processes and state."
|
||||||
|
(setq live-dictate--active nil) ; keep the ffmpeg sentinel quiet
|
||||||
|
(when live-dictate--stop-timer
|
||||||
|
(cancel-timer live-dictate--stop-timer)
|
||||||
|
(setq live-dictate--stop-timer nil))
|
||||||
|
(when live-dictate--flush-timer
|
||||||
|
(cancel-timer live-dictate--flush-timer)
|
||||||
|
(setq live-dictate--flush-timer nil))
|
||||||
|
(when (process-live-p live-dictate--ffmpeg)
|
||||||
|
(delete-process live-dictate--ffmpeg))
|
||||||
|
(setq live-dictate--ffmpeg nil)
|
||||||
|
(when (and live-dictate--ws (websocket-openp live-dictate--ws))
|
||||||
|
(websocket-close live-dictate--ws))
|
||||||
|
(setq live-dictate--ws nil
|
||||||
|
live-dictate--outbox nil)
|
||||||
|
(live-dictate--clear-interim)
|
||||||
|
(when (markerp live-dictate--marker)
|
||||||
|
(set-marker live-dictate--marker nil))
|
||||||
|
(setq live-dictate--marker nil
|
||||||
|
live-dictate--target-buffer nil)
|
||||||
|
(live-dictate--mode-line nil))
|
||||||
|
|
||||||
|
(defun live-dictate--load-model ()
|
||||||
|
"Ask the server to load the transcription model, like the official clients do."
|
||||||
|
(condition-case err
|
||||||
|
(let ((url-request-method "POST")
|
||||||
|
(url-request-extra-headers '(("Content-Type" . "application/json")))
|
||||||
|
(url-request-data (json-encode `((model_name . ,live-dictate-model)))))
|
||||||
|
(with-timeout (20 (message "live-dictate: /load preflight timed out, continuing"))
|
||||||
|
(url-retrieve-synchronously
|
||||||
|
(concat live-dictate-server-url "/api/v1/load")
|
||||||
|
nil nil 20)
|
||||||
|
(live-dictate--dbg "model load preflight done")))
|
||||||
|
(error (message "live-dictate: /load preflight failed: %s (continuing)" err))))
|
||||||
|
|
||||||
|
(defun live-dictate--stop ()
|
||||||
|
"Stop dictation: commit remaining audio, then close."
|
||||||
|
(setq live-dictate--active nil) ; before killing ffmpeg, so the sentinel stays quiet
|
||||||
|
(when (process-live-p live-dictate--ffmpeg)
|
||||||
|
(delete-process live-dictate--ffmpeg))
|
||||||
|
(setq live-dictate--ffmpeg nil)
|
||||||
|
(live-dictate--queue-send '((type . "input_audio_buffer.commit")))
|
||||||
|
(live-dictate--flush-outbox)
|
||||||
|
(setq live-dictate--stop-timer
|
||||||
|
(run-with-timer live-dictate-stop-delay nil
|
||||||
|
(lambda ()
|
||||||
|
(live-dictate--cleanup)
|
||||||
|
(message "live-dictate: stopped")))))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun live-dictate ()
|
||||||
|
"Toggle live dictation in the current buffer.
|
||||||
|
While active, speech is transcribed continuously via Lemonade's realtime
|
||||||
|
endpoint and text is inserted at point as it arrives. Run again to stop."
|
||||||
|
(interactive)
|
||||||
|
(if live-dictate--active
|
||||||
|
(live-dictate--stop)
|
||||||
|
(unless (require 'websocket nil t)
|
||||||
|
(user-error "live-dictate needs the `websocket' package (run `doom sync')"))
|
||||||
|
(when live-dictate-debug
|
||||||
|
(setq websocket-debug t ; frames -> *websocket ...debug* buffer
|
||||||
|
websocket-callback-debug-on-error t)) ; surface swallowed callback errors
|
||||||
|
(unless (and live-dictate-ffmpeg-input-format live-dictate-ffmpeg-input-device)
|
||||||
|
(user-error "live-dictate: set live-dictate-ffmpeg-input-format/-device"))
|
||||||
|
(unless (executable-find "ffmpeg")
|
||||||
|
(user-error "live-dictate: FFmpeg not found in PATH"))
|
||||||
|
(when buffer-read-only
|
||||||
|
(user-error "Buffer is read-only, can't dictate here"))
|
||||||
|
(setq live-dictate--active t
|
||||||
|
live-dictate--target-buffer (current-buffer)
|
||||||
|
live-dictate--marker (copy-marker (point))
|
||||||
|
live-dictate--chunk-count 0
|
||||||
|
live-dictate--chunk-bytes 0
|
||||||
|
live-dictate--outbox nil)
|
||||||
|
(live-dictate--mode-line t)
|
||||||
|
(live-dictate--load-model)
|
||||||
|
(setq live-dictate--ws
|
||||||
|
(websocket-open (live-dictate--ws-url)
|
||||||
|
:on-open #'live-dictate--on-open
|
||||||
|
:on-message #'live-dictate--on-message
|
||||||
|
:on-close #'live-dictate--on-close
|
||||||
|
:on-error #'live-dictate--on-error))
|
||||||
|
(message "live-dictate: connecting to %s..." live-dictate-server-url)))
|
||||||
|
|
||||||
|
(provide 'live-dictate)
|
||||||
|
;;; live-dictate.el ends here
|
||||||
@@ -0,0 +1,369 @@
|
|||||||
|
;;; firehose-blog.el --- Blog helpers for the firehose repo -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
|
;;; Commentary:
|
||||||
|
|
||||||
|
;; Drag-and-drop for firehose blog posts (app/priv/blog/<blog>/<YYYY>/*.md):
|
||||||
|
;;
|
||||||
|
;; - Dropping an image file copies it into
|
||||||
|
;; <firehose-blog-root>/app/priv/static/images/blog/<year>/ and inserts
|
||||||
|
;; `' at point.
|
||||||
|
;; The year comes from the post's own directory; clashing names get a
|
||||||
|
;; _YYYYMMDD_HHMMSS suffix (and a counter if that still clashes).
|
||||||
|
;; - Dropping any other file opens it, like the default Emacs behaviour.
|
||||||
|
;;
|
||||||
|
;; These are the same conventions implemented by scripts/roam-export-blog.el
|
||||||
|
;; in the firehose repo for the org-roam export flow.
|
||||||
|
;;
|
||||||
|
;; All firehose-specific decisions (destination directory and web path) live
|
||||||
|
;; in `firehose-blog--drop-target'; the DND machinery around it is
|
||||||
|
;; site-agnostic, so a Jekyll equivalent only needs its own drop-target
|
||||||
|
;; function.
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
|
|
||||||
|
(require 'dnd)
|
||||||
|
|
||||||
|
(defgroup firehose-blog nil
|
||||||
|
"Helpers for writing posts in the firehose repo."
|
||||||
|
:group 'markdown)
|
||||||
|
|
||||||
|
(defcustom firehose-blog-root "~/dev/elixir/firehose"
|
||||||
|
"Path to the firehose repository root."
|
||||||
|
:type 'directory)
|
||||||
|
|
||||||
|
(defcustom firehose-blog-image-extensions
|
||||||
|
'("svg" "png" "jpg" "jpeg" "gif" "webp" "avif")
|
||||||
|
"Dropped files with these extensions count as images."
|
||||||
|
:type '(repeat string))
|
||||||
|
|
||||||
|
(defcustom firehose-blog-alt-text "alt-text goes here"
|
||||||
|
"Alt text inserted for dropped images; edited in place afterwards."
|
||||||
|
:type 'string)
|
||||||
|
|
||||||
|
(defun firehose-blog--stamp ()
|
||||||
|
"Timestamp used to uniquify dropped images with clashing names."
|
||||||
|
(format-time-string "%Y%m%d_%H%M%S"))
|
||||||
|
|
||||||
|
(defun firehose-blog--post-year (post-file)
|
||||||
|
"Return the year directory of POST-FILE, or the current year.
|
||||||
|
|
||||||
|
Posts live in app/priv/blog/<blog>/<YYYY>/<file>.md; the immediate
|
||||||
|
parent directory is used as the year when it is four digits."
|
||||||
|
(let ((parent (car (nreverse (split-string (file-name-directory post-file)
|
||||||
|
"/" t)))))
|
||||||
|
(if (and parent (string-match-p "\\`[0-9]\\{4\\}\\'" parent))
|
||||||
|
parent
|
||||||
|
(format-time-string "%Y"))))
|
||||||
|
|
||||||
|
(defun firehose-blog--image-p (file)
|
||||||
|
"Return non-nil when FILE looks like an image."
|
||||||
|
(member (downcase (or (file-name-extension file) ""))
|
||||||
|
firehose-blog-image-extensions))
|
||||||
|
|
||||||
|
(defun firehose-blog--web-path (year name)
|
||||||
|
"Return the web path for NAME stored in the images dir of YEAR."
|
||||||
|
(format "/images/blog/%s/%s" year name))
|
||||||
|
|
||||||
|
(defun firehose-blog--unique-dest (dir name)
|
||||||
|
"Return a destination path for NAME in DIR that does not yet exist.
|
||||||
|
|
||||||
|
When NAME already exists in DIR, a _YYYYMMDD_HHMMSS stamp (and, if
|
||||||
|
that still clashes, a counter) is inserted before the extension."
|
||||||
|
(let* ((base (file-name-sans-extension name))
|
||||||
|
(ext (file-name-extension name))
|
||||||
|
(stamp (firehose-blog--stamp))
|
||||||
|
(candidate
|
||||||
|
(lambda (n)
|
||||||
|
(expand-file-name
|
||||||
|
(cond
|
||||||
|
((zerop n) name)
|
||||||
|
((and (= n 1) ext) (format "%s_%s.%s" base stamp ext))
|
||||||
|
((= n 1) (format "%s_%s" base stamp))
|
||||||
|
(ext (format "%s_%s_%d.%s" base stamp (1- n) ext))
|
||||||
|
(t (format "%s_%s_%d" base stamp (1- n))))
|
||||||
|
dir)))
|
||||||
|
(dest (funcall candidate 0))
|
||||||
|
(count 0))
|
||||||
|
(while (file-exists-p dest)
|
||||||
|
(setq count (1+ count)
|
||||||
|
dest (funcall candidate count)))
|
||||||
|
dest))
|
||||||
|
|
||||||
|
(defun firehose-blog--drop-target (source)
|
||||||
|
"Return (DEST-FILE . WEB-PATH) for dropping image SOURCE.
|
||||||
|
|
||||||
|
Called from a buffer visiting a firehose blog post; the year comes
|
||||||
|
from the post's own directory. This is the only firehose-specific
|
||||||
|
function; the DND machinery is site-agnostic so other blogs (e.g.
|
||||||
|
Jekyll) can supply their own drop-target."
|
||||||
|
(let* ((year (firehose-blog--post-year (buffer-file-name)))
|
||||||
|
(dest (firehose-blog--unique-dest
|
||||||
|
(expand-file-name
|
||||||
|
(format "app/priv/static/images/blog/%s" year)
|
||||||
|
firehose-blog-root)
|
||||||
|
(file-name-nondirectory source))))
|
||||||
|
(cons dest (firehose-blog--web-path year (file-name-nondirectory dest)))))
|
||||||
|
|
||||||
|
(defun firehose-blog--drop-image (source)
|
||||||
|
"Copy image SOURCE into the firehose static images dir and insert a link."
|
||||||
|
(let* ((target (firehose-blog--drop-target source))
|
||||||
|
(dest (car target))
|
||||||
|
(web-path (cdr target)))
|
||||||
|
(make-directory (file-name-directory dest) t)
|
||||||
|
(copy-file source dest)
|
||||||
|
(insert (format "" firehose-blog-alt-text web-path))))
|
||||||
|
|
||||||
|
(defun firehose-blog-in-post-p ()
|
||||||
|
"Return non-nil when the current buffer visits a firehose blog post."
|
||||||
|
(and buffer-file-name
|
||||||
|
(string-prefix-p (expand-file-name "app/priv/blog/" firehose-blog-root)
|
||||||
|
(expand-file-name buffer-file-name))))
|
||||||
|
|
||||||
|
(defun firehose-blog--dnd-file-handler (url &optional _action)
|
||||||
|
"DND handler for firehose blog posts.
|
||||||
|
Copy images into the static images dir and insert a web-path link;
|
||||||
|
open any other dropped file. A URI that cannot be resolved to a
|
||||||
|
local file is reported in *Messages* instead of opening nothing."
|
||||||
|
(let ((file (dnd-get-local-file-name url t)))
|
||||||
|
(cond
|
||||||
|
((and file (firehose-blog--image-p file))
|
||||||
|
(firehose-blog--drop-image file))
|
||||||
|
(file (dnd-open-local-file url nil))
|
||||||
|
(t (message "firehose-blog: cannot resolve dropped file URI %S" url)
|
||||||
|
nil))))
|
||||||
|
|
||||||
|
(defun firehose-blog--enable-dnd ()
|
||||||
|
"Install the firehose DND handler in the current buffer.
|
||||||
|
|
||||||
|
Only active in buffers visiting files under the firehose blog
|
||||||
|
tree; other markdown buffers keep the default handlers. Idempotent,
|
||||||
|
so it can be called from both the mode hook and the retrofit sweep."
|
||||||
|
(when (and (firehose-blog-in-post-p)
|
||||||
|
(not (rassq 'firehose-blog--dnd-file-handler dnd-protocol-alist)))
|
||||||
|
(setq-local dnd-protocol-alist
|
||||||
|
(cons '("^file:" . firehose-blog--dnd-file-handler)
|
||||||
|
dnd-protocol-alist))))
|
||||||
|
|
||||||
|
(defun firehose-blog--enable-dnd-in-existing-buffers ()
|
||||||
|
"Install the firehose DND handler in already-open blog buffers.
|
||||||
|
|
||||||
|
Mode hooks never re-run in existing buffers, so a post opened
|
||||||
|
before this file was loaded keeps the default handlers without
|
||||||
|
this sweep."
|
||||||
|
(dolist (buffer (buffer-list))
|
||||||
|
(with-current-buffer buffer
|
||||||
|
(when (derived-mode-p 'markdown-mode)
|
||||||
|
(firehose-blog--enable-dnd)))))
|
||||||
|
|
||||||
|
;; markdown-mode installs its own buffer-local `dnd-protocol-alist' in the
|
||||||
|
;; mode body, which runs before mode hooks, so hooking here makes our
|
||||||
|
;; handler take precedence in firehose posts. The sweep retrofits posts
|
||||||
|
;; that were already open when this file was loaded.
|
||||||
|
(with-eval-after-load 'markdown-mode
|
||||||
|
(add-hook 'markdown-mode-hook #'firehose-blog--enable-dnd)
|
||||||
|
(firehose-blog--enable-dnd-in-existing-buffers))
|
||||||
|
|
||||||
|
(defun firehose-blog--dnd-handle-one-url (fn window action url)
|
||||||
|
"Dispatch FN for a drop in the buffer of the drop WINDOW.
|
||||||
|
|
||||||
|
`dnd-handle-one-url' consults `dnd-protocol-alist' in the current
|
||||||
|
buffer, which is not necessarily the buffer the file was dropped
|
||||||
|
on: dropping on a window while another buffer is current runs the
|
||||||
|
wrong (or no) buffer-local handler. x-dnd.el never switches to
|
||||||
|
the drop window's buffer (not even `dnd-insert-text'), so force it
|
||||||
|
here. This repairs buffer-local drop handlers in general -- ours,
|
||||||
|
markdown-mode's and org-download's alike."
|
||||||
|
(if (window-live-p window)
|
||||||
|
(with-current-buffer (window-buffer window)
|
||||||
|
(funcall fn window action url))
|
||||||
|
(funcall fn window action url)))
|
||||||
|
|
||||||
|
(advice-add 'dnd-handle-one-url :around #'firehose-blog--dnd-handle-one-url)
|
||||||
|
|
||||||
|
(provide 'firehose-blog)
|
||||||
|
;;; firehose-blog.el ends here
|
||||||
|
|
||||||
|
;;; Tests:
|
||||||
|
|
||||||
|
(require 'ert)
|
||||||
|
(require 'cl-lib)
|
||||||
|
|
||||||
|
(defvar firehose-blog--test-stamp nil
|
||||||
|
"Test override for `firehose-blog--stamp'.")
|
||||||
|
|
||||||
|
(defun firehose-blog-test--make-repo ()
|
||||||
|
"Create a fake firehose repo tree; return its root."
|
||||||
|
(let ((root (make-temp-file "firehose-blog-" t)))
|
||||||
|
(make-directory (expand-file-name "app/priv/blog/engineering/2026" root) t)
|
||||||
|
root))
|
||||||
|
|
||||||
|
(defun firehose-blog-test--post (root)
|
||||||
|
"Return a post path for the fake repo at ROOT."
|
||||||
|
(expand-file-name "app/priv/blog/engineering/2026/10-20-test-post.md" root))
|
||||||
|
|
||||||
|
(defun firehose-blog-test--image (name)
|
||||||
|
"Create an empty image file called NAME in a temp dir; return its path."
|
||||||
|
(let ((file (expand-file-name name (make-temp-file "firehose-blog-src-" t))))
|
||||||
|
(write-region "" nil file)
|
||||||
|
file))
|
||||||
|
|
||||||
|
(defmacro firehose-blog-test--with-fixed-stamp (&rest body)
|
||||||
|
"Run BODY with `firehose-blog--stamp' pinned to a fixed value."
|
||||||
|
`(cl-letf (((symbol-function 'firehose-blog--stamp)
|
||||||
|
(lambda () (or firehose-blog--test-stamp "20260101_000000"))))
|
||||||
|
,@body))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-post-year-comes-from-post-directory ()
|
||||||
|
(should (equal (firehose-blog--post-year
|
||||||
|
"/repo/app/priv/blog/engineering/2026/post.md")
|
||||||
|
"2026")))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-post-year-falls-back-to-current-year ()
|
||||||
|
(should (equal (firehose-blog--post-year "/repo/app/priv/blog/post.md")
|
||||||
|
(format-time-string "%Y"))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-image-p-recognises-image-extensions ()
|
||||||
|
(should (firehose-blog--image-p "shot.png"))
|
||||||
|
(should (firehose-blog--image-p "shot.PNG"))
|
||||||
|
(should (firehose-blog--image-p "diagram.svg"))
|
||||||
|
(should-not (firehose-blog--image-p "post.md"))
|
||||||
|
(should-not (firehose-blog--image-p "no-extension")))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-web-path-uses-images-blog-year ()
|
||||||
|
(should (equal (firehose-blog--web-path "2026" "shot.png")
|
||||||
|
"/images/blog/2026/shot.png")))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-unique-dest-keeps-name-when-free ()
|
||||||
|
(firehose-blog-test--with-fixed-stamp
|
||||||
|
(let ((dir (make-temp-file "firehose-blog-dst-" t)))
|
||||||
|
(should (equal (firehose-blog--unique-dest dir "shot.png")
|
||||||
|
(expand-file-name "shot.png" dir))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-unique-dest-stamps-clashing-name ()
|
||||||
|
(firehose-blog-test--with-fixed-stamp
|
||||||
|
(let ((dir (make-temp-file "firehose-blog-dst-" t)))
|
||||||
|
(write-region "" nil (expand-file-name "shot.png" dir))
|
||||||
|
(should (equal (firehose-blog--unique-dest dir "shot.png")
|
||||||
|
(expand-file-name "shot_20260101_000000.png" dir))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-unique-dest-counts-up-when-still-clashing ()
|
||||||
|
(firehose-blog-test--with-fixed-stamp
|
||||||
|
(let ((dir (make-temp-file "firehose-blog-dst-" t)))
|
||||||
|
(write-region "" nil (expand-file-name "shot.png" dir))
|
||||||
|
(write-region "" nil (expand-file-name "shot_20260101_000000.png" dir))
|
||||||
|
(should (equal (firehose-blog--unique-dest dir "shot.png")
|
||||||
|
(expand-file-name "shot_20260101_000000_1.png" dir))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-drop-image-copies-into-static-and-inserts-web-link ()
|
||||||
|
(let* ((root (firehose-blog-test--make-repo))
|
||||||
|
(source (firehose-blog-test--image "shot.png")))
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(firehose-blog--drop-image source)
|
||||||
|
(should (file-exists-p
|
||||||
|
(expand-file-name
|
||||||
|
"app/priv/static/images/blog/2026/shot.png" root)))
|
||||||
|
(should (equal (buffer-string)
|
||||||
|
""))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-drop-image-uniquifies-web-path-on-clash ()
|
||||||
|
(let* ((root (firehose-blog-test--make-repo))
|
||||||
|
(source (firehose-blog-test--image "shot.png")))
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(firehose-blog-test--with-fixed-stamp
|
||||||
|
(firehose-blog--drop-image source)
|
||||||
|
(firehose-blog--drop-image source)
|
||||||
|
(should (file-exists-p
|
||||||
|
(expand-file-name
|
||||||
|
"app/priv/static/images/blog/2026/shot.png" root)))
|
||||||
|
(should (file-exists-p
|
||||||
|
(expand-file-name
|
||||||
|
"app/priv/static/images/blog/2026/shot_20260101_000000.png"
|
||||||
|
root)))
|
||||||
|
(should (equal (buffer-string)
|
||||||
|
(concat ""
|
||||||
|
""))))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-in-post-p-accepts-blog-files-only ()
|
||||||
|
(let* ((root (firehose-blog-test--make-repo))
|
||||||
|
(readme (expand-file-name "README.md" root)))
|
||||||
|
(write-region "" nil readme)
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(should (firehose-blog-in-post-p)))
|
||||||
|
(with-current-buffer (find-file-noselect readme)
|
||||||
|
(should-not (firehose-blog-in-post-p))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-enable-dnd-installs-handler-in-posts-only ()
|
||||||
|
(let* ((root (firehose-blog-test--make-repo))
|
||||||
|
(readme (expand-file-name "README.md" root)))
|
||||||
|
(write-region "" nil readme)
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(firehose-blog--enable-dnd)
|
||||||
|
(should (eq (cdr (car dnd-protocol-alist))
|
||||||
|
#'firehose-blog--dnd-file-handler)))
|
||||||
|
(with-current-buffer (find-file-noselect readme)
|
||||||
|
(let ((before dnd-protocol-alist))
|
||||||
|
(firehose-blog--enable-dnd)
|
||||||
|
(should (equal dnd-protocol-alist before)))))))
|
||||||
|
|
||||||
|
(defun firehose-blog-test--handler-count ()
|
||||||
|
"Count our handler entries in the current buffer's alist."
|
||||||
|
(let ((n 0))
|
||||||
|
(dolist (entry dnd-protocol-alist n)
|
||||||
|
(when (eq (cdr entry) 'firehose-blog--dnd-file-handler)
|
||||||
|
(setq n (1+ n))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-enable-dnd-is-idempotent ()
|
||||||
|
(let ((root (firehose-blog-test--make-repo)))
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(firehose-blog--enable-dnd)
|
||||||
|
(firehose-blog--enable-dnd)
|
||||||
|
(should (= 1 (firehose-blog-test--handler-count)))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-enable-dnd-in-existing-buffers-retrofits-posts ()
|
||||||
|
(let* ((root (firehose-blog-test--make-repo))
|
||||||
|
(readme (expand-file-name "README.md" root)))
|
||||||
|
(write-region "" nil readme)
|
||||||
|
(let ((firehose-blog-root root)
|
||||||
|
(post-buffer (find-file-noselect (firehose-blog-test--post root)))
|
||||||
|
(readme-buffer (find-file-noselect readme)))
|
||||||
|
(cl-letf (((symbol-function 'derived-mode-p) (lambda (&rest _) t)))
|
||||||
|
(firehose-blog--enable-dnd-in-existing-buffers)
|
||||||
|
(with-current-buffer post-buffer
|
||||||
|
(should (eq (cdr (car dnd-protocol-alist))
|
||||||
|
'firehose-blog--dnd-file-handler)))
|
||||||
|
(with-current-buffer readme-buffer
|
||||||
|
(should-not (rassq 'firehose-blog--dnd-file-handler
|
||||||
|
dnd-protocol-alist)))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-dnd-file-handler-copies-dropped-image ()
|
||||||
|
(let* ((root (firehose-blog-test--make-repo))
|
||||||
|
(source (firehose-blog-test--image "shot.png")))
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(firehose-blog-test--with-fixed-stamp
|
||||||
|
(firehose-blog--dnd-file-handler (concat "file://" source))
|
||||||
|
(should (equal (buffer-string)
|
||||||
|
""))
|
||||||
|
(should (file-exists-p
|
||||||
|
(expand-file-name
|
||||||
|
"app/priv/static/images/blog/2026/shot.png" root))))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-dnd-file-handler-reports-unresolvable-uri ()
|
||||||
|
(let ((root (firehose-blog-test--make-repo)))
|
||||||
|
(let ((firehose-blog-root root))
|
||||||
|
(with-current-buffer (find-file-noselect (firehose-blog-test--post root))
|
||||||
|
(should-not (firehose-blog--dnd-file-handler "file:///does/not/exist.png"))
|
||||||
|
(should (equal (buffer-string) ""))))))
|
||||||
|
|
||||||
|
(ert-deftest firehose-blog-dnd-advice-passes-through-without-window ()
|
||||||
|
(let ((called nil))
|
||||||
|
(cl-letf (((symbol-function 'dnd-handle-one-url)
|
||||||
|
(lambda (_window _action _url) (setq called t))))
|
||||||
|
(firehose-blog--dnd-handle-one-url (lambda (&rest _) (setq called t))
|
||||||
|
'not-a-window 'copy "file:///x.png")
|
||||||
|
(should called))))
|
||||||
Reference in New Issue
Block a user