dotfiles/emacs/.emacs.d/config/init-built-ins.el
Jeremy Dormitzer 454ffb7d33 Add EWW functions for URL handling and buffer renaming
This commit introduces three new custom functions for EWW: `eww-rename-buffer-to-title` for renaming buffers based on page titles, `eww-capture-urls-on-page` to capture all links on the current page, `eww-visit-url-on-page` to select and visit links using completion, and `eww-jump-to-url-on-page` to jump to URLs on the page. These enhancements improve the usability and navigation of EWW.
2024-07-23 13:18:09 -04:00

165 lines
5.8 KiB
EmacsLisp

;; -*- lexical-binding: t; -*-
;; Configuration for built-ins that don't fit anywhere else
(use-package custom
:straight (:type built-in)
:defer t)
(use-package view
:straight (:type built-in)
:defer t
:config
(evil-collection-define-key 'normal 'view-mode-map
"0" 'evil-beginning-of-line
"g0" 'text-scale-adjust))
(use-package simple
:straight (:type built-in)
:general
(leader-map "!" #'shell-command
"|" #'shell-command-on-region
";" #'async-shell-command)
(normal special-mode-map
"gr" #'revert-buffer
"q" #'quit-window)
(normal messages-buffer-mode-map
"gr" #'revert-buffer
"q" #'quit-window))
(use-package shell
:straight (:type built-in)
:general
(normal shell-mode-map "q" #'quit-window))
(use-package ediff
:defer t
:straight (:type built-in)
:config
(setq ediff-window-setup-function #'ediff-setup-windows-plain)
;; Restore window configuration after an ediff session
(defvar ediff-last-windows nil)
(defun store-pre-ediff-winconfig ()
(setq ediff-last-windows (current-window-configuration)))
(defun restore-pre-ediff-winconfig ()
(set-window-configuration ediff-last-windows))
(add-hook 'ediff-before-setup-hook #'store-pre-ediff-winconfig)
(add-hook 'ediff-quit-hook #'restore-pre-ediff-winconfig))
(use-package replace
:defer t
:straight (:type built-in))
(use-package man
:straight (:type built-in)
:commands (man)
:custom
(manual-program "gman"))
(use-package re-builder
:straight (:type built-in)
:commands (re-builder)
:custom
(reb-re-syntax 'string))
(use-package hippie-expand
:straight (:type built-in)
:general
([remap dabbrev-expand] #'hippie-expand))
(use-package browse-url
:straight (:type built-in)
:config
(defun browse-url-or-search (url-or-symbol)
"If URL-OR-SYMBOL is a URL, browse it. Otherwise, search for it."
(interactive (list (thing-at-point 'symbol)))
(if (ffap-url-p url-or-symbol)
(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)