Skip to content

Commit 66483db

Browse files
committed
Add jsonian-edit-string-hook
This should allow `jsonian` users to apply arbitrary changes (such as major mode changes) to the edit string environment. Fixes #58
1 parent bdd681d commit 66483db

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

jsonian-tests.el

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,5 +832,31 @@ If START and END are provided, they are set as point and mark."
832832
(should (equal b (current-buffer)))
833833
(should (string= (buffer-string) json-string)))))
834834

835+
(ert-deftest jsonian-edit-string-hook ()
836+
"Test `jsonian-edit-string' moves to a new buffer and then returns correctly."
837+
(with-temp-buffer
838+
(let* ((json-string "{\"key\": \"some complex value\\nwith new lines\"}")
839+
(b (current-buffer))
840+
(edit-buffer nil)
841+
(hook-fn (lambda ()
842+
(should (string= "some complex value\nwith new lines" (buffer-string)))
843+
(should (not (equal (current-buffer) b)))
844+
(setq edit-buffer (current-buffer))
845+
(text-mode))))
846+
(add-hook 'jsonian-edit-string-hook hook-fn)
847+
(jsonian-mode)
848+
(insert json-string)
849+
(goto-char 10) ;; Move the point into the "complex" string
850+
(jsonian-edit-string)
851+
(should (eq major-mode 'text-mode))
852+
(should (equal (current-buffer) edit-buffer))
853+
(delete-region (point-min) (point-max))
854+
(insert "some new value")
855+
(jsonian-edit-mode-return)
856+
(should (equal b (current-buffer)))
857+
(should (string= (buffer-string) "{\"key\": \"some new value\"}"))
858+
(should (eq major-mode 'jsonian-mode))
859+
(remove-hook 'jsonian-edit-string-hook hook-fn))))
860+
835861
(provide 'jsonian-tests)
836862
;;; jsonian-tests.el ends here

jsonian.el

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,11 @@ POS must be a valid node location."
10841084
(defvar-local jsonian-edit-return-var nil
10851085
"Information necessary to jump back from `jsonian--edit-mode'.")
10861086

1087+
(defvar jsonian-edit-string-hook nil
1088+
"A normal hook run when `jsonian-edit-string' is called.
1089+
1090+
It is run in the context of the edit buffer.")
1091+
10871092
(defun jsonian-edit-string ()
10881093
"Edit the string at point in another buffer."
10891094
(interactive)
@@ -1100,6 +1105,7 @@ POS must be a valid node location."
11001105
(insert text)
11011106
(jsonian--unintern-special-chars (current-buffer))
11021107
(goto-char (point-min))
1108+
(run-hooks 'jsonian-edit-string-hook)
11031109
(setq-local jsonian-edit-return-var (make-jsonian--edit-return
11041110
:match match
11051111
:back-buffer cbuffer

0 commit comments

Comments
 (0)