aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElías Gabriel Pérez2026-01-02 19:24:48 -0600
committerEli Zaretskii2026-01-10 14:55:55 +0200
commitbdd9acb6aa3b7ef049b5aa4b8114e37476d0d6ed (patch)
treef06c84ec80fe2b3148bec10315d3f0c8899ce973
parent316fd1070242a48ab28c0e5223ca53486cc11e68 (diff)
downloademacs-bdd9acb6aa3b7ef049b5aa4b8114e37476d0d6ed.tar.gz
emacs-bdd9acb6aa3b7ef049b5aa4b8114e37476d0d6ed.zip
hideshow: Reword documentation (bug#80116)
* etc/NEWS: Reword entry. * lisp/progmodes/hideshow.el: Reword Commentary Header. (hs-block-start-mdata-select, hs-block-end-regexp) (hs-c-start-regexp, hs-forward-sexp-function) (hs-adjust-block-beginning-function) (hs-adjust-block-end-function, hs-find-block-beginning-function) (hs-find-next-block-function) (hs-looking-at-block-start-predicate) (hs-inside-comment-predicate): Reword docstrings. (hs-block-positions): Reword docstring, and move 'hs-adjust-block-beginning-function' so that it is not affected by 'pos-eol'. (hs-find-block-beg-fn--default): Use 0 instead of 'hs-block-start-mdata-select'. * lisp/treesit.el (treesit-hs-find-block-beginning): Update code.
-rw-r--r--etc/NEWS2
-rw-r--r--lisp/progmodes/hideshow.el131
-rw-r--r--lisp/treesit.el4
3 files changed, 73 insertions, 64 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 27aff3b7a1e..93d40a9d384 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1241,7 +1241,7 @@ buffer-local variables 'hs-block-start-regexp', 'hs-c-start-regexp',
1241'hs-forward-sexp-function', etc. 1241'hs-forward-sexp-function', etc.
1242 1242
1243+++ 1243+++
1244*** 'hs-hide-level' and 'hs-cycle' can now hide comments too. 1244*** 'hs-hide-level' can now hide comments too.
1245This is controlled by 'hs-hide-comments-when-hiding-all'. 1245This is controlled by 'hs-hide-comments-when-hiding-all'.
1246 1246
1247** C-ts mode 1247** C-ts mode
diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el
index 0c665f2afdf..4f2942ee9e9 100644
--- a/lisp/progmodes/hideshow.el
+++ b/lisp/progmodes/hideshow.el
@@ -169,13 +169,25 @@
169;; These variables help hideshow know what is considered a block, which 169;; These variables help hideshow know what is considered a block, which
170;; function to use to get the block positions, etc. 170;; function to use to get the block positions, etc.
171;; 171;;
172;; A block is defined as text surrounded by `hs-block-start-regexp' and 172;; A (code) block is defined as text surrounded by
173;; `hs-block-end-regexp'. 173;; `hs-block-start-regexp' and `hs-block-end-regexp'.
174;; 174;;
175;; For some major modes, forward-sexp does not work properly. In those 175;; For some major modes, forward-sexp does not work properly. In those
176;; cases, `hs-forward-sexp-function' specifies another function to use 176;; cases, `hs-forward-sexp-function' specifies another function to use
177;; instead. 177;; instead.
178 178
179;; *** Non-regexp matching
180;;
181;; By default, Hideshow uses regular expressions to match blocks. For
182;; something more advanced than regexp is necessary to modify these
183;; variables (see their docstring):
184;; - `hs-forward-sexp-function'
185;; - `hs-find-block-beginning-function'
186;; - `hs-find-next-block-function'
187;; - `hs-looking-at-block-start-predicate'
188;; - `hs-inside-comment-predicate' (For comments)
189;; - `hs-block-end-regexp' (Preferably, this should be set to nil)
190;;
179;; *** Tree-sitter support 191;; *** Tree-sitter support
180;; 192;;
181;; All the treesit based modes already have support for hiding/showing 193;; All the treesit based modes already have support for hiding/showing
@@ -616,24 +628,26 @@ Note that `mode-line-format' is buffer-local.")
616(defvar-local hs-block-start-regexp "\\s(" 628(defvar-local hs-block-start-regexp "\\s("
617 "Regexp for beginning of block.") 629 "Regexp for beginning of block.")
618 630
619;; This is useless, so probably should be deprecated.
620(defvar-local hs-block-start-mdata-select 0 631(defvar-local hs-block-start-mdata-select 0
621 "Element in `hs-block-start-regexp' match data to consider as block start. 632 "Element in `hs-block-start-regexp' match data to consider as block start.
622The internal function `hs-forward-sexp' moves point to the beginning of this 633This is used by `hs-block-positions' to move point to the beginning of
623element (using `match-beginning') before calling `hs-forward-sexp-function'.") 634this element (using `match-beginning') before calling
635`hs-forward-sexp-function'.
636
637This is used for regexp matching.")
624 638
625(defvar-local hs-block-end-regexp "\\s)" 639(defvar-local hs-block-end-regexp "\\s)"
626 "Regexp for end of block. 640 "Regexp for end of block.
627As a special case, the value can be also a function without arguments to 641This is mostly used to determine if point is at the end of the block.
628determine if point is looking at the end of the block, and return 642
629non-nil and set `match-data' to that block end positions.") 643As a special case, it can be nil (to use the position from
644`hs-forward-sexp-function'), or a function without arguments. If it's a
645function, it should return non-nil if point is at end of a block, and
646set `match-data' to that position.")
630 647
631(defvar-local hs-c-start-regexp nil 648(defvar-local hs-c-start-regexp nil
632 "Regexp for beginning of comments. 649 "Regexp for beginning of comments.
633Differs from mode-specific comment regexps in that surrounding 650If not bound, Hideshow will use current `comment-start' value without
634whitespace is stripped.
635
636If not bound, hideshow will use current `comment-start' value without
637any trailing whitespace.") 651any trailing whitespace.")
638 652
639(define-obsolete-variable-alias 653(define-obsolete-variable-alias
@@ -641,13 +655,13 @@ any trailing whitespace.")
641 'hs-forward-sexp-function "31.1") 655 'hs-forward-sexp-function "31.1")
642 656
643(defvar-local hs-forward-sexp-function #'forward-sexp 657(defvar-local hs-forward-sexp-function #'forward-sexp
644 "Function used to do a `forward-sexp'. 658 "Function used to reposition point to the end of the region to hide.
645It is called with 1 argument (like `forward-sexp'). 659For backward compatibility, the function is called with one argument,
660which can be ignored.
646 661
647Should change for Algol-ish modes. For single-character block 662The function is called in front of the beginning of the block (usually the
648delimiters such as `(' and `)' `hs-forward-sexp-function' would just be 663current value of `hs-block-start-regexp' in the buffer) and should
649`forward-sexp'. For other modes such as simula, a more specialized 664reposition point to the end of the block.")
650function is necessary.")
651 665
652(define-obsolete-variable-alias 666(define-obsolete-variable-alias
653 'hs-adjust-block-beginning 667 'hs-adjust-block-beginning
@@ -655,22 +669,23 @@ function is necessary.")
655 669
656(defvar-local hs-adjust-block-beginning-function nil 670(defvar-local hs-adjust-block-beginning-function nil
657 "Function used to tweak the block beginning. 671 "Function used to tweak the block beginning.
658It should return the position from where we should start hiding, as 672It is called at the beginning of the block (usually the current value of
659opposed to hiding it from the position returned when searching for 673`hs-block-start-regexp' in the buffer) and should return the start
660`hs-block-start-regexp'. 674position of the region in the buffer that will be hidden.
661 675
662It is called with a single argument ARG which is the position in 676It is called with a single argument ARG which is the position in
663buffer after the block beginning.") 677buffer after the block beginning.")
664 678
665(defvar-local hs-adjust-block-end-function nil 679(defvar-local hs-adjust-block-end-function nil
666 "Function used to tweak the block end. 680 "Function used to tweak the block end.
681It is called at the end of the block with one argument, the start
682position of the region in the buffer that will be hidden. It should
683return either the last position to hide or nil. If it returns nil,
684Hideshow will guess the end position.
685
667This is useful to ensure some characters such as parenthesis or curly 686This is useful to ensure some characters such as parenthesis or curly
668braces get properly hidden in modes without parenthesis pairs 687braces get properly hidden in modes without parenthesis pairs
669delimiters (such as python). 688delimiters (such as python).")
670
671It is called with one argument, which is the start position where the
672overlay will be created, and should return either the last position to
673hide or nil. If it returns nil, hideshow will guess the end position.")
674 689
675(define-obsolete-variable-alias 690(define-obsolete-variable-alias
676 'hs-find-block-beginning-func 691 'hs-find-block-beginning-func
@@ -679,13 +694,9 @@ hide or nil. If it returns nil, hideshow will guess the end position.")
679 694
680(defvar-local hs-find-block-beginning-function 695(defvar-local hs-find-block-beginning-function
681 #'hs-find-block-beg-fn--default 696 #'hs-find-block-beg-fn--default
682 "Function used to do `hs-find-block-beginning'. 697 "Function used to reposition point at the beginning of current block.
683It should reposition point at the beginning of the current block 698If it finds the block beginning, it should reposition point there and
684and return point, or nil if original point was not in a block. 699return non-nil, otherwise it should return nil.")
685
686Specifying this function is necessary for languages such as
687Python, where regexp search and `syntax-ppss' check is not enough
688to find the beginning of the current block.")
689 700
690(define-obsolete-variable-alias 701(define-obsolete-variable-alias
691 'hs-find-next-block-func 702 'hs-find-next-block-func
@@ -694,20 +705,20 @@ to find the beginning of the current block.")
694 705
695(defvar-local hs-find-next-block-function 706(defvar-local hs-find-next-block-function
696 #'hs-find-next-block-fn--default 707 #'hs-find-next-block-fn--default
697 "Function used to do `hs-find-next-block'. 708 "Function to find the start of the next block.
698It should reposition point at next block start. 709It should reposition point at next block start.
699 710
700It is called with three arguments REGEXP, BOUND, and COMMENTS. REGEXP 711It is called with three arguments REGEXP, BOUND, and COMMENTS.
701is a regexp representing block start. When block start is found, 712
702`match-data' should be set using REGEXP. BOUND is a buffer position 713REGEXP is a regexp representing block start. When block start is found,
703that limits the search. When COMMENTS is non-nil, REGEXP matches not 714should set the match data according to the beginning position of the
704only beginning of a block but also beginning of a comment. In this 715matched REGEXP or block start position.
705case, the function should find nearest block or comment and return
706non-nil.
707 716
708Specifying this function is necessary for languages such as Python, 717BOUND is a buffer position that limits the search.
709where regexp search is not enough to find the beginning of the next 718
710block.") 719When COMMENTS is non-nil, REGEXP matches not only beginning of a block
720but also beginning of a comment. In this case, the function should find
721the nearest block or comment and return non-nil.")
711 722
712(define-obsolete-variable-alias 723(define-obsolete-variable-alias
713 'hs-looking-at-block-start-p-func 724 'hs-looking-at-block-start-p-func
@@ -716,16 +727,13 @@ block.")
716 727
717(defvar-local hs-looking-at-block-start-predicate 728(defvar-local hs-looking-at-block-start-predicate
718 #'hs-looking-at-block-start-p--default 729 #'hs-looking-at-block-start-p--default
719 "Function used to do `hs-looking-at-block-start-p'. 730 "Function used to check if point is at the block start.
720It should return non-nil if the point is at the block start and set 731It should return non-nil if point is at the block start and modify the
721match data with the beginning and end of that position. 732match data to the block beginning start and end positions (specifically,
722 733for `match-end').")
723Specifying this function is necessary for languages such as
724Python, where `looking-at' and `syntax-ppss' check is not enough
725to check if the point is at the block start.")
726 734
727(defvar-local hs-inside-comment-predicate #'hs-inside-comment-p--default 735(defvar-local hs-inside-comment-predicate #'hs-inside-comment-p--default
728 "Function used to check if point is inside a comment. 736 "Function used to get comment positions.
729If point is inside a comment, the function should return a list 737If point is inside a comment, the function should return a list
730containing the buffer position of the start and the end of the 738containing the buffer position of the start and the end of the
731comment, otherwise it should return nil.") 739comment, otherwise it should return nil.")
@@ -802,21 +810,24 @@ point.
802 810
803If either ADJUST-BEG or ADJUST-END are non-nil, adjust block positions 811If either ADJUST-BEG or ADJUST-END are non-nil, adjust block positions
804according to `hs-adjust-block-beginning', `hs-adjust-block-end-function' 812according to `hs-adjust-block-beginning', `hs-adjust-block-end-function'
805and `hs-block-end-regexp'." 813and `hs-block-end-regexp'.
814
815This is for code block positions only, for comments use
816`hs-inside-comment-predicate'."
806 ;; `catch' is used here if the search fails due unbalanced parentheses 817 ;; `catch' is used here if the search fails due unbalanced parentheses
807 ;; or any other unknown error caused in `hs-forward-sexp-function'. 818 ;; or any other unknown error caused in `hs-forward-sexp-function'.
808 (catch 'hs--block-exit 819 (catch 'hs--block-exit
809 (save-match-data 820 (save-match-data
810 (save-excursion 821 (save-excursion
811 (when (funcall hs-looking-at-block-start-predicate) 822 (when (funcall hs-looking-at-block-start-predicate)
812 (let ((beg (match-end 0)) end) 823 (let* ((beg (match-end 0)) end)
813 ;; `beg' is the point at the end of the block 824 ;; `beg' is the point at the block beginning, which may need
814 ;; beginning, which may need to be adjusted 825 ;; to be adjusted
815 (when adjust-beg 826 (when adjust-beg
827 (setq beg (pos-eol))
816 (save-excursion 828 (save-excursion
817 (when hs-adjust-block-beginning-function 829 (when hs-adjust-block-beginning-function
818 (goto-char (funcall hs-adjust-block-beginning-function beg))) 830 (goto-char (funcall hs-adjust-block-beginning-function beg)))))
819 (setq beg (pos-eol))))
820 831
821 (goto-char (match-beginning hs-block-start-mdata-select)) 832 (goto-char (match-beginning hs-block-start-mdata-select))
822 (condition-case _ 833 (condition-case _
@@ -1146,7 +1157,7 @@ property of an overlay."
1146 (overlay-put ov 'invisible (and hide-p 'hs))) 1157 (overlay-put ov 'invisible (and hide-p 'hs)))
1147 1158
1148(defun hs-looking-at-block-start-p--default () 1159(defun hs-looking-at-block-start-p--default ()
1149 "Return non-nil if the point is at the block start." 1160 "Return non-nil if point is at the block start."
1150 (and (looking-at hs-block-start-regexp) 1161 (and (looking-at hs-block-start-regexp)
1151 (save-match-data (not (nth 8 (syntax-ppss)))))) 1162 (save-match-data (not (nth 8 (syntax-ppss))))))
1152 1163
@@ -1262,7 +1273,7 @@ Return point, or nil if original point was not in a block."
1262 ;; look backward for the start of a block that contains the cursor 1273 ;; look backward for the start of a block that contains the cursor
1263 (save-excursion 1274 (save-excursion
1264 (while (and (re-search-backward hs-block-start-regexp nil t) 1275 (while (and (re-search-backward hs-block-start-regexp nil t)
1265 (goto-char (match-beginning hs-block-start-mdata-select)) 1276 (goto-char (match-beginning 0))
1266 ;; go again if in a comment or a string 1277 ;; go again if in a comment or a string
1267 (or (save-match-data (nth 8 (syntax-ppss))) 1278 (or (save-match-data (nth 8 (syntax-ppss)))
1268 (not (setq done (and (<= here (cadr (hs-block-positions))) 1279 (not (setq done (and (<= here (cadr (hs-block-positions)))
diff --git a/lisp/treesit.el b/lisp/treesit.el
index ee19606c1b2..3feaa51c0a6 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -4287,11 +4287,9 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
4287 "Tree-sitter implementation of `hs-find-block-beginning-function'." 4287 "Tree-sitter implementation of `hs-find-block-beginning-function'."
4288 (let* ((pred (bound-and-true-p hs-treesit-things)) 4288 (let* ((pred (bound-and-true-p hs-treesit-things))
4289 (thing (treesit-thing-at (point) pred)) 4289 (thing (treesit-thing-at (point) pred))
4290 (beg (when thing (treesit-node-start thing))) 4290 (beg (when thing (treesit-node-start thing))))
4291 (end (when beg (min (1+ beg) (point-max)))))
4292 (when thing 4291 (when thing
4293 (goto-char beg) 4292 (goto-char beg)
4294 (set-match-data (list beg end))
4295 t))) 4293 t)))
4296 4294
4297(defun treesit-hs-find-next-block (_regexp maxp comments) 4295(defun treesit-hs-find-next-block (_regexp maxp comments)