- :host host
- :service (if port port 70)
- :filter filter
- :sentinel sentinel)
- (process-send-string "elopher-process" (concat selector "\n")))
-
-(defun elopher-index-filter (proc string)
- (with-current-buffer (get-buffer "*elopher*")
- (let ((marker (process-mark proc))
- (inhibit-read-only t))
- (if (not (marker-position marker))
- (set-marker marker 0 (current-buffer)))
- (save-excursion
- (goto-char marker)
- (elopher-render-complete-records string)
- (set-marker marker (point))))))
-
-(defun elopher-get-index (selector host port)
- (setq elopher-incomplete-record "")
- (elopher-get-selector selector host port #'elopher-index-filter))
-
-(defun elopher-text-filter (proc string)
- (with-current-buffer (get-buffer "*elopher*")
- (let ((marker (process-mark proc))
- (inhibit-read-only t))
- (if (not (marker-position marker))
- (set-marker marker 0 (current-buffer)))
- (save-excursion
- (goto-char marker)
- (dolist (line (split-string string "\r"))
- (insert line))
- (set-marker marker (point))))))
-
-(defun elopher-get-text (selector host port)
- (elopher-get-selector selector host port #'elopher-text-filter))
-
-(defvar elopher-image-buffer "")
-
-(defun elopher-image-filter (proc string)
- (setq elopher-image-buffer (concat elopher-image-buffer string)))
-
-(defun elopher-image-sentinel (proc event)
- (let ((inhibit-read-only t))
- (insert-image (create-image elopher-image-buffer))))
-
-(defun elopher-get-image (selector host port)
- (setq elopher-image-buffer "")
- (elopher-get-selector selector host port #'elopher-image-filter #'elopher-image-sentinel))
-
-(defun elopher-history-back ()
- (interactive)
- (let ((inhibit-read-only t))
- (undo)))
+ :host (elopher-address-host address)
+ :service (elopher-address-port address)
+ :filter (lambda (proc string)
+ (setq elopher-selector-string (concat elopher-selector-string string)))
+ :sentinel after)
+ (process-send-string "elopher-process"
+ (concat (elopher-address-selector address) "\n")))
+
+;; 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
+;;