From 1c962993e71a146e7f5e05eeb4e200077b1512b8 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Sun, 29 Oct 2017 19:32:05 +0100 Subject: [PATCH] Introduce user option to translate filenames when following links Fixes #268 --- CHANGES.md | 2 ++ README.md | 3 +++ markdown-mode.el | 24 ++++++++++++++++++++++-- tests/markdown-test.el | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 978358fa..c5d15f3d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,6 +24,8 @@ - Save the buffer before running `markdown-open-command` and run `markdown-open-command` asynchronously. Thanks to Dmitry Safronov for a patch. ([GH-248][]) + - New user option `markdown-translate-filename-function` to translate + filenames when following file links. * Bug fixes: diff --git a/README.md b/README.md index 01c8a798..4f3be4bf 100644 --- a/README.md +++ b/README.md @@ -843,6 +843,9 @@ provides an interface to all of the possible customizations: This is useful for compatibility with `org-mode`, which doesn't recognize the lowercase variant. + * `markdown-translate-filename-function` - A function to be used to + translate filenames in links. + Additionally, the faces used for syntax highlighting can be modified to your liking by issuing M-x customize-group RET markdown-faces or by using the "Markdown Faces" link at the bottom of the mode diff --git a/markdown-mode.el b/markdown-mode.el index c3ee7a7d..ec442686 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -859,6 +859,9 @@ ;; This is useful for compatibility with `org-mode', which doesn't ;; recognize the lowercase variant. ;; +;; * `markdown-translate-filename-function' - A function to be used to +;; translate filenames in links. +;; ;; Additionally, the faces used for syntax highlighting can be modified to ;; your liking by issuing `M-x customize-group RET markdown-faces` ;; or by using the "Markdown Faces" link at the bottom of the mode @@ -1502,6 +1505,21 @@ or from the menu Markdown > Links & Images menu." :package-version '(markdown-mode . "2.3")) (make-variable-buffer-local 'markdown-hide-urls) +(defcustom markdown-translate-filename-function #'identity + "Function to use to translate filenames when following links. +\\\\[markdown-follow-thing-at-point] and \\[markdown-follow-link-at-point] +call this function with the filename as only argument whenever +they encounter a filename (instead of a URL) to be visited and +use its return value instead of the filename in the link. For +example, if absolute filenames are actually relative to a server +root directory, you can set +`markdown-translate-filename-function' to a function that +prepends the root directory to the given filename." + :group 'markdown + :type 'function + :risky t + :package-version '(markdown-mode . "2.4")) + ;;; Regular Expressions ======================================================= @@ -8085,7 +8103,8 @@ returns nil." (defun markdown-follow-link-at-point () "Open the current non-wiki link. If the link is a complete URL, open in browser with `browse-url'. -Otherwise, open with `find-file' after stripping anchor and/or query string." +Otherwise, open with `find-file' after stripping anchor and/or query string. +Translate filenames using `markdown-filename-translate-function'." (interactive) (if (markdown-link-p) (let* ((url (markdown-link-url)) @@ -8101,7 +8120,8 @@ Otherwise, open with `find-file' after stripping anchor and/or query string." ;; Open full URLs in browser, files in Emacs (if full (browse-url url) - (when (and file (> (length file) 0)) (find-file file)))) + (when (and file (> (length file) 0)) + (find-file (funcall markdown-translate-filename-function file))))) (user-error "Point is not at a Markdown link or URL"))) (defun markdown-fontify-inline-links (last) diff --git a/tests/markdown-test.el b/tests/markdown-test.el index d619c028..fbdd04f6 100644 --- a/tests/markdown-test.el +++ b/tests/markdown-test.el @@ -4489,6 +4489,23 @@ like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75" (markdown-test-string "http://jblevins.org/projects/markdown-mode/" (should (equal (markdown-link-at-pos (point)) '(1 44 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil))))) +(ert-deftest test-markdown-link/follow-filename () + "Test that `markdown-follow-thing-at-pos' uses +`markdown-translate-filename-function' to translate filenames." + (markdown-test-string "[text](/foo/bar/baz)" + (cl-letf* ((visited-files ()) + ((symbol-function #'find-file) + (lambda (filename) + (push filename visited-files))) + (translated-files ()) + (markdown-translate-filename-function + (lambda (filename) + (push filename translated-files) + (format "/root%s.md" filename)))) + (markdown-follow-thing-at-point nil) + (should (equal translated-files '("/foo/bar/baz"))) + (should (equal visited-files '("/root/foo/bar/baz.md")))))) + ;;; Wiki link tests: (ert-deftest test-markdown-wiki-link/file-local-variables ()