1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
;;; browser-config.el --- Browser Configuration -*- lexical-binding: t; coding: utf-8; -*-
;; author: Craig Jennings <c@cjennings.net>
;;; Commentary:
;;
;; Layer: 3 (Domain Workflow).
;; Category: D/P.
;; Load shape: eager.
;; Eager reason: registers one browser-selection key; a command-loaded deferral
;; candidate.
;; Top-level side effects: one global key binding.
;; Runtime requires: cl-lib.
;; Direct test load: yes.
;;
;; This module provides browser selection and configuration for Emacs.
;; It automatically discovers available browsers on the system, allows the user
;; to choose their preferred browser via completing-read, and persists the choice
;; to a file. Works with all link types including org-mode links.
;;
;; Interactive Commands:
;; M-x cj/choose-browser - Select default browser from available options
;;; Code:
(require 'cl-lib)
;; Persistence file for storing browser choice
(defvar cj/browser-choice-file
(expand-file-name "browser-choice.el" user-emacs-directory)
"File to persist the user's browser choice.")
;; Browser definitions: (executable-name browse-function display-name [program-var])
;; Use nil for executable-name to indicate a built-in Emacs browser (always available)
(defvar cj/browser-definitions
'((nil eww-browse-url "EWW (Emacs Browser)" nil)
("google-chrome" browse-url-chrome "Google Chrome" browse-url-chrome-program)
("google-chrome-stable" browse-url-chrome "Google Chrome" browse-url-chrome-program)
("chrome" browse-url-chrome "Chrome" browse-url-chrome-program)
("chromium" browse-url-chromium "Chromium" browse-url-chromium-program)
("chromium-browser" browse-url-chromium "Chromium" browse-url-chromium-program)
("firefox" browse-url-firefox "Firefox" browse-url-firefox-program)
("brave" browse-url-chrome "Brave" browse-url-chrome-program)
("brave-browser" browse-url-chrome "Brave Browser" browse-url-chrome-program)
("microsoft-edge" browse-url-chrome "Microsoft Edge" browse-url-chrome-program)
("vivaldi" browse-url-chrome "Vivaldi" browse-url-chrome-program)
("opera" browse-url-chrome "Opera" browse-url-chrome-program)
("qutebrowser" browse-url-generic "qutebrowser" browse-url-generic-program))
"List of browser definitions.
Each entry is (EXECUTABLE BROWSE-FUNCTION DISPLAY-NAME [PROGRAM-VAR]).
Use nil for EXECUTABLE to indicate a built-in Emacs browser (always available).")
(defun cj/discover-browsers ()
"Discover available browsers on the system.
Returns a list of plists with :executable, :function, :name, and :path.
Includes built-in Emacs browsers (those with nil executable)."
(let ((found-browsers '())
(seen-names (make-hash-table :test 'equal)))
(dolist (def cj/browser-definitions)
(let* ((executable (nth 0 def))
(browse-fn (nth 1 def))
(display-name (nth 2 def))
(program-var (nth 3 def))
(path (when executable (executable-find executable)))
;; Built-in browsers (nil executable) are always available
(available-p (or (null executable) path)))
(when (and available-p (not (gethash display-name seen-names)))
(puthash display-name t seen-names)
(push (list :executable executable
:function browse-fn
:name display-name
:path path
:program-var program-var)
found-browsers))))
(nreverse found-browsers)))
(defun cj/save-browser-choice (browser-plist)
"Save BROWSER-PLIST to the persistence file."
(with-temp-file cj/browser-choice-file
(insert ";;; browser-choice.el --- Generated browser selection -*- lexical-binding: t; -*-\n")
(insert ";;\n")
(insert ";; Generated by browser-config.el. Do not edit by hand; use\n")
(insert ";; `cj/choose-browser' to rewrite this file.\n")
(insert (format "(setq cj/saved-browser-choice '%S)\n" browser-plist))))
(defun cj/load-browser-choice ()
"Load browser choice from the persistence file.
Returns the browser plist if found, nil otherwise."
(when (file-exists-p cj/browser-choice-file)
(condition-case nil
(progn
(load cj/browser-choice-file)
(when (boundp 'cj/saved-browser-choice)
cj/saved-browser-choice))
(error nil))))
(defun cj/--do-apply-browser-choice (browser-plist)
"Apply the browser settings from BROWSER-PLIST.
Returns: \\='success if applied successfully,
\\='invalid-plist if browser-plist is nil or missing required keys."
(if (null browser-plist)
'invalid-plist
(let ((browse-fn (plist-get browser-plist :function))
(executable (plist-get browser-plist :executable))
(path (plist-get browser-plist :path))
(program-var (plist-get browser-plist :program-var)))
(if (null browse-fn)
'invalid-plist
(setq browse-url-browser-function browse-fn)
;; Set the specific browser program variable if it exists
(when program-var
(set program-var (or path executable)))
'success))))
(defun cj/--do-choose-browser (browser-plist)
"Save and apply BROWSER-PLIST as the default browser.
Returns: \\='success if browser was saved and applied,
\\='save-failed if save operation failed,
\\='invalid-plist if browser-plist is invalid."
(condition-case _err
(progn
(cj/save-browser-choice browser-plist)
(let ((result (cj/--do-apply-browser-choice browser-plist)))
(if (eq result 'success)
'success
'invalid-plist)))
(error 'save-failed)))
(defun cj/choose-browser ()
"Interactively choose a browser from available options.
Persists the choice for future sessions."
(interactive)
(let* ((browsers (cj/discover-browsers))
(choices (mapcar (lambda (b) (plist-get b :name)) browsers)))
(if (null browsers)
(message "No supported browsers found on system PATH")
(let* ((choice (completing-read "Choose default browser: " choices nil t))
(selected (cl-find-if (lambda (b)
(string= (plist-get b :name) choice))
browsers)))
(when selected
(pcase (cj/--do-choose-browser selected)
('success (message "Default browser set to: %s" (plist-get selected :name)))
('save-failed (message "Failed to save browser choice"))
('invalid-plist (message "Invalid browser configuration"))))))))
(defun cj/--preferred-default-browser (browsers)
"Return the browser plist to adopt as the first-run default from BROWSERS.
Prefers the first entry with a non-nil :executable -- a real external
browser -- and falls back to the first entry overall when none is
installed. Built-in browsers carry a nil :executable and so are always
\"available\", which put EWW at the head of `cj/discover-browsers' on
every machine. Taking the head therefore opened every link in the text
browser on a fresh checkout even with Chrome installed, until the user
happened to run `cj/choose-browser'. EWW stays reachable as the
deliberate fallback when nothing external is on PATH.
Returns nil for an empty BROWSERS list."
(or (seq-find (lambda (b) (plist-get b :executable)) browsers)
(car browsers)))
;; Initialize: Load saved choice or use the preferred available browser
(defun cj/--do-initialize-browser ()
"Initialize browser configuration.
Returns: (cons \\='loaded browser-plist) if saved choice was loaded,
(cons \\='first-available browser-plist) if using first
discovered browser,
(cons \\='no-browsers nil) if no browsers found."
(let ((saved-choice (cj/load-browser-choice)))
(if saved-choice
(cons 'loaded saved-choice)
;; No saved choice - adopt the preferred available browser
(let ((browsers (cj/discover-browsers)))
(if browsers
(cons 'first-available (cj/--preferred-default-browser browsers))
(cons 'no-browsers nil))))))
(defun cj/initialize-browser ()
"Initialize browser configuration on startup."
(let ((result (cj/--do-initialize-browser)))
(pcase (car result)
('loaded
(cj/--do-apply-browser-choice (cdr result)))
('first-available
(let ((browser (cdr result)))
(cj/--do-apply-browser-choice browser)
(message "No browser configured. Using %s. Run M-x cj/choose-browser to change."
(plist-get browser :name))))
('no-browsers
(message "No supported browsers found")))))
;; Run initialization
(cj/initialize-browser)
(keymap-global-set "C-; B" #'cj/choose-browser)
(provide 'browser-config)
;;; browser-config.el ends here
|