Subject: bug#76955: 30.1; php-ts-mode-php-executable default path may not match remote path

From: Vincenzo Pupillo <[email-masked]>
Date: Mon, 17 Mar 2025 22:27:28 +0100

Ciao Eli, this is the new patch. 
I did not write anything to /etc/NEWS in this patch, there is no new 30.2 
section. Do I need to create one?

In data giovedì 13 marzo 2025 17:47:42 Ora standard dell’Europa centrale, Eli 
Zaretskii ha scritto:
> > From: Vincenzo Pupillo <[email-masked]>
> > Cc: [email-masked], [email-masked]
> > Date: Thu, 13 Mar 2025 17:28:41 +0100
> > 
> > In data giovedì 13 marzo 2025 15:25:57 Ora standard dell’Europa centrale,
> > Eli> 
> > Zaretskii ha scritto:
> > > How about the following solution:
> > >  . we replace references to php-ts-mode-php-executable with a call to
> > >  
> > >    a function
> > >  
> > >  . that function will do something like
> > >  
> > >    (let ((php-prog (if (file-remote-p default-directory)
> > >    
> > >                        (executable-find "php" t)
> > > 		     
> > > 		     php-ts-mode-php-executable)))
> > >      
> > >      php-prog)
> > > 
> > > WDYT?
> > 
> > Yes, that's fine. Maybe we should also rename
> > 'php-ts-mode-php-executable',
> > something like 'php-ts-mode-php-default-executable'. It seems to me that
> > it
> > might not be clear that this value would not be used if the file was
> > remote.
> Yes, good idea.  Would you like to post a patch to that effect?

From 4232862c660215dc27e729a32c42e260bc7936aa Mon Sep 17 00:00:00 2001
From: Vincenzo Pupillo <[email-masked]>
Date: Fri, 14 Mar 2025 21:11:22 +0100
Subject: [PATCH] PHP should be in the PATH, either locally or remotely.
 (bug#76242).
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/progmodes/php-ts-mode.el
(php-ts-mode-php-default-executable): Renamed from
'php-ts-mode-php-executable'.
(php-ts-mode--executable): New function that returns the absolute
filename of the PHP executable, local or remote, based on
'default-directory'.
(php-ts-mode--anchor-prev-sibling): Replaced 'when-let' with
“when-let*.”
(php-ts-mode--indent-defun): Replaced 'when-let' with
'when-let*'.
(php-ts-mode-run-php-webserver): Use the new function
(php-ts-mode-php-default-executable).
(run-php): Use the new function (php-ts-mode-php-default-executable).
---
 lisp/progmodes/php-ts-mode.el | 43 ++++++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/lisp/progmodes/php-ts-mode.el b/lisp/progmodes/php-ts-mode.el
index 56c15dad36f..2ba04b09f06 100644
--- a/lisp/progmodes/php-ts-mode.el
+++ b/lisp/progmodes/php-ts-mode.el
@@ -134,8 +134,8 @@ php-ts-mode-css-fontify-colors
   :type 'boolean
   :safe 'booleanp)
 
-(defcustom php-ts-mode-php-executable (or (executable-find "php") "/usr/bin/php")
-  "The location of PHP executable."
+(defcustom php-ts-mode-php-default-executable (or (executable-find "php") "/usr/bin/php")
+  "The default PHP executable."
   :tag "PHP Executable"
   :version "30.1"
   :type 'file)
@@ -270,7 +270,7 @@ php-ts-mode-flymake-php
              :noquery t
              :connection-type 'pipe
              :buffer (generate-new-buffer " *php-ts-mode-flymake*")
-             :command `(,php-ts-mode-php-executable
+             :command `(,(php-ts-mode--executable)
                         "-l" "-d" "display_errors=0")
              :sentinel
              (lambda (proc _event)
@@ -306,6 +306,16 @@ php-ts-mode-flymake-php
 
 ;;; Utils
 
+(defun php-ts-mode--executable ()
+  "Return the absolute filename of the php executable.
+If the `default-directory' is remote, search on a remote host,
+otherwise return `php-ts-mode-php-default-executable'."
+  (let ((php-prog
+         (if (file-remote-p default-directory)
+             (executable-find "php" t)
+           php-ts-mode-php-default-executable)))
+    php-prog))
+
 (defun php-ts-mode--get-indent-style ()
   "Helper function to set indentation style.
 MODE can be `psr2', `pear', `drupal', `wordpress', `symfony', `zend'."
@@ -595,7 +605,7 @@ php-ts-mode--anchor-prev-sibling
 
 PARENT is NODE's parent, BOL is the beginning of non-whitespace
 characters of the current line."
-  (when-let ((prev-sibling
+  (when-let* ((prev-sibling
               (or (treesit-node-prev-sibling node t)
                   (treesit-node-prev-sibling
                    (treesit-node-first-child-for-pos parent bol) t)
@@ -1236,7 +1246,7 @@ php-ts-mode--indent-defun
   "Indent the current top-level declaration syntactically.
 `treesit-defun-type-regexp' defines what constructs to indent."
   (interactive "*")
-  (when-let ((orig-point (point-marker))
+  (when-let* ((orig-point (point-marker))
              (node (treesit-defun-at-point)))
     (indent-region (treesit-node-start node)
                    (treesit-node-end node))
@@ -1613,7 +1623,7 @@ php-ts-mode-run-php-webserver
       (message "Run PHP built-in web server with args %s into buffer %s"
                (string-join args " ")
                buf-name)
-      (apply #'make-comint name php-ts-mode-php-executable nil args))
+      (apply #'make-comint name (php-ts-mode--executable) nil args))
     (funcall
      (if (called-interactively-p 'interactive) #'display-buffer #'get-buffer)
      buf-name)))
@@ -1677,18 +1687,19 @@ run-php
 Optional CONFIG, if supplied, is the php.ini file to use."
   (interactive (when current-prefix-arg
                  (list
-                  (read-string "Run PHP: " php-ts-mode-php-executable)
+                  (read-string "Run PHP: " (php-ts-mode--executable))
                   (expand-file-name
                    (read-file-name "With config: " php-ts-mode-php-config)))))
-  (let ((buffer (get-buffer-create php-ts-mode-inferior-php-buffer))
-        (cmd (or
-              cmd
-              php-ts-mode-php-executable
-              (read-string "Run PHP: " php-ts-mode-php-executable)))
-        (config (or
-                 config
-                 (and php-ts-mode-php-config
-                      (expand-file-name php-ts-mode-php-config)))))
+  (let* ((php-prog (php-ts-mode--executable))
+         (buffer (get-buffer-create php-ts-mode-inferior-php-buffer))
+         (cmd (or
+               cmd
+               php-prog
+               (read-string "Run PHP: " php-prog)))
+         (config (or
+                  config
+                  (and php-ts-mode-php-config
+                       (expand-file-name php-ts-mode-php-config)))))
     (unless (comint-check-proc buffer)
       (with-current-buffer buffer
         (inferior-php-ts-mode-startup cmd config)
-- 
2.48.1