;; -*- lexical-binding: t -*- ;; https://dataswamp.org/~incal/emacs-init/w3m/w3m-download.el ;; https://dataswamp.org/~incal/conf/.zsh/download (require 'cl-lib) (cl-pushnew "." load-path :test #'string=) (cl-pushnew (file-name-concat ".." "ll") load-path :test #'string=) (require 'll) (-> 'll-file-write-to) (-> 'w3m) (-> 'w3m-search) (defun w3m-search-torrent (key) (interactive "ssearch: ") (w3m-search w3m-search-default-engine (@f "\"%s\" \"torrent\" magnet" key))) (defun w3m-dl-magnet () (i) (goto-beg) (& (re-search-forward "magnet" (point-max) t) (w3m-dl-dwim))) (defun write-dl-URL (&optional url append) (or url (setf url (w3m-anchor))) (let* ((dl-file (getenv "DL_FILE")) (url-newline (@f "%s\n" url))) (& (file-exists-p dl-file) (! append) (delete-file dl-file)) (append-to-file url-newline nil dl-file))) (defun w3m-img-at-point-p () (w3m-url-valid (w3m-image))) (defun w3m-dl-str-p (str) (& (su str) (or (string-match-p "^magnet:" str) (string-match-p "^https*://www.youtube.com" str)))) (defun w3m-dl-dwim (&optional prefer-link-or-append) (interactive "P") (let* ((img (w3m-img-at-point-p)) (link-str (w3m-anchor)) (special-link (w3m-dl-str-p link-str)) (url (thing-at-point 'url)) (special-url (w3m-dl-str-p url))) (cond (special-link (write-dl-URL link-str prefer-link-or-append)) (special-url (write-dl-URL url prefer-link-or-append)) ((& link-str (or prefer-link-or-append (! img))) (w3m-download link-str)) (img (w3m-download-this-image)) (url (w3m-download url)) (t (w3m-download w3m-current-url))))) (defalias 'dl #'w3m-dl-dwim) (<- 'w3m-download)