From a4fca96b09bb31a55dd8fde35c30b4bc86e7da10 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Thu, 2 Nov 2017 10:36:46 +0100 Subject: [PATCH] Helm/EXWM: Fix loss of focus --- .emacs.d/lisp/package-helm-exwm.el | 48 ++++++++++++++---------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/.emacs.d/lisp/package-helm-exwm.el b/.emacs.d/lisp/package-helm-exwm.el index ca9d084d..554b8781 100644 --- a/.emacs.d/lisp/package-helm-exwm.el +++ b/.emacs.d/lisp/package-helm-exwm.el @@ -18,13 +18,7 @@ ;; TODO: Write a emacs-buffers helm source to filter out EXWM buffers from buffer list. ;; Write an all-exwm buffer source. -;; 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? +;; REVIEW: Helm buffer does not die? Seems to be fixed. ;; TODO: kill-persistent is not persistent. @@ -98,24 +92,28 @@ 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." - (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) class)) - (if (fboundp 'helm-exwm-buffers) - (helm-exwm-buffers class) - (when other-window (other-window 1)) - (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) 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 program nil program))))) + ;; If current window is not in `exwm-mode' we switch to it. Therefore we must + ;; also make sure that current window is not a Helm buffer, otherwise calling + ;; this function will lose focus in Helm. + (unless helm-alive-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) class)) + (if (fboundp 'helm-exwm-buffers) + (helm-exwm-buffers class) + (when other-window (other-window 1)) + (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) 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 program nil program)))))) (defun helm-exwm-switch-browser () "Switch to some `browse-url-generic-program' windows.