
dc4e6b1329
; Update copyright years in more files64b3777631
; Run set-copyright from admin.el8e1c56ae46
; Add 2024 to copyright years # Conflicts: # doc/misc/modus-themes.org # doc/misc/texinfo.tex # etc/NEWS # etc/refcards/ru-refcard.tex # etc/themes/modus-operandi-theme.el # etc/themes/modus-themes.el # etc/themes/modus-vivendi-theme.el # lib/alloca.in.h # lib/binary-io.h # lib/c-ctype.h # lib/c-strcasecmp.c # lib/c-strncasecmp.c # lib/careadlinkat.c # lib/cloexec.c # lib/close-stream.c # lib/diffseq.h # lib/dup2.c # lib/filemode.h # lib/fpending.c # lib/fpending.h # lib/fsusage.c # lib/getgroups.c # lib/getloadavg.c # lib/gettext.h # lib/gettime.c # lib/gettimeofday.c # lib/group-member.c # lib/malloc.c # lib/md5-stream.c # lib/md5.c # lib/md5.h # lib/memmem.c # lib/memrchr.c # lib/nanosleep.c # lib/save-cwd.h # lib/sha1.c # lib/sig2str.c # lib/stdlib.in.h # lib/strtoimax.c # lib/strtol.c # lib/strtoll.c # lib/time_r.c # lib/xalloc-oversized.h # lisp/auth-source-pass.el # lisp/emacs-lisp/lisp-mnt.el # lisp/emacs-lisp/timer.el # lisp/info-look.el # lisp/jit-lock.el # lisp/loadhist.el # lisp/mail/rmail.el # lisp/net/ntlm.el # lisp/net/webjump.el # lisp/progmodes/asm-mode.el # lisp/progmodes/project.el # lisp/progmodes/sh-script.el # lisp/textmodes/flyspell.el # lisp/textmodes/reftex-toc.el # lisp/textmodes/reftex.el # lisp/textmodes/tex-mode.el # lisp/url/url-gw.el # m4/alloca.m4 # m4/clock_time.m4 # m4/d-type.m4 # m4/dirent_h.m4 # m4/dup2.m4 # m4/euidaccess.m4 # m4/fchmodat.m4 # m4/filemode.m4 # m4/fsusage.m4 # m4/getgroups.m4 # m4/getloadavg.m4 # m4/getrandom.m4 # m4/gettime.m4 # m4/gettimeofday.m4 # m4/gnulib-common.m4 # m4/group-member.m4 # m4/inttypes.m4 # m4/malloc.m4 # m4/manywarnings.m4 # m4/mempcpy.m4 # m4/memrchr.m4 # m4/mkostemp.m4 # m4/mktime.m4 # m4/nproc.m4 # m4/nstrftime.m4 # m4/pathmax.m4 # m4/pipe2.m4 # m4/pselect.m4 # m4/pthread_sigmask.m4 # m4/readlink.m4 # m4/realloc.m4 # m4/sig2str.m4 # m4/ssize_t.m4 # m4/stat-time.m4 # m4/stddef_h.m4 # m4/stdint.m4 # m4/stdio_h.m4 # m4/stdlib_h.m4 # m4/stpcpy.m4 # m4/strnlen.m4 # m4/strtoimax.m4 # m4/strtoll.m4 # m4/time_h.m4 # m4/timegm.m4 # m4/timer_time.m4 # m4/timespec.m4 # m4/unistd_h.m4 # m4/warnings.m4 # nt/configure.bat # nt/preprep.c # test/lisp/register-tests.el
179 lines
8.2 KiB
EmacsLisp
179 lines
8.2 KiB
EmacsLisp
;;; external-completion.el --- Let external tools control completion style -*- lexical-binding: t; -*-
|
|
|
|
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
|
|
|
|
;; Version: 0.1
|
|
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
|
|
;; Maintainer: João Távora <joaotavora@gmail.com>
|
|
;; Keywords:
|
|
|
|
;; This is a GNU ELPA :core package. Avoid functionality that is not
|
|
;; compatible with the version of Emacs recorded above.
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;; GNU Emacs 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.
|
|
|
|
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;; Written by Stefan Monnier circa 2016. Variants of this code have
|
|
;; been working stably in SLY and other packages for a long time.
|
|
|
|
;; The `external' completion style is used with a "programmable
|
|
;; completion" table that gathers completions from an external tool
|
|
;; such as a shell utility, an inferior process, an http server.
|
|
|
|
;; The table and external tool are fully in control of the matching of
|
|
;; the pattern string to the potential candidates of completion. When
|
|
;; `external' is in use, the usual styles configured by the user or
|
|
;; other in `completion-styles' are ignored.
|
|
;;
|
|
;; This compromise is for speed: all other styles need the full data
|
|
;; set to be available in Emacs' addressing space, which is often slow
|
|
;; if not completely unfeasible.
|
|
;;
|
|
;; To make use of the `external' style the function
|
|
;; `external-completion-table' should be used. See its docstring.
|
|
|
|
;;; Code:
|
|
(require 'cl-lib)
|
|
|
|
(add-to-list 'completion-styles-alist
|
|
'(external
|
|
external-completion--try-completion
|
|
external-completion--all-completions
|
|
"Ad-hoc completion style provided by the completion table."))
|
|
|
|
(defun external-completion-table (category lookup
|
|
&optional metadata
|
|
try-completion-function)
|
|
"Make completion table using the `external' completion style.
|
|
|
|
The `external' style is particularly useful when the caller
|
|
interfaces with an external tool that provides completions. This
|
|
may be a shell utility, an inferior process, an http server, etc.
|
|
Given a pattern string, the external tool matches it to an
|
|
arbitrarily large set of candidates. Since the full set doesn't
|
|
need to be transferred to Emacs's address space, this often
|
|
results in much faster overall experience, at the expense of the
|
|
convenience of offered by other completion styles.
|
|
|
|
CATEGORY is a symbol uniquely naming the external tool. This
|
|
function links CATEGORY to the style `external', by modifying
|
|
`completion-category-defaults', overriding any styles normally
|
|
set in `completion-styles'.
|
|
|
|
LOOKUP is a function taking a string PATTERN and a number
|
|
POINT. The function should contact the tool and return a list of
|
|
strings representing the completions for PATTERN given that POINT
|
|
is the location of point within it. LOOKUP decides if PATTERN is
|
|
interpreted as a substring, a regular expression, or any other
|
|
type of matching method. The strings returned may be propertized
|
|
with `completions-common-part' to illustrate the specific method
|
|
used. LOOKUP may ignore POINT if it doesn't meaningfully alter
|
|
the results.
|
|
|
|
LOOKUP is a synchronous blocking function. Since it contacts an
|
|
external tool, it's possible that it takes significant time to
|
|
return results. To maintain Emacs's responsiveness, LOOKUP
|
|
should detect pending user input using `while-no-input' or
|
|
`sit-for' (which see). In those cases, LOOKUP should attempt to
|
|
cancel the request (if possible) and immediately return any
|
|
non-list.
|
|
|
|
METADATA is an alist of additional properties such as
|
|
`cycle-sort-function' to associate with CATEGORY. This means
|
|
that the caller may still retain control the sorting of the
|
|
candidates while the tool controls the matching.
|
|
|
|
Optional TRY-COMPLETION-FUNCTION helps some frontends partially
|
|
or fully expand PATTERN before finishing the completion
|
|
operation. If supplied, it is a function taking a (PATTERN POINT
|
|
ALL-COMPLETIONS), where PATTERN and POINT are as described above
|
|
and ALL-COMPLETIONS are gathered by LOOKUP for these
|
|
arguments (this function ensures LOOKUP isn't called more than
|
|
needed). If you know the matching method that the external tool
|
|
using, TRY-COMPLETION-FUNCTION may return a cons
|
|
cell (EXPANDED-PATTERN . NEW-POINT). For example, if the tool is
|
|
completing by prefix, one could call `try-completion' to find the
|
|
largest common prefix in ALL-COMPLETIONS and then return that as
|
|
EXPANDED-PATTERN."
|
|
(let ((probe (alist-get category completion-category-defaults)))
|
|
(if probe
|
|
(cl-assert (equal '(external) (alist-get 'styles probe))
|
|
nil "Category `%s' must only use `external' style" category)
|
|
(push `(,category (styles external))
|
|
completion-category-defaults)))
|
|
(let ((cache (make-hash-table :test #'equal)))
|
|
(cl-flet ((lookup-internal (string point)
|
|
(let* ((key (cons string point))
|
|
(probe (gethash key cache 'external--notfound)))
|
|
(if (eq probe 'external--notfound)
|
|
(puthash key (funcall lookup string point) cache)
|
|
probe))))
|
|
(lambda (string pred action)
|
|
(pcase action
|
|
(`metadata
|
|
`(metadata (category . ,category) . ,metadata))
|
|
;; Note: the `--tryc' `--allc' suffixes are made akward on
|
|
;; purpose, so it's easy to pick them apart from the jungle
|
|
;; of combinations of "try" and "all" and "completion" that
|
|
;; inhabit Emacs's completion logic.
|
|
(`(external-completion--tryc . ,point)
|
|
;; FIXME: Obey `pred'? Pass it to `try-completion-function'?
|
|
`(external-completion--tryc
|
|
. ,(if try-completion-function
|
|
(funcall try-completion-function
|
|
string
|
|
point
|
|
(lookup-internal string point))
|
|
(cons string point))))
|
|
(`(external-completion--allc . ,point)
|
|
(let ((all (lookup-internal string point)))
|
|
`(external-completion--allc
|
|
. ,(if pred (cl-remove-if-not pred all) all))))
|
|
(`(boundaries . ,_) nil)
|
|
(_method
|
|
(let ((all (lookup-internal string (length string))))
|
|
;; This branch might be taken:
|
|
;;
|
|
;; * when users work around
|
|
;; `completion-category-defaults' (via
|
|
;; `completion-category-overrides') and access this
|
|
;; table with another completion style. We assume
|
|
;; these users know what they are doing, but it might
|
|
;; not work very well, as this whatever is in `all'
|
|
;; very often doesn't equate the full set of candidates
|
|
;; (many tools cap to sth like 100-1000 results).
|
|
;;
|
|
;; * when `_method' is nil or `lambda' which some
|
|
;; frontends will invoke. Here, `all' should be
|
|
;; sufficient information for `complete-with-action' to
|
|
;; do the job correctly.
|
|
(complete-with-action action all string pred))))))))
|
|
|
|
(defun external-completion--call (op string table pred point)
|
|
(when (functionp table)
|
|
(let ((res (funcall table string pred (cons op point))))
|
|
(when (eq op (car-safe res))
|
|
(cdr res)))))
|
|
|
|
(defun external-completion--try-completion (string table pred point)
|
|
(external-completion--call 'external-completion--tryc string table pred point))
|
|
|
|
(defun external-completion--all-completions (string table pred point)
|
|
(external-completion--call 'external-completion--allc string table pred point))
|
|
|
|
(provide 'external-completion)
|
|
;;; external-completion.el ends here
|