From 131a54ee37028f709a5f77e04513e9a80535e8c7 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Wed, 8 Nov 2017 19:08:11 +0100 Subject: Upgrade php-mode.el --- .emacs.d/lisp/php-mode.el | 247 ++++++++++++++++++++++------------------------ 1 file 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." :type 'string) ;;;###autoload -(add-to-list 'interpreter-mode-alist - ;; Match php, php-3, php5, php7, php5.5, php-7.0.1, etc. - (cons "php\\(?:-?[3457]\\(?:\\.[0-9]+\\)*\\)?" 'php-mode)) +(if (version< emacs-version "24.4") + (dolist (i '("php" "php5" "php7")) + (add-to-list 'interpreter-mode-alist (cons i 'php-mode))) + (add-to-list 'interpreter-mode-alist + ;; Match php, php-3, php5, php7, php5.5, php-7.0.1, etc. + (cons "php\\(?:-?[3457]\\(?:\\.[0-9]+\\)*\\)?" 'php-mode))) (defcustom php-mode-hook nil "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: (when (eq major-mode 'php-mode) (set sym value) (set-default sym value) - (cond ((eq value 'pear) - (php-enable-pear-coding-style)) - ((eq value 'default) - (php-enable-default-coding-style)) - ((eq value 'drupal) - (php-enable-drupal-coding-style)) - ((eq value 'wordpress) - (php-enable-wordpress-coding-style)) - ((eq value 'symfony2) - (php-enable-symfony2-coding-style)) - ((eq value 'psr2) - (php-enable-psr2-coding-style))))) + (php-set-style (symbol-name value)))) (defun php-mode-version () "Display string describing the version of PHP Mode." @@ -423,6 +415,7 @@ This variable can take one of the following symbol values: ;; c-end-of-defun to something other than C-M-e. (define-key map [remap c-beginning-of-defun] 'php-beginning-of-defun) (define-key map [remap c-end-of-defun] 'php-end-of-defun) + (define-key map [remap c-set-style] 'php-set-style) (define-key map [(control c) (control f)] 'php-search-documentation) (define-key map [(meta tab)] 'php-complete-function) @@ -436,6 +429,34 @@ This variable can take one of the following symbol values: map) "Keymap for `php-mode'") +(c-lang-defconst c-get-state-before-change-functions + ;; const might be a symbol in older versions + php (let ((const (c-lang-const c-get-state-before-change-functions))) + (cl-set-difference (if (listp const) const (list const)) + '(c-parse-quotes-before-change)))) + +(defun php-unescape-identifiers (beg end &optional old-len) + "Change syntax of backslashes in identifiers between BEG and END, ignore OLD-LEN." + (c-save-buffer-state (num-beg num-end) + (save-restriction + (goto-char c-new-BEG) + (while (and (< (point) c-new-END) + (search-forward "\\" c-new-END 'limit)) + (if (and (not (php-in-string-p)) + (looking-at-p c-identifier-key)) + ;; within function `c-forward-name' when looking at + ;; `c-identifier-key' ensure that `c-simple-skip-symbol-backward' + ;; skips over backslashes too in making it at word entry. + (c-put-char-property (1- (point)) 'syntax-table '(2))))))) + +(c-lang-defconst c-before-font-lock-functions + ;; const might be a symbol in older versions + php (let ((const (c-lang-const c-before-font-lock-functions))) + (append (cl-set-difference (if (listp const) const (list const)) + '(c-restore-<>-properties + c-parse-quotes-after-change)) + '(php-unescape-identifiers)))) + (c-lang-defconst c-mode-menu php (append '(["Complete function name" php-complete-function t] ["Browse manual" php-browse-manual t] @@ -624,7 +645,7 @@ but only if the setting is enabled" (c-add-style "php" - '((c-basic-offset . 4) + `((c-basic-offset . 4) (c-offsets-alist . ((arglist-close . php-lineup-arglist-close) (arglist-cont . (first php-lineup-cascaded-calls 0)) (arglist-cont-nonempty . (first php-lineup-cascaded-calls c-lineup-arglist)) @@ -639,103 +660,90 @@ but only if the setting is enabled" (label . +) (statement-cont . (first php-lineup-cascaded-calls php-lineup-string-cont +)) (substatement-open . 0) - (topmost-intro-cont . (first php-lineup-cascaded-calls +)))))) + (topmost-intro-cont . (first php-lineup-cascaded-calls +)))) + (indent-tabs-mode . nil) + (tab-width . ,(default-value 'tab-width)) + (fill-column . ,(default-value 'fill-column)) + (require-final-newline . ,(default-value 'require-final-newline)) + (show-trailing-whitespace . ,(default-value 'show-trailing-whitespace)) + (php-style-delete-trailing-whitespace . nil))) (defun php-enable-default-coding-style () "Set PHP Mode to use reasonable default formatting." (interactive) - (c-set-style "php")) + (php-set-style "php")) (c-add-style "pear" '("php" (c-basic-offset . 4) - (c-offsets-alist . ((case-label . 0))))) + (c-offsets-alist . ((case-label . 0))) + (tab-width . 4))) (defun php-enable-pear-coding-style () "Sets up php-mode to use the coding styles preferred for PEAR code and modules." (interactive) - (setq tab-width 4 - indent-tabs-mode nil) - (c-set-style "pear") - - ;; Undo drupal/PSR-2 coding style whitespace effects - (set (make-local-variable 'show-trailing-whitespace) - (default-value 'show-trailing-whitespace))) + (php-set-style "pear")) (c-add-style "drupal" '("php" - (c-basic-offset . 2))) + (c-basic-offset . 2) + (tab-width . 2) + (fill-column . 78) + (show-trailing-whitespace . t) + (php-style-delete-trailing-whitespace . t))) (defun php-enable-drupal-coding-style () "Makes php-mode use coding styles that are preferable for working with Drupal." (interactive) - (setq tab-width 2 - indent-tabs-mode nil - fill-column 78) - (set (make-local-variable 'show-trailing-whitespace) t) - (add-hook 'before-save-hook 'delete-trailing-whitespace nil t) - (c-set-style "drupal")) + (php-set-style "drupal")) (c-add-style "wordpress" '("php" - (c-basic-offset . 4))) + (c-basic-offset . 4) + (c-indent-comments-syntactically-p t) + (indent-tabs-mode . t) + (tab-width . 4) + (fill-column . 78))) (defun php-enable-wordpress-coding-style () "Makes php-mode use coding styles that are preferable for working with Wordpress." (interactive) - (setq indent-tabs-mode t - fill-column 78 - tab-width 4 - c-indent-comments-syntactically-p t) - (c-set-style "wordpress") - - ;; Undo drupal/PSR-2 coding style whitespace effects - (set (make-local-variable 'show-trailing-whitespace) - (default-value 'show-trailing-whitespace))) + (php-set-style "wordpress")) (c-add-style "symfony2" '("php" - (c-offsets-alist . ((statement-cont . php-lineup-hanging-semicolon))))) + (c-offsets-alist . ((statement-cont . php-lineup-hanging-semicolon))) + (c-indent-comments-syntactically-p . t) + (fill-column . 78) + (require-final-newline . t))) (defun php-enable-symfony2-coding-style () "Makes php-mode use coding styles that are preferable for working with Symfony2." (interactive) - (setq indent-tabs-mode nil - fill-column 78 - c-indent-comments-syntactically-p t - require-final-newline t) - (c-set-style "symfony2") - - ;; Undo drupal/PSR-2 coding style whitespace effects - (set (make-local-variable 'show-trailing-whitespace) - (default-value 'show-trailing-whitespace))) + (php-set-style "symfony2")) (c-add-style "psr2" '("php" - (c-offsets-alist . ((statement-cont . +))))) + (c-offsets-alist . ((statement-cont . +))) + (c-indent-comments-syntactically-p . t) + (fill-column . 78) + (require-final-newline . t) + (show-trailing-whitespace . t) + (php-style-delete-trailing-whitespace . t))) (defun php-enable-psr2-coding-style () "Makes php-mode comply to the PSR-2 coding style" (interactive) - (setq indent-tabs-mode nil - fill-column 78 - c-indent-comments-syntactically-p t - require-final-newline t) - (c-set-style "psr2") - - ;; Apply drupal-like coding style whitespace effects - (set (make-local-variable 'require-final-newline) t) - (set (make-local-variable 'show-trailing-whitespace) t) - (add-hook 'before-save-hook 'delete-trailing-whitespace nil t)) + (php-set-style "psr2")) (defconst php-beginning-of-defun-regexp "^\\s-*\\(?:\\(?:abstract\\|final\\|private\\|protected\\|public\\|static\\)\\s-+\\)*function\\s-+&?\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" @@ -949,9 +957,8 @@ the string HEREDOC-START." (defun php-syntax-propertize-function (start end) "Apply propertize rules from START to END." - ;; (defconst php-syntax-propertize-function - ;; (syntax-propertize-rules - ;; (php-heredoc-start-re (0 (ignore (php-heredoc-syntax)))))) + ;; versions < git-snapshot as of 2017-10 need this here + (php-unescape-identifiers start end) (goto-char start) (while (and (< (point) end) (re-search-forward php-heredoc-start-re end t)) @@ -1097,6 +1104,33 @@ PHP heredoc." (define-obsolete-face-alias 'php-annotations-annotation-face 'php-doc-annotation-tag "1.19.0") +(defun php-set-style (stylename &optional dont-override) + "Set the current `php-mode' buffer to use the style STYLENAME. +STYLENAME is one of the names selectable in `php-mode-coding-style'. + +Borrow the `interactive-form' from `c-set-style' and use the original +`c-set-style' function to set all declared stylevars. +For compatibility with `c-set-style' pass DONT-OVERRIDE to it. + +After setting the stylevars run hooks according to STYLENAME + + \"pear\" `php-mode-pear-hook' + \"drupal\" `php-mode-drupal-hook' + \"wordpress\" `php-mode-wordpress-hook' + \"symfony2\" `php-mode-symfony2-hook' + \"psr2\" `php-mode-psr2-hook'" + (interactive) + (c-set-style stylename dont-override) + (if (eq (symbol-value 'php-style-delete-trailing-whitespace) t) + (add-hook 'before-save-hook 'delete-trailing-whitespace nil t) + (remove-hook 'before-save-hook 'delete-trailing-whitespace t)) + (cond ((eq stylename "pear") (run-hooks 'php-mode-pear-hook)) + ((eq stylename "drupal") (run-hooks 'php-mode-drupal-hook)) + ((eq stylename "wordpress") (run-hooks 'php-mode-wordpress-hook)) + ((eq stylename "symfony2") (run-hooks 'php-mode-symfony2-hook)) + ((eq stylename "psr2") (run-hooks 'php-mode-psr2-hook)))) + +(put 'php-set-style 'interactive-form (interactive-form 'c-set-style)) ;;;###autoload (define-derived-mode php-mode c-mode "PHP" @@ -1116,16 +1150,10 @@ PHP heredoc." (set (make-local-variable font-lock-constant-face) 'php-constant) (modify-syntax-entry ?_ "_" php-mode-syntax-table) - (modify-syntax-entry ?` "\"" php-mode-syntax-table) - (modify-syntax-entry ?\" "\"" php-mode-syntax-table) (modify-syntax-entry ?# "< b" php-mode-syntax-table) (modify-syntax-entry ?\n "> b" php-mode-syntax-table) (modify-syntax-entry ?$ "'" php-mode-syntax-table) - (set (make-local-variable 'syntax-propertize-via-font-lock) - '(("\\(\"\\)\\(\\\\.\\|[^\"\n\\]\\)*\\(\"\\)" (1 "\"") (3 "\"")) - ("\\(\'\\)\\(\\\\.\\|[^\'\n\\]\\)*\\(\'\\)" (1 "\"") (3 "\"")))) - (add-to-list (make-local-variable 'syntax-propertize-extend-region-functions) #'php-syntax-propertize-extend-region) (set (make-local-variable 'syntax-propertize-function) @@ -1142,48 +1170,14 @@ PHP heredoc." (set (make-local-variable 'require-final-newline) nil) (set (make-local-variable 'next-line-add-newlines) nil) - ;; PEAR coding standards - (add-hook 'php-mode-pear-hook 'php-enable-pear-coding-style - nil t) - - ;; ;; Drupal coding standards - (add-hook 'php-mode-drupal-hook 'php-enable-drupal-coding-style - nil t) - - ;; ;; WordPress coding standards - (add-hook 'php-mode-wordpress-hook 'php-enable-wordpress-coding-style - nil t) - - ;; ;; Symfony2 coding standards - (add-hook 'php-mode-symfony2-hook 'php-enable-symfony2-coding-style - nil t) - - ;; ;; PSR-2 coding standards - (add-hook 'php-mode-psr2-hook 'php-enable-psr2-coding-style - nil t) - - (cond ((eq php-mode-coding-style 'pear) - (php-enable-pear-coding-style) - (run-hooks 'php-mode-pear-hook)) - ((eq php-mode-coding-style 'drupal) - (php-enable-drupal-coding-style) - (run-hooks 'php-mode-drupal-hook)) - ((eq php-mode-coding-style 'wordpress) - (php-enable-wordpress-coding-style) - (run-hooks 'php-mode-wordpress-hook)) - ((eq php-mode-coding-style 'symfony2) - (php-enable-symfony2-coding-style) - (run-hooks 'php-mode-symfony2-hook)) - ((eq php-mode-coding-style 'psr2) - (php-enable-psr2-coding-style) - (run-hooks 'php-mode-psr2-hook))) + (php-set-style (symbol-name php-mode-coding-style)) (if (or php-mode-force-pear (and (stringp buffer-file-name) (string-match "PEAR\\|pear" (buffer-file-name)) (string-match "\\.php$" (buffer-file-name)))) - (run-hooks 'php-mode-pear-hook)) + (php-set-style "pear")) (setq indent-line-function 'php-cautious-indent-line) (setq indent-region-function 'php-cautious-indent-region) @@ -1478,7 +1472,8 @@ a completion list." "callable" "iterable" "number")) (defconst php-phpdoc-type-tags - (list "param" "property" "property-read" "property-write" "return" "var")) + (list "package" "param" "property" "property-read" "property-write" + "return" "throws" "var")) (defconst php-phpdoc-font-lock-doc-comments `(("{@[-[:alpha:]]+\\s-\\([^}]*\\)}" ; "{@foo ...}" markup. @@ -1608,17 +1603,26 @@ a completion list." ;;; Provide support for Flymake so that users can see warnings and ;;; errors in real-time as they write code. -(defun flymake-php-init () - (let* ((temp-file (flymake-init-create-temp-buffer-copy - 'flymake-create-temp-inplace)) +(defun php-flymake-php-init () + "PHP specific init-cleanup routines. + +This is an alternative function of `flymake-php-init'. +Look at the `php-executable' variable instead of the constant \"php\" command." + (let* ((temp-file + (funcall + (eval-when-compile + (if (fboundp 'flymake-proc-init-create-temp-buffer-copy) + 'flymake-proc-init-create-temp-buffer-copy + 'flymake-init-create-temp-buffer-copy)) + 'flymake-create-temp-inplace)) (local-file (file-relative-name temp-file (file-name-directory buffer-file-name)))) (list php-executable (list "-f" local-file "-l")))) (add-to-list 'flymake-allowed-file-name-masks - '("\\.php[345s]?$" - flymake-php-init + '("\\.php[345s]?\\'" + php-flymake-php-init flymake-simple-cleanup flymake-get-real-file-name)) @@ -1676,19 +1680,6 @@ The output will appear in the buffer *PHP*." (ad-activate 'fixup-whitespace) -;; Advice `font-lock-fontify-keywords-region' to support namespace -;; separators in class names. Use word syntax for backslashes when -;; doing keyword fontification, but not when doing syntactic -;; fontification because that breaks \ as escape character in strings. -;; -;; Special care is taken to restore the original syntax, because we -;; want \ not to be word for functions like forward-word. -(defadvice font-lock-fontify-keywords-region (around backslash-as-word activate) - "Fontify keywords with backslash as word character." - (let ((old-syntax (string (char-syntax ?\\)))) - (modify-syntax-entry ?\\ "w") - ad-do-it - (modify-syntax-entry ?\\ old-syntax))) (defcustom php-class-suffix-when-insert "::" -- cgit v1.3