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