aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElías Gabriel Pérez2025-11-09 09:20:05 +0200
committerJuri Linkov2025-11-09 09:20:05 +0200
commit9fc46f1fd9580c19656688e379e146401c19ef66 (patch)
tree9de541c8a0915ae246b5746ebd12471c5a870a63
parentf11349ea1a5f391a457da90a7a07af9ba7f47e6d (diff)
downloademacs-9fc46f1fd9580c19656688e379e146401c19ef66.tar.gz
emacs-9fc46f1fd9580c19656688e379e146401c19ef66.zip
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.
-rw-r--r--lisp/progmodes/hideshow.el24
-rw-r--r--lisp/progmodes/python.el38
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."
1019 (when (>= (point) q) 1019 (when (>= (point) q)
1020 (list (and hideable p) (point)))))))) 1020 (list (and hideable p) (point))))))))
1021 1021
1022(defun hs--set-variable (var old-nth &optional default) 1022(defun hs--set-variable (var nth &optional default)
1023 "Set Hideshow VAR if already not set. 1023 "Set Hideshow VAR if already not set.
1024This function is meant to be used for backward compatibility with 1024This function is meant to be used for backward compatibility with
1025`hs-special-modes-alist'. 1025`hs-special-modes-alist'.
1026 1026
1027OLD-NTH is a number indicating NTH element of `hs-special-modes-alist'. 1027NTH must be a number indicating NTH element of
1028`hs-special-modes-alist' or a function.
1028 1029
1029DEFAULT is a value to use as fallback." 1030DEFAULT is a value to use as fallback."
1030 (unless (local-variable-p var) ; Already set, nothing to do. 1031 (unless (local-variable-p var) ; Already set, nothing to do.
1031 (if-let* (old-nth 1032 (if-let* ((old-lookup (assoc major-mode hs-special-modes-alist))
1032 (old-lookup (assoc major-mode hs-special-modes-alist))) 1033 (val (if (integerp nth)
1033 (set (make-local-variable var) 1034 (nth nth old-lookup)
1034 (if (integerp old-nth) 1035 (funcall nth old-lookup))))
1035 (nth old-nth old-lookup) 1036 (set (make-local-variable var) val)
1036 (funcall old-nth old-lookup)))
1037 (when default 1037 (when default
1038 (set (make-local-variable var) default))))) 1038 (set (make-local-variable var) default)))))
1039 1039
@@ -1048,12 +1048,14 @@ If `hs-special-modes-alist' has information associated with the current
1048buffer's major mode, use that. Otherwise, guess start, end and 1048buffer's major mode, use that. Otherwise, guess start, end and
1049`comment-start' regexps; `forward-sexp' function; and 1049`comment-start' regexps; `forward-sexp' function; and
1050adjust-block-beginning function." 1050adjust-block-beginning function."
1051 (hs--set-variable 'hs-block-start-regexp #'caadr) 1051 (hs--set-variable 'hs-block-start-regexp
1052 (hs--set-variable 'hs-block-start-mdata-select #'cadadr) 1052 (lambda (v) (car (ensure-list (nth 1 v)))))
1053 (hs--set-variable 'hs-block-start-mdata-select
1054 (lambda (v) (cadr (ensure-list (nth 1 v)))))
1053 (hs--set-variable 'hs-block-end-regexp 2) 1055 (hs--set-variable 'hs-block-end-regexp 2)
1054 (hs--set-variable 'hs-c-start-regexp 3 1056 (hs--set-variable 'hs-c-start-regexp 3
1055 (string-trim-right (regexp-quote comment-start))) 1057 (string-trim-right (regexp-quote comment-start)))
1056 (hs--set-variable 'hs-forward-sexp-function 4) 1058 (hs--set-variable 'hs-forward-sexp-function 4 #'forward-sexp)
1057 (hs--set-variable 'hs-adjust-block-beginning-function 5) 1059 (hs--set-variable 'hs-adjust-block-beginning-function 5)
1058 (hs--set-variable 'hs-find-block-beginning-function 6) 1060 (hs--set-variable 'hs-find-block-beginning-function 6)
1059 (hs--set-variable 'hs-find-next-block-function 7) 1061 (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'."
7355 #'python-eldoc-function)))) 7355 #'python-eldoc-function))))
7356 (eldoc-add-command-completions "python-indent-dedent-line-backspace") 7356 (eldoc-add-command-completions "python-indent-dedent-line-backspace")
7357 7357
7358 (setq-local hs-block-start-regexp python-nav-beginning-of-block-regexp) 7358 (if (< emacs-major-version 31)
7359 ;; Use the empty string as end regexp so it doesn't default to 7359 (dolist (mode '(python-mode python-ts-mode))
7360 ;; "\\s)". This way parens at end of defun are properly hidden. 7360 (add-to-list
7361 (setq-local hs-block-end-regexp "") 7361 'hs-special-modes-alist
7362 (setq-local hs-c-start-regexp "#") 7362 `(,mode
7363 (setq-local hs-forward-sexp-function #'python-hideshow-forward-sexp-function) 7363 ,python-nav-beginning-of-block-regexp
7364 (setq-local hs-find-block-beginning-function #'python-nav-beginning-of-block) 7364 ;; Use the empty string as end regexp so it doesn't default to
7365 (setq-local hs-find-next-block-function #'python-hideshow-find-next-block) 7365 ;; "\\s)". This way parens at end of defun are properly hidden.
7366 (setq-local hs-looking-at-block-start-predicate #'python-info-looking-at-beginning-of-block) 7366 ""
7367 "#"
7368 python-hideshow-forward-sexp-function
7369 nil
7370 python-nav-beginning-of-block
7371 python-hideshow-find-next-block
7372 python-info-looking-at-beginning-of-block)))
7373 (setq-local hs-block-start-regexp python-nav-beginning-of-block-regexp)
7374 ;; Use the empty string as end regexp so it doesn't default to
7375 ;; "\\s)". This way parens at end of defun are properly hidden.
7376 (setq-local hs-block-end-regexp "")
7377 (setq-local hs-c-start-regexp "#")
7378 (setq-local hs-forward-sexp-function #'python-hideshow-forward-sexp-function)
7379 (setq-local hs-find-block-beginning-function #'python-nav-beginning-of-block)
7380 (setq-local hs-find-next-block-function #'python-hideshow-find-next-block)
7381 (setq-local hs-looking-at-block-start-predicate #'python-info-looking-at-beginning-of-block))
7367 7382
7368 (setq-local outline-regexp (python-rx (* space) block-start)) 7383 (setq-local outline-regexp (python-rx (* space) block-start))
7369 (setq-local outline-level 7384 (setq-local outline-level
@@ -7437,8 +7452,9 @@ implementations: `python-mode' and `python-ts-mode'."
7437 (setq-local forward-sexp-function #'treesit-forward-sexp 7452 (setq-local forward-sexp-function #'treesit-forward-sexp
7438 treesit-sexp-thing 'sexp) 7453 treesit-sexp-thing 'sexp)
7439 7454
7440 (setq-local hs-treesit-things '(or defun sexp)) 7455 (when (>= emacs-major-version 31)
7441 (setq-local hs-adjust-block-end-function #'python-ts-hs-adjust-block-end-fn) 7456 (setq-local hs-treesit-things '(or defun sexp))
7457 (setq-local hs-adjust-block-end-function #'python-ts-hs-adjust-block-end-fn))
7442 7458
7443 (setq-local syntax-propertize-function #'python--treesit-syntax-propertize) 7459 (setq-local syntax-propertize-function #'python--treesit-syntax-propertize)
7444 7460