sh-mode: highlight variables in strings
This commit is contained in:
parent
8652505d8e
commit
1bebba9853
1 changed files with 32 additions and 0 deletions
|
@ -4,6 +4,38 @@
|
|||
(define-repl! sh-mode narf-inf-shell)
|
||||
(add-hook! sh-mode 'flycheck-mode)
|
||||
|
||||
(defun sh-extra-font-lock--is-in-double-quoted-string ()
|
||||
"Non-nil if point in inside a double-quoted string."
|
||||
(let ((state (syntax-ppss)))
|
||||
(eq (nth 3 state) ?\")))
|
||||
|
||||
(defun sh-extra-font-lock--match-var-in-double-quoted-string (limit)
|
||||
"Search for variables in double-quoted strings bounded by LIMIT."
|
||||
(let (res)
|
||||
(while
|
||||
(and (setq res
|
||||
(re-search-forward
|
||||
"\\$\\({#?\\)?\\([[:alpha:]_][[:alnum:]_]*\\|[-#?@!]\\)"
|
||||
limit t))
|
||||
(not (sh-extra-font-lock--is-in-double-quoted-string))))
|
||||
res))
|
||||
|
||||
(defvar sh-extra-font-lock--keywords
|
||||
'((sh-extra-font-lock--match-var-in-double-quoted-string
|
||||
(2 font-lock-variable-name-face prepend))))
|
||||
|
||||
(defun sh-extra-font-lock-activate ()
|
||||
"Activate sh-extra-font-lock."
|
||||
(interactive)
|
||||
(font-lock-add-keywords nil sh-extra-font-lock--keywords)
|
||||
(if (fboundp 'font-lock-flush)
|
||||
(font-lock-flush)
|
||||
(when font-lock-mode
|
||||
(with-no-warnings
|
||||
(font-lock-fontify-buffer)))))
|
||||
|
||||
(add-hook 'sh-mode-hook 'sh-script-extra-font-lock-activate)
|
||||
|
||||
;; [pedantry intensifies]
|
||||
(defadvice sh-mode (after sh-mode-rename-modeline activate)
|
||||
(setq mode-name "sh"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue