config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

with-editor.el (42782B)


      1 ;;; with-editor.el --- Use the Emacsclient as $EDITOR  -*- lexical-binding:t -*-
      2 
      3 ;; Copyright (C) 2014-2024 The Magit Project Contributors
      4 
      5 ;; Author: Jonas Bernoulli <emacs.with-editor@jonas.bernoulli.dev>
      6 ;; Homepage: https://github.com/magit/with-editor
      7 ;; Keywords: processes terminals
      8 
      9 ;; Package-Version: 3.4.1
     10 ;; Package-Requires: ((emacs "26.1") (compat "30.0.0.0"))
     11 
     12 ;; SPDX-License-Identifier: GPL-3.0-or-later
     13 
     14 ;; This file is free software: you can redistribute it and/or modify
     15 ;; it under the terms of the GNU General Public License as published
     16 ;; by the Free Software Foundation, either version 3 of the License,
     17 ;; or (at your option) any later version.
     18 ;;
     19 ;; This file is distributed in the hope that it will be useful,
     20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22 ;; GNU General Public License for more details.
     23 ;;
     24 ;; You should have received a copy of the GNU General Public License
     25 ;; along with this file.  If not, see <https://www.gnu.org/licenses/>.
     26 
     27 ;;; Commentary:
     28 
     29 ;; This library makes it possible to reliably use the Emacsclient as
     30 ;; the `$EDITOR' of child processes.  It makes sure that they know how
     31 ;; to call home.  For remote processes a substitute is provided, which
     32 ;; communicates with Emacs on standard output/input instead of using a
     33 ;; socket as the Emacsclient does.
     34 
     35 ;; It provides the commands `with-editor-async-shell-command' and
     36 ;; `with-editor-shell-command', which are intended as replacements
     37 ;; for `async-shell-command' and `shell-command'.  They automatically
     38 ;; export `$EDITOR' making sure the executed command uses the current
     39 ;; Emacs instance as "the editor".  With a prefix argument these
     40 ;; commands prompt for an alternative environment variable such as
     41 ;; `$GIT_EDITOR'.  To always use these variants add this to your init
     42 ;; file:
     43 ;;
     44 ;;   (keymap-global-set "<remap> <async-shell-command>"
     45 ;;                      #'with-editor-async-shell-command)
     46 ;;   (keymap-global-set "<remap> <shell-command>"
     47 ;;                      #'with-editor-shell-command)
     48 
     49 ;; Alternatively use the global `shell-command-with-editor-mode',
     50 ;; which always sets `$EDITOR' for all Emacs commands which ultimately
     51 ;; use `shell-command' to asynchronously run some shell command.
     52 
     53 ;; The command `with-editor-export-editor' exports `$EDITOR' or
     54 ;; another such environment variable in `shell-mode', `eshell-mode',
     55 ;; `term-mode' and `vterm-mode' buffers.  Use this Emacs command
     56 ;; before executing a shell command which needs the editor set, or
     57 ;; always arrange for the current Emacs instance to be used as editor
     58 ;; by adding it to the appropriate mode hooks:
     59 ;;
     60 ;;   (add-hook 'shell-mode-hook  #'with-editor-export-editor)
     61 ;;   (add-hook 'eshell-mode-hook #'with-editor-export-editor)
     62 ;;   (add-hook 'term-exec-hook   #'with-editor-export-editor)
     63 ;;   (add-hook 'vterm-mode-hook  #'with-editor-export-editor)
     64 
     65 ;; Some variants of this function exist, these two forms are
     66 ;; equivalent:
     67 ;;
     68 ;;   (add-hook 'shell-mode-hook
     69 ;;             (apply-partially #'with-editor-export-editor "GIT_EDITOR"))
     70 ;;   (add-hook 'shell-mode-hook #'with-editor-export-git-editor)
     71 
     72 ;; This library can also be used by other packages which need to use
     73 ;; the current Emacs instance as editor.  In fact this library was
     74 ;; written for Magit and its `git-commit-mode' and `git-rebase-mode'.
     75 ;; Consult `git-rebase.el' and the related code in `magit-sequence.el'
     76 ;; for a simple example.
     77 
     78 ;;; Code:
     79 
     80 (require 'cl-lib)
     81 (require 'compat)
     82 (require 'server)
     83 (require 'shell)
     84 (eval-when-compile (require 'subr-x))
     85 
     86 (declare-function dired-get-filename "dired"
     87                   (&optional localp no-error-if-not-filep))
     88 (declare-function term-emulate-terminal "term" (proc str))
     89 (defvar eshell-preoutput-filter-functions)
     90 (defvar git-commit-post-finish-hook)
     91 (defvar vterm--process)
     92 (defvar warning-minimum-level)
     93 (defvar warning-minimum-log-level)
     94 
     95 ;;; Options
     96 
     97 (defgroup with-editor nil
     98   "Use the Emacsclient as $EDITOR."
     99   :group 'external
    100   :group 'server)
    101 
    102 (defun with-editor-locate-emacsclient ()
    103   "Search for a suitable Emacsclient executable."
    104   (or (with-editor-locate-emacsclient-1
    105        (with-editor-emacsclient-path)
    106        (length (split-string emacs-version "\\.")))
    107       (prog1 nil (display-warning 'with-editor "\
    108 Cannot determine a suitable Emacsclient
    109 
    110 Determining an Emacsclient executable suitable for the
    111 current Emacs instance failed.  For more information
    112 please see https://github.com/magit/magit/wiki/Emacsclient."))))
    113 
    114 (defun with-editor-locate-emacsclient-1 (path depth)
    115   (let* ((version-lst (cl-subseq (split-string emacs-version "\\.") 0 depth))
    116          (version-reg (concat "^" (string-join version-lst "\\."))))
    117     (or (locate-file
    118          (cond ((equal (downcase invocation-name) "remacs")
    119                 "remacsclient")
    120                ((bound-and-true-p emacsclient-program-name))
    121                ("emacsclient"))
    122          path
    123          (cl-mapcan
    124           (lambda (v) (cl-mapcar (lambda (e) (concat v e)) exec-suffixes))
    125           (nconc (and (boundp 'debian-emacs-flavor)
    126                       (list (format ".%s" debian-emacs-flavor)))
    127                  (cl-mapcon (lambda (v)
    128                               (setq v (string-join (reverse v) "."))
    129                               (list v (concat "-" v) (concat ".emacs" v)))
    130                             (reverse version-lst))
    131                  (list "" "-snapshot" ".emacs-snapshot")))
    132          (lambda (exec)
    133            (ignore-errors
    134              (string-match-p version-reg
    135                              (with-editor-emacsclient-version exec)))))
    136         (and (> depth 1)
    137              (with-editor-locate-emacsclient-1 path (1- depth))))))
    138 
    139 (defun with-editor-emacsclient-version (exec)
    140   (let ((default-directory (file-name-directory exec)))
    141     (ignore-errors
    142       (cadr (split-string (car (process-lines exec "--version")))))))
    143 
    144 (defun with-editor-emacsclient-path ()
    145   (let ((path exec-path))
    146     (when invocation-directory
    147       (push (directory-file-name invocation-directory) path)
    148       (let* ((linkname (expand-file-name invocation-name invocation-directory))
    149              (truename (file-chase-links linkname)))
    150         (unless (equal truename linkname)
    151           (push (directory-file-name (file-name-directory truename)) path)))
    152       (when (eq system-type 'darwin)
    153         (let ((dir (expand-file-name "bin" invocation-directory)))
    154           (when (file-directory-p dir)
    155             (push dir path)))
    156         (when (string-search "Cellar" invocation-directory)
    157           (let ((dir (expand-file-name "../../../bin" invocation-directory)))
    158             (when (file-directory-p dir)
    159               (push dir path))))))
    160     (cl-remove-duplicates path :test #'equal)))
    161 
    162 (defcustom with-editor-emacsclient-executable (with-editor-locate-emacsclient)
    163   "The Emacsclient executable used by the `with-editor' macro."
    164   :group 'with-editor
    165   :type '(choice (string :tag "Executable")
    166                  (const  :tag "Don't use Emacsclient" nil)))
    167 
    168 (defcustom with-editor-sleeping-editor "\
    169 sh -c '\
    170 printf \"\\nWITH-EDITOR: $$ OPEN $0\\037$1\\037 IN $(pwd)\\n\"; \
    171 sleep 604800 & sleep=$!; \
    172 trap \"kill $sleep; exit 0\" USR1; \
    173 trap \"kill $sleep; exit 1\" USR2; \
    174 wait $sleep'"
    175   "The sleeping editor, used when the Emacsclient cannot be used.
    176 
    177 This fallback is used for asynchronous processes started inside
    178 the macro `with-editor', when the process runs on a remote machine
    179 or for local processes when `with-editor-emacsclient-executable'
    180 is nil (i.e., when no suitable Emacsclient was found, or the user
    181 decided not to use it).
    182 
    183 Where the latter uses a socket to communicate with Emacs' server,
    184 this substitute prints edit requests to its standard output on
    185 which a process filter listens for such requests.  As such it is
    186 not a complete substitute for a proper Emacsclient, it can only
    187 be used as $EDITOR of child process of the current Emacs instance.
    188 
    189 Some shells do not execute traps immediately when waiting for a
    190 child process, but by default we do use such a blocking child
    191 process.
    192 
    193 If you use such a shell (e.g., `csh' on FreeBSD, but not Debian),
    194 then you have to edit this option.  You can either replace \"sh\"
    195 with \"bash\" (and install that), or you can use the older, less
    196 performant implementation:
    197 
    198   \"sh -c '\\
    199   echo -e \\\"\\nWITH-EDITOR: $$ OPEN $0$1 IN $(pwd)\\n\\\"; \\
    200   trap \\\"exit 0\\\" USR1; \\
    201   trap \\\"exit 1\" USR2; \\
    202   while true; do sleep 1; done'\"
    203 
    204 Note that the two unit separator characters () right after $0
    205 and $1 are required.  Normally $0 is the file name and $1 is
    206 missing or else gets ignored.  But if $0 has the form \"+N[:N]\",
    207 then it is treated as a position in the file and $1 is expected
    208 to be the file.
    209 
    210 Also note that using this alternative implementation leads to a
    211 delay of up to a second.  The delay can be shortened by replacing
    212 \"sleep 1\" with \"sleep 0.01\", or if your implementation does
    213 not support floats, then by using \"nanosleep\" instead."
    214   :package-version '(with-editor . "2.8.0")
    215   :group 'with-editor
    216   :type 'string)
    217 
    218 (defcustom with-editor-finish-query-functions nil
    219   "List of functions called to query before finishing session.
    220 
    221 The buffer in question is current while the functions are called.
    222 If any of them returns nil, then the session is not finished and
    223 the buffer is not killed.  The user should then fix the issue and
    224 try again.  The functions are called with one argument.  If it is
    225 non-nil then that indicates that the user used a prefix argument
    226 to force finishing the session despite issues.  Functions should
    227 usually honor that and return non-nil."
    228   :group 'with-editor
    229   :type 'hook)
    230 (put 'with-editor-finish-query-functions 'permanent-local t)
    231 
    232 (defcustom with-editor-cancel-query-functions nil
    233   "List of functions called to query before canceling session.
    234 
    235 The buffer in question is current while the functions are called.
    236 If any of them returns nil, then the session is not canceled and
    237 the buffer is not killed.  The user should then fix the issue and
    238 try again.  The functions are called with one argument.  If it is
    239 non-nil then that indicates that the user used a prefix argument
    240 to force canceling the session despite issues.  Functions should
    241 usually honor that and return non-nil."
    242   :group 'with-editor
    243   :type 'hook)
    244 (put 'with-editor-cancel-query-functions 'permanent-local t)
    245 
    246 (defcustom with-editor-mode-lighter " WE"
    247   "The mode-line lighter of the With-Editor mode."
    248   :group 'with-editor
    249   :type '(choice (const :tag "No lighter" "") string))
    250 
    251 (defvar with-editor-server-window-alist nil
    252   "Alist of filename patterns vs corresponding `server-window'.
    253 
    254 Each element looks like (REGEXP . FUNCTION).  Files matching
    255 REGEXP are selected using FUNCTION instead of the default in
    256 `server-window'.
    257 
    258 Note that when a package adds an entry here then it probably
    259 has a reason to disrespect `server-window' and it likely is
    260 not a good idea to change such entries.")
    261 
    262 (defvar with-editor-file-name-history-exclude nil
    263   "List of regexps for filenames `server-visit' should not remember.
    264 When a filename matches any of the regexps, then `server-visit'
    265 does not add it to the variable `file-name-history', which is
    266 used when reading a filename in the minibuffer.")
    267 
    268 (defcustom with-editor-shell-command-use-emacsclient t
    269   "Whether to use the emacsclient when running shell commands.
    270 
    271 This affects `with-editor-async-shell-command' and, if the input
    272 ends with \"&\" `with-editor-shell-command' .
    273 
    274 If `shell-command-with-editor-mode' is enabled, then it also
    275 affects `shell-command-async' and, if the input ends with \"&\"
    276 `shell-command'.
    277 
    278 This is a temporary kludge that lets you choose between two
    279 possible defects, the ones described in the issues #23 and #40.
    280 
    281 When t, then use the emacsclient.  This has the disadvantage that
    282 `with-editor-mode' won't be enabled because we don't know whether
    283 this package was involved at all in the call to the emacsclient,
    284 and when it is not, then we really should.  The problem is that
    285 the emacsclient doesn't pass along any environment variables to
    286 the server.  This will hopefully be fixed in Emacs eventually.
    287 
    288 When nil, then use the sleeping editor.  Because in this case we
    289 know that this package is involved, we can enable the mode.  But
    290 this makes it necessary that you invoke $EDITOR in shell scripts
    291 like so:
    292 
    293   eval \"$EDITOR\" file
    294 
    295 And some tools that do not handle $EDITOR properly also break."
    296   :package-version '(with-editor . "2.7.1")
    297   :group 'with-editor
    298   :type 'boolean)
    299 
    300 ;;; Mode Commands
    301 
    302 (defvar with-editor-pre-finish-hook nil)
    303 (defvar with-editor-pre-cancel-hook nil)
    304 (defvar with-editor-post-finish-hook nil)
    305 (defvar with-editor-post-finish-hook-1 nil)
    306 (defvar with-editor-post-cancel-hook nil)
    307 (defvar with-editor-post-cancel-hook-1 nil)
    308 (defvar with-editor-cancel-alist nil)
    309 (put 'with-editor-pre-finish-hook 'permanent-local t)
    310 (put 'with-editor-pre-cancel-hook 'permanent-local t)
    311 (put 'with-editor-post-finish-hook 'permanent-local t)
    312 (put 'with-editor-post-cancel-hook 'permanent-local t)
    313 
    314 (defvar-local with-editor-show-usage t)
    315 (defvar-local with-editor-cancel-message nil)
    316 (defvar-local with-editor-previous-winconf nil)
    317 (put 'with-editor-cancel-message 'permanent-local t)
    318 (put 'with-editor-previous-winconf 'permanent-local t)
    319 
    320 (defvar-local with-editor--pid nil "For internal use.")
    321 (put 'with-editor--pid 'permanent-local t)
    322 
    323 (defun with-editor-finish (force)
    324   "Finish the current edit session."
    325   (interactive "P")
    326   (when (run-hook-with-args-until-failure
    327          'with-editor-finish-query-functions force)
    328     (let ((post-finish-hook with-editor-post-finish-hook)
    329           (post-commit-hook (bound-and-true-p git-commit-post-finish-hook))
    330           (dir default-directory))
    331       (run-hooks 'with-editor-pre-finish-hook)
    332       (with-editor-return nil)
    333       (accept-process-output nil 0.1)
    334       (with-temp-buffer
    335         (setq default-directory dir)
    336         (setq-local with-editor-post-finish-hook post-finish-hook)
    337         (when post-commit-hook
    338           (setq-local git-commit-post-finish-hook post-commit-hook))
    339         (run-hooks 'with-editor-post-finish-hook)))))
    340 
    341 (defun with-editor-cancel (force)
    342   "Cancel the current edit session."
    343   (interactive "P")
    344   (when (run-hook-with-args-until-failure
    345          'with-editor-cancel-query-functions force)
    346     (let ((message with-editor-cancel-message))
    347       (when (functionp message)
    348         (setq message (funcall message)))
    349       (let ((post-cancel-hook with-editor-post-cancel-hook)
    350             (with-editor-cancel-alist nil)
    351             (dir default-directory))
    352         (run-hooks 'with-editor-pre-cancel-hook)
    353         (with-editor-return t)
    354         (accept-process-output nil 0.1)
    355         (with-temp-buffer
    356           (setq default-directory dir)
    357           (setq-local with-editor-post-cancel-hook post-cancel-hook)
    358           (run-hooks 'with-editor-post-cancel-hook)))
    359       (message (or message "Canceled by user")))))
    360 
    361 (defun with-editor-return (cancel)
    362   (let ((winconf with-editor-previous-winconf)
    363         (clients server-buffer-clients)
    364         (dir default-directory)
    365         (pid with-editor--pid))
    366     (remove-hook 'kill-buffer-query-functions
    367                  #'with-editor-kill-buffer-noop t)
    368     (cond (cancel
    369            (save-buffer)
    370            (if clients
    371                (let ((buf (current-buffer)))
    372                  (dolist (client clients)
    373                    (message "client %S" client)
    374                    (ignore-errors
    375                      (server-send-string client "-error Canceled by user"))
    376                    (delete-process client))
    377                  (when (buffer-live-p buf)
    378                    (kill-buffer buf)))
    379              ;; Fallback for when emacs was used as $EDITOR
    380              ;; instead of emacsclient or the sleeping editor.
    381              ;; See https://github.com/magit/magit/issues/2258.
    382              (ignore-errors (delete-file buffer-file-name))
    383              (kill-buffer)))
    384           (t
    385            (save-buffer)
    386            (if clients
    387                ;; Don't use `server-edit' because we do not want to
    388                ;; show another buffer belonging to another client.
    389                ;; See https://github.com/magit/magit/issues/2197.
    390                (server-done)
    391              (kill-buffer))))
    392     (when pid
    393       (let ((default-directory dir))
    394         (process-file "kill" nil nil nil
    395                       "-s" (if cancel "USR2" "USR1") pid)))
    396     (when (and winconf (eq (window-configuration-frame winconf)
    397                            (selected-frame)))
    398       (set-window-configuration winconf))))
    399 
    400 ;;; Mode
    401 
    402 (defvar-keymap with-editor-mode-map
    403   "C-c C-c"                                #'with-editor-finish
    404   "<remap> <server-edit>"                  #'with-editor-finish
    405   "<remap> <evil-save-and-close>"          #'with-editor-finish
    406   "<remap> <evil-save-modified-and-close>" #'with-editor-finish
    407   "C-c C-k"                                #'with-editor-cancel
    408   "<remap> <kill-buffer>"                  #'with-editor-cancel
    409   "<remap> <ido-kill-buffer>"              #'with-editor-cancel
    410   "<remap> <iswitchb-kill-buffer>"         #'with-editor-cancel
    411   "<remap> <evil-quit>"                    #'with-editor-cancel)
    412 
    413 (define-minor-mode with-editor-mode
    414   "Edit a file as the $EDITOR of an external process."
    415   :lighter with-editor-mode-lighter
    416   ;; Protect the user from enabling or disabling the mode interactively.
    417   ;; Manually enabling the mode is dangerous because canceling the buffer
    418   ;; deletes the visited file.  The mode must not be disabled manually,
    419   ;; either `with-editor-finish' or `with-editor-cancel' must be used.
    420   :interactive nil                    ; >= 28.1
    421   (when (called-interactively-p 'any) ; <  28.1
    422     (setq with-editor-mode (not with-editor-mode))
    423     (user-error "With-Editor mode is not intended for interactive use"))
    424   ;; The buffer must also not be killed using regular kill commands.
    425   (add-hook 'kill-buffer-query-functions
    426             #'with-editor-kill-buffer-noop nil t)
    427   ;; `server-execute' displays a message which is not
    428   ;; correct when using this mode.
    429   (when with-editor-show-usage
    430     (with-editor-usage-message)))
    431 
    432 (put 'with-editor-mode 'permanent-local t)
    433 
    434 (defun with-editor-kill-buffer-noop ()
    435   ;; We started doing this in response to #64, but it is not safe
    436   ;; to do so, because the client has already been killed, causing
    437   ;; `with-editor-return' (called by `with-editor-cancel') to delete
    438   ;; the file, see #66.  The reason we delete the file in the first
    439   ;; place are https://github.com/magit/magit/issues/2258 and
    440   ;; https://github.com/magit/magit/issues/2248.
    441   ;; (if (memq this-command '(save-buffers-kill-terminal
    442   ;;                          save-buffers-kill-emacs))
    443   ;;     (let ((with-editor-cancel-query-functions nil))
    444   ;;       (with-editor-cancel nil)
    445   ;;       t)
    446   ;;   ...)
    447   ;; So go back to always doing this instead:
    448   (user-error (substitute-command-keys (format "\
    449 Don't kill this buffer %S.  Instead cancel using \\[with-editor-cancel]"
    450                                                (current-buffer)))))
    451 
    452 (defvar-local with-editor-usage-message "\
    453 Type \\[with-editor-finish] to finish, \
    454 or \\[with-editor-cancel] to cancel")
    455 
    456 (defun with-editor-usage-message ()
    457   ;; Run after `server-execute', which is run using
    458   ;; a timer which starts immediately.
    459   (let ((buffer (current-buffer)))
    460     (run-with-timer
    461      0.05 nil
    462      (lambda ()
    463        (with-current-buffer buffer
    464          (message (substitute-command-keys with-editor-usage-message)))))))
    465 
    466 ;;; Wrappers
    467 
    468 (defvar with-editor--envvar nil "For internal use.")
    469 
    470 (defmacro with-editor (&rest body)
    471   "Use the Emacsclient as $EDITOR while evaluating BODY.
    472 Modify the `process-environment' for processes started in BODY,
    473 instructing them to use the Emacsclient as $EDITOR.  If optional
    474 ENVVAR is a literal string then bind that environment variable
    475 instead.
    476 \n(fn [ENVVAR] BODY...)"
    477   (declare (indent defun) (debug (body)))
    478   `(let ((with-editor--envvar ,(if (stringp (car body))
    479                                    (pop body)
    480                                  '(or with-editor--envvar "EDITOR")))
    481          (process-environment process-environment))
    482      (with-editor--setup)
    483      ,@body))
    484 
    485 (defmacro with-editor* (envvar &rest body)
    486   "Use the Emacsclient as the editor while evaluating BODY.
    487 Modify the `process-environment' for processes started in BODY,
    488 instructing them to use the Emacsclient as editor.  ENVVAR is the
    489 environment variable that is exported to do so, it is evaluated
    490 at run-time.
    491 \n(fn [ENVVAR] BODY...)"
    492   (declare (indent defun) (debug (sexp body)))
    493   `(let ((with-editor--envvar ,envvar)
    494          (process-environment process-environment))
    495      (with-editor--setup)
    496      ,@body))
    497 
    498 (defun with-editor--setup ()
    499   (if (or (not with-editor-emacsclient-executable)
    500           (file-remote-p default-directory))
    501       (push (concat with-editor--envvar "=" with-editor-sleeping-editor)
    502             process-environment)
    503     ;; Make sure server-use-tcp's value is valid.
    504     (unless (featurep 'make-network-process '(:family local))
    505       (setq server-use-tcp t))
    506     ;; Make sure the server is running.
    507     (unless (process-live-p server-process)
    508       (when (server-running-p server-name)
    509         (setq server-name (format "server%s" (emacs-pid)))
    510         (when (server-running-p server-name)
    511           (server-force-delete server-name)))
    512       (server-start))
    513     ;; Tell $EDITOR to use the Emacsclient.
    514     (push (concat with-editor--envvar "="
    515                   ;; Quoting is the right thing to do.  Applications that
    516                   ;; fail because of that, are the ones that need fixing,
    517                   ;; e.g., by using 'eval "$EDITOR" file'.  See #121.
    518                   (shell-quote-argument
    519                    ;; If users set the executable manually, they might
    520                    ;; begin the path with "~", which would get quoted.
    521                    (if (string-prefix-p "~" with-editor-emacsclient-executable)
    522                        (concat (expand-file-name "~")
    523                                (substring with-editor-emacsclient-executable 1))
    524                      with-editor-emacsclient-executable))
    525                   ;; Tell the process where the server file is.
    526                   (and (not server-use-tcp)
    527                        (concat " --socket-name="
    528                                (shell-quote-argument
    529                                 (expand-file-name server-name
    530                                                   server-socket-dir)))))
    531           process-environment)
    532     (when server-use-tcp
    533       (push (concat "EMACS_SERVER_FILE="
    534                     (expand-file-name server-name server-auth-dir))
    535             process-environment))
    536     ;; As last resort fallback to the sleeping editor.
    537     (push (concat "ALTERNATE_EDITOR=" with-editor-sleeping-editor)
    538           process-environment)))
    539 
    540 (defun with-editor-server-window ()
    541   (or (and buffer-file-name
    542            (cdr (cl-find-if (lambda (cons)
    543                               (string-match-p (car cons) buffer-file-name))
    544                             with-editor-server-window-alist)))
    545       server-window))
    546 
    547 (define-advice server-switch-buffer
    548     (:around (fn &optional next-buffer &rest args)
    549              with-editor-server-window-alist)
    550   "Honor `with-editor-server-window-alist' (which see)."
    551   (let ((server-window (with-current-buffer
    552                            (or next-buffer (current-buffer))
    553                          (when with-editor-mode
    554                            (setq with-editor-previous-winconf
    555                                  (current-window-configuration)))
    556                          (with-editor-server-window))))
    557     (apply fn next-buffer args)))
    558 
    559 (define-advice start-file-process
    560     (:around (fn name buffer program &rest program-args)
    561              with-editor-process-filter)
    562   "When called inside a `with-editor' form and the Emacsclient
    563 cannot be used, then give the process the filter function
    564 `with-editor-process-filter'.  To avoid overriding the filter
    565 being added here you should use `with-editor-set-process-filter'
    566 instead of `set-process-filter' inside `with-editor' forms.
    567 
    568 When the `default-directory' is located on a remote machine,
    569 then also manipulate PROGRAM and PROGRAM-ARGS in order to set
    570 the appropriate editor environment variable."
    571   (if (not with-editor--envvar)
    572       (apply fn name buffer program program-args)
    573     (when (file-remote-p default-directory)
    574       (unless (equal program "env")
    575         (push program program-args)
    576         (setq program "env"))
    577       (push (concat with-editor--envvar "=" with-editor-sleeping-editor)
    578             program-args))
    579     (let ((process (apply fn name buffer program program-args)))
    580       (set-process-filter process #'with-editor-process-filter)
    581       (process-put process 'default-dir default-directory)
    582       process)))
    583 
    584 (advice-add #'make-process :around
    585             #'make-process@with-editor-process-filter)
    586 (cl-defun make-process@with-editor-process-filter
    587     (fn &rest keys &key name buffer command coding noquery stop
    588         connection-type filter sentinel stderr file-handler
    589         &allow-other-keys)
    590   "When called inside a `with-editor' form and the Emacsclient
    591 cannot be used, then give the process the filter function
    592 `with-editor-process-filter'.  To avoid overriding the filter
    593 being added here you should use `with-editor-set-process-filter'
    594 instead of `set-process-filter' inside `with-editor' forms.
    595 
    596 When the `default-directory' is located on a remote machine and
    597 FILE-HANDLER is non-nil, then also manipulate COMMAND in order
    598 to set the appropriate editor environment variable."
    599   (if (or (not file-handler) (not with-editor--envvar))
    600       (apply fn keys)
    601     (when (file-remote-p default-directory)
    602       (unless (equal (car command) "env")
    603         (push "env" command))
    604       (push (concat with-editor--envvar "=" with-editor-sleeping-editor)
    605             (cdr command)))
    606     (let* ((filter (if filter
    607                        (lambda (process output)
    608                          (funcall filter process output)
    609                          (with-editor-process-filter process output t))
    610                      #'with-editor-process-filter))
    611            (process (funcall fn
    612                              :name name
    613                              :buffer buffer
    614                              :command command
    615                              :coding coding
    616                              :noquery noquery
    617                              :stop stop
    618                              :connection-type connection-type
    619                              :filter filter
    620                              :sentinel sentinel
    621                              :stderr stderr
    622                              :file-handler file-handler)))
    623       (process-put process 'default-dir default-directory)
    624       process)))
    625 
    626 (defun with-editor-set-process-filter (process filter)
    627   "Like `set-process-filter' but keep `with-editor-process-filter'.
    628 Give PROCESS the new FILTER but keep `with-editor-process-filter'
    629 if that was added earlier by the advised `start-file-process'.
    630 
    631 Do so by wrapping the two filter functions using a lambda, which
    632 becomes the actual filter.  It calls FILTER first, which may or
    633 may not insert the text into the PROCESS's buffer.  Then it calls
    634 `with-editor-process-filter', passing t as NO-STANDARD-FILTER."
    635   (set-process-filter
    636    process
    637    (if (eq (process-filter process) 'with-editor-process-filter)
    638        `(lambda (proc str)
    639           (,filter proc str)
    640           (with-editor-process-filter proc str t))
    641      filter)))
    642 
    643 (defvar with-editor-filter-visit-hook nil)
    644 
    645 (defconst with-editor-sleeping-editor-regexp "^\
    646 WITH-EDITOR: \\([0-9]+\\) \
    647 OPEN \\([^]+?\\)\
    648 \\(?:\\([^]*\\)\\)?\
    649 \\(?: IN \\([^\r]+?\\)\\)?\r?$")
    650 
    651 (defvar with-editor--max-incomplete-length 1000)
    652 
    653 (defun with-editor-sleeping-editor-filter (process string)
    654   (when-let ((incomplete (and process (process-get process 'incomplete))))
    655     (setq string (concat incomplete string)))
    656   (save-match-data
    657     (cond
    658      ((and process (not (string-suffix-p "\n" string)))
    659       (let ((length (length string)))
    660         (when (> length with-editor--max-incomplete-length)
    661           (setq string
    662                 (substring string
    663                            (- length with-editor--max-incomplete-length)))))
    664       (process-put process 'incomplete string)
    665       nil)
    666      ((string-match with-editor-sleeping-editor-regexp string)
    667       (when process
    668         (process-put process 'incomplete nil))
    669       (let ((pid  (match-string 1 string))
    670             (arg0 (match-string 2 string))
    671             (arg1 (match-string 3 string))
    672             (dir  (match-string 4 string))
    673             file line column)
    674         (cond ((string-match "\\`\\+\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?\\'" arg0)
    675                (setq file arg1)
    676                (setq line (string-to-number (match-string 1 arg0)))
    677                (setq column (match-string 2 arg0))
    678                (setq column (and column (string-to-number column))))
    679               ((setq file arg0)))
    680         (unless (file-name-absolute-p file)
    681           (setq file (expand-file-name file dir)))
    682         (when default-directory
    683           (setq file (concat (file-remote-p default-directory) file)))
    684         (with-current-buffer (find-file-noselect file)
    685           (with-editor-mode 1)
    686           (setq with-editor--pid pid)
    687           (setq with-editor-previous-winconf
    688                 (current-window-configuration))
    689           (when line
    690             (let ((pos (save-excursion
    691                          (save-restriction
    692                            (goto-char (point-min))
    693                            (forward-line (1- line))
    694                            (when column
    695                              (move-to-column column))
    696                            (point)))))
    697               (when (and (buffer-narrowed-p)
    698                          widen-automatically
    699                          (not (<= (point-min) pos (point-max))))
    700                 (widen))
    701               (goto-char pos)))
    702           (run-hooks 'with-editor-filter-visit-hook)
    703           (funcall (or (with-editor-server-window) #'switch-to-buffer)
    704                    (current-buffer))
    705           (kill-local-variable 'server-window)))
    706       nil)
    707      (t string))))
    708 
    709 (defun with-editor-process-filter
    710     (process string &optional no-default-filter)
    711   "Listen for edit requests by child processes."
    712   (let ((default-directory (process-get process 'default-dir)))
    713     (with-editor-sleeping-editor-filter process string))
    714   (unless no-default-filter
    715     (internal-default-process-filter process string)))
    716 
    717 (define-advice server-visit-files
    718     (:after (files _proc &optional _nowait)
    719             with-editor-file-name-history-exclude)
    720   "Prevent certain files from being added to `file-name-history'.
    721 Files matching a regexp in `with-editor-file-name-history-exclude'
    722 are prevented from being added to that list."
    723   (pcase-dolist (`(,file . ,_) files)
    724     (when (cl-find-if (lambda (regexp)
    725                         (string-match-p regexp file))
    726                       with-editor-file-name-history-exclude)
    727       (setq file-name-history
    728             (delete (abbreviate-file-name file) file-name-history)))))
    729 
    730 ;;; Augmentations
    731 
    732 ;;;###autoload
    733 (cl-defun with-editor-export-editor (&optional (envvar "EDITOR"))
    734   "Teach subsequent commands to use current Emacs instance as editor.
    735 
    736 Set and export the environment variable ENVVAR, by default
    737 \"EDITOR\".  The value is automatically generated to teach
    738 commands to use the current Emacs instance as \"the editor\".
    739 
    740 This works in `shell-mode', `term-mode', `eshell-mode' and
    741 `vterm'."
    742   (interactive (list (with-editor-read-envvar)))
    743   (cond
    744    ((derived-mode-p 'comint-mode 'term-mode)
    745     (when-let ((process (get-buffer-process (current-buffer))))
    746       (goto-char (process-mark process))
    747       (process-send-string
    748        process (format " export %s=%s\n" envvar
    749                        (shell-quote-argument with-editor-sleeping-editor)))
    750       (while (accept-process-output process 0.1))
    751       (if (derived-mode-p 'term-mode)
    752           (with-editor-set-process-filter process #'with-editor-emulate-terminal)
    753         (add-hook 'comint-output-filter-functions #'with-editor-output-filter
    754                   nil t))))
    755    ((derived-mode-p 'eshell-mode)
    756     (add-to-list 'eshell-preoutput-filter-functions
    757                  #'with-editor-output-filter)
    758     (setenv envvar with-editor-sleeping-editor))
    759    ((and (derived-mode-p 'vterm-mode)
    760          (fboundp 'vterm-send-return)
    761          (fboundp 'vterm-send-string))
    762     (if with-editor-emacsclient-executable
    763         (let ((with-editor--envvar envvar)
    764               (process-environment process-environment))
    765           (with-editor--setup)
    766           (while (accept-process-output vterm--process 0.1))
    767           (when-let ((v (getenv envvar)))
    768             (vterm-send-string (format " export %s=%S" envvar v))
    769             (vterm-send-return))
    770           (when-let ((v (getenv "EMACS_SERVER_FILE")))
    771             (vterm-send-string (format " export EMACS_SERVER_FILE=%S" v))
    772             (vterm-send-return))
    773           (vterm-send-string "clear")
    774           (vterm-send-return))
    775       (error "Cannot use sleeping editor in this buffer")))
    776    (t
    777     (error "Cannot export environment variables in this buffer")))
    778   (message "Successfully exported %s" envvar))
    779 
    780 ;;;###autoload
    781 (defun with-editor-export-git-editor ()
    782   "Like `with-editor-export-editor' but always set `$GIT_EDITOR'."
    783   (interactive)
    784   (with-editor-export-editor "GIT_EDITOR"))
    785 
    786 ;;;###autoload
    787 (defun with-editor-export-hg-editor ()
    788   "Like `with-editor-export-editor' but always set `$HG_EDITOR'."
    789   (interactive)
    790   (with-editor-export-editor "HG_EDITOR"))
    791 
    792 (defun with-editor-output-filter (string)
    793   "Handle edit requests on behalf of `comint-mode' and `eshell-mode'."
    794   (with-editor-sleeping-editor-filter nil string))
    795 
    796 (defun with-editor-emulate-terminal (process string)
    797   "Like `term-emulate-terminal' but also handle edit requests."
    798   (let ((with-editor-sleeping-editor-regexp
    799          (substring with-editor-sleeping-editor-regexp 1)))
    800     (with-editor-sleeping-editor-filter process string))
    801   (term-emulate-terminal process string))
    802 
    803 (defvar with-editor-envvars '("EDITOR" "GIT_EDITOR" "HG_EDITOR"))
    804 
    805 (cl-defun with-editor-read-envvar
    806     (&optional (prompt  "Set environment variable")
    807                (default "EDITOR"))
    808   (let ((reply (completing-read (if default
    809                                     (format "%s (%s): " prompt default)
    810                                   (concat prompt ": "))
    811                                 with-editor-envvars nil nil nil nil default)))
    812     (if (string= reply "") (user-error "Nothing selected") reply)))
    813 
    814 ;;;###autoload
    815 (define-minor-mode shell-command-with-editor-mode
    816   "Teach `shell-command' to use current Emacs instance as editor.
    817 
    818 Teach `shell-command', and all commands that ultimately call that
    819 command, to use the current Emacs instance as editor by executing
    820 \"EDITOR=CLIENT COMMAND&\" instead of just \"COMMAND&\".
    821 
    822 CLIENT is automatically generated; EDITOR=CLIENT instructs
    823 COMMAND to use to the current Emacs instance as \"the editor\",
    824 assuming no other variable overrides the effect of \"$EDITOR\".
    825 CLIENT may be the path to an appropriate emacsclient executable
    826 with arguments, or a script which also works over Tramp.
    827 
    828 Alternatively you can use the `with-editor-async-shell-command',
    829 which also allows the use of another variable instead of
    830 \"EDITOR\"."
    831   :global t)
    832 
    833 ;;;###autoload
    834 (defun with-editor-async-shell-command
    835     (command &optional output-buffer error-buffer envvar)
    836   "Like `async-shell-command' but with `$EDITOR' set.
    837 
    838 Execute string \"ENVVAR=CLIENT COMMAND\" in an inferior shell;
    839 display output, if any.  With a prefix argument prompt for an
    840 environment variable, otherwise the default \"EDITOR\" variable
    841 is used.  With a negative prefix argument additionally insert
    842 the COMMAND's output at point.
    843 
    844 CLIENT is automatically generated; ENVVAR=CLIENT instructs
    845 COMMAND to use to the current Emacs instance as \"the editor\",
    846 assuming it respects ENVVAR as an \"EDITOR\"-like variable.
    847 CLIENT may be the path to an appropriate emacsclient executable
    848 with arguments, or a script which also works over Tramp.
    849 
    850 Also see `async-shell-command' and `shell-command'."
    851   (interactive (with-editor-shell-command-read-args "Async shell command: " t))
    852   (let ((with-editor--envvar envvar))
    853     (with-editor
    854       (async-shell-command command output-buffer error-buffer))))
    855 
    856 ;;;###autoload
    857 (defun with-editor-shell-command
    858     (command &optional output-buffer error-buffer envvar)
    859   "Like `shell-command' or `with-editor-async-shell-command'.
    860 If COMMAND ends with \"&\" behave like the latter,
    861 else like the former."
    862   (interactive (with-editor-shell-command-read-args "Shell command: "))
    863   (if (string-match "&[ \t]*\\'" command)
    864       (with-editor-async-shell-command
    865        command output-buffer error-buffer envvar)
    866     (shell-command command output-buffer error-buffer)))
    867 
    868 (defun with-editor-shell-command-read-args (prompt &optional async)
    869   (let ((command (read-shell-command
    870                   prompt nil nil
    871                   (let ((filename (or buffer-file-name
    872                                       (and (eq major-mode 'dired-mode)
    873                                            (dired-get-filename nil t)))))
    874                     (and filename (file-relative-name filename))))))
    875     (list command
    876           (if (or async (setq async (string-match-p "&[ \t]*\\'" command)))
    877               (< (prefix-numeric-value current-prefix-arg) 0)
    878             current-prefix-arg)
    879           shell-command-default-error-buffer
    880           (and async current-prefix-arg (with-editor-read-envvar)))))
    881 
    882 (define-advice shell-command
    883     (:around (fn command &optional output-buffer error-buffer)
    884              shell-command-with-editor-mode)
    885   "`shell-mode' and its hook are intended for buffers in which an
    886 interactive shell is running, but `shell-command' also turns on
    887 that mode, even though it only runs the shell to run a single
    888 command.  The `with-editor-export-editor' hook function is only
    889 intended to be used in buffers in which an interactive shell is
    890 running, so it has to be removed here."
    891   (let ((shell-mode-hook (remove 'with-editor-export-editor shell-mode-hook)))
    892     (cond ((or (not (or with-editor--envvar shell-command-with-editor-mode))
    893                (not (string-suffix-p "&" command)))
    894            (funcall fn command output-buffer error-buffer))
    895           ((and with-editor-shell-command-use-emacsclient
    896                 with-editor-emacsclient-executable
    897                 (not (file-remote-p default-directory)))
    898            (with-editor (funcall fn command output-buffer error-buffer)))
    899           (t
    900            (funcall fn (format "%s=%s %s"
    901                                (or with-editor--envvar "EDITOR")
    902                                (shell-quote-argument with-editor-sleeping-editor)
    903                                command)
    904                     output-buffer error-buffer)
    905            (ignore-errors
    906              (let ((process (get-buffer-process
    907                              (or output-buffer
    908                                  (get-buffer "*Async Shell Command*")))))
    909                (set-process-filter
    910                 process (lambda (proc str)
    911                           (comint-output-filter proc str)
    912                           (with-editor-process-filter proc str t)))
    913                process))))))
    914 
    915 ;;; _
    916 
    917 (defun with-editor-debug ()
    918   "Debug configuration issues.
    919 See info node `(with-editor)Debugging' for instructions."
    920   (interactive)
    921   (require 'warnings)
    922   (with-current-buffer (get-buffer-create "*with-editor-debug*")
    923     (pop-to-buffer (current-buffer))
    924     (erase-buffer)
    925     (ignore-errors (with-editor))
    926     (insert
    927      (format "with-editor: %s\n" (locate-library "with-editor.el"))
    928      (format "emacs: %s (%s)\n"
    929              (expand-file-name invocation-name invocation-directory)
    930              emacs-version)
    931      "system:\n"
    932      (format "  system-type: %s\n" system-type)
    933      (format "  system-configuration: %s\n" system-configuration)
    934      (format "  system-configuration-options: %s\n" system-configuration-options)
    935      "server:\n"
    936      (format "  server-running-p: %s\n" (server-running-p))
    937      (format "  server-process: %S\n" server-process)
    938      (format "  server-use-tcp: %s\n" server-use-tcp)
    939      (format "  server-name: %s\n" server-name)
    940      (format "  server-socket-dir: %s\n" server-socket-dir))
    941     (if (and server-socket-dir (file-accessible-directory-p server-socket-dir))
    942         (dolist (file (directory-files server-socket-dir nil "^[^.]"))
    943           (insert (format "    %s\n" file)))
    944       (insert (format "    %s: not an accessible directory\n"
    945                       (if server-use-tcp "WARNING" "ERROR"))))
    946     (insert (format "  server-auth-dir: %s\n" server-auth-dir))
    947     (if (file-accessible-directory-p server-auth-dir)
    948         (dolist (file (directory-files server-auth-dir nil "^[^.]"))
    949           (insert (format "    %s\n" file)))
    950       (insert (format "    %s: not an accessible directory\n"
    951                       (if server-use-tcp "ERROR" "WARNING"))))
    952     (let ((val with-editor-emacsclient-executable)
    953           (def (default-value 'with-editor-emacsclient-executable))
    954           (fun (let ((warning-minimum-level :error)
    955                      (warning-minimum-log-level :error))
    956                  (with-editor-locate-emacsclient))))
    957       (insert "with-editor-emacsclient-executable:\n"
    958               (format " value:   %s (%s)\n" val
    959                       (and val (with-editor-emacsclient-version val)))
    960               (format " default: %s (%s)\n" def
    961                       (and def (with-editor-emacsclient-version def)))
    962               (format " funcall: %s (%s)\n" fun
    963                       (and fun (with-editor-emacsclient-version fun)))))
    964     (insert "path:\n"
    965             (format "  $PATH:     %s\n" (split-string (getenv "PATH") ":"))
    966             (format "  exec-path: %s\n" exec-path))
    967     (insert (format "  with-editor-emacsclient-path:\n"))
    968     (dolist (dir (with-editor-emacsclient-path))
    969       (insert (format "    %s (%s)\n" dir (car (file-attributes dir))))
    970       (when (file-directory-p dir)
    971         ;; Don't match emacsclientw.exe, it makes popup windows.
    972         (dolist (exec (directory-files dir t "emacsclient\\(?:[^w]\\|\\'\\)"))
    973           (insert (format "      %s (%s)\n" exec
    974                           (with-editor-emacsclient-version exec))))))))
    975 
    976 (defconst with-editor-font-lock-keywords
    977   '(("(\\(with-\\(?:git-\\)?editor\\)\\_>" (1 'font-lock-keyword-face))))
    978 (font-lock-add-keywords 'emacs-lisp-mode with-editor-font-lock-keywords)
    979 
    980 (provide 'with-editor)
    981 ;; Local Variables:
    982 ;; indent-tabs-mode: nil
    983 ;; byte-compile-warnings: (not docstrings-control-chars)
    984 ;; End:
    985 ;;; with-editor.el ends here