archwiki.el/archwiki.el

99 lines
3.4 KiB
EmacsLisp
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; archwiki.el --- Browse the Arch Wiki from Emacs -*- lexical-binding: t -*-
;; Author: Lucien Cartier-Tilet <lucien@phundrak.com>
;; Maintainer: Lucien Cartier-Tilet <lucien@phundrak.com>
;; Version: 0.1.1
;; Package-Requires: ((emacs "25"))
;; Homepage: https://labs.phundrak.com/phundrak/archwiki.el
;; Keywords: keywords
;; This file is not part of GNU Emacs
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package relies on the fact the Arch Wiki docs are available to
;; Emacs, either locally or through TRAMP. If available through TRAMP,
;; opening pages should work through eww, although it can be slow
;; depending on the quality of your network, but I dont expect it to
;; work with external programs such as qutebrowser, Nyxt, or Firefox.
;; If you dont open the ArchWiki pages through eww, either make sure
;; `xdg-open' is available in your path or that you customized
;; `archwiki-external-browser'.
;;; Code:
(defgroup archwiki nil
"Browse the Arch Wiki from Emacs."
:prefix "archwiki-"
:link '(url-link :tag "Gitea" "https://labs.phundrak.com/phundrak/archwiki.el"))
(defcustom archwiki-local-path "/usr/share/doc/arch-wiki/html/en/"
"Path to local copy of the Arch Wiki."
:group 'archwiki
:version "0.1.0"
:type 'path)
(defcustom archwiki-use-eww t
"Whether to use eww or an external program."
:group 'archwiki
:version "0.1.0"
:type 'boolean
:safe #'booleanp)
(defcustom archwiki-external-browser ""
"Which external web browser to use.
If its value is an empty string, use `xdg-open'."
:group 'archwiki
:version "0.1.0"
:type 'string)
(defun archwiki--list-web-pages ()
"List web pages of the Arch Wiki."
(mapcar (lambda (file) (replace-regexp-in-string "_" "_" (file-name-sans-extension file)))
(directory-files archwiki-local-path
nil
(rx (+ ascii)
".html"))))
;;;###autoload
(defun archwiki (page)
"Find and open an Arch Wiki PAGE in Emacs.
PAGE is the name of the Arch Wiki page we want to open, without
any underscore or extension."
(interactive
`(,(completing-read "Page to open: "
(archwiki--list-web-pages))))
(let ((path (concat "file://"
archwiki-local-path
(replace-regexp-in-string " " "_" page)
".html")))
(if archwiki-use-eww
(eww-browse-url path)
(with-temp-buffer
(call-process (if (or (string= "" archwiki-external-browser)
(null (executable-find archwiki-external-browser)))
"xdg-open"
archwiki-external-browser)
nil t nil
path)))))
(provide 'archwiki)
;;; archwiki.el ends here