From faee757b1a0664f9d146546f1bc6ade8e68cd4a8 Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Wed, 27 Jan 2021 13:32:13 +0900 Subject: [PATCH] Fix markdown-wiki-link-p overriding match data issue --- CHANGES.md | 2 + markdown-mode.el | 60 ++++++++++--------- tests/markdown-test.el | 17 ++++++ tests/wiki/pr590/Guide.md | 1 + tests/wiki/pr590/Guide/Zettel Markdown.md | 2 + .../wiki/pr590/Guide/Zettel Markdown/math.md | 1 + 6 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 tests/wiki/pr590/Guide.md create mode 100644 tests/wiki/pr590/Guide/Zettel Markdown.md create mode 100644 tests/wiki/pr590/Guide/Zettel Markdown/math.md diff --git a/CHANGES.md b/CHANGES.md index 518353c8..aebbb826 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -39,6 +39,7 @@ - Fix too indended list face issue [GH-569][] - Fix creating imenu index issue when there is no level-1 header too[GH-571][] - Fix highlighting consecutive HTML comments[GH-584][] + - Fix `markdown-follow-thing-at-point` failing on subdir search [GH-590][] [gh-290]: https://github.com/jrblevin/markdown-mode/issues/290 [gh-311]: https://github.com/jrblevin/markdown-mode/issues/311 @@ -62,6 +63,7 @@ [gh-571]: https://github.com/jrblevin/markdown-mode/issues/571 [gh-584]: https://github.com/jrblevin/markdown-mode/issues/584 [gh-587]: https://github.com/jrblevin/markdown-mode/issues/587 + [gh-590]: https://github.com/jrblevin/markdown-mode/pull/590 # Markdown Mode 2.4 diff --git a/markdown-mode.el b/markdown-mode.el index 66e1744e..f9cbd0c2 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -7839,35 +7839,37 @@ directory first, then in subdirectories if `markdown-wiki-link-search-subdirectories' is non-nil, and then in parent directories if `markdown-wiki-link-search-parent-directories' is non-nil." - (let* ((basename (replace-regexp-in-string - "[[:space:]\n]" markdown-link-space-sub-char name)) - (basename (if (derived-mode-p 'gfm-mode) - (concat (upcase (substring basename 0 1)) - (downcase (substring basename 1 nil))) - basename)) - directory extension default candidates dir) - (when buffer-file-name - (setq directory (file-name-directory buffer-file-name) - extension (file-name-extension buffer-file-name))) - (setq default (concat basename - (when extension (concat "." extension)))) - (cond - ;; Look in current directory first. - ((or (null buffer-file-name) - (file-exists-p default)) - default) - ;; Possibly search in subdirectories, next. - ((and markdown-wiki-link-search-subdirectories - (setq candidates - (directory-files-recursively - directory (concat "^" default "$")))) - (car candidates)) - ;; Possibly search in parent directories as a last resort. - ((and markdown-wiki-link-search-parent-directories - (setq dir (locate-dominating-file directory default))) - (concat dir default)) - ;; If nothing is found, return default in current directory. - (t default)))) + (save-match-data + ;; This function must not overwrite match data(PR #590) + (let* ((basename (replace-regexp-in-string + "[[:space:]\n]" markdown-link-space-sub-char name)) + (basename (if (derived-mode-p 'gfm-mode) + (concat (upcase (substring basename 0 1)) + (downcase (substring basename 1 nil))) + basename)) + directory extension default candidates dir) + (when buffer-file-name + (setq directory (file-name-directory buffer-file-name) + extension (file-name-extension buffer-file-name))) + (setq default (concat basename + (when extension (concat "." extension)))) + (cond + ;; Look in current directory first. + ((or (null buffer-file-name) + (file-exists-p default)) + default) + ;; Possibly search in subdirectories, next. + ((and markdown-wiki-link-search-subdirectories + (setq candidates + (directory-files-recursively + directory (concat "^" default "$")))) + (car candidates)) + ;; Possibly search in parent directories as a last resort. + ((and markdown-wiki-link-search-parent-directories + (setq dir (locate-dominating-file directory default))) + (concat dir default)) + ;; If nothing is found, return default in current directory. + (t default))))) (defun markdown-follow-wiki-link (name &optional other) "Follow the wiki link NAME. diff --git a/tests/markdown-test.el b/tests/markdown-test.el index db9c98ae..6487c2ff 100644 --- a/tests/markdown-test.el +++ b/tests/markdown-test.el @@ -6352,6 +6352,23 @@ x|" (markdown-test-range-has-property 19 26 'font-lock-face 'markdown-link-face)) (kill-buffer))))) +(ert-deftest test-markdown-ext/wiki-link-keep-match-data () + "Test that markdown-wiki-link-p keeps expected match data. +Detail: https://github.com/jrblevin/markdown-mode/pull/590" + (let ((markdown-enable-wiki-links t) + (markdown-link-space-sub-char " ") + (markdown-wiki-link-search-subdirectories t)) + (progn + (find-file "wiki/pr590/Guide.md") + (unwind-protect + (progn + (markdown-mode) + (re-search-forward "Zettel Markdown") + (goto-char (match-beginning 0)) + (should (markdown-wiki-link-p)) + (should (string= (markdown-wiki-link-link) "Zettel Markdown"))) + (kill-buffer))))) + (ert-deftest test-markdown-ext/wiki-link-major-mode () "Test major-mode of linked page." (let ((markdown-enable-wiki-links t) diff --git a/tests/wiki/pr590/Guide.md b/tests/wiki/pr590/Guide.md new file mode 100644 index 00000000..dd8c8e2a --- /dev/null +++ b/tests/wiki/pr590/Guide.md @@ -0,0 +1 @@ +**Basics**: A neuron notebook is just a directory of [[Zettel Markdown]] files diff --git a/tests/wiki/pr590/Guide/Zettel Markdown.md b/tests/wiki/pr590/Guide/Zettel Markdown.md new file mode 100644 index 00000000..f9289b5e --- /dev/null +++ b/tests/wiki/pr590/Guide/Zettel Markdown.md @@ -0,0 +1,2 @@ +Zettel files are written in Markdown + diff --git a/tests/wiki/pr590/Guide/Zettel Markdown/math.md b/tests/wiki/pr590/Guide/Zettel Markdown/math.md new file mode 100644 index 00000000..cca282e8 --- /dev/null +++ b/tests/wiki/pr590/Guide/Zettel Markdown/math.md @@ -0,0 +1 @@ +# Math support