Variable: shell-highlight-undef-mode

shell-highlight-undef-mode is a buffer-local variable defined in shell.el.gz.

Documentation

Non-nil if Shell-Highlight-Undef mode is enabled.

Use the command shell-highlight-undef-mode(var)/shell-highlight-undef-mode(fun) to change this variable.

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))