Skip to content

Commit

Permalink
Don't use regexp, DRY, improve docs
Browse files Browse the repository at this point in the history
Although immediate motivation is byte compiler error in older Emacs
due to rx not supporting "literal" clause, it is also the case that we
need not use a regexp at all.
  • Loading branch information
greghendershott committed Feb 12, 2024
1 parent 3dc7d92 commit 4ad7c29
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions racket-hash-lang.el
Expand Up @@ -313,18 +313,19 @@ named `racket-hash-lang:LANG-mode'.
LANG should be an unquoted symbol, same as you would use in a
Racket #lang line.
EXT should be a string with the file extension for LANG, not
EXT should be a string with the file extension for LANG, /not/
including any dot.
For example (racket-define-hash-lang rhombus \"rhm\")
- defines `racket-hash-lang:rhombus-mode'.
1. Defines `racket-hash-lang:rhombus-mode'.
- configures things like `auto-mode-alist', `org-src-lang-modes',
and `org-babel-tangle-lang-exts'.
2. Configures things like `auto-mode-alist', `org-src-lang-modes',
and `org-babel-tangle-lang-exts'.
- allows a buffer to omit the explicit #lang line, when it is by
`org-mode'.
3. Allows a buffer to omit the explicit #lang line, when it is
created by `org-mode' for user editing or formatting of a
source code block whose language property is \"rhombus\".
Although `racket-hash-lang-mode' works for any Racket hash-lang
simply by starting the buffer with a #lang line, some features in
Expand Down Expand Up @@ -403,17 +404,7 @@ Otherwise things like `racket-xp-mode' will report errors.
IFF we add one, arrange for a write-back function to remove it.
Note: `org-src--contents-for-write-back' strips text properties
so we can't insert a propertized string to look for later."
(save-excursion
(goto-char (point-min))
(unless (looking-at-p (rx bol "#lang" (1+ " ") (literal lang-str) "\n"))
(let ((lang-line-str (concat "#lang " lang-str "\n")))
(insert lang-line-str)
(setq org-src--allow-write-back
(lambda ()
(save-excursion
(goto-char (point-min))
(when (looking-at-p (rx bol (literal lang-line-str)))
(delete-region (point-min) (1+ (length lang-line-str)))))))))))
(racket--hash-lang-maybe-add-lang-line lang-str t))

(defun racket--hash-lang-org-babel-execute (lang-str body params)
"A basic way to run Racket programs using any #lang.
Expand All @@ -435,17 +426,28 @@ Only supports :result-type output -- not values."
body))
(tmp-src-file (org-babel-temp-file "racket-hash-lang-src-" ".rkt"))
(_ (with-temp-file tmp-src-file
;; Unless there's already a #lang line (according to
;; <100% reliable regexp test) add one.
(unless (string-match-p (rx bol "#lang" (1+ " ") (literal lang-str) "\n")
body)
(insert "#lang " lang-str "\n"))
(insert body)))
(insert body)
(racket--hash-lang-maybe-add-lang-line lang-str nil)))
(cmdline (concat racket-program " " tmp-src-file))
(result (org-babel-eval cmdline "")))
(delete-file tmp-src-file)
(org-babel-result-cond result-params result)))

(defun racket--hash-lang-maybe-add-lang-line (lang-str &optional set-write-back-p)
(let* ((lang-line-str (concat "#lang " lang-str "\n"))
(end-pos (1+ (length lang-line-str))))
(unless (string= (buffer-substring-no-properties (point-min) end-pos)
lang-line-str)
(save-excursion
(goto-char (point-min))
(insert lang-line-str))
(when set-write-back-p
(setq org-src--allow-write-back
(lambda ()
(when (string= (buffer-substring-no-properties (point-min) end-pos)
lang-line-str)
(delete-region (point-min) end-pos))))))))

;; org-babel support
;;
;; The above suffices for edit and tangle. Suffices for execute in
Expand Down

0 comments on commit 4ad7c29

Please sign in to comment.