1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-03-18 12:52:46 -07:00

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.
This commit is contained in:
Elías Gabriel Pérez 2025-11-09 09:20:05 +02:00 committed by Juri Linkov
parent f11349ea1a
commit 9fc46f1fd9
2 changed files with 40 additions and 22 deletions

View file

@ -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)

View file

@ -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)