"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))
(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 ()
;;
(defmacro elopher-with-clean-buffer (&rest args)
+ "Evaluate ARGS with a clean *elopher* buffer as current."
(list 'progn
'(switch-to-buffer "*elopher*")
'(elopher-mode)
(help-string (format "mouse-1, RET: open %s on %s port %s"
selector host port)))
(pcase type
- (?i (elopher-insert-margin)
+ (?i (elopher-insert-margin) ; Information
(insert (propertize display-string
'face elopher-info-face)))
- (?0 (elopher-insert-margin "T")
+ (?0 (elopher-insert-margin "T") ; Text
(insert-text-button display-string
'face elopher-text-face
'elopher-node (elopher-make-node elopher-current-node
'action #'elopher-click-link
'follow-link t
'help-echo help-string))
- (?1 (elopher-insert-margin "/")
+ (?1 (elopher-insert-margin "/") ; Index
(insert-text-button display-string
'face elopher-index-face
'elopher-node (elopher-make-node elopher-current-node
'action #'elopher-click-link
'follow-link t
'help-echo help-string))
- ((or ?g ?p ?I) (elopher-insert-margin "im")
- (insert-text-button display-string
- 'face elopher-image-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")
+ ((or ?g ?p ?I) (elopher-insert-margin "im") ; Image
+ (insert-text-button display-string
+ 'face elopher-image-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))
+ ((or ?4 ?9) (elopher-insert-margin "B") ; Binary
+ (insert-text-button display-string
+ 'face elopher-binary-face
+ 'elopher-node (elopher-make-node elopher-current-node
+ address
+ #'elopher-get-node-download)
+ 'action #'elopher-click-link
+ 'follow-link t
+ 'help-echo help-string))
+ (?7 (elopher-insert-margin "S") ; Query
(insert-text-button display-string
'face elopher-search-face
'elopher-node (elopher-make-node elopher-current-node
- address
- #'elopher-get-search-node)
+ address
+ #'elopher-get-search-node)
'action #'elopher-click-link
'follow-link t
'help-echo help-string))
- (?h (elopher-insert-margin "W")
+ (?h (elopher-insert-margin "W") ; Web link
(let ((url (elt (split-string selector "URL:") 1)))
(insert-text-button display-string
'face elopher-http-face
;;; 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)
(if content
(progn
(elopher-with-clean-buffer
- (insert content))
+ (insert content))
(elopher-restore-pos))
(if address
(progn
(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-process-text (string)
- (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
- (replace-regexp-in-string "\r" "" chopped-str)))
+ (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)))
+
+(defvar elopher-url-regex "\\https?://\\([a-z.\-]+\\)\\([^ \r\n\t(),]*\\)")
+
+(defun elopher-buttonify-urls (string)
+ (with-temp-buffer
+ (insert string)
+ (goto-char (point-min))
+ (while (re-search-forward elopher-url-regex nil t)
+ (let ((url (match-string 0)))
+ (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-get-text-node ()
(let ((content (elopher-node-content elopher-current-node))
(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
(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
(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
(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))
(defvar elopher-download-filename)
-(defun elopher-download-node (node filename)
- (let* ((address (elopher-node-address node)))
- (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))))))
+(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-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."
(interactive)
(let ((button (button-at (point))))
(if button
- (let* ((node (button-get button 'elopher-node))
- (address (elopher-node-address node))
- (selector (elopher-address-selector address))
- (filename-proposal (file-name-nondirectory selector))
- (filename (read-file-name "Name of file to write: "
- nil nil nil
- (if (> 0 (length filename-proposal))
- filename-proposal
- "gopher.file"))))
- (elopher-download-node node filename))
+ (elopher-visit-node (button-get button 'elopher-node)
+ #'elopher-get-node-download)
(message "No link selected."))))
+
;;; Mode and keymap
;;
;;; 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