-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpel-hideshow.el
286 lines (244 loc) · 9.22 KB
/
pel-hideshow.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
;;; pel-hideshow.el --- PEL Hide/Show -*- lexical-binding: t -*-
;; Copyright (C) 2020, 2024 Pierre Rouleau
;; Author: Pierre Rouleau <prouleau001@gmail.com>
;; This file is part of the PEL package
;; This file is not part of GNU Emacs.
;; This program 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 3 of the License, or
;; (at your option) any later version.
;;
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Credits:
;; Several ideas came from the source of hideshow.el.
;; I wanted to use a single key to toggle hiding/showing all blocks
;; and that was already proposed by Joseph Eydelnant who has the idea
;; a little more than 19 years before (I wish I had been using Emacs then).
;; Unfortunately his code is not yet in hideshow.el and I don't want to
;; modify that code, so I implemented something similar here.
;;
;;; Commentary:
;;
;; This is just a simple collection of functions that complement the hideshow
;; functionality. Several of these functions activate the minor mode if it is
;; not active.
;;; Code:
(require 'hideshow)
(require 'pel--base) ; uses: pel-toggle, pel-dec, pel-inc
(defvar pel--hs-all-hidden nil
"Current state of hiding all blocks.
Value is t when hiding all blocks, nil otherwise.
LIMITATION:
- No attempt is made by pel-hideshow.el to ensure that
this variable is set to nil when function `hs-minor-mode' is called.
The impact is negligible: in the worst case the value becomes out of sync with
the real state and it will take 2 toggle commands to change the state of
hiding.")
(make-variable-buffer-local 'pel--hs-all-hidden)
(defvar pel--hs-hide-relative-block nil
"Number of blocks to hide below current block level.")
(make-variable-buffer-local 'pel--hs-hide-relative-block)
;;-pel-autoload
(defun pel-show-hide-state ()
"Display state of pel-hideshow in current buffer."
(interactive)
(message "All blocks: %s.%s"
(if pel--hs-all-hidden "hidden" "shown")
(if pel--hs-hide-relative-block
(format " Hide blocks %d level deeper than current."
pel--hs-hide-relative-block)
"")))
(defun pel--activate-hideshow-mode (&optional use-relative-block)
"Activate the Hide/Show minor mode if it is not already active."
(unless use-relative-block
(setq pel--hs-hide-relative-block nil))
(unless hs-minor-mode
(setq pel--hs-all-hidden nil)
(hs-minor-mode)))
;;-pel-autoload
(defun pel-toggle-hide-all ()
"Toggle hiding of all blocks.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(if (pel-toggle 'pel--hs-all-hidden)
(hs-hide-all)
(hs-show-all)))
;;-pel-autoload
(defun pel-toggle-hide-block ()
"Toggle hiding of current block.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-toggle-hiding))
;;-pel-autoload
(defun pel-hide-block (&optional end)
"Select a block and hide it.
With prefix arg, reposition to END.
Activate Hide/Show minor mode if not already active."
(interactive "P")
(pel--activate-hideshow-mode)
(hs-hide-block end))
;;-pel-autoload
(defun pel-show-block (&optional end)
"Select a block and show it.
With prefix arg, reposition to END.
Activate Hide/Show minor mode if not already active."
(interactive "P")
(pel--activate-hideshow-mode)
(hs-show-block end))
;;-pel-autoload
(defun pel-hide-all ()
"Hide all top level blocks, displaying only first and last lines.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-hide-all)
(setq pel--hs-all-hidden t))
;;-pel-autoload
(defun pel-show-all ()
"Show all blocks.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-show-all)
(setq pel--hs-all-hidden nil))
;;-pel-autoload
(defun pel-hide-level-1 ()
"Hide all blocks 1 level below the current block.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-hide-level 1))
;;-pel-autoload
(defun pel-hide-level-2 ()
"Hide all blocks 2 levels below the current block.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-hide-level 2))
;;-pel-autoload
(defun pel-hide-level-3 ()
"Hide all blocks 3 levels below the current block.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-hide-level 3))
;;-pel-autoload
(defun pel-hide-level-4 ()
"Hide all blocks 4 levels below the current block.
Activate Hide/Show minor mode if not already active."
(interactive)
(pel--activate-hideshow-mode)
(hs-hide-level 4))
;;-pel-autoload
(defun pel-hs-hide-block-below-inc ()
"Hide one more block level below this block."
(interactive)
(pel--activate-hideshow-mode :use-relative-block)
(unless pel--hs-hide-relative-block
(setq pel--hs-hide-relative-block 1))
(if (pel-inc 'pel--hs-hide-relative-block 10)
(progn
(hs-hide-level pel--hs-hide-relative-block)
(message "Hiding level %d below current level" pel--hs-hide-relative-block))
(message "Nested block limit of 10 reached")
(beep)))
;;-pel-autoload
(defun pel-hs-hide-block-below-dec ()
"Hide one less block level below this block."
(interactive)
(pel--activate-hideshow-mode :use-relative-block)
(if pel--hs-hide-relative-block
(if (pel-dec 'pel--hs-hide-relative-block 1 1)
(progn
(hs-hide-level pel--hs-hide-relative-block)
(message "Hiding level %d below current level" pel--hs-hide-relative-block))
(setq pel--hs-hide-relative-block nil))
(message "All blocks below current are shown")
(beep)))
;; -----------------------------------------------------------------------------
;; Column-based Selective display
;; ------------------------------
(defun pel--set-vline (column)
"Set the vertical line position when vline-mode is available."
(when (bound-and-true-p vline-mode)
(move-to-column (max 0 (- column 1)))))
;;-pel-autoload
(defun pel-selective-display-column-inc (n)
"Increment variable `selective-display' by N (defaults to 1).
This hides text indented by that many columns."
(interactive "p")
(let ((sd-value (or selective-display 1)))
(setq sd-value (+ sd-value (or n 1)))
(set-selective-display sd-value)
(pel--set-vline sd-value)))
;;-pel-autoload
(defun pel-selective-display-column-dec (n)
"Decrement variable `selective-display' by N (defaults to 1).
This hides text indented by that many columns."
(interactive "p")
(let ((sd-value selective-display))
(when sd-value
(setq sd-value (pel-dec sd-value (or n 1) 0)))
(set-selective-display sd-value)
(pel--set-vline sd-value)))
(defun pel--indent-size ()
"Return the indentation size used by the current buffer if any, 1 otherwise."
;; for bracket-type programming languages: use c-basic-offset
(if (boundp 'c-basic-offset)
c-basic-offset
1))
;;-pel-autoload
(defun pel-selective-display-indent-inc (n)
"Increment variable `selective-display' by N indentation levels.
N defaults to 1. If indentation level is not known, use N columns.
This hides text indented a the new value of variable `selective-display'."
(interactive "p")
(let ((indent-columns (pel--indent-size))
(sd-value (or selective-display 1)))
(setq sd-value (+ sd-value (* (or n 1) indent-columns)))
(set-selective-display sd-value)
(pel--set-vline sd-value)))
;;-pel-autoload
(defun pel-selective-display-indent-dec (n)
"Decrement variable `selective-display' by N indentation levels.
N defaults to 1. If indentation level is not known, use N columns.
This hides text indented a the new value of variable `selective-display'."
(interactive "p")
(let ((indent-columns (pel--indent-size))
(sd-value (or selective-display 0)))
(when sd-value
(setq sd-value (pel-dec sd-value (* (or n 1) indent-columns) 0))
(set-selective-display sd-value)
(pel--set-vline sd-value))))
;;-pel-autoload
(defun pel-selective-display-unhide ()
"Selective display un-hide column."
(interactive)
(set-selective-display nil))
;;-pel-autoload
(defun pel-selective-display-at-1 ()
"Selective display hide at column 1."
(interactive)
(set-selective-display 1))
;; ---------------------------------------------------------------------------
;; Hide indented lines only
;; ------------------------
;;-pel-autoload
(defun pel-toggle-hide-indent ()
"Toggle hiding lines more indented than current line."
(interactive)
(save-excursion
(back-to-indentation)
(set-selective-display
(if selective-display nil (+ 1 (current-column))))))
;; -----------------------------------------------------------------------------
(provide 'pel-hideshow)
;;; pel-hideshow.el ends here