Skip to content

Instantly share code, notes, and snippets.

@jamescherti
Last active April 7, 2025 13:03
Show Gist options
  • Save jamescherti/3dcfe5b67b69a331b09c3db66e82d5fc to your computer and use it in GitHub Desktop.
Save jamescherti/3dcfe5b67b69a331b09c3db66e82d5fc to your computer and use it in GitHub Desktop.
Emacs Bash scripts imenu: Support hyphens in function names
;;; add-hyphen-bash-imenu.el --- Bash scripts imenu: Support hyphens in function names -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/3dcfe5b67b69a331b09c3db66e82d5fc
;; License: MIT
;; Author: James Cherti
;;
;; This patch has been integrated into Emacs:
;; https://lists.gnu.org/archive/html/emacs-devel/2024-11/msg00684.html
(with-eval-after-load "sh-script"
;; By default, imenu does not include functions with hyphens in their names by
;; default. While it is true that sh does not allow hyphens in function names,
;; Bash does permit them. The following code snippet adds a hyphen '-' to the imenu
;; Bash script regular expression.
(let ((bash-entry
'((nil
;; function FOO / function FOO()
"^\\s-*function\\s-+\\([[:alpha:]_][[:alnum:]_-]*\\)\\s-*\\(?:()\\)?"
1)
;; FOO()
(nil
"^\\s-*\\([[:alpha:]_][[:alnum:]_-]*\\)\\s-*()"
1))))
(if-let ((entry (assoc 'bash sh-imenu-generic-expression)))
(setcdr entry bash-entry)
(push (cons 'bash bash-entry) sh-imenu-generic-expression))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment