about summary refs log tree commit diff
path: root/emacs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-04-02T17·34+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-04-02T17·34+0100
commit691583ed5c8a722fec40c719599e9b2f458e7bdb (patch)
treeb79a888f80951212b23676e12f451d408231236c /emacs
parentd12b8b3dcb97aa6365eca061a260b74b41bfd452 (diff)
Refactor opening X applications from Emacs
I borrowed heavily from Vincent's depot.
Diffstat (limited to 'emacs')
-rw-r--r--emacs/.emacs.d/wpc/ivy-helpers.el34
-rw-r--r--emacs/.emacs.d/wpc/window-manager.el24
2 files changed, 35 insertions, 23 deletions
diff --git a/emacs/.emacs.d/wpc/ivy-helpers.el b/emacs/.emacs.d/wpc/ivy-helpers.el
index c71a907a20c1..75b22fa4bb79 100644
--- a/emacs/.emacs.d/wpc/ivy-helpers.el
+++ b/emacs/.emacs.d/wpc/ivy-helpers.el
@@ -26,6 +26,40 @@ with the key and value from KV."
    :action (lambda (entry)
              (funcall f (car entry) (cdr entry)))))
 
+(defun ivy-helpers/do-run-external-command (cmd)
+  "Execute the specified CMD and notify the user when it finishes."
+  (message "Starting %s..." cmd)
+  (set-process-sentinel
+   (start-process-shell-command cmd nil cmd)
+   (lambda (process event)
+     (when (string= event "finished\n")
+       (message "%s process finished." process)))))
+
+(defun ivy-helpers/list-external-commands ()
+  "Creates a list of all external commands available on $PATH while filtering
+NixOS wrappers."
+  (cl-loop
+   for dir in (split-string (getenv "PATH") path-separator)
+   when (and (file-exists-p dir) (file-accessible-directory-p dir))
+   for lsdir = (cl-loop for i in (directory-files dir t)
+                        for bn = (file-name-nondirectory i)
+                        when (and (not (s-contains? "-wrapped" i))
+                                  (not (member bn completions))
+                                  (not (file-directory-p i))
+                                  (file-executable-p i))
+                        collect bn)
+   append lsdir into completions
+   finally return (sort completions 'string-lessp)))
+
+(defun ivy-helpers/run-external-command ()
+  "Prompts the user with a list of all installed applications and
+lets them select one to launch."
+  (interactive)
+  (let ((external-commands-list (list-external-commands)))
+    (ivy-read "Command:" external-commands-list
+              :require-match t
+              :action #'ivy-helpers/do-run-external-command)))
+
 ;;; Code:
 (provide 'ivy-helpers)
 ;;; ivy-helpers.el ends here
diff --git a/emacs/.emacs.d/wpc/window-manager.el b/emacs/.emacs.d/wpc/window-manager.el
index 6558f1decf5a..3bbf749e8389 100644
--- a/emacs/.emacs.d/wpc/window-manager.el
+++ b/emacs/.emacs.d/wpc/window-manager.el
@@ -197,7 +197,7 @@
                 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
                 (:key "M-:"               :fn eval-expression)
-                (:key "M-SPC"             :fn window-manager/apps)
+                (:key "M-SPC"             :fn ivy-helpers/run-external-command)
                 (:key "M-x"               :fn counsel-M-x)
                 (:key "<M-tab>"           :fn exwm/next-workspace)
                 (:key "<M-S-iso-lefttab>" :fn exwm/prev-workspace)
@@ -379,28 +379,6 @@ Ivy is used to capture the user's input."
         (alist/get (ivy-read "System: " (alist/keys name->cmd))
                    name->cmd))))))
 
-(cl-defun exwm/open (command &key
-                             (process-name command)
-                             (buffer-name command))
-  "Open COMMAND, which should be an X11 window."
-  (start-process-shell-command process-name buffer-name command))
-
-(cl-defun window-manager/execute-from-counsel (&key prompt list)
-  "Display a counsel menu of `LIST' with `PROMPT' and pipe the output through
-`start-process-shell-command'."
-  (let ((x (ivy-read prompt list)))
-    (exwm/open
-     x
-     :buffer-name (string/format "*exwm/open*<%s>" x)
-     :process-name x)))
-
-(defun window-manager/apps ()
-  "Open commonly used applications from counsel."
-  (interactive)
-  (window-manager/execute-from-counsel
-   :prompt "Application: "
-   :list window-manager/applications))
-
 (defun exwm/label->index (label workspaces)
   "Return the index of the workspace in WORKSPACES named LABEL."
   (let ((workspace (->> workspaces