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