branch: elpa/markdown-mode commit 9d2850ed58719778d3e76411242d112e54998fd3 Merge: d2d960bec1 f464a6fa68 Author: Shohei YOSHIDA <syo...@gmail.com> Commit: GitHub <nore...@github.com>
Merge pull request #861 from jayemar/jayemar/toggle-hiding-of-wiki-links Hide wiki markup as part of markdown-toggle-markup-hiding --- CHANGES.md | 2 + README.md | 42 +++++--- markdown-mode.el | 288 +++++++++++++++++++++++++++++++++---------------- tests/markdown-test.el | 86 +++++++-------- tests/wiki-links.text | 2 +- 5 files changed, 264 insertions(+), 156 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b4171603c5..38b24bfa5f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,12 +11,14 @@ * Bug fixes: - `markdown-export` should not output stderr content to output file + - Hide wikilink markup as part of `markdown-toggle-markup-hiding` [GH-847][] * Improvements: - Support drag and drop features on Windows and multiple files' drag and drop - Added cmark and cmark-gfm to the markdown command list. - Disable `imenu-submenus-on-top` by default [GH-882][] + [gh-847]: https://github.com/jrblevin/markdown-mode/issues/847 [gh-882]: https://github.com/jrblevin/markdown-mode/issues/882 [gh-891]: https://github.com/jrblevin/markdown-mode/issues/891 diff --git a/README.md b/README.md index 903a903d40..cfb62cf0fe 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ using `package.el`. First, configure `package.el` and the MELPA Stable repository by adding the following to your `.emacs`, `init.el`, or equivalent startup file: -```lisp +```emacs-lisp (require 'package) (add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/")) @@ -79,7 +79,7 @@ then you can automatically install and configure `markdown-mode` by adding a declaration such as this one to your init file (as an example; adjust settings as desired): -```lisp +```emacs-lisp (use-package markdown-mode :ensure t :mode ("README\\.md\\'" . gfm-mode) @@ -99,7 +99,7 @@ save the file where Emacs can find it (i.e., a directory in your `load-path`). You can then configure `markdown-mode` and `gfm-mode` to load automatically by adding the following to your init file: -```lisp +```emacs-lisp (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files" t) (add-to-list 'auto-mode-alist @@ -133,7 +133,7 @@ repository as above or install markdown-mode from If you clone the repository directly, then make sure that Emacs can find it by adding the following line to your startup file: -```lisp +```emacs-lisp (add-to-list 'load-path "/path/to/markdown-mode/repository") ``` @@ -350,7 +350,7 @@ prefix. The most commonly used commands are described below. preview window to appear at the bottom or right, you can customize `markdown-split-window-direction`. - ```lisp + ```emacs-lisp ;; Set custom markdown preview function (setq markdown-live-preview-window-function #'my-markdown-preview-function) @@ -952,18 +952,17 @@ customization screen. [Marked 2]: https://itunes.apple.com/us/app/marked-2/id890031187?mt=12&uo=4&at=11l5Vs&ct=mm -## Extensions - +## Wiki Links Customization Besides supporting the basic Markdown syntax, Markdown Mode also includes syntax highlighting for `[[Wiki Links]]`. This can be enabled by setting `markdown-enable-wiki-links` to a non-nil value. -Wiki links may be followed by pressing <kbd>C-c C-o</kbd> when the point -is at a wiki link. Use <kbd>M-p</kbd> and <kbd>M-n</kbd> to quickly jump to the -previous and next links (including links of other types). + Aliased or piped wiki links of the form `[[link text|PageName]]` -are also supported. Since some wikis reverse these components, set +are supported. Since some wikis reverse these components, set `markdown-wiki-link-alias-first` to nil to treat them as -`[[PageName|link text]]`. If `markdown-wiki-link-fontify-missing` +`[[PageName|link text]]`. + +If `markdown-wiki-link-fontify-missing` 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. @@ -974,18 +973,31 @@ This value type is a symbol list. Possible values are - `parent-directories` : search in parent directories - `project` : search under project root -[SmartyPants][] support is possible by customizing `markdown-command`. +## Extensions +### SmartyPants + +[SmartyPants][] is a free tool for easily translating plain ASCII punctuation +characters into "smart" typographic punctuation HTML entities. It can perform +the following transformations: +- straight quotes ( " and ' ) into “curly” quote HTML entities +- backticks-style quotes (``like this'') into “curly” quote HTML entities +- dashes (“--” and “---”) into en- and em-dash entities +- three consecutive dots (“...”) into an ellipsis entity + +SmartyPants support is possible by customizing `markdown-command`. If you install `SmartyPants.pl` at, say, `/usr/local/bin/smartypants`, then you can set `markdown-command` to `"markdown | smartypants"`. You can do this either by using <kbd>M-x customize-group markdown</kbd> or by placing the following in your `.emacs` file: -```lisp +```emacs-lisp (setq markdown-command "markdown | smartypants") ``` [SmartyPants]: http://daringfireball.net/projects/smartypants/ +### LaTeX Mathematical Expressions + Syntax highlighting for mathematical expressions written in LaTeX (only expressions denoted by `$..$`, `$$..$$`, or `\[..\]`) can be enabled by setting `markdown-enable-math` to a non-nil value, @@ -1068,7 +1080,7 @@ by `markdown-mode` and `gfm-mode` as described below. for line wrapping in buffers. You can do this with a `gfm-mode-hook` as follows: - ```lisp + ```emacs-lisp ;; Use visual-line-mode in gfm-mode (defun my-gfm-mode-hook () (visual-line-mode 1)) diff --git a/markdown-mode.el b/markdown-mode.el index 311050f44f..6987741a5b 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -263,11 +263,11 @@ This is the default search behavior of Ikiwiki." :package-version '(markdown-mode . "2.2")) (defcustom markdown-wiki-link-search-type nil - "Searching type for markdown wiki link. + "A list of options for searching for markdown wiki links. -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" +`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) @@ -278,6 +278,16 @@ project: search for wiki link targets under project root" (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") +(make-obsolete 'markdown-match-wiki-link nil "2.8") +(make-obsolete 'markdown-highlight-wiki-link nil "2.8") +(make-obsolete 'markdown-unfontify-region-wiki-links nil "2.8") +(make-obsolete 'markdown-fontify-region-wiki-links nil "2.8") +(make-obsolete 'markdown-extend-changed-region nil "2.8") +(make-obsolete 'markdown-check-change-for-wiki-link nil "2.8") +(make-obsolete 'markdown-check-change-for-wiki-link-after-change nil "2.8") +(make-obsolete 'markdown-fontify-buffer-wiki-links nil "2.8") +(make-obsolete 'markdown-setup-wiki-link-hooks nil "2.8") + (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 @@ -930,7 +940,6 @@ This matches typical bracketed [[WikiLinks]] as well as \\='aliased wiki links of the form [[PageName|link text]]. The meanings of the first and second components depend on the value of `markdown-wiki-link-alias-first'. - Group 1 matches the entire link. Group 2 matches the opening square brackets. Group 3 matches the first component of the wiki link. @@ -2007,12 +2016,14 @@ inline code fragments and code blocks." (defface markdown-link-face '((t (:inherit link))) - "Face for links." + "Face for link text, ie the alias portion of a link. +If a wiki link does not have an alias portion, use this face +for the entire link." :group 'markdown-faces) (defface markdown-missing-link-face '((t (:inherit font-lock-warning-face))) - "Face for missing links." + "Face for the link text if the link points to a missing file." :group 'markdown-faces) (defface markdown-reference-face @@ -2283,6 +2294,7 @@ Depending on your font, some reasonable choices are: (7 markdown-markup-properties))) (markdown-fontify-inline-links) (markdown-fontify-reference-links) + (markdown-fontify-wiki-links) (,markdown-regex-reference-definition . ((1 'markdown-markup-face) ; [ (2 'markdown-reference-face) ; label (3 'markdown-markup-face) ; ] @@ -2319,8 +2331,7 @@ Depending on your font, some reasonable choices are: (markdown-fontify-sub-superscripts) (markdown-match-inline-attributes . ((0 markdown-markup-properties prepend))) (markdown-match-leanpub-sections . ((0 markdown-markup-properties))) - (markdown-fontify-blockquotes) - (markdown-match-wiki-link . ((0 'markdown-link-face prepend)))) + (markdown-fontify-blockquotes)) "Syntax highlighting for Markdown files.") ;; Footnotes @@ -3422,7 +3433,8 @@ the buffer)." (markdown-code-block-at-pos begin)) (progn (goto-char (min (1+ begin) last)) (when (< (point) last) - (markdown-match-wiki-link last))) + (with-no-warnings + (markdown-match-wiki-link last)))) (set-match-data (list begin end)) t)))) @@ -5254,8 +5266,7 @@ Otherwise, do normal delete by repeating (back-to-indentation) (unless (looking-at-p "[ \t]*$") (setq mincol (min mincol (current-column)))) - (forward-line 1) - )) + (forward-line 1))) mincol)) (defun markdown-indent-region (beg end arg) @@ -5730,7 +5741,39 @@ Assumes match data is available for `markdown-regex-italic'." "Keymap for `gfm-mode'. See also `markdown-mode-map'.") - + +;;; Text Properties =========================================================== + +(defconst markdown--markup-props (list 'invisible 'markdown-markup + 'rear-nonsticky t + 'font-lock-multiline t)) + +(defconst markdown--title-props (list 'invisible 'markdown-markup + 'font-lock-multiline t)) + +(defun markdown--url-props () + (if markdown-mouse-follow-link + (list 'invisible 'markdown-markup + 'keymap markdown-mode-mouse-map + 'mouse-face 'markdown-highlight-face + 'font-lock-multiline t) + (list 'invisible 'markdown-markup + 'keymap markdown-mode-mouse-map + 'font-lock-multiline t))) + +(defun markdown--link-props (url &optional title) + "Return a property list for URL with optional TITLE." + (let ((echo-text (if title (concat title "\n" url) url))) + (if markdown-mouse-follow-link + (list 'keymap markdown-mode-mouse-map + 'mouse-face 'markdown-highlight-face + 'font-lock-multiline t + 'help-echo echo-text) + (list 'keymap markdown-mode-mouse-map + 'font-lock-multiline t + 'help-echo echo-text)))) + + ;;; Menu ====================================================================== (easy-menu-define markdown-mode-menu markdown-mode-map @@ -8208,39 +8251,20 @@ Translate filenames using `markdown-filename-translate-function'." (title-start (match-beginning 7)) (title-end (match-end 7)) (title (match-string-no-properties 7)) - ;; Markup part - (mp (list 'invisible 'markdown-markup - 'rear-nonsticky t - 'font-lock-multiline t)) - ;; Link part (without face) - (lp (list 'keymap markdown-mode-mouse-map - 'font-lock-multiline t - 'help-echo (if title (concat title "\n" url) url))) - ;; URL part - (up (list 'keymap markdown-mode-mouse-map - 'invisible 'markdown-markup - 'font-lock-multiline t)) - ;; URL composition character - (url-char (markdown--first-displayable markdown-url-compose-char)) - ;; Title part - (tp (list 'invisible 'markdown-markup - 'font-lock-multiline t))) - (when markdown-mouse-follow-link - (setq lp (append lp '(mouse-face 'markdown-highlight-face))) - (setq up (append up '(mouse-face 'markdown-highlight-face)))) + (url-char (markdown--first-displayable markdown-url-compose-char))) (dolist (g '(1 2 4 5 8)) (when (match-end g) - (add-text-properties (match-beginning g) (match-end g) mp) + (add-text-properties (match-beginning g) (match-end g) markdown--markup-props) (add-face-text-property (match-beginning g) (match-end g) 'markdown-markup-face))) ;; Preserve existing faces applied to link part (e.g., inline code) (when link-start - (add-text-properties link-start link-end lp) + (add-text-properties link-start link-end (markdown--link-props url title)) (add-face-text-property link-start link-end 'markdown-link-face)) (when url-start - (add-text-properties url-start url-end up) + (add-text-properties url-start url-end (markdown--url-props)) (add-face-text-property url-start url-end 'markdown-url-face)) (when title-start - (add-text-properties url-end title-end tp) + (add-text-properties url-end title-end markdown--title-props) (add-face-text-property url-end title-end 'markdown-link-title-face)) (when (and markdown-hide-urls url-start) (compose-region url-start (or title-end url-end) url-char)) @@ -8253,11 +8277,7 @@ Translate filenames using `markdown-filename-translate-function'." (link-end (match-end 3)) (ref-start (match-beginning 6)) (ref-end (match-end 6)) - ;; Markup part - (mp (list 'invisible 'markdown-markup - 'rear-nonsticky t - 'font-lock-multiline t)) - ;; Link part + ;; Link properties (lp (list 'keymap markdown-mode-mouse-map 'font-lock-multiline t 'help-echo (lambda (_ __ pos) @@ -8268,14 +8288,14 @@ Translate filenames using `markdown-filename-translate-function'." "Undefined reference")))))) ;; URL composition character (url-char (markdown--first-displayable markdown-url-compose-char)) - ;; Reference part + ;; Reference properties (rp (list 'invisible 'markdown-markup 'font-lock-multiline t))) (when markdown-mouse-follow-link (setq lp (append lp '(mouse-face markdown-highlight-face)))) (dolist (g '(1 2 4 5 8)) (when (match-end g) - (add-text-properties (match-beginning g) (match-end g) mp) + (add-text-properties (match-beginning g) (match-end g) markdown--markup-props) (add-face-text-property (match-beginning g) (match-end g) 'markdown-markup-face))) (when link-start (add-text-properties link-start link-end lp) @@ -8287,6 +8307,75 @@ Translate filenames using `markdown-filename-translate-function'." (compose-region ref-start ref-end url-char))) t))) +(defun markdown-fontify-wiki-links (last) + "Add text properties to next wiki link from point to LAST." + (when (and markdown-enable-wiki-links + (markdown-match-inline-generic markdown-regex-wiki-link last)) + (let* ((begin (match-beginning 1)) + (end (match-end 1)) + (beg2 (match-beginning 2)) + (end2 (match-end 2)) + (beg3 (match-beginning 3)) + (end3 (match-end 3)) + (beg4 (match-beginning 4)) + (end4 (match-end 4)) + (beg5 (match-beginning 5)) + (end5 (match-end 5)) + (beg6 (match-beginning 6)) + (end6 (match-end 6)) + (part1 (match-string-no-properties 3)) + (part2 (match-string-no-properties 5)) + (aliasp (string-equal (match-string-no-properties 4) "|")) + (file-name (markdown-convert-wiki-link-to-filename (markdown-wiki-link-link))) + (file-missing-p (not (file-exists-p file-name)))) + (if (or (markdown-in-comment-p begin) + (markdown-in-comment-p end) + (markdown-inline-code-at-pos-p begin) + (markdown-inline-code-at-pos-p end) + (markdown-code-block-at-pos begin)) + (progn (goto-char (min (1+ begin) last)) + (when (< (point) last) + (markdown-fontify-wiki-links last))) + ;; Add text properties for hiding markup + (progn + ;; Propertize opening and closing brackets + (add-text-properties beg2 end2 markdown--markup-props) + (add-face-text-property beg2 end2 'markdown-markup-face) + (add-text-properties beg6 end6 markdown--markup-props) + (add-face-text-property beg6 end6 'markdown-markup-face) + (if aliasp + (progn + ;; Propertize pipe separating URL from link text + (add-text-properties beg4 end4 markdown--markup-props) + (add-face-text-property beg4 end4 'markdown-markup-face) + (if markdown-wiki-link-alias-first + (progn + ;; Properties alias portion of link + (add-text-properties beg3 end3 (markdown--link-props part2)) + (add-face-text-property beg3 end3 'markdown-link-face) + (add-text-properties beg5 end5 (markdown--url-props)) + (add-text-properties beg5 end5 '(invisible markdown-markup)) + (add-face-text-property beg5 end5 'markdown-url-face) + (when (and file-missing-p markdown-wiki-link-fontify-missing) + (put-text-property beg3 end3 'face 'markdown-missing-link-face))) + (progn + ;; Properties URL portion of link + (add-text-properties beg3 end3 (markdown--url-props)) + (add-text-properties beg3 end3 '(invisible markdown-markup)) + (add-face-text-property beg3 end3 'markdown-url-face) + (add-text-properties beg5 end5 (markdown--link-props part1)) + (add-face-text-property beg5 end5 'markdown-link-face) + (when (and file-missing-p markdown-wiki-link-fontify-missing) + (put-text-property beg5 end5 'face 'markdown-missing-link-face))))) + (progn + ;; Properties link as link text + (add-text-properties beg3 end3 (markdown--link-props part1)) + (add-face-text-property beg3 end3 'markdown-link-face) + (when (and file-missing-p markdown-wiki-link-fontify-missing) + (put-text-property beg3 end3 'face 'markdown-missing-link-face))))) + (set-match-data (list begin end)) + t)))) + (defun markdown-fontify-angle-uris (last) "Add text properties to angle URIs from point to LAST." (when (markdown-match-angle-uris last) @@ -8294,19 +8383,14 @@ Translate filenames using `markdown-filename-translate-function'." (url-end (match-end 2))) (unless (or (markdown-in-inline-code-p url-start) (markdown-in-inline-code-p url-end)) - (let* (;; Markup part - (mp (list 'face 'markdown-markup-face - 'invisible 'markdown-markup - 'rear-nonsticky t - 'font-lock-multiline t)) - ;; URI part + (let* (;; URI part (up (list 'keymap markdown-mode-mouse-map 'face 'markdown-plain-url-face 'font-lock-multiline t))) (when markdown-mouse-follow-link (setq up (append up '(mouse-face markdown-highlight-face)))) (dolist (g '(1 3)) - (add-text-properties (match-beginning g) (match-end g) mp)) + (add-text-properties (match-beginning g) (match-end g) markdown--markup-props)) (add-text-properties url-start url-end up) t))))) @@ -8372,6 +8456,11 @@ The location of the alias component depends on the value of (or (match-string-no-properties 5) (match-string-no-properties 3)))) (defun markdown--wiki-link-search-types () + "Return a list of the currently selected search types. + +Due to deprecated variables, as of markdown-mode version 2.5, the return value +of this function is the same as the value of the variable +`markdown-wiki-link-search-type'." (let ((ret (and markdown-wiki-link-search-type (cl-copy-list markdown-wiki-link-search-type)))) (when (and markdown-wiki-link-search-subdirectories @@ -8383,6 +8472,7 @@ The location of the alias component depends on the value of ret)) (defun markdown--project-root () + "Try various approaches to find the project root." (or (cl-loop for dir in '(".git" ".hg" ".svn") when (locate-dominating-file default-directory dir) return it) @@ -8397,12 +8487,9 @@ The location of the alias component depends on the value of (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'. +Search depth is determined by `markdown-wiki-link-search-type'. When in `gfm-mode', follow GitHub's conventions where [[Test Test]] -and [[test test]] both map to Test-test.ext. Look in the current -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." +and [[test test]] both map to Test-test.ext." (save-match-data ;; This function must not overwrite match data(PR #590) (let* ((basename (replace-regexp-in-string @@ -8493,11 +8580,12 @@ and highlight accordingly." (file-name (markdown-convert-wiki-link-to-filename (markdown-wiki-link-link)))) - (if (condition-case nil (file-exists-p file-name) (error nil)) + (with-no-warnings + (if (condition-case nil (file-exists-p file-name) (error nil)) + (markdown-highlight-wiki-link + highlight-beginning highlight-end 'markdown-link-face) (markdown-highlight-wiki-link - highlight-beginning highlight-end 'markdown-link-face) - (markdown-highlight-wiki-link - highlight-beginning highlight-end 'markdown-missing-link-face))))))) + highlight-beginning highlight-end 'markdown-missing-link-face)))))))) (defun markdown-extend-changed-region (from to) "Extend region given by FROM and TO so that we can fontify all links. @@ -8533,7 +8621,8 @@ newline after." ;; Extend the region to fontify so that it starts ;; and ends at safe places. (cl-multiple-value-bind (new-from new-to) - (markdown-extend-changed-region from to) + (with-no-warnings + (markdown-extend-changed-region from to)) (goto-char new-from) ;; Only refontify when the range contains text with a ;; wiki link face or if the wiki link regexp matches. @@ -8542,10 +8631,11 @@ newline after." '(markdown-link-face markdown-missing-link-face)) (re-search-forward markdown-regex-wiki-link new-to t)) - ;; Unfontify existing fontification (start from scratch) - (markdown-unfontify-region-wiki-links new-from new-to) - ;; Now do the fontification. - (markdown-fontify-region-wiki-links new-from new-to)))))) + (with-no-warnings + ;; Unfontify existing fontification (start from scratch) + (markdown-unfontify-region-wiki-links new-from new-to) + ;; Now do the fontification. + (markdown-fontify-region-wiki-links new-from new-to))))))) (cursor-intangible-mode -1) (and (not modified) (buffer-modified-p) @@ -8554,12 +8644,14 @@ newline after." (defun markdown-check-change-for-wiki-link-after-change (from to _) "Check region between FROM and TO for wiki links and re-fontify as needed. Designed to be used with the `after-change-functions' hook." - (markdown-check-change-for-wiki-link from to)) + (with-no-warnings + (markdown-check-change-for-wiki-link from to))) (defun markdown-fontify-buffer-wiki-links () "Refontify all wiki links in the buffer." (interactive) - (markdown-check-change-for-wiki-link (point-min) (point-max))) + (with-no-warnings + (markdown-check-change-for-wiki-link (point-min) (point-max)))) (defun markdown-toggle-wiki-links (&optional arg) "Toggle support for wiki links. @@ -8571,33 +8663,36 @@ and disable it otherwise." (not markdown-enable-wiki-links) (> (prefix-numeric-value arg) 0))) (when (called-interactively-p 'interactive) - (message "markdown-mode wiki link support %s" (if markdown-enable-wiki-links "enabled" "disabled"))) + (message "markdown-mode wiki link support %s" + (if markdown-enable-wiki-links "enabled" "disabled"))) (markdown-reload-extensions)) (defun markdown-setup-wiki-link-hooks () "Add or remove hooks for fontifying wiki links. These are only enabled when `markdown-wiki-link-fontify-missing' is non-nil." ;; Anytime text changes make sure it gets fontified correctly - (if (and markdown-enable-wiki-links - markdown-wiki-link-fontify-missing) - (add-hook 'after-change-functions - #'markdown-check-change-for-wiki-link-after-change t t) - (remove-hook 'after-change-functions - #'markdown-check-change-for-wiki-link-after-change t)) - ;; If we left the buffer there is a really good chance we were - ;; creating one of the wiki link documents. Make sure we get - ;; refontified when we come back. - (if (and markdown-enable-wiki-links - markdown-wiki-link-fontify-missing) - (progn - (add-hook 'window-configuration-change-hook - #'markdown-fontify-buffer-wiki-links t t) - (markdown-fontify-buffer-wiki-links)) - (remove-hook 'window-configuration-change-hook - #'markdown-fontify-buffer-wiki-links t) - (markdown-unfontify-region-wiki-links (point-min) (point-max)))) + (with-no-warnings + (if (and markdown-enable-wiki-links + markdown-wiki-link-fontify-missing) + (add-hook 'after-change-functions + #'markdown-check-change-for-wiki-link-after-change t t) + (remove-hook 'after-change-functions + #'markdown-check-change-for-wiki-link-after-change t)) + ;; If we left the buffer there is a really good chance we were + ;; creating one of the wiki link documents. Make sure we get + ;; refontified when we come back. + (if (and markdown-enable-wiki-links + markdown-wiki-link-fontify-missing) + (progn + (add-hook 'window-configuration-change-hook + #'markdown-fontify-buffer-wiki-links t t) + (markdown-fontify-buffer-wiki-links)) + (remove-hook 'window-configuration-change-hook + #'markdown-fontify-buffer-wiki-links t) + (with-no-warnings + (markdown-unfontify-region-wiki-links (point-min) (point-max))))) ) + - ;;; Following & Doing ========================================================= (defun markdown-follow-thing-at-point (arg) @@ -8784,9 +8879,7 @@ or span." (interactive) (when (derived-mode-p 'markdown-mode) ;; Refontify buffer - (font-lock-flush) - ;; Add or remove hooks related to extensions - (markdown-setup-wiki-link-hooks))) + (font-lock-flush))) (defun markdown-handle-local-variables () "Run in `hack-local-variables-hook' to update font lock rules. @@ -10090,6 +10183,19 @@ rows and columns and the column alignment." (propertize link 'face 'markdown-reference-face) (propertize "]" 'face 'markdown-markup-face)) (propertize link 'face 'markdown-url-face))))) + ;; Hidden URL for wiki links + ((and (and markdown-enable-wiki-links + (thing-at-point-looking-at markdown-regex-wiki-link)) + (or markdown-hide-urls markdown-hide-markup)) + (let* ((aliasp (string-equal (match-string-no-properties 4) "|")) + (part1 (match-string-no-properties 3)) + (part2 (match-string-no-properties 5)) + (link (if (and aliasp markdown-wiki-link-alias-first) part2 part1)) + (edit-keys (markdown--substitute-command-keys + "\\[markdown-insert-wiki-link]")) + (edit-str (propertize edit-keys 'face 'font-lock-constant-face))) + (format "Hidden URL (%s to edit): %s" + edit-str (propertize link 'face 'markdown-reference-face)))) ;; Hidden language name for fenced code blocks ((and (markdown-code-block-at-point-p) (not (get-text-property (point) 'markdown-pre)) @@ -10224,8 +10330,6 @@ rows and columns and the column alignment." (if markdown-hide-markup (add-to-invisibility-spec 'markdown-markup) (remove-from-invisibility-spec 'markdown-markup)) - ;; Wiki links - (markdown-setup-wiki-link-hooks) ;; Math mode (when markdown-enable-math (markdown-toggle-math t)) ;; Add a buffer-local hook to reload after file-local variables are read diff --git a/tests/markdown-test.el b/tests/markdown-test.el index 999b76268a..79b8831552 100644 --- a/tests/markdown-test.el +++ b/tests/markdown-test.el @@ -151,7 +151,7 @@ This file is not saved." (unless (eq vals value) (throw 'fail loc)))))) (when fail-loc - (message "Testing range (%d,%d) for property %s equal to %s." + (message "Testing that range (%d,%d) for property %s includes %s." begin end prop value) (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc) (markdown-test-report-property-range begin end prop)) @@ -162,12 +162,12 @@ This file is not saved." (let ((fail-loc (catch 'fail (dolist (loc (number-sequence begin end)) - (unless (eq (get-char-property loc prop) value) + (unless (equal (get-char-property loc prop) value) (throw 'fail loc)))))) (when fail-loc - (message "Testing range (%d,%d) for property %s equal to %s." + (message "Testing that range (%d,%d) for property %s equal to %s." begin end prop value) - (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc) + (message "Expected value (%s) does not equal property (%s) at location %d" value prop fail-loc) (markdown-test-report-property-range begin end prop)) (should-not fail-loc))) @@ -5766,39 +5766,24 @@ http://example.com \"title\" ) (ert-deftest test-markdown-wiki-link/font-lock () "Test font lock faces for wiki links." - ;; If `temporary-file-directory' contains an inaccessible - ;; subdirectory, `markdown-fontify-buffer-wiki-links' fails because - ;; it calls `directory-files-recursively' on the directory, which - ;; fails because of - ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=28567>. To fix - ;; this, we run the entire test in a new subdirectory of - ;; `temporary-file-directory', which is guaranteed to not contain - ;; any inaccessible directories. - (let ((temporary-file-directory - (file-name-as-directory (make-temp-file "markdown-test" :dir-flag)))) - (markdown-test-temp-file "wiki-links.text" - (let* ((fn (concat (file-name-directory buffer-file-name) - "inline.text")) - (markdown-enable-wiki-links t)) - ;; Create inline.text in the same temp directory, refontify - (write-region "" nil fn nil 1) - (markdown-fontify-buffer-wiki-links) - ;; Confirm location of first wiki link - (should (eq (markdown-next-link) 8)) - ;; First wiki link doesn't have a corresponding file - (markdown-test-range-has-property 8 20 'font-lock-face 'markdown-missing-link-face) - ;; Second wiki link doesn't have a corresponding file - (should (eq (markdown-next-link) 73)) - (markdown-test-range-has-property 73 88 'font-lock-face 'markdown-missing-link-face) - ;; Move to third wiki link, and create the missing file - (should (eq (markdown-next-link) 155)) - (should (string-equal (markdown-wiki-link-link) "inline")) - (markdown-test-range-has-property 155 164 'font-lock-face 'markdown-link-face) - ;; Check wiki links in code blocks - (markdown-test-range-has-face 360 395 'markdown-pre-face) - ;; Remove temporary files - (delete-file fn))) - (delete-directory temporary-file-directory))) + (let ((markdown-wiki-link-alias-first nil) + (markdown-wiki-link-search-type '(project)) + (markdown-wiki-link-fontify-missing t) + (markdown-enable-wiki-links t)) + (markdown-test-file "wiki-links.text" + ;; Confirm location of first wiki link + (should (eq (markdown-next-link) 8)) + ;; First wiki link doesn't have a corresponding file + (markdown-test-range-has-property 10 18 'face 'markdown-missing-link-face) + ;; Second wiki link doesn't have a corresponding file + (should (eq (markdown-next-link) 73)) + (markdown-test-range-has-property 81 86 'face 'markdown-missing-link-face) + ;; Third link DOES have a corresponding file + (should (eq (markdown-next-link) 155)) + (should (string-equal (markdown-wiki-link-link) "inline")) + (markdown-test-range-has-property 157 162 'face 'markdown-missing-link-face) + ;; Check wiki links in code blocks + (markdown-test-range-has-face 370 405 'markdown-code-face)))) (ert-deftest test-markdown-wiki-link/kill () "Simple tests for `markdown-kill-thing-at-point' for wiki links." @@ -7085,7 +7070,7 @@ x|" (markdown-indent-region (line-beginning-position) (line-end-position) nil) (should (string-equal (buffer-string) " #. abc\n def\n")))) -(ert-deftest test-markdown-ext/wiki-link-rules () +(ert-deftest test-markdown/wiki-link-rules () "Test wiki link search rules and font lock for missing pages." (let ((markdown-enable-wiki-links t) (markdown-wiki-link-fontify-missing t) @@ -7095,6 +7080,7 @@ x|" (unwind-protect (progn (markdown-mode) + (font-lock-ensure) ;; search rules (should (string-match-p "/sub/foo$" @@ -7103,16 +7089,20 @@ x|" (markdown-convert-wiki-link-to-filename "doesnotexist") "doesnotexist")) ;; font lock - (markdown-test-range-has-property 1 11 'font-lock-face 'markdown-link-face) - (markdown-test-range-has-property 14 33 'font-lock-face 'markdown-missing-link-face) - (markdown-test-range-has-property 36 42 'font-lock-face 'markdown-link-face) - (markdown-test-range-has-property 45 60 'font-lock-face 'markdown-missing-link-face)) + (markdown-test-range-has-property 1 2 'face 'markdown-markup-face) + (markdown-test-range-has-property 3 9 'face 'markdown-link-face) + (markdown-test-range-has-property 10 11 'face 'markdown-markup-face) + (markdown-test-range-has-property 16 31 'face 'markdown-missing-link-face) + (markdown-test-range-has-property 38 40 'face 'markdown-link-face) + (markdown-test-range-has-property 47 58 'face 'markdown-missing-link-face) + (markdown-test-range-has-property 65 74 'face 'markdown-link-face)) (kill-buffer))) (progn (find-file (expand-file-name "wiki/sub/foo" markdown-test-dir)) (unwind-protect (progn (markdown-mode) + (font-lock-ensure) ;; search rules (should (string-match-p "/wiki/root$" @@ -7121,11 +7111,11 @@ x|" (markdown-convert-wiki-link-to-filename "doesnotexist") "doesnotexist")) ;; font lock - (markdown-test-range-has-property 1 16 'font-lock-face 'markdown-missing-link-face) - (markdown-test-range-has-property 19 26 'font-lock-face 'markdown-link-face)) + (markdown-test-range-has-property 3 14 'face 'markdown-missing-link-face) + (markdown-test-range-has-property 21 24 'face 'markdown-link-face)) (kill-buffer))))) -(ert-deftest test-markdown-ext/wiki-link-keep-match-data () +(ert-deftest test-markdown/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) @@ -7142,7 +7132,7 @@ Detail: https://github.com/jrblevin/markdown-mode/pull/590" (should (string= (markdown-wiki-link-link) "Zettel Markdown"))) (kill-buffer))))) -(ert-deftest test-markdown-ext/wiki-link-search-under-project () +(ert-deftest test-markdown/wiki-link-search-under-project () "Test that searching link under project root." (let ((markdown-enable-wiki-links t) (markdown-link-space-sub-char " ") @@ -7180,7 +7170,7 @@ Detail: https://github.com/jrblevin/markdown-mode/pull/590" (should (string= "FOOBAR.md" (file-name-nondirectory link-file-name))))) (kill-buffer)))) -(ert-deftest test-markdown-ext/wiki-link-major-mode () +(ert-deftest test-markdown/wiki-link-major-mode () "Test major-mode of linked page." (let ((markdown-enable-wiki-links t) (auto-mode-alist (cons '("bar\\.md" . gfm-mode) auto-mode-alist))) @@ -7193,7 +7183,7 @@ Detail: https://github.com/jrblevin/markdown-mode/pull/590" (should (eq major-mode 'gfm-mode))) (kill-buffer)))) -(ert-deftest test-markdown-ext/wiki-link-nonexistent-file () +(ert-deftest test-markdown/wiki-link-nonexistent-file () "Test following wiki link to nonexistent file visits the buffer." (let ((markdown-enable-wiki-links t)) (find-file (expand-file-name "wiki/foo.md" markdown-test-dir)) diff --git a/tests/wiki-links.text b/tests/wiki-links.text index 6c33349ba3..d5473c617c 100644 --- a/tests/wiki-links.text +++ b/tests/wiki-links.text @@ -9,7 +9,7 @@ point 155. Bracketed expressions in code blocks should not be matched as wiki links: -``` +```clojure { :user { :repositories [["clojars" {:sign-releases false}]]