emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/cape e341f81a79 098/146: README: Add emoji example


From: ELPA Syncer
Subject: [elpa] externals/cape e341f81a79 098/146: README: Add emoji example
Date: Sun, 9 Jan 2022 20:57:45 -0500 (EST)

branch: externals/cape
commit e341f81a79be8d6880b9cf608432e6098af034f8
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    README: Add emoji example
---
 README.org | 33 +++++++++++++++++++++++++++++++++
 cape.el    | 15 ++++++++++-----
 2 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/README.org b/README.org
index 87b81af7e0..7bc9784ac9 100644
--- a/README.org
+++ b/README.org
@@ -88,6 +88,39 @@ have certain edge cases. The adapter can be used as follows:
       (list #'company-files #'company-ispell #'company-dabbrev)))
 #+end_src
 
+Note that the adapter does not require Company to be installed. Backends
+implementing the Company specification do not necessarily have to depend on
+Company, however in practice most backends do. The following shows a small
+example completion backend, which can be used with both ~completion-at-point~
+(Corfu, default completion) and Company.
+
+#+begin_src emacs-lisp
+  (defvar emojis
+    '((":-D" . "😀")
+      (";-)" . "😉")
+      (":-/" . "😕")
+      (":-(" . "🙁")
+      (":-*" . "😙")))
+
+  (defun emoji-backend (action &optional arg &rest _)
+    (pcase action
+      ('prefix (and (memq (char-before) '(?: ?\;))
+                    (cons (string (char-before)) t)))
+      ('candidates (all-completions arg emojis))
+      ('annotation (concat " " (cdr (assoc arg emojis))))
+      ('post-completion
+       (let ((str (buffer-substring (- (point) 3) (point))))
+         (delete-region (- (point) 3) (point))
+       (insert (cdr (assoc str emojis)))))))
+
+  ;; Register emoji backend with `completion-at-point'
+  (setq completion-at-point-functions
+        (list (cape-company-to-capf #'emoji-backend)))
+
+  ;; Register emoji backend with Company.
+  (setq company-backends '(emoji-backend))
+#+end_src
+
 ** Super-Capf - Merging multiple Capfs
 
 /Throw multiple Capfs under the Cape and get a Super-Capf!/
diff --git a/cape.el b/cape.el
index 8656c3e078..3a90031b6b 100644
--- a/cape.el
+++ b/cape.el
@@ -42,6 +42,14 @@
   "Dictionary word list file."
   :type 'string)
 
+(defcustom cape-company-async-timeout 1.0
+  "Company asynchronous timeout."
+  :type 'float)
+
+(defcustom cape-company-async-wait 0.02
+  "Company asynchronous busy waiting time."
+  :type 'float)
+
 (defcustom cape-dabbrev-min-length 4
   "Minimum length of dabbrev expansions."
   :type 'integer)
@@ -727,9 +735,6 @@ If INTERACTIVE is nil the function acts like a capf."
               :annotation-function (funcall extra-fun :annotation-function)
               :exit-function (lambda (x _status) (funcall (funcall extra-fun 
:exit-function) x)))))))
 
-(defvar company-async-wait)
-(defvar company-async-timeout)
-
 (defun cape--company-call (backend &rest args)
   "Call Company BACKEND with ARGS."
   ;; Company backends are non-interruptible.
@@ -741,8 +746,8 @@ If INTERACTIVE is nil the function acts like a capf."
        (let (throw-on-input) (funcall fetcher (lambda (arg) (setq res arg))))
        ;; Force synchronization. The synchronization is interruptible!
        (while (eq res 'trash)
-         (sleep-for company-async-wait)
-         (when (> (- (time-to-seconds) start) company-async-timeout)
+         (sleep-for cape-company-async-wait)
+         (when (> (- (time-to-seconds) start) cape-company-async-timeout)
            (error "Cape company backend async timeout")))
        res))
     (res res)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]