all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* reclaiming focus after `browse-url'
@ 2022-06-29 16:17 Sam Steingold
  2022-06-30  6:36 ` Emanuel Berg
  0 siblings, 1 reply; 2+ messages in thread
From: Sam Steingold @ 2022-06-29 16:17 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I wrote this code (mostly for hackernews on gnus where every article is
just 2 links):

--8<---------------cut here---------------start------------->8---
(defvar sds-reclaim-focus '(0)
  "Cons cell (how-many-times . frame-to-give-focus-to).")
(defun sds-reclaim-focus ()
  "Re-acquire OS input focus until `(car sds-reclaim-focus)' is 0.
Should be used, after calling 'browse-url' 5 times, as
  (setq sds-reclaim-focus (cons 5 (selected-frame)))
  (add-function :after after-focus-change-function #'sds-reclaim-focus)
it removes itself from 'after-focus-change-function'."
  ;; (message "sds-reclaim-focus %s %s" sds-reclaim-focus
  ;;          (frame-focus-state (cdr sds-reclaim-focus)))
  (unless (eq t (frame-focus-state (cdr sds-reclaim-focus)))
    (x-focus-frame (cdr sds-reclaim-focus))
    ;; (message "focus grab %s" sds-reclaim-focus)
    (if (zerop (car sds-reclaim-focus))
        (remove-function after-focus-change-function #'sds-reclaim-focus)
      (cl-decf (car sds-reclaim-focus)))))

(defun sds-gnus-summary-browse-all-urls ()
  "Scan the current article body for links, and browse them."
  (interactive)
  (let ((urls (gnus-collect-urls-from-article)))
    (dolist (url urls)
      (browse-url url))
    (setq sds-reclaim-focus (cons (length urls) (selected-frame))))
  ;; delete *Article* window, cf "=" bound to 'gnus-summary-expand-window'
  (gnus-configure-windows 'summary 'force)
  (add-function :after after-focus-change-function #'sds-reclaim-focus))

(with-eval-after-load "gnus-sum"
  (define-key gnus-summary-mode-map "v" 'sds-gnus-summary-browse-all-urls))
--8<---------------cut here---------------end--------------->8---

It works when I have just _one_ frame, but with _2_ frames, the focus is
reclaimed to "the other" frame, not the `(cdr sds-reclaim-focus)`.
So, when I hit "v" in the *Summary* buffer, the 2 pages are opened in a
browser, but the OS focus goes to the 2nd frame, not to the *Summary* one.
The other one has the code above open with the point behind the
`x-focus-frame` form, so C-x C-e switches the focus to the correct frame.
(thus, `x-focus-frame' _works_).

I wonder what I am doing wrong, and, maybe, what others use for similar tasks...

Thank you very much!

-- 
Sam Steingold (https://youtube.com/channel/UCiW5WB6K4Fx-NhhtZscW7Og) on darwin Ns 10.3.2113
http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com
https://www.dhimmitude.org https://mideasttruth.com https://camera.org
There is a fine line between a numerator and a denominator.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-30  6:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-29 16:17 reclaiming focus after `browse-url' Sam Steingold
2022-06-30  6:36 ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.