Skip to content

Instantly share code, notes, and snippets.

@feklee
Last active January 17, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feklee/06548708bef6ddbe2073f94f6da16b25 to your computer and use it in GitHub Desktop.
Save feklee/06548708bef6ddbe2073f94f6da16b25 to your computer and use it in GitHub Desktop.
To browse files associated with DocURLs
;; About DocURLs, see: http://f76.eu/a0
;;
;; Felix E. Klee <felix.klee@inka.de>
(defgroup docurl nil
"Support for browsing DocURLs"
:group 'communication)
(defcustom docurl-cache-filename
"~/.local/share/docurl_cache"
"Path of file that contains the DocURL cache"
:type 'file)
(defcustom docurl-doc-dir
"~"
"Path of directory below which documents are found"
:type 'file)
(defun docurlp (url)
"Return t if URL is a DocURL."
(let* ((urlobj (url-generic-parse-url url))
(domain (url-domain urlobj))
(path (url-filename urlobj)))
(and
(string-collate-equalp domain "f76.eu" "C" t)
(not (or (string= path "") (string= path "/"))))))
(defun docurl-idp (object)
"Return t is a valid DocURL ID."
(let ((case-fold-search nil))
(eq (string-match "^a[0-9a-zA-Z]+$" object) 0)))
(defun docurl-id (url)
(let* ((urlobj (url-generic-parse-url url))
(path-with-leading-slash (url-filename urlobj)))
(substring path-with-leading-slash 1)))
(defun docurl-copy-relative-doc-filename ()
"Copy relative filename of the current document into the kill
ring. This function is useful when manually appending to the
cache file."
(interactive)
(let* ((base-dir (expand-file-name docurl-doc-dir))
(regexp (concat "^" base-dir "\\(.*\\)$"))
(filename (buffer-file-name)))
(if (string-match regexp filename)
(kill-new (match-string 1 filename)))))
(defun docurl-relative-doc-filename (id)
"File name of document relative to document directory"
(with-temp-buffer
(insert-file-contents (expand-file-name docurl-cache-filename))
(let ((case-fold-search nil))
(search-forward-regexp (concat "^" id "[[:blank:]]+\\(.*\\)")))
(match-string 1)))
(defun docurl-doc-filename (id)
(expand-file-name
(docurl-relative-doc-filename id)
docurl-doc-dir))
(defun docurl-browse-by-id (id)
(find-file-other-window (docurl-doc-filename id)))
(defun docurl-browse (url)
"Opens the file associated with URL in a buffer."
(let ((id (docurl-id url)))
(docurl-browse-by-id id)))
(provide 'docurl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment