diff options
| author | Stefan Monnier | 2019-05-19 09:36:22 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2019-05-19 09:36:42 -0400 |
| commit | d0e9113de9783094b4da7f6aeee131194653c325 (patch) | |
| tree | df9758c8188b38068527e3c820172effb1c55721 | |
| parent | 9199643ab2c38cd9e7f81ccff4485463f376e19d (diff) | |
| download | emacs-d0e9113de9783094b4da7f6aeee131194653c325.tar.gz emacs-d0e9113de9783094b4da7f6aeee131194653c325.zip | |
* lisp/progmodes/hideshow.el: Simplify mouse binding; Use lexical-binding
(hs-set-up-overlay, hs-adjust-block-beginning): Use non-nil default for
function variables, so `add-function` can be used on them.
(hs-toggle-hiding): Make it work for mouse bindings as well.
(hs-minor-mode-map): Use it for the mouse binding.
(hs-grok-mode-type): Use bound-and-true-p.
(hs-life-goes-on): Use `declare` for the debug spec.
(hs-mouse-toggle-hiding): Make it an obsolete alias.
| -rw-r--r-- | lisp/progmodes/hideshow.el | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el index dcce2a6e2a9..1d62bb58750 100644 --- a/lisp/progmodes/hideshow.el +++ b/lisp/progmodes/hideshow.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks | 1 | ;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks -*- lexical-binding:t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1994-2019 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1994-2019 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -37,7 +37,7 @@ | |||
| 37 | ;; hs-show-all C-c @ C-M-s | 37 | ;; hs-show-all C-c @ C-M-s |
| 38 | ;; hs-hide-level C-c @ C-l | 38 | ;; hs-hide-level C-c @ C-l |
| 39 | ;; hs-toggle-hiding C-c @ C-c | 39 | ;; hs-toggle-hiding C-c @ C-c |
| 40 | ;; hs-mouse-toggle-hiding [(shift mouse-2)] | 40 | ;; hs-toggle-hiding [(shift mouse-2)] |
| 41 | ;; hs-hide-initial-comment-block | 41 | ;; hs-hide-initial-comment-block |
| 42 | ;; | 42 | ;; |
| 43 | ;; Blocks are defined per mode. In c-mode, c++-mode and java-mode, they | 43 | ;; Blocks are defined per mode. In c-mode, c++-mode and java-mode, they |
| @@ -55,8 +55,7 @@ | |||
| 55 | ;; Then, add the following to your init file: | 55 | ;; Then, add the following to your init file: |
| 56 | ;; | 56 | ;; |
| 57 | ;; (load-library "hideshow") | 57 | ;; (load-library "hideshow") |
| 58 | ;; (add-hook 'X-mode-hook ; other modes similarly | 58 | ;; (add-hook 'X-mode-hook #'hs-minor-mode) ; other modes similarly |
| 59 | ;; (lambda () (hs-minor-mode 1))) | ||
| 60 | ;; | 59 | ;; |
| 61 | ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle | 60 | ;; where X = {emacs-lisp,c,c++,perl,...}. You can also manually toggle |
| 62 | ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is | 61 | ;; hideshow minor mode by typing `M-x hs-minor-mode'. After hideshow is |
| @@ -181,8 +180,8 @@ | |||
| 181 | ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the | 180 | ;; (5) Hideshow interacts badly with Ediff and `vc-diff'. At the moment, the |
| 182 | ;; suggested workaround is to turn off hideshow entirely, for example: | 181 | ;; suggested workaround is to turn off hideshow entirely, for example: |
| 183 | ;; | 182 | ;; |
| 184 | ;; (add-hook 'ediff-prepare-buffer-hook 'turn-off-hideshow) | 183 | ;; (add-hook 'ediff-prepare-buffer-hook #'turn-off-hideshow) |
| 185 | ;; (add-hook 'vc-before-checkin-hook 'turn-off-hideshow) | 184 | ;; (add-hook 'vc-before-checkin-hook #'turn-off-hideshow) |
| 186 | ;; | 185 | ;; |
| 187 | ;; In the case of `vc-diff', here is a less invasive workaround: | 186 | ;; In the case of `vc-diff', here is a less invasive workaround: |
| 188 | ;; | 187 | ;; |
| @@ -317,7 +316,7 @@ a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.") | |||
| 317 | These commands include the toggling commands (when the result is to show | 316 | These commands include the toggling commands (when the result is to show |
| 318 | a block), `hs-show-all' and `hs-show-block'.") | 317 | a block), `hs-show-all' and `hs-show-block'.") |
| 319 | 318 | ||
| 320 | (defvar hs-set-up-overlay nil | 319 | (defvar hs-set-up-overlay #'ignore |
| 321 | "Function called with one arg, OV, a newly initialized overlay. | 320 | "Function called with one arg, OV, a newly initialized overlay. |
| 322 | Hideshow puts a unique overlay on each range of text to be hidden | 321 | Hideshow puts a unique overlay on each range of text to be hidden |
| 323 | in the buffer. Here is a simple example of how to use this variable: | 322 | in the buffer. Here is a simple example of how to use this variable: |
| @@ -329,7 +328,7 @@ in the buffer. Here is a simple example of how to use this variable: | |||
| 329 | (count-lines (overlay-start ov) | 328 | (count-lines (overlay-start ov) |
| 330 | (overlay-end ov)))))) | 329 | (overlay-end ov)))))) |
| 331 | 330 | ||
| 332 | (setq hs-set-up-overlay \\='display-code-line-counts) | 331 | (setq hs-set-up-overlay #\\='display-code-line-counts) |
| 333 | 332 | ||
| 334 | This example shows how to get information from the overlay as well | 333 | This example shows how to get information from the overlay as well |
| 335 | as how to set its `display' property. See `hs-make-overlay' and | 334 | as how to set its `display' property. See `hs-make-overlay' and |
| @@ -355,7 +354,7 @@ Use the command `hs-minor-mode' to toggle or set this variable.") | |||
| 355 | (define-key map "\C-c@\C-t" 'hs-hide-all) | 354 | (define-key map "\C-c@\C-t" 'hs-hide-all) |
| 356 | (define-key map "\C-c@\C-d" 'hs-hide-block) | 355 | (define-key map "\C-c@\C-d" 'hs-hide-block) |
| 357 | (define-key map "\C-c@\C-e" 'hs-toggle-hiding) | 356 | (define-key map "\C-c@\C-e" 'hs-toggle-hiding) |
| 358 | (define-key map [(shift mouse-2)] 'hs-mouse-toggle-hiding) | 357 | (define-key map [(shift mouse-2)] 'hs-toggle-hiding) |
| 359 | map) | 358 | map) |
| 360 | "Keymap for hideshow minor mode.") | 359 | "Keymap for hideshow minor mode.") |
| 361 | 360 | ||
| @@ -410,7 +409,7 @@ element (using `match-beginning') before calling `hs-forward-sexp-func'.") | |||
| 410 | (defvar-local hs-block-end-regexp nil | 409 | (defvar-local hs-block-end-regexp nil |
| 411 | "Regexp for end of block.") | 410 | "Regexp for end of block.") |
| 412 | 411 | ||
| 413 | (defvar-local hs-forward-sexp-func 'forward-sexp | 412 | (defvar-local hs-forward-sexp-func #'forward-sexp |
| 414 | "Function used to do a `forward-sexp'. | 413 | "Function used to do a `forward-sexp'. |
| 415 | Should change for Algol-ish modes. For single-character block | 414 | Should change for Algol-ish modes. For single-character block |
| 416 | delimiters -- ie, the syntax table regexp for the character is | 415 | delimiters -- ie, the syntax table regexp for the character is |
| @@ -418,7 +417,7 @@ either `(' or `)' -- `hs-forward-sexp-func' would just be | |||
| 418 | `forward-sexp'. For other modes such as simula, a more specialized | 417 | `forward-sexp'. For other modes such as simula, a more specialized |
| 419 | function is necessary.") | 418 | function is necessary.") |
| 420 | 419 | ||
| 421 | (defvar-local hs-adjust-block-beginning nil | 420 | (defvar-local hs-adjust-block-beginning #'identity |
| 422 | "Function used to tweak the block beginning. | 421 | "Function used to tweak the block beginning. |
| 423 | The block is hidden from the position returned by this function, | 422 | The block is hidden from the position returned by this function, |
| 424 | as opposed to hiding it from the position returned when searching | 423 | as opposed to hiding it from the position returned when searching |
| @@ -575,10 +574,8 @@ and then further adjusted to be at the end of the line." | |||
| 575 | ;; `p' is the point at the end of the block beginning, which | 574 | ;; `p' is the point at the end of the block beginning, which |
| 576 | ;; may need to be adjusted | 575 | ;; may need to be adjusted |
| 577 | (save-excursion | 576 | (save-excursion |
| 578 | (if hs-adjust-block-beginning | 577 | (goto-char (funcall (or hs-adjust-block-beginning #'identity) |
| 579 | (goto-char (funcall hs-adjust-block-beginning | 578 | header-end)) |
| 580 | header-end)) | ||
| 581 | (goto-char header-end)) | ||
| 582 | (setq p (line-end-position))) | 579 | (setq p (line-end-position))) |
| 583 | ;; `q' is the point at the end of the block | 580 | ;; `q' is the point at the end of the block |
| 584 | (hs-forward-sexp mdata 1) | 581 | (hs-forward-sexp mdata 1) |
| @@ -657,9 +654,8 @@ If `hs-special-modes-alist' has information associated with the | |||
| 657 | current buffer's major mode, use that. | 654 | current buffer's major mode, use that. |
| 658 | Otherwise, guess start, end and `comment-start' regexps; `forward-sexp' | 655 | Otherwise, guess start, end and `comment-start' regexps; `forward-sexp' |
| 659 | function; and adjust-block-beginning function." | 656 | function; and adjust-block-beginning function." |
| 660 | (if (and (boundp 'comment-start) | 657 | (if (and (bound-and-true-p comment-start) |
| 661 | (boundp 'comment-end) | 658 | (bound-and-true-p comment-end)) |
| 662 | comment-start comment-end) | ||
| 663 | (let* ((lookup (assoc major-mode hs-special-modes-alist)) | 659 | (let* ((lookup (assoc major-mode hs-special-modes-alist)) |
| 664 | (start-elem (or (nth 1 lookup) "\\s("))) | 660 | (start-elem (or (nth 1 lookup) "\\s("))) |
| 665 | (if (listp start-elem) | 661 | (if (listp start-elem) |
| @@ -677,8 +673,8 @@ function; and adjust-block-beginning function." | |||
| 677 | (substring c-start-regexp | 673 | (substring c-start-regexp |
| 678 | 0 (1- (match-end 0))) | 674 | 0 (1- (match-end 0))) |
| 679 | c-start-regexp))) | 675 | c-start-regexp))) |
| 680 | hs-forward-sexp-func (or (nth 4 lookup) 'forward-sexp) | 676 | hs-forward-sexp-func (or (nth 4 lookup) #'forward-sexp) |
| 681 | hs-adjust-block-beginning (nth 5 lookup))) | 677 | hs-adjust-block-beginning (or (nth 5 lookup) #'identity))) |
| 682 | (setq hs-minor-mode nil) | 678 | (setq hs-minor-mode nil) |
| 683 | (error "%s Mode doesn't support Hideshow Minor Mode" | 679 | (error "%s Mode doesn't support Hideshow Minor Mode" |
| 684 | (format-mode-line mode-name)))) | 680 | (format-mode-line mode-name)))) |
| @@ -729,13 +725,12 @@ Return point, or nil if original point was not in a block." | |||
| 729 | "Evaluate BODY forms if variable `hs-minor-mode' is non-nil. | 725 | "Evaluate BODY forms if variable `hs-minor-mode' is non-nil. |
| 730 | In the dynamic context of this macro, `inhibit-point-motion-hooks' | 726 | In the dynamic context of this macro, `inhibit-point-motion-hooks' |
| 731 | and `case-fold-search' are both t." | 727 | and `case-fold-search' are both t." |
| 728 | (declare (debug t)) | ||
| 732 | `(when hs-minor-mode | 729 | `(when hs-minor-mode |
| 733 | (let ((inhibit-point-motion-hooks t) | 730 | (let ((inhibit-point-motion-hooks t) |
| 734 | (case-fold-search t)) | 731 | (case-fold-search t)) |
| 735 | ,@body))) | 732 | ,@body))) |
| 736 | 733 | ||
| 737 | (put 'hs-life-goes-on 'edebug-form-spec '(&rest form)) | ||
| 738 | |||
| 739 | (defun hs-overlay-at (position) | 734 | (defun hs-overlay-at (position) |
| 740 | "Return hideshow overlay at POSITION, or nil if none to be found." | 735 | "Return hideshow overlay at POSITION, or nil if none to be found." |
| 741 | (let ((overlays (overlays-at position)) | 736 | (let ((overlays (overlays-at position)) |
| @@ -895,24 +890,18 @@ The hook `hs-hide-hook' is run; see `run-hooks'." | |||
| 895 | (message "Hiding blocks ... done")) | 890 | (message "Hiding blocks ... done")) |
| 896 | (run-hooks 'hs-hide-hook))) | 891 | (run-hooks 'hs-hide-hook))) |
| 897 | 892 | ||
| 898 | (defun hs-toggle-hiding () | 893 | (defun hs-toggle-hiding (&optional e) |
| 899 | "Toggle hiding/showing of a block. | 894 | "Toggle hiding/showing of a block. |
| 900 | See `hs-hide-block' and `hs-show-block'." | 895 | See `hs-hide-block' and `hs-show-block'. |
| 896 | Argument E should be the event that triggered this action." | ||
| 901 | (interactive) | 897 | (interactive) |
| 902 | (hs-life-goes-on | 898 | (hs-life-goes-on |
| 899 | (posn-set-point (event-end e)) | ||
| 903 | (if (hs-already-hidden-p) | 900 | (if (hs-already-hidden-p) |
| 904 | (hs-show-block) | 901 | (hs-show-block) |
| 905 | (hs-hide-block)))) | 902 | (hs-hide-block)))) |
| 906 | 903 | ||
| 907 | (defun hs-mouse-toggle-hiding (e) | 904 | (define-obsolete-function-alias 'hs-mouse-toggle-hiding #'hs-toggle-hiding "27.1") |
| 908 | "Toggle hiding/showing of a block. | ||
| 909 | This command should be bound to a mouse key. | ||
| 910 | Argument E is a mouse event used by `mouse-set-point'. | ||
| 911 | See `hs-hide-block' and `hs-show-block'." | ||
| 912 | (interactive "@e") | ||
| 913 | (hs-life-goes-on | ||
| 914 | (mouse-set-point e) | ||
| 915 | (hs-toggle-hiding))) | ||
| 916 | 905 | ||
| 917 | (defun hs-hide-initial-comment-block () | 906 | (defun hs-hide-initial-comment-block () |
| 918 | "Hide the first block of comments in a file. | 907 | "Hide the first block of comments in a file. |
| @@ -939,7 +928,7 @@ The value (hs . t) is added to `buffer-invisibility-spec'. | |||
| 939 | 928 | ||
| 940 | The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block', | 929 | The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block', |
| 941 | `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also | 930 | `hs-show-block', `hs-hide-level' and `hs-toggle-hiding'. There is also |
| 942 | `hs-hide-initial-comment-block' and `hs-mouse-toggle-hiding'. | 931 | `hs-hide-initial-comment-block'. |
| 943 | 932 | ||
| 944 | Turning hideshow minor mode off reverts the menu bar and the | 933 | Turning hideshow minor mode off reverts the menu bar and the |
| 945 | variables to default values and disables the hideshow commands. | 934 | variables to default values and disables the hideshow commands. |
| @@ -957,7 +946,7 @@ Key bindings: | |||
| 957 | (hs-grok-mode-type) | 946 | (hs-grok-mode-type) |
| 958 | ;; Turn off this mode if we change major modes. | 947 | ;; Turn off this mode if we change major modes. |
| 959 | (add-hook 'change-major-mode-hook | 948 | (add-hook 'change-major-mode-hook |
| 960 | 'turn-off-hideshow | 949 | #'turn-off-hideshow |
| 961 | nil t) | 950 | nil t) |
| 962 | (easy-menu-add hs-minor-mode-menu) | 951 | (easy-menu-add hs-minor-mode-menu) |
| 963 | (set (make-local-variable 'line-move-ignore-invisible) t) | 952 | (set (make-local-variable 'line-move-ignore-invisible) t) |