commit b813bdd496564d1592fb2b83f401f1b458d673b9
parent adaef6427b7cb1c9041b14a7f06db93a0fcea5cd
Author: Christoph Lohmann <20h@r-36.net>
Date: Mon, 27 Jul 2026 12:16:41 +0200
Improve gph handling in emacs. Thanks pocketlint!
Diffstat:
| M | gph/emacs/gph-mode.el | | | 127 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 127 insertions(+), 0 deletions(-)
diff --git a/gph/emacs/gph-mode.el b/gph/emacs/gph-mode.el
@@ -67,6 +67,133 @@
(setq-local paragraph-separate (concat "^\\[\\|[ \t]*$\\|" page-delimiter))
(setq-local font-lock-defaults '(gph--font-lock-defaults)))
+
+(defun gph-insert-link (c label path server port)
+
+ "Easily insert a gopher link. You will be prompted for:
+- C: a character representing the link type (see list below)
+- LABEL: a string to be used as the link label
+- PATH: path to the item you wish to link to
+- SERVER: the domain of the server (defaults to 'server')
+- PORT: port to use (defaults to '70')
+
+the possible options for C are:
+ key type gopher equivalent
+- f file 0
+- d directory 1
+- b binary 9
+- g gif g
+- I image I
+- s search 7
+- h http h
+- t telnet 8
+- e error 3
+- i info i
+
+the resulting link looks something like:
+[0|my file|./file.txt|example.com|70]
+
+see also `gph-generate-link', which converts a gopher url to a link"
+
+ (interactive "c[f]ile [d]ir [b]inary [g]if [I]mage [s]earch [h]ttp [t]elnet [e]rror [i]nfo: \nMLabel: \nMPath: \nsServer (server): \nsPort (70): ")
+
+ (cond ((char-equal c ?f)
+ (insert "[0|"))
+ ((char-equal c ?d)
+ (insert "[1|"))
+ ((char-equal c ?b)
+ (insert "[9|"))
+ ((char-equal c ?g)
+ (insert "[g|"))
+ ((char-equal c ?I)
+ (insert "[I|"))
+ ((char-equal c ?s)
+ (insert "[7|"))
+ ((char-equal c ?h)
+ (insert "[h|"))
+ ((char-equal c ?t)
+ (insert "[8|"))
+ ((char-equal c ?e)
+ (insert "[3|"))
+ ((char-equal c ?i)
+ (insert "[i|")))
+ (insert (concat label "|" path "|"
+ (if (string-equal server "")
+ "server" server)
+ "|"
+ (if (string-equal port "")
+ "70" port)
+ "]")))
+
+;; these are some functions that break up a url
+;; for when automagically transforming a gopher:// url
+;; into a proper geomyidae link
+
+(defun gph--remove-proto (url)
+ "Remove 'gopher://' from a URL string."
+ (string-trim url "gopher://" ""))
+
+(defun gph--get-domain (url)
+ "Return only the domain name of a URL."
+(car (string-split (gph--remove-proto url) "/")))
+
+(defun gph--get-path (url)
+ "Return only the resource path of the URL"
+ (string-trim-left
+ (string-remove-prefix (gph--get-domain url)
+ (gph--remove-proto url))
+ "/[^z-a]"))
+
+(defun gph--get-type (url)
+ "Attempt to get the link type of the URL (like 0 for file, 1 for dir etc)."
+ (caddr (string-split
+ (string-remove-prefix (gph--get-domain url)
+ (gph--remove-proto url))
+ "")))
+
+(defun gph-url-to-link (url label)
+ "Convert a gopher:// or http:// url to the geomyidae link format.
+this function prompts you for a URL, then for a link label. it tries to
+parse the URL you provide and output a correct link to that resource.
+
+The URL tries to default to whatever is in your clipboard. I don't think
+this works in non-graphical environments.
+
+ gopher://example.com/0/some/file.txt
+becomes
+ [0|my label|/some/file.txt|example.com|70]"
+
+ ;; this interactive funciton is kinda big but it works okay
+ (interactive (let* ((selection (string-truncate-left (gui-get-selection) 70))
+ (prompt (if (gui-get-selection)
+ (format "URL (%s): " selection)
+ "URL: "))
+ (url (read-string prompt nil nil selection nil))
+ (label (read-string "Label: " nil nil nil nil)))
+ (list url label)))
+
+ (if (or (string-prefix-p "gopher://" url 1)
+ (string-prefix-p "gophers://" url 1))
+ (insert (concat
+ "[" (gph--get-type url)
+ "|" label
+ "|" (gph--get-path url)
+ "|" (gph--get-domain url)
+ "|70]"))
+
+ ;; else, it's probably an http:// link
+ (insert (concat
+ "[h|" label "|URL:" url "|server|70]"))))
+
+;;gph-mode-map
+(define-key gph-mode-map (kbd "C-c C-,") 'gph-insert-link)
+(define-key gph-mode-map (kbd "C-c C-l") 'gph-url-to-link)
+
+;; add a reminder about these bindings in the minibuffer
+(add-hook 'gph-mode-hook
+ (lambda ()
+ (message "[C-c C-,] insert link. [C-c C-l] convert URL to link. ")))
+
(provide 'gph-mode)
;;; gph-mode.el ends here