summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimeon Simeonov2017-11-08 19:08:11 +0100
committerSimeon Simeonov2017-11-08 19:08:11 +0100
commit131a54ee37028f709a5f77e04513e9a80535e8c7 (patch)
treee7aaab12df1e6761f549496190c8aaf668faccc8
parent39a0a3ab34dbb74a66b71d6dd72d4f9f18968d70 (diff)
Upgrade php-mode.el
-rw-r--r--.emacs.d/lisp/php-mode.el247
1 files changed, 119 insertions, 128 deletions
diff --git a/.emacs.d/lisp/php-mode.el b/.emacs.d/lisp/php-mode.el
index c666cc5..9413fce 100644
--- a/.emacs.d/lisp/php-mode.el
+++ b/.emacs.d/lisp/php-mode.el
@@ -294,9 +294,12 @@ You can replace \"en\" with your ISO language code."
294 :type 'string) 294 :type 'string)
295 295
296;;;###autoload 296;;;###autoload
297(add-to-list 'interpreter-mode-alist 297(if (version< emacs-version "24.4")
298 ;; Match php, php-3, php5, php7, php5.5, php-7.0.1, etc. 298 (dolist (i '("php" "php5" "php7"))
299 (cons "php\\(?:-?[3457]\\(?:\\.[0-9]+\\)*\\)?" 'php-mode)) 299 (add-to-list 'interpreter-mode-alist (cons i 'php-mode)))
300 (add-to-list 'interpreter-mode-alist
301 ;; Match php, php-3, php5, php7, php5.5, php-7.0.1, etc.
302 (cons "php\\(?:-?[3457]\\(?:\\.[0-9]+\\)*\\)?" 'php-mode)))
300 303
301(defcustom php-mode-hook nil 304(defcustom php-mode-hook nil
302 "List of functions to be executed on entry to `php-mode'." 305 "List of functions to be executed on entry to `php-mode'."
@@ -362,18 +365,7 @@ This variable can take one of the following symbol values:
362 (when (eq major-mode 'php-mode) 365 (when (eq major-mode 'php-mode)
363 (set sym value) 366 (set sym value)
364 (set-default sym value) 367 (set-default sym value)
365 (cond ((eq value 'pear) 368 (php-set-style (symbol-name value))))
366 (php-enable-pear-coding-style))
367 ((eq value 'default)
368 (php-enable-default-coding-style))
369 ((eq value 'drupal)
370 (php-enable-drupal-coding-style))
371 ((eq value 'wordpress)
372 (php-enable-wordpress-coding-style))
373 ((eq value 'symfony2)
374 (php-enable-symfony2-coding-style))
375 ((eq value 'psr2)
376 (php-enable-psr2-coding-style)))))
377 369
378(defun php-mode-version () 370(defun php-mode-version ()
379 "Display string describing the version of PHP Mode." 371 "Display string describing the version of PHP Mode."
@@ -423,6 +415,7 @@ This variable can take one of the following symbol values:
423 ;; c-end-of-defun to something other than C-M-e. 415 ;; c-end-of-defun to something other than C-M-e.
424 (define-key map [remap c-beginning-of-defun] 'php-beginning-of-defun) 416 (define-key map [remap c-beginning-of-defun] 'php-beginning-of-defun)
425 (define-key map [remap c-end-of-defun] 'php-end-of-defun) 417 (define-key map [remap c-end-of-defun] 'php-end-of-defun)
418 (define-key map [remap c-set-style] 'php-set-style)
426 419
427 (define-key map [(control c) (control f)] 'php-search-documentation) 420 (define-key map [(control c) (control f)] 'php-search-documentation)
428 (define-key map [(meta tab)] 'php-complete-function) 421 (define-key map [(meta tab)] 'php-complete-function)
@@ -436,6 +429,34 @@ This variable can take one of the following symbol values:
436 map) 429 map)
437 "Keymap for `php-mode'") 430 "Keymap for `php-mode'")
438 431
432(c-lang-defconst c-get-state-before-change-functions
433 ;; const might be a symbol in older versions
434 php (let ((const (c-lang-const c-get-state-before-change-functions)))
435 (cl-set-difference (if (listp const) const (list const))
436 '(c-parse-quotes-before-change))))
437
438(defun php-unescape-identifiers (beg end &optional old-len)
439 "Change syntax of backslashes in identifiers between BEG and END, ignore OLD-LEN."
440 (c-save-buffer-state (num-beg num-end)
441 (save-restriction
442 (goto-char c-new-BEG)
443 (while (and (< (point) c-new-END)
444 (search-forward "\\" c-new-END 'limit))
445 (if (and (not (php-in-string-p))
446 (looking-at-p c-identifier-key))
447 ;; within function `c-forward-name' when looking at
448 ;; `c-identifier-key' ensure that `c-simple-skip-symbol-backward'
449 ;; skips over backslashes too in making it at word entry.
450 (c-put-char-property (1- (point)) 'syntax-table '(2)))))))
451
452(c-lang-defconst c-before-font-lock-functions
453 ;; const might be a symbol in older versions
454 php (let ((const (c-lang-const c-before-font-lock-functions)))
455 (append (cl-set-difference (if (listp const) const (list const))
456 '(c-restore-<>-properties
457 c-parse-quotes-after-change))
458 '(php-unescape-identifiers))))
459
439(c-lang-defconst c-mode-menu 460(c-lang-defconst c-mode-menu
440 php (append '(["Complete function name" php-complete-function t] 461 php (append '(["Complete function name" php-complete-function t]
441 ["Browse manual" php-browse-manual t] 462 ["Browse manual" php-browse-manual t]
@@ -624,7 +645,7 @@ but only if the setting is enabled"
624 645
625(c-add-style 646(c-add-style
626 "php" 647 "php"
627 '((c-basic-offset . 4) 648 `((c-basic-offset . 4)
628 (c-offsets-alist . ((arglist-close . php-lineup-arglist-close) 649 (c-offsets-alist . ((arglist-close . php-lineup-arglist-close)
629 (arglist-cont . (first php-lineup-cascaded-calls 0)) 650 (arglist-cont . (first php-lineup-cascaded-calls 0))
630 (arglist-cont-nonempty . (first php-lineup-cascaded-calls c-lineup-arglist)) 651 (arglist-cont-nonempty . (first php-lineup-cascaded-calls c-lineup-arglist))
@@ -639,103 +660,90 @@ but only if the setting is enabled"
639 (label . +) 660 (label . +)
640 (statement-cont . (first php-lineup-cascaded-calls php-lineup-string-cont +)) 661 (statement-cont . (first php-lineup-cascaded-calls php-lineup-string-cont +))
641 (substatement-open . 0) 662 (substatement-open . 0)
642 (topmost-intro-cont . (first php-lineup-cascaded-calls +)))))) 663 (topmost-intro-cont . (first php-lineup-cascaded-calls +))))
664 (indent-tabs-mode . nil)
665 (tab-width . ,(default-value 'tab-width))
666 (fill-column . ,(default-value 'fill-column))
667 (require-final-newline . ,(default-value 'require-final-newline))
668 (show-trailing-whitespace . ,(default-value 'show-trailing-whitespace))
669 (php-style-delete-trailing-whitespace . nil)))
643 670
644(defun php-enable-default-coding-style () 671(defun php-enable-default-coding-style ()
645 "Set PHP Mode to use reasonable default formatting." 672 "Set PHP Mode to use reasonable default formatting."
646 (interactive) 673 (interactive)
647 (c-set-style "php")) 674 (php-set-style "php"))
648 675
649(c-add-style 676(c-add-style
650 "pear" 677 "pear"
651 '("php" 678 '("php"
652 (c-basic-offset . 4) 679 (c-basic-offset . 4)
653 (c-offsets-alist . ((case-label . 0))))) 680 (c-offsets-alist . ((case-label . 0)))
681 (tab-width . 4)))
654 682
655(defun php-enable-pear-coding-style () 683(defun php-enable-pear-coding-style ()
656 "Sets up php-mode to use the coding styles preferred for PEAR 684 "Sets up php-mode to use the coding styles preferred for PEAR
657code and modules." 685code and modules."
658 (interactive) 686 (interactive)
659 (setq tab-width 4 687 (php-set-style "pear"))
660 indent-tabs-mode nil)
661 (c-set-style "pear")
662
663 ;; Undo drupal/PSR-2 coding style whitespace effects
664 (set (make-local-variable 'show-trailing-whitespace)
665 (default-value 'show-trailing-whitespace)))
666 688
667(c-add-style 689(c-add-style
668 "drupal" 690 "drupal"
669 '("php" 691 '("php"
670 (c-basic-offset . 2))) 692 (c-basic-offset . 2)
693 (tab-width . 2)
694 (fill-column . 78)
695 (show-trailing-whitespace . t)
696 (php-style-delete-trailing-whitespace . t)))
671 697
672(defun php-enable-drupal-coding-style () 698(defun php-enable-drupal-coding-style ()
673 "Makes php-mode use coding styles that are preferable for 699 "Makes php-mode use coding styles that are preferable for
674working with Drupal." 700working with Drupal."
675 (interactive) 701 (interactive)
676 (setq tab-width 2 702 (php-set-style "drupal"))
677 indent-tabs-mode nil
678 fill-column 78)
679 (set (make-local-variable 'show-trailing-whitespace) t)
680 (add-hook 'before-save-hook 'delete-trailing-whitespace nil t)
681 (c-set-style "drupal"))
682 703
683(c-add-style 704(c-add-style
684 "wordpress" 705 "wordpress"
685 '("php" 706 '("php"
686 (c-basic-offset . 4))) 707 (c-basic-offset . 4)
708 (c-indent-comments-syntactically-p t)
709 (indent-tabs-mode . t)
710 (tab-width . 4)
711 (fill-column . 78)))
687 712
688(defun php-enable-wordpress-coding-style () 713(defun php-enable-wordpress-coding-style ()
689 "Makes php-mode use coding styles that are preferable for 714 "Makes php-mode use coding styles that are preferable for
690working with Wordpress." 715working with Wordpress."
691 (interactive) 716 (interactive)
692 (setq indent-tabs-mode t 717 (php-set-style "wordpress"))
693 fill-column 78
694 tab-width 4
695 c-indent-comments-syntactically-p t)
696 (c-set-style "wordpress")
697
698 ;; Undo drupal/PSR-2 coding style whitespace effects
699 (set (make-local-variable 'show-trailing-whitespace)
700 (default-value 'show-trailing-whitespace)))
701 718
702(c-add-style 719(c-add-style
703 "symfony2" 720 "symfony2"
704 '("php" 721 '("php"
705 (c-offsets-alist . ((statement-cont . php-lineup-hanging-semicolon))))) 722 (c-offsets-alist . ((statement-cont . php-lineup-hanging-semicolon)))
723 (c-indent-comments-syntactically-p . t)
724 (fill-column . 78)
725 (require-final-newline . t)))
706 726
707(defun php-enable-symfony2-coding-style () 727(defun php-enable-symfony2-coding-style ()
708 "Makes php-mode use coding styles that are preferable for 728 "Makes php-mode use coding styles that are preferable for
709working with Symfony2." 729working with Symfony2."
710 (interactive) 730 (interactive)
711 (setq indent-tabs-mode nil 731 (php-set-style "symfony2"))
712 fill-column 78
713 c-indent-comments-syntactically-p t
714 require-final-newline t)
715 (c-set-style "symfony2")
716
717 ;; Undo drupal/PSR-2 coding style whitespace effects
718 (set (make-local-variable 'show-trailing-whitespace)
719 (default-value 'show-trailing-whitespace)))
720 732
721(c-add-style 733(c-add-style
722 "psr2" 734 "psr2"
723 '("php" 735 '("php"
724 (c-offsets-alist . ((statement-cont . +))))) 736 (c-offsets-alist . ((statement-cont . +)))
737 (c-indent-comments-syntactically-p . t)
738 (fill-column . 78)
739 (require-final-newline . t)
740 (show-trailing-whitespace . t)
741 (php-style-delete-trailing-whitespace . t)))
725 742
726(defun php-enable-psr2-coding-style () 743(defun php-enable-psr2-coding-style ()
727 "Makes php-mode comply to the PSR-2 coding style" 744 "Makes php-mode comply to the PSR-2 coding style"
728 (interactive) 745 (interactive)
729 (setq indent-tabs-mode nil 746 (php-set-style "psr2"))
730 fill-column 78
731 c-indent-comments-syntactically-p t
732 require-final-newline t)
733 (c-set-style "psr2")
734
735 ;; Apply drupal-like coding style whitespace effects
736 (set (make-local-variable 'require-final-newline) t)
737 (set (make-local-variable 'show-trailing-whitespace) t)
738 (add-hook 'before-save-hook 'delete-trailing-whitespace nil t))
739 747
740(defconst php-beginning-of-defun-regexp 748(defconst php-beginning-of-defun-regexp
741 "^\\s-*\\(?:\\(?:abstract\\|final\\|private\\|protected\\|public\\|static\\)\\s-+\\)*function\\s-+&?\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" 749 "^\\s-*\\(?:\\(?:abstract\\|final\\|private\\|protected\\|public\\|static\\)\\s-+\\)*function\\s-+&?\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*("
@@ -949,9 +957,8 @@ the string HEREDOC-START."
949 957
950(defun php-syntax-propertize-function (start end) 958(defun php-syntax-propertize-function (start end)
951 "Apply propertize rules from START to END." 959 "Apply propertize rules from START to END."
952 ;; (defconst php-syntax-propertize-function 960 ;; versions < git-snapshot as of 2017-10 need this here
953 ;; (syntax-propertize-rules 961 (php-unescape-identifiers start end)
954 ;; (php-heredoc-start-re (0 (ignore (php-heredoc-syntax))))))
955 (goto-char start) 962 (goto-char start)
956 (while (and (< (point) end) 963 (while (and (< (point) end)
957 (re-search-forward php-heredoc-start-re end t)) 964 (re-search-forward php-heredoc-start-re end t))
@@ -1097,6 +1104,33 @@ PHP heredoc."
1097 1104
1098(define-obsolete-face-alias 'php-annotations-annotation-face 'php-doc-annotation-tag "1.19.0") 1105(define-obsolete-face-alias 'php-annotations-annotation-face 'php-doc-annotation-tag "1.19.0")
1099 1106
1107(defun php-set-style (stylename &optional dont-override)
1108 "Set the current `php-mode' buffer to use the style STYLENAME.
1109STYLENAME is one of the names selectable in `php-mode-coding-style'.
1110
1111Borrow the `interactive-form' from `c-set-style' and use the original
1112`c-set-style' function to set all declared stylevars.
1113For compatibility with `c-set-style' pass DONT-OVERRIDE to it.
1114
1115After setting the stylevars run hooks according to STYLENAME
1116
1117 \"pear\" `php-mode-pear-hook'
1118 \"drupal\" `php-mode-drupal-hook'
1119 \"wordpress\" `php-mode-wordpress-hook'
1120 \"symfony2\" `php-mode-symfony2-hook'
1121 \"psr2\" `php-mode-psr2-hook'"
1122 (interactive)
1123 (c-set-style stylename dont-override)
1124 (if (eq (symbol-value 'php-style-delete-trailing-whitespace) t)
1125 (add-hook 'before-save-hook 'delete-trailing-whitespace nil t)
1126 (remove-hook 'before-save-hook 'delete-trailing-whitespace t))
1127 (cond ((eq stylename "pear") (run-hooks 'php-mode-pear-hook))
1128 ((eq stylename "drupal") (run-hooks 'php-mode-drupal-hook))
1129 ((eq stylename "wordpress") (run-hooks 'php-mode-wordpress-hook))
1130 ((eq stylename "symfony2") (run-hooks 'php-mode-symfony2-hook))
1131 ((eq stylename "psr2") (run-hooks 'php-mode-psr2-hook))))
1132
1133(put 'php-set-style 'interactive-form (interactive-form 'c-set-style))
1100 1134
1101;;;###autoload 1135;;;###autoload
1102(define-derived-mode php-mode c-mode "PHP" 1136(define-derived-mode php-mode c-mode "PHP"
@@ -1116,16 +1150,10 @@ PHP heredoc."
1116 (set (make-local-variable font-lock-constant-face) 'php-constant) 1150 (set (make-local-variable font-lock-constant-face) 'php-constant)
1117 1151
1118 (modify-syntax-entry ?_ "_" php-mode-syntax-table) 1152 (modify-syntax-entry ?_ "_" php-mode-syntax-table)
1119 (modify-syntax-entry ?` "\"" php-mode-syntax-table)
1120 (modify-syntax-entry ?\" "\"" php-mode-syntax-table)
1121 (modify-syntax-entry ?# "< b" php-mode-syntax-table) 1153 (modify-syntax-entry ?# "< b" php-mode-syntax-table)
1122 (modify-syntax-entry ?\n "> b" php-mode-syntax-table) 1154 (modify-syntax-entry ?\n "> b" php-mode-syntax-table)
1123 (modify-syntax-entry ?$ "'" php-mode-syntax-table) 1155 (modify-syntax-entry ?$ "'" php-mode-syntax-table)
1124 1156
1125 (set (make-local-variable 'syntax-propertize-via-font-lock)
1126 '(("\\(\"\\)\\(\\\\.\\|[^\"\n\\]\\)*\\(\"\\)" (1 "\"") (3 "\""))
1127 ("\\(\'\\)\\(\\\\.\\|[^\'\n\\]\\)*\\(\'\\)" (1 "\"") (3 "\""))))
1128
1129 (add-to-list (make-local-variable 'syntax-propertize-extend-region-functions) 1157 (add-to-list (make-local-variable 'syntax-propertize-extend-region-functions)
1130 #'php-syntax-propertize-extend-region) 1158 #'php-syntax-propertize-extend-region)
1131 (set (make-local-variable 'syntax-propertize-function) 1159 (set (make-local-variable 'syntax-propertize-function)
@@ -1142,48 +1170,14 @@ PHP heredoc."
1142 (set (make-local-variable 'require-final-newline) nil) 1170 (set (make-local-variable 'require-final-newline) nil)
1143 (set (make-local-variable 'next-line-add-newlines) nil) 1171 (set (make-local-variable 'next-line-add-newlines) nil)
1144 1172
1145 ;; PEAR coding standards 1173 (php-set-style (symbol-name php-mode-coding-style))
1146 (add-hook 'php-mode-pear-hook 'php-enable-pear-coding-style
1147 nil t)
1148
1149 ;; ;; Drupal coding standards
1150 (add-hook 'php-mode-drupal-hook 'php-enable-drupal-coding-style
1151 nil t)
1152
1153 ;; ;; WordPress coding standards
1154 (add-hook 'php-mode-wordpress-hook 'php-enable-wordpress-coding-style
1155 nil t)
1156
1157 ;; ;; Symfony2 coding standards
1158 (add-hook 'php-mode-symfony2-hook 'php-enable-symfony2-coding-style
1159 nil t)
1160
1161 ;; ;; PSR-2 coding standards
1162 (add-hook 'php-mode-psr2-hook 'php-enable-psr2-coding-style
1163 nil t)
1164
1165 (cond ((eq php-mode-coding-style 'pear)
1166 (php-enable-pear-coding-style)
1167 (run-hooks 'php-mode-pear-hook))
1168 ((eq php-mode-coding-style 'drupal)
1169 (php-enable-drupal-coding-style)
1170 (run-hooks 'php-mode-drupal-hook))
1171 ((eq php-mode-coding-style 'wordpress)
1172 (php-enable-wordpress-coding-style)
1173 (run-hooks 'php-mode-wordpress-hook))
1174 ((eq php-mode-coding-style 'symfony2)
1175 (php-enable-symfony2-coding-style)
1176 (run-hooks 'php-mode-symfony2-hook))
1177 ((eq php-mode-coding-style 'psr2)
1178 (php-enable-psr2-coding-style)
1179 (run-hooks 'php-mode-psr2-hook)))
1180 1174
1181 (if (or php-mode-force-pear 1175 (if (or php-mode-force-pear
1182 (and (stringp buffer-file-name) 1176 (and (stringp buffer-file-name)
1183 (string-match "PEAR\\|pear" 1177 (string-match "PEAR\\|pear"
1184 (buffer-file-name)) 1178 (buffer-file-name))
1185 (string-match "\\.php$" (buffer-file-name)))) 1179 (string-match "\\.php$" (buffer-file-name))))
1186 (run-hooks 'php-mode-pear-hook)) 1180 (php-set-style "pear"))
1187 1181
1188 (setq indent-line-function 'php-cautious-indent-line) 1182 (setq indent-line-function 'php-cautious-indent-line)
1189 (setq indent-region-function 'php-cautious-indent-region) 1183 (setq indent-region-function 'php-cautious-indent-region)
@@ -1478,7 +1472,8 @@ a completion list."
1478 "callable" "iterable" "number")) 1472 "callable" "iterable" "number"))
1479 1473
1480(defconst php-phpdoc-type-tags 1474(defconst php-phpdoc-type-tags
1481 (list "param" "property" "property-read" "property-write" "return" "var")) 1475 (list "package" "param" "property" "property-read" "property-write"
1476 "return" "throws" "var"))
1482 1477
1483(defconst php-phpdoc-font-lock-doc-comments 1478(defconst php-phpdoc-font-lock-doc-comments
1484 `(("{@[-[:alpha:]]+\\s-\\([^}]*\\)}" ; "{@foo ...}" markup. 1479 `(("{@[-[:alpha:]]+\\s-\\([^}]*\\)}" ; "{@foo ...}" markup.
@@ -1608,17 +1603,26 @@ a completion list."
1608;;; Provide support for Flymake so that users can see warnings and 1603;;; Provide support for Flymake so that users can see warnings and
1609;;; errors in real-time as they write code. 1604;;; errors in real-time as they write code.
1610 1605
1611(defun flymake-php-init () 1606(defun php-flymake-php-init ()
1612 (let* ((temp-file (flymake-init-create-temp-buffer-copy 1607 "PHP specific init-cleanup routines.
1613 'flymake-create-temp-inplace)) 1608
1609This is an alternative function of `flymake-php-init'.
1610Look at the `php-executable' variable instead of the constant \"php\" command."
1611 (let* ((temp-file
1612 (funcall
1613 (eval-when-compile
1614 (if (fboundp 'flymake-proc-init-create-temp-buffer-copy)
1615 'flymake-proc-init-create-temp-buffer-copy
1616 'flymake-init-create-temp-buffer-copy))
1617 'flymake-create-temp-inplace))
1614 (local-file (file-relative-name 1618 (local-file (file-relative-name
1615 temp-file 1619 temp-file
1616 (file-name-directory buffer-file-name)))) 1620 (file-name-directory buffer-file-name))))
1617 (list php-executable (list "-f" local-file "-l")))) 1621 (list php-executable (list "-f" local-file "-l"))))
1618 1622
1619(add-to-list 'flymake-allowed-file-name-masks 1623(add-to-list 'flymake-allowed-file-name-masks
1620 '("\\.php[345s]?$" 1624 '("\\.php[345s]?\\'"
1621 flymake-php-init 1625 php-flymake-php-init
1622 flymake-simple-cleanup 1626 flymake-simple-cleanup
1623 flymake-get-real-file-name)) 1627 flymake-get-real-file-name))
1624 1628
@@ -1676,19 +1680,6 @@ The output will appear in the buffer *PHP*."
1676 1680
1677(ad-activate 'fixup-whitespace) 1681(ad-activate 'fixup-whitespace)
1678 1682
1679;; Advice `font-lock-fontify-keywords-region' to support namespace
1680;; separators in class names. Use word syntax for backslashes when
1681;; doing keyword fontification, but not when doing syntactic
1682;; fontification because that breaks \ as escape character in strings.
1683;;
1684;; Special care is taken to restore the original syntax, because we
1685;; want \ not to be word for functions like forward-word.
1686(defadvice font-lock-fontify-keywords-region (around backslash-as-word activate)
1687 "Fontify keywords with backslash as word character."
1688 (let ((old-syntax (string (char-syntax ?\\))))
1689 (modify-syntax-entry ?\\ "w")
1690 ad-do-it
1691 (modify-syntax-entry ?\\ old-syntax)))
1692 1683
1693 1684
1694(defcustom php-class-suffix-when-insert "::" 1685(defcustom php-class-suffix-when-insert "::"