Helm/EXWM: Make code more composable

master
Pierre Neidhardt 2017-11-02 10:10:03 +01:00
parent ee26adc834
commit 348be638fd
1 changed files with 42 additions and 34 deletions

View File

@ -16,6 +16,8 @@
;; TODO: s-w s-w loses focus. ;; TODO: s-w s-w loses focus.
;; We don't get the expected error: ;; We don't get the expected error:
;; "helm: Error: Trying to run helm within a running helm session" ;; "helm: Error: Trying to run helm within a running helm session"
;; Instead, another s-w shows
;; "Aborting an helm session running in background"
;; TODO: Helm buffer does not die? ;; TODO: Helm buffer does not die?
@ -26,8 +28,7 @@
;; A workaround would be to discard the result of kill-buffer and print the ;; A workaround would be to discard the result of kill-buffer and print the
;; count manually. ;; count manually.
;;; TODO: Rename to helm-exwm-map. (defvar helm-exwm-map
(defvar helm-exwm-browser-map
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map) (set-keymap-parent map helm-map)
(define-key map (kbd "C-c o") 'helm-buffer-switch-other-window) (define-key map (kbd "C-c o") 'helm-buffer-switch-other-window)
@ -54,12 +55,12 @@
(kill-buffer (car (helm-marked-candidates))) (kill-buffer (car (helm-marked-candidates)))
(message "after")) (message "after"))
(defun helm-exwm-generic-candidates (class-name) (defun helm-exwm-candidates (class)
(let ((bufs (delq nil (mapcar (let ((bufs (delq nil (mapcar
(lambda (buf) (lambda (buf)
(if (with-current-buffer buf (if (with-current-buffer buf
(and (eq major-mode 'exwm-mode) (and (eq major-mode 'exwm-mode)
(string= (downcase exwm-class-name) class-name))) (string= (downcase exwm-class-name) class)))
(buffer-name buf) (buffer-name buf)
nil)) nil))
(buffer-list))))) (buffer-list)))))
@ -68,18 +69,11 @@
(setcdr (last bufs) (list (pop bufs)))) (setcdr (last bufs) (list (pop bufs))))
bufs)) bufs))
(defun helm-exwm-browser-candidates () (defun helm-exwm-windows (class)
(message "HELM") "Preconfigured `helm' to list EXWM windows belonging to CLASS."
(helm-exwm-generic-candidates (file-name-nondirectory browse-url-generic-program)))
;;; TODO: Make the candidate function and argument.
(defun helm-exwm-browser-buffers ()
"Preconfigured `helm' to list browser buffers."
(interactive)
(helm :sources (helm :sources
(helm-build-sync-source "EXWM buffers" (helm-build-sync-source "EXWM buffers"
:candidates 'helm-exwm-browser-candidates :candidates (helm-exwm-candidates class)
;; TODO: Make action variable? Maybe not necessary if this function become generic.
:action '(("Switch to browser buffer(s)" . helm-buffer-switch-buffers) :action '(("Switch to browser buffer(s)" . helm-buffer-switch-buffers)
("Switch to browser buffer(s) in other window `C-c o'" . helm-buffer-switch-buffers-other-window) ("Switch to browser buffer(s) in other window `C-c o'" . helm-buffer-switch-buffers-other-window)
("Switch to browser buffer in other frame `C-c C-o'" . switch-to-buffer-other-frame) ("Switch to browser buffer in other frame `C-c C-o'" . switch-to-buffer-other-frame)
@ -87,44 +81,58 @@
;; When follow-mode is on, the persistent-action allows for multiple candidate selection. ;; When follow-mode is on, the persistent-action allows for multiple candidate selection.
:persistent-action 'helm-buffers-list-persistent-action :persistent-action 'helm-buffers-list-persistent-action
;; :update 'helm-exwm-update ;; :update 'helm-exwm-update
:keymap helm-exwm-browser-map) :keymap helm-exwm-map)
:buffer "*helm-exwm*")) :buffer "*helm-exwm*"))
;; (add-to-list 'helm-source-names-using-follow "helm-exwm")) ;; (add-to-list 'helm-source-names-using-follow "helm-exwm"))
;; (setq helm-source-names-using-follow nil) ;; (setq helm-source-names-using-follow nil)
;;; TODO: Use namespace. (defun helm-exwm-switch (class &optional program other-window)
;;; TODO: Make generic function with class-name and program arguments. "Switch to some EXWM windows belonging to CLASS.
(defun exwm-start-browser (&optional other-window)
"Fire-up the web browser as defined in `browse-url-generic-program'. If current window is not showing CLASS, switch to the last open CLASS window.
If current window is the web browser already, fire-up a new window. If there is none, start PROGRAM.
If not, switch to the last open window.
If there is none, fire it up. If PROGRAM is nil, it defaults to CLASS.
With prefix argument or if OTHER-WINDOW is non-nil, open in other window." With prefix argument or if OTHER-WINDOW is non-nil, open in other window."
(interactive "P") (setq program (or program class)
other-window (or other-window current-prefix-arg))
(if (and (eq major-mode 'exwm-mode) (if (and (eq major-mode 'exwm-mode)
(string= (downcase exwm-class-name) (file-name-nondirectory browse-url-generic-program))) (string= (downcase exwm-class-name) class))
(if (fboundp 'helm-exwm-browser-buffers) (if (fboundp 'helm-exwm-windows)
(helm-exwm-browser-buffers) (helm-exwm-windows class)
(when other-window (other-window 1)) (when other-window (other-window 1))
(start-process-shell-command browse-url-generic-program nil browse-url-generic-program)) (start-process-shell-command program nil program))
(let ((last (buffer-list))) (let ((last (buffer-list)))
(while (and last (while (and last
(not (with-current-buffer (car last) (not (with-current-buffer (car last)
(and (eq major-mode 'exwm-mode) (and (eq major-mode 'exwm-mode)
(string= (downcase exwm-class-name) (file-name-nondirectory browse-url-generic-program)))))) (string= (downcase exwm-class-name) class)))))
(setq last (cdr last))) (setq last (cdr last)))
(if last (if last
(funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer) (car last)) (funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer) (car last))
(when other-window (select-window (split-window-sensibly))) (when other-window (select-window (split-window-sensibly)))
(start-process-shell-command browse-url-generic-program nil browse-url-generic-program))))) (start-process-shell-command program nil program)))))
(defun exwm-start-browser-other-window () (defun helm-exwm-switch-browser ()
"Like `exwm-start-browser' but use other window if possible." "Switch to some `browse-url-generic-program' windows.
See `helm-exwm-start'."
(interactive) (interactive)
(exwm-start-browser t)) (helm-exwm-switch (file-name-nondirectory browse-url-generic-program) browse-url-generic-program))
(exwm-input-set-key (kbd "s-w") #'exwm-start-browser)
(exwm-input-set-key (kbd "s-W") #'exwm-start-browser-other-window) (defun helm-exwm-switch-browser-other-window ()
"Switch to some `browse-url-generic-program' windows in other window.
See `helm-exwm-start'."
(interactive)
(helm-exwm-switch (file-name-nondirectory browse-url-generic-program) browse-url-generic-program t))
(exwm-input-set-key (kbd "s-w") #'helm-exwm-start-browser)
(exwm-input-set-key (kbd "s-W") #'helm-exwm-start-browser-other-window)
;; (defun helm-exwm-browser-candidates ()
;; (helm-exwm-generic-candidates (file-name-nondirectory browse-url-generic-program)))
(provide 'package-helm-exwm) (provide 'package-helm-exwm)