From 99be4333aa9a662fb49a182140e4079ffcecb570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augustin=20Ch=C3=A9neau?= Date: Sat, 26 Aug 2023 00:03:41 +0200 Subject: [PATCH] Escape strings displayed by the mode-line Strings containing a percent character need to be escaped to be displayed properly, as a percent ('%') in the mode-line denotes a '%-contruct' with a special meaning. * lisp/treesit.el (treesit-inspect-mode): Escape `treesit--inspect-name` --- lisp/treesit.el | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index 04d460fdea4..2ae94594f75 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2466,6 +2466,13 @@ treesit-inspect-node-at-point (message "%s" treesit--inspect-name) (message "No node at point"))))) +(defun treesit--escape-mode-line (str) + "Escape the string STR that will be displayed in the mode-line. + +It replaces the percent character '%' with a double percent '%%', as a single +percent has a special meaning in the mode-line." + (string-replace "%" "%%" str)) + (define-minor-mode treesit-inspect-mode "Minor mode that displays in the mode-line the node which starts at point. @@ -2490,11 +2497,11 @@ treesit-inspect-mode (add-hook 'post-command-hook #'treesit-inspect-node-at-point 0 t) (add-to-list 'mode-line-misc-info - '(:eval treesit--inspect-name))) + '(:eval (treesit--escape-mode-line treesit--inspect-name)))) (remove-hook 'post-command-hook #'treesit-inspect-node-at-point t) (setq mode-line-misc-info - (remove '(:eval treesit--inspect-name) + (remove '(:eval (treesit--escape-mode-line treesit--inspect-name)) mode-line-misc-info)))) (defun treesit-query-validate (language query) -- 2.42.0