unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* open in browser
@ 2017-01-27 12:59 Martin
  2017-01-27 18:16 ` John Mastro
  0 siblings, 1 reply; 3+ messages in thread
From: Martin @ 2017-01-27 12:59 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

Hi there,

at the moment I'm using

;; (setq browse-url-browser-function 'browse-url-chromium)
(setq browse-url-browser-function 'browse-url-firefox)

to determine which browser to use within emacs.  For some reasons I
switch between firefox and chromium, and now I wonder if I could get
emacs to use the browser I'm using at the moment.  But I don't really
know where to start maybe you can help me.

I'm using arch linux with emacs 25.1.1 here.

If firefox is running (we can see it in proc maybe get it with ps) I
want emacs to open links in firefox and if chromium is running I want
emacs to open links in chromium.  And if both are running i would
prefer chromium.

How could a function for browse-url-browser-function look like for that
purpose?

Thanks,
Martin

--
Lösungen statt Diagnosen



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

* Re: open in browser
  2017-01-27 12:59 open in browser Martin
@ 2017-01-27 18:16 ` John Mastro
  2017-01-29 11:38   ` Martin
  0 siblings, 1 reply; 3+ messages in thread
From: John Mastro @ 2017-01-27 18:16 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org; +Cc: Martin

Martin <mylists@kaffanke.at> wrote:
> Hi there,
>
> at the moment I'm using
>
> ;; (setq browse-url-browser-function 'browse-url-chromium)
> (setq browse-url-browser-function 'browse-url-firefox)
>
> to determine which browser to use within emacs.  For some reasons I
> switch between firefox and chromium, and now I wonder if I could get
> emacs to use the browser I'm using at the moment.  But I don't really
> know where to start maybe you can help me.
>
> I'm using arch linux with emacs 25.1.1 here.
>
> If firefox is running (we can see it in proc maybe get it with ps) I
> want emacs to open links in firefox and if chromium is running I want
> emacs to open links in chromium.  And if both are running i would
> prefer chromium.
>
> How could a function for browse-url-browser-function look like for that
> purpose?

Not really tested, but maybe something along these lines would work:

(require 'seq)

(defun find-current-browser ()
  (let ((regexp "\\`\\(chrome\\|chromium\\|firefox\\)\\(\\.exe\\)?\\'"))
    (seq-some (lambda (pid)
                (let ((comm (alist-get 'comm (process-attributes pid))))
                  (when (string-match regexp comm)
                    (intern (match-string 1 comm)))))
              (list-system-processes))))

(defun browse-url-dynamic-browser-function (&rest args)
  (apply (pcase (find-current-browser)
           (`chrome   #'browse-url-chrome)
           (`chromium #'browse-url-chromium)
           (`firefox  #'browse-url-firefox)
           (_         #'browse-url-default-browser))
         args))

(setq browse-url-browser-function #'browse-url-dynamic-browser-function)

Hope that helps

        John



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

* Re: open in browser
  2017-01-27 18:16 ` John Mastro
@ 2017-01-29 11:38   ` Martin
  0 siblings, 0 replies; 3+ messages in thread
From: Martin @ 2017-01-29 11:38 UTC (permalink / raw)
  To: John Mastro; +Cc: help-gnu-emacs@gnu.org

John Mastro writes:

> Martin <mylists@kaffanke.at> wrote:
>> How could a function for browse-url-browser-function look like for that
>> purpose?
>
> Not really tested, but maybe something along these lines would work:
>
> [skipping your code]

Works great, thanks.  (I just took out the .exe thing, because I'm on
linux, but it works with it too.)

Martin

--
Lösungen statt Diagnosen



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

end of thread, other threads:[~2017-01-29 11:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-27 12:59 open in browser Martin
2017-01-27 18:16 ` John Mastro
2017-01-29 11:38   ` Martin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).