Skip to content

Instantly share code, notes, and snippets.

@douo
Last active July 6, 2023 06:52
Show Gist options
  • Save douo/6503aa39d4165fa116eb9085a0b1b292 to your computer and use it in GitHub Desktop.
Save douo/6503aa39d4165fa116eb9085a0b1b292 to your computer and use it in GitHub Desktop.
Emacs (no-x) as kitty scrollback_pager

Test on:

  • Linux
  • macOS

Linux dependencies:

  • xsel

~/.local/bin/kitty_emacs_pager

#!/bin/bash
# use /bin/zsh if macOS

if ! [ -v TTY ]; then
    TTY=/dev/tty
fi
STDIN=$TTY
STDOUT=$TTY

TMP="$(mktemp /tmp/kitty_scrollback_XXXX)";
cat >"$TMP";

# `--terminal/-t $TTY` or `0<$STDIN 1>$STDOUT`
# Eliminate the "standard input is not a tty" error message when running Emacs in a pipe.
emacs -t $TTY -nw -Q --eval  \
      "(let ((b (create-file-buffer \"*kitty_scrollback*\")))
            (menu-bar-mode -1)  \
            (switch-to-buffer b) \
            (insert-file-contents \"${TMP}\")  \
            (delete-file \"${TMP}\")  \
            (require 'ansi-color)  \
            (ansi-color-apply-on-region (point-min) (point-max)) \
            (read-only-mode)  \
            (visual-line-mode) \
            (set-window-start (selected-window) (line-beginning-position ${1})) \
            (setq mode-line-format nil) \
            (setq truncate-lines t) \
            (defadvice kill-ring-save (before copy-to-system-in-tty activate) \
                (pcase system-type  \
                   ('gnu/linux (call-process-region (ad-get-arg 0) (ad-get-arg 1) \"xsel\" nil nil nil \"-ib\"))  \
                   ('darwin (call-process-region (ad-get-arg 0) (ad-get-arg 1) \"pbcopy\" nil nil nil))  \
                )) \
)"

Explanation of elisp:

(let ((b (create-file-buffer "*kitty_scrollback*")))
  ;; you can add any package here, ex:
  ;; (load \"~/.emacs.d/elpa/avy-20230420.404/avy.el\") \
  ;; (global-set-key (kbd \"M-.\") 'avy-goto-char) \
  (menu-bar-mode -1) ;; hide menubar
  (switch-to-buffer b)
  (insert-file-contents ${TMP})
  (delete-file "${TMP}") ;; clear the tmp file
  (require 'ansi-color) ;; support color
  (ansi-color-apply-on-region (point-min) (point-max))
  (read-only-mode)
  ;; Make sure that Emacs and Kitty are in the same viewport.
  (set-window-start (selected-window) (line-beginning-position ${1}))
  (setq mode-line-format nil) ;; don't show modeline
  (setq truncate-lines t)  ;; don't line-wrap
  ;; sync kill region to system clipboard
  ;; check gui exist if you want to using this script in general purpose.
  (defadvice kill-ring-save (before copy-to-system-in-tty activate)
    (pcase system-type
      ('gnu/linux (call-process-region (ad-get-arg 0) (ad-get-arg 1) "xsel" nil nil nil "-ib"))
      ('darwin (call-process-region (ad-get-arg 0) (ad-get-arg 1) "pbcopy" nil nil nil))
      ))
  )

Now, edit the kitty.conf file and ensure that ~/.local/bin and emacs are included in the PATH environment variable of kitty. Alternatively, you can use absolute paths.

scrollback_pager  kitty_emacs_pager INPUT_LINE_NUMBER CURSOR_LINE CURSOR_COLUMN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment