+;; Index retrieval
+
+(defun elopher-get-index-node ()
+ (let ((content (elopher-node-content elopher-current-node))
+ (address (elopher-node-address elopher-current-node)))
+ (if content
+ (progn
+ (elopher-with-clean-buffer
+ (insert content))
+ (elopher-restore-pos))
+ (if address
+ (progn
+ (elopher-with-clean-buffer
+ (insert "LOADING DIRECTORY..."))
+ (elopher-get-selector address
+ (lambda (proc event)
+ (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-restore-pos)
+ (elopher-set-node-content elopher-current-node
+ (buffer-string)))))))
+
+;; Text retrieval
+
+(defvar elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?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))
+ (address (elopher-node-address elopher-current-node)))
+ (if content
+ (progn
+ (elopher-with-clean-buffer
+ (insert content))
+ (elopher-restore-pos))
+ (progn
+ (elopher-with-clean-buffer
+ (insert "LOADING TEXT..."))
+ (elopher-get-selector address
+ (lambda (proc event)
+ (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
+
+(defun elopher-get-image-node ()
+ (let ((content (elopher-node-content elopher-current-node))
+ (address (elopher-node-address elopher-current-node)))
+ (if content
+ (progn
+ (elopher-with-clean-buffer
+ (insert-image content))
+ (setq cursor-type nil)
+ (elopher-restore-pos))
+ (progn
+ (elopher-with-clean-buffer
+ (insert "LOADING IMAGE..."))
+ (elopher-get-selector address
+ (lambda (proc event)
+ (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
+
+(defun elopher-get-search-node ()
+ (let* ((content (elopher-node-content elopher-current-node))
+ (address (elopher-node-address elopher-current-node)))
+ (if content
+ (progn
+ (elopher-with-clean-buffer
+ (insert content))
+ (elopher-restore-pos)
+ (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)
+ (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
+;;
+
+(defun elopher-next-link ()
+ (interactive)
+ (forward-button 1))
+
+(defun elopher-prev-link ()