Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make simplify php-check-html-for-indentation and deprecate multi-modes support #742

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 35 additions & 95 deletions lisp/php-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,12 @@ Turning this on will force PEAR rules on all PHP files."
:tag "PHP Mode Force Pear"
:type 'boolean)

(defcustom php-mode-warn-if-mumamo-off t
"Warn once per buffer if you try to indent a buffer without
mumamo-mode turned on. Detects if there are any HTML tags in the
buffer before warning, but this is is not very smart; e.g. if you
have any tags inside a PHP string, it will be fooled."
:tag "PHP Mode Warn If MuMaMo Off"
(defcustom php-mode-warn-if-html-template t
"When indenting HTML tags, warn and prompt to switch major mode for HTML template."
:tag "PHP Mode Warn If HTML Template"
:safe #'booleanp
:type '(choice (const :tag "Warn" t) (const "Don't warn" nil)))
(make-obsolete-variable 'php-mode-warn-if-mumamo-off 'php-mode-warn-if-html-template "2.0.0")

(defcustom php-mode-coding-style 'pear
"Select default coding style to use with `php-mode'.
Expand Down Expand Up @@ -811,105 +810,46 @@ See `php-beginning-of-defun'."

(defvar php-warned-bad-indent nil)

;; Do it but tell it is not good if html tags in buffer.
(defun php-check-html-for-indentation ()
(let ((html-tag-re "^\\s-*</?\\sw+.*?>")
(here (point)))
(goto-char (line-beginning-position))
(if (or (when (boundp 'mumamo-multi-major-mode) mumamo-multi-major-mode)
;; Fix-me: no idea how to check for mmm or multi-mode
(save-match-data
(not (or (re-search-forward html-tag-re (line-end-position) t)
(re-search-backward html-tag-re (line-beginning-position) t)))))
(prog1 t
(goto-char here))
(goto-char here)
(setq php-warned-bad-indent t)
(let* ((known-multi-libs '(("mumamo" mumamo (lambda () (nxhtml-mumamo)))
("mmm-mode" mmm-mode (lambda () (mmm-mode 1)))
("multi-mode" multi-mode (lambda () (multi-mode 1)))
("web-mode" web-mode (lambda () (web-mode)))))
(known-names (mapcar (lambda (lib) (car lib)) known-multi-libs))
(available-multi-libs (delq nil
(mapcar
(lambda (lib)
(when (locate-library (car lib)) lib))
known-multi-libs)))
(available-names (mapcar (lambda (lib) (car lib)) available-multi-libs))
(base-msg
(concat
"Indentation fails badly with mixed HTML/PHP in the HTML part in
plain `php-mode'. To get indentation to work you must use an
Emacs library that supports 'multiple major modes' in a buffer.
Parts of the buffer will then be in `php-mode' and parts in for
example `html-mode'. Known such libraries are:\n\t"
(mapconcat #'identity known-names ", ")
"\n"
(if available-multi-libs
(concat
"You have these available in your `load-path':\n\t"
(mapconcat #'identity available-names ", ")
"\n\n"
"Do you want to turn any of those on? ")
"You do not have any of those in your `load-path'.")))
(is-using-multi
(catch 'is-using
(dolist (lib available-multi-libs)
(when (and (boundp (cadr lib))
(symbol-value (cadr lib)))
(throw 'is-using t))))))
(unless is-using-multi
(if available-multi-libs
(if (not (y-or-n-p base-msg))
(message "Did not do indentation, but you can try again now if you want")
(let* ((name
(if (= 1 (length available-multi-libs))
(car available-names)
;; Minibuffer window is more than one line, fix that first:
(message "")
(completing-read "Choose multiple major mode support library: "
available-names nil t
(car available-names)
'(available-names . 1)
)))
(mode (when name
(cl-caddr (assoc name available-multi-libs)))))
(when mode
;; Minibuffer window is more than one line, fix that first:
(message "")
(load name)
(funcall mode))))
(lwarn 'php-indent :warning base-msg)))
nil))))
"Do it but tell it is not good if html tags in buffer."
(cond
((not php-mode-warn-if-html-template) t)
((php-in-poly-php-html-mode) nil)
((not (php-buffer-has-html-tag)) t)
(php-warned-bad-indent nil)
((fboundp php-html-template-major-mode)
(if (y-or-n-p (format "This file seems to contains HTML tag. Switch to %s? "
php-html-template-major-mode))
(funcall php-html-template-major-mode)
(prog1 nil
(setq-local php-warned-bad-indent t))))
(t ;; Suppress warnings in Emacs session
(setq php-warned-bad-indent t)
(lwarn 'php-mode
:warning "Indentation fails badly with mixed HTML/PHP in the HTML part in plain `php-mode'.
It is highly recommended to install a major mode that supports PHP and HTML templates, such as Web Mode.

Set `php-html-template-major-mode' variable to use a mode other than `web-mode'.
Set `php-mode-warn-if-html-template' variable to nil to suppress the warning.
")
nil)))

(defun php-cautious-indent-region (start end &optional quiet)
"Carefully indent region START to END in contexts other than HTML templates.

If the optional argument QUIET is non-nil then no syntactic errors are
reported, even if `c-report-syntactic-errors' is non-nil."
(if (or (not php-mode-warn-if-mumamo-off)
(not (php-in-poly-php-html-mode))
php-warned-bad-indent
(php-check-html-for-indentation))
(funcall 'c-indent-region start end quiet)))
(when (php-check-html-for-indentation)
(c-indent-region start end quiet)))

(defun php-cautious-indent-line ()
"Carefully indent lines in contexts other than HTML templates."
(if (or (not php-mode-warn-if-mumamo-off)
(not (php-in-poly-php-html-mode))
php-warned-bad-indent
(php-check-html-for-indentation))
(let ((here (point))
doit)
(move-beginning-of-line nil)
;; Don't indent heredoc end mark
(save-match-data
(unless (and (looking-at "[a-zA-Z0-9_]+;\n")
(php-in-string-p))
(setq doit t)))
(goto-char here)
(when doit
(funcall 'c-indent-line)))))
(when (and (php-check-html-for-indentation)
(save-excursion
(beginning-of-line)
;; Don't indent heredoc end mark
(not (and (looking-at-p "[a-zA-Z0-9_]+;\n") (php-in-string-p)))))
(c-indent-line)))

(defun php-c-at-vsemi-p (&optional pos)
"Return T on HTML lines (including php tag) or PHP8 Attribute, otherwise NIL.
Expand Down