87 lines
3.0 KiB
Org Mode
87 lines
3.0 KiB
Org Mode
:PROPERTIES:
|
|
:ID: cce/use_emacs_as_a_pager
|
|
:END:
|
|
#+TITLE: Use Emacs as a Pager
|
|
#+filetags: :Emacs:CCE:Shells:
|
|
#+ARCOLOGY_KEY: cce/emacs-pager
|
|
|
|
#+PROPERTY: header-args :mkdirp yes :results none
|
|
#+PROPERTY: header-args:emacs-lisp :tangle emacs-pager.el
|
|
#+AUTO_TANGLE: t
|
|
#+ARCOLOGY_ALLOW_CRAWL: t
|
|
|
|
#+ARROYO_EMACS_MODULE: emacs-pager
|
|
#+ARROYO_MODULE_WANTS: cce/configure_packaging.org
|
|
#+ARROYO_HOME_MODULE: hm/emacs-pager.nix
|
|
|
|
#+begin_src emacs-lisp
|
|
(provide 'cce/emacs-pager)
|
|
#+end_src
|
|
|
|
[[https://www.reddit.com/r/emacs/comments/2rr1ha/use_a_buffer_as_pager_from_shellmode/cnik8wb/][uhoreg]] has a cool script they shared on Reddit which lets you use Emacs buffers as your pager.
|
|
|
|
#+BEGIN_SRC shell :tangle ~/arroyo-nix/files/emacs-pager-bin :shebang #!/bin/sh
|
|
[[ -z "$(which emacsclient)" ]] && less -
|
|
t=$(mktemp --suffix .emacs-pager) || exit 1
|
|
cat - >> $t
|
|
echo 'reading into emacs...'
|
|
emacsclient --alternate-editor=less "$t"
|
|
rm -f -- $t
|
|
#+END_SRC
|
|
|
|
#+BEGIN_SRC shell :tangle bashrc.d/01-pager.sh :mkdirp yes
|
|
export PAGER=emacs-pager
|
|
#+END_SRC
|
|
|
|
#+begin_src nix :tangle ~/arroyo-nix/hm/emacs-pager.nix
|
|
{ ... }:
|
|
|
|
{
|
|
home.sessionVariables = {
|
|
PAGER = "${../files/emacs-pager-bin}";
|
|
};
|
|
}
|
|
#+end_src
|
|
|
|
Of course, if you feed random terminal shit in to your Emacs, you're gonna end up with ANSI escape
|
|
codes. Luckily, [[https://github.com/atomontage/xterm-color][xterm-color.el]] exists, and we can use that to automatically color our buffers as
|
|
necessary.
|
|
|
|
We start with =xterm-colorize-maybe=, a function which searches the buffer for an ANSI control-code,
|
|
and colorizes the buffer if it finds one.
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
(defun cce/xterm-colorize-maybe ()
|
|
(interactive)
|
|
(goto-char (point-min))
|
|
(when (search-forward (char-to-string 27) nil t)
|
|
(xterm-color-colorize-buffer)))
|
|
(add-hook 'find-file-hook #'cce/xterm-colorize-maybe)
|
|
#+END_SRC
|
|
|
|
I set =TERM= to =xterm-256color= everywhere that I can so that commands try to output the text and
|
|
don't assume that Emacs (identifying as =TERM=dumb=) can't handle it. =comint= and =compilation= are
|
|
handled a little bit differently due to one having a process filter and the other not.
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
(use-package xterm-color
|
|
:commands xterm-color-colorize-buffer xterm-color-test
|
|
:autoload xterm-color-filter
|
|
:config
|
|
(setenv "TERM" "xterm-256color")
|
|
(setq compilation-environment '("TERM=xterm-256color"))
|
|
(setq comint-output-filter-functions
|
|
(remove 'ansi-color-process-output comint-output-filter-functions))
|
|
(defun cce/compilation-start-hook (proc)
|
|
(when (eq (process-filter proc) 'compilation-filter)
|
|
(set-process-filter proc
|
|
(lambda (proc string)
|
|
(funcall 'compilation-filter proc
|
|
(xterm-color-filter string))))))
|
|
:hook
|
|
(shell-mode . (lambda ()
|
|
(add-hook 'comint-preoutput-filter-functions
|
|
'xterm-color-filter nil t)))
|
|
(compilation-start . cce/compilation-start-hook))
|
|
#+END_SRC
|