Skip to content

Commit

Permalink
feat: browse url via xwidget if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
seagle0128 committed Aug 8, 2024
1 parent 01becb1 commit f89c89a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
25 changes: 16 additions & 9 deletions lisp/init-funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,24 @@ Same as '`replace-string' `C-q' `C-m' `RET' `RET''."
(warn "Current buffer is not attached to a file!"))))

;; Browse URL
(defun centaur-browse-url (url)
"Open URL using a configurable method.
See `browse-url' for more details."
(interactive (progn
(require 'browse-url)
(browse-url-interactive-arg "URL: ")))
(if (and (featurep 'xwidget-internal) (display-graphic-p))
(centaur-webkit-browse-url url t)
(browse-url url)))

(defun centaur-webkit-browse-url (url &optional pop-buffer new-session)
"Browse URL with xwidget-webkit' and switch or pop to the buffer.
POP-BUFFER specifies whether to pop to the buffer.
NEW-SESSION specifies whether to create a new xwidget-webkit session."
NEW-SESSION specifies whether to create a new xwidget-webkit session.
Interactively, URL defaults to the string looking like a url around point."
(interactive (progn
(require 'browse-url)
(browse-url-interactive-arg "xwidget-webkit URL: ")))
(or (featurep 'xwidget-internal)
(user-error "Your Emacs was not compiled with xwidgets support"))

(browse-url-interactive-arg "URL: ")))
(xwidget-webkit-browse-url url new-session)
(let ((buf (xwidget-buffer (xwidget-webkit-current-session))))
(when (buffer-live-p buf)
Expand Down Expand Up @@ -242,7 +249,7 @@ Same as '`replace-string' `C-q' `C-m' `RET' `RET''."

(defun centaur-treesit-available-p ()
"Check whether tree-sitter is available.
Native tree-sitter is introduced since 29.1."
Native tree-sitter is introduced since 29.1."
(and centaur-tree-sitter
(fboundp 'treesit-available-p)
(treesit-available-p)))
Expand All @@ -259,8 +266,8 @@ Native tree-sitter is introduced since 29.1."
(goto-char (point-min))
(while (re-search-forward
(format "^[\t ]*[;]*[\t ]*(setq %s .*)" variable)
nil t)
(replace-match (format "(setq %s '%s)" variable value) nil nil))
nil t)
(replace-match (format "(setq %s '%s)" variable value) nil nil))
(write-region nil nil custom-file)
(message "Saved %s (%s) to %s" variable value custom-file))))

Expand Down
2 changes: 1 addition & 1 deletion lisp/init-markdown.el
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mermaid.initialize({
;; Preview with built-in webkit
(defun my-markdown-export-and-preview (fn)
"Preview with `xwidget' if applicable, otherwise with the default browser."
(if (featurep 'xwidget-internal)
(if (and (featurep 'xwidget-internal) (display-graphic-p))
(centaur-webkit-browse-url (concat "file://" (markdown-export)) t)
(funcall fn)))
(advice-add #'markdown-export-and-preview :around #'my-markdown-export-and-preview))
Expand Down
12 changes: 5 additions & 7 deletions lisp/init-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ prepended to the element after the #+HEADER: tag."
(add-to-list 'org-structure-template-alist '("n" . "note"))

;; Use embedded webkit browser if possible
(when (featurep 'xwidget-internal)
(when (and (featurep 'xwidget-internal) (display-graphic-p))
(push '("\\.\\(x?html?\\|pdf\\)\\'"
.
(lambda (file _link)
Expand Down Expand Up @@ -255,7 +255,7 @@ prepended to the element after the #+HEADER: tag."
:diminish
:bind (:map org-mode-map
("C-c C-h" . org-preview-html-mode))
:init (when (featurep 'xwidget-internal)
:init (when (and (featurep 'xwidget-internal) (display-graphic-p))
(setq org-preview-html-viewer 'xwidget)))

;; Presentation
Expand Down Expand Up @@ -304,6 +304,7 @@ prepended to the element after the #+HEADER: tag."
;; Roam
(use-package org-roam
:diminish
:functions centaur-browse-url
:defines org-roam-graph-viewer
:bind (("C-c n l" . org-roam-buffer-toggle)
("C-c n f" . org-roam-node-find)
Expand All @@ -314,9 +315,7 @@ prepended to the element after the #+HEADER: tag."
:init
(setq org-roam-directory (file-truename centaur-org-directory)
org-roam-node-display-template (concat "${title:*} " (propertize "${tags:10}" 'face 'org-tag))
org-roam-graph-viewer (if (featurep 'xwidget-internal)
#'xwidget-webkit-browse-url
#'browse-url))
org-roam-graph-viewer #'centaur-browse-url)
:config
(unless (file-exists-p org-roam-directory)
(make-directory org-roam-directory))
Expand All @@ -326,8 +325,7 @@ prepended to the element after the #+HEADER: tag."

(use-package org-roam-ui
:bind ("C-c n u" . org-roam-ui-mode)
:init (when (featurep 'xwidget-internal)
(setq org-roam-ui-browser-function #'xwidget-webkit-browse-url)))
:init (setq org-roam-ui-browser-function #'centaur-browse-url))

(provide 'init-org)

Expand Down
18 changes: 6 additions & 12 deletions lisp/init-reader.el
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,9 @@ browser defined by `browse-url-generic-program'."
(let ((link (elfeed-entry-link elfeed-show-entry)))
(when link
(message "Sent to browser: %s" link)
(cond
((featurep 'xwidget-internal)
(centaur-webkit-browse-url link))
(use-generic-p
(browse-url-generic link))
(t (browse-url link))))))
(if use-generic-p
(browse-url-generic link)
(centaur-browse-url link)))))
(advice-add #'elfeed-show-visit :override #'my-elfeed-show-visit)

(defun my-elfeed-search-browse-url (&optional use-generic-p)
Expand All @@ -214,12 +211,9 @@ browser defined by `browse-url-generic-program'."
(cl-loop for entry in entries
do (elfeed-untag entry 'unread)
when (elfeed-entry-link entry)
do (cond
((featurep 'xwidget-internal)
(centaur-webkit-browse-url it t))
(use-generic-p
(browse-url-generic it))
(t (browse-url it))))
do (if use-generic-p
(browse-url-generic it)
(centaur-browse-url it)))
(mapc #'elfeed-search-update-entry entries)
(unless (or elfeed-search-remain-on-entry (use-region-p))
(forward-line))))
Expand Down

0 comments on commit f89c89a

Please sign in to comment.