Skip to content

Commit 5a4e33a

Browse files
authored
Do not let the vendored md-ts-mode claim Markdown files globally (dnouri#157)
pi-coding-agent uses the vendored md-ts-mode as an internal rendering engine for chat buffers. When that file registers itself in auto-mode-alist and treesit-major-mode-remap-alist, merely loading pi-coding-agent changes how unrelated Markdown files open throughout Emacs. That breaks user expectations and interferes with configurations that rely on markdown-mode hooks and keymaps. Remove those global registrations from the vendored copy, clarify that md-ts-mode opt-in remains explicit, and strengthen regression coverage. The tests now verify both loading the vendored mode itself and loading pi-coding-agent leave Markdown file associations untouched. A shared batch-Emacs test helper initializes packages before evaluation so the package-load regression test exercises the real loading path. See: dnouri#155
1 parent 1f0f2c6 commit 5a4e33a

3 files changed

Lines changed: 86 additions & 8 deletions

File tree

‎md-ts-mode.el‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,20 +1242,15 @@ VALUE non-nil hides markup, nil shows it."
12421242

12431243
;;;###autoload
12441244
(defun md-ts-mode-maybe ()
1245-
"Enable `md-ts-mode' when its grammar is available."
1245+
"Enable `md-ts-mode' when its grammar is available.
1246+
This helper is for explicit user configuration; loading the bundled
1247+
mode does not register global Markdown file associations or remaps."
12461248
(declare-function treesit-language-available-p "treesit.c")
12471249
(if (or (treesit-language-available-p 'markdown)
12481250
(eq treesit-enabled-modes t)
12491251
(memq 'md-ts-mode treesit-enabled-modes))
12501252
(md-ts-mode)
12511253
(fundamental-mode)))
12521254

1253-
;;;###autoload
1254-
(add-to-list 'auto-mode-alist '("\\.md\\'" . md-ts-mode-maybe))
1255-
;;;###autoload
1256-
(when (boundp 'treesit-major-mode-remap-alist)
1257-
(add-to-list 'treesit-major-mode-remap-alist
1258-
'(markdown-mode . md-ts-mode)))
1259-
12601255
(provide 'md-ts-mode)
12611256
;;; md-ts-mode.el ends here

‎test/pi-coding-agent-test-common.el‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ previous 90s timeout gave only 38%% margin; 180s gives ~177%%.")
3838
"Format SECONDS as a human-readable duration with millisecond precision."
3939
(format "%.3fs" (float seconds)))
4040

41+
;;;; Batch Emacs Helpers
42+
43+
(defun pi-coding-agent-test--read-batch-emacs-result (expression)
44+
"Evaluate EXPRESSION in a fresh batch Emacs and read its printed result.
45+
Initializes packages, then re-prepends the current project root to
46+
`load-path' so the checkout under test wins over any installed copy."
47+
(let* ((emacs (expand-file-name invocation-name invocation-directory))
48+
(repo-root (file-name-directory (locate-library "pi-coding-agent")))
49+
(output-buffer (generate-new-buffer " *pi-coding-agent-batch-emacs*"))
50+
(exit-code (call-process emacs nil output-buffer nil
51+
"--batch" "-Q" "-L" repo-root
52+
"--eval" "(require 'package)"
53+
"--eval" "(package-initialize)"
54+
"--eval" "(setq load-prefer-newer t)"
55+
"--eval"
56+
(format "(setq load-path (cons %S load-path))"
57+
repo-root)
58+
"--eval" expression)))
59+
(unwind-protect
60+
(progn
61+
(unless (eq 0 exit-code)
62+
(error "Batch Emacs exited with %s" exit-code))
63+
(with-current-buffer output-buffer
64+
(goto-char (point-min))
65+
(read (current-buffer))))
66+
(kill-buffer output-buffer))))
67+
4168
;;;; Waiting Helpers
4269

4370
(defun pi-coding-agent-test-wait-until (predicate &optional timeout poll-interval process)

‎test/pi-coding-agent-test.el‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,61 @@ must decide whether this is a no-op."
523523
(ignore-errors (kill-buffer "*pi-coding-agent-test-non-pi*"))
524524
(delete-other-windows)))))
525525

526+
(ert-deftest pi-coding-agent-test-vendored-md-ts-mode-leaves-global-markdown-settings-alone ()
527+
"Loading vendored `md-ts-mode' keeps Markdown associations opt-in."
528+
(let* ((expression
529+
(mapconcat
530+
#'identity
531+
'("(progn"
532+
" (defvar treesit-major-mode-remap-alist nil)"
533+
" (let ((before-auto (copy-tree auto-mode-alist))"
534+
" (before-remap (copy-tree treesit-major-mode-remap-alist)))"
535+
" (require 'md-ts-mode)"
536+
" (prin1 (list"
537+
" :auto-unchanged (equal before-auto auto-mode-alist)"
538+
" :remap-unchanged (equal before-remap treesit-major-mode-remap-alist)"
539+
" :md-mode-defined (fboundp 'md-ts-mode)"
540+
" :md-mode-maybe-defined (fboundp 'md-ts-mode-maybe)"
541+
" :before-md-association (assoc \"\\.md\\'\" before-auto)"
542+
" :after-md-association (assoc \"\\.md\\'\" auto-mode-alist)"
543+
" :before-markdown-remap (alist-get 'markdown-mode before-remap)"
544+
" :after-markdown-remap (alist-get 'markdown-mode treesit-major-mode-remap-alist)))))")
545+
" "))
546+
(result (pi-coding-agent-test--read-batch-emacs-result expression)))
547+
(should (eq t (plist-get result :auto-unchanged)))
548+
(should (eq t (plist-get result :remap-unchanged)))
549+
(should (eq t (plist-get result :md-mode-defined)))
550+
(should (eq t (plist-get result :md-mode-maybe-defined)))
551+
(should (equal (plist-get result :before-md-association)
552+
(plist-get result :after-md-association)))
553+
(should (equal (plist-get result :before-markdown-remap)
554+
(plist-get result :after-markdown-remap)))))
555+
556+
(ert-deftest pi-coding-agent-test-package-load-leaves-global-markdown-settings-alone ()
557+
"Loading `pi-coding-agent' does not change global Markdown mode settings."
558+
(let* ((expression
559+
(mapconcat
560+
#'identity
561+
'("(progn"
562+
" (defvar treesit-major-mode-remap-alist nil)"
563+
" (let ((before-auto (copy-tree auto-mode-alist))"
564+
" (before-remap (copy-tree treesit-major-mode-remap-alist)))"
565+
" (require 'pi-coding-agent)"
566+
" (prin1 (list"
567+
" :auto-unchanged (equal before-auto auto-mode-alist)"
568+
" :remap-unchanged (equal before-remap treesit-major-mode-remap-alist)"
569+
" :before-md-association (assoc \"\\.md\\'\" before-auto)"
570+
" :after-md-association (assoc \"\\.md\\'\" auto-mode-alist)"
571+
" :before-markdown-remap (alist-get 'markdown-mode before-remap)"
572+
" :after-markdown-remap (alist-get 'markdown-mode treesit-major-mode-remap-alist)))))")
573+
" "))
574+
(result (pi-coding-agent-test--read-batch-emacs-result expression)))
575+
(should (eq t (plist-get result :auto-unchanged)))
576+
(should (eq t (plist-get result :remap-unchanged)))
577+
(should (equal (plist-get result :before-md-association)
578+
(plist-get result :after-md-association)))
579+
(should (equal (plist-get result :before-markdown-remap)
580+
(plist-get result :after-markdown-remap)))))
581+
526582
(provide 'pi-coding-agent-test)
527583
;;; pi-coding-agent-test.el ends here

0 commit comments

Comments
 (0)