;;; named-entity.el --- Completion for named entities -*- lexical-binding: t; -*- ;; Copyright (C) 2025 Philip Kaludercic ;; Author: Philip Kaludercic ;; Maintainer: Philip Kaludercic ;; URL: https://wwwcip.cs.fau.de/~oj14ozun/src+etc/named-entity.el ;; Version: $Id: named-entity.el,v 1.3 2025/04/23 15:58:14 oj14ozun Exp $ ;; Package-Version: 1 ;; Keywords: convenience, text ;; 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 . ;;; Commentary: ;; A Completion at point (see Info node `(elisp) Completion in ;; Buffers') backend for named entities in HTML. ;; To configure, evaluate the following expression in a context you ;; wish to have completion enabled: ;; ;; (add-hook 'completion-at-point-functions #'html-named-entity-capf) ;; Note that the script fetches the list of entity names from the web! ;; That keeps this script small and easily up-to-date, but requires an ;; internet connection. To avoid re-fetching the data, you can ;; byte-compile the file. ;;; Code: (defconst html-named-entity-table (eval-when-compile (with-current-buffer (url-retrieve-synchronously "https://html.spec.whatwg.org/entities.json") (let ((table (make-hash-table))) (maphash (lambda (key value) (puthash (replace-regexp-in-string (rx bos "&" (group (+ alnum)) (? ";")eos) "&\\1;" key) (aref (gethash "characters" value) 0) table)) (json-parse-buffer)) (kill-buffer) table))) "Hashtable mapping named entities to their characters.") ;;;###autoload (defun html-named-entity-capf () "Completion-at-point backend for named entities in HTML." (let ((beg (save-excursion (skip-syntax-backward "w_") (and (looking-at-p "&") (point)))) (end (save-excursion (skip-syntax-forward "w_") (if (looking-at-p ";") (1+ (point)) (point))))) (and beg (/= beg end) (list beg end (completion-table-with-cache (lambda (_) html-named-entity-table)) :annotation-function (lambda (str) (format " %c" (gethash str html-named-entity-table))))))) (provide 'named-entity) ;;; named-entity.el ends here