Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

930 lines
39 KiB

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