I set chromium as my default browser by putting the following lines in my init:

(setq browse-url-browser-function 'browse-url-generic
      browse-url-generic-program "chromium") 

Sometimes I need to browse with Firefox in private mode, which is feasible with the following setting:

(setq browse-url-browser-function 'browse-url-generic
      browse-url-generic-program "firefox"
      browse-url-generic-args (quote ("--private-window")))

I'd like to know how I could create a function (e.g. defun open-url-incognito ()) that could toggle (e.g. (define-key org-mode-map (kbd "C-*") open-url-incognito)) between these two settings.

1 Answer 1

(defun cycle-incognito ()
  (let ((browsers '("chromium" "firefox")))
    (setq browse-url-generic-program
          (or (cadr (member browse-url-generic-program browsers))
              (car browsers))))
  (setq browse-url-generic-args (when (equal browse-url-generic-program "firefox")
                                  (list "--private-window")))
  (message (string-join (cons browse-url-generic-program browse-url-generic-args) " ")))

(global-set-key (kbd "C-*") #'cycle-incognito)

Of course for a simple toggle function you could replace the or form with an if (or a pcase), but the code above implements the more general version that cycles over a list (for a list of two elements this effectively becomes a toggle).

  • 1
    Great, thank you! It works like a charm. Just had to change "chrome" to "chromium".
    – crocefisso
    Dec 31, 2022 at 17:34
  • 1
    Great, I've corrected the answer. I also noticed that on some previous 'edit' of the answer, I removed the keybinding definition. I have placed it back, as I wanted to suggest using a global keybinding instead of the mode specific one. Dec 31, 2022 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.