aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-web.el
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-06-17 17:20:29 +1000
committerYuchen Pei <id@ypei.org>2023-06-17 17:20:29 +1000
commit093ffa5fbf7143f4668bb0a3dc9659a5cc836e12 (patch)
tree1ed4e14b2a43b8e338f4ad6a04d969b99b9239be /emacs/.emacs.d/lisp/my/my-web.el
parentabc686827ae38ee715d9eed1c5c29161c74127e6 (diff)
Moving things one level deeper
To ease gnu stow usage. Now we can do stow -t ~ emacs
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-web.el')
-rw-r--r--emacs/.emacs.d/lisp/my/my-web.el129
1 files changed, 129 insertions, 0 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-web.el b/emacs/.emacs.d/lisp/my/my-web.el
new file mode 100644
index 0000000..c8517de
--- /dev/null
+++ b/emacs/.emacs.d/lisp/my/my-web.el
@@ -0,0 +1,129 @@
+;;; my-web.el -- web related extensions for emacs core -*- lexical-binding: t -*-
+
+;; Copyright (C) 2023 Free Software Foundation.
+
+;; Author: Yuchen Pei <id@ypei.org>
+;; Package-Requires: ((emacs "28.2"))
+
+;; This file is part of dotfiles.
+
+;; dotfiles is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU Affero General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
+;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+;; Public License for more details.
+
+;; You should have received a copy of the GNU Affero General Public
+;; License along with dotfiles. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; web related extensions for emacs core. Covers eww etc.
+
+;;; Code:
+
+
+
+(defun my-eww-next-path ()
+ (interactive)
+ (let ((url (plist-get eww-data :url)))
+ (when (string-match "^\\(.*?\\)\\([0-9]+\\)\\(.*\\)$" url)
+ (eww (concat
+ (match-string 1 url)
+ (number-to-string
+ (1+ (string-to-number (match-string 2 url))))
+ (match-string 3 url))))))
+
+(defun my-eww-prev-path ()
+ (interactive)
+ (let ((url (plist-get eww-data :url)))
+ (when (string-match "^\\(.*\\)\\([0-9]+\\)\\(.*\\)$" url)
+ (eww (concat
+ (match-string 1 url)
+ (number-to-string
+ (1- (string-to-number (match-string 2 url))))
+ (match-string 3 url))))))
+
+(defun my-eww-up-path ()
+ (interactive)
+ (let ((url (plist-get eww-data :url)))
+ (when (and (string-match "^\\(.*//.*/\\)[^/]+\\(/\\)?$" url)
+ (match-string 1 url))
+ (eww (match-string 1 url)))))
+
+(defun my-eww-top-path ()
+ (interactive)
+ (let ((url (plist-get eww-data :url)))
+ (when (and (string-match "^\\(.*//.*?/\\).*$" url)
+ (match-string 1 url))
+ (eww (match-string 1 url)))))
+
+(defun my-browse-url-tor-browser (url)
+ "Browse URL with tor-browser."
+ (setq url (browse-url-encode-url url))
+ (start-process (concat "tor-browser " url) nil "tor-browser"
+ "--allow-remote" url))
+
+(defun my-browse-url-firefox-private (url)
+ "Browse URL in a private firefox window."
+ (setq url (browse-url-encode-url url))
+ (start-process (concat "firefox-private " url) nil "firefox"
+ "--private-window" url))
+
+;; TODO: change to using hmm matching url with default app
+;; override browse-url
+(defun my-browse-url (url &optional arg)
+ (interactive "P")
+ (cond ((equal arg '(4))
+ (funcall browse-url-secondary-browser-function url))
+ ((equal arg '(16))
+ (my-browse-url-tor-browser url))
+ (t (luwak-open url))))
+
+;; this fixes clicking url buttons like those in gnus messages
+(defalias 'browse-url-button-open-url 'my-browse-url)
+
+(defun my-browse-url-at-point (arg)
+ (interactive "P")
+ (my-browse-url (browse-url-url-at-point) arg))
+
+;; override eww-copy-page-url to work with bookmark id frags.
+(defun eww-copy-page-url ()
+ "Copy the URL of the current page into the kill ring."
+ (interactive)
+ (let* ((url (plist-get eww-data :url))
+ (id (get-text-property (point) 'shr-frag-id))
+ (url-no-frag
+ (if (string-match "^\\(.*\\)#.*$" url)
+ (match-string 1 url)
+ url))
+ (final-url
+ (if id (concat url-no-frag "#" id)
+ url))
+ )
+ (message "%s" final-url)
+ (kill-new final-url)))
+
+(defun my-eww-switch-by-title (title-and-buffer)
+ "Switches to an eww buffer with selected title."
+ (interactive
+ (list
+ (let ((com-table))
+ (dolist (buffer (buffer-list))
+ (with-current-buffer buffer
+ (when (equal major-mode 'eww-mode)
+ (add-to-list
+ 'com-table
+ (concat (plist-get eww-data :title)
+ (propertize (concat " " (buffer-name))
+ 'invisible t))))))
+ (completing-read "Eww buffer title: " com-table))))
+ (string-match "^.* \\(.*\\)$" title-and-buffer)
+ (switch-to-buffer (match-string 1 title-and-buffer)))
+
+(provide 'my-web)
+;;; my-web.el ends here