all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sam Steingold <sds@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: reclaiming focus after `browse-url'
Date: Wed, 29 Jun 2022 12:17:11 -0400	[thread overview]
Message-ID: <lzh743qwag.fsf@3c22fb11fdab.ant.amazon.com> (raw)

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.



             reply	other threads:[~2022-06-29 16:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-29 16:17 Sam Steingold [this message]
2022-06-30  6:36 ` reclaiming focus after `browse-url' Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=lzh743qwag.fsf@3c22fb11fdab.ant.amazon.com \
    --to=sds@gnu.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.