-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfreex-hiert.el
More file actions
347 lines (288 loc) · 9.97 KB
/
freex-hiert.el
File metadata and controls
347 lines (288 loc) · 9.97 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
;;; freex-hiert.el --- miscellaneous functions for dealing
;;; with hierarchical indenting
;;
;; Copyright (C) 2007 Per B. Sederberg, Greg Detre
;;
;; Author: Per B. Sederberg, Greg Detre
;; Keywords: hypermedia
;; Date:
;;
;; This file is part of Emacs Freex. It is not part of GNU
;; Emacs.
;;
;; Emacs Freex 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.
;;
;; Emacs Freex 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 Emacs Freex; see the file COPYING. If
;; not, write to the Free Software Foundation, Inc., 51
;; Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;;
;;; Contributors:
;;; Code:
;; the idea is that you press M-. (i.e. alt->) to indent a
;; paragraph, and M-, (i.e. alt-<) to outdent the
;; paragraph. the paragraph does not have to be selected in
;; order for this to work
;;
;; if you want to in/outdent multiple paragraphs at once,
;; just select them. you don't have to select the entire
;; paragraph - just make sure the start and end regions
;; include at least some of the paragraphs you're interested
;; in
;;
;; everything here is designed for indenting 2 spaces. i
;; haven't attempted to generalize things for n spaces.
;; just for freex-hiert-is-mark-active
(require 'version-info)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; from http://www.dotemacs.de/dotfiles/SarirKhamsi.emacs.html
;;
;; i'm not convinced these are doing anything, but they were
;; in my .emacs file...
(setq tab-width 2)
(setq tab-interval 2)
(setq adaptive-fill-mode t)
(defun freex-hiert-in2 ()
(interactive)
(freex-hiert-indent-rigidly 2))
(defun freex-hiert-out2 ()
(interactive)
(let ((dent-size 2))
(if (or
(> (current-column) 0)
(not (freex-hiert-on-blank-line)))
(freex-hiert-indent-rigidly (* dent-size -1)))))
(defun freex-hiert-mark-paragraph ()
(interactive)
(freex-hiert-mark-multiple-paragraphs (point) (point)))
;; this works, but the recentering after narrowing gets
;; screwy if you change the font size
(defun freex-hiert-mark-multiple-paragraphs (start end)
"According to the help for save-restriction, you're supposed to
use the save-excursion outside the save-restriction, which might
explain why i get weird recentering issues when i call this in a
frame with small font on a mac..."
(interactive "r")
(save-restriction
(narrow-to-region
(save-excursion
(goto-char start)
(freex-hiert-backward-paragraph))
(save-excursion
(goto-char end)
(freex-hiert-forward-paragraph)))
(mark-whole-buffer)))
;; (defun freex-hiert-mark-multiple-paragraphs (start end)
;; (interactive "r")
;; (let ((beg-par
;; (save-excursion
;; (goto-char start)
;; (freex-hiert-backward-paragraph)))
;; (end-par
;; (save-excursion
;; (goto-char end)
;; (freex-hiert-forward-paragraph))))
;; (push-mark beg-par)
;; (push-mark end-par)))
; based on http://xahlee.org/emacs/examples.html
(defun freex-hiert-indent-rigidly-paragraphs-ncols(start end ncols)
(freex-hiert-mark-multiple-paragraphs start end)
(save-restriction
(narrow-to-region (point) (mark))
(indent-rigidly (point-min) (point-max) ncols)))
(defun freex-hiert-indent-rigidly (ncols)
"If the mark is inactive, run indent-rigidly on the (whole
of the) current paragraph. If the mark is active, then run
it on (the entirety of) all the paragraphs in the region. In
other words, make sure that you indent entire paragraphs at
a time."
(if (and (freex-hiert-on-blank-line) (not (freex-hiert-is-mark-active)))
(if (= ncols 2)
(insert " ")
(delete-char -2))
(progn
(save-excursion
(if (freex-hiert-is-mark-active)
(progn
(if (< (mark) (point))
(exchange-point-and-mark))
(freex-hiert-indent-rigidly-paragraphs-ncols
(point) (mark) ncols))
(progn
(freex-hiert-mark-paragraph)
(indent-rigidly (point) (mark) ncols))))
; if the point is at the beginning of the line, don't let
; it stay there - move it forward to the beginning of the
; paragraph
(if (= 0 (current-column))
(skip-chars-forward " \t"))
(if (and (freex-hiert-at-end-of-paragraph)
(< (skip-chars-backward " ") 0))
(progn
(freex-hiert-fill-paragraph)
(insert " "))
(progn
(freex-hiert-fill-paragraph))))))
;; ;; worked fairly well for a while, but was frustrating
;; (defun freex-hiert-newline-and-indent ()
;; (interactive)
;; (if (freex-hiert-on-blank-line)
;; (newline)
;; (progn
;; (newline-and-indent)
;; (newline-and-indent))))
;; ;; new attempt at newline-and-indent, 070222
;; (defun freex-hiert-newline-and-indent ()
;; (interactive)
;; ;; ;; need to check if we're at the leftmost margin
;; ;; (if (equal (current-column) 0)
;; (let ((length-of-line
;; (save-excursion
;; (end-of-line)
;; (current-column))))
;; ;; need to check whether we're on a completely blank line
;; ;; (i.e. no whitespace at all)
;; (if (> length-of-line 0)
;; ;; if so
;; (newline-and-indent)
;; ;; otherwise, just add a newline
;; (newline))))
(defun freex-hiert-newline-and-indent ()
(interactive)
(let ((length-of-line (save-excursion (end-of-line) (current-column)))
(ncols (current-column)))
(if (> length-of-line 0)
(if (looking-back "^\\([ ]\\)*") ;; blank up to point
(progn
(newline)
(dotimes (i ncols) (insert " ")))
(newline-and-indent))
(newline))))
; based on muse-freex-hiert-on-blank-line, but muse-freex-hiert-on-blank-line
; doesn't deal well with the last line of the buffer
(defun freex-hiert-on-blank-line ()
"See if point is on a blank line (i.e. containing nothing,
or nothing-but-whitespace"
(interactive)
(let ((isblank))
(save-excursion
(beginning-of-line)
(setq isblank (looking-at "\\([ ]\\)*$")))
isblank))
(defun freex-hiert-add-two-spaces ()
(interactive)
(insert " "))
; from http://www.cs.berkeley.edu/~smcpeak/elisp/scott.emacs.el
(defun freex-hiert-is-mark-active ()
"True if the selection is displayed."
(cond
(version-emacs
mark-active) ; (mark) causes error if inactive under regular emacs
(version-xemacs
(not (not (mark)))) ; but nil with xemacs, and mark-active doesn't exist
(t t)
))
(defun freex-hiert-backward-paragraph ()
(interactive)
(if (<= (count-lines 1 (point)) 1)
;; if we're on the first line, then just go to
;; (point-min) and return it
;;
;; N.B. count-lines returns 0 if at point-min, 1 if
;; you're on the first line but not at point-min, and
;; otherwise returns a 1-indexed integer
(goto-char (point-min))
;; otherwise, go back up a para
(progn
(re-search-backward "^\\( \\)*$")
(forward-line)
(point))))
;; ;; trying to make this function more robust (e.g. for
;; ;; indenting paragraphs at the very end of the buffer)
;; (defun freex-hiert-forward-paragraph ()
;; (interactive)
;; (condition-case err ;; unneeded variable argument
;; (progn ;; try block
;; (re-search-forward "^\\( \\)*$")
;; (backward-char))
;; ;; catch block
;; ;; (search-failed (message "hello")))
;; (search-failed nil))
;; (point))
;; (defun freex-hiert-forward-paragraph ()
;; (interactive)
;; (when (not (= (point) (point-max)))
;; (when
;; ;; try and scoot ahead to the end of this
;; ;; paragraph
;; (re-search-forward "^\\( \\)*$" nil t)
;; ;; and if that worked, then move back one
;; (backward-char)))
;; (point))
(defun freex-hiert-forward-paragraph ()
(interactive)
(when (not (= (point) (point-max)))
;; if you can't scoot ahead to the end of this
;; paragraph
(when (not (re-search-forward "^\\( \\)*$" nil t))
(save-excursion
(goto-char (point-max))
(insert "\n"))
;; and now try again
(re-search-forward "^\\( \\)*$" nil t))
;; now you're ready to move back one
(backward-char))
(point))
;; I don't want to allow hanging indents. So the first-line
;; indent should set the indentation for the entire paragraph,
;; block-quote style. The easiest way to do this is to unfill
;; the paragraph, turning it into a single long line, and then
;; fill it again.
(defun freex-hiert-fill-paragraph ()
(interactive)
(freex-hiert-unfill-paragraph)
(fill-paragraph nil))
;; (local-set-key "\M-q" 'freex-hiert-fill-paragraph)
(define-key freex-mode-map "\M-q" 'freex-hiert-fill-paragraph)
; http://www.emacswiki.org/cgi-bin/wiki/UnfillParagraph
;
;;; Stefan Monnier <foo at acm.org>. It is the opposite of
;;; fill-paragraph Takes a multi-line paragraph and makes it
;;; into a single line of text.
(defun freex-hiert-unfill-paragraph ()
(interactive)
(let ((fill-column (point-max)))
(fill-paragraph nil)))
(defun freex-hiert-at-beginning-of-paragraph ()
(interactive)
(let ((cur (point))
(at-beg nil))
(save-excursion
(freex-hiert-backward-paragraph)
(setq at-beg (= cur (point))))
at-beg))
(defun freex-hiert-at-end-of-paragraph ()
(interactive)
(let ((cur (point))
(at-end nil))
(save-excursion
(freex-hiert-forward-paragraph)
(setq at-end (= cur (point))))
at-end))
(defun freex-hiert-at-beginning-of-line ()
(= (point) (point-at-bol)))
(defun freex-hiert-at-end-of-line ()
(= (point) (point-at-eol)))
(provide 'freex-hiert)