emacs/lisp/progmodes/cc-menus.el

531 lines
17 KiB
EmacsLisp
Raw Normal View History

1997-07-10 07:54:06 +00:00
;;; cc-menus.el --- imenu support for CC Mode
;; Copyright (C) 1985, 1987, 1992-2020 Free Software Foundation, Inc.
1997-07-10 07:54:06 +00:00
2003-07-03 12:30:59 +00:00
;; Authors: 1998- Martin Stjernholm
;; 1992-1999 Barry A. Warsaw
2009-01-11 00:37:05 +00:00
;; 1987 Dave Detlefs
;; 1987 Stewart Clamen
1997-07-10 07:54:06 +00:00
;; 1985 Richard M. Stallman
1999-02-08 16:53:18 +00:00
;; Maintainer: bug-cc-mode@gnu.org
1997-07-10 07:54:06 +00:00
;; Created: 22-Apr-1997 (split from cc-mode.el)
;; Keywords: c languages
;; Package: cc-mode
1997-07-10 07:54:06 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1997-07-10 07:54:06 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1997-07-10 07:54:06 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
1997-07-10 07:54:06 +00:00
;;; Commentary:
;;; Code:
1999-02-08 16:53:18 +00:00
(eval-when-compile
1999-12-12 18:24:19 +00:00
(let ((load-path
2001-03-21 12:59:36 +00:00
(if (and (boundp 'byte-compile-dest-file)
(stringp byte-compile-dest-file))
(cons (file-name-directory byte-compile-dest-file) load-path)
1999-12-12 18:24:19 +00:00
load-path)))
2003-07-03 12:30:59 +00:00
(load "cc-bytecomp" nil t)))
(cc-require 'cc-defs)
1999-12-12 18:24:19 +00:00
2001-03-21 12:59:36 +00:00
;; The things referenced in imenu, which we don't require.
(cc-bytecomp-defvar imenu-case-fold-search)
(cc-bytecomp-defvar imenu-generic-expression)
2003-07-03 12:30:59 +00:00
(cc-bytecomp-defvar imenu-create-index-function)
(cc-bytecomp-defun c-literal-limits)
1999-02-08 16:53:18 +00:00
1997-07-10 07:54:06 +00:00
;; imenu integration
(defvar cc-imenu-c-prototype-macro-regexp nil
"RE matching macro names used to conditionally specify function prototypes.
For example:
#ifdef __STDC__
#define _P(x) x
#else
#define _P(x) /*nothing*/
#endif
int main _P( (int argc, char *argv[]) )
A sample value might look like: `\\(_P\\|_PROTO\\)'.")
;; *Warning for cc-mode developers*
;;
;; `cc-imenu-objc-generic-expression' elements depend on
;; `cc-imenu-c++-generic-expression'. So if you change this
;; expression, you need to change following variables,
;; `cc-imenu-objc-generic-expression-*-index',
;; too. `cc-imenu-objc-function' uses these *-index variables, in
;; order to know where the each regexp *group \\(foobar\\)* elements
;; are started.
;;
;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
;; being initialized.
;;
1997-07-10 07:54:06 +00:00
(defvar cc-imenu-c++-generic-expression
1999-12-12 18:24:19 +00:00
`(
;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
2011-11-19 18:29:42 -08:00
;; will be incorrectly recognized as function `new ()' because the regexps
;; work by backtracking from the end of the definition.
(nil
1999-12-12 18:24:19 +00:00
,(concat
"^\\<.*"
2003-07-03 12:30:59 +00:00
"[^" c-alnum "_:<>~]" ; match any non-identifier char
; (note: this can be `\n')
"\\("
2003-07-03 12:30:59 +00:00
"\\([" c-alnum "_:<>~]*::\\)?" ; match an operator
"operator\\>[ \t]*"
"\\(()\\|[^(]*\\)" ; special case for `()' operator
"\\)"
"[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
; require something other than
; a `;' after the (...) to
; avoid prototypes. Can't
; catch cases with () inside
; the parentheses surrounding
; the parameters. e.g.:
; `int foo(int a=bar()) {...}'
1999-12-12 18:24:19 +00:00
) 1)
;; Special case to match a line like `main() {}'
;; e.g. no return type, not even on the previous line.
(nil
1999-12-12 18:24:19 +00:00
,(concat
"^"
2003-07-03 12:30:59 +00:00
"\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
1998-02-17 07:10:49 +00:00
"[ \t]*(" ; see above, BUT
"[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
"[ \t]*[^ \t;(]" ; with an asterisk or parentheses
1999-12-12 18:24:19 +00:00
) 1)
;; General function name regexp
(nil
1999-12-12 18:24:19 +00:00
,(concat
"^\\<" ; line MUST start with word char
;; \n added to prevent overflow in regexp matcher.
;; https://lists.gnu.org/r/emacs-pretest-bug/2007-02/msg00021.html
"[^()\n]*" ; no parentheses before
2003-07-03 12:30:59 +00:00
"[^" c-alnum "_:<>~]" ; match any non-identifier char
"\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
"\\([ \t\n]\\|\\\\\n\\)*(" ; see above, BUT the arg list
"\\([ \t\n]\\|\\\\\n\\)*" ; must not start
"\\([^ \t\n(*]" ; with an asterisk or parentheses
"[^()]*\\(([^()]*)[^()]*\\)*" ; Maybe function pointer arguments
"\\)?)"
"\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]"
1999-12-12 18:24:19 +00:00
) 1)
;; Special case for definitions using phony prototype macros like:
;; `int main _PROTO( (int argc,char *argv[]) )'.
;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
;; Only supported in c-code, so no `:<>~' chars in function name!
1999-12-12 18:24:19 +00:00
,@(if cc-imenu-c-prototype-macro-regexp
`((nil
,(concat
"^\\<.*" ; line MUST start with word char
2003-07-03 12:30:59 +00:00
"[^" c-alnum "_]" ; match any non-identifier char
"\\([" c-alpha "_][" c-alnum "_]*\\)" ; match function name
"[ \t]*" ; whitespace before macro name
cc-imenu-c-prototype-macro-regexp
"[ \t]*(" ; ws followed by first paren.
"[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
1999-12-12 18:24:19 +00:00
) 1)))
;; Class definitions
("Class"
1999-12-12 18:24:19 +00:00
,(concat
"^" ; beginning of line is required
"\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
2001-03-21 12:59:36 +00:00
"\\(class\\|struct\\)[ \t]+"
1999-02-08 16:53:18 +00:00
"\\(" ; the string we want to get
2003-07-03 12:30:59 +00:00
"[" c-alnum "_]+" ; class name
2001-03-21 12:59:36 +00:00
"\\(<[^>]+>\\)?" ; possibly explicitly specialized
1999-02-08 16:53:18 +00:00
"\\)"
"\\([ \t\n]\\|\\\\\n\\)*[:{]"
2001-03-21 12:59:36 +00:00
) 3))
1997-07-10 07:54:06 +00:00
"Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
1997-07-10 07:54:06 +00:00
(defvar cc-imenu-c-generic-expression
cc-imenu-c++-generic-expression
"Imenu generic expression for C mode. See `imenu-generic-expression'.")
;; Auxiliary regexps for Java try to match their trailing whitespace where
;; appropriate, but _not_ starting whitespace.
(defconst cc-imenu-java-ellipsis-regexp
(concat
"\\.\\{3\\}"
"[ \t\n\r]*"))
(defun cc-imenu-java-build-type-args-regex (depth)
"Builds regexp for type arguments list with DEPTH allowed
nested angle brackets constructs."
(if (> depth 0)
(concat "<"
"[][.," c-alnum "_? \t\n\r]+"
(if (> depth 1)
"\\(")
(cc-imenu-java-build-type-args-regex (1- depth))
(if (> depth 1)
(concat "[][.," c-alnum "_? \t\n\r]*"
"\\)*"))
">")))
(defconst cc-imenu-java-type-spec-regexp
(concat
;; zero or more identifiers followed by a dot
"\\("
"[" c-alpha "_][" c-alnum "_]*\\."
"\\)*"
;; a single mandatory identifier without a dot
"[" c-alpha "_][" c-alnum "_]*"
;; then choice:
"\\("
;; (option 1) type arguments list which _may_ be followed with brackets
;; and/or spaces, then optional variable arity
"[ \t\n\r]*"
(cc-imenu-java-build-type-args-regex 3)
"[][ \t\n\r]*"
"\\(" cc-imenu-java-ellipsis-regexp "\\)?"
"\\|"
;; (option 2) just brackets and/or spaces (there should be at least one),
;; then optional variable arity
"[][ \t\n\r]+"
"\\(" cc-imenu-java-ellipsis-regexp "\\)?"
"\\|"
;; (option 3) just variable arity
cc-imenu-java-ellipsis-regexp
"\\)"))
(defconst cc-imenu-java-comment-regexp
(concat
"/"
"\\("
;; a traditional comment
"\\*"
"\\("
"[^*]"
"\\|"
"\\*+[^/*]"
"\\)*"
"\\*+/"
"\\|"
;; an end-of-line comment
"/[^\n\r]*[\n\r]"
"\\)"
"[ \t\n\r]*"
))
;; Comments are allowed before the argument, after any of the
;; modifiers and after the identifier.
(defconst cc-imenu-java-method-arg-regexp
(concat
"\\(" cc-imenu-java-comment-regexp "\\)*"
;; optional modifiers
"\\("
;; a modifier is either an annotation or "final"
"\\("
"@[" c-alpha "_]"
"[" c-alnum "._]*"
;; TODO support element-value pairs!
"\\|"
"final"
"\\)"
;; a modifier ends with comments and/or ws
"\\("
"\\(" cc-imenu-java-comment-regexp "\\)+"
"\\|"
"[ \t\n\r]+"
"\\(" cc-imenu-java-comment-regexp "\\)*"
"\\)"
"\\)*"
;; type spec
cc-imenu-java-type-spec-regexp
;; identifier
"[" c-alpha "_]"
"[" c-alnum "_]*"
;; optional comments and/or ws
"[ \t\n\r]*"
"\\(" cc-imenu-java-comment-regexp "\\)*"
))
(defconst cc-imenu-java-generic-expression
1999-12-12 18:24:19 +00:00
`((nil
,(concat
cc-imenu-java-type-spec-regexp
"\\(" ; method name which gets captured
; into index
"[" c-alpha "_]"
Merge some of the differences from the standalone CC-mode. The main change is to only use the `category' text-property only when available. For that many calls are changed to use c-get-char-property, c-next-single-property-change, c-sc-scan-lists, c-sc-parse-partial-sexp, c-unmark-<->-as-paren. * lisp/progmodes/cc-align.el (c-lineup-respect-col-0): New function. * lisp/progmodes/cc-cmds.el (c-forward-into-nomenclature) (c-backward-into-nomenclature): Use cc-subword if subword-mode is not available. (c-beginning-of-defun, c-end-of-defun, c-mark-function) (c-indent-line-or-region): Use c-region-is-active-p. * lisp/progmodes/cc-defs.el (c-version): Bump up to 5.33. (c-use-category): New const. (c-next-single-property-change): New macro. (c-region-is-active-p): Prefer region-active-p when available. (c-search-backward-char-property): Fix old min/max typo; probably a copy/paste error. (c-mark-<-as-paren, c-mark->-as-paren, c-unmark-<->-as-paren): Turn them into macros that obey c-use-category. (c-sc-scan-lists-no-category+1+1, c-sc-scan-lists-no-category+1-1) (c-sc-scan-lists-no-category-1+1, c-sc-scan-lists-no-category-1-1) (c-sc-scan-lists, c-sc-parse-partial-sexp) (c-looking-at-non-alphnumspace): New macros. (c-sc-parse-partial-sexp-no-category): New function. (c-emacs-features): Add `category-properties' element. * lisp/progmodes/cc-engine.el (c-invalidate-state-cache, c-parse-state): Handle the case where categories are not available. (c-record-parse-state-state, c-replay-parse-state-state): Handle marker values. (c-before-change-check-<>-operators): Look for the `syntax-table' property rather than for the corresponding `category'. (c-looking-at-decl-block): Remove unused var `c-disallow-comma-in-<>-arglists'. (c-forward-<>-arglist-recur): Remove unused var `orig-record-found-types'. * lisp/progmodes/cc-langs.el (c-modified-constant): New lang var. (c-known-type-key): Don't make a list just to throw it away. * lisp/progmodes/cc-bytecomp.el (cc-bytecomp-unbound-variables) (cc-bytecomp-original-functions, cc-bytecomp-original-properties) (cc-bytecomp-loaded-files): Re-set each time the file is loaded. (cc-bytecomp-obsolete-var, cc-bytecomp-ignore-obsolete) (cc-bytecomp-obsolete-fun): Delete unused functions. * lisp/progmodes/cc-mode.el (c-just-done-before-change): New var. (c-basic-common-init): Initialize it. (c-common-init): Only use mode-require-final-newline when available. (c-before-change): Check and set c-just-done-before-change. (c-after-change): Re-set c-just-done-before-change. (c-advise-fl-for-region): New macro. (lazy-lock-defer-rest-after-change, lazy-lock-defer-line-after-change) (font-lock-after-change-function, jit-lock-after-change): Advise if needed.
2014-12-01 15:06:49 -05:00
"[" c-alnum "_]*"
"\\)"
2003-07-03 12:30:59 +00:00
"[ \t\n\r]*"
;; An argument list that contains zero or more arguments.
(concat
"("
"[ \t\n\r]*"
"\\("
"\\(" cc-imenu-java-method-arg-regexp ",[ \t\n\r]*\\)*"
cc-imenu-java-method-arg-regexp
"\\)?"
")"
"[.,_" c-alnum " \t\n\r]*" ; throws etc.
"{"
)) 7))
"Imenu generic expression for Java mode. See `imenu-generic-expression'.")
1998-02-17 07:10:49 +00:00
1998-02-17 07:10:49 +00:00
;; Internal variables
(defvar cc-imenu-objc-generic-expression-noreturn-index nil)
(defvar cc-imenu-objc-generic-expression-general-func-index nil)
(defvar cc-imenu-objc-generic-expression-proto-index nil)
(defvar cc-imenu-objc-generic-expression-objc-base-index nil)
(defvar cc-imenu-objc-generic-expression
(concat
;;
;; For C
;;
;; > Special case to match a line like `main() {}'
;; > e.g. no return type, not even on the previous line.
;; Pick a token by (match-string 1)
1998-02-17 07:10:49 +00:00
(car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
(prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
"\\|"
;; > General function name regexp
1998-02-17 07:10:49 +00:00
;; Pick a token by (match-string 3)
(car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 6
1998-02-17 07:10:49 +00:00
(prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
;; > Special case for definitions using phony prototype macros like:
;; > `int main _PROTO( (int argc,char *argv[]) )'.
2003-07-03 12:30:59 +00:00
;; Pick a token by (match-string 8)
(if cc-imenu-c-prototype-macro-regexp
(concat
"\\|"
1998-02-17 07:10:49 +00:00
(car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
(prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 10) "")
1998-02-17 07:10:49 +00:00
)
(prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
1998-02-17 07:10:49 +00:00
"") ; -> index += 0
(prog2 (setq cc-imenu-objc-generic-expression-proto-index 9) "")
;;
;; For Objective-C
2003-07-03 12:30:59 +00:00
;; Pick a token by (match-string 8 or 9)
;;
"\\|\\("
2003-07-03 12:30:59 +00:00
"^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
"\\|"
2003-07-03 12:30:59 +00:00
"^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
"\\|"
2003-07-03 12:30:59 +00:00
"^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
"\\|"
;; For NSObject, NSProxy and Object... They don't have super class.
2003-07-03 12:30:59 +00:00
"^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
"\\|"
2003-07-03 12:30:59 +00:00
"^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
"\\|"
2003-07-03 12:30:59 +00:00
"^@implementation[\t ]+[" c-alnum "_]+"
"\\|"
2003-07-03 12:30:59 +00:00
"^@protocol[\t ]+[" c-alnum "_]+" "\\)")
"Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
;; Imenu support for objective-c uses functions.
(defsubst cc-imenu-objc-method-to-selector (method)
"Return the objc selector style string of METHOD.
Example:
- perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
=>
-perform:withObject:withObject:withObject: /* selector */"
(let ((return "") ; String to be returned
(p 0) ; Current scanning position in METHOD
(pmax (length method)) ;
char ; Current scanning target
(betweenparen 0) ; CHAR is in parentheses.
argreq ; An argument is required.
inargvar) ; position of CHAR is in an argument variable.
(while (< p pmax)
(setq char (aref method p)
p (1+ p))
(cond
;; Is CHAR part of an objc token?
((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
(eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
(or (and (<= ?a char) (<= char ?z))
(and (<= ?A char) (<= char ?Z))
(and (<= ?0 char) (<= char ?9))
(= ?_ char)))
(if argreq
(setq inargvar t
argreq nil)
(setq return (concat return (char-to-string char)))))
;; Or a white space?
((and inargvar (or (eq ?\ char) (eq ?\n char))
(setq inargvar nil)))
;; Or a method separator?
;; If a method separator, the next token will be an argument variable.
((eq ?: char)
(setq argreq t
return (concat return (char-to-string char))))
;; Or an open parentheses?
((eq ?\( char)
(setq betweenparen (1+ betweenparen)))
;; Or a close parentheses?
((eq ?\) char)
(setq betweenparen (1- betweenparen)))))
return))
(defun cc-imenu-objc-remove-white-space (str)
"Remove all spaces and tabs from STR."
(let ((return "")
(p 0)
(max (length str))
char)
(while (< p max)
(setq char (aref str p))
(setq p (1+ p))
(if (or (= char ?\ ) (= char ?\t))
()
(setq return (concat return (char-to-string char)))))
return))
(defun cc-imenu-objc-function ()
"Imenu support for Objective C mode."
(let (methodlist
clist
;;
1998-02-17 07:10:49 +00:00
;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
;;
;; *Warning for developers*
;; These constants depend on `cc-imenu-c++-generic-expression'.
;;
1998-02-17 07:10:49 +00:00
(OBJC cc-imenu-objc-generic-expression-objc-base-index)
;; Special case to match a line like `main() {}'
(Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
;; General function name regexp
(Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
;; Special case for definitions using phony prototype macros like:
(Cproto cc-imenu-objc-generic-expression-proto-index)
langnum
;;
(classcount 0)
toplist
str
str2
(intflen (length "@interface"))
(implen (length "@implementation"))
(prtlen (length "@protocol"))
1998-02-17 07:10:49 +00:00
(func
;;
;; Does this emacs have buffer-substring-no-properties?
1998-02-17 07:10:49 +00:00
;;
(if (fboundp 'buffer-substring-no-properties)
'buffer-substring-no-properties
'buffer-substring)))
1999-02-08 16:53:18 +00:00
(goto-char (point-max))
;;
(while (re-search-backward cc-imenu-objc-generic-expression nil t)
(when (not (c-literal-limits))
(setq langnum (if (match-beginning OBJC)
OBJC
(cond
((match-beginning Cproto) Cproto)
((match-beginning Cgeneralfunc) Cgeneralfunc)
((match-beginning Cnoreturn) Cnoreturn))))
(setq str (funcall func (match-beginning langnum) (match-end langnum)))
;;
(cond
;;
;; C
;;
((not (eq langnum OBJC))
(setq clist (cons (cons str (match-beginning langnum)) clist)))
;;
;; ObjC
;;
;; An instance Method
((eq (aref str 0) ?-)
(setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
(setq methodlist (cons (cons str
(match-beginning langnum))
methodlist)))
;; A factory Method
((eq (aref str 0) ?+)
(setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
(setq methodlist (cons (cons str
(match-beginning langnum))
methodlist)))
;; Interface or implementation or protocol
((eq (aref str 0) ?@)
(setq classcount (1+ classcount))
(cond
((and (> (length str) implen)
(string= (substring str 0 implen) "@implementation"))
(setq str (substring str implen)
str2 "@implementation"))
((string= (substring str 0 intflen) "@interface")
(setq str (substring str intflen)
str2 "@interface"))
((string= (substring str 0 prtlen) "@protocol")
(setq str (substring str prtlen)
str2 "@protocol")))
(setq str (cc-imenu-objc-remove-white-space str))
(setq methodlist (cons (cons str2
(match-beginning langnum))
methodlist))
(setq toplist (cons (cons str methodlist) toplist)
methodlist nil)))))
;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
(if (< classcount 2)
(let ((classname (car (car toplist)))
(p (cdr (car (cdr (car toplist)))))
last)
(setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
;; Add C lang token
(if clist
(progn
(setq last toplist)
(while (cdr last)
(setq last (cdr last)))
(setcdr last clist))))
;; Add C lang tokens as a sub menu
2003-07-03 12:30:59 +00:00
(if clist
(setq toplist (cons (cons "C" clist) toplist))))
;;
toplist
))
1999-02-08 16:53:18 +00:00
;(defvar cc-imenu-pike-generic-expression
; ())
; FIXME: Please contribute one!
2003-07-03 12:30:59 +00:00
(defun cc-imenu-init (mode-generic-expression
&optional mode-create-index-function)
1999-12-12 18:24:19 +00:00
(setq imenu-generic-expression mode-generic-expression
2003-07-03 12:30:59 +00:00
imenu-case-fold-search nil)
(when mode-create-index-function
(setq imenu-create-index-function mode-create-index-function)))
1999-12-12 18:24:19 +00:00
1997-07-10 07:54:06 +00:00
2001-03-21 12:59:36 +00:00
(cc-provide 'cc-menus)
;; Local Variables:
;; indent-tabs-mode: t
;; tab-width: 8
;; End:
1997-07-10 07:54:06 +00:00
;;; cc-menus.el ends here