X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=elopher.el;h=217ef983605585f3e07a6764e35127d4a6821df2;hb=c695f0cfa7b424e174cb736ef44efd1980acff41;hp=ebab261a0959a95ba32897495890e89d25fc6f4d;hpb=0e408c92b155d79826eb98a2665a1d1b04aeeae6;p=elpher.git diff --git a/elopher.el b/elopher.el index ebab261..217ef98 100644 --- a/elopher.el +++ b/elopher.el @@ -16,7 +16,8 @@ "Width of left-hand margin used when rendering indicies.") (defconst elopher-start-index - (mapconcat 'identity + (mapconcat + 'identity (list "i\tfake\tfake\t1" "i--------------------------------------------\tfake\tfake\t1" "i Elopher Gopher Client \tfake\tfake\t1" @@ -30,7 +31,8 @@ "i - u: return to parent directory entry\tfake\tfake\t1" "i - g: go to a particular page\tfake\tfake\t1" "i - r: reload current page\tfake\tfake\t1" - "i - t: display the current page as text (i.e. \"source\")\tfake\tfake\t1" + "i - d: download directory entry under cursor\tfake\tfake\t1" + "i - w: display the raw server response for the current page\tfake\tfake\t1" "i\tfake\tfake\t1" "iPlaces to start exploring Gopherspace:\tfake\tfake\t1" "i\tfake\tfake\t1" @@ -75,6 +77,10 @@ "Face used for image records." :type '(face)) +(defcustom elopher-binary-face '(foreground-color . "magenta") + "Face used for image records." + :type '(face)) + (defcustom elopher-unknown-face '(foreground-color . "red") "Face used for unknown record types." :type '(face)) @@ -141,13 +147,12 @@ Otherwise, use the system browser via the BROWSE-URL function." (defvar elopher-current-node) -(defun elopher-visit-node (node &optional text) +(defun elopher-visit-node (node &optional getter) (elopher-save-pos) + (elopher-process-cleanup) (setq elopher-current-node node) - (if text - (progn - (elopher-set-node-content elopher-current-node nil) - (elopher-get-text-node)) + (if getter + (funcall getter) (funcall (elopher-node-getter node)))) (defun elopher-visit-parent-node () @@ -163,6 +168,7 @@ Otherwise, use the system browser via the BROWSE-URL function." ;; (defmacro elopher-with-clean-buffer (&rest args) + "Evaluate ARGS with a clean *elopher* buffer as current." (list 'progn '(switch-to-buffer "*elopher*") '(elopher-mode) @@ -191,6 +197,17 @@ Otherwise, use the system browser via the BROWSE-URL function." (insert " ")) (insert (make-string elopher-margin-width ?\s)))) +(defvar elopher-type-map + `((?0 elopher-get-text-node "T" ,elopher-text-face) + (?1 elopher-get-index-node "/" ,elopher-index-face) + (?g elopher-get-image-node "im" ,elopher-image-face) + (?p elopher-get-image-node "im" ,elopher-image-face) + (?I elopher-get-image-node "im" ,elopher-image-face) + (?4 elopher-get-node-download "B" ,elopher-binary-face) + (?5 elopher-get-node-download "B" ,elopher-binary-face) + (?9 elopher-get-node-download "B" ,elopher-binary-face) + (?7 elopher-get-search-node "?" ,elopher-search-face))) + (defun elopher-insert-index-record (line) "Insert the index record corresponding to LINE into the current buffer." (let* ((type (elt line 0)) @@ -200,74 +217,54 @@ Otherwise, use the system browser via the BROWSE-URL function." (host (elt fields 2)) (port (elt fields 3)) (address (elopher-make-address selector host port)) - (help-string (format "mouse-1, RET: open %s on %s port %s" - selector host port))) - (pcase type - (?i (elopher-insert-margin) - (insert (propertize display-string - 'face elopher-info-face))) - (?0 (elopher-insert-margin "T") - (insert-text-button display-string - 'face elopher-text-face - 'elopher-node (elopher-make-node elopher-current-node - address - #'elopher-get-text-node) - 'action #'elopher-click-link - 'follow-link t - 'help-echo help-string)) - (?1 (elopher-insert-margin "/") - (insert-text-button display-string - 'face elopher-index-face - 'elopher-node (elopher-make-node elopher-current-node - address - #'elopher-get-index-node) - 'action #'elopher-click-link - 'follow-link t - 'help-echo help-string)) - ((or ?g ?p ?I) (elopher-insert-margin "im") + (type-map-entry (alist-get type elopher-type-map))) + (if type-map-entry + (let ((getter (car type-map-entry)) + (margin-code (cadr type-map-entry)) + (face (caddr type-map-entry))) + (elopher-insert-margin margin-code) (insert-text-button display-string - 'face elopher-image-face + 'face face 'elopher-node (elopher-make-node elopher-current-node address - #'elopher-get-image-node) - 'action #'elopher-click-link - 'follow-link t - 'help-echo help-string)) - (?7 (elopher-insert-margin "S") - (insert-text-button display-string - 'face elopher-search-face - 'elopher-node (elopher-make-node elopher-current-node - address - #'elopher-get-search-node) + getter) 'action #'elopher-click-link 'follow-link t - 'help-echo help-string)) - (?h (elopher-insert-margin "W") - (let ((url (elt (split-string selector "URL:") 1))) - (insert-text-button display-string - 'face elopher-http-face - 'elopher-url url - 'action #'elopher-click-url - 'follow-link t - 'help-echo (format "mouse-1, RET: open url %s" url)))) - (?.) ; Occurs at end of index, can safely ignore. - (tp (elopher-insert-margin (concat (char-to-string tp) "?")) - (insert (propertize display-string - 'face elopher-unknown-face)))) + 'help-echo (format "mouse-1, RET: open %s on %s port %s" + selector host port))) + (pcase type + (?i (elopher-insert-margin) ; Information + (insert (propertize display-string + 'face elopher-info-face))) + (?h (elopher-insert-margin "W") ; Web link + (let ((url (elt (split-string selector "URL:") 1))) + (insert-text-button display-string + 'face elopher-http-face + 'elopher-url url + 'action #'elopher-click-url + 'follow-link t + 'help-echo (format "mouse-1, RET: open url %s" url)))) + (?.) ; Occurs at end of index, can safely ignore. + (tp (elopher-insert-margin (concat (char-to-string tp) "?")) + (insert (propertize display-string + 'face elopher-unknown-face))))) (insert "\n"))) ;;; Selector retrieval (all kinds) ;; +(defun elopher-process-cleanup () + "Immediately shut down any extant elopher process." + (let ((p (get-process "elopher-process"))) + (if p (delete-process p)))) + (defvar elopher-selector-string) (defun elopher-get-selector (address after) "Retrieve selector specified by ADDRESS, then execute AFTER. The result is stored as a string in the variable elopher-selector-string." (setq elopher-selector-string "") - (let ((p (get-process "elopher-process"))) - (if p (delete-process p))) (make-network-process :name "elopher-process" :host (elopher-address-host address) @@ -286,32 +283,72 @@ The result is stored as a string in the variable elopher-selector-string." (if content (progn (elopher-with-clean-buffer - (insert content)) + (insert content)) (elopher-restore-pos)) (if address (progn (elopher-with-clean-buffer (insert "LOADING DIRECTORY...")) - ;; (let ((inhibit-read-only t)) - ;; (insert "LOADING DIRECTORY...")) (elopher-get-selector address (lambda (proc event) - (elopher-with-clean-buffer - (elopher-insert-index elopher-selector-string)) - (elopher-restore-pos) - (elopher-set-node-content elopher-current-node - (buffer-string))))) + (unless (string-prefix-p "deleted" event) + (elopher-with-clean-buffer + (elopher-insert-index elopher-selector-string)) + (elopher-restore-pos) + (elopher-set-node-content elopher-current-node + (buffer-string)))))) (progn (elopher-with-clean-buffer - (elopher-insert-index elopher-start-index)) + (elopher-insert-index elopher-start-index)) (elopher-restore-pos) (elopher-set-node-content elopher-current-node (buffer-string))))))) ;; Text retrieval -(defun elopher-strip-CRs (string) - (replace-regexp-in-string "\r" "" string)) +(defvar elopher-url-regex "\\(https?\\|gopher\\)://\\([a-z.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)") + +(defun elopher-buttonify-urls (string) + "Turn substrings which look like urls in STRING into clickable buttons." + (with-temp-buffer + (insert string) + (goto-char (point-min)) + (while (re-search-forward elopher-url-regex nil t) + (let ((url (match-string 0)) + (protocol (downcase (match-string 1)))) + (if (string= protocol "gopher") + (let* ((host (match-string 2)) + (port 70) + (type-and-selector (match-string 4)) + (type (if (> (length type-and-selector) 1) + (elt type-and-selector 1) + ?1)) + (selector (if (> (length type-and-selector) 1) + (substring type-and-selector 2) + "")) + (address (elopher-make-address selector host port)) + (getter (car (alist-get type elopher-type-map)))) + (make-text-button (match-beginning 0) + (match-end 0) + 'elopher-node (elopher-make-node elopher-current-node + address + getter) + 'action #'elopher-click-link + 'follow-link t + 'help-echo (format "mouse-1, RET: open %s on %s port %s" + selector host port))) + (make-text-button (match-beginning 0) + (match-end 0) + 'elopher-url url + 'action #'elopher-click-url + 'follow-link t + 'help-echo (format "mouse-1, RET: open url %s" url))))) + (buffer-string))) + +(defun elopher-process-text (string) + (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)) + (cleaned-str (replace-regexp-in-string "\r" "" chopped-str))) + (elopher-buttonify-urls cleaned-str))) (defun elopher-get-text-node () (let ((content (elopher-node-content elopher-current-node)) @@ -319,18 +356,19 @@ The result is stored as a string in the variable elopher-selector-string." (if content (progn (elopher-with-clean-buffer - (insert content)) + (insert content)) (elopher-restore-pos)) (progn (elopher-with-clean-buffer - (insert "LOADING TEXT...")) + (insert "LOADING TEXT...")) (elopher-get-selector address (lambda (proc event) - (elopher-with-clean-buffer - (insert (elopher-strip-CRs elopher-selector-string))) - (elopher-restore-pos) - (elopher-set-node-content elopher-current-node - (buffer-string)))))))) + (unless (string-prefix-p "deleted" event) + (elopher-with-clean-buffer + (insert (elopher-process-text elopher-selector-string))) + (elopher-restore-pos) + (elopher-set-node-content elopher-current-node + (buffer-string))))))))) ;; Image retrieval @@ -340,22 +378,24 @@ The result is stored as a string in the variable elopher-selector-string." (if content (progn (elopher-with-clean-buffer - (insert-image content)) + (insert-image content)) (setq cursor-type nil) (elopher-restore-pos)) (progn (elopher-with-clean-buffer - (insert "LOADING IMAGE...")) + (insert "LOADING IMAGE...")) (elopher-get-selector address (lambda (proc event) - (let ((image (create-image - (string-as-unibyte elopher-selector-string) - nil t))) - (elopher-with-clean-buffer - (insert-image image)) - (setq cursor-type nil) - (elopher-restore-pos) - (elopher-set-node-content elopher-current-node image)))))))) + (unless (string-prefix-p "deleted" event) + (let ((image (create-image + (string-as-unibyte elopher-selector-string) + nil t))) + (elopher-with-clean-buffer + (insert-image image)) + (setq cursor-type nil) + (elopher-restore-pos) + (elopher-set-node-content elopher-current-node + image))))))))) ;; Search retrieval @@ -365,23 +405,70 @@ The result is stored as a string in the variable elopher-selector-string." (if content (progn (elopher-with-clean-buffer - (insert content)) + (insert content)) (elopher-restore-pos) - (message "Displaying cached search results. Reload to perform a new search.")) - (let* ((query-string (read-string "Query: ")) - (query-selector (concat (elopher-address-selector address) "\t" query-string)) - (search-address (elopher-make-address query-selector - (elopher-address-host address) - (elopher-address-port address)))) - (elopher-with-clean-buffer - (insert "LOADING RESULTS...")) - (elopher-get-selector search-address + (message "Displaying cached search results. Reload to perform a new search.")) + (unwind-protect + (let* ((query-string (read-string "Query: ")) + (query-selector (concat (elopher-address-selector address) "\t" query-string)) + (search-address (elopher-make-address query-selector + (elopher-address-host address) + (elopher-address-port address)))) + (elopher-with-clean-buffer + (insert "LOADING RESULTS...")) + (elopher-get-selector search-address + (lambda (proc event) + (unless (string-prefix-p "deleted" event) + (elopher-with-clean-buffer + (elopher-insert-index elopher-selector-string)) + (goto-char (point-min)) + (elopher-set-node-content elopher-current-node + (buffer-string)))))) + (elopher-visit-parent-node))))) + +;; Raw server response retrieval + +(defun elopher-get-node-raw () + (let* ((content (elopher-node-content elopher-current-node)) + (address (elopher-node-address elopher-current-node))) + (elopher-with-clean-buffer + (insert "LOADING RAW SERVER RESPONSE...")) + (if address + (elopher-get-selector address (lambda (proc event) - (elopher-with-clean-buffer - (elopher-insert-index elopher-selector-string)) - (goto-char (point-min)) - (elopher-set-node-content elopher-current-node - (buffer-string)))))))) + (unless (string-prefix-p "deleted" event) + (elopher-with-clean-buffer + (insert elopher-selector-string)) + (goto-char (point-min))))) + (progn + (elopher-with-clean-buffer + (insert elopher-start-index)) + (goto-char (point-min))))) + (message "Displaying raw server response. Reload to return to standard view.")) + +;; File export retrieval + +(defvar elopher-download-filename) + +(defun elopher-get-node-download () + (let* ((address (elopher-node-address elopher-current-node)) + (selector (elopher-address-selector address))) + (elopher-visit-parent-node) ; Do first in case of non-local exits. + (let* ((filename-proposal (file-name-nondirectory selector)) + (filename (read-file-name "Save file as: " + nil nil nil + (if (> (length filename-proposal) 0) + filename-proposal + "gopher.file")))) + (message "Downloading...") + (setq elopher-download-filename filename) + (elopher-get-selector address + (lambda (proc event) + (let ((coding-system-for-write 'binary)) + (with-temp-file elopher-download-filename + (insert elopher-selector-string) + (message (format "Download complate, saved to file %s." + elopher-download-filename))))))))) ;;; Navigation procedures @@ -427,10 +514,11 @@ The result is stored as a string in the variable elopher-selector-string." (interactive) (elopher-reload-current-node)) -(defun elopher-view-text () +(defun elopher-view-raw () "View current page as plain text." (interactive) - (elopher-visit-node elopher-current-node t)) + (elopher-visit-node elopher-current-node + #'elopher-get-node-raw)) (defun elopher-back () "Go to previous site." @@ -439,6 +527,15 @@ The result is stored as a string in the variable elopher-selector-string." (elopher-visit-parent-node) (message "No previous site."))) +(defun elopher-download () + "Download the link at point." + (interactive) + (let ((button (button-at (point)))) + (if button + (elopher-visit-node (button-get button 'elopher-node) + #'elopher-get-node-download) + (message "No link selected.")))) + ;;; Mode and keymap ;; @@ -450,7 +547,8 @@ The result is stored as a string in the variable elopher-selector-string." (define-key map (kbd "u") 'elopher-back) (define-key map (kbd "g") 'elopher-go) (define-key map (kbd "r") 'elopher-reload) - (define-key map (kbd "t") 'elopher-view-text) + (define-key map (kbd "w") 'elopher-view-raw) + (define-key map (kbd "d") 'elopher-download) (when (fboundp 'evil-define-key) (evil-define-key 'normal map (kbd "C-]") 'elopher-follow-closest-link @@ -458,7 +556,8 @@ The result is stored as a string in the variable elopher-selector-string." (kbd "u") 'elopher-back (kbd "g") 'elopher-go (kbd "r") 'elopher-reload - (kbd "t") 'elopher-view-text)) + (kbd "w") 'elopher-view-raw + (kbd "d") 'elopher-download)) map) "Keymap for gopher client.") @@ -468,12 +567,14 @@ The result is stored as a string in the variable elopher-selector-string." ;;; Main start procedure ;; + (defun elopher () "Start elopher with default landing page." (interactive) (setq elopher-current-node nil) (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node))) - (elopher-visit-node start-node))) + (elopher-visit-node start-node)) + "Started Elopher.") ; Otherwise (elopher) evaluates to start page string. ;;; elopher.el ends here