aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-10-06 15:07:43 +1100
committerYuchen Pei <hi@ypei.me>2022-10-06 15:07:43 +1100
commit39ac2162c5361260ae6b87911050766a4332c3ed (patch)
treebd22a657489b3c2c0ce39c800d0d8c6ca2738e12
parent9341d7ac6b29bb599209297534ed65e1e4fc091b (diff)
adding luwak-mode
- keymaps for forwarding and backwarding buttons and history
-rw-r--r--luwak.el75
1 files changed, 67 insertions, 8 deletions
diff --git a/luwak.el b/luwak.el
index 4fd69d5..a0c7922 100644
--- a/luwak.el
+++ b/luwak.el
@@ -1,10 +1,29 @@
;; -*- lexical-binding: t; -*-
(defvar luwak-buffer "*luwak*")
+
+(defvar-local luwak-data nil)
+(defvar-local luwak-history nil)
+
(defun luwak-lynx-buffer (url) (format "*luwak-lynx %s*" url))
+(define-derived-mode luwak-mode special-mode "luwak"
+ "Major mode for browsing the web using lynx -dump.")
+
+(defvar luwak-mode-map
+ (let ((kmap (make-sparse-keymap)))
+ (define-key kmap "\t" 'forward-button)
+ (define-key kmap [backtab] 'backward-button)
+ (define-key kmap "g" 'luwak-reload)
+ (define-key kmap "l" 'luwak-history-backward)
+ (define-key kmap "r" 'luwak-history-forward)
+ kmap))
+
(defun luwak-open (url)
(interactive "sUrl to open: ")
(setq url (eww--dwim-expand-url url))
+ (luwak-open-internal url current-prefix-arg 'luwak-add-to-history))
+
+(defun luwak-open-internal (url no-tor &optional cb)
(set-process-sentinel
(start-process-with-torsocks
current-prefix-arg
@@ -12,16 +31,56 @@
"lynx" "-dump" "--display_charset" "utf-8" url)
(lambda (process _)
(with-current-buffer (get-buffer-create luwak-buffer)
- (erase-buffer)
- (insert-buffer-substring (process-buffer process))
- (kill-buffer (process-buffer process))
- (goto-char (point-min))
- (luwak-render-links (luwak-get-links)))
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert-buffer-substring (process-buffer process))
+ (kill-buffer (process-buffer process))
+ (goto-char (point-min))
+ (luwak-render-links (luwak-get-links)))
+ (unless (derived-mode-p 'luwak-mode) (luwak-mode))
+ (if luwak-data
+ (plist-put luwak-data :url url)
+ (setq luwak-data (list :url url :no-tor no-tor :history-pos 0)))
+ (when cb (funcall cb)))
(display-buffer luwak-buffer))))
+(defun luwak-add-to-history ()
+ (let ((history-delete-duplicates nil))
+ (setq luwak-history (nthcdr (plist-get luwak-data :history-pos)
+ luwak-history))
+ (add-to-history 'luwak-history (plist-get luwak-data :url))
+ (plist-put luwak-data :history-pos 0)))
+
+(defun luwak-history-backward ()
+ (interactive)
+ (let ((history-pos
+ (1+ (plist-get luwak-data :history-pos))))
+ (when (<= (length luwak-history) history-pos)
+ (error "Already at the earliest history."))
+ (plist-put luwak-data :history-pos history-pos)
+ (luwak-open-internal (nth history-pos luwak-history)
+ (plist-get luwak-data :no-tor))))
+
+(defun luwak-history-forward ()
+ (interactive)
+ (let ((history-pos
+ (1- (plist-get luwak-data :history-pos))))
+ (when (< history-pos 0)
+ (error "Already at the latest history."))
+ (plist-put luwak-data :history-pos history-pos)
+ (luwak-open-internal (nth history-pos luwak-history)
+ (plist-get luwak-data :no-tor))))
+
+(defun luwak-reload ()
+ (interactive)
+ (luwak-open-internal
+ (plist-get luwak-data :url)
+ (plist-get luwak-data :no-tor)))
+
(defun luwak-follow-link (marker)
- (luwak-open
- (get-text-property marker 'url)))
+ (let ((url (get-text-property marker 'url)))
+ (luwak-open-internal
+ url (plist-get luwak-data :no-tor) 'luwak-add-to-history)))
(defun luwak-render-links (urls)
(with-current-buffer luwak-buffer
@@ -31,7 +90,7 @@
(dolist (url urls)
(when (re-search-forward (format "\\[%d\\]" i) nil t)
(replace-match "")
- (make-text-button (point) (1+ (point))
+ (make-text-button (point) (progn (forward-sexp) (point))
'url url
'action 'luwak-follow-link
'face 'button))