Skip to content

Instantly share code, notes, and snippets.

@matthewpersico
Created April 6, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewpersico/40a8de316bfc05a02582bf71fdd64dd1 to your computer and use it in GitHub Desktop.
Save matthewpersico/40a8de316bfc05a02582bf71fdd64dd1 to your computer and use it in GitHub Desktop.
Adding colon as a valid function character
;;; sh-script-imode.el --- imenu does not recognize some legal bash constructs.
;;; Commentary:
;;; Dashes in function names. 2016-02-23
;;; Colons in function names. 2020-04-06
;;; Code:
;;; First, make sure to load shell mode.
(require 'sh-script)
;;; Now, redefine the function checker, ripped out of sh-script.el. We simply added '-:'
;;; next to each '_' in the regexps.
(defun sh-current-defun-name ()
"Advice for sh-current-defun-name, adding - and : as legal function name chars."
(save-excursion
(end-of-line)
(when (re-search-backward
(concat "\\(?:"
;; function FOO
;; function FOO()
"^\\s-*function\\s-+\\\([[:alpha:]_:-][[:alnum:]_:-]*\\)\\s-*\\(?:()\\)?"
"\\)\\|\\(?:"
;; FOO()
"^\\s-*\\([[:alpha:]_:-][[:alnum:]_:-]*\\)\\s-*()"
"\\)\\|\\(?:"
;; FOO=
"^\\([[:alpha:]_:-][[:alnum:]_:-]*\\)="
"\\)")
nil t)
(or (match-string-no-properties 1)
(match-string-no-properties 2)
(match-string-no-properties 3))))
)
;;; Also, we custom defined sh-imenu-generic-expression by adding the '-' where
;;; needed. See below.
(setq sh-imenu-generic-expression
(quote
((sh
(nil "^\\s-*function\\s-+\\([[:alpha:]_:-][[:alnum:]_:-]*\\)\\s-*\\(?:()\\)?" 1)
(nil "^\\s-*\\([[:alpha:]_:-][[:alnum:]_:-]*\\)\\s-*()" 1)))))
(provide 'sh-script-imode)
;;; sh-script-imode.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment