From 9fc46f1fd9580c19656688e379e146401c19ef66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= Date: Sun, 9 Nov 2025 09:20:05 +0200 Subject: [PATCH] hideshow: Rework previous changes * lisp/progmodes/hideshow.el (hs-grok-mode-type): Improve 'hs--set-variable'. * lisp/progmodes/python.el (python-base-mode, python-ts-mode): Provide backward-compatibility for older versions. --- lisp/progmodes/hideshow.el | 24 +++++++++++++----------- lisp/progmodes/python.el | 38 +++++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el index 764fdb66848..c8b4e68f00f 100644 --- a/lisp/progmodes/hideshow.el +++ b/lisp/progmodes/hideshow.el @@ -1019,21 +1019,21 @@ Otherwise, return nil." (when (>= (point) q) (list (and hideable p) (point)))))))) -(defun hs--set-variable (var old-nth &optional default) +(defun hs--set-variable (var nth &optional default) "Set Hideshow VAR if already not set. This function is meant to be used for backward compatibility with `hs-special-modes-alist'. -OLD-NTH is a number indicating NTH element of `hs-special-modes-alist'. +NTH must be a number indicating NTH element of +`hs-special-modes-alist' or a function. DEFAULT is a value to use as fallback." (unless (local-variable-p var) ; Already set, nothing to do. - (if-let* (old-nth - (old-lookup (assoc major-mode hs-special-modes-alist))) - (set (make-local-variable var) - (if (integerp old-nth) - (nth old-nth old-lookup) - (funcall old-nth old-lookup))) + (if-let* ((old-lookup (assoc major-mode hs-special-modes-alist)) + (val (if (integerp nth) + (nth nth old-lookup) + (funcall nth old-lookup)))) + (set (make-local-variable var) val) (when default (set (make-local-variable var) default))))) @@ -1048,12 +1048,14 @@ If `hs-special-modes-alist' has information associated with the current buffer's major mode, use that. Otherwise, guess start, end and `comment-start' regexps; `forward-sexp' function; and adjust-block-beginning function." - (hs--set-variable 'hs-block-start-regexp #'caadr) - (hs--set-variable 'hs-block-start-mdata-select #'cadadr) + (hs--set-variable 'hs-block-start-regexp + (lambda (v) (car (ensure-list (nth 1 v))))) + (hs--set-variable 'hs-block-start-mdata-select + (lambda (v) (cadr (ensure-list (nth 1 v))))) (hs--set-variable 'hs-block-end-regexp 2) (hs--set-variable 'hs-c-start-regexp 3 (string-trim-right (regexp-quote comment-start))) - (hs--set-variable 'hs-forward-sexp-function 4) + (hs--set-variable 'hs-forward-sexp-function 4 #'forward-sexp) (hs--set-variable 'hs-adjust-block-beginning-function 5) (hs--set-variable 'hs-find-block-beginning-function 6) (hs--set-variable 'hs-find-next-block-function 7) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 12557aecf4d..c4cf7ec46cf 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -7355,15 +7355,30 @@ implementations: `python-mode' and `python-ts-mode'." #'python-eldoc-function)))) (eldoc-add-command-completions "python-indent-dedent-line-backspace") - (setq-local hs-block-start-regexp python-nav-beginning-of-block-regexp) - ;; Use the empty string as end regexp so it doesn't default to - ;; "\\s)". This way parens at end of defun are properly hidden. - (setq-local hs-block-end-regexp "") - (setq-local hs-c-start-regexp "#") - (setq-local hs-forward-sexp-function #'python-hideshow-forward-sexp-function) - (setq-local hs-find-block-beginning-function #'python-nav-beginning-of-block) - (setq-local hs-find-next-block-function #'python-hideshow-find-next-block) - (setq-local hs-looking-at-block-start-predicate #'python-info-looking-at-beginning-of-block) + (if (< emacs-major-version 31) + (dolist (mode '(python-mode python-ts-mode)) + (add-to-list + 'hs-special-modes-alist + `(,mode + ,python-nav-beginning-of-block-regexp + ;; Use the empty string as end regexp so it doesn't default to + ;; "\\s)". This way parens at end of defun are properly hidden. + "" + "#" + python-hideshow-forward-sexp-function + nil + python-nav-beginning-of-block + python-hideshow-find-next-block + python-info-looking-at-beginning-of-block))) + (setq-local hs-block-start-regexp python-nav-beginning-of-block-regexp) + ;; Use the empty string as end regexp so it doesn't default to + ;; "\\s)". This way parens at end of defun are properly hidden. + (setq-local hs-block-end-regexp "") + (setq-local hs-c-start-regexp "#") + (setq-local hs-forward-sexp-function #'python-hideshow-forward-sexp-function) + (setq-local hs-find-block-beginning-function #'python-nav-beginning-of-block) + (setq-local hs-find-next-block-function #'python-hideshow-find-next-block) + (setq-local hs-looking-at-block-start-predicate #'python-info-looking-at-beginning-of-block)) (setq-local outline-regexp (python-rx (* space) block-start)) (setq-local outline-level @@ -7437,8 +7452,9 @@ implementations: `python-mode' and `python-ts-mode'." (setq-local forward-sexp-function #'treesit-forward-sexp treesit-sexp-thing 'sexp) - (setq-local hs-treesit-things '(or defun sexp)) - (setq-local hs-adjust-block-end-function #'python-ts-hs-adjust-block-end-fn) + (when (>= emacs-major-version 31) + (setq-local hs-treesit-things '(or defun sexp)) + (setq-local hs-adjust-block-end-function #'python-ts-hs-adjust-block-end-fn)) (setq-local syntax-propertize-function #'python--treesit-syntax-propertize)