Skip to content

Instantly share code, notes, and snippets.

@wrightmikea
Created March 15, 2021 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrightmikea/d88279b89ff87be85a920e365f84e7b2 to your computer and use it in GitHub Desktop.
Save wrightmikea/d88279b89ff87be85a920e365f84e7b2 to your computer and use it in GitHub Desktop.
emacs lisp functions that use dmenu; demonstrated at the Emacs SF 20210305T1000 online meetup by Mike Wright
;; emacs lisp functions that use dmenu; demonstrated at the Emacs SF 20210305T1000 online meetup by Mike Wright
;; some of these are copied or derived from a file I found via a github search for dmenu in .el files:
;; https://github.com/maavelar5/dotemacs/blob/2dd1d7e7af035edbfccfcdbbcbefdd55ba19ce44/.emacs.d/functions.el
;; to honor the license in Marco Avelar's functions.el, this file is also licensed under zlib.
;; https://en.wikipedia.org/wiki/Zlib_License
;; FYI, Marco (@marco) has a nice video demo: https://lbry.tv/@marco:55/emacs_dmenu_replacing_ivy:6
;; "Emacs + dmenu is awesome! bye bye Ivy!" October 31st, 2020
;; pre-reqs:
;;
;; (needed for all demos) install dmenu (e.g., 'sudo pacman -Sy dmenu' or 'sudo apt install dmenu' etc. linux only)
;; also see: [[https://tools.suckless.org/dmenu/]]
;;
;; for Demo 2 (dmenu-ag-in-file) also install the_silver_searcher (ag)
;; for Demo 4 (dmenu-rg) also install ripgrep (rg)
;;
;; See usage instructions at the end of this file...
(progn
;; Note: since the 2021 March 5th Emacs SF demo I figured out how to change the dmenu font to be bigger
;; (so this source doesn't exactly match the source I used for my screencast)
;; default (small) font: (setq dmenu-cfg " dmenu -i -l 20 -p .")
(setq dmenu-cfg "dmenu -l 20 -p pick -fn xft:terminus:style=bold:pixelsize=22") ;; dmenu has many options, including colors
;; Demo 1 - find a file in the current directory
(defun dmenu-find-file (&optional dir)
(interactive)
(setq command (concat "ls -a | " dmenu-cfg))
(setq output (shell-command-to-string command))
(when (> (length output) 1)
(find-file (substring output 0 -1))))
;; Demo 2 - use silver searcher to narrow to (and jump to) a specific line in the last saved version of this buffer
(defun dmenu-ag-in-file ()
(interactive)
(setq my_command (concat "ag . " buffer-file-name " | " dmenu-cfg))
(setq my_shell_output (shell-command-to-string my_command))
(setq splitted (split-string my_shell_output ":"))
(when (> (length splitted) 1)
(goto-line (string-to-number (car splitted)))))
;; Demo 3: use a predifined multi-line string of buffer names to switch between buffers.
;; Note: after I gave this demo, I changed the code from using a static list to dynamically creating the list.
;; FYI, a pre-req for this demo is the "s" emacs lisp package (used to trim a string)
;; (use-package s :ensure t)
(defun dmenu-buffer-demo ()
(interactive)
(require 's)
(setq choices "")
(dolist (buf (buffer-list)) (setq choices (concat choices (buffer-name buf) " \n")))
;; you could also build this as a static list
;; (setq choices "*Buffer List* \n*Messages* \n*scratch* \n*shell* ")
(setq cmd (concat "echo -e '" choices "' | " dmenu-cfg))
(setq out (s-trim-right (shell-command-to-string cmd)))
(if (< 0 (length out))
(switch-to-buffer out)))
;; Demo 4: use ripgrep to seach all files in this and subdirectories to narrow to a line (and jump to the file).
(defun dmenu-rg ()
(interactive)
(setq my_shell_output (shell-command-to-string (concat "rg . | " dmenu-cfg)))
(setq splitted (split-string my_shell_output ":"))
(when (> (length splitted) 1)
(find-file (car splitted))
(goto-line (string-to-number (nth 1 splitted)))))
;; end (progn...
)
;; eval above (progn) expression (using C-x C-e), or evaluate this buffer (using M-x eval-buffer), to load all demos.
;; Demo 1: M-x dmenu-find-file <enter> and type .el to narrow, use up/down arrow keys; press enter to select; escape to exit
;; Demo 2: M-x dmenu-ag-in-file <enter> and type 4 to narrow, use up/down arrow keys; press enter to select; escape to quit
;; Demo 3: M-x dmenu-buffer-demo <enter> and type scr to narrow, use up/down arrow keys; press enter to select; escape to quit
;; Demo 4: M-x dmenu-rg <enter> and the type -cfg to narrow, use up/down arrow keys; press enter to select; escape to exit
;; end dmenu demos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment