hamacs/elisp/ob-evennia.el
2026-10-16 10:52:47 -07:00

102 lines
3.4 KiB
EmacsLisp

;;; ob-evennia --- Evennia source blocks in Org -*- lexical-binding: t; -*-
;;
;; © 2025 Howard X. Abrams
;; Licensed under a Creative Commons Attribution 4.0 International License.
;; See http://creativecommons.org/licenses/by/4.0/
;;
;; Author: Howard X. Abrams <http://gitlab.com/howardabrams>
;; Maintainer: Howard X. Abrams
;; Created: January 18, 2025
;;
;; While obvious, GNU Emacs does not include this file or project.
;;
;; *NB:* Do not edit this file. Instead, edit the original literate file at:
;; /Users/howard/src/hamacs/pud.org
;; And tangle the file to recreate this one.
;;
;;; Code:
(defun pud-get-all-buffers ()
"Return a list of all buffers with a live PUD connection."
(save-window-excursion
(seq-filter (lambda (buf)
(switch-to-buffer buf)
(and
(eq major-mode 'pud-mode)
(get-buffer-process (current-buffer))))
(buffer-list))))
(defun pud-current-world ()
"Return buffer based on user choice of current PUD connections."
(let ((pud-buffers (pud-get-all-buffers)))
(cond
((null pud-buffers) nil)
((length= pud-buffers 1) (car pud-buffers))
(t
(completing-read "Choose connection: "
(seq-map (lambda (buf) (buffer-name buf))
pud-buffers))))))
(defun pud-send-string (buf-name text)
"Send TEXT to a comint buffer, BUF-NAME."
(save-window-excursion
(save-excursion
(pop-to-buffer buf-name)
(goto-char (point-max))
(comint-send-string (get-buffer-process (current-buffer))
(format "%s\n" text)))))
(defun pud-send-line (world)
"Send the current line or region to WORLD."
(interactive (list (pud-current-world)))
(unless world
(error "No current MUD connection."))
(let ((text (buffer-substring-no-properties
(if (region-active-p) (region-beginning)
(beginning-of-line-text) (point))
(if (region-active-p) (region-end)
(end-of-line) (point)))))
(pud-send-string world text)))
(global-set-key (kbd "<f6>") 'pud-send-line)
(defun pud-send-block (world)
"Send the current Org block to WORLD."
(interactive (list (pud-current-world)))
(unless world
(error "No current MUD connection."))
(let ((text (thread-last (org-element-at-point)
(org-src--contents-area)
(nth 2))))
(pud-send-string world
(replace-regexp-in-string
(rx (one-or-more space)) " " text))))
(require 'ob)
(defvar org-babel-default-header-args:evennia '())
(defun org-babel-execute:evennia (body params)
"Execute evennia BODY.
PARAMS can contain the following:
:session - The buffer to send the block
Called by `org-babel-execute-src-block'."
(let* ((session (cdr (assq :session params)))
(buffer (or session (pud-current-world))))
(if session
(setq buffer (format "*%s*" session)))
(pud-send-string buffer
(replace-regexp-in-string
(rx (one-or-more space)) " " body))
(message "No connected world.")))
(defun org-babel-prep-session:evennia (_session _params)
"Signal error; Evennia does not (currently) support sessions."
(error "Evennia sessions are nonsensical"))
(provide 'ob-evennia)