1
0
Fork 0

Fix %s expansion in prompts defined with define-kagi-fastgpt-prompt

The expansion is done with replace-regexp-in-string, which does a
combination of string-match and replace-match. When a function is
passed to replace-regexp-in-string instead of a text, it will call
kagi--get-text-for-prompt which uses the minibuffer / completing read,
possibly messing up the (match-data) along the way that
replace-regexp-in-string relies on. Therefore, save the matching data
before calling the supplied text function, so it will be restored and
replace-regexp-in-string won't notice it.
This commit is contained in:
Bram Schoenmakers 2024-07-23 21:20:41 +02:00
parent 153ec9a838
commit 002393ad7a
Signed by: bram
GPG key ID: 0CCD19DFDC63258F
3 changed files with 7 additions and 1 deletions

View file

@ -268,6 +268,10 @@ The full changelog can be found in README.org.
- Show a welcome message when starting the FastGPT shell (=kagi-fastgpt-shell=). This can be customized with =kagi-fastgpt-welcome-function=, a variable with a function that returns a welcome string.
*** Fixes
- Prompts defined with =define-kagi-fastgpt-prompt= wouldn't always work when expanding =%s= placeholders (possibly resulting in =Lisp error: (args-out-of-range 0 7)=).
*** Misc
- Minor refactoring in the code calling the FastGPT API.

View file

@ -465,7 +465,9 @@ user input."
(lambda (match)
(pcase match
("%%" "%")
("%s" (or user-text (setq user-text (funcall text-function))))
("%s" (or user-text
(setq user-text (save-match-data
(funcall text-function)))))
(_ match)))
prompt t t)))

BIN
kagi.info

Binary file not shown.