Skip to content

Conversation

@syohex
Copy link
Collaborator

@syohex syohex commented Jan 27, 2021

Description

This is improved version of #590.

Related Issue

#590. CC: @srid

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have updated the documentation in the README.md file if necessary.
  • I have added an entry to CHANGES.md.
  • I have added tests to cover my changes.
  • All new and existing tests passed (using make test).

@srid
Copy link

srid commented Jan 27, 2021

@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:

git clone [email protected]:srid/neuron.git
cd neuron/doc/
emacs -nw Guide/Plugins/Linking.md
  1. Insert [[math]] in that markdown
  2. C-c C-o

Instead of opening ./doc/Guide/Zettel Markdown/math.md as one would expect, it creates a new file at ./doc/Guide/Plugins/math.md.

@syohex
Copy link
Collaborator Author

syohex commented Jan 27, 2021

I'll check later

@syohex
Copy link
Collaborator Author

syohex commented Jan 27, 2021

Would this be in scope for this PR, or is it a different issue?

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 ../foo/bar/baz.md, markdown-mode cannot find it. We need other changes to support that feature.

@srid
Copy link

srid commented Jan 27, 2021

If markdown-mode becomes project-aware (say, by looking at the project-root of project.el, which is built into emacs), then it can simply search recursively from (project-root), instead of searching from the base directory of the current buffer file.

Then we can replace both markdown-wiki-link-search-subdirectories and markdown-wiki-link-search-parent-directories with a single markdown-wiki-link-search-mode that takes one of the three values:

  • 'search-subdirectories
  • 'search-sub-and-parent-directories
  • 'search-project-root

What do you all think?

@syohex
Copy link
Collaborator Author

syohex commented Jan 28, 2021

Sounds good. I suppose value naming can be improved like subdirectories, sub-and-parent-directories, project (I suppose search- prefix is redundant)

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.

@syohex syohex merged commit 47f60b7 into master Jan 28, 2021
@syohex syohex deleted the syohex/590 branch January 28, 2021 00:41
@syohex
Copy link
Collaborator Author

syohex commented Feb 2, 2021

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
Copy link
Collaborator Author

syohex commented Feb 2, 2021

@srid How about #592 ?

Here is sample configuration of it.

(require 'markdown-mode)
(setq markdown-enable-wiki-links t)
(setq markdown-link-space-sub-char " ")
(setq markdown-wiki-link-search-type '(project))

@srid
Copy link

srid commented Feb 2, 2021

@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 markdown-mode to https://neuron.zettel.page/editor.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants