I do not know a shortcut for identifier regexps. Maybe, that is due to the complicated rules that exist for identifiers of some languages.
Nevertheless you can define your own format strings and convert them by format-spec
to regexps with identifiers.
The following lisp code defines a function my-re
for transforming extended regular expressions with %i
as regexp for identifiers to normal regular expressions.
Furthermore, it exemplarily defines my-re-search-forward
for searching with extended regexps.
format-spec
implies some constraints:
- if you do not want the identifier regexp at some place where
%i
occurs in the search string you have to replace it with %%i
- the regexp may not end with a single
%
if that is a really problem you can replace it with something like %\{1\}
(defconst my-identifier-re "\\(?:\\w\\|\\s_\\)\\(?:\\w\\|\\s_\\|[0-9]\\)+"
"Regular expression for identifiers.")
(defconst my-re-format-with-identifier
`((?i . ,my-identifier-re)
(?% . "%") ;; escape
;; self-insert all others:
,@(cl-loop for c from 0 upto 255
collect (cons c (string ?% c))))
"Format for format string with ")
(defun my-re (re)
"Transform extended REGEXP into regexp."
(format-spec re my-re-format-with-identifier))
(defun read-interactive (fun)
"Read argument list like `interactive' does."
(call-interactively `(lambda (&rest args)
,(interactive-form fun)
args)))
(defun my-re-search-forward (re &rest args)
"Imitate `re-search-forward' with RE and ARGS but add %i as identifier re."
(interactive (read-interactive 're-search-forward))
(apply #'re-search-forward
(my-re re)
args))
(defun my-occur (re &rest args)
"Run `occur' with `re-search-forward' replaced by `my-re-search-froward'."
(interactive (read-interactive 'occur))
(apply #'occur (my-re re) args))
Example: my-occur
input for a search for C-function declarations/headers.
It is not complete but covers quite many situations and there are only a few false positives.
^ \{0,3\}\(%i[&*]?[[:space:]
]+\)+%i[[:space:]
]*( *\(%i[&*]? *\)*\(, *\(%i[&*]? *\)+\)*)[[:space:]
]*\({\|;\)
\_<.*?\_>
for a non-greedy match between symbol-start and symbol-end positions._foo
, I'd try\_<.*?_foo\_>
. but this would match the whole linebaz_baz bar_bar foo_foo
.