-
Notifications
You must be signed in to change notification settings - Fork 173
Fix markdown-wiki-link-p overriding match data issue #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@syohex I confirm that your change works! However, both yours and mine doesn't work for the opposite case (of looking up a markdown file in a sibling directory of a parent). Would this be in scope for this PR, or is it a different issue? To reproduce:
Instead of opening |
|
I'll check later |
It is a different issue. Current markdown-mode searches only current directory, sub-directories and parent directories. If target file is in other than then such as |
|
If markdown-mode becomes project-aware (say, by looking at the project-root of Then we can replace both
What do you all think? |
|
Sounds good. I suppose value naming can be improved like However we should consider compatibility. I'm not sure we can use functions of project.el because some functions of project.el does not work non-Unix platform such as Windows. I'll think about it. |
|
Initial implementation for searching wiki links under project root. diff --git a/markdown-mode.el b/markdown-mode.el
index f9cbd0c..f6bfab4 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,18 @@ 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."
+ :group 'markdown
+ :type '(set
+ (const :tag "search wiki link from subdirectories" sub-dir)
+ (const :tag "search wiki link from parent directory" parent-dir)
+ (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 +7844,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-dir markdown-wiki-link-search-type)))
+ (push 'sub-dir ret))
+ (when (and markdown-wiki-link-search-parent-directories
+ (not (memq 'parent-dir markdown-wiki-link-search-type)))
+ (push 'parent-dir 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 +7872,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 +7885,22 @@ in parent directories if
(file-exists-p default))
default)
;; Possibly search in subdirectories, next.
- ((and markdown-wiki-link-search-subdirectories
+ ((and (memq 'sub-dir 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-dir 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))))) |
|
@syohex It works perfectly! Tested it on my private Zettelkasten -- which is really just a directory tree of .md files (about 600 of them) -- and markdown-mode can successfully follow links from anywhere to anywhere under the project. Once your PR gets merged, and released to elpa/melpa, I'd be happy to add |
Description
This is improved version of #590.
Related Issue
#590. CC: @srid
Type of Change
Checklist
make test).