-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparasep.el
330 lines (254 loc) · 10.8 KB
/
parasep.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
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
;;; parasep.el --- more paragraph separators
;; Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2015 Kevin Ryde
;; Author: Kevin Ryde <user42_kevin@yahoo.com.au>
;; Version: 6
;; Keywords: convenience, paragraphs
;; URL: http://user42.tuxfamily.org/parasep/index.html
;; parasep.el 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, or (at your option) any later
;; version.
;;
;; parasep.el 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 can get a copy of the GNU General Public License online at
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is a few functions to add further paragraph separators to the
;; `paragraph-separate' and `paragraph-start' variables.
;;
;; parasep-dashes line of --- dashes separator
;; parasep-empty-comments empty comments separator
;; parasep-perl-pod pod =foo command separators
;; parasep-perl-pod-X X<> index directive separator
;; parasep-tex-index \index{} on a line alone
;; parasep-texinfo-@* @* line break separator
;;
;; The functions are designed to be used either from a mode hook, or
;; M-x interactively for an occasional change.
;;; Emacsen:
;; Designed for Emacs 21 and up, works in XEmacs 21, believe works in Emacs
;; 20.
;;; Install:
;; Put parasep.el in one of your `load-path' directories, and in your .emacs
;; add
;;
;; (require 'parasep)
;;
;; Then give the functions a one-off try like `M-x parasep-dashes', or add
;; it to a hook
;;
;; (add-hook 'text-mode-hook 'parasep-dashes)
;;
;; There's autoload cookies for the functions if you know how to use
;; `update-file-autoloads' and friends, after which add or customize the
;; hooks.
;;; History:
;; Version 1 - the first version
;; Version 2 - new parasep-texinfo-@*
;; - better check for parts already in regexp
;; Version 3 - correction to custom-add-option
;; Version 4 - new parasep-perl-pod-X
;; Version 5 - new email
;; Version 6 - new parasep-tex-index
;;; Code:
(defun parasep-regexp-valid-p (str)
"An internal part of parasep.el.
Return non-nil if string STR is a valid regexp."
(condition-case nil
(progn (string-match str "") t)
(invalid-regexp nil)))
(defun parasep-regexp-split (regexp)
"An internal part of parasep.el.
Return a list of the toplevel alternates of REGEXP.
REGEXP is split on top-level \\=\\| so for instance
\"a\\=\\|\\=\\(b\\=\\|c\\=\\)\" gives a list (\"a\" \"\\=\\(b\\=\\|c\\=\\)\").
If REGEXP is invalid then currently there's no error but there's
no splitting past the bad point."
(let* ((ret (split-string regexp "\\\\|"))
(upto ret))
(while (cdr upto)
(if (parasep-regexp-valid-p (car upto))
(setq upto (cdr upto))
(setcar upto (concat (car upto) "\\|" (cadr upto)))
(setcdr upto (cddr upto))))
ret))
(defun parasep-add-to-regexp-var (var regexp)
"An internal part of parasep.el.
Extend regexp variable VAR to match REGEXP too.
VAR is a symbol, the name of a variable containing a regexp
string. If the given REGEXP is not already among the toplevel
alternates in VAR then it's prepended.
The return is the new value of VAR."
(let ((old (symbol-value var)))
(if (member regexp (parasep-regexp-split old))
old
(set var (concat regexp "\\|" old)))))
(defun parasep-add (regexp)
"An internal part of parasep.el.
Append REGEXP to `paragraph-separate' and `paragraph-start' if
it's not already present in those variables.
Checking for already present means that repeating the parasep
commands doesn't make the variables ever longer."
(parasep-add-to-regexp-var (make-local-variable 'paragraph-separate) regexp)
(parasep-add-to-regexp-var (make-local-variable 'paragraph-start) regexp))
;;-----------------------------------------------------------------------------
;;;###autoload
(defun parasep-dashes ()
"Make a line of ---- dashes a paragraph separator.
`paragraph-start' and `paragraph-separate' are extended so that a
line of dashes is a separator.
-------------
Or a comment line of dashes similarly.
;; ----------
/*---------*/
The pattern added is quite loose, simply an optional
`comment-start-skip' followed by 4 or more \"-\". Perhaps in the
future this may have to be a bit tighter.
The current value of `comment-start-skip' is copied into
`paragraph-start' and `paragraph-separate' etc, so if customizing
`comment-start-skip' be sure to do that before `parasep-dashes'."
(interactive)
(parasep-add (concat
(unless (member comment-start-skip '(nil ""))
(concat "\\(\\(" comment-start-skip "\\)[ \t]*\\)?"))
"----+[ \t]*")))
;; Don't think need to insist that the dashes run to the end-of-line (with
;; possible whitespace or comment-end).
;;
;; If asking this then cc-mode comment-end " */" is a bit strict and would
;; want to turn the space into [ \t] to allow tab as well as space. (No
;; comment-end-skip regexp as of cc-mode 5.32.)
;;
;; ;; `comment-end-skip' not in xemacs21, but don't lock down the check at
;; ;; compile time, just in case someone fires up newcomment.el there
;; (or (and (boundp 'comment-end-skip) ;; not in xemacs21
;; (not (member comment-end-skip '(nil "")))
;; (concat "\\(\\(" comment-end-skip "\\)[ \t]*\\)?"))
;; (and (not (member comment-end '(nil "")))
;; (concat "\\(" (regexp-quote comment-end)
;; "[ \t]*\\)?")))
;; "$"
;;-----------------------------------------------------------------------------
;;;###autoload
(defun parasep-empty-comments ()
"Make an empty comment line a paragraph separator.
`paragraph-start' and `paragraph-separate' are extended so a
comment line with no text is a paragraph separator.
;; One paragraph.
;; <-- separator
;; Another paragraph.
This is good if you use `comment-empty-lines'. The default
paragraph separators in Emacs are generally only geared towards
completely blank lines between comment paragraphs.
The commenting matched follows `comment-start-skip'. If it's nil
or empty then there's no comment syntax and this function does
nothing."
(interactive)
(unless (member comment-start-skip '(nil ""))
(parasep-add (concat "\\(" comment-start-skip "\\)[ \t]*$"))))
;;-----------------------------------------------------------------------------
;;;###autoload
(defun parasep-perl-pod ()
"Make POD =foo directives a paragraph separator.
Usually this is unnecessary because there's a blank line between
directives and other text or other directives, but Perl-Gtk XS
crunched with Glib::CodeGen is instead like
=for apidoc
Something about this func.
=cut
and treating directives as separators helps filling the text in
between."
;; an unindented =item, =for, etc, and standard directives are all lower
;; case
(interactive)
(parasep-add "=[a-z]"))
;;;###autoload
(custom-add-option 'perl-mode-hook 'parasep-perl-pod)
;;;###autoload
(custom-add-option 'cperl-mode-hook 'parasep-perl-pod)
;;;###autoload
(custom-add-option 'pod-mode-hook 'parasep-perl-pod)
;;-----------------------------------------------------------------------------
;;;###autoload
(defun parasep-perl-pod-X ()
"Have POD X<> index directives as paragraph separators.
This is designed for X<> directives on a line by themselves
either at the start or end of a paragraph.
X<blah> <-- separator
Something about blah.
Foo bar quux.
X<foo> X<bar> <-- separator
Making X<> a paragraph separator stops `fill-paragraph'
\(\\[fill-paragraph]) flowing it into the paragraph text.
Paragraph movement commands will skip them too, which may or may
not be good.
There can be multiple X<> on the line as shown above, but the X<>
cannot extend across multiple lines since Emacs
`paragraph-separate' mechanism isn't designed for that.. It's
unlikely an X<> entry would be longer than a single line.
X<This is a multi <-- not a separator
line index entry>
A line with plain text after the X<> is not a separator. This
means you can sometimes write X<> on a line alone and sometimes
together with the text and the style written is preserved.
X<foo> Some thing <-- not a separator
blah blah."
(interactive)
(parasep-add "\\(X<[^>\n]*>\\s-*\\)+$"))
;;;###autoload
(custom-add-option 'perl-mode-hook 'parasep-perl-pod-X)
;;;###autoload
(custom-add-option 'cperl-mode-hook 'parasep-perl-pod-X)
;;;###autoload
(custom-add-option 'pod-mode-hook 'parasep-perl-pod-X)
;;-----------------------------------------------------------------------------
;; TeX
;;;###autoload
(defun parasep-tex-index ()
"Make \\=\\index{} on a line alone a paragraph separator.
This is designed to prevent a \\=\\index entry on a line alone
from being included in an adjacent paragraph when filling etc.
\\=\\index{Foo|(}
Foo is blah blah blah ...
\\=\\index{Foo|)}
If \\=\\index is within text, not on a line of its own, then it's
not a separator and is filled in the usual way.
Foo\\=\\index{Foo} is blah blah blah ...
An \\=\\index can contain braces, but no more than one nested
level. This allows a little TeX for a dual plain and formatted
style index entry.
\\=\\index{Some-thing@Some\\=\\hyp{}thing}
\\=\\index is usually for LaTeX but with some packages the same
can apparently be used in other flavours, including plain TeX."
(interactive)
(parasep-add "\\\\index{[^{}]*\\({[^{}]*}[^{}]*\\)*}[ \t]*\\(%.*\\)?$"))
;;;###autoload
(custom-add-option 'latex-mode-hook 'parasep-tex-index)
;;-----------------------------------------------------------------------------
;; Texinfo
;;;###autoload
(defun parasep-texinfo-@* ()
"Make a line with \"@*\" start or separate a paragraph.
An @* alone on a line is a paragraph separator, an @* at the
beginning of a non-empty line is the start of a paragraph.
The effect is that paragraph filling won't flow an @* into
surrounding lines, so the texinfo source keeps a line break
similar to what @* will produce in the formatted output.
\(As of Emacs 24.5, the default settings have alphabetical
@foo commands as paragraph separators, but not @*.)"
(interactive)
(parasep-add-to-regexp-var (make-local-variable 'paragraph-start)
"@\\*\\(\\s-\\|$\\)")
(parasep-add-to-regexp-var (make-local-variable 'paragraph-separate)
"@\\*\\s-*$"))
;;;###autoload
(custom-add-option 'texinfo-mode-hook 'parasep-texinfo-@*)
;;-----------------------------------------------------------------------------
;; LocalWords: texinfo parasep toplevel prepended el Gtk CodeGen apidoc func
;; LocalWords: foo quux multi
(provide 'parasep)
;;; parasep.el ends here