Skip to content

Commit

Permalink
Only set font-lock-defaults once to prevent clobbering user keywords
Browse files Browse the repository at this point in the history
Closes GH-222.
  • Loading branch information
jrblevin committed Nov 7, 2017
1 parent a2cac4c commit 772d607
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@
([GH-276][])
- Don't set `markdown-code-face` background color at package
load time. ([GH-273][])
- Don't clobber user specified font-lock keywords when toggling
features. ([GH-222][])

[gh-171]: https://github.com/jrblevin/markdown-mode/issues/171
[gh-216]: https://github.com/jrblevin/markdown-mode/issues/216
[gh-222]: https://github.com/jrblevin/markdown-mode/issues/222
[gh-227]: https://github.com/jrblevin/markdown-mode/issues/227
[gh-229]: https://github.com/jrblevin/markdown-mode/pull/229
[gh-238]: https://github.com/jrblevin/markdown-mode/issues/238
Expand Down
31 changes: 8 additions & 23 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2913,11 +2913,6 @@ Depending on your font, some reasonable choices are:
(markdown-match-wiki-link . ((0 markdown-link-face prepend))))
"Syntax highlighting for Markdown files.")

(defvar markdown-mode-font-lock-keywords nil
"Default highlighting expressions for Markdown mode.
This variable is defined as a buffer-local variable for dynamic
extension support.")

;; Footnotes
(defvar markdown-footnote-counter 0
"Counter for footnote numbers.")
Expand Down Expand Up @@ -8649,20 +8644,9 @@ or span."
"Check settings, update font-lock keywords and hooks, and re-fontify buffer."
(interactive)
(when (member major-mode '(markdown-mode gfm-mode))
;; Update font lock keywords with extensions
(setq markdown-mode-font-lock-keywords
(append
(markdown-mode-font-lock-keywords-math)
markdown-mode-font-lock-keywords-basic))
;; Update font lock defaults
(setq font-lock-defaults
'(markdown-mode-font-lock-keywords
nil nil nil nil
(font-lock-syntactic-face-function . markdown-syntactic-face)))
;; Refontify buffer
(when (and font-lock-mode (fboundp 'font-lock-refresh-defaults))
(font-lock-refresh-defaults))

;; Add or remove hooks related to extensions
(markdown-setup-wiki-link-hooks)))

Expand Down Expand Up @@ -8754,10 +8738,6 @@ if ARG is omitted or nil."
(message "markdown-mode math support disabled"))
(markdown-reload-extensions))

(defun markdown-mode-font-lock-keywords-math ()
"Return math font lock keywords if support is enabled."
'())


;;; GFM Checkboxes ============================================================

Expand Down Expand Up @@ -9789,7 +9769,6 @@ spaces, or alternatively a TAB should be used as the separator."
#'markdown-font-lock-extend-region-function t t)
(setq-local syntax-propertize-function #'markdown-syntax-propertize)
;; Font lock.
(setq-local markdown-mode-font-lock-keywords nil)
(setq-local font-lock-defaults nil)
(setq-local font-lock-multiline t)
(setq-local font-lock-extra-managed-props
Expand All @@ -9798,8 +9777,14 @@ spaces, or alternatively a TAB should be used as the separator."
(if markdown-hide-markup
(add-to-invisibility-spec 'markdown-markup)
(remove-from-invisibility-spec 'markdown-markup))
;; Reload extensions
(markdown-reload-extensions)
(setq font-lock-defaults
'(markdown-mode-font-lock-keywords-basic
nil nil nil nil
(font-lock-syntactic-face-function . markdown-syntactic-face)))
;; 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
(add-hook 'hack-local-variables-hook #'markdown-handle-local-variables nil t)
;; For imenu support
Expand Down
19 changes: 19 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -4979,6 +4979,25 @@ This includes preserving whitespace after the pipe."
(should (member (car markdown-mode-font-lock-keywords-math)
(cadr font-lock-keywords))))))

(ert-deftest test-markdown-math/preserve-user-keywords ()
"Test preserving user-specified font-lock keywords."
(let ((user-keyword '("\\<\\(FIXME\\):" 1 font-lock-warning-face t)))
;; Add user font-lock keyword using `font-lock-add-keywords'.
(font-lock-add-keywords 'markdown-mode (list user-keyword))
;; Visit a file using `markdown-mode'.
(markdown-test-file "math.text"
;; User keyword should be present initially.
(should (member user-keyword (cadr font-lock-keywords)))
;; User keyword should persist after calling `markdown-reload-extensions'.
(markdown-reload-extensions)
(should (member user-keyword (cadr font-lock-keywords))))
;; Remove the user keyword using `font-lock-remove-keywords'.
(font-lock-remove-keywords 'markdown-mode (list user-keyword))
;; Visit a file using `markdown-mode'.
(markdown-test-file "inline.text"
;; User keyword should not be present after removal.
(should-not (member user-keyword (cadr font-lock-keywords))))))

(ert-deftest test-markdown-math/font-lock ()
"Test markdown math mode."
(let ((markdown-enable-math t))
Expand Down

0 comments on commit 772d607

Please sign in to comment.