X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=elopher.el;h=8985bfee9d642033b103c8a446090e8957535635;hb=2c8c533b723793234fbefd411fd2dc193b79ab3e;hp=374249724f644876db632b698757cb01566fadb5;hpb=f0f484af61bc9482c8313f03e499646e80d21db7;p=elpher.git diff --git a/elopher.el b/elopher.el index 3742497..8985bfe 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" @@ -141,11 +143,12 @@ Otherwise, use the system browser via the BROWSE-URL function." (defvar elopher-current-node) -(defun elopher-visit-node (node &optional raw) +(defun elopher-visit-node (node &optional getter) (elopher-save-pos) + (elopher-process-cleanup) (setq elopher-current-node node) - (if raw - (elopher-get-node-raw) + (if getter + (funcall getter) (funcall (elopher-node-getter node)))) (defun elopher-visit-parent-node () @@ -161,6 +164,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) @@ -258,14 +262,17 @@ Otherwise, use the system browser via the BROWSE-URL function." ;;; 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) @@ -284,7 +291,7 @@ 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 @@ -292,14 +299,15 @@ The result is stored as a string in the variable elopher-selector-string." (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))))))) @@ -316,18 +324,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-process-text 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 @@ -337,22 +346,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 @@ -364,7 +375,7 @@ The result is stored as a string in the variable elopher-selector-string." (elopher-with-clean-buffer (insert content)) (elopher-restore-pos) - (message "Displaying cached search results. Reload to perform a new search.")) + (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 @@ -374,11 +385,14 @@ The result is stored as a string in the variable elopher-selector-string." (insert "LOADING RESULTS...")) (elopher-get-selector search-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 + (elopher-insert-index elopher-selector-string)) + (goto-char (point-min)) + (elopher-set-node-content elopher-current-node + (buffer-string))))))))) + +;; Raw server response retrieval (defun elopher-get-node-raw () (let* ((content (elopher-node-content elopher-current-node)) @@ -388,9 +402,10 @@ The result is stored as a string in the variable elopher-selector-string." (if address (elopher-get-selector address (lambda (proc event) - (elopher-with-clean-buffer - (insert elopher-selector-string)) - (goto-char (point-min)))) + (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)) @@ -398,6 +413,32 @@ The result is stored as a string in the variable elopher-selector-string." (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))) + (unwind-protect + (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))))) + (elopher-visit-parent-node)))) + + ;;; Navigation procedures ;; @@ -444,7 +485,8 @@ The result is stored as a string in the variable elopher-selector-string." (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." @@ -453,6 +495,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 ;; @@ -465,6 +516,7 @@ The result is stored as a string in the variable elopher-selector-string." (define-key map (kbd "g") 'elopher-go) (define-key map (kbd "r") 'elopher-reload) (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 @@ -472,7 +524,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 "w") 'elopher-view-raw)) + (kbd "w") 'elopher-view-raw + (kbd "d") 'elopher-download)) map) "Keymap for gopher client.") @@ -482,12 +535,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