From 899fc4cb9bab7448d41bf1f83f86f7d143255be0 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Sun, 26 Jul 2020 02:22:47 +0900 Subject: [PATCH] Support PHP 8 match expression https://wiki.php.net/rfc/match_expression_v2 refs https://github.com/emacs-php/php-mode/issues/631 --- php-mode.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/php-mode.el b/php-mode.el index 1f2ecca4..31fbd65e 100644 --- a/php-mode.el +++ b/php-mode.el @@ -519,6 +519,9 @@ PHP does not have an \"enum\"-like keyword." (c-lang-defconst c-lambda-kwds php '("function" "use")) +(c-lang-defconst c-inexpr-block-kwds + php '("match")) + (c-lang-defconst c-other-block-decl-kwds php '("namespace")) @@ -643,6 +646,23 @@ but only if the setting is enabled" (beginning-of-line) (if (looking-at-p "\\s-*->") '+ nil)))) +(defun php-c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim) + "Replace `c-looking-at-or-maybe-in-bracelist'. + +CONTAINING-SEXP is the position of the brace/paren/bracket enclosing +POINT, or nil if there is no such position, or we do not know it. LIM is +a backward search limit." + (cond + ((looking-at-p "{") + (save-excursion + (c-backward-token-2 2 t lim) + ;; PHP 8.0 match expression + ;; echo match ($var) |{ + ;; ↑ matches ↑ initial position + (when (looking-at-p (eval-when-compile (rx symbol-start "match" symbol-end))) + (cons (point) t)))) + (t nil))) + (c-add-style "php" `((c-basic-offset . 4) @@ -1186,6 +1206,10 @@ After setting the stylevars run hooks according to STYLENAME (setq-local add-log-current-defun-function nil) (setq-local add-log-current-defun-header-regexp php-beginning-of-defun-regexp) + (when (fboundp #'c-looking-at-or-maybe-in-bracelist) + (advice-add #'c-looking-at-or-maybe-in-bracelist + :override 'php-c-looking-at-or-maybe-in-bracelist)) + (when (>= emacs-major-version 25) (with-silent-modifications (save-excursion