[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/markdown-mode 15a0294 1/3: Implement searching wiki link u
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/markdown-mode 15a0294 1/3: Implement searching wiki link under project root |
Date: |
Tue, 2 Feb 2021 23:57:08 -0500 (EST) |
branch: elpa/markdown-mode
commit 15a0294bdcc1c3ec53d7de154c6892110de01a88
Author: Shohei YOSHIDA <syohex@gmail.com>
Commit: Shohei YOSHIDA <syohex@gmail.com>
Implement searching wiki link under project root
---
CHANGES.md | 1 +
README.md | 11 +++++-----
markdown-mode.el | 41 +++++++++++++++++++++++++++++++++--
tests/markdown-test.el | 21 +++++++++++++++++-
tests/wiki/pr590/Guide/Plugin/Link.md | 1 +
5 files changed, 66 insertions(+), 9 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index aebbb82..8ef39e4 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -11,6 +11,7 @@
- Option to inhibit the prompt for a tooltip text via
`markdown-disable-tooltip-prompt`.
- Introduce `markdown-ordered-list-enumeration` variable [GH-587][]
+ - Search wiki link under project
* Improvements:
- Correct indirect buffer's indentation in `markdown-edit-code-block`
[GH-375][]
diff --git a/README.md b/README.md
index 55ba1b4..c46bf2e 100644
--- a/README.md
+++ b/README.md
@@ -931,13 +931,12 @@ are also supported. Since some wikis reverse these
components, set
is also non-nil, Markdown Mode will highlight wiki links with
missing target file in a different color. By default, Markdown
Mode only searches for target files in the current directory.
-Search in subdirectories can be enabled by setting
-`markdown-wiki-link-search-subdirectories` to a non-nil value.
-Sequential parent directory search (as in [Ikiwiki][]) can be
-enabled by setting `markdown-wiki-link-search-parent-directories`
-to a non-nil value.
+You can control search type by setting `markdown-wiki-link-search-type`.
+This value type is a symbol list. Possible values are
-[Ikiwiki]: https://ikiwiki.info
+- `sub-directories` : search in sub directories
+- `parent-directories` : search in parent directories
+- `project` : search under project root
[SmartyPants][] support is possible by customizing `markdown-command`.
If you install `SmartyPants.pl` at, say, `/usr/local/bin/smartypants`,
diff --git a/markdown-mode.el b/markdown-mode.el
index f9cbd0c..91e982b 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -48,6 +48,8 @@
(defvar flyspell-generic-check-word-predicate)
(defvar electric-pair-pairs)
+(declare-function project-roots "project")
+
;;; Constants =================================================================
@@ -243,6 +245,22 @@ This is the default search behavior of Ikiwiki."
:safe 'booleanp
:package-version '(markdown-mode . "2.2"))
+(defcustom markdown-wiki-link-search-type nil
+ "Searching type for markdown wiki link.
+
+sub-directories: search for wiki link targets in sub directories
+parent-directories: search for wiki link targets in parent directories
+project: search for wiki link targets under project root"
+ :group 'markdown
+ :type '(set
+ (const :tag "search wiki link from subdirectories" sub-directories)
+ (const :tag "search wiki link from parent directories"
parent-directories)
+ (const :tag "search wiki link under project root" project))
+ :package-version '(markdown-mode . "2.5"))
+
+(make-obsolete-variable 'markdown-wiki-link-search-subdirectories
'markdown-wiki-link-search-type "2.5")
+(make-obsolete-variable 'markdown-wiki-link-search-parent-directories
'markdown-wiki-link-search-type "2.5")
+
(defcustom markdown-wiki-link-fontify-missing nil
"When non-nil, change wiki link face according to existence of target files.
This is expensive because it requires checking for the file each time the
buffer
@@ -7830,6 +7848,17 @@ The location of the alias component depends on the value
of
(match-string-no-properties 3)
(or (match-string-no-properties 5) (match-string-no-properties 3))))
+(defun markdown--wiki-link-search-types ()
+ (let ((ret (and markdown-wiki-link-search-type
+ (cl-copy-list markdown-wiki-link-search-type))))
+ (when (and markdown-wiki-link-search-subdirectories
+ (not (memq 'sub-directories markdown-wiki-link-search-type)))
+ (push 'sub-directories ret))
+ (when (and markdown-wiki-link-search-parent-directories
+ (not (memq 'parent-directories markdown-wiki-link-search-type)))
+ (push 'parent-directories ret))
+ ret))
+
(defun markdown-convert-wiki-link-to-filename (name)
"Generate a filename from the wiki link NAME.
Spaces in NAME are replaced with `markdown-link-space-sub-char'.
@@ -7847,6 +7876,7 @@ in parent directories if
(concat (upcase (substring basename 0 1))
(downcase (substring basename 1 nil)))
basename))
+ (search-types (markdown--wiki-link-search-types))
directory extension default candidates dir)
(when buffer-file-name
(setq directory (file-name-directory buffer-file-name)
@@ -7859,15 +7889,22 @@ in parent directories if
(file-exists-p default))
default)
;; Possibly search in subdirectories, next.
- ((and markdown-wiki-link-search-subdirectories
+ ((and (memq 'sub-directories search-types)
(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
+ ((and (memq 'parent-directories search-types)
(setq dir (locate-dominating-file directory default)))
(concat dir default))
+ ((and (memq 'project search-types)
+ (progn
+ (require 'project)
+ (let ((roots (project-roots (project-current t))))
+ (setq candidates
+ (directory-files-recursively (car roots) (concat "^"
default "$"))))))
+ (car candidates))
;; If nothing is found, return default in current directory.
(t default)))))
diff --git a/tests/markdown-test.el b/tests/markdown-test.el
index 6487c2f..e32ebb6 100644
--- a/tests/markdown-test.el
+++ b/tests/markdown-test.el
@@ -6365,10 +6365,29 @@ Detail:
https://github.com/jrblevin/markdown-mode/pull/590"
(markdown-mode)
(re-search-forward "Zettel Markdown")
(goto-char (match-beginning 0))
- (should (markdown-wiki-link-p))
+ (should (markdown-wiki-link-p)) ;; create match-data
(should (string= (markdown-wiki-link-link) "Zettel Markdown")))
(kill-buffer)))))
+(ert-deftest test-markdown-ext/wiki-link-search-under-project ()
+ "Test that searching link under project root."
+ (let ((markdown-enable-wiki-links t)
+ (markdown-link-space-sub-char " ")
+ (markdown-wiki-link-search-type '(project))
+ (expected (concat (expand-file-name default-directory)
+ "wiki/pr590/Guide/Zettel Markdown/math.md")))
+ (progn
+ (find-file "wiki/pr590/Guide/Plugin/Link.md")
+ (unwind-protect
+ (progn
+ (markdown-mode)
+ (re-search-forward "math")
+ (goto-char (match-beginning 0))
+ (markdown-wiki-link-p) ;; create match-data
+ (let ((link (markdown-convert-wiki-link-to-filename
(markdown-wiki-link-link))))
+ (should (string= (expand-file-name link) expected))))
+ (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/Plugin/Link.md
b/tests/wiki/pr590/Guide/Plugin/Link.md
new file mode 100644
index 0000000..85a72a1
--- /dev/null
+++ b/tests/wiki/pr590/Guide/Plugin/Link.md
@@ -0,0 +1 @@
+[[math]]