3

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
crocefisso
  • 1,141
  • 7
  • 15

1 Answers1

4
(defun cycle-incognito ()
  (interactive)
  (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).

dalanicolai
  • 6,108
  • 7
  • 23
  • 1
    Great, thank you! It works like a charm. Just had to change `"chrome"` to `"chromium"`. – crocefisso Dec 31 '22 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. – dalanicolai Dec 31 '22 at 17:51