From d60fa4e873fbf9db6fa04667f04197341c97cf76 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 24 Jul 2024 00:25:57 -0400 Subject: [PATCH] Move eww init to its own file and add syntax highlighting to pre tags --- emacs/.emacs.d/config/init-built-ins.el | 81 ---------------------- emacs/.emacs.d/config/init-eww.el | 91 +++++++++++++++++++++++++ emacs/.emacs.d/init.el | 1 + 3 files changed, 92 insertions(+), 81 deletions(-) create mode 100644 emacs/.emacs.d/config/init-eww.el diff --git a/emacs/.emacs.d/config/init-built-ins.el b/emacs/.emacs.d/config/init-built-ins.el index 6df150d..089bfa6 100644 --- a/emacs/.emacs.d/config/init-built-ins.el +++ b/emacs/.emacs.d/config/init-built-ins.el @@ -80,85 +80,4 @@ (browse-url url-or-symbol) (browse-url (format "https://www.google.com/search?q=%s" url-or-symbol))))) -(use-package eww - :straight (:type built-in) - :config - (defun eww-before-advice (&rest args) - (interactive - (let* ((uris (eww-suggested-uris)) - (browser-history (mapcar (lambda (h) (plist-get h :url)) eww-history)) - (suggestions (delete-dups (append uris eww-prompt-history browser-history))) - (current-uri (plist-get eww-data :url))) - (list (completing-read "URL or keywords: " suggestions nil nil current-uri 'eww-prompt-history) - current-prefix-arg)))) - (advice-add 'eww :before #'eww-before-advice) - (defun eww-rename-buffer-to-title () - (rename-buffer (format "*eww: %s*" (plist-get eww-data :title)) t)) - (add-hook 'eww-after-render-hook #'eww-rename-buffer-to-title) - ;; Copied from https://protesilaos.com/emacs/dotemacs#h:abc20037-7a4f-4555-809a-dc4165c5db6a - (defun eww-capture-urls-on-page (&optional position) - "Capture all the links on the current web page. - -Return a list of strings. Strings are in the form LABEL @ URL. -When optional argument POSITION is non-nil, include position info -in the strings too, so strings take the form -LABEL @ URL ~ POSITION." - (let (links match) - (save-excursion - (goto-char (point-max)) - ;; NOTE 2021-07-25: The first clause in the `or' is meant to - ;; address a bug where if a URL is in `point-min' it does not get - ;; captured. - (while (setq match (text-property-search-backward 'shr-url)) - (let* ((raw-url (prop-match-value match)) - (start-point-prop (prop-match-beginning match)) - (end-point-prop (prop-match-end match)) - (url (when (stringp raw-url) - (propertize raw-url 'face 'link))) - (label (replace-regexp-in-string "\n" " " ; NOTE 2021-07-25: newlines break completion - (buffer-substring-no-properties - start-point-prop end-point-prop))) - (point start-point-prop) - (line (line-number-at-pos point t)) - (column (save-excursion (goto-char point) (current-column))) - (coordinates (propertize - (format "%d,%d (%d)" line column point) - 'face 'shadow))) - (when url - (if position - (push (format "%-15s ~ %s @ %s" - coordinates label url) - links) - (push (format "%s @ %s" - label url) - links)))))) - links)) - (defun eww-visit-url-on-page (&optional arg) - "Visit URL from list of links on the page using completion. - -With optional prefix ARG (\\[universal-argument]) open URL in a -new EWW buffer." - (interactive "P") - (when (derived-mode-p 'eww-mode) - (let* ((links (eww-capture-urls-on-page)) - (selection (completing-read "Browse URL: " links nil t)) - (url (replace-regexp-in-string ".*@ " "" selection))) - (eww url (when arg 4))))) - (defun eww-jump-to-url-on-page (&optional arg) - "Jump to URL position on the page using completion." - (interactive "P") - (when (derived-mode-p 'eww-mode) - (let* ((links (eww-capture-urls-on-page t)) - (prompt (format "Jump to URL: ")) - (selection (completing-read prompt links nil t)) - (position (replace-regexp-in-string "^.*(\\([0-9]+\\))[\s\t]+~" "\\1" selection)) - (point (string-to-number position))) - (goto-char point)))) - :general - (leader-map "E" #'eww) - (normal eww-mode-map - "go" #'eww - "gJ"#'eww-jump-to-url-on-page - "gV" #'eww-visit-url-on-page)) - (provide 'init-built-ins) diff --git a/emacs/.emacs.d/config/init-eww.el b/emacs/.emacs.d/config/init-eww.el new file mode 100644 index 0000000..1313482 --- /dev/null +++ b/emacs/.emacs.d/config/init-eww.el @@ -0,0 +1,91 @@ +;; -*- lexical-binding: t; -*- + +(use-package eww + :straight (:type built-in) + :config + (defun eww-before-advice (&rest args) + (interactive + (let* ((uris (eww-suggested-uris)) + (browser-history (mapcar (lambda (h) (plist-get h :url)) eww-history)) + (suggestions (delete-dups (append uris eww-prompt-history browser-history))) + (current-uri (plist-get eww-data :url))) + (list (completing-read "URL or keywords: " suggestions nil nil current-uri 'eww-prompt-history) + current-prefix-arg)))) + (advice-add 'eww :before #'eww-before-advice) + (defun eww-rename-buffer-to-title () + (rename-buffer (format "*eww: %s*" (plist-get eww-data :title)) t)) + (add-hook 'eww-after-render-hook #'eww-rename-buffer-to-title) + ;; Copied from https://protesilaos.com/emacs/dotemacs#h:abc20037-7a4f-4555-809a-dc4165c5db6a + (defun eww-capture-urls-on-page (&optional position) + "Capture all the links on the current web page. + +Return a list of strings. Strings are in the form LABEL @ URL. +When optional argument POSITION is non-nil, include position info +in the strings too, so strings take the form +LABEL @ URL ~ POSITION." + (let (links match) + (save-excursion + (goto-char (point-max)) + ;; NOTE 2021-07-25: The first clause in the `or' is meant to + ;; address a bug where if a URL is in `point-min' it does not get + ;; captured. + (while (setq match (text-property-search-backward 'shr-url)) + (let* ((raw-url (prop-match-value match)) + (start-point-prop (prop-match-beginning match)) + (end-point-prop (prop-match-end match)) + (url (when (stringp raw-url) + (propertize raw-url 'face 'link))) + (label (replace-regexp-in-string "\n" " " ; NOTE 2021-07-25: newlines break completion + (buffer-substring-no-properties + start-point-prop end-point-prop))) + (point start-point-prop) + (line (line-number-at-pos point t)) + (column (save-excursion (goto-char point) (current-column))) + (coordinates (propertize + (format "%d,%d (%d)" line column point) + 'face 'shadow))) + (when url + (if position + (push (format "%-15s ~ %s @ %s" + coordinates label url) + links) + (push (format "%s @ %s" + label url) + links)))))) + links)) + (defun eww-visit-url-on-page (&optional arg) + "Visit URL from list of links on the page using completion. + +With optional prefix ARG (\\[universal-argument]) open URL in a +new EWW buffer." + (interactive "P") + (when (derived-mode-p 'eww-mode) + (let* ((links (eww-capture-urls-on-page)) + (selection (completing-read "Browse URL: " links nil t)) + (url (replace-regexp-in-string ".*@ " "" selection))) + (eww url (when arg 4))))) + (defun eww-jump-to-url-on-page (&optional arg) + "Jump to URL position on the page using completion." + (interactive "P") + (when (derived-mode-p 'eww-mode) + (let* ((links (eww-capture-urls-on-page t)) + (prompt (format "Jump to URL: ")) + (selection (completing-read prompt links nil t)) + (position (replace-regexp-in-string "^.*(\\([0-9]+\\))[\s\t]+~" "\\1" selection)) + (point (string-to-number position))) + (goto-char point)))) + :general + (leader-map "E" #'eww) + (normal eww-mode-map + "go" #'eww + "gJ"#'eww-jump-to-url-on-page + "gV" #'eww-visit-url-on-page)) + +(use-package shr-tag-pre-highlight + :ensure t + :after shr + :config + (add-to-list 'shr-external-rendering-functions + '(pre . shr-tag-pre-highlight))) + +(provide 'init-eww) diff --git a/emacs/.emacs.d/init.el b/emacs/.emacs.d/init.el index 15fe119..2fb8b6b 100644 --- a/emacs/.emacs.d/init.el +++ b/emacs/.emacs.d/init.el @@ -123,6 +123,7 @@ (require 'init-kubernetes) (require 'init-epub) (require 'init-pdf) +(require 'init-eww) (require 'init-homebrew) (require 'init-elfeed) (require 'init-1pass)