aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2019-05-19 09:36:22 -0400
committerStefan Monnier2019-05-19 09:36:42 -0400
commitd0e9113de9783094b4da7f6aeee131194653c325 (patch)
treedf9758c8188b38068527e3c820172effb1c55721
parent9199643ab2c38cd9e7f81ccff4485463f376e19d (diff)
downloademacs-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.el59
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'.")
317These commands include the toggling commands (when the result is to show 316These commands include the toggling commands (when the result is to show
318a block), `hs-show-all' and `hs-show-block'.") 317a 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.
322Hideshow puts a unique overlay on each range of text to be hidden 321Hideshow puts a unique overlay on each range of text to be hidden
323in the buffer. Here is a simple example of how to use this variable: 322in 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
334This example shows how to get information from the overlay as well 333This example shows how to get information from the overlay as well
335as how to set its `display' property. See `hs-make-overlay' and 334as 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'.
415Should change for Algol-ish modes. For single-character block 414Should change for Algol-ish modes. For single-character block
416delimiters -- ie, the syntax table regexp for the character is 415delimiters -- 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
419function is necessary.") 418function 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.
423The block is hidden from the position returned by this function, 422The block is hidden from the position returned by this function,
424as opposed to hiding it from the position returned when searching 423as 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
657current buffer's major mode, use that. 654current buffer's major mode, use that.
658Otherwise, guess start, end and `comment-start' regexps; `forward-sexp' 655Otherwise, guess start, end and `comment-start' regexps; `forward-sexp'
659function; and adjust-block-beginning function." 656function; 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.
730In the dynamic context of this macro, `inhibit-point-motion-hooks' 726In the dynamic context of this macro, `inhibit-point-motion-hooks'
731and `case-fold-search' are both t." 727and `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.
900See `hs-hide-block' and `hs-show-block'." 895See `hs-hide-block' and `hs-show-block'.
896Argument 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.
909This command should be bound to a mouse key.
910Argument E is a mouse event used by `mouse-set-point'.
911See `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
940The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block', 929The 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
944Turning hideshow minor mode off reverts the menu bar and the 933Turning hideshow minor mode off reverts the menu bar and the
945variables to default values and disables the hideshow commands. 934variables 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)