summary refs log tree commit diff
path: root/emenu.el
blob: e0f862992a5c9605a2785f91b52eb0db3c7cdf30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;;; emenu.el --- An emacs dmenu clone -*- lexical-binding: t; -*-

;; Copyright © 2022 Erik Oosting

;; Author: Erik Oosting <crazazy@tilde.cafe>
;; Keywords: application-launcher, misc
;; URL: https://crazazy.tilde.cafe/emenu.git/log.html

;;; License:
;; This file comes with the MIT license, and without any warranty whatsoever
;; You can do with this stuff whatever you want to, but just remember
;; to put me in the footnote :D. Would be nice at least

;;; Commentary:
;; This is basically what dmenu does, but then in emacs. There is a function 'all-commands'
;; that gets you all the commands that are on your system. If you instead want to use emenu
;; on a different list of strings, you can just pass that list to emenu itself and it will
;; let you select something yourself

;;; Code:
(require 'cl-seq)
(require 'ido)

(defun emenu--all-commands ()
  "returns a list of all the programs accessible to you in $PATH"
  ;; This ls-output part was mostly created by phundrak because he found my shell-piping implementation
  ;; inelegant. If something has to change about this chances are this is going to return to shell
  ;; scripting again
  (let ((ls-output (mapcan (lambda (path)
                             (when (file-readable-p path)
                               (cl-remove-if (lambda (file)
                                               (let ((fullpath (concat path "/" file)))
                                                 (or (file-directory-p fullpath)
                                                     (not (file-executable-p fullpath)))))
                                             (directory-files path nil directory-files-no-dot-files-regexp nil))))
                           (split-string (getenv "PATH") ":" t)))
        (alias-output (split-string (shell-command-to-string "alias -p | sed -E 's/^alias ([^=]*)=.*$/\\1/'") "\n")))
    (append ls-output alias-output)))

(defun emenu (&optional command-list)
  "A dmenu-inspired application launcher using a separate emacs frame"
  (interactive)
  (with-selected-frame (make-frame '((name . "emenu")
                                     (minibuffer . only)
                                     (width . 151)
                                     (height . 1)))
    (unwind-protect
        (progn
          (call-process
           (ido-completing-read "Command: " (or
                                             command-list
                                             (emenu--all-commands)))
           nil
           0)
          (keyboard-quit))
      (delete-frame))))

(provide 'emenu)
;;; emenu.el ends here