lang/sh: improve fontification for sh-mode

+ Fontify variables in double quotes (better)
+ Fontify command substitution in double quotes
+ Fontify built-in/common commands (see `+sh-builtin-keywords`)
This commit is contained in:
Henrik Lissner 2017-06-20 15:34:10 +02:00
parent 9252c54576
commit 02bfb73264
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 46 additions and 33 deletions

View file

@ -120,6 +120,8 @@
+ =lang/org= + =lang/org=
+ Fix M-RET in plain lists not preserving indent level for new items. + Fix M-RET in plain lists not preserving indent level for new items.
+ Fix cursor jumping away when toggling folds or realigning org tables (pressing TAB). + Fix cursor jumping away when toggling folds or realigning org tables (pressing TAB).
+ =lang/sh=
+ Fix fontification of command substitutions in double-quoted strings to help distinguish them from the rest of the string literal.
* 2.0.3 (Jun 11, 2017) * 2.0.3 (Jun 11, 2017)
+ *New modules* + *New modules*

View file

@ -1,41 +1,29 @@
;;; lang/sh/autoload.el -*- lexical-binding: t; -*- ;;; lang/sh/autoload.el -*- lexical-binding: t; -*-
(defvar sh-extra-font-lock--keywords
`((+sh--match-var-in-double-quoted-string
(2 font-lock-variable-name-face prepend))
(,(concat
"\\<"
(regexp-opt '("sudo" "echo" "ls" "sleep" "tee" "cd" "cat" "service"))
"\\>")
(0 'font-lock-builtin-face))))
;;;###autoload ;;;###autoload
(defun +sh--in-double-quoted-string-p () (defun +sh--match-variables-in-quotes (limit)
"Non-nil if point in inside a double-quoted string."
(eq (nth 3 (syntax-ppss)) ?\"))
;;;###autoload
(defun +sh--match-var-in-double-quoted-string (limit)
"Search for variables in double-quoted strings bounded by LIMIT." "Search for variables in double-quoted strings bounded by LIMIT."
(let (res) (with-syntax-table sh-mode-syntax-table
(while (let (res)
(and (setq res (while
(re-search-forward (and (setq res
"\\$\\({#?\\)?\\([[:alpha:]_][[:alnum:]_]*\\|[-#?@!]\\)" (re-search-forward
limit t)) "\\(\\$\\)\\({.+?}\\|\\<.+?\\>\\)"
(not (+sh--in-double-quoted-string-p)))) limit t))
res)) (not (eq (nth 3 (syntax-ppss)) ?\"))))
res)))
;;;###autoload ;;;###autoload
(defun +sh|extra-fontify () (defun +sh--match-command-subst-in-quotes (limit)
"Activate sh-extra-font-lock." "Search for variables in double-quoted strings bounded by LIMIT."
(interactive) (with-syntax-table sh-mode-syntax-table
(font-lock-add-keywords nil sh-extra-font-lock--keywords) (let (res)
(if (fboundp 'font-lock-flush) (while
(font-lock-flush) (and (setq res
(when font-lock-mode (re-search-forward "\\(\\$(.+?)\\|`.+?`\\)"
(with-no-warnings limit t))
(font-lock-fontify-buffer))))) (not (eq (nth 3 (syntax-ppss)) ?\"))))
res)))
(defvar sh-shell-file) (defvar sh-shell-file)
;;;###autoload ;;;###autoload

View file

@ -1,15 +1,38 @@
;;; lang/sh/config.el -*- lexical-binding: t; -*- ;;; lang/sh/config.el -*- lexical-binding: t; -*-
(defvar +sh-builtin-keywords
'("sudo" "echo" "ls" "sleep" "tee" "cd" "cat")
"A list of common shell commands and keywords to be fontified especially in
`sh-mode'.")
;;
;; Plugins
;;
(def-package! sh-script ; built-in (def-package! sh-script ; built-in
:mode ("\\.zsh$" . sh-mode) :mode ("\\.zsh$" . sh-mode)
:mode ("/bspwmrc$" . sh-mode) :mode ("/bspwmrc$" . sh-mode)
:init :init
(add-hook! sh-mode #'(flycheck-mode highlight-numbers-mode +sh|extra-fontify)) (add-hook! sh-mode #'(flycheck-mode highlight-numbers-mode))
:config :config
(set! :electric 'sh-mode :words '("else" "elif" "fi" "done" "then" "do" "esac" ";;")) (set! :electric 'sh-mode :words '("else" "elif" "fi" "done" "then" "do" "esac" ";;"))
(set! :repl 'sh-mode #'+sh/repl) (set! :repl 'sh-mode #'+sh/repl)
(setq sh-indent-after-continuation 'always) (setq sh-indent-after-continuation 'always)
;; 1. Fontifies variables in double quotes
;; 2. Fontify command substitution in double quotes
;; 3. Fontify built-in/common commands (see `+sh-builtin-keywords')
(font-lock-add-keywords
'sh-mode `((+sh--match-variables-in-quotes
(1 'default prepend)
(2 'font-lock-variable-name-face prepend))
(+sh--match-command-subst-in-quotes
(0 'sh-quoted-exec prepend))
(,(concat "^\\s-*" (regexp-opt +sh-builtin-keywords 'words))
(0 'font-lock-builtin-face t))))
;; sh-mode has file extensions checks for other shells, but not zsh, so... ;; sh-mode has file extensions checks for other shells, but not zsh, so...
(defun +sh|detect-zsh () (defun +sh|detect-zsh ()
(when (and buffer-file-name (string-match-p "\\.zsh\\'" buffer-file-name)) (when (and buffer-file-name (string-match-p "\\.zsh\\'" buffer-file-name))