-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhide-region.el
168 lines (154 loc) · 5.65 KB
/
hide-region.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;;; hide-region.el --- hide regions of text using overlays
;;
;; Copyright (C) 2001, 2005 Mathias Dahl
;;
;; Version: 1.0.2
;; Keywords: hide, region
;; Author: Mathias Dahl <[email protected]>
;; Maintainer: Mathias Dahl
;; URL: http://mathias.dahl.net/pgm/emacs/elisp/hide-region.el
;; Last-Updated: Thu Sun Jul 28 16:10:15 2011 (+0200)
;; By: Deniz Dogan <[email protected]>
;;
;; This file is not part of GNU Emacs.
;;
;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;;; Commentary:
;;
;; The function `hide-region-hide' hides the region. You can hide many
;; different regions and they will be "marked" by two configurable
;; strings (so that you know where the hidden text is).
;;
;; The hidden regions is pushed on a kind of hide-region \"ring".
;;
;; The function `hide-region-unhide' "unhides" one region, starting
;; with the last one you hid.
;;
;; The best is to try it out. Test on the following:
;;
;; Test region 1
;; Test region 2
;; Test region 3
;;
;; It can be useful to bind the commands to mnemonic keys, e.g.:
;; (global-set-key (kbd "C-c h r") 'hide-region-hide)
;; (global-set-key (kbd "C-c h u") 'hide-region-unhide)
;;
;;; Version history
;;
;; Version 1.0.2
;;
;; * Added defface for text properties.
;;
;; * Minor tweaks.
;;
;;
;; Version 1.0.1
;;
;; * Seems that the getting-stuck problem have disappeared since Emacs
;; 21.3 was released, so no need anymore for the extra movement
;; commands.
;;
;; * Added the intangible property to the overlays because that seemed
;; to remove a minor getting-stuck problem (the overlay "ate" one
;; keystroke) when navigating around an overlay. Adding the intangible
;; property makes it impossible to navigate into the overlay.
;;
;; * Added custom option to propertize the overlay markers for greater
;; visibility.
;;
;; * Minor code cleanup
;;
;;
;;; Bugs
;;
;; Probably many, but none that I know of. Comments and suggestions
;; are welcome!
;;; Code:
(defgroup hide-region nil
"Functions to hide region using an overlay with the invisible
property. The text is not affected."
:prefix "hide-region-"
:group 'convenience)
(defcustom hide-region-before-string "@["
"String to mark the beginning of an invisible region. This string is
not really placed in the text, it is just shown in the overlay"
:type 'string
:group 'hide-region)
(defcustom hide-region-after-string "]@"
"String to mark the beginning of an invisible region. This string is
not really placed in the text, it is just shown in the overlay"
:type 'string
:group 'hide-region)
(defcustom hide-region-propertize-markers t
"If non-nil, add text properties to the region markers."
:type 'boolean
:group 'hide-region)
(defface hide-region-before-string-face
'((t (:inherit region)))
"Face for the before string.")
(defface hide-region-after-string-face
'((t (:inherit region)))
"Face for the after string.")
(defvar hide-region-overlays nil
"Variable to store the regions we put an overlay on.")
(defun hide-region-hide ()
"Hides a region by making an invisible overlay over it and save the
overlay on the hide-region-overlays \"ring\""
(interactive)
(let ((new-overlay (make-overlay (mark) (point))))
(push new-overlay hide-region-overlays)
(overlay-put new-overlay 'invisible t)
(overlay-put new-overlay 'intangible t)
(overlay-put new-overlay 'before-string
(if hide-region-propertize-markers
(propertize hide-region-before-string
'font-lock-face 'hide-region-before-string-face)
hide-region-before-string))
(overlay-put new-overlay 'after-string
(if hide-region-propertize-markers
(propertize hide-region-after-string
'font-lock-face 'hide-region-after-string-face)
hide-region-after-string))))
(defun hide-region-unhide-all ()
"Unhide all hidden regions."
(interactive)
(dolist (x hide-region-overlays)
(delete-overlay x)))
(defun hide-region-unhide ()
"Unhide a region at a time, starting with the last one hidden and
deleting the overlay from the hide-region-overlays \"ring\"."
(interactive)
(when (car hide-region-overlays)
(delete-overlay (car hide-region-overlays))
(setq hide-region-overlays (cdr hide-region-overlays))))
(defun hide-region-unhide-at-point ()
"Unhide the top-level region at point. Do not unhide hidden
sub-regions. Deletes the overlay from the hide-region-overlays \"ring\"."
(interactive)
(let* ((child-p (lambda (c p) (and (>= (overlay-start c) (overlay-start p))
(<= (overlay-end c) (overlay-end p)))))
(overlays (remove-duplicates (sort (overlays-at (- (point) 1)) child-p)
:test child-p)))
(mapc (lambda (o)
(setq hide-region-overlays
(delete-if (lambda (x) (equal x o))
hide-region-overlays))
(delete-overlay o))
overlays)))
(provide 'hide-region)
;;; hide-region.el ends here