1 ;;; sort.el --- commands to sort text in an Emacs buffer
3 ;; Copyright (C) 1986-1987, 1994-1995, 2001-2016 Free Software
7 ;; Maintainer: emacs-devel@gnu.org
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/>.
27 ;; This package provides the sorting facilities documented in the Emacs
33 "Commands to sort text in an Emacs buffer."
36 (defcustom sort-fold-case nil
37 "Non-nil if the buffer sort functions should ignore case."
40 ;;;###autoload(put 'sort-fold-case 'safe-local-variable 'booleanp)
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
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
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
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
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)))
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
))
97 (or reverse
(setq sort-lists
(nreverse sort-lists
)))
98 (if messages
(message "Sorting records..."))
102 `(lambda (a b
) (,predicate
(car a
) (car b
))))
103 ((numberp (car (car sort-lists
)))
105 ((consp (car (car sort-lists
)))
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"))))
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 ())
129 ;; Loop over sort records.
130 ;(goto-char (point-min)) -- it is the caller's responsibility to
131 ;arrange this if necessary
133 (setq start-rec
(point)) ;save record start
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
)))
149 ;; consing optimization in case in which key is same as record.
151 (equal (car key
) start-rec
)
152 (equal (cdr key
) (point)))
154 (cons key
(cons start-rec
(point))))
156 (and (not done
) nextrecfun
(funcall nextrecfun
)))
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
)
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.
172 (goto-char (point-max))
173 (insert-buffer-substring old-buffer
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
)
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
))))))
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
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
)))))
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
225 (narrow-to-region beg end
)
226 (goto-char (point-min))
230 (while (and (not (eobp)) (looking-at paragraph-separate
))
232 'forward-paragraph
))))
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
244 (narrow-to-region beg end
)
245 (goto-char (point-min))
247 (function (lambda () (skip-chars-forward "\n")))
250 (defvar sort-fields-syntax-table nil
)
251 (if sort-fields-syntax-table nil
252 (let ((table (make-syntax-table))
255 (modify-syntax-entry i
"w" table
)
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'."
267 ;;;###autoload(put 'sort-numeric-base 'safe-local-variable 'integerp)
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."
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
284 (sort-skip-fields field
)
285 (let* ((case-fold-search t
)
287 (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]")
288 (cond ((match-beginning 1)
289 (goto-char (match-end 1))
292 (goto-char (match-end 2))
295 (string-to-number (buffer-substring (point)
299 (or base sort-numeric-base
))))
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
310 ;; (interactive "p\nr")
311 ;; (sort-fields-1 field beg end
312 ;; (function (lambda ()
313 ;; (sort-skip-fields field)
318 ;; (re-search-forward
319 ;; "[+-]?[0-9]*\\.?[0-9]*\\([eE][+-]?[0-9]+\\)?")
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
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
337 (sort-skip-fields field
)
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))
347 (narrow-to-region beg end
)
348 (goto-char (point-min))
349 (set-syntax-table sort-fields-syntax-table
)
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)
359 ;; Skip across N - 1 fields.
362 (skip-chars-forward " \t")
363 (skip-chars-forward "^ \t\n")
365 (skip-chars-forward " \t")
367 (error "Line has too few fields: %s"
369 (line-beginning-position)
370 (line-end-position)))))
372 ;; Skip back across - N - 1 fields.
373 (let ((i (1- (- n
))))
375 (skip-chars-backward " \t")
376 (skip-chars-backward "^ \t\n")
378 (skip-chars-backward " \t"))
380 (error "Line has too few fields: %s"
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
)
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)))))
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
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
"\\&"))
440 ((string-match "\\`\\\\[1-9]\\'" key-regexp
)
441 (setq key-regexp
(- (aref key-regexp
1) ?
0))))
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))
452 'sort-regexp-fields-next-record
454 (goto-char sort-regexp-record-end
)))
457 (cond ((numberp key-regexp
)
460 key-regexp sort-regexp-record-end t
)
462 (t (throw 'key nil
)))
464 (cons (match-beginning n
)
466 ;; if there was no such register
467 (error (throw 'key nil
)))))))))))
470 (defvar sort-columns-subprocess t
)
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
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."
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))
496 (goto-char (max beg end
))
497 (setq col-end1
(current-column))
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
)
508 (setq plist
(text-properties-at pos
))
509 (setq fontified
(plist-get plist
'fontified
))
511 (unless (or (eq (car plist
) 'fontified
)
512 (and (eq (car plist
) 'face
)
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"
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.
532 (narrow-to-region beg1 end1
)
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
))))))))
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."
544 (let (mid) (setq mid end end beg beg mid
)))
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
549 (or (bolp) (forward-line 1))
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.
561 (setq ll
(cons (buffer-substring (point) (progn (end-of-line) (point)))
563 (setq do
(/= (point) end
))
564 (delete-region beg
(if do
(1+ (point)) (point))))
566 (insert (car ll
) "\n")
571 (defun delete-duplicate-lines (beg end
&optional reverse adjacent keep-blanks
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
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."
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))
598 (let ((lines (unless adjacent
(make-hash-table :test
'equal
)))
599 line prev-line first-line
601 (beg (copy-marker beg
))
602 (end (copy-marker end
)))
604 (goto-char (if reverse end beg
))
605 (if (and reverse
(bolp)) (forward-char -
1))
608 (and (< (point) end
) (not (eobp))))
609 (setq first-line
(and reverse
(or (<= (point) beg
) (bobp))))
610 (setq line
(buffer-substring-no-properties
611 (line-beginning-position) (line-end-position)))
612 (if (and keep-blanks
(string= "" line
))
614 (if (if adjacent
(equal line prev-line
) (gethash line lines
))
616 (delete-region (progn (forward-line 0) (point))
617 (progn (forward-line 1) (point)))
618 (if reverse
(forward-line -
1))
619 (setq count
(1+ count
)))
620 (if adjacent
(setq prev-line line
) (puthash line t lines
))
621 (forward-line (if reverse -
1 1))))))
625 (message "Deleted %d %sduplicate line%s%s"
627 (if adjacent
"adjacent " "")
628 (if (= count
1) "" "s")
629 (if reverse
" backward" "")))
634 ;;; sort.el ends here