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