From dedcfc3d71260543bea0052bcb71cf4705720dd6 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 25 Nov 2023 17:39:16 +0300 Subject: feat(tazjin/emacs): add interactive commands for configuring screens Uses a bunch of weird xrandr invocations and completing reads to configure screens the way I want. Note that this has a known bug where disconnecting a primary screen will *not* make one of the remaining screens primary. Change-Id: Ide5322df446685cc4740d4ddd7b6ca8682375050 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10120 Reviewed-by: tazjin Autosubmit: tazjin Tested-by: BuildkiteCI --- users/tazjin/emacs/config/desktop.el | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'users/tazjin') diff --git a/users/tazjin/emacs/config/desktop.el b/users/tazjin/emacs/config/desktop.el index 51a3f208481d..856265bcdb73 100644 --- a/users/tazjin/emacs/config/desktop.el +++ b/users/tazjin/emacs/config/desktop.el @@ -346,6 +346,58 @@ ring." (exwm-input-set-key (kbd "s-m 2") #'randr-khamovnik-layout-office) (exwm-input-set-key (kbd "s-m s") #'randr-khamovnik-layout-single))) +(defun list-available-monitors () + "List connected, but unused monitors." + (let* ((all-connected + (seq-map (lambda (line) (car (s-split " " line))) + (s-lines (s-trim (shell-command-to-string "xrandr | grep connected | grep -v disconnected"))))) + (all-active (seq-map #'car (cadr (exwm-randr--get-monitors))))) + (seq-filter (lambda (s) (not (seq-contains-p all-active s))) + all-connected))) + +(defun exwm-enable-monitor () + "Interactively construct an EXWM invocation that enable the +given monitor and assigns a workspace to it." + (interactive) + + (let* ((monitors (list-available-monitors)) + (primary (car (exwm-randr--get-monitors))) + (monitor (pcase (seq-length monitors) + (0 (error "No available monitors.")) + (1 (car monitors)) + (_ + (completing-read "Which monitor? " (list-available-monitors) nil t)))) + + (configurations `(("secondary (left)" . ,(format "--left-of %s" primary)) + ("secondary (right)" . ,(format "--right-of %s" primary)) + ("primary (left)" . ,(format "--left-of %s --primary" primary)) + ("primary (right)" . ,(format "--right-of %s --primary" primary)) + ("mirror" . ,(format "--same-as %s" primary)))) + + (where (completing-read (format "%s should be " monitor) + (seq-map #'car configurations) + nil t)) + (xrandr-pos (cdr (assoc where configurations))) + (xrandr-cmd (format "xrandr --output %s --auto %s" monitor xrandr-pos))) + (message "Invoking '%s'" xrandr-cmd) + (shell-command xrandr-cmd) + (exwm-assign-workspaces))) + +(defun exwm-disable-monitor () + "Interactively choose a monitor to disable." + (interactive) + + (let* ((active (seq-map #'car (cadr (exwm-randr--get-monitors)))) + (monitor (if (> (seq-length active) 1) + (completing-read "Disable which monitor? " active nil t) + (error "Only one monitor is active!"))) + (xrandr-cmd (format "xrandr --output %s --off" monitor))) + (shell-command xrandr-cmd) + (exwm-assign-workspaces))) + +(exwm-input-set-key (kbd "s-m e") #'exwm-enable-monitor) +(exwm-input-set-key (kbd "s-m d") #'exwm-disable-monitor) + ;; Notmuch shortcuts as EXWM globals ;; (g m => gmail) (exwm-input-set-key (kbd "s-g m") #'notmuch) -- cgit 1.4.1