Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / sort.el
CommitLineData
55535639 1;;; sort.el --- commands to sort text in an Emacs buffer
c88ab9ce 2
73b0cd50 3;; Copyright (C) 1986-1987, 1994-1995, 2001-2011 Free Software Foundation, Inc.
eea8d4ef 4
e5167999
ER
5;; Author: Howie Kaye
6;; Maintainer: FSF
d7b4d18f 7;; Keywords: unix
e5167999 8
d32200ac
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
d32200ac 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
d32200ac
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
d32200ac 23
d9ecc911
ER
24;;; Commentary:
25
b578f267
EN
26;; This package provides the sorting facilities documented in the Emacs
27;; user's manual.
d9ecc911 28
e5167999 29;;; Code:
d32200ac 30
ab1c7f35
RS
31(defgroup sort nil
32 "Commands to sort text in an Emacs buffer."
33 :group 'data)
34
35(defcustom sort-fold-case nil
9201cc28 36 "Non-nil if the buffer sort functions should ignore case."
ab1c7f35
RS
37 :group 'sort
38 :type 'boolean)
aee00263 39;;;###autoload(put 'sort-fold-case 'safe-local-variable 'booleanp)
faf603f3 40
32033024 41;;;###autoload
e2292b24
SM
42(defun sort-subr (reverse nextrecfun endrecfun
43 &optional startkeyfun endkeyfun predicate)
d32200ac 44 "General text sorting routine to divide buffer into records and sort them.
d32200ac 45
a08caf95 46We divide the accessible portion of the buffer into disjoint pieces
fbfed6f0
JB
47called sort records. A portion of each sort record (perhaps all of
48it) is designated as the sort key. The records are rearranged in the
49buffer in order by their sort keys. The records may or may not be
50contiguous.
d32200ac
RS
51
52Usually the records are rearranged in order of ascending sort key.
53If REVERSE is non-nil, they are rearranged in order of descending sort key.
469b44cb
RS
54The variable `sort-fold-case' determines whether alphabetic case affects
55the sort order.
d32200ac
RS
56
57The next four arguments are functions to be called to move point
58across a sort record. They will be called many times from within sort-subr.
59
60NEXTRECFUN is called with point at the end of the previous record.
61It moves point to the start of the next record.
62It should move point to the end of the buffer if there are no more records.
63The first record is assumed to start at the position of point when sort-subr
64is called.
65
13f4b6b3 66ENDRECFUN is called with point within the record.
d32200ac
RS
67It should move point to the end of the record.
68
13f4b6b3
RS
69STARTKEYFUN moves from the start of the record to the start of the key.
70It may return either a non-nil value to be used as the key, or
a08caf95 71else the key is the substring between the values of point after
d9a55d32
RS
72STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key
73starts at the beginning of the record.
d32200ac
RS
74
75ENDKEYFUN moves from the start of the sort key to the end of the sort key.
76ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
e2292b24
SM
77same as ENDRECFUN.
78
79PREDICATE is the function to use to compare keys. If keys are numbers,
80it defaults to `<', otherwise it defaults to `string<'."
a08caf95
RS
81 ;; Heuristically try to avoid messages if sorting a small amt of text.
82 (let ((messages (> (- (point-max) (point-min)) 50000)))
83 (save-excursion
84 (if messages (message "Finding sort keys..."))
85 (let* ((sort-lists (sort-build-lists nextrecfun endrecfun
86 startkeyfun endkeyfun))
faf603f3
RS
87 (old (reverse sort-lists))
88 (case-fold-search sort-fold-case))
a08caf95
RS
89 (if (null sort-lists)
90 ()
91 (or reverse (setq sort-lists (nreverse sort-lists)))
92 (if messages (message "Sorting records..."))
93 (setq sort-lists
e2292b24
SM
94 (sort sort-lists
95 (cond (predicate
96 `(lambda (a b) (,predicate (car a) (car b))))
97 ((numberp (car (car sort-lists)))
98 'car-less-than-car)
99 ((consp (car (car sort-lists)))
100 (lambda (a b)
101 (> 0 (compare-buffer-substrings
102 nil (car (car a)) (cdr (car a))
103 nil (car (car b)) (cdr (car b))))))
104 (t
105 (lambda (a b) (string< (car a) (car b)))))))
a08caf95
RS
106 (if reverse (setq sort-lists (nreverse sort-lists)))
107 (if messages (message "Reordering buffer..."))
108 (sort-reorder-buffer sort-lists old)))
109 (if messages (message "Reordering buffer... Done"))))
d32200ac
RS
110 nil)
111
112;; Parse buffer into records using the arguments as Lisp expressions;
e453d35a 113;; return a list of records. Each record looks like (KEY STARTPOS . ENDPOS)
d32200ac
RS
114;; where KEY is the sort key (a number or string),
115;; and STARTPOS and ENDPOS are the bounds of this record in the buffer.
116
117;; The records appear in the list lastmost first!
118
119(defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun)
120 (let ((sort-lists ())
121 (start-rec nil)
122 done key)
123 ;; Loop over sort records.
124 ;(goto-char (point-min)) -- it is the caller's responsibility to
125 ;arrange this if necessary
126 (while (not (eobp))
127 (setq start-rec (point)) ;save record start
128 (setq done nil)
129 ;; Get key value, or move to start of key.
130 (setq key (catch 'key
131 (or (and startkeyfun (funcall startkeyfun))
132 ;; If key was not returned as value,
133 ;; move to end of key and get key from the buffer.
134 (let ((start (point)))
135 (funcall (or endkeyfun
136 (prog1 endrecfun (setq done t))))
4f1b6c54 137 (cons start (point))))))
d32200ac
RS
138 ;; Move to end of this record (start of next one, or end of buffer).
139 (cond ((prog1 done (setq done nil)))
140 (endrecfun (funcall endrecfun))
141 (nextrecfun (funcall nextrecfun) (setq done t)))
e2292b24
SM
142 (if key (push
143 ;; consing optimization in case in which key is same as record.
144 (if (and (consp key)
145 (equal (car key) start-rec)
146 (equal (cdr key) (point)))
147 (cons key key)
148 (cons key (cons start-rec (point))))
149 sort-lists))
d32200ac
RS
150 (and (not done) nextrecfun (funcall nextrecfun)))
151 sort-lists))
152
153(defun sort-reorder-buffer (sort-lists old)
67f5954c
RS
154 (let ((last (point-min))
155 (min (point-min)) (max (point-max))
156 (old-buffer (current-buffer))
1fa85ea8 157 (mb enable-multibyte-characters)
67f5954c
RS
158 temp-buffer)
159 (with-temp-buffer
1fa85ea8 160 (set-buffer-multibyte mb)
67f5954c
RS
161 ;; Record the temporary buffer.
162 (setq temp-buffer (current-buffer))
163
164 ;; Copy the sorted text into the temporary buffer.
165 (while sort-lists
166 (goto-char (point-max))
167 (insert-buffer-substring old-buffer
168 last
169 (nth 1 (car old)))
170 (goto-char (point-max))
171 (insert-buffer-substring old-buffer
172 (nth 1 (car sort-lists))
173 (cdr (cdr (car sort-lists))))
174 (setq last (cdr (cdr (car old)))
175 sort-lists (cdr sort-lists)
176 old (cdr old)))
d32200ac 177 (goto-char (point-max))
ce4ef587 178 (insert-buffer-substring old-buffer last max)
67f5954c
RS
179
180 ;; Copy the reordered text from the temporary buffer
181 ;; to the buffer we sorted (OLD-BUFFER).
182 (set-buffer old-buffer)
183 (let ((inhibit-quit t))
184 ;; Make sure insertions done for reordering
9b392284
JB
185 ;; saves any markers at the end of the sorted region,
186 ;; by leaving the last character of the region.
187 (delete-region min (1- max))
188 ;; Now replace the one remaining old character with the sorted text.
189 (goto-char (point-min))
ce4ef587 190 (insert-buffer-substring temp-buffer)
9b392284 191 (delete-region max (1+ max))))))
d32200ac 192
f9f9507e 193;;;###autoload
f1180544 194(defun sort-lines (reverse beg end)
d32200ac
RS
195 "Sort lines in region alphabetically; argument means descending order.
196Called from a program, there are three arguments:
469b44cb
RS
197REVERSE (non-nil means reverse order), BEG and END (region to sort).
198The variable `sort-fold-case' determines whether alphabetic case affects
199the sort order."
d32200ac
RS
200 (interactive "P\nr")
201 (save-excursion
202 (save-restriction
203 (narrow-to-region beg end)
204 (goto-char (point-min))
979a59c2
KH
205 (let ;; To make `end-of-line' and etc. to ignore fields.
206 ((inhibit-field-text-motion t))
207 (sort-subr reverse 'forward-line 'end-of-line)))))
d32200ac 208
f9f9507e 209;;;###autoload
d32200ac
RS
210(defun sort-paragraphs (reverse beg end)
211 "Sort paragraphs in region alphabetically; argument means descending order.
212Called from a program, there are three arguments:
469b44cb
RS
213REVERSE (non-nil means reverse order), BEG and END (region to sort).
214The variable `sort-fold-case' determines whether alphabetic case affects
215the sort order."
d32200ac
RS
216 (interactive "P\nr")
217 (save-excursion
218 (save-restriction
219 (narrow-to-region beg end)
220 (goto-char (point-min))
221 (sort-subr reverse
ee5a3408
KH
222 (function
223 (lambda ()
224 (while (and (not (eobp)) (looking-at paragraph-separate))
225 (forward-line 1))))
d32200ac
RS
226 'forward-paragraph))))
227
f9f9507e 228;;;###autoload
d32200ac
RS
229(defun sort-pages (reverse beg end)
230 "Sort pages in region alphabetically; argument means descending order.
231Called from a program, there are three arguments:
469b44cb
RS
232REVERSE (non-nil means reverse order), BEG and END (region to sort).
233The variable `sort-fold-case' determines whether alphabetic case affects
234the sort order."
d32200ac
RS
235 (interactive "P\nr")
236 (save-excursion
237 (save-restriction
238 (narrow-to-region beg end)
239 (goto-char (point-min))
240 (sort-subr reverse
241 (function (lambda () (skip-chars-forward "\n")))
242 'forward-page))))
243\f
244(defvar sort-fields-syntax-table nil)
245(if sort-fields-syntax-table nil
246 (let ((table (make-syntax-table))
247 (i 0))
248 (while (< i 256)
249 (modify-syntax-entry i "w" table)
250 (setq i (1+ i)))
89226f32 251 (modify-syntax-entry ?\s " " table)
d32200ac
RS
252 (modify-syntax-entry ?\t " " table)
253 (modify-syntax-entry ?\n " " table)
254 (modify-syntax-entry ?\. "_" table) ; for floating pt. numbers. -wsr
255 (setq sort-fields-syntax-table table)))
256
5d6dd963 257(defcustom sort-numeric-base 10
9201cc28 258 "The default base used by `sort-numeric-fields'."
5d6dd963
GM
259 :group 'sort
260 :type 'integer)
aee00263 261;;;###autoload(put 'sort-numeric-base 'safe-local-variable 'integerp)
5d6dd963 262
f9f9507e 263;;;###autoload
d32200ac
RS
264(defun sort-numeric-fields (field beg end)
265 "Sort lines in region numerically by the ARGth field of each line.
266Fields are separated by whitespace and numbered from 1 up.
5d6dd963
GM
267Specified field must contain a number in each line of the region,
268which may begin with \"0x\" or \"0\" for hexadecimal and octal values.
269Otherwise, the number is interpreted according to sort-numeric-base.
2af3a0e3 270With a negative arg, sorts by the ARGth field counted from the right.
d32200ac 271Called from a program, there are three arguments:
4138e600 272FIELD, BEG and END. BEG and END specify region to sort."
d32200ac 273 (interactive "p\nr")
979a59c2
KH
274 (let ;; To make `end-of-line' and etc. to ignore fields.
275 ((inhibit-field-text-motion t))
276 (sort-fields-1 field beg end
277 (lambda ()
278 (sort-skip-fields field)
279 (let* ((case-fold-search t)
280 (base
281 (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]")
282 (cond ((match-beginning 1)
283 (goto-char (match-end 1))
284 16)
285 ((match-beginning 2)
286 (goto-char (match-end 2))
287 8)
288 (t nil)))))
289 (string-to-number (buffer-substring (point)
290 (save-excursion
291 (forward-sexp 1)
292 (point)))
293 (or base sort-numeric-base))))
294 nil)))
d32200ac 295
4138e600
RS
296;;;;;###autoload
297;;(defun sort-float-fields (field beg end)
298;; "Sort lines in region numerically by the ARGth field of each line.
299;;Fields are separated by whitespace and numbered from 1 up. Specified field
300;;must contain a floating point number in each line of the region. With a
301;;negative arg, sorts by the ARGth field counted from the right. Called from a
302;;program, there are three arguments: FIELD, BEG and END. BEG and END specify
303;;region to sort."
304;; (interactive "p\nr")
305;; (sort-fields-1 field beg end
306;; (function (lambda ()
307;; (sort-skip-fields field)
308;; (string-to-number
309;; (buffer-substring
310;; (point)
311;; (save-excursion
312;; (re-search-forward
313;; "[+-]?[0-9]*\.?[0-9]*\\([eE][+-]?[0-9]+\\)?")
314;; (point))))))
315;; nil))
2af3a0e3 316
f9f9507e 317;;;###autoload
d32200ac
RS
318(defun sort-fields (field beg end)
319 "Sort lines in region lexicographically by the ARGth field of each line.
320Fields are separated by whitespace and numbered from 1 up.
2af3a0e3 321With a negative arg, sorts by the ARGth field counted from the right.
d32200ac 322Called from a program, there are three arguments:
469b44cb
RS
323FIELD, BEG and END. BEG and END specify region to sort.
324The variable `sort-fold-case' determines whether alphabetic case affects
325the sort order."
d32200ac 326 (interactive "p\nr")
979a59c2
KH
327 (let ;; To make `end-of-line' and etc. to ignore fields.
328 ((inhibit-field-text-motion t))
329 (sort-fields-1 field beg end
330 (function (lambda ()
331 (sort-skip-fields field)
332 nil))
333 (function (lambda () (skip-chars-forward "^ \t\n"))))))
d32200ac
RS
334
335(defun sort-fields-1 (field beg end startkeyfun endkeyfun)
2af3a0e3 336 (let ((tbl (syntax-table)))
337 (if (zerop field) (setq field 1))
d32200ac
RS
338 (unwind-protect
339 (save-excursion
340 (save-restriction
341 (narrow-to-region beg end)
342 (goto-char (point-min))
343 (set-syntax-table sort-fields-syntax-table)
2af3a0e3 344 (sort-subr nil
d32200ac
RS
345 'forward-line 'end-of-line
346 startkeyfun endkeyfun)))
347 (set-syntax-table tbl))))
348
220eb88b
RS
349;; Position at the beginning of field N on the current line,
350;; assuming point is initially at the beginning of the line.
d32200ac 351(defun sort-skip-fields (n)
220eb88b
RS
352 (if (> n 0)
353 ;; Skip across N - 1 fields.
354 (let ((i (1- n)))
355 (while (> i 0)
356 (skip-chars-forward " \t")
357 (skip-chars-forward "^ \t\n")
358 (setq i (1- i)))
359 (skip-chars-forward " \t")
220eb88b
RS
360 (if (eolp)
361 (error "Line has too few fields: %s"
362 (buffer-substring
5ed619e0
GM
363 (line-beginning-position)
364 (line-end-position)))))
220eb88b
RS
365 (end-of-line)
366 ;; Skip back across - N - 1 fields.
367 (let ((i (1- (- n))))
368 (while (> i 0)
369 (skip-chars-backward " \t")
370 (skip-chars-backward "^ \t\n")
371 (setq i (1- i)))
372 (skip-chars-backward " \t"))
373 (if (bolp)
d32200ac 374 (error "Line has too few fields: %s"
220eb88b 375 (buffer-substring
5ed619e0
GM
376 (line-beginning-position)
377 (line-end-position))))
220eb88b
RS
378 ;; Position at the front of the field
379 ;; even if moving backwards.
380 (skip-chars-backward "^ \t\n")))
d32200ac 381\f
387f3b21
RS
382(defvar sort-regexp-fields-regexp)
383(defvar sort-regexp-record-end)
384
385;; Move to the beginning of the next match for record-regexp,
386;; and set sort-regexp-record-end to the end of that match.
387;; If the next match is empty and does not advance point,
388;; skip one character and try again.
389(defun sort-regexp-fields-next-record ()
390 (let ((oldpos (point)))
391 (and (re-search-forward sort-regexp-fields-regexp nil 'move)
392 (setq sort-regexp-record-end (match-end 0))
393 (if (= sort-regexp-record-end oldpos)
394 (progn
395 (forward-char 1)
396 (re-search-forward sort-regexp-fields-regexp nil 'move)
397 (setq sort-regexp-record-end (match-end 0)))
398 t)
399 (goto-char (match-beginning 0)))))
400
f9f9507e 401;;;###autoload
d32200ac 402(defun sort-regexp-fields (reverse record-regexp key-regexp beg end)
eb8c3be9 403 "Sort the region lexicographically as specified by RECORD-REGEXP and KEY.
d32200ac
RS
404RECORD-REGEXP specifies the textual units which should be sorted.
405 For example, to sort lines RECORD-REGEXP would be \"^.*$\"
406KEY specifies the part of each record (ie each match for RECORD-REGEXP)
407 is to be used for sorting.
8e5b59e1 408 If it is \"\\\\digit\" then the digit'th \"\\\\(...\\\\)\" match field from
d32200ac 409 RECORD-REGEXP is used.
8e5b59e1 410 If it is \"\\\\&\" then the whole record is used.
d32200ac
RS
411 Otherwise, it is a regular-expression for which to search within the record.
412If a match for KEY is not found within a record then that record is ignored.
413
414With a negative prefix arg sorts in reverse order.
415
469b44cb
RS
416The variable `sort-fold-case' determines whether alphabetic case affects
417the sort order.
418
d32200ac
RS
419For example: to sort lines in the region by the first word on each line
420 starting with the letter \"f\",
8e5b59e1 421 RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\\\=\\<f\\\\w*\\\\>\""
2af3a0e3 422 ;; using negative prefix arg to mean "reverse" is now inconsistent with
423 ;; other sort-.*fields functions but then again this was before, since it
424 ;; didn't use the magnitude of the arg to specify anything.
f1180544 425 (interactive "P\nsRegexp specifying records to sort:
d32200ac
RS
426sRegexp specifying key within record: \nr")
427 (cond ((or (equal key-regexp "") (equal key-regexp "\\&"))
428 (setq key-regexp 0))
429 ((string-match "\\`\\\\[1-9]\\'" key-regexp)
430 (setq key-regexp (- (aref key-regexp 1) ?0))))
431 (save-excursion
432 (save-restriction
433 (narrow-to-region beg end)
434 (goto-char (point-min))
387f3b21
RS
435 (let (sort-regexp-record-end
436 (sort-regexp-fields-regexp record-regexp))
4cc7cae8 437 (re-search-forward sort-regexp-fields-regexp nil t)
d32200ac
RS
438 (setq sort-regexp-record-end (point))
439 (goto-char (match-beginning 0))
440 (sort-subr reverse
387f3b21 441 'sort-regexp-fields-next-record
d32200ac
RS
442 (function (lambda ()
443 (goto-char sort-regexp-record-end)))
444 (function (lambda ()
445 (let ((n 0))
446 (cond ((numberp key-regexp)
447 (setq n key-regexp))
448 ((re-search-forward
449 key-regexp sort-regexp-record-end t)
450 (setq n 0))
451 (t (throw 'key nil)))
452 (condition-case ()
469b44cb
RS
453 (cons (match-beginning n)
454 (match-end n))
d32200ac
RS
455 ;; if there was no such register
456 (error (throw 'key nil)))))))))))
457
458\f
459(defvar sort-columns-subprocess t)
460
f9f9507e 461;;;###autoload
d32200ac
RS
462(defun sort-columns (reverse &optional beg end)
463 "Sort lines in region alphabetically by a certain range of columns.
02ebb2ea 464For the purpose of this command, the region BEG...END includes
d32200ac
RS
465the entire line that point is in and the entire line the mark is in.
466The column positions of point and mark bound the range of columns to sort on.
02ebb2ea 467A prefix argument means sort into REVERSE order.
469b44cb
RS
468The variable `sort-fold-case' determines whether alphabetic case affects
469the sort order.
d32200ac
RS
470
471Note that `sort-columns' rejects text that contains tabs,
472because tabs could be split across the specified columns
473and it doesn't know how to handle that. Also, when possible,
474it uses the `sort' utility program, which doesn't understand tabs.
475Use \\[untabify] to convert tabs to spaces before sorting."
476 (interactive "P\nr")
477 (save-excursion
979a59c2
KH
478 (let ;; To make `end-of-line' and etc. to ignore fields.
479 ((inhibit-field-text-motion t)
480 beg1 end1 col-beg1 col-end1 col-start col-end)
d32200ac
RS
481 (goto-char (min beg end))
482 (setq col-beg1 (current-column))
483 (beginning-of-line)
484 (setq beg1 (point))
485 (goto-char (max beg end))
486 (setq col-end1 (current-column))
487 (forward-line)
488 (setq end1 (point))
489 (setq col-start (min col-beg1 col-end1))
490 (setq col-end (max col-beg1 col-end1))
491 (if (search-backward "\t" beg1 t)
c47e669b 492 (error "sort-columns does not work with tabs -- use M-x untabify"))
7c2fb837 493 (if (not (or (memq system-type '(windows-nt))
2e8d40a1
RS
494 (let ((pos beg1) plist fontified)
495 (catch 'found
496 (while (< pos end1)
497 (setq plist (text-properties-at pos))
498 (setq fontified (plist-get plist 'fontified))
499 (while (consp plist)
500 (unless (or (eq (car plist) 'fontified)
501 (and (eq (car plist) 'face)
502 fontified))
503 (throw 'found t))
504 (setq plist (cddr plist)))
505 (setq pos (next-property-change pos nil end1)))))))
d32200ac 506 ;; Use the sort utility if we can; it is 4 times as fast.
2e8d40a1
RS
507 ;; Do not use it if there are any non-font-lock properties
508 ;; in the region, since the sort utility would lose the
7075e9c8
JB
509 ;; properties. Tabs are used as field separator; on NetBSD,
510 ;; sort complains if "\n" is used as field separator.
e7cd761f 511 (let ((sort-args (list (if reverse "-rt\t" "-t\t")
12549864
RS
512 (format "-k1.%d,1.%d"
513 (1+ col-start)
514 (1+ col-end)))))
c844fe9d
GM
515 (when sort-fold-case
516 (push "-f" sort-args))
517 (apply #'call-process-region beg1 end1 "sort" t t nil sort-args))
7c2fb837 518 ;; On ms-windows, use Emacs's own facilities.
d32200ac
RS
519 (save-excursion
520 (save-restriction
521 (narrow-to-region beg1 end1)
522 (goto-char beg1)
523 (sort-subr reverse 'forward-line 'end-of-line
c844fe9d
GM
524 #'(lambda () (move-to-column col-start) nil)
525 #'(lambda () (move-to-column col-end) nil))))))))
2af3a0e3 526
f9f9507e 527;;;###autoload
2af3a0e3 528(defun reverse-region (beg end)
529 "Reverse the order of lines in a region.
530From a program takes two point or marker arguments, BEG and END."
531 (interactive "r")
532 (if (> beg end)
533 (let (mid) (setq mid end end beg beg mid)))
534 (save-excursion
535 ;; put beg at the start of a line and end and the end of one --
536 ;; the largest possible region which fits this criteria
537 (goto-char beg)
538 (or (bolp) (forward-line 1))
539 (setq beg (point))
540 (goto-char end)
541 ;; the test for bolp is for those times when end is on an empty line;
542 ;; it is probably not the case that the line should be included in the
543 ;; reversal; it isn't difficult to add it afterward.
544 (or (and (eolp) (not (bolp))) (progn (forward-line -1) (end-of-line)))
545 (setq end (point-marker))
546 ;; the real work. this thing cranks through memory on large regions.
547 (let (ll (do t))
548 (while do
549 (goto-char beg)
550 (setq ll (cons (buffer-substring (point) (progn (end-of-line) (point)))
551 ll))
552 (setq do (/= (point) end))
553 (delete-region beg (if do (1+ (point)) (point))))
554 (while (cdr ll)
555 (insert (car ll) "\n")
556 (setq ll (cdr ll)))
557 (insert (car ll)))))
49116ac0
JB
558
559(provide 'sort)
560
c88ab9ce 561;;; sort.el ends here