X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=elopher.el;h=7e45cab27db2489b328b6702ec4959bd5be379d9;hb=12320978c9fab9ad8c46e3c92d39547c30ceef69;hp=6ee53a3364db80811df6b14d4ac1fe5fa60e2c20;hpb=b8a17684a0969e773056d05335e1cffd608fd977;p=elpher.git diff --git a/elopher.el b/elopher.el index 6ee53a3..7e45cab 100644 --- a/elopher.el +++ b/elopher.el @@ -110,53 +110,62 @@ Otherwise, use the system browser via the BROWSE-URL function." ;; Address (defun elopher-make-address (selector host port) + "Create an address of a gopher object with SELECTOR, HOST and PORT." (list selector host port)) (defun elopher-address-selector (address) + "Retrieve selector from ADDRESS." (car address)) (defun elopher-address-host (address) + "Retrieve host from ADDRESS." (cadr address)) (defun elopher-address-port (address) + "Retrieve port from ADDRESS." (caddr address)) ;; Node (defun elopher-make-node (parent address getter &optional content pos) + "Create a node in the gopher page hierarchy. + +PARENT specifies the parent of the node, ADDRESS specifies the address of +the gopher page, GETTER provides the getter function used to obtain this +page. + +The optional arguments CONTENT and POS can be used to fill the cached +content and cursor position fields of the node." (list parent address getter content pos)) (defun elopher-node-parent (node) + "Retrieve the parent node of NODE." (elt node 0)) (defun elopher-node-address (node) + "Retrieve the address of NODE." (elt node 1)) (defun elopher-node-getter (node) + "Retrieve the preferred getter function of NODE." (elt node 2)) (defun elopher-node-content (node) + "Retrieve the cached content of NODE, or nil if none exists." (elt node 3)) (defun elopher-node-pos (node) + "Retrieve the cached cursor position for NODE, or nil if none exists." (elt node 4)) (defun elopher-set-node-content (node content) + "Set the content cache of NODE to CONTENT." (setcar (nthcdr 3 node) content)) (defun elopher-set-node-pos (node pos) + "Set the cursor position cache of NODE to POS." (setcar (nthcdr 4 node) pos)) -(defun elopher-save-pos () - (when elopher-current-node - (elopher-set-node-pos elopher-current-node (point)))) - -(defun elopher-restore-pos () - (let ((pos (elopher-node-pos elopher-current-node))) - (if pos - (goto-char pos) - (goto-char (point-min))))) - ;; Node graph traversal (defvar elopher-current-node) @@ -171,14 +180,28 @@ Otherwise, use the system browser via the BROWSE-URL function." (funcall (elopher-node-getter node)))) (defun elopher-visit-parent-node () + "Visit the parent of the current node." (let ((parent-node (elopher-node-parent elopher-current-node))) (when parent-node (elopher-visit-node parent-node)))) (defun elopher-reload-current-node () + "Reload the current node, discarding any existing cached content." (elopher-set-node-content elopher-current-node nil) (elopher-visit-node elopher-current-node)) +(defun elopher-save-pos () + "Save the current position of point to the current node." + (when elopher-current-node + (elopher-set-node-pos elopher-current-node (point)))) + +(defun elopher-restore-pos () + "Restore the position of point to that cached in the current node." + (let ((pos (elopher-node-pos elopher-current-node))) + (if pos + (goto-char pos) + (goto-char (point-min))))) + ;;; Buffer preparation ;; @@ -249,7 +272,7 @@ Otherwise, use the system browser via the BROWSE-URL function." 'help-echo (format "mouse-1, RET: open %s on %s port %s" selector host port))) (pcase type - (?i (elopher-insert-margin) ; Information + (?i (elopher-insert-margin) ; Information (insert (propertize display-string 'face 'elopher-info))) (?h (elopher-insert-margin "W") ; Web link @@ -263,7 +286,7 @@ Otherwise, use the system browser via the BROWSE-URL function." (?.) ; 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))))) + 'face 'elopher-unknown-face))))) (insert "\n"))) @@ -294,6 +317,7 @@ The result is stored as a string in the variable elopher-selector-string." ;; Index retrieval (defun elopher-get-index-node () + "Getter which retrieves the current node contents as an index." (let ((content (elopher-node-content elopher-current-node)) (address (elopher-node-address elopher-current-node))) (if content @@ -322,7 +346,8 @@ The result is stored as a string in the variable elopher-selector-string." ;; Text retrieval -(defconst elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?" +(defconst elopher-url-regex + "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?" "Regexp used to locate and buttinofy URLs in text files loaded by elopher.") (defun elopher-buttonify-urls (string) @@ -363,11 +388,13 @@ The result is stored as a string in the variable elopher-selector-string." (buffer-string))) (defun elopher-process-text (string) + "Remove CRs and trailing period from the gopher text document 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 () + "Getter which retrieves the current node contents as a text document." (let ((content (elopher-node-content elopher-current-node)) (address (elopher-node-address elopher-current-node))) (if content @@ -390,6 +417,7 @@ The result is stored as a string in the variable elopher-selector-string." ;; Image retrieval (defun elopher-get-image-node () + "Getter which retrieves the current node contents as an image to view." (let ((content (elopher-node-content elopher-current-node)) (address (elopher-node-address elopher-current-node))) (if content @@ -405,7 +433,8 @@ The result is stored as a string in the variable elopher-selector-string." (lambda (proc event) (unless (string-prefix-p "deleted" event) (let ((image (create-image - (string-as-unibyte elopher-selector-string) + (encode-coding-string elopher-selector-string + 'no-conversion) nil t))) (elopher-with-clean-buffer (insert-image image)) @@ -417,6 +446,7 @@ The result is stored as a string in the variable elopher-selector-string." ;; Search retrieval (defun elopher-get-search-node () + "Getter which submits a search query to the address of the current node." (let ((content (elopher-node-content elopher-current-node)) (address (elopher-node-address elopher-current-node)) (aborted t)) @@ -449,6 +479,7 @@ The result is stored as a string in the variable elopher-selector-string." ;; Raw server response retrieval (defun elopher-get-node-raw () + "Getter which retrieves the raw server response for the current node." (let* ((content (elopher-node-content elopher-current-node)) (address (elopher-node-address elopher-current-node))) (elopher-with-clean-buffer @@ -471,6 +502,7 @@ The result is stored as a string in the variable elopher-selector-string." (defvar elopher-download-filename) (defun elopher-get-node-download () + "Getter which retrieves the current node and writes the result to a file." (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. @@ -495,24 +527,29 @@ The result is stored as a string in the variable elopher-selector-string." ;; (defun elopher-next-link () + "Move point to the next link on the current page." (interactive) (forward-button 1)) (defun elopher-prev-link () + "Move point to the previous link on the current page." (interactive) (backward-button 1)) (defun elopher-click-link (button) + "Function called when the gopher link BUTTON is activated (via mouse or keypress)." (let ((node (button-get button 'elopher-node))) (elopher-visit-node node))) (defun elopher-click-url (button) + "Function called when the url link BUTTON is activated (via mouse or keypress)." (let ((url (button-get button 'elopher-url))) (if elopher-open-urls-with-eww (browse-web url) (browse-url url)))) -(defun elopher-follow-closest-link () +(defun elopher-follow-current-link () + "Open the link or url at point." (interactive) (push-button)) @@ -569,20 +606,23 @@ The result is stored as a string in the variable elopher-selector-string." (defvar elopher-mode-map (let ((map (make-sparse-keymap))) - (define-key map (kbd "") 'elopher-next-link) - (define-key map (kbd "") 'elopher-prev-link) + (define-key map (kbd "TAB") 'elopher-next-link) + (define-key map (kbd "") 'elopher-prev-link) (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 "r") 'elopher-redraw) + (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 + (kbd "TAB") 'elopher-next-link + (kbd "C-]") 'elopher-follow-current-link (kbd "C-t") 'elopher-back (kbd "u") 'elopher-back (kbd "g") 'elopher-go - (kbd "r") 'elopher-reload + (kbd "r") 'elopher-redraw + (kbd "R") 'elopher-reload (kbd "w") 'elopher-view-raw (kbd "d") 'elopher-download)) map) @@ -595,6 +635,7 @@ The result is stored as a string in the variable elopher-selector-string." ;;; Main start procedure ;; +;;;###autoload (defun elopher () "Start elopher with default landing page." (interactive)