Skip to content

Commit 47f60b7

Browse files
authored
Merge pull request #591 from jrblevin/syohex/590
Fix markdown-wiki-link-p overriding match data issue
2 parents 779a4c6 + faee757 commit 47f60b7

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- Fix too indended list face issue [GH-569][]
4040
- Fix creating imenu index issue when there is no level-1 header too[GH-571][]
4141
- Fix highlighting consecutive HTML comments[GH-584][]
42+
- Fix `markdown-follow-thing-at-point` failing on subdir search [GH-590][]
4243

4344
[gh-290]: https://github.com/jrblevin/markdown-mode/issues/290
4445
[gh-311]: https://github.com/jrblevin/markdown-mode/issues/311
@@ -62,6 +63,7 @@
6263
[gh-571]: https://github.com/jrblevin/markdown-mode/issues/571
6364
[gh-584]: https://github.com/jrblevin/markdown-mode/issues/584
6465
[gh-587]: https://github.com/jrblevin/markdown-mode/issues/587
66+
[gh-590]: https://github.com/jrblevin/markdown-mode/pull/590
6567

6668
# Markdown Mode 2.4
6769

markdown-mode.el

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7839,35 +7839,37 @@ directory first, then in subdirectories if
78397839
`markdown-wiki-link-search-subdirectories' is non-nil, and then
78407840
in parent directories if
78417841
`markdown-wiki-link-search-parent-directories' is non-nil."
7842-
(let* ((basename (replace-regexp-in-string
7843-
"[[:space:]\n]" markdown-link-space-sub-char name))
7844-
(basename (if (derived-mode-p 'gfm-mode)
7845-
(concat (upcase (substring basename 0 1))
7846-
(downcase (substring basename 1 nil)))
7847-
basename))
7848-
directory extension default candidates dir)
7849-
(when buffer-file-name
7850-
(setq directory (file-name-directory buffer-file-name)
7851-
extension (file-name-extension buffer-file-name)))
7852-
(setq default (concat basename
7853-
(when extension (concat "." extension))))
7854-
(cond
7855-
;; Look in current directory first.
7856-
((or (null buffer-file-name)
7857-
(file-exists-p default))
7858-
default)
7859-
;; Possibly search in subdirectories, next.
7860-
((and markdown-wiki-link-search-subdirectories
7861-
(setq candidates
7862-
(directory-files-recursively
7863-
directory (concat "^" default "$"))))
7864-
(car candidates))
7865-
;; Possibly search in parent directories as a last resort.
7866-
((and markdown-wiki-link-search-parent-directories
7867-
(setq dir (locate-dominating-file directory default)))
7868-
(concat dir default))
7869-
;; If nothing is found, return default in current directory.
7870-
(t default))))
7842+
(save-match-data
7843+
;; This function must not overwrite match data(PR #590)
7844+
(let* ((basename (replace-regexp-in-string
7845+
"[[:space:]\n]" markdown-link-space-sub-char name))
7846+
(basename (if (derived-mode-p 'gfm-mode)
7847+
(concat (upcase (substring basename 0 1))
7848+
(downcase (substring basename 1 nil)))
7849+
basename))
7850+
directory extension default candidates dir)
7851+
(when buffer-file-name
7852+
(setq directory (file-name-directory buffer-file-name)
7853+
extension (file-name-extension buffer-file-name)))
7854+
(setq default (concat basename
7855+
(when extension (concat "." extension))))
7856+
(cond
7857+
;; Look in current directory first.
7858+
((or (null buffer-file-name)
7859+
(file-exists-p default))
7860+
default)
7861+
;; Possibly search in subdirectories, next.
7862+
((and markdown-wiki-link-search-subdirectories
7863+
(setq candidates
7864+
(directory-files-recursively
7865+
directory (concat "^" default "$"))))
7866+
(car candidates))
7867+
;; Possibly search in parent directories as a last resort.
7868+
((and markdown-wiki-link-search-parent-directories
7869+
(setq dir (locate-dominating-file directory default)))
7870+
(concat dir default))
7871+
;; If nothing is found, return default in current directory.
7872+
(t default)))))
78717873

78727874
(defun markdown-follow-wiki-link (name &optional other)
78737875
"Follow the wiki link NAME.

tests/markdown-test.el

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6352,6 +6352,23 @@ x|"
63526352
(markdown-test-range-has-property 19 26 'font-lock-face 'markdown-link-face))
63536353
(kill-buffer)))))
63546354

6355+
(ert-deftest test-markdown-ext/wiki-link-keep-match-data ()
6356+
"Test that markdown-wiki-link-p keeps expected match data.
6357+
Detail: https://github.com/jrblevin/markdown-mode/pull/590"
6358+
(let ((markdown-enable-wiki-links t)
6359+
(markdown-link-space-sub-char " ")
6360+
(markdown-wiki-link-search-subdirectories t))
6361+
(progn
6362+
(find-file "wiki/pr590/Guide.md")
6363+
(unwind-protect
6364+
(progn
6365+
(markdown-mode)
6366+
(re-search-forward "Zettel Markdown")
6367+
(goto-char (match-beginning 0))
6368+
(should (markdown-wiki-link-p))
6369+
(should (string= (markdown-wiki-link-link) "Zettel Markdown")))
6370+
(kill-buffer)))))
6371+
63556372
(ert-deftest test-markdown-ext/wiki-link-major-mode ()
63566373
"Test major-mode of linked page."
63576374
(let ((markdown-enable-wiki-links t)

tests/wiki/pr590/Guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**Basics**: A neuron notebook is just a directory of [[Zettel Markdown]] files
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Zettel files are written in Markdown
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Math support

0 commit comments

Comments
 (0)