Change defgeneric so it doesn't completely redefine the function
[emacs.git] / lisp / sort.el
blob9b8cec6ac682c5d8dd0d58bec04170f02e8073b5
1 ;;; sort.el --- commands to sort text in an Emacs buffer
3 ;; Copyright (C) 1986-1987, 1994-1995, 2001-2015 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Howie Kaye
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: unix
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package provides the sorting facilities documented in the Emacs
28 ;; user's manual.
30 ;;; Code:
32 (defgroup sort nil
33 "Commands to sort text in an Emacs buffer."
34 :group 'data)
36 (defcustom sort-fold-case nil
37 "Non-nil if the buffer sort functions should ignore case."
38 :group 'sort
39 :type 'boolean)
40 ;;;###autoload(put 'sort-fold-case 'safe-local-variable 'booleanp)
42 ;;;###autoload
43 (defun sort-subr (reverse nextrecfun endrecfun
44 &optional startkeyfun endkeyfun predicate)
45 "General text sorting routine to divide buffer into records and sort them.
47 We divide the accessible portion of the buffer into disjoint pieces
48 called sort records. A portion of each sort record (perhaps all of
49 it) is designated as the sort key. The records are rearranged in the
50 buffer in order by their sort keys. The records may or may not be
51 contiguous.
53 Usually the records are rearranged in order of ascending sort key.
54 If REVERSE is non-nil, they are rearranged in order of descending sort key.
55 The variable `sort-fold-case' determines whether alphabetic case affects
56 the sort order.
58 The next four arguments are functions to be called to move point
59 across a sort record. They will be called many times from within sort-subr.
61 NEXTRECFUN is called with point at the end of the previous record.
62 It moves point to the start of the next record.
63 It should move point to the end of the buffer if there are no more records.
64 The first record is assumed to start at the position of point when sort-subr
65 is called.
67 ENDRECFUN is called with point within the record.
68 It should move point to the end of the record.
70 STARTKEYFUN moves from the start of the record to the start of the key.
71 It may return either a non-nil value to be used as the key, or
72 else the key is the substring between the values of point after
73 STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key
74 starts at the beginning of the record.
76 ENDKEYFUN moves from the start of the sort key to the end of the sort key.
77 ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
78 same as ENDRECFUN.
80 PREDICATE, if non-nil, is the predicate function for comparing
81 keys; it is called with two arguments, the keys to compare, and
82 should return non-nil if the first key should sort before the
83 second key. If PREDICATE is nil, comparison is done with `<' if
84 the keys are numbers, with `compare-buffer-substrings' if the
85 keys are cons cells (the car and cdr of each cons cell are taken
86 as start and end positions), and with `string<' otherwise."
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))
93 (old (reverse sort-lists))
94 (case-fold-search sort-fold-case))
95 (if (null sort-lists)
97 (or reverse (setq sort-lists (nreverse sort-lists)))
98 (if messages (message "Sorting records..."))
99 (setq sort-lists
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))))))
111 (lambda (a b) (string< (car a) (car b)))))))
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"))))
116 nil)
118 ;; Parse buffer into records using the arguments as Lisp expressions;
119 ;; return a list of records. Each record looks like (KEY STARTPOS . ENDPOS)
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.
123 ;; The records appear in the list lastmost first!
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))))
143 (cons start (point))))))
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)))
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))
156 (and (not done) nextrecfun (funcall nextrecfun)))
157 sort-lists))
159 (defun sort-reorder-buffer (sort-lists old)
160 (let ((last (point-min))
161 (min (point-min)) (max (point-max))
162 (old-buffer (current-buffer))
163 (mb enable-multibyte-characters)
164 temp-buffer)
165 (with-temp-buffer
166 (set-buffer-multibyte mb)
167 ;; Record the temporary buffer.
168 (setq temp-buffer (current-buffer))
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)))
183 (goto-char (point-max))
184 (insert-buffer-substring old-buffer last max)
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
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))
196 (insert-buffer-substring temp-buffer)
197 (delete-region max (1+ max))))))
199 ;;;###autoload
200 (defun sort-lines (reverse beg end)
201 "Sort lines in region alphabetically; argument means descending order.
202 Called from a program, there are three arguments:
203 REVERSE (non-nil means reverse order), BEG and END (region to sort).
204 The variable `sort-fold-case' determines whether alphabetic case affects
205 the sort order."
206 (interactive "P\nr")
207 (save-excursion
208 (save-restriction
209 (narrow-to-region beg end)
210 (goto-char (point-min))
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)))))
215 ;;;###autoload
216 (defun sort-paragraphs (reverse beg end)
217 "Sort paragraphs in region alphabetically; argument means descending order.
218 Called from a program, there are three arguments:
219 REVERSE (non-nil means reverse order), BEG and END (region to sort).
220 The variable `sort-fold-case' determines whether alphabetic case affects
221 the sort order."
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
228 (function
229 (lambda ()
230 (while (and (not (eobp)) (looking-at paragraph-separate))
231 (forward-line 1))))
232 'forward-paragraph))))
234 ;;;###autoload
235 (defun sort-pages (reverse beg end)
236 "Sort pages in region alphabetically; argument means descending order.
237 Called from a program, there are three arguments:
238 REVERSE (non-nil means reverse order), BEG and END (region to sort).
239 The variable `sort-fold-case' determines whether alphabetic case affects
240 the sort order."
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))))
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)))
257 (modify-syntax-entry ?\s " " table)
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)))
263 (defcustom sort-numeric-base 10
264 "The default base used by `sort-numeric-fields'."
265 :group 'sort
266 :type 'integer)
267 ;;;###autoload(put 'sort-numeric-base 'safe-local-variable 'integerp)
269 ;;;###autoload
270 (defun sort-numeric-fields (field beg end)
271 "Sort lines in region numerically by the ARGth field of each line.
272 Fields are separated by whitespace and numbered from 1 up.
273 Specified field must contain a number in each line of the region,
274 which may begin with \"0x\" or \"0\" for hexadecimal and octal values.
275 Otherwise, the number is interpreted according to sort-numeric-base.
276 With a negative arg, sorts by the ARGth field counted from the right.
277 Called from a program, there are three arguments:
278 FIELD, BEG and END. BEG and END specify region to sort."
279 (interactive "p\nr")
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))
291 ((match-beginning 2)
292 (goto-char (match-end 2))
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)))
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))
323 ;;;###autoload
324 (defun sort-fields (field beg end)
325 "Sort lines in region lexicographically by the ARGth field of each line.
326 Fields are separated by whitespace and numbered from 1 up.
327 With a negative arg, sorts by the ARGth field counted from the right.
328 Called from a program, there are three arguments:
329 FIELD, BEG and END. BEG and END specify region to sort.
330 The variable `sort-fold-case' determines whether alphabetic case affects
331 the sort order."
332 (interactive "p\nr")
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"))))))
341 (defun sort-fields-1 (field beg end startkeyfun endkeyfun)
342 (let ((tbl (syntax-table)))
343 (if (zerop field) (setq field 1))
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)
350 (sort-subr nil
351 'forward-line 'end-of-line
352 startkeyfun endkeyfun)))
353 (set-syntax-table tbl))))
355 ;; Position at the beginning of field N on the current line,
356 ;; assuming point is initially at the beginning of the line.
357 (defun sort-skip-fields (n)
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")
366 (if (eolp)
367 (error "Line has too few fields: %s"
368 (buffer-substring
369 (line-beginning-position)
370 (line-end-position)))))
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)
380 (error "Line has too few fields: %s"
381 (buffer-substring
382 (line-beginning-position)
383 (line-end-position))))
384 ;; Position at the front of the field
385 ;; even if moving backwards.
386 (skip-chars-backward "^ \t\n")))
388 (defvar sort-regexp-fields-regexp)
389 (defvar sort-regexp-record-end)
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)))
405 (goto-char (match-beginning 0)))))
407 ;;;###autoload
408 (defun sort-regexp-fields (reverse record-regexp key-regexp beg end)
409 "Sort the text in the region region lexicographically.
410 If called interactively, prompt for two regular expressions,
411 RECORD-REGEXP and KEY-REGEXP.
413 RECORD-REGEXP specifies the textual units to be sorted.
414 For example, to sort lines, RECORD-REGEXP would be \"^.*$\".
416 KEY-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.
425 With a negative prefix arg, sort in reverse order.
427 The variable `sort-fold-case' determines whether alphabetic case affects
428 the sort order.
430 For example: to sort lines in the region by the first word on each line
431 starting with the letter \"f\",
432 RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\\\=\\<f\\\\w*\\\\>\""
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.
436 (interactive "P\nsRegexp specifying records to sort: \n\
437 sRegexp 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))
446 (let (sort-regexp-record-end
447 (sort-regexp-fields-regexp record-regexp))
448 (re-search-forward sort-regexp-fields-regexp nil t)
449 (setq sort-regexp-record-end (point))
450 (goto-char (match-beginning 0))
451 (sort-subr reverse
452 'sort-regexp-fields-next-record
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 ()
464 (cons (match-beginning n)
465 (match-end n))
466 ;; if there was no such register
467 (error (throw 'key nil)))))))))))
470 (defvar sort-columns-subprocess t)
472 ;;;###autoload
473 (defun sort-columns (reverse &optional beg end)
474 "Sort lines in region alphabetically by a certain range of columns.
475 For the purpose of this command, the region BEG...END includes
476 the entire line that point is in and the entire line the mark is in.
477 The column positions of point and mark bound the range of columns to sort on.
478 A prefix argument means sort into REVERSE order.
479 The variable `sort-fold-case' determines whether alphabetic case affects
480 the sort order.
482 Note that `sort-columns' rejects text that contains tabs,
483 because tabs could be split across the specified columns
484 and it doesn't know how to handle that. Also, when possible,
485 it uses the `sort' utility program, which doesn't understand tabs.
486 Use \\[untabify] to convert tabs to spaces before sorting."
487 (interactive "P\nr")
488 (save-excursion
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)
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)
503 (error "sort-columns does not work with tabs -- use M-x untabify"))
504 (if (not (or (memq system-type '(windows-nt))
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)))))))
517 ;; Use the sort utility if we can; it is 4 times as fast.
518 ;; Do not use it if there are any non-font-lock properties
519 ;; in the region, since the sort utility would lose the
520 ;; properties. Tabs are used as field separator; on NetBSD,
521 ;; sort complains if "\n" is used as field separator.
522 (let ((sort-args (list (if reverse "-rt\t" "-t\t")
523 (format "-k1.%d,1.%d"
524 (1+ col-start)
525 (1+ col-end)))))
526 (when sort-fold-case
527 (push "-f" sort-args))
528 (apply #'call-process-region beg1 end1 "sort" t t nil sort-args))
529 ;; On ms-windows, use Emacs's own facilities.
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
535 #'(lambda () (move-to-column col-start) nil)
536 #'(lambda () (move-to-column col-end) nil))))))))
538 ;;;###autoload
539 (defun reverse-region (beg end)
540 "Reverse the order of lines in a region.
541 From 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)))))
570 ;;;###autoload
571 (defun delete-duplicate-lines (beg end &optional reverse adjacent keep-blanks
572 interactive)
573 "Delete all but one copy of any identical lines in the region.
574 Non-interactively, arguments BEG and END delimit the region.
575 Normally it searches forwards, keeping the first instance of
576 each identical line. If REVERSE is non-nil (interactively, with
577 a C-u prefix), it searches backwards and keeps the last instance of
578 each repeated line.
580 Identical lines need not be adjacent, unless the argument
581 ADJACENT is non-nil (interactively, with a C-u C-u prefix).
582 This is a more efficient mode of operation, and may be useful
583 on large regions that have already been sorted.
585 If the argument KEEP-BLANKS is non-nil (interactively, with a
586 C-u C-u C-u prefix), it retains repeated blank lines.
588 Returns the number of deleted lines. Interactively, or if INTERACTIVE
589 is non-nil, it also prints a message describing the number of deletions."
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))
596 (equal current-prefix-arg '(64))
597 t)))
598 (let ((lines (unless adjacent (make-hash-table :test 'equal)))
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)))
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))))))
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))
631 (provide 'sort)
633 ;;; sort.el ends here