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

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

Re: open in browser


From: John Mastro
Subject: Re: open in browser
Date: Fri, 27 Jan 2017 10:16:41 -0800

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



reply via email to

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