From f89c89a6ea507758340e25d71ac0e2f1942008a1 Mon Sep 17 00:00:00 2001 From: Vincent Zhang Date: Thu, 8 Aug 2024 15:52:40 +0800 Subject: [PATCH] feat: browse url via xwidget if possible --- lisp/init-funcs.el | 25 ++++++++++++++++--------- lisp/init-markdown.el | 2 +- lisp/init-org.el | 12 +++++------- lisp/init-reader.el | 18 ++++++------------ 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lisp/init-funcs.el b/lisp/init-funcs.el index faae8e03d..c9e6830d5 100644 --- a/lisp/init-funcs.el +++ b/lisp/init-funcs.el @@ -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) @@ -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))) @@ -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)))) diff --git a/lisp/init-markdown.el b/lisp/init-markdown.el index 0791e492a..fdbefb3c0 100644 --- a/lisp/init-markdown.el +++ b/lisp/init-markdown.el @@ -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)) diff --git a/lisp/init-org.el b/lisp/init-org.el index 1c2803c42..38356cda7 100644 --- a/lisp/init-org.el +++ b/lisp/init-org.el @@ -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) @@ -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 @@ -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) @@ -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)) @@ -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) diff --git a/lisp/init-reader.el b/lisp/init-reader.el index 63fc26202..6b654ecb0 100644 --- a/lisp/init-reader.el +++ b/lisp/init-reader.el @@ -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) @@ -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))))