diff options
| -rw-r--r-- | etc/NEWS | 2 | ||||
| -rw-r--r-- | lisp/progmodes/hideshow.el | 131 | ||||
| -rw-r--r-- | lisp/treesit.el | 4 |
3 files changed, 73 insertions, 64 deletions
| @@ -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. |
| 1245 | This is controlled by 'hs-hide-comments-when-hiding-all'. | 1245 | This 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. |
| 622 | The internal function `hs-forward-sexp' moves point to the beginning of this | 633 | This is used by `hs-block-positions' to move point to the beginning of |
| 623 | element (using `match-beginning') before calling `hs-forward-sexp-function'.") | 634 | this element (using `match-beginning') before calling |
| 635 | `hs-forward-sexp-function'. | ||
| 636 | |||
| 637 | This 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. |
| 627 | As a special case, the value can be also a function without arguments to | 641 | This is mostly used to determine if point is at the end of the block. |
| 628 | determine if point is looking at the end of the block, and return | 642 | |
| 629 | non-nil and set `match-data' to that block end positions.") | 643 | As 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 | ||
| 645 | function, it should return non-nil if point is at end of a block, and | ||
| 646 | set `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. |
| 633 | Differs from mode-specific comment regexps in that surrounding | 650 | If not bound, Hideshow will use current `comment-start' value without |
| 634 | whitespace is stripped. | ||
| 635 | |||
| 636 | If not bound, hideshow will use current `comment-start' value without | ||
| 637 | any trailing whitespace.") | 651 | any 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. |
| 645 | It is called with 1 argument (like `forward-sexp'). | 659 | For backward compatibility, the function is called with one argument, |
| 660 | which can be ignored. | ||
| 646 | 661 | ||
| 647 | Should change for Algol-ish modes. For single-character block | 662 | The function is called in front of the beginning of the block (usually the |
| 648 | delimiters such as `(' and `)' `hs-forward-sexp-function' would just be | 663 | current value of `hs-block-start-regexp' in the buffer) and should |
| 649 | `forward-sexp'. For other modes such as simula, a more specialized | 664 | reposition point to the end of the block.") |
| 650 | function 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. |
| 658 | It should return the position from where we should start hiding, as | 672 | It is called at the beginning of the block (usually the current value of |
| 659 | opposed 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'. | 674 | position of the region in the buffer that will be hidden. |
| 661 | 675 | ||
| 662 | It is called with a single argument ARG which is the position in | 676 | It is called with a single argument ARG which is the position in |
| 663 | buffer after the block beginning.") | 677 | buffer 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. |
| 681 | It is called at the end of the block with one argument, the start | ||
| 682 | position of the region in the buffer that will be hidden. It should | ||
| 683 | return either the last position to hide or nil. If it returns nil, | ||
| 684 | Hideshow will guess the end position. | ||
| 685 | |||
| 667 | This is useful to ensure some characters such as parenthesis or curly | 686 | This is useful to ensure some characters such as parenthesis or curly |
| 668 | braces get properly hidden in modes without parenthesis pairs | 687 | braces get properly hidden in modes without parenthesis pairs |
| 669 | delimiters (such as python). | 688 | delimiters (such as python).") |
| 670 | |||
| 671 | It is called with one argument, which is the start position where the | ||
| 672 | overlay will be created, and should return either the last position to | ||
| 673 | hide 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. |
| 683 | It should reposition point at the beginning of the current block | 698 | If it finds the block beginning, it should reposition point there and |
| 684 | and return point, or nil if original point was not in a block. | 699 | return non-nil, otherwise it should return nil.") |
| 685 | |||
| 686 | Specifying this function is necessary for languages such as | ||
| 687 | Python, where regexp search and `syntax-ppss' check is not enough | ||
| 688 | to 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. |
| 698 | It should reposition point at next block start. | 709 | It should reposition point at next block start. |
| 699 | 710 | ||
| 700 | It is called with three arguments REGEXP, BOUND, and COMMENTS. REGEXP | 711 | It is called with three arguments REGEXP, BOUND, and COMMENTS. |
| 701 | is a regexp representing block start. When block start is found, | 712 | |
| 702 | `match-data' should be set using REGEXP. BOUND is a buffer position | 713 | REGEXP is a regexp representing block start. When block start is found, |
| 703 | that limits the search. When COMMENTS is non-nil, REGEXP matches not | 714 | should set the match data according to the beginning position of the |
| 704 | only beginning of a block but also beginning of a comment. In this | 715 | matched REGEXP or block start position. |
| 705 | case, the function should find nearest block or comment and return | ||
| 706 | non-nil. | ||
| 707 | 716 | ||
| 708 | Specifying this function is necessary for languages such as Python, | 717 | BOUND is a buffer position that limits the search. |
| 709 | where regexp search is not enough to find the beginning of the next | 718 | |
| 710 | block.") | 719 | When COMMENTS is non-nil, REGEXP matches not only beginning of a block |
| 720 | but also beginning of a comment. In this case, the function should find | ||
| 721 | the 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. |
| 720 | It should return non-nil if the point is at the block start and set | 731 | It should return non-nil if point is at the block start and modify the |
| 721 | match data with the beginning and end of that position. | 732 | match data to the block beginning start and end positions (specifically, |
| 722 | 733 | for `match-end').") | |
| 723 | Specifying this function is necessary for languages such as | ||
| 724 | Python, where `looking-at' and `syntax-ppss' check is not enough | ||
| 725 | to 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. |
| 729 | If point is inside a comment, the function should return a list | 737 | If point is inside a comment, the function should return a list |
| 730 | containing the buffer position of the start and the end of the | 738 | containing the buffer position of the start and the end of the |
| 731 | comment, otherwise it should return nil.") | 739 | comment, otherwise it should return nil.") |
| @@ -802,21 +810,24 @@ point. | |||
| 802 | 810 | ||
| 803 | If either ADJUST-BEG or ADJUST-END are non-nil, adjust block positions | 811 | If either ADJUST-BEG or ADJUST-END are non-nil, adjust block positions |
| 804 | according to `hs-adjust-block-beginning', `hs-adjust-block-end-function' | 812 | according to `hs-adjust-block-beginning', `hs-adjust-block-end-function' |
| 805 | and `hs-block-end-regexp'." | 813 | and `hs-block-end-regexp'. |
| 814 | |||
| 815 | This 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) |