Files
stride-el/stride-el.el
2026-03-26 01:01:07 -07:00

125 lines
3.5 KiB
EmacsLisp

;;; Provides Stride minor mode.
;;;
(require 'eww)
(require 'eww-http-code-advice)
(defgroup stride nil
"Stride custom group."
:group 'novels)
(defcustom stride-nav-file (concat user-emacs-directory "stride-nav.el")
"File that Stride reads site navigation info from."
:group 'stride)
;; (defcustom stride-nav
;; (with-temp-buffer
;; (insert-file-contents stride-nav-file)
;; (read (buffer-string)))
;; "Stride navigation list."
;; :type '()
;; :group 'stride)
(defun stride-read-file ()
"Read `stride-nav' from `stride-nav-file' file."
(interactive)
(with-temp-buffer
(insert-file-contents stride-nav-file)
(setq stride-nav (read (buffer-string)))))
(defun stride-write-file ()
"Write `stride-nav' to `stride-nav-file' file."
(interactive)
(let ((inhibit-message t))
(with-temp-buffer
(insert (pp stride-nav))
(write-file stride-nav-file))) )
(defun stride-match ()
"Return matching stride-nav member."
(interactive)
;; (defvar-local stride-nav '())
(catch 'break
(dolist (s stride-nav)
(if (string-match (car s) (plist-get eww-data :url))
(progn
(throw 'break s))))))
(defun stride-head ()
"Attempt to match site in stride-nav list and go to chapter heading."
(interactive)
;;; first version. Looks for error message string in buffer contents.
;; (catch 'break
;; (unless
;; (catch 'reload
;; (let ((line (buffer-substring-no-properties 1 (line-end-position))))
;; (mapcan
;; (lambda (substr)
;; (if (search substr line)
;; (throw 'break (eww-reload))))
;; '("400 Bad Request"
;; "404 File Not Found")))))
;; (eval (car (cdr (stride-match)))))
;;; second version. Uses error code in eww-data to determine response. Requires eww-render-fix.
;; (let ((status (third (plist-get eww-data :error))))
;; (if (member status '(400 404))
;; (eww-reload)
;; (eval (nth 1 (stride-match)))))
;;; Third version. Only gives eww-reload 3 tries.
(let ((status (third (plist-get eww-data :error)))
(tries (or (plist-get eww-data :tries) 1)))
(if (eq status 400)
(when (<= tries 3)
(plist-put eww-data :tries (+ tries 1))
(message "HTTP/1.1 ERROR 400, retry count: %i" (+ tries 1))
(eww-reload))
(plist-put eww-data :tries 1)
(eval (second (stride-match)))))
)
(defun stride-forward ()
"Goes to next chapter if in view, else advances to next page.
TODO: move function definition to stride-nav for more flexibility
when dealing with dificult sites. then eval the lisp here."
(interactive)
(move-to-window-line -1)
(setq-local bottom (point))
(move-to-window-line 0)
(if (re-search-forward
(car (cdr (cdr (stride-match))))
bottom t)
(progn
(backward-char)
(eww-follow-link))
(scroll-up-command)))
(defun stride-eww-after-render-hook ()
(progn
(stride-head)))
(setq stride-mode-map (make-sparse-keymap))
(define-key stride-mode-map [?\s] 'stride-forward)
;; (define-key stride-mode-map (kbd "<mouse-5>") 'stride-forward)
(define-minor-mode stride-mode
"More intelligent space bar navigation."
:lighter " Stride"
:keymap stride-mode-map
(if stride-mode
(progn
(add-hook 'eww-after-render-hook 'stride-eww-after-render-hook nil t)
(setq stride-nav (with-temp-buffer
(insert-file-contents stride-nav-file)
(read (buffer-string))))
;; (setq-local stride-mode-on t)
;; (stride-update)
)
(remove-hook 'eww-after-render-hook 'stride-eww-after-render-hook t) ))
(provide 'stride)
;; Local Variables:
;; nameless-current-name: "stride"
;; End: