1
0
Fork 0

Apply trick to insert font-locked strings in a font-locked buffer

This commit is contained in:
Bram Schoenmakers 2024-07-23 20:05:18 +02:00
parent 3a9964e905
commit 714db74bb0
Signed by: bram
GPG key ID: 0CCD19DFDC63258F

19
kagi.el
View file

@ -257,6 +257,21 @@ https://help.kagi.com/kagi/api/fastgpt.html for more information."
nil)
(buffer-string)))
(defun kagi--fontify-using-faces (s)
"Set the fontified property in the string S.
This is needed to insert a font-locked string (generated by
`kagi--apply-markdown-font-lock') in a font-lock enabled buffer."
;; Inspired by this answer at Emacs StackExchange:
;; https://emacs.stackexchange.com/a/5408
(let ((pos 0)
(next))
(while (setq next (next-single-property-change pos 'face s))
(put-text-property pos next 'font-lock-face (get-text-property pos 'face s) s)
(setq pos next))
(add-text-properties 0 (length s) '(fontified t) s)
s))
(defun kagi--curl-flags ()
"Collect flags for a `curl' command to call the Kagi API."
(let ((token (cond ((functionp kagi-api-token) (funcall kagi-api-token))
@ -402,7 +417,9 @@ object (created with `make-shell-maker-config')."
:execute-command
(lambda (command _history callback error-callback)
(condition-case err
(funcall callback (kagi-fastgpt-prompt command) nil)
(funcall callback
(kagi--fontify-using-faces (kagi-fastgpt-prompt command))
nil)
(json-parse-error (funcall error-callback
(format "Could not parse the server response %s" (cdr err))))
(error (funcall error-callback (format "An error occurred during the request %s" (cdr err)))))))