Skip to content
Browse files

factor out regexps that look for "sub"

This makes it possible to add other sub-alikes trivially, like
MooseX::Declare's method/before/after/around/override/augment

also make cperl-after-block-p respect cperl-sub-keywords
  • Loading branch information...
1 parent 509bcc5 commit 9dae637283c475ec046e802fbda819ed788ce5be @jrockway committed May 10, 2009
Showing with 32 additions and 14 deletions.
  1. +32 −14 cperl-mode.el
View
46 cperl-mode.el
@@ -1407,7 +1407,6 @@ Should contain exactly one group.")
"Regular expression to match whitespace with interpspersed comments.
Should contain exactly one group.")
-
;;; Is incorporated in `cperl-imenu--function-name-regexp-perl'
;;; `cperl-outline-regexp', `defun-prompt-regexp'.
;;; Details of groups in this may be used in several functions; see comments
@@ -1447,6 +1446,19 @@ the last)."
"\\)?" ; END n+6=proto-group
))
+;;; Tired of editing this in 8 places every time I remember that there
+;;; is another method-defining keyword
+(defvar cperl-sub-keywords
+ '("sub"))
+
+(defvar cperl-sub-regexp (regexp-opt cperl-sub-keywords))
+
+(defun cperl-char-ends-sub-keyword-p (char)
+ "Return T if CHAR is the last character of a perl sub keyword."
+ (loop for keyword in cperl-sub-keywords
+ when (eq char (aref keyword (1- (length keyword))))
+ return t))
+
;;; Details of groups in this are used in `cperl-imenu--create-perl-index'
;;; and `cperl-outline-level'.
;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-name (+3)
@@ -1458,7 +1470,8 @@ the last)."
cperl-white-and-comment-rex ; 4 = pre-package-name
"\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name
"\\|"
- "[ \t]*sub"
+ "[ \t]*"
+ cperl-sub-regexp
(cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
cperl-maybe-white-and-comment-rex ; 15=pre-block
"\\|"
@@ -1781,7 +1794,8 @@ or as help on variables `cperl-tips', `cperl-problems',
;;; (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-start
;;; cperl-maybe-white-and-comment-rex ; 15=pre-block
(setq defun-prompt-regexp
- (concat "^[ \t]*\\(sub"
+ (concat "^[ \t]*\\("
+ cperl-sub-regexp
(cperl-after-sub-regexp 'named 'attr-groups)
"\\|" ; per toke.c
"\\(BEGIN\\|UNITCHECK\\|CHECK\\|INIT\\|END\\|AUTOLOAD\\|DESTROY\\)"
@@ -2989,7 +3003,7 @@ Will not look before LIM."
(point) 'attrib-group)))
((eq (preceding-char) ?b)
(forward-sexp -1)
- (looking-at "sub\\>")))
+ (looking-at (concat cperl-sub-regexp "\\>"))))
(setq p (nth 1 ; start of innermost containing list
(parse-partial-sexp
(save-excursion (beginning-of-line)
@@ -3715,7 +3729,7 @@ the sections using `cperl-pod-head-face', `cperl-pod-face',
"\\([?/<]\\)" ; /blah/ or ?blah? or <file*glob>
"\\|"
;; 1+6+2+1+1=11 extra () before this
- "\\<sub\\>" ; sub with proto/attr
+ "\\<" cperl-sub-regexp "\\>" ; sub with proto/attr
"\\("
cperl-white-and-comment-rex
"\\(::[a-zA-Z_:'0-9]*\\|[a-zA-Z_'][a-zA-Z_:'0-9]*\\)\\)?" ; name
@@ -4755,8 +4769,8 @@ the sections using `cperl-pod-head-face', `cperl-pod-face',
(setq stop t))))))
;; Used only in `cperl-calculate-indent'...
-(defun cperl-block-p () ; Do not C-M-q ! One string contains ";" !
- ;; Positions is before ?\{. Checks whether it starts a block.
+(defun cperl-block-p ()
+ "Positions is before ?\{. Checks whether it starts a block."
;; No save-excursion! This is more a distinguisher of a block/hash ref...
(cperl-backward-to-noncomment (point-min))
(or (memq (preceding-char) (append ";){}$@&%\C-@" nil)) ; Or label! \C-@ at bobp
@@ -4775,7 +4789,7 @@ the sections using `cperl-pod-head-face', `cperl-pod-face',
(and (eq (preceding-char) ?b)
(progn
(forward-sexp -1)
- (looking-at "sub[ \t\n\f#]")))))))))
+ (looking-at (concat cperl-sub-regexp "[ \t\n\f#]"))))))))))
;;; What is the difference of (cperl-after-block-p lim t) and (cperl-block-p)?
;;; No save-excursion; condition-case ... In (cperl-block-p) the block
@@ -4809,10 +4823,11 @@ statement would start; thus the block in ${func()} does not count."
;; sub f {}
(progn
(cperl-backward-to-noncomment lim)
- (and (eq (preceding-char) ?b)
+ (and (cperl-char-ends-sub-keyword-p (preceding-char))
(progn
(forward-sexp -1)
- (looking-at "sub[ \t\n\f#]"))))))
+ (looking-at
+ (concat cperl-sub-regexp "[ \t\n\f#]")))))))
;; What preceeds is not word... XXXX Last statement in sub???
(cperl-after-expr-p lim))))
(error nil))))
@@ -5643,14 +5658,17 @@ indentation and initial hashes. Behaves usually outside of comment."
"\\(^\\|[^$@%&\\]\\)\\<\\("
(mapconcat
'identity
- '("if" "until" "while" "elsif" "else"
+ (append
+ cperl-sub-keywords
+ '("if" "until" "while" "elsif" "else"
"given" "when" "default" "break"
"unless" "for"
"foreach" "continue" "exit" "die" "last" "goto" "next"
- "redo" "return" "local" "exec" "sub" "do" "dump"
+ "redo" "return" "local" "exec"
+ "do" "dump"
"use" "our"
"require" "package" "eval" "my" "state"
- "BEGIN" "END" "CHECK" "INIT" "UNITCHECK")
+ "BEGIN" "END" "CHECK" "INIT" "UNITCHECK"))
"\\|") ; Flow control
"\\)\\>") 2) ; was "\\)[ \n\t;():,\|&]"
; In what follows we use `type' style
@@ -5758,7 +5776,7 @@ indentation and initial hashes. Behaves usually outside of comment."
;; This highlights declarations and definitions differenty.
;; We do not try to highlight in the case of attributes:
;; it is already done by `cperl-find-pods-heres'
- (list (concat "\\<sub"
+ (list (concat "\\<" cperl-sub-regexp
cperl-white-and-comment-rex ; whitespace/comments
"\\([^ \n\t{;()]+\\)" ; 2=name (assume non-anonymous)
"\\("

0 comments on commit 9dae637

Please sign in to comment.
Something went wrong with that request. Please try again.