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.
;; We don't get the expected error:
;; "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?
@ -26,8 +28,7 @@
;; A workaround would be to discard the result of kill-buffer and print the
;; count manually.
;;; TODO: Rename to helm-exwm-map.
(defvar helm-exwm-browser-map
(defvar helm-exwm-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "C-c o") 'helm-buffer-switch-other-window)
@ -54,12 +55,12 @@
(kill-buffer (car (helm-marked-candidates)))
(message "after"))
(defun helm-exwm-generic-candidates (class-name)
(defun helm-exwm-candidates (class)
(let ((bufs (delq nil (mapcar
(lambda (buf)
(if (with-current-buffer buf
(and (eq major-mode 'exwm-mode)
(string= (downcase exwm-class-name) class-name)))
(string= (downcase exwm-class-name) class)))
(buffer-name buf)
nil))
(buffer-list)))))
@ -68,18 +69,11 @@
(setcdr (last bufs) (list (pop bufs))))
bufs))
(defun helm-exwm-browser-candidates ()
(message "HELM")
(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)
(defun helm-exwm-windows (class)
"Preconfigured `helm' to list EXWM windows belonging to CLASS."
(helm :sources
(helm-build-sync-source "EXWM buffers"
:candidates 'helm-exwm-browser-candidates
;; TODO: Make action variable? Maybe not necessary if this function become generic.
:candidates (helm-exwm-candidates class)
: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 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.
:persistent-action 'helm-buffers-list-persistent-action
;; :update 'helm-exwm-update
:keymap helm-exwm-browser-map)
:keymap helm-exwm-map)
:buffer "*helm-exwm*"))
;; (add-to-list 'helm-source-names-using-follow "helm-exwm"))
;; (setq helm-source-names-using-follow nil)
;;; TODO: Use namespace.
;;; TODO: Make generic function with class-name and program arguments.
(defun exwm-start-browser (&optional other-window)
"Fire-up the web browser as defined in `browse-url-generic-program'.
If current window is the web browser already, fire-up a new window.
If not, switch to the last open window.
If there is none, fire it up.
(defun helm-exwm-switch (class &optional program other-window)
"Switch to some EXWM windows belonging to CLASS.
If current window is not showing CLASS, switch to the last open CLASS window.
If there is none, start PROGRAM.
If PROGRAM is nil, it defaults to CLASS.
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)
(string= (downcase exwm-class-name) (file-name-nondirectory browse-url-generic-program)))
(if (fboundp 'helm-exwm-browser-buffers)
(helm-exwm-browser-buffers)
(string= (downcase exwm-class-name) class))
(if (fboundp 'helm-exwm-windows)
(helm-exwm-windows class)
(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)))
(while (and last
(not (with-current-buffer (car last)
(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)))
(if last
(funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer) (car last))
(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 ()
"Like `exwm-start-browser' but use other window if possible."
(defun helm-exwm-switch-browser ()
"Switch to some `browse-url-generic-program' windows.
See `helm-exwm-start'."
(interactive)
(exwm-start-browser t))
(exwm-input-set-key (kbd "s-w") #'exwm-start-browser)
(exwm-input-set-key (kbd "s-W") #'exwm-start-browser-other-window)
(helm-exwm-switch (file-name-nondirectory browse-url-generic-program) browse-url-generic-program))
(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)