From 66483dbf32df65d4290828c15d3a6e8d66078211 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Thu, 17 Apr 2025 14:34:53 +0200 Subject: [PATCH] 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 --- jsonian-tests.el | 26 ++++++++++++++++++++++++++ jsonian.el | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/jsonian-tests.el b/jsonian-tests.el index f56c12b..c558c0d 100644 --- a/jsonian-tests.el +++ b/jsonian-tests.el @@ -832,5 +832,31 @@ If START and END are provided, they are set as point and mark." (should (equal b (current-buffer))) (should (string= (buffer-string) json-string))))) +(ert-deftest jsonian-edit-string-hook () + "Test `jsonian-edit-string' moves to a new buffer and then returns correctly." + (with-temp-buffer + (let* ((json-string "{\"key\": \"some complex value\\nwith new lines\"}") + (b (current-buffer)) + (edit-buffer nil) + (hook-fn (lambda () + (should (string= "some complex value\nwith new lines" (buffer-string))) + (should (not (equal (current-buffer) b))) + (setq edit-buffer (current-buffer)) + (text-mode)))) + (add-hook 'jsonian-edit-string-hook hook-fn) + (jsonian-mode) + (insert json-string) + (goto-char 10) ;; Move the point into the "complex" string + (jsonian-edit-string) + (should (eq major-mode 'text-mode)) + (should (equal (current-buffer) edit-buffer)) + (delete-region (point-min) (point-max)) + (insert "some new value") + (jsonian-edit-mode-return) + (should (equal b (current-buffer))) + (should (string= (buffer-string) "{\"key\": \"some new value\"}")) + (should (eq major-mode 'jsonian-mode)) + (remove-hook 'jsonian-edit-string-hook hook-fn)))) + (provide 'jsonian-tests) ;;; jsonian-tests.el ends here diff --git a/jsonian.el b/jsonian.el index 94c1646..7723a06 100644 --- a/jsonian.el +++ b/jsonian.el @@ -1084,6 +1084,11 @@ POS must be a valid node location." (defvar-local jsonian-edit-return-var nil "Information necessary to jump back from `jsonian--edit-mode'.") +(defvar jsonian-edit-string-hook nil + "A normal hook run when `jsonian-edit-string' is called. + +It is run in the context of the edit buffer.") + (defun jsonian-edit-string () "Edit the string at point in another buffer." (interactive) @@ -1100,6 +1105,7 @@ POS must be a valid node location." (insert text) (jsonian--unintern-special-chars (current-buffer)) (goto-char (point-min)) + (run-hooks 'jsonian-edit-string-hook) (setq-local jsonian-edit-return-var (make-jsonian--edit-return :match match :back-buffer cbuffer