ambevar-dotfiles/.emacs.d/lisp/init-exwm.el

227 lines
11 KiB
EmacsLisp
Raw Normal View History

2017-07-26 18:39:19 +02:00
;;; EXWM
;;; When stating the client from .xinitrc, `save-buffer-kill-terminal' will
2017-12-27 11:36:35 +01:00
;;; force-kill Emacs before it can run through `kill-emacs-hook'.
(global-set-key (kbd "C-x C-c") 'save-buffers-kill-emacs)
;;; REVIEW: Athena+Xaw3d confuses xcape when binding Caps-lock to both L_Ctrl
;;; escape, in which case it will procude <C-escape> in Emacs. In practice, it
;;; means that `C-` keys will works but `<escape>` will need a fast double tap
;;; on Caps Lock.
;;;
;;; See https://github.com/ch11ng/exwm/issues/285
;;; and https://gitlab.com/interception/linux/plugins/caps2esc/issues/2.
2017-07-28 09:12:25 +02:00
2017-07-26 18:39:19 +02:00
;;; Rename buffer to window title.
2018-02-15 13:28:49 +01:00
(defun ambrevar/exwm-rename-buffer-to-title () (exwm-workspace-rename-buffer exwm-title))
(add-hook 'exwm-update-title-hook 'ambrevar/exwm-rename-buffer-to-title)
2017-07-26 18:39:19 +02:00
(add-hook 'exwm-floating-setup-hook 'exwm-layout-hide-mode-line)
(add-hook 'exwm-floating-exit-hook 'exwm-layout-show-mode-line)
;;; Allow non-floating resizing with mouse.
(setq window-divider-default-bottom-width 2
window-divider-default-right-width 2)
(window-divider-mode)
;;; System tray
2017-08-07 12:17:49 +02:00
(require 'exwm-systemtray)
(exwm-systemtray-enable)
(setq exwm-systemtray-height 16)
2017-07-26 18:39:19 +02:00
2017-07-26 22:27:16 +02:00
;;; Those cannot be set globally: if Emacs would be run in another WM, the "s-"
;;; prefix will conflict with the WM bindings.
2017-07-26 22:30:53 +02:00
(exwm-input-set-key (kbd "s-R") #'exwm-reset)
2017-07-26 18:39:19 +02:00
(exwm-input-set-key (kbd "s-x") #'exwm-input-toggle-keyboard)
(exwm-input-set-key (kbd "s-h") #'windmove-left)
(exwm-input-set-key (kbd "s-j") #'windmove-down)
(exwm-input-set-key (kbd "s-k") #'windmove-up)
(exwm-input-set-key (kbd "s-l") #'windmove-right)
(exwm-input-set-key (kbd "s-D") #'kill-this-buffer)
2017-07-26 18:39:19 +02:00
(exwm-input-set-key (kbd "s-b") #'list-buffers)
(exwm-input-set-key (kbd "s-f") #'find-file)
2017-10-28 09:58:10 +02:00
(when (require 'functions)
(exwm-input-set-key (kbd "s-\\") 'ambrevar/toggle-window-split)
(exwm-input-set-key (kbd "s-H") 'ambrevar/swap-windows-left)
(exwm-input-set-key (kbd "s-J") 'ambrevar/swap-windows-below)
(exwm-input-set-key (kbd "s-K") 'ambrevar/swap-windows-above)
(exwm-input-set-key (kbd "s-L") 'ambrevar/swap-windows-right))
2017-10-28 09:58:10 +02:00
;; The following can only apply to EXWM buffers, else it could have unexpected effects.
(push ?\s- exwm-input-prefix-keys)
(define-key exwm-mode-map (kbd "s-SPC") #'exwm-floating-toggle-floating)
2017-07-26 18:39:19 +02:00
(exwm-input-set-key (kbd "s-i") #'follow-delete-other-windows-and-split)
(exwm-input-set-key (kbd "s-o") #'ambrevar/toggle-single-window)
(exwm-input-set-key (kbd "s-O") #'exwm-layout-toggle-fullscreen)
2017-07-26 18:39:19 +02:00
(with-eval-after-load 'helm
;; Need `with-eval-after-load' here since 'helm-map is not defined in 'helm-config.
(ambrevar/define-keys helm-map
"s-\\" 'helm-toggle-resplit-and-swap-windows)
(exwm-input-set-key (kbd "s-c") #'helm-resume)
2017-07-26 18:39:19 +02:00
(exwm-input-set-key (kbd "s-b") #'helm-mini)
(exwm-input-set-key (kbd "s-f") #'helm-find-files)
(exwm-input-set-key (kbd "s-F") #'helm-locate)
2018-02-15 13:28:49 +01:00
(when (fboundp 'ambrevar/helm-locate-meta)
(exwm-input-set-key (kbd "s-F") #'ambrevar/helm-locate-meta))
(exwm-input-set-key (kbd "s-g") 'ambrevar/helm-grep-git-or-ag)
2019-02-27 12:53:55 +01:00
(exwm-input-set-key (kbd "s-G") 'ambrevar/helm-grep-git-all-or-ag)
;; Launcher
(exwm-input-set-key (kbd "s-r") 'helm-run-external-command))
2017-07-26 18:39:19 +02:00
(require 'functions)
2018-02-15 13:28:49 +01:00
(exwm-input-set-key (kbd "s-<tab>") #'ambrevar/switch-to-last-buffer)
2017-07-26 18:39:19 +02:00
(when (require 'evil nil t)
(exwm-input-set-key (kbd "s-<tab>") #'evil-switch-to-windows-last-buffer)
(exwm-input-set-key (kbd "C-6") #'evil-switch-to-windows-last-buffer))
;;; Emacs mode shortcuts.
(exwm-input-set-key (kbd "s-t") #'ambrevar/org-switch-agenda-file)
(exwm-input-set-key (kbd "s-T") #'ambrevar/org-switch-agenda-file-other-window)
(exwm-input-set-key (kbd "s-<return>") #'helm-eshell-switch)
(exwm-input-set-key (kbd "S-s-<return>") #'helm-eshell-switch-other-window)
(with-eval-after-load 'slime
(exwm-input-set-key (kbd "s-<backspace>") #'ambrevar/slime-switch-to-repl))
(when (fboundp 'magit-status)
2017-07-26 18:39:19 +02:00
(exwm-input-set-key (kbd "s-v") #'magit-status))
2017-07-31 08:57:18 +02:00
(when (fboundp 'emms-all)
2017-07-26 18:39:19 +02:00
(exwm-input-set-key (kbd "s-a") #'emms-smart-browse)
(exwm-input-set-key (kbd "S-s-<kp-enter>") #'emms-pause)
2017-11-02 22:00:38 +01:00
(if (fboundp 'helm-emms)
(exwm-input-set-key (kbd "s-A") #'helm-emms)
(exwm-input-set-key (kbd "s-A") #'emms)))
(when (or (fboundp 'mu4e)
(delq nil (mapcar (lambda (path) (string-match "/mu4e/\\|/mu4e$" path)) load-path)))
2018-02-15 13:28:49 +01:00
(exwm-input-set-key (kbd "s-m") #'ambrevar/mu4e-headers))
(exwm-input-set-key (kbd "s-n") #'ambrevar/elfeed-switch-back) ; "n" for "news"
(exwm-input-set-key (kbd "s-e") #'ambrevar/eww-switch-back)
(exwm-input-set-key (kbd "s-E") #'eww)
2017-07-26 18:39:19 +02:00
2018-02-15 23:40:33 +01:00
(when (fboundp 'helm-pass)
2019-02-27 12:53:55 +01:00
(defun ambrevar/helm-pass-for-page () ; TODO: Share with upstream.
2018-02-15 23:40:33 +01:00
"Default prompt to current exwm-title"
(interactive)
2018-02-18 11:39:49 +01:00
(require 'helm-pass)
2018-04-23 08:27:34 +02:00
(helm :sources 'helm-pass-source-pass
:input (cond
((derived-mode-p 'eww-mode)
(let* ((url (replace-regexp-in-string ".*//\\([^/]*\\).*" "\\1" (eww-current-url)))
(domain (split-string url "\\.")))
(concat (nth (- (length domain) 2) domain) "." (nth (1- (length domain)) domain))))
((and (derived-mode-p 'exwm-mode) exwm-title)
2018-06-28 00:34:37 +02:00
(let* ((title exwm-title) url domain)
(with-temp-buffer
(insert title)
(goto-char (point-min))
(while (and (not (eobp))
(not (thing-at-point-url-at-point)))
(forward-word))
(setq url (thing-at-point-url-at-point)))
(if (not url)
""
(if (not (string-match (rx "//"
(group
(* (not (any "/")))
"."
(* (not (any "." "/"))))
"/")
url))
""
(setq domain (split-string (match-string 1 url) "\\."))
(concat (nth (- (length domain) 2) domain) "." (nth (1- (length domain)) domain)))))))
2018-02-15 23:40:33 +01:00
:buffer "*helm-pass*"))
(exwm-input-set-key (kbd "s-p") #'ambrevar/helm-pass-for-page))
;;; External application shortcuts.
2018-02-15 13:28:49 +01:00
(defun ambrevar/exwm-start (command)
(interactive (list (read-shell-command "$ ")))
(start-process-shell-command command nil command))
2018-02-15 13:28:49 +01:00
(exwm-input-set-key (kbd "s-&") #'ambrevar/exwm-start)
(when (require 'helm-exwm nil t)
(add-to-list 'helm-source-names-using-follow "EXWM buffers")
(setq helm-exwm-emacs-buffers-source (helm-exwm-build-emacs-buffers-source))
(setq helm-exwm-source (helm-exwm-build-source))
2017-11-08 17:47:29 +01:00
(setq helm-mini-default-sources `(helm-exwm-emacs-buffers-source
helm-exwm-source
helm-source-recentf
,(when (boundp 'helm-source-ls-git) 'helm-source-ls-git)
helm-source-bookmarks
helm-source-bookmark-set
helm-source-buffer-not-found))
2018-04-23 08:27:34 +02:00
(ambrevar/define-keys
helm-exwm-map
"M-d" 'helm-buffer-run-kill-persistent
"S-<return>" 'helm-buffer-switch-other-window)
;; Web browser
(exwm-input-set-key (kbd "s-w") #'helm-exwm-switch-browser)
(exwm-input-set-key (kbd "s-W") #'helm-exwm-switch-browser-other-window))
2017-07-26 18:39:19 +02:00
2019-02-27 12:53:55 +01:00
(when (require 'desktop-environment nil 'noerror)
(setq desktop-environment-screenshot-directory "~/temp")
;; Use s-l to navigate windows.
(define-key desktop-environment-mode-map (kbd "s-z") #'desktop-environment-lock-screen)
(desktop-environment-mode)
(exwm-input-set-key (kbd "s-l") #'windmove-right))
2017-07-26 18:39:19 +02:00
;;; Volume control
(when (require 'pulseaudio-control nil t)
(exwm-input-set-key (kbd "s-<kp-subtract>") #'pulseaudio-control-decrease-volume)
(exwm-input-set-key (kbd "s-<kp-add>") #'pulseaudio-control-increase-volume)
(exwm-input-set-key (kbd "s-<kp-enter>") #'pulseaudio-control-toggle-current-sink-mute)
(exwm-input-set-key (kbd "s--") #'pulseaudio-control-decrease-volume)
(exwm-input-set-key (kbd "s-=") #'pulseaudio-control-increase-volume)
(exwm-input-set-key (kbd "s-0") #'pulseaudio-control-toggle-current-sink-mute))
2017-07-26 18:39:19 +02:00
2017-08-31 20:47:38 +02:00
;;; Check for start-up errors. See ~/.profile.
(let ((error-logs (directory-files "~" t "errors.*log$")))
(when error-logs
(warn "Error during system startup. See %s." (mapconcat 'identity error-logs ", "))
(when (daemonp)
;; Non-daemon Emacs already brings up the *Warning* buffer.
(setq initial-buffer-choice
(lambda () (get-buffer "*Warnings*"))))))
2017-08-31 20:47:38 +02:00
2017-10-11 18:45:45 +02:00
;;; Some programs such as 'emacs' are better off being started in char-mode.
2018-02-15 13:28:49 +01:00
(defun ambrevar/exwm-start-in-char-mode ()
(when (string-prefix-p "emacs" exwm-instance-name)
2017-10-11 18:45:45 +02:00
(exwm-input-release-keyboard (exwm--buffer->id (window-buffer)))))
2018-02-15 13:28:49 +01:00
(add-hook 'exwm-manage-finish-hook 'ambrevar/exwm-start-in-char-mode)
2017-10-11 18:45:45 +02:00
;; Function to automatically toggle between internal/external screens.
(defun ambrevar/exwm-change-screen-hook ()
(let ((xrandr-output-regexp "\n\\([^ ]+\\) connected ")
2019-01-13 15:31:56 +01:00
(xrandr-monitor-regexp "\n .* \\([^ \n]+\\)")
default-output)
(with-temp-buffer
(call-process "xrandr" nil t nil)
(goto-char (point-min))
(re-search-forward xrandr-output-regexp nil 'noerror)
(setq default-output (match-string 1))
(forward-line)
(if (not (re-search-forward xrandr-output-regexp nil 'noerror))
2019-01-13 15:31:56 +01:00
(progn
(call-process "xrandr" nil nil nil "--output" default-output "--auto" "--primary")
(with-temp-buffer
;; Turn off all monitors that are not DEFAULT-OUTPUT.
;; See "--auto" in xrandr(1) and https://github.com/ch11ng/exwm/issues/529.
(call-process "xrandr" nil t nil "--listactivemonitors")
(goto-char (point-min))
(while (not (eobp))
(when (and (re-search-forward xrandr-monitor-regexp nil 'noerror)
(not (string= (match-string 1) default-output)))
(call-process "xrandr" nil nil nil "--output" (match-string 1) "--auto")))))
(call-process
"xrandr" nil nil nil
"--output" (match-string 1) "--primary" "--auto"
"--output" default-output "--off")
2019-01-13 15:31:56 +01:00
(setq exwm-randr-workspace-monitor-plist (list 0 (match-string 1)))))))
(require 'exwm-randr)
(add-hook 'exwm-randr-screen-change-hook 'ambrevar/exwm-change-screen-hook)
(exwm-randr-enable)
2017-07-26 18:39:19 +02:00
(provide 'init-exwm)