Refactor :lang emacs-lisp
This commit is contained in:
parent
7ca20f158b
commit
ada4110730
2 changed files with 79 additions and 71 deletions
|
@ -22,50 +22,6 @@ to a pop up buffer."
|
||||||
(error (error-message-string e))))
|
(error (error-message-string e))))
|
||||||
(current-buffer)))
|
(current-buffer)))
|
||||||
|
|
||||||
(defvar +emacs-lisp--face nil)
|
|
||||||
;;;###autoload
|
|
||||||
(defun +emacs-lisp-highlight-vars-and-faces (end)
|
|
||||||
"Match defined variables and functions.
|
|
||||||
|
|
||||||
Functions are differentiated into special forms, built-in functions and
|
|
||||||
library/userland functions"
|
|
||||||
(catch 'matcher
|
|
||||||
(while (re-search-forward "\\(?:\\sw\\|\\s_\\)+" end t)
|
|
||||||
(let ((ppss (save-excursion (syntax-ppss))))
|
|
||||||
(cond ((nth 3 ppss) ; strings
|
|
||||||
(search-forward "\"" end t))
|
|
||||||
((nth 4 ppss) ; comments
|
|
||||||
(forward-line +1))
|
|
||||||
((let ((symbol (intern-soft (match-string-no-properties 0))))
|
|
||||||
(and (cond ((null symbol) nil)
|
|
||||||
((eq symbol t) nil)
|
|
||||||
((special-variable-p symbol)
|
|
||||||
(setq +emacs-lisp--face 'font-lock-variable-name-face))
|
|
||||||
((and (fboundp symbol)
|
|
||||||
(eq (char-before (match-beginning 0)) ?\()
|
|
||||||
(not (memq (char-before (1- (match-beginning 0)))
|
|
||||||
(list ?\' ?\`))))
|
|
||||||
(let ((unaliased (indirect-function symbol)))
|
|
||||||
(unless (or (macrop unaliased)
|
|
||||||
(special-form-p unaliased))
|
|
||||||
(let (unadvised)
|
|
||||||
(while (not (eq (setq unadvised (ad-get-orig-definition unaliased))
|
|
||||||
(setq unaliased (indirect-function unadvised)))))
|
|
||||||
unaliased)
|
|
||||||
(setq +emacs-lisp--face
|
|
||||||
(if (subrp unaliased)
|
|
||||||
'font-lock-constant-face
|
|
||||||
'font-lock-function-name-face))))))
|
|
||||||
(throw 'matcher t)))))))
|
|
||||||
nil))
|
|
||||||
|
|
||||||
;; `+emacs-lisp-highlight-vars-and-faces' is a potentially expensive function
|
|
||||||
;; and should be byte-compiled, no matter what, to ensure it runs as fast as
|
|
||||||
;; possible:
|
|
||||||
(unless (byte-code-function-p (symbol-function '+emacs-lisp-highlight-vars-and-faces))
|
|
||||||
(with-no-warnings
|
|
||||||
(byte-compile #'+emacs-lisp-highlight-vars-and-faces)))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;;; Handlers
|
;;; Handlers
|
||||||
|
@ -230,6 +186,14 @@ https://emacs.stackexchange.com/questions/10230/how-to-indent-keywords-aligned"
|
||||||
;;
|
;;
|
||||||
;;; Hooks
|
;;; Hooks
|
||||||
|
|
||||||
|
(autoload 'straight-register-file-modification "straight")
|
||||||
|
;;;###autoload
|
||||||
|
(defun +emacs-lisp-init-straight-maybe-h ()
|
||||||
|
"Make sure straight sees modifications to installed packages."
|
||||||
|
(when (file-in-directory-p (or buffer-file-name default-directory) doom-local-dir)
|
||||||
|
(add-hook 'after-save-hook #'straight-register-file-modification
|
||||||
|
nil 'local)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +emacs-lisp-extend-imenu-h ()
|
(defun +emacs-lisp-extend-imenu-h ()
|
||||||
"Improve imenu support in `emacs-lisp-mode', including recognition for Doom's API."
|
"Improve imenu support in `emacs-lisp-mode', including recognition for Doom's API."
|
||||||
|
@ -277,6 +241,10 @@ verbosity when editing a file in `doom-private-dir' or `doom-emacs-dir'."
|
||||||
(default-value 'flycheck-emacs-lisp-check-form)
|
(default-value 'flycheck-emacs-lisp-check-form)
|
||||||
")"))))
|
")"))))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;;; Fontification
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +emacs-lisp-truncate-pin ()
|
(defun +emacs-lisp-truncate-pin ()
|
||||||
"Truncates long SHA1 hashes in `package!' :pin's."
|
"Truncates long SHA1 hashes in `package!' :pin's."
|
||||||
|
@ -290,3 +258,47 @@ verbosity when editing a file in `doom-private-dir' or `doom-emacs-dir'."
|
||||||
(when (and start finish)
|
(when (and start finish)
|
||||||
(put-text-property start finish 'display "...")))))
|
(put-text-property start finish 'display "...")))))
|
||||||
nil)
|
nil)
|
||||||
|
|
||||||
|
(defvar +emacs-lisp--face nil)
|
||||||
|
;;;###autoload
|
||||||
|
(defun +emacs-lisp-highlight-vars-and-faces (end)
|
||||||
|
"Match defined variables and functions.
|
||||||
|
|
||||||
|
Functions are differentiated into special forms, built-in functions and
|
||||||
|
library/userland functions"
|
||||||
|
(catch 'matcher
|
||||||
|
(while (re-search-forward "\\(?:\\sw\\|\\s_\\)+" end t)
|
||||||
|
(let ((ppss (save-excursion (syntax-ppss))))
|
||||||
|
(cond ((nth 3 ppss) ; strings
|
||||||
|
(search-forward "\"" end t))
|
||||||
|
((nth 4 ppss) ; comments
|
||||||
|
(forward-line +1))
|
||||||
|
((let ((symbol (intern-soft (match-string-no-properties 0))))
|
||||||
|
(and (cond ((null symbol) nil)
|
||||||
|
((eq symbol t) nil)
|
||||||
|
((special-variable-p symbol)
|
||||||
|
(setq +emacs-lisp--face 'font-lock-variable-name-face))
|
||||||
|
((and (fboundp symbol)
|
||||||
|
(eq (char-before (match-beginning 0)) ?\()
|
||||||
|
(not (memq (char-before (1- (match-beginning 0)))
|
||||||
|
(list ?\' ?\`))))
|
||||||
|
(let ((unaliased (indirect-function symbol)))
|
||||||
|
(unless (or (macrop unaliased)
|
||||||
|
(special-form-p unaliased))
|
||||||
|
(let (unadvised)
|
||||||
|
(while (not (eq (setq unadvised (ad-get-orig-definition unaliased))
|
||||||
|
(setq unaliased (indirect-function unadvised)))))
|
||||||
|
unaliased)
|
||||||
|
(setq +emacs-lisp--face
|
||||||
|
(if (subrp unaliased)
|
||||||
|
'font-lock-constant-face
|
||||||
|
'font-lock-function-name-face))))))
|
||||||
|
(throw 'matcher t)))))))
|
||||||
|
nil))
|
||||||
|
|
||||||
|
;; HACK Fontification is already expensive enough. We byte-compile
|
||||||
|
;; `+emacs-lisp-highlight-vars-and-faces' and `+emacs-lisp-truncate-pin' to
|
||||||
|
;; ensure they run as fast as possible:
|
||||||
|
(dolist (fn '(+emacs-lisp-highlight-vars-and-faces +emacs-lisp-truncate-pin))
|
||||||
|
(unless (byte-code-function-p (symbol-function fn))
|
||||||
|
(with-no-warnings (byte-compile fn))))
|
||||||
|
|
|
@ -47,40 +47,39 @@ This marks a foldable marker for `outline-minor-mode' in elisp buffers.")
|
||||||
;; Fixed indenter that intends plists sensibly.
|
;; Fixed indenter that intends plists sensibly.
|
||||||
lisp-indent-function #'+emacs-lisp-indent-function)
|
lisp-indent-function #'+emacs-lisp-indent-function)
|
||||||
|
|
||||||
;; variable-width indentation is superior in elisp
|
;; variable-width indentation is superior in elisp. Otherwise, `dtrt-indent'
|
||||||
(add-to-list 'doom-detect-indentation-excluded-modes 'emacs-lisp-mode nil #'eq)
|
;; and `editorconfig' would force fixed indentation on elisp.
|
||||||
|
(add-to-list 'doom-detect-indentation-excluded-modes 'emacs-lisp-mode)
|
||||||
|
|
||||||
(add-hook! 'emacs-lisp-mode-hook
|
(add-hook! 'emacs-lisp-mode-hook
|
||||||
|
;; Allow folding of outlines in comments
|
||||||
#'outline-minor-mode
|
#'outline-minor-mode
|
||||||
;; fontificiation
|
;; Make parenthesis depth easier to distinguish at a glance
|
||||||
#'rainbow-delimiters-mode
|
#'rainbow-delimiters-mode
|
||||||
|
;; Make quoted symbols easier to distinguish from free variables
|
||||||
#'highlight-quoted-mode
|
#'highlight-quoted-mode
|
||||||
;; initialization
|
;; Extend imenu support to Doom constructs
|
||||||
#'+emacs-lisp-extend-imenu-h)
|
#'+emacs-lisp-extend-imenu-h
|
||||||
|
;; Ensure straight sees modifications to installed packages
|
||||||
(autoload 'straight-register-file-modification "straight")
|
#'+emacs-lisp-init-straight-maybe-h)
|
||||||
(add-hook! 'emacs-lisp-mode-hook
|
|
||||||
(defun +emacs-lisp-init-straight-h ()
|
|
||||||
(when (file-in-directory-p (or buffer-file-name default-directory) doom-local-dir)
|
|
||||||
(add-hook 'after-save-hook #'straight-register-file-modification
|
|
||||||
nil 'local))))
|
|
||||||
|
|
||||||
;; Flycheck's two emacs-lisp checkers produce a *lot* of false positives in
|
;; Flycheck's two emacs-lisp checkers produce a *lot* of false positives in
|
||||||
;; emacs configs, so we disable `emacs-lisp-checkdoc' and reduce the
|
;; emacs configs, so we disable `emacs-lisp-checkdoc' and reduce the
|
||||||
;; `emacs-lisp' checker's verbosity.
|
;; `emacs-lisp' checker's verbosity.
|
||||||
(add-hook 'flycheck-mode-hook #'+emacs-lisp-reduce-flycheck-errors-in-emacs-config-h)
|
(add-hook 'flycheck-mode-hook #'+emacs-lisp-reduce-flycheck-errors-in-emacs-config-h)
|
||||||
|
|
||||||
;; Special syntax highlighting for elisp...
|
;; Enhance elisp syntax highlighting, by highlighting Doom-specific
|
||||||
|
;; constructs, defined symbols, and truncating :pin's in `package!' calls.
|
||||||
(font-lock-add-keywords
|
(font-lock-add-keywords
|
||||||
'emacs-lisp-mode
|
'emacs-lisp-mode
|
||||||
(append `(;; custom Doom cookies
|
(append `(;; custom Doom cookies
|
||||||
("^;;;###\\(autodef\\|if\\|package\\)[ \n]" (1 font-lock-warning-face t)))
|
("^;;;###\\(autodef\\|if\\|package\\)[ \n]" (1 font-lock-warning-face t)))
|
||||||
|
;; Shorten the :pin of `package!' statements to 10 characters
|
||||||
|
`(("(package!\\_>" (0 (+emacs-lisp-truncate-pin))))
|
||||||
;; highlight defined, special variables & functions
|
;; highlight defined, special variables & functions
|
||||||
(when +emacs-lisp-enable-extra-fontification
|
(when +emacs-lisp-enable-extra-fontification
|
||||||
`((+emacs-lisp-highlight-vars-and-faces . +emacs-lisp--face)))
|
`((+emacs-lisp-highlight-vars-and-faces . +emacs-lisp--face)))))
|
||||||
|
|
||||||
`(("(package!\\_>" (0 (+emacs-lisp-truncate-pin))))))
|
|
||||||
|
|
||||||
;; Recenter window after following definition
|
;; Recenter window after following definition
|
||||||
(advice-add #'elisp-def :after #'doom-recenter-a)
|
(advice-add #'elisp-def :after #'doom-recenter-a)
|
||||||
|
|
||||||
|
@ -90,17 +89,13 @@ This marks a foldable marker for `outline-minor-mode' in elisp buffers.")
|
||||||
(when-let (ret (funcall orig-fn sym))
|
(when-let (ret (funcall orig-fn sym))
|
||||||
(concat ret " "
|
(concat ret " "
|
||||||
(let* ((truncated " [...]")
|
(let* ((truncated " [...]")
|
||||||
(limit (- (frame-width) (length ret) (length truncated) 1))
|
(print-escape-newlines t)
|
||||||
(str (symbol-value sym))
|
(str (symbol-value sym))
|
||||||
(str (prin1-to-string
|
(str (prin1-to-string str))
|
||||||
(if (stringp str)
|
(limit (- (frame-width) (length ret) (length truncated) 1)))
|
||||||
(replace-regexp-in-string "\n" "
" str)
|
(format (format "%%0.%ds%%s" limit)
|
||||||
str)))
|
(propertize str 'face 'warning)
|
||||||
(str-length (length str))
|
(if (< (length str) limit) "" truncated))))))
|
||||||
(short (< str-length limit)))
|
|
||||||
(concat (substring (propertize str 'face 'warning)
|
|
||||||
0 (if short str-length limit))
|
|
||||||
(unless short truncated))))))
|
|
||||||
|
|
||||||
(map! :localleader
|
(map! :localleader
|
||||||
:map emacs-lisp-mode-map
|
:map emacs-lisp-mode-map
|
||||||
|
@ -145,6 +140,7 @@ This marks a foldable marker for `outline-minor-mode' in elisp buffers.")
|
||||||
|
|
||||||
;;;###package overseer
|
;;;###package overseer
|
||||||
(autoload 'overseer-test "overseer" nil t)
|
(autoload 'overseer-test "overseer" nil t)
|
||||||
|
;; Properly lazy load overseer by not loading it so early:
|
||||||
(remove-hook 'emacs-lisp-mode-hook #'overseer-enable-mode)
|
(remove-hook 'emacs-lisp-mode-hook #'overseer-enable-mode)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue