Function: shell-highlight-undef-mode

shell-highlight-undef-mode is an interactive and byte-compiled function defined in shell.el.gz.

Signature

(shell-highlight-undef-mode &optional ARG)

Documentation

Highlight undefined shell commands and aliases.

This minor mode is mostly useful in shell-mode buffers and works better if comint-fontify-input-mode(var)/comint-fontify-input-mode(fun) is enabled.

This is a minor mode. If called interactively, toggle the Shell-Highlight-Undef mode mode. If the prefix argument is positive, enable the mode, and if it is zero or negative, disable the mode.

If called from Lisp, toggle the mode if ARG is toggle. Enable the mode if ARG is nil, omitted, or is a positive number. Disable the mode if ARG is a negative number.

To check whether the minor mode is enabled in the current buffer, evaluate the variable shell-highlight-undef-mode(var)/shell-highlight-undef-mode(fun).

The mode's hook is called both when the mode is enabled and when it is disabled.

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/shell.el.gz
(define-minor-mode shell-highlight-undef-mode
  "Highlight undefined shell commands and aliases.
This minor mode is mostly useful in `shell-mode' buffers and
works better if `comint-fontify-input-mode' is enabled."
  :init-value nil
  (if shell--highlight-undef-indirect
      (progn
        (remove-hook 'comint-indirect-setup-hook shell--highlight-undef-indirect t)
        (setq shell--highlight-undef-indirect nil)
        (when-let ((buf (comint-indirect-buffer t)))
          (with-current-buffer buf
            (font-lock-remove-keywords nil shell-highlight-undef-keywords))))
    (font-lock-remove-keywords nil shell-highlight-undef-keywords))
  (remove-hook 'comint-fontify-input-mode-hook
               #'shell-highlight-undef-mode-restart t)

  (when shell-highlight-undef-mode
    (when comint-use-prompt-regexp
      (setq shell-highlight-undef-mode nil)
      (error
       "`shell-highlight-undef-mode' is incompatible with `comint-use-prompt-regexp'"))

    (require 'sh-script)

    (let* ((regexp
            ;; Adapted from `sh-font-lock-keywords-1'
            (concat
             "\\("
             "[;(){}`|&]"
             (if comint-fontify-input-mode
                 ;; `comint-fontify-input-mode' already puts
                 ;; point-min on end of prompt
                 ""
               (concat "\\|" comint-prompt-regexp))
             "\\|^"
             "\\)"
             "[ \t]*\\(\\("
             (regexp-opt (sh-feature sh-leading-keywords) t)
             "[ \t]+\\)?"
             (regexp-opt (append (sh-feature sh-leading-keywords)
                                 (sh-feature sh-other-keywords))
                         t)
             "[ \t]+\\)?\\_<\\(\\(?:\\s_\\|\\sw\\|/\\)+\\)\\_>"))
           (setup
            (lambda ()
              (setq shell-highlight-undef-regexp regexp)
              (font-lock-add-keywords nil shell-highlight-undef-keywords t))))
      (cond (comint-fontify-input-mode
             (setq shell--highlight-undef-indirect setup)
             (if-let ((buf (comint-indirect-buffer t)))
                 (with-current-buffer buf
                   (funcall setup))
               (add-hook 'comint-indirect-setup-hook setup nil t)))
            (t (funcall setup))))

    (add-hook 'comint-fontify-input-mode-hook
              #'shell-highlight-undef-mode-restart nil t))

  (font-lock-flush))