1 ;;; sort.el --- commands to sort text in an Emacs buffer.
3 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;; This package provides the sorting facilities documented in the Emacs
32 (defun sort-subr (reverse nextrecfun endrecfun
&optional startkeyfun endkeyfun
)
33 "General text sorting routine to divide buffer into records and sort them.
34 Arguments are REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN.
36 We divide the accessible portion of the buffer into disjoint pieces
37 called sort records. A portion of each sort record (perhaps all of
38 it) is designated as the sort key. The records are rearranged in the
39 buffer in order by their sort keys. The records may or may not be
42 Usually the records are rearranged in order of ascending sort key.
43 If REVERSE is non-nil, they are rearranged in order of descending sort key.
45 The next four arguments are functions to be called to move point
46 across a sort record. They will be called many times from within sort-subr.
48 NEXTRECFUN is called with point at the end of the previous record.
49 It moves point to the start of the next record.
50 It should move point to the end of the buffer if there are no more records.
51 The first record is assumed to start at the position of point when sort-subr
54 ENDRECFUN is called with point within the record.
55 It should move point to the end of the record.
57 STARTKEYFUN moves from the start of the record to the start of the key.
58 It may return either a non-nil value to be used as the key, or
59 else the key is the substring between the values of point after
60 STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key
61 starts at the beginning of the record.
63 ENDKEYFUN moves from the start of the sort key to the end of the sort key.
64 ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
66 ;; Heuristically try to avoid messages if sorting a small amt of text.
67 (let ((messages (> (- (point-max) (point-min)) 50000)))
69 (if messages
(message "Finding sort keys..."))
70 (let* ((sort-lists (sort-build-lists nextrecfun endrecfun
71 startkeyfun endkeyfun
))
72 (old (reverse sort-lists
)))
75 (or reverse
(setq sort-lists
(nreverse sort-lists
)))
76 (if messages
(message "Sorting records..."))
78 (if (fboundp 'sortcar
)
80 (cond ((numberp (car (car sort-lists
)))
81 ;; This handles both ints and floats.
83 ((consp (car (car sort-lists
)))
86 (> 0 (compare-buffer-substrings
88 nil
(car b
) (cdr b
))))))
92 (cond ((numberp (car (car sort-lists
)))
95 (< (car a
) (car b
)))))
96 ((consp (car (car sort-lists
)))
99 (> 0 (compare-buffer-substrings
100 nil
(car (car a
)) (cdr (car a
))
101 nil
(car (car b
)) (cdr (car b
)))))))
105 (string< (car a
) (car b
)))))))))
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"))))
112 ;; Parse buffer into records using the arguments as Lisp expressions;
113 ;; return a list of records. Each record looks like (KEY STARTPOS . ENDPOS)
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.
117 ;; The records appear in the list lastmost first!
119 (defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun
)
120 (let ((sort-lists ())
123 ;; Loop over sort records.
124 ;(goto-char (point-min)) -- it is the caller's responsibility to
125 ;arrange this if necessary
127 (setq start-rec
(point)) ;save record start
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
))))
137 (cons start
(point))))))
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
)))
142 (if key
(setq sort-lists
(cons
143 ;; consing optimization in case in which key
144 ;; is same as record.
146 (equal (car key
) start-rec
)
147 (equal (cdr key
) (point)))
149 (cons key
(cons start-rec
(point))))
151 (and (not done
) nextrecfun
(funcall nextrecfun
)))
154 (defun sort-reorder-buffer (sort-lists old
)
155 (let ((inhibit-quit t
)
157 (min (point-min)) (max (point-max)))
158 ;; Make sure insertions done for reordering
159 ;; do not go after any markers at the end of the sorted region,
160 ;; by inserting a space to separate them.
161 (goto-char (point-max))
162 (insert-before-markers " ")
163 (narrow-to-region min
(1- (point-max)))
165 (goto-char (point-max))
166 (insert-buffer-substring (current-buffer)
169 (goto-char (point-max))
170 (insert-buffer-substring (current-buffer)
171 (nth 1 (car sort-lists
))
172 (cdr (cdr (car sort-lists
))))
173 (setq last
(cdr (cdr (car old
)))
174 sort-lists
(cdr sort-lists
)
176 (goto-char (point-max))
177 (insert-buffer-substring (current-buffer)
180 ;; Delete the original copy of the text.
181 (delete-region min max
)
182 ;; Get rid of the separator " ".
183 (goto-char (point-max))
184 (narrow-to-region min
(1+ (point)))
185 (delete-region (point) (1+ (point)))))
188 (defun sort-lines (reverse beg end
)
189 "Sort lines in region alphabetically; argument means descending order.
190 Called from a program, there are three arguments:
191 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
195 (narrow-to-region beg end
)
196 (goto-char (point-min))
197 (sort-subr reverse
'forward-line
'end-of-line
))))
200 (defun sort-paragraphs (reverse beg end
)
201 "Sort paragraphs 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)."
207 (narrow-to-region beg end
)
208 (goto-char (point-min))
210 (function (lambda () (skip-chars-forward "\n \t\f")))
211 'forward-paragraph
))))
214 (defun sort-pages (reverse beg end
)
215 "Sort pages in region alphabetically; argument means descending order.
216 Called from a program, there are three arguments:
217 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
221 (narrow-to-region beg end
)
222 (goto-char (point-min))
224 (function (lambda () (skip-chars-forward "\n")))
227 (defvar sort-fields-syntax-table nil
)
228 (if sort-fields-syntax-table nil
229 (let ((table (make-syntax-table))
232 (modify-syntax-entry i
"w" table
)
234 (modify-syntax-entry ?\
" " table
)
235 (modify-syntax-entry ?
\t " " table
)
236 (modify-syntax-entry ?
\n " " table
)
237 (modify-syntax-entry ?\.
"_" table
) ; for floating pt. numbers. -wsr
238 (setq sort-fields-syntax-table table
)))
241 (defun sort-numeric-fields (field beg end
)
242 "Sort lines in region numerically by the ARGth field of each line.
243 Fields are separated by whitespace and numbered from 1 up.
244 Specified field must contain a number in each line of the region.
245 With a negative arg, sorts by the ARGth field counted from the right.
246 Called from a program, there are three arguments:
247 FIELD, BEG and END. BEG and END specify region to sort.
248 If you want to sort floating-point numbers, try `sort-float-fields'."
250 (sort-fields-1 field beg end
252 (sort-skip-fields (1- field
))
257 ;; This is just wrong! Even without floats...
258 ;; (skip-chars-forward "[0-9]")
264 (defun sort-float-fields (field beg end
)
265 "Sort lines in region numerically by the ARGth field of each line.
266 Fields are separated by whitespace and numbered from 1 up. Specified field
267 must contain a floating point number in each line of the region. With a
268 negative arg, sorts by the ARGth field counted from the right. Called from a
269 program, there are three arguments: FIELD, BEG and END. BEG and END specify
272 (sort-fields-1 field beg end
274 (sort-skip-fields (1- field
))
280 "[+-]?[0-9]*\.?[0-9]*\\([eE][+-]?[0-9]+\\)?")
285 (defun sort-fields (field beg end
)
286 "Sort lines in region lexicographically by the ARGth field of each line.
287 Fields are separated by whitespace and numbered from 1 up.
288 With a negative arg, sorts by the ARGth field counted from the right.
289 Called from a program, there are three arguments:
290 FIELD, BEG and END. BEG and END specify region to sort."
292 (sort-fields-1 field beg end
294 (sort-skip-fields (1- field
))
296 (function (lambda () (skip-chars-forward "^ \t\n")))))
298 (defun sort-fields-1 (field beg end startkeyfun endkeyfun
)
299 (let ((tbl (syntax-table)))
300 (if (zerop field
) (setq field
1))
304 (narrow-to-region beg end
)
305 (goto-char (point-min))
306 (set-syntax-table sort-fields-syntax-table
)
308 'forward-line
'end-of-line
309 startkeyfun endkeyfun
)))
310 (set-syntax-table tbl
))))
312 (defun sort-skip-fields (n)
314 (eol (save-excursion (end-of-line 1) (point))))
315 (if (> n
0) (forward-word n
)
317 (forward-word (1+ n
)))
318 (if (or (and (>= (point) eol
) (> n
0))
319 ;; this is marginally wrong; if the first line of the sort
320 ;; at bob has the wrong number of fields the error won't be
321 ;; reported until the next short line.
322 (and (< (point) bol
) (< n
0)))
323 (error "Line has too few fields: %s"
324 (buffer-substring bol eol
)))
325 (skip-chars-forward " \t")))
329 (defun sort-regexp-fields (reverse record-regexp key-regexp beg end
)
330 "Sort the region lexicographically as specifed by RECORD-REGEXP and KEY.
331 RECORD-REGEXP specifies the textual units which should be sorted.
332 For example, to sort lines RECORD-REGEXP would be \"^.*$\"
333 KEY specifies the part of each record (ie each match for RECORD-REGEXP)
334 is to be used for sorting.
335 If it is \"\\digit\" then the digit'th \"\\(...\\)\" match field from
336 RECORD-REGEXP is used.
337 If it is \"\\&\" then the whole record is used.
338 Otherwise, it is a regular-expression for which to search within the record.
339 If a match for KEY is not found within a record then that record is ignored.
341 With a negative prefix arg sorts in reverse order.
343 For example: to sort lines in the region by the first word on each line
344 starting with the letter \"f\",
345 RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\=\\<f\\w*\\>\""
346 ;; using negative prefix arg to mean "reverse" is now inconsistent with
347 ;; other sort-.*fields functions but then again this was before, since it
348 ;; didn't use the magnitude of the arg to specify anything.
349 (interactive "P\nsRegexp specifying records to sort:
350 sRegexp specifying key within record: \nr")
351 (cond ((or (equal key-regexp
"") (equal key-regexp
"\\&"))
353 ((string-match "\\`\\\\[1-9]\\'" key-regexp
)
354 (setq key-regexp
(- (aref key-regexp
1) ?
0))))
357 (narrow-to-region beg end
)
358 (goto-char (point-min))
359 (let (sort-regexp-record-end) ;isn't dynamic scoping wonderful?
360 (re-search-forward record-regexp
)
361 (setq sort-regexp-record-end
(point))
362 (goto-char (match-beginning 0))
365 (and (re-search-forward record-regexp nil
'move
)
366 (setq sort-regexp-record-end
(match-end 0))
367 (goto-char (match-beginning 0)))))
369 (goto-char sort-regexp-record-end
)))
372 (cond ((numberp key-regexp
)
375 key-regexp sort-regexp-record-end t
)
377 (t (throw 'key nil
)))
379 (if (fboundp 'buffer-substring-lessp
)
380 (cons (match-beginning n
)
382 (buffer-substring (match-beginning n
)
384 ;; if there was no such register
385 (error (throw 'key nil
)))))))))))
388 (defvar sort-columns-subprocess t
)
391 (defun sort-columns (reverse &optional beg end
)
392 "Sort lines in region alphabetically by a certain range of columns.
393 For the purpose of this command, the region includes
394 the entire line that point is in and the entire line the mark is in.
395 The column positions of point and mark bound the range of columns to sort on.
396 A prefix argument means sort into reverse order.
398 Note that `sort-columns' rejects text that contains tabs,
399 because tabs could be split across the specified columns
400 and it doesn't know how to handle that. Also, when possible,
401 it uses the `sort' utility program, which doesn't understand tabs.
402 Use \\[untabify] to convert tabs to spaces before sorting."
405 (let (beg1 end1 col-beg1 col-end1 col-start col-end
)
406 (goto-char (min beg end
))
407 (setq col-beg1
(current-column))
410 (goto-char (max beg end
))
411 (setq col-end1
(current-column))
414 (setq col-start
(min col-beg1 col-end1
))
415 (setq col-end
(max col-beg1 col-end1
))
416 (if (search-backward "\t" beg1 t
)
417 (error "sort-columns does not work with tabs. Use M-x untabify."))
418 (if (not (eq system-type
'vax-vms
))
419 ;; Use the sort utility if we can; it is 4 times as fast.
420 (call-process-region beg1 end1
"sort" t t nil
421 (if reverse
"-rt\n" "-t\n")
422 (concat "+0." col-start
)
423 (concat "-0." col-end
))
424 ;; On VMS, use Emacs's own facilities.
427 (narrow-to-region beg1 end1
)
429 (sort-subr reverse
'forward-line
'end-of-line
430 (function (lambda () (move-to-column col-start
) nil
))
431 (function (lambda () (move-to-column col-end
) nil
)))))))))
434 (defun reverse-region (beg end
)
435 "Reverse the order of lines in a region.
436 From a program takes two point or marker arguments, BEG and END."
439 (let (mid) (setq mid end end beg beg mid
)))
441 ;; put beg at the start of a line and end and the end of one --
442 ;; the largest possible region which fits this criteria
444 (or (bolp) (forward-line 1))
447 ;; the test for bolp is for those times when end is on an empty line;
448 ;; it is probably not the case that the line should be included in the
449 ;; reversal; it isn't difficult to add it afterward.
450 (or (and (eolp) (not (bolp))) (progn (forward-line -
1) (end-of-line)))
451 (setq end
(point-marker))
452 ;; the real work. this thing cranks through memory on large regions.
456 (setq ll
(cons (buffer-substring (point) (progn (end-of-line) (point)))
458 (setq do
(/= (point) end
))
459 (delete-region beg
(if do
(1+ (point)) (point))))
461 (insert (car ll
) "\n")
467 ;;; sort.el ends here