dotemacs

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

with-editor.org (12226B)


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