help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Making browse-url-firefox more robust?


From: Tim Landscheidt
Subject: Making browse-url-firefox more robust?
Date: Fri, 04 Feb 2022 22:51:05 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)

Hi,

I have set browse-url-browser-function to
'browse-url-firefox (and browse-url-firefox-arguments to
'("-P" "default")) and it works reasonably fine: I see a
link in Gnus or elsewhere in Emacs, I click on it, the link
opens in a new tab in Firefox.

Where it becomes a nuisance is when I don't need to open one
link, but many, for example generated by some database query
or generator function:

| (dotimes (i 100)
|   (browse-url (format "https://some.url/%d"; i)))

In this case, each call to browse-url launches a separate,
memory-hungry Firefox instance which tries to connect to the
initial instance and either succeeds or fails after some
time in which my system is essentially unusable.

So for situations where I anticipate a large number of URLs
to be browsed, I replace the calls to browse-url with accu-
mulating the URLs in a list and then executing:

| (apply 'call-process "firefox" nil "*firefox*" nil urls)

This works, but is rather distracting for (mapc #'browse-url
(generate-urls)) oneliners in *scratch* or IELM.  So I came
up with:

| (defvar accumulated-browse-url-timer nil
|   "Timer used for accumulating browsing URLs.")

| (defvar accumulated-browse-url-urls nil
|   "Accumulated URLs to browse.")

| (defun accumulated-browse-url (URL &rest ARGS)
|   "Browse URL accumulatedly."
|   (if accumulated-browse-url-timer
|       (cancel-timer accumulated-browse-url-timer))
|   (push URL accumulated-browse-url-urls)
|   (setq accumulated-browse-url-timer
|         (run-with-idle-timer 5 nil
|                              (lambda ()
|                                (apply
|                                 'call-process
|                                 "firefox"
|                                 nil
|                                 "*firefox*"
|                                 nil
|                                 (reverse accumulated-browse-url-urls))
|                                (setq accumulated-browse-url-urls nil)))))

which collects all URLs to be browsed until Emacs is idling
for five seconds and then launch a single Firefox instance
that then can communicate with the initial Firefox instance
and open a new window.

This works even better, but it does not solve the underlying
problem in The Right Way™: It just thinks five seconds sure-
ly must be enough instead of letting Firefox open the
link(s) and then waiting for it to sort itself out (or wait-
ing for Firefox to be "ready" and then open the link).
Also, if one just wants to open one link, one has to wait
(at least) those five seconds to look at the URL in Firefox.

Are there other solutions to this problem that are more
robust?

TIA,
Tim




reply via email to

[Prev in Thread] Current Thread [Next in Thread]