itch

Switch to and create scratch buffers
git clone https://git.jamzattack.xyz/itch
Log | Files | Refs | LICENSE

ModeNameSize
-rw-r--r--.gitignore3L
-rw-r--r--LICENSE674L
-rw-r--r--README.org95L
-rw-r--r--itch.el146L
Itch.el – Switch to and create scratch buffers easily

Itch.el – Switch to and create scratch buffers easily

This library provides a unified interface to switch to and create buffers of a particular major mode. Basically, I wanted to switch to the scratch buffer quickly but that wasn't high-tech enough.

Semantics of buffer switching and creation

  • If prefix arg is a number, the buffer is named "NAME<PREFIX>". e.g. C-2 M-x itch-org RET will switch to "*org*<2>"
  • If prefix arg is (16) (that is, a list whose sole element is the number 16, accessible with a double universal prefix C-u C-u), switch to a buffer chosen from all existing buffers in MODE whose name starts with NAME
  • If the current buffer is in MODE and its name starts with NAME, bury it.

Defining new commands with itch

The function itch is the powerhouse of this package, and allows you to define new commands that use the above logic.

It takes 4 arguments: MODE NAME &optional PREFIX POST-CREATE-FUNCTION

  • MODE is the major mode of the new buffer
  • NAME is the name of the new buffer
  • PREFIX is intended to be the prefix argument (see its docstring for details)
  • POST-CREATE-FUNCTION is run within all newly created buffers

For example, to create a new org-mode scratch buffer called *notes* in ~/org/notes and insert a timestamp, you could use:

(defun itch-notes (&optional arg)
  (interactive "P")
  (itch 'org-mode "*notes*" arg
        (lambda ()
          (setq default-directory "~/org/notes")
          (insert (format-time-string
                   "#+date: [%Y-%m-%d %a %H:%M]\n\n")))))

Command index

  • Function: (itch mode name &optional prefix post-create-function)

    Create a temporary buffer in MODE called NAME. This follows some complex but intuitive logic (in order):

    • If PREFIX is a number, the buffer is named "NAME<PREFIX>"
    • If PREFIX is (16), switch to a buffer chosen from all existing buffers in MODE whose name starts with NAME
    • If the current buffer is in MODE and its name starts with NAME, bury it.
    • If the buffer is newly created (i.e. empty and not narrowed), POST-CREATE-FUNCTION will be called in the new buffer.
  • Function: (itch-eshell &optional arg)

    Create or switch to an eshell buffer. Prefix ARG is passed to itch. See its docstring for usage.

  • Function: (itch-switch-to-eshell)

    Switch to an existing eshell buffer.

  • Function: (itch-elisp &optional arg)

    Create or switch to an emacs lisp buffer. Prefix ARG is passed to itch. See its docstring for usage.

  • Function: (itch-switch-to-elisp)

    Switch to an existing emacs lisp scratch buffer.

  • Function: (itch-org &optional arg)

    Create or switch to an org buffer. Prefix ARG is passed to itch. See its docstring for usage.

  • Function: (itch-switch-to-org)

    Switch to an existing org scratch buffer.

  • Function: (itch-fundamental &optional arg)

    Create or switch to a fundamental-mode buffer. Prefix ARG is passed to itch. See its docstring for usage.

  • Function: (itch-switch-to-fundamental)

    Switch to an existing fundamental-mode scratch buffer.

License

GPL3+