cperl-here-face)
(t (funcall (default-value 'font-lock-syntactic-face-function) state))))
+(defface cperl-method-call
+ '((t (:inherit 'default )))
+ "The face for method calls. Usually, they are not fontified.
+We use this face to prevent calls to methods which look like
+builtin functions to be fontified like, well, builtin
+functions (which they are not). Inherits from `default'.")
+
(defun cperl-init-faces ()
(condition-case errs
(progn
;; -------- trailing spaces -> use invalid-face as a warning
;; (matcher subexp facespec)
`("[ \t]+$" 0 ',cperl-invalid-face t)
+ ;; -------- function definition _and_ declaration
+ ;; (matcher (subexp facespec))
+ ;; facespec is evaluated depending on whether the
+ ;; statement ends in a "{" (definition) or ";"
+ ;; (declaration without body)
+ (list (concat "\\<" cperl-sub-regexp
+ ;; group 1: optional subroutine name
+ (rx
+ (sequence (eval cperl--ws+-rx)
+ (group (optional
+ (eval cperl--normal-identifier-rx)))))
+ ;; "fontified" elsewhere: Prototype
+ (rx (optional
+ (sequence (eval cperl--ws*-rx)
+ (eval cperl--prototype-rx))))
+ ;; fontified elsewhere: Attributes
+ (rx (optional (sequence (eval cperl--ws*-rx)
+ (eval cperl--attribute-list-rx))))
+ (rx (eval cperl--ws*-rx))
+ ;; group 2: Identifies the start of the anchor
+ (rx (group
+ (or (group-n 3 ";") ; Either a declaration...
+ "{" ; ... or a code block
+ ;; ... or a complete signature
+ (sequence (eval cperl--signature-rx)
+ (eval cperl--ws*-rx))
+ ;; ... or the start of a "sloppy" signature
+ (sequence (eval cperl--sloppy-signature-rx)
+ ;; arbtrarily continue "a few lines"
+ (repeat 0 200 (not (in "{"))))
+ ;; make sure we have a reasonably
+ ;; short match for an incomplete sub
+ (not (in ";{("))
+ buffer-end))))
+ '(1 (if (match-beginning 3)
+ 'font-lock-variable-name-face
+ 'font-lock-function-name-face)
+ nil ; override
+ t) ; laxmatch in case of anonymous subroutines
+ ;; -------- anchored: Signature
+ `(,(rx (sequence (in "(,")
+ (eval cperl--ws*-rx)
+ (group (eval cperl--basic-variable-rx))))
+ (progn
+ (goto-char (match-beginning 2)) ; pre-match: Back to sig
+ (match-end 2))
+ nil
+ (1 font-lock-variable-name-face)))
;; -------- flow control
;; (matcher . subexp) font-lock-keyword-face by default
+ ;; This highlights declarations and definitions differently.
+ ;; We do not try to highlight in the case of attributes:
+ ;; it is already done by `cperl-find-pods-heres'
(cons
(concat
"\\(^\\|[^$@%&\\]\\)\\<\\("
"\\)\\>") 2) ; was "\\)[ \n\t;():,|&]"
; In what follows we use `type' style
; for overwritable builtins
+ ;; -------- avoid method calls being fontified as keywords
+ ;; (matcher (subexp facespec))
+ (list
+ (rx "->" (* space) (group-n 1(eval cperl--basic-identifier-rx)))
+ 1 ''cperl-method-call)
;; -------- builtin functions
;; (matcher subexp facespec)
(list
;; (matcher subexp facespec)
'("-[rwxoRWXOezsfdlpSbctugkTBMAC]\\>\\([ \t]+_\\>\\)?" 0
font-lock-function-name-face keep) ; Not very good, triggers at "[a-z]"
- ;; This highlights declarations and definitions differently.
- ;; We do not try to highlight in the case of attributes:
- ;; it is already done by `cperl-find-pods-heres'
- ;; -------- function definition _and_ declaration
- ;; (matcher (subexp facespec))
- ;; facespec is evaluated depending on whether the
- ;; statement ends in a "{" (definition) or ";"
- ;; (declaration without body)
- (list (concat "\\<" cperl-sub-regexp
- ;; group 1: optional subroutine name
- (rx
- (sequence (eval cperl--ws+-rx)
- (group (optional
- (eval cperl--normal-identifier-rx)))))
- ;; "fontified" elsewhere: Prototype
- (rx (optional
- (sequence (eval cperl--ws*-rx)
- (eval cperl--prototype-rx))))
- ;; fontified elsewhere: Attributes
- (rx (optional (sequence (eval cperl--ws*-rx)
- (eval cperl--attribute-list-rx))))
- (rx (eval cperl--ws*-rx))
- ;; group 2: Identifies the start of the anchor
- (rx (group
- (or (group-n 3 ";") ; Either a declaration...
- "{" ; ... or a code block
- ;; ... or a complete signature
- (sequence (eval cperl--signature-rx)
- (eval cperl--ws*-rx))
- ;; ... or the start of a "sloppy" signature
- (sequence (eval cperl--sloppy-signature-rx)
- ;; arbtrarily continue "a few lines"
- (repeat 0 200 (not (in "{"))))
- ;; make sure we have a reasonably
- ;; short match for an incomplete sub
- (not (in ";{("))
- buffer-end))))
- '(1 (if (match-beginning 3)
- 'font-lock-variable-name-face
- 'font-lock-function-name-face)
- t ;; override
- t) ;; laxmatch in case of anonymous subroutines
- ;; -------- anchored: Signature
- `(,(rx (sequence (in "(,")
- (eval cperl--ws*-rx)
- (group (eval cperl--basic-variable-rx))))
- (progn
- (goto-char (match-beginning 2)) ; pre-match: Back to sig
- (match-end 2))
- nil
- (1 font-lock-variable-name-face)))
;; -------- various stuff calling for a package name
;; (matcher (subexp facespec) (subexp facespec))
`(,(rx (sequence
(should (equal (get-text-property (point) 'face)
'font-lock-variable-name-face))))
+(ert-deftest cperl-test-fontify-sub-names ()
+ "Test fontification of subroutines named like builtins.
+On declaration, they should look like other used defined
+functions. When called, they should not be fontified. In
+comments and POD they should be fontified as POD."
+ (let ((file (ert-resource-file "sub-names.pl")))
+ (with-temp-buffer
+ (insert-file-contents file)
+ (goto-char (point-min))
+ (funcall cperl-test-mode)
+ (font-lock-ensure)
+ ;; The declaration
+ (search-forward-regexp "sub \\(m\\)")
+ (should (equal (get-text-property (match-beginning 1) 'face)
+ 'font-lock-function-name-face))
+ ;; calling as a method
+ (search-forward-regexp "C->new->\\(m\\)")
+ (should (equal (get-text-property (match-beginning 1) 'face)
+ (if (equal cperl-test-mode 'perl-mode) nil
+ 'cperl-method-call)))
+ ;; POD
+ (search-forward-regexp "\\(method\\) \\(name\\)")
+ (should (equal (get-text-property (match-beginning 1) 'face)
+ 'font-lock-comment-face))
+ (should (equal (get-text-property (match-beginning 2) 'face)
+ 'font-lock-comment-face))
+ ;; comment
+ (search-forward-regexp "\\(method\\) \\(name\\)")
+ (should (equal (get-text-property (match-beginning 1) 'face)
+ 'font-lock-comment-face))
+ (should (equal (get-text-property (match-beginning 2) 'face)
+ 'font-lock-comment-face)))))
+
(ert-deftest cperl-test-identify-heredoc ()
"Test whether a construct containing \"<<\" followed by a
bareword is properly identified for a here-document if