Skip to content

Commit

Permalink
Check for org-src--allow-write-back
Browse files Browse the repository at this point in the history
Not defined in Emacs 25.1. In that case we won't be able to
auto-remove a lang line that we auto-added.

Other small organization changes.
  • Loading branch information
greghendershott committed Feb 13, 2024
1 parent 4ad7c29 commit 4f8b2a3
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions racket-hash-lang.el
Expand Up @@ -367,7 +367,7 @@ for configuration."

;; Define org-babel-edit-prep:<lang>.
(defun ,org-babel-edit-prep-name (_babel-info)
(racket--hash-lang-org-babel-edit-prep-for-lang ,lang-str))
(racket--hash-lang-org-babel-edit-prep ,lang-str))

(define-derived-mode ,full-mode-name racket-hash-lang-mode
,lighter
Expand Down Expand Up @@ -396,14 +396,7 @@ for configuration."
(prog-mode) ;note: resets all buffer-local variables
(message "hash-lang support not available; needs newer syntax-color-lib"))))

(defun racket--hash-lang-org-babel-edit-prep-for-lang (lang-str)
"When the buffer lacks a lang line, add one.
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."
(defun racket--hash-lang-org-babel-edit-prep (lang-str)
(racket--hash-lang-maybe-add-lang-line lang-str t))

(defun racket--hash-lang-org-babel-execute (lang-str body params)
Expand Down Expand Up @@ -434,14 +427,22 @@ Only supports :result-type output -- not values."
(org-babel-result-cond result-params result)))

(defun racket--hash-lang-maybe-add-lang-line (lang-str &optional set-write-back-p)
"When the buffer lacks a lang line, add one.
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."
(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
(when (and set-write-back-p
(boundp 'org-src--allow-write-back)) ;>25.1
(setq org-src--allow-write-back
(lambda ()
(when (string= (buffer-substring-no-properties (point-min) end-pos)
Expand Down

0 comments on commit 4f8b2a3

Please sign in to comment.