dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

with-editor.texi (12824B)


      1 \input texinfo    @c -*- texinfo -*-
      2 @c %**start of header
      3 @setfilename with-editor.info
      4 @settitle With-Editor User Manual
      5 @documentencoding UTF-8
      6 @documentlanguage en
      7 @c %**end of header
      8 
      9 @copying
     10 @quotation
     11 Copyright (C) 2015-2023 Jonas Bernoulli <jonas@@bernoul.li>
     12 
     13 You can redistribute this document and/or modify it under the terms
     14 of the GNU General Public License as published by the Free Software
     15 Foundation, either version 3 of the License, or (at your option) any
     16 later version.
     17 
     18 This document 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 GNU
     21 General Public License for more details.
     22 
     23 @end quotation
     24 @end copying
     25 
     26 @dircategory Emacs
     27 @direntry
     28 * With-Editor: (with-editor). Using the Emacsclient as $EDITOR.
     29 @end direntry
     30 
     31 @finalout
     32 @titlepage
     33 @title With-Editor User Manual
     34 @subtitle for version 3.3.2
     35 @author Jonas Bernoulli
     36 @page
     37 @vskip 0pt plus 1filll
     38 @insertcopying
     39 @end titlepage
     40 
     41 @contents
     42 
     43 @ifnottex
     44 @node Top
     45 @top With-Editor User Manual
     46 
     47 The library @code{with-editor} makes it easy to use the Emacsclient as the
     48 @code{$EDITOR} of child processes, making sure they know how to call home.
     49 For remote processes a substitute is provided, which communicates with
     50 Emacs on standard output instead of using a socket as the Emacsclient
     51 does.
     52 
     53 This library was written because Magit has to be able to do the above
     54 to allow the user to edit commit messages gracefully and to edit
     55 rebase sequences, which wouldn't be possible at all otherwise.
     56 
     57 Because other packages can benefit from such functionality, this
     58 library is made available as a separate package.  It also defines some
     59 additional functionality which makes it useful even for end-users, who
     60 don't use Magit or another package which uses it internally.
     61 
     62 @noindent
     63 This manual is for With-Editor version 3.3.2.
     64 
     65 @insertcopying
     66 @end ifnottex
     67 
     68 @menu
     69 * Using the With-Editor package::
     70 * Using With-Editor as a library::
     71 * Debugging::
     72 * Function and Command Index::
     73 * Variable Index::
     74 
     75 @detailmenu
     76 --- The Detailed Node Listing ---
     77 
     78 Using the With-Editor package
     79 
     80 * Configuring With-Editor::
     81 * Using With-Editor commands::
     82 
     83 @end detailmenu
     84 @end menu
     85 
     86 @node Using the With-Editor package
     87 @chapter Using the With-Editor package
     88 
     89 The @code{With-Editor} package is used internally by Magit when editing
     90 commit messages and rebase sequences.  It also provides some commands
     91 and features which are useful by themselves, even if you don't use
     92 Magit.
     93 
     94 For information about using this library in you own package, see
     95 @ref{Using With-Editor as a library}.
     96 
     97 @menu
     98 * Configuring With-Editor::
     99 * Using With-Editor commands::
    100 @end menu
    101 
    102 @node Configuring With-Editor
    103 @section Configuring With-Editor
    104 
    105 With-Editor tries very hard to locate a suitable @code{emacsclient}
    106 executable, so ideally you should never have to customize the option
    107 @code{with-editor-emacsclient-executable}.  When it fails to do so, then the
    108 most likely reason is that someone found yet another way to package
    109 Emacs (most likely on macOS) without putting the executable on @code{$PATH},
    110 and we have to add another kludge to find it anyway.
    111 
    112 @defopt with-editor-emacsclient-executable
    113 The @code{emacsclient} executable used as the editor by child process of
    114 this Emacs instance.  By using this executable, child processes can
    115 call home to their parent process.
    116 
    117 This option is automatically set at startup by looking in @code{exec-path},
    118 and other places where the executable could be installed, to find
    119 the @code{emacsclient} executable most suitable for the current Emacs
    120 instance.
    121 
    122 You should @strong{not} customize this option permanently.  If you have to do
    123 it, then you should consider that a temporary kludge and inform the
    124 Magit maintainer as described in @ref{Debugging}.
    125 
    126 If With-Editor fails to find a suitable @code{emacsclient} on you system,
    127 then this should be fixed for all users at once, by teaching
    128 @code{with-editor-locate-emacsclient} how to do so on your system and
    129 system like yours. Doing it this way has the advantage, that you
    130 won't have do it again every time you update Emacs, and that other
    131 users who have installed Emacs the same way as you have, won't have
    132 to go through the same trouble.
    133 
    134 Note that there also is a nuclear option; setting this variable to
    135 @code{nil} causes the "sleeping editor" described below to be used even for
    136 local child processes.  Obviously we don't recommend that you use
    137 this except in "emergencies", i.e., before we had a change to add a
    138 kludge appropriate for you setup.
    139 @end defopt
    140 
    141 @defun with-editor-locate-emacsclient
    142 The function used to set the initial value of the option
    143 @code{with-editor-emacsclient-executable}.  There's a lot of voodoo here.
    144 @end defun
    145 
    146 The @code{emacsclient} cannot be used when using Tramp to run a process on a
    147 remote machine.  (Theoretically it could, but that would be hard to
    148 setup, very fragile, and rather insecure).
    149 
    150 With-Editor provides an alternative "editor" which can be used by
    151 remote processes in much the same way as local processes use an
    152 @code{emacsclient} executable.  This alternative is known as the "sleeping
    153 editor" because it is implemented as a shell script which sleeps until
    154 it receives a signal.
    155 
    156 @defopt with-editor-sleeping-editor
    157 The sleeping editor is a shell script used as the editor of child
    158 processes when the @code{emacsclient} executable cannot be used.
    159 
    160 This fallback is used for asynchronous process started inside the
    161 macro @code{with-editor}, when the process runs on a remote machine or for
    162 local processes when @code{with-editor-emacsclient-executable} is @code{nil}.
    163 
    164 Where the latter uses a socket to communicate with Emacs' server,
    165 this substitute prints edit requests to its standard output on
    166 which a process filter listens for such requests.  As such it is
    167 not a complete substitute for a proper @code{emacsclient}, it can only
    168 be used as @code{$EDITOR} of child process of the current Emacs instance.
    169 
    170 Some shells do not execute traps immediately when waiting for a
    171 child process, but by default we do use such a blocking child
    172 process.
    173 
    174 If you use such a shell (e.g., @code{csh} on FreeBSD, but not Debian), then
    175 you have to edit this option.  You can either replace @code{sh} with @code{bash}
    176 (and install that), or you can use the older, less performant
    177 implementation:
    178 
    179 @lisp
    180 "sh -c '\
    181 echo \"WITH-EDITOR: $$ OPEN $0$1 IN $(pwd)\"; \
    182 trap \"exit 0\" USR1; \
    183 trap \"exit 1\" USR2; \
    184 while true; do sleep 1; done'"
    185 @end lisp
    186 
    187 Note that the unit separator character () right after the file
    188 name ($0) is required.
    189 
    190 Also note that using this alternative implementation leads to a
    191 delay of up to a second.  The delay can be shortened by replacing
    192 @code{sleep 1} with @code{sleep 0.01}, or if your implementation does not support
    193 floats, then by using @code{nanosleep} instead.
    194 @end defopt
    195 
    196 @node Using With-Editor commands
    197 @section Using With-Editor commands
    198 
    199 This section describes how to use the @code{with-editor} library @emph{outside} of
    200 Magit.  You don't need to know any of this just to create commits
    201 using Magit.
    202 
    203 The commands @code{with-editor-async-shell-command} and
    204 @code{with-editor-shell-command} are intended as drop in replacements for
    205 @code{async-shell-command} and @code{shell-command}.  They automatically export
    206 @code{$EDITOR} making sure the executed command uses the current Emacs
    207 instance as "the editor".  With a prefix argument these commands
    208 prompt for an alternative environment variable such as @code{$GIT_EDITOR}.
    209 
    210 @deffn Command with-editor-async-shell-command
    211 This command is like @code{async-shell-command}, but it runs the shell
    212 command with the current Emacs instance exported as @code{$EDITOR}.
    213 @end deffn
    214 
    215 @deffn Command with-editor-shell-command
    216 This command is like @code{shell-command}, but if the shell command ends
    217 with @code{&} and is therefore run asynchronously, then the current Emacs
    218 instance is exported as @code{$EDITOR}.
    219 @end deffn
    220 
    221 To always use these variants add this to you init file:
    222 
    223 @lisp
    224 (define-key (current-global-map)
    225   [remap async-shell-command] 'with-editor-async-shell-command)
    226 (define-key (current-global-map)
    227   [remap shell-command] 'with-editor-shell-command)
    228 @end lisp
    229 
    230 Alternatively use the global @code{shell-command-with-editor-mode}.
    231 
    232 @defvar shell-command-with-editor-mode
    233 When this mode is active, then @code{$EDITOR} is exported whenever
    234 ultimately @code{shell-command} is called to asynchronously run some shell
    235 command.  This affects most variants of that command, whether they
    236 are defined in Emacs or in some third-party package.
    237 @end defvar
    238 
    239 The command @code{with-editor-export-editor} exports @code{$EDITOR} or another
    240 such environment variable in @code{shell-mode}, @code{eshell-mode}, @code{term-mode} and
    241 @code{vterm-mode} buffers.  Use this Emacs command before executing a shell
    242 command which needs the editor set, or always arrange for the current
    243 Emacs instance to be used as editor by adding it to the appropriate
    244 mode hooks:
    245 
    246 @lisp
    247 (add-hook 'shell-mode-hook  'with-editor-export-editor)
    248 (add-hook 'eshell-mode-hook 'with-editor-export-editor)
    249 (add-hook 'term-exec-hook   'with-editor-export-editor)
    250 (add-hook 'vterm-mode-hook  'with-editor-export-editor)
    251 @end lisp
    252 
    253 Some variants of this function exist; these two forms are equivalent:
    254 
    255 @lisp
    256 (add-hook 'shell-mode-hook
    257           (apply-partially 'with-editor-export-editor "GIT_EDITOR"))
    258 (add-hook 'shell-mode-hook 'with-editor-export-git-editor)
    259 @end lisp
    260 
    261 @deffn Command with-editor-export-editor
    262 When invoked in a @code{shell-mode}, @code{eshell-mode}, @code{term-mode} or @code{vterm-mode}
    263 buffer, this command teaches shell commands to use the current Emacs
    264 instance as the editor, by exporting @code{$EDITOR}.
    265 @end deffn
    266 
    267 @deffn Command with-editor-export-git-editor
    268 This command is like @code{with-editor-export-editor} but exports
    269 @code{$GIT_EDITOR}.
    270 @end deffn
    271 
    272 @deffn Command with-editor-export-hg-editor
    273 This command is like @code{with-editor-export-editor} but exports
    274 @code{$HG_EDITOR}.
    275 @end deffn
    276 
    277 @node Using With-Editor as a library
    278 @chapter Using With-Editor as a library
    279 
    280 This section describes how to use the @code{with-editor} library @emph{outside} of
    281 Magit to teach another package how to have its child processes call
    282 home, just like Magit does.  You don't need to know any of this just
    283 to create commits using Magit.  You can also ignore this if you use
    284 @code{with-editor} outside of Magit, but only as an end-user.
    285 
    286 For information about interactive use and options that affect both
    287 interactive and non-interactive use, see @ref{Using the With-Editor package}.
    288 
    289 @defmac with-editor &rest body
    290 This macro arranges for the @code{emacsclient} or the sleeping editor to be
    291 used as the editor of child processes, effectively teaching them to
    292 call home to the current Emacs instance when they require that the
    293 user edits a file.
    294 
    295 This is done by establishing a local binding for @code{process-environment}
    296 and changing the value of the @code{EDITOR} environment variable in that
    297 scope.  This affects all (asynchronous) processes started by forms
    298 (dynamically) inside BODY@.
    299 
    300 If BODY begins with a literal string, then that variable is set
    301 instead of @code{EDITOR}.
    302 @end defmac
    303 
    304 @defmac with-editor envvar &rest body
    305 This macro is like @code{with-editor} instead that the ENVVAR argument is
    306 required and that it is evaluated at run-time.
    307 @end defmac
    308 
    309 @defun with-editor-set-process-filter process filter
    310 This function is like @code{set-process-filter} but ensures that adding the
    311 new FILTER does not remove the @code{with-editor-process-filter}.  This is
    312 done by wrapping the two filter functions using a lambda, which
    313 becomes the actual filter.  It calls FILTER first, which may or
    314 may not insert the text into the PROCESS's buffer.  Then it calls
    315 @code{with-editor-process-filter}, passing t as NO-STANDARD-FILTER@.
    316 @end defun
    317 
    318 @node Debugging
    319 @chapter Debugging
    320 
    321 With-Editor tries very hard to locate a suitable @code{emacsclient}
    322 executable, and then sets option @code{with-editor-emacsclient-executable}
    323 accordingly.  In very rare cases this fails.  When it does fail, then
    324 the most likely reason is that someone found yet another way to
    325 package Emacs (most likely on macOS) without putting the executable on
    326 @code{$PATH}, and we have to add another kludge to find it anyway.
    327 
    328 If you are having problems using @code{with-editor}, e.g., you cannot commit
    329 in Magit, then please open a new issue at
    330 @uref{https://github.com/magit/with-editor/issues} and provide information
    331 about your Emacs installation.  Most importantly how did you install
    332 Emacs and what is the output of @code{M-x with-editor-debug RET}.
    333 
    334 @node Function and Command Index
    335 @appendix Function and Command Index
    336 
    337 @printindex fn
    338 
    339 @node Variable Index
    340 @appendix Variable Index
    341 
    342 @printindex vr
    343 
    344 @bye