config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

cape-char.el (5787B)


      1 ;;; cape-char.el --- Character completion functions -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2021-2024 Free Software Foundation, Inc.
      4 
      5 ;; This file is part of GNU Emacs.
      6 
      7 ;; This program is free software: you can redistribute it and/or modify
      8 ;; it under the terms of the GNU General Public License as published by
      9 ;; the Free Software Foundation, either version 3 of the License, or
     10 ;; (at your option) any later version.
     11 
     12 ;; This program is distributed in the hope that it will be useful,
     13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 ;; GNU General Public License for more details.
     16 
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     19 
     20 ;;; Commentary:
     21 
     22 ;; This package provides the `cape-emoji', `cape-tex', `cape-sgml' and
     23 ;; `cape-rfc1345' completion functions.
     24 
     25 ;;; Code:
     26 
     27 (require 'cape)
     28 
     29 (declare-function quail-deactivate "quail")
     30 (declare-function quail-build-decode-map "quail")
     31 (declare-function quail-map "quail")
     32 
     33 (eval-and-compile
     34   (defun cape-char--translation (method prefix)
     35     "Return character translation hash for input method METHOD.
     36 PREFIX are the prefix characters. Names (hash keys) that map to
     37 multiple candidates (hash values) in the quail translation map
     38 are not included. Hash values are either char or strings."
     39     (when-let ((im (assoc method input-method-alist))
     40                ((eq #'quail-use-package (nth 2 im))))
     41       (let ((hash (make-hash-table :test #'equal))
     42             (dm (list 'decode-map)))
     43         (require 'quail)
     44         (apply #'quail-use-package method (nthcdr 5 im))
     45         (quail-build-decode-map (list (quail-map)) "" dm 0)
     46         (pcase-dolist (`(,name . ,val) (cdr dm))
     47           (when (memq (aref name 0) prefix)
     48             (puthash
     49              (if (equal method "emoji")
     50                  (string-replace "_" "-" name)
     51                name)
     52              (if (vectorp val) (aref val 0) val) hash)))
     53         (quail-deactivate)
     54         hash))))
     55 
     56 (defun cape-char--annotation (hash name)
     57   "Lookup NAME in HASH and return annotation."
     58   (when-let ((char (gethash name hash)))
     59     (format (if (stringp char) " %s " " %c ") char)))
     60 
     61 (defun cape-char--signature (hash name)
     62   "Lookup NAME in HASH and return signature."
     63   (when-let ((val (gethash name hash)))
     64     (concat
     65      (and (stringp val) (concat val " = "))
     66      (mapconcat
     67       (lambda (char)
     68         (format "%c %s (%s)"
     69                 char
     70                 (get-char-code-property char 'name)
     71                 (char-code-property-description
     72                  'general-category
     73                  (get-char-code-property char 'general-category))))
     74       (if (stringp val) val (list val))
     75       " + "))))
     76 
     77 (defun cape-char--exit (hash name status)
     78   "Exit function given completion STATUS, looks-up NAME in HASH."
     79   (when-let (((not (eq status 'exact)))
     80              (char (gethash name hash)))
     81     (delete-region (max (point-min) (- (point) (length name))) (point))
     82     (insert char)))
     83 
     84 (defmacro cape-char--define (name method &rest prefix)
     85   "Define character translation Capf.
     86 NAME is the name of the Capf.
     87 METHOD is the input method.
     88 PREFIX are the prefix characters."
     89   (when-let ((capf (intern (format "cape-%s" name)))
     90              (pre-req (intern (format "cape-%s-prefix-required" name)))
     91              (props (intern (format "cape--%s-properties" name)))
     92              (pre-rx (concat (regexp-opt (mapcar #'char-to-string prefix)) "[^ \n\t]*" ))
     93              (hash (intern (format "cape--%s-hash" name)))
     94              (hash-val (cape-char--translation method prefix)))
     95     `(progn
     96        (defvar ,hash ,hash-val)
     97        (defcustom ,pre-req t
     98          ,(format "Initial prefix is required for `%s' to trigger." capf)
     99          :type 'boolean
    100          :group 'cape)
    101        (defvar ,props
    102          (list :annotation-function (apply-partially #'cape-char--annotation ,hash)
    103                :company-docsig (apply-partially #'cape-char--signature ,hash)
    104                :exit-function (apply-partially #'cape-char--exit ,hash)
    105                :company-kind (lambda (_) 'text)
    106                :exclusive 'no)
    107          ,(format "Completion extra properties for `%s'." capf))
    108        (defun ,capf (&optional interactive)
    109          ,(format "Complete Unicode character at point.
    110 Uses the input format of the %s input method,
    111 see (describe-input-method %S). If INTERACTIVE is nil the
    112 function acts like a Capf." method method)
    113          (interactive (list t))
    114          (if interactive
    115              ;; No cycling since it breaks the :exit-function.
    116              (let (completion-cycle-threshold ,pre-req)
    117                (when (and (memq last-input-event ',prefix)
    118                           (not (looking-back ,pre-rx (pos-bol))))
    119                  (self-insert-command 1 last-input-event))
    120                (cape-interactive #',capf))
    121            (when-let ((bounds
    122                        (cond
    123                         ((looking-back ,pre-rx (pos-bol))
    124                          (cons (match-beginning 0) (point)))
    125                         ((not ,pre-req) (cons (point) (point))))))
    126              (append
    127               (list (car bounds) (cdr bounds)
    128                     (cape--properties-table ,hash :category ',capf))
    129               ,props)))))))
    130 
    131 ;;;###autoload (autoload 'cape-tex "cape-char" nil t)
    132 (cape-char--define tex "TeX" ?\\ ?^ ?_)
    133 
    134 ;;;###autoload (autoload 'cape-sgml "cape-char" nil t)
    135 (cape-char--define sgml "sgml" ?&)
    136 
    137 ;;;###autoload (autoload 'cape-rfc1345 "cape-char" nil t)
    138 (cape-char--define rfc1345 "rfc1345" ?&)
    139 
    140 ;;;###autoload (when (> emacs-major-version 28) (autoload 'cape-emoji "cape-char" nil t))
    141 (cape-char--define emoji "emoji" ?:)
    142 
    143 (provide 'cape-char)
    144 ;;; cape-char.el ends here