From 9b97dbbc7624415ee25f79de9ea357feb1e2e547 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Fri, 23 Dec 2022 19:19:43 +0100 Subject: Add cape-capf-inside-string and cape-capf-inside-comment These utilties can be used to tie a Capf to a certain context. There exist multiple ways you can organize your `completion-at-point-functions'. The main programming mode Capf can come first in the list. Then subsequent auxiliary Capfs will only take over if the main Capf bails out. Sometimes the main Capf is exclusive and never bails out. You could then turn it non-exclusive with `cape-capf-nonexclusive'. Alternatively you could move the auxiliary Capfs before the main Capf. Then you could use the `cape-capf-inside-*' helpers to make the auxiliary Capfs less aggressive, such that they trigger only inside comments or strings. There is no silver bullet here and the setup depends highly on the mode. These Capf transformers are just meant to give you flexible tools at hand such that you can build the completion pipeline which works best for you. (There has been some prior discussion about context dependent Capfs and predicates a long time ago in #9 and #11, but the addition of this feature got actually inspired by @LuigiPiucco's Doom PR.) --- README.org | 9 +++++---- cape.el | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.org b/README.org index c6d6474..aa853aa 100644 --- a/README.org +++ b/README.org @@ -100,7 +100,7 @@ could be upstreamed into Emacs itself. ) #+end_src -* Experimental features +* CAPF adapters and transformers ** Company adapter /Wrap your Company backend in a Cape and turn it into a Capf!/ @@ -219,7 +219,7 @@ achieve a similarly refreshing strategy. (list (cape-capf-buster #'some-caching-capf))) #+end_src -** Other Capf transformers +** Capf transformers Cape provides a set of additional Capf transformation functions, which are mostly meant to used by experts to fine tune the Capf behavior and Capf @@ -236,8 +236,9 @@ the Capf transformers with =defalias= to a function symbol. - ~cape-wrap-case-fold~, ~cape-capf-case-fold~: Create a Capf which is case insensitive. - ~cape-wrap-properties~, ~cape-capf-properties~: Add completion properties to a Capf. - ~cape-wrap-predicate~, ~cape-capf-predicate~: Add candidate predicate to a Capf. -- ~cape-wrap-prefix-length~, ~cape-capf-prefix-length~: Enforce a minimal prefix - length. +- ~cape-wrap-prefix-length~, ~cape-capf-prefix-length~: Enforce a minimal prefix length. +- ~cape-wrap-inside-comment~, ~cape-capf-inside-comment~: Ensure that Capf triggers only inside comment. +- ~cape-wrap-inside-string~, ~cape-capf-inside-string~: Ensure that Capf triggers only inside a string literal. In the following we show a few example configurations, which have come up on the [[https://github.com/minad/cape/issues][Cape]] or [[https://github.com/minad/corfu/issues][Corfu issue tracker]] or the [[https://github.com/minad/corfu/wiki][Corfu wiki.]] I use some of these tweaks in my diff --git a/cape.el b/cape.el index 575dc90..cc1a93b 100644 --- a/cape.el +++ b/cape.el @@ -833,6 +833,16 @@ If the prefix is long enough, enforce auto completion." ,@plist))))) ;;;###autoload +(defun cape-wrap-inside-comment (capf) + "Call CAPF only if inside comment." + (and (nth 4 (syntax-ppss)) (funcall capf))) + +;;;###autoload +(defun cape-wrap-inside-string (capf) + "Call CAPF only if inside string." + (and (nth 3 (syntax-ppss)) (funcall capf))) + +;;;###autoload (defun cape-wrap-purify (capf) "Call CAPF and ensure that it does not modify the buffer." ;; bug#50470: Fix Capfs which illegally modify the buffer or which @@ -868,6 +878,10 @@ If the prefix is long enough, enforce auto completion." (cape--capf-wrapper buster) ;;;###autoload (autoload 'cape-capf-case-fold "cape") (cape--capf-wrapper case-fold) +;;;###autoload (autoload 'cape-capf-inside-comment "cape") +(cape--capf-wrapper inside-comment) +;;;###autoload (autoload 'cape-capf-inside-string "cape") +(cape--capf-wrapper inside-string) ;;;###autoload (autoload 'cape-capf-noninterruptible "cape") (cape--capf-wrapper noninterruptible) ;;;###autoload (autoload 'cape-capf-nonexclusive "cape") -- cgit v1.0