### Export Specific Editor Variable (Emacs Lisp) Source: https://github.com/magit/with-editor/blob/main/README.org This Emacs Lisp example illustrates how to export a specific environment variable, such as GIT_EDITOR, to be used as the editor. It utilizes 'apply-partially' with 'with-editor-export-editor' or the convenience function 'with-editor-export-git-editor' to achieve this. This is useful for packages that require a specific editor variable to be set, like Magit's Git integration. ```emacs-lisp (add-hook 'shell-mode-hook (apply-partially 'with-editor-export-editor "GIT_EDITOR")) (add-hook 'shell-mode-hook 'with-editor-export-git-editor) ``` -------------------------------- ### Specialized Git and Mercurial Editor Export (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt These are specialized functions for Git and Mercurial editor configuration, simplifying the process of setting the appropriate environment variable for these version control systems. They can be added to relevant mode hooks for automatic setup. ```emacs-lisp ;; Configure Git editor specifically (add-hook 'shell-mode-hook #'with-editor-export-git-editor) (add-hook 'eshell-mode-hook #'with-editor-export-git-editor) ;; Configure Mercurial editor (add-hook 'shell-mode-hook #'with-editor-export-hg-editor) ;; Manual invocation ;; M-x with-editor-export-git-editor RET ;; Result: $GIT_EDITOR set to current Emacs instance ``` -------------------------------- ### Preserve Editor Functionality with Process Filters (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor-set-process-filter` function allows setting a custom process filter while ensuring that the `with-editor` functionality for detecting edit requests is preserved. This is crucial when overriding default process filters, as shown in examples for Git commit and rebase processes. ```emacs-lisp ;; Standard process filter overrides with-editor filter (let ((proc (start-file-process "git" "*git*" "git" "commit"))) ;; Wrong - breaks edit request handling ;; (set-process-filter proc #'my-filter) ;; Correct - preserves edit request detection (with-editor-set-process-filter proc (lambda (process output) (with-current-buffer (process-buffer process) (goto-char (point-max)) (insert output))))) ;; Example with custom output processing (with-editor (let ((proc (start-file-process "rebase" "*rebase*" "git" "rebase" "-i" "HEAD~3"))) (with-editor-set-process-filter proc (lambda (proc str) (when (string-match "error:" str) (message "Rebase error: %s" str)) (internal-default-process-filter proc str))))) ``` -------------------------------- ### Set Editor Commands Keybindings (Emacs Lisp) Source: https://github.com/magit/with-editor/blob/main/README.org This snippet demonstrates how to set keybindings for 'with-editor-async-shell-command' and 'with-editor-shell-command' to replace the default 'async-shell-command' and 'shell-command'. It ensures that child processes use the current Emacs instance as their editor. No external dependencies are required beyond the 'with-editor' library itself. ```emacs-lisp (keymap-global-set " " #'with-editor-async-shell-command) (keymap-global-set " " #'with-editor-shell-command) ``` -------------------------------- ### Troubleshoot with-editor Configuration Issues (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor-debug` function is a diagnostic tool used to troubleshoot configuration issues with the `with-editor` package. Running this function provides detailed information about the Emacs and system configuration, server status, Emacsclient discovery, and path contents, aiding in identifying and resolving problems. ```emacs-lisp ;; Run diagnostics (with-editor-debug) ;; Output includes: ;; - Library and Emacs version information ;; - System configuration details ;; - Server status and socket information ;; - Emacsclient executable discovery results ;; - PATH and exec-path contents ;; - Available emacsclient executables ;; Example output interpretation: ;; server-running-p: t ; Server is active ;; server-process: # ; Process running ;; with-editor-emacsclient-executable: /usr/bin/emacsclient ; Found executable ``` -------------------------------- ### Customize with-editor Behavior with Variables (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt Various configuration variables can be set to customize the behavior of the `with-editor` package. These include specifying the Emacsclient executable path, controlling async shell command behavior, changing the mode-line indicator, defining a sleeping editor, excluding files from history, and configuring server window selection. ```emacs-lisp ;; Customize Emacsclient executable location (setq with-editor-emacsclient-executable "/usr/local/bin/emacsclient") ;; Disable Emacsclient usage (forces sleeping editor) (setq with-editor-emacsclient-executable nil) ;; Control async shell command behavior (setq with-editor-shell-command-use-emacsclient t) ;; Customize mode-line indicator (setq with-editor-mode-lighter " ✎") ;; Custom sleeping editor for shells without proper trap support (setq with-editor-sleeping-editor "sh -c 'echo WITH-EDITOR: $$ OPEN $0 $1 IN $(pwd); while true; do sleep 1; done'") ;; Prevent files from being added to file-name-history (add-to-list 'with-editor-file-name-history-exclude "\.git/COMMIT_EDITMSG\'") ;; Custom server window selection per file pattern (add-to-list 'with-editor-server-window-alist '("COMMIT_EDITMSG\'" . pop-to-buffer)) ``` -------------------------------- ### Run Async Shell Commands with Emacsclient Editor (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor-async-shell-command` function runs asynchronous shell commands with the `$EDITOR` environment variable automatically set to the current Emacs instance. It allows specifying custom output, error buffers, and environment variable names. ```emacs-lisp ;; Basic async command - opens commit message in Emacs (with-editor-async-shell-command "git commit") ;; With prefix argument, choose environment variable ;; C-u M-x with-editor-async-shell-command RET git commit RET GIT_EDITOR RET ;; Remap standard async-shell-command in init file (keymap-global-set " " #'with-editor-async-shell-command) ;; Programmatic usage with custom envvar (with-editor-async-shell-command "hg commit" "*hg-output*" "*hg-errors*" "HG_EDITOR") ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Complete or Abort External Edit Sessions (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt These commands, `with-editor-finish` and `with-editor-cancel`, are used to conclude or abort an edit session initiated by an external process. `with-editor-finish` saves changes and closes the buffer, while `with-editor-cancel` discards them. They can be programmatically called and accept a prefix argument to bypass query functions. ```emacs-lisp ;; Bound to C-c C-c - saves and closes, returns to previous window config ;; (with-editor-finish nil) ;; Bound to C-c C-k - cancels without applying changes ;; (with-editor-cancel nil) ;; Force finish/cancel with prefix argument (bypasses query functions) ;; C-u C-c C-c or C-u C-c C-k ;; Programmatic usage with validation (defun my-safe-finish () "Finish edit session after validation." (interactive) (when (and (with-editor-mode) (not (buffer-modified-p))) (user-error "No changes to commit")) (with-editor-finish nil)) ``` -------------------------------- ### Export Editor Configuration to Shell Buffers (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor-export-editor` function exports editor configuration to interactive shell buffers, enabling manual command execution with Emacsclient as the editor. It can be automatically set up for various terminal modes using hooks. ```emacs-lisp ;; Manual invocation in a shell buffer ;; M-x with-editor-export-editor RET ;; Then run: git commit ;; Automatic setup for shell-mode (add-hook 'shell-mode-hook #'with-editor-export-editor) ;; Setup for all supported terminal modes (add-hook 'shell-mode-hook #'with-editor-export-editor) (add-hook 'eshell-mode-hook #'with-editor-export-editor) (add-hook 'term-exec-hook #'with-editor-export-editor) (add-hook 'vterm-mode-hook #'with-editor-export-editor) ;; Export specific environment variable (add-hook 'shell-mode-hook (apply-partially #'with-editor-export-editor "GIT_EDITOR")) ;; Or use convenience functions (add-hook 'shell-mode-hook #'with-editor-export-git-editor) ``` -------------------------------- ### Wrap shell-command for Editor Environment Detection (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor-shell-command` function is a wrapper for `shell-command` that detects asynchronous operations (commands ending with `&`) and configures the editor environment accordingly. It can be remapped globally for convenience. ```emacs-lisp ;; Synchronous command - no editor setup (with-editor-shell-command "git status") ;; Async command (ends with &) - editor configured (with-editor-shell-command "git rebase -i HEAD~5 &") ;; Remap standard shell-command globally (keymap-global-set " " #'with-editor-shell-command) ;; Use in elisp to conditionally handle both cases (defun run-editor-command (cmd) (if (string-suffix-p "&" cmd) (with-editor-shell-command cmd) (shell-command cmd))) ``` -------------------------------- ### Untitled No description -------------------------------- ### Execute Code with $EDITOR as Emacsclient (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor` macro executes Lisp code with the `EDITOR` environment variable set to use Emacsclient, modifying `process-environment` for child processes. It supports a custom environment variable name and programmatically setting the editor for subprocesses. ```emacs-lisp ;; Basic usage with default EDITOR variable (with-editor (async-shell-command "git commit")) ;; Use a custom environment variable (with-editor "GIT_EDITOR" (start-process "git-rebase" "*git-rebase*" "git" "rebase" "-i" "HEAD~3")) ;; Programmatically set the editor for a subprocess (defun my-git-commit () "Start an interactive git commit." (interactive) (with-editor (let ((process (start-file-process "git" "*git-commit*" "git" "commit" "-v"))) (set-process-sentinel process (lambda (proc event) (when (string= event "finished\n") (message "Commit completed"))))))) ``` -------------------------------- ### Untitled No description -------------------------------- ### Configure Shell Commands with Emacs Editor (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `shell-command-with-editor-mode` globally configures Emacs as the editor for all asynchronous shell commands. This is useful for commands like `git commit` or `crontab -e` that require user input via an editor. It can be enabled, disabled, or toggled. ```emacs-lisp ;; Enable globally (shell-command-with-editor-mode 1) ;; Now all async shell commands use current Emacs as editor (async-shell-command "git commit") (async-shell-command "crontab -e") ;; Toggle in init file (add-hook 'after-init-hook #'shell-command-with-editor-mode) ;; Disable when needed (shell-command-with-editor-mode -1) ``` -------------------------------- ### Untitled No description -------------------------------- ### Export EDITOR in Shell Modes (Emacs Lisp) Source: https://github.com/magit/with-editor/blob/main/README.org This code snippet shows how to automatically export the EDITOR environment variable for child processes within various Emacs shell modes (shell-mode, eshell-mode, term-mode, vterm-mode) by adding 'with-editor-export-editor' to their respective hooks. This ensures that any command executed within these buffers will use the current Emacs instance as the editor. It requires the 'with-editor' library to be loaded. ```emacs-lisp (add-hook 'shell-mode-hook 'with-editor-export-editor) (add-hook 'eshell-mode-hook 'with-editor-export-editor) (add-hook 'term-exec-hook 'with-editor-export-editor) (add-hook 'vterm-mode-hook 'with-editor-export-editor) ``` -------------------------------- ### Manage External Editor Buffers (Emacs Lisp) Source: https://context7.com/magit/with-editor/llms.txt The `with-editor-mode` is a minor mode activated in buffers opened by external processes. It provides commands to finish editing (`with-editor-finish`, bound to C-c C-c) or cancel editing (`with-editor-cancel`, bound to C-c C-k). Hooks like `with-editor-pre-finish-hook` and `with-editor-finish-query-functions` allow customization of the finish behavior and validation. ```emacs-lisp ;; Automatically enabled when external process opens a file ;; User finishes editing with: C-c C-c (with-editor-finish) ;; User cancels editing with: C-c C-k (with-editor-cancel) ;; Customize finish behavior with hooks (add-hook 'with-editor-pre-finish-hook (lambda () (when (buffer-modified-p) (save-buffer)))) ;; Add validation before finishing (add-hook 'with-editor-finish-query-functions (lambda (force) (or force (yes-or-no-p "Really finish? ")))) ;; Custom cancel message (setq-local with-editor-cancel-message "Edit cancelled - changes discarded") ;; Post-finish actions (add-hook 'with-editor-post-finish-hook (lambda () (message "Edit session completed successfully"))) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.