1 ;; Commands to sort text in an Emacs buffer.
2 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;; Original version of most of this contributed by Howie Kaye
24 (defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun)
25 "General text sorting routine to divide buffer into records and sort them.
26 Arguments are REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN.
28 We consider this portion of the buffer to be divided into disjoint pieces
29 called sort records. A portion of each sort record (perhaps all of it)
30 is designated as the sort key. The records are rearranged in the buffer
31 in order by their sort keys. The records may or may not be contiguous.
33 Usually the records are rearranged in order of ascending sort key.
34 If REVERSE is non-nil, they are rearranged in order of descending sort key.
36 The next four arguments are functions to be called to move point
37 across a sort record. They will be called many times from within sort-subr.
39 NEXTRECFUN is called with point at the end of the previous record.
40 It moves point to the start of the next record.
41 It should move point to the end of the buffer if there are no more records.
42 The first record is assumed to start at the position of point when sort-subr
45 ENDRECFUN is is called with point within the record.
46 It should move point to the end of the record.
48 STARTKEYFUN may moves from the start of the record to the start of the key.
49 It may return either return a non-nil value to be used as the key, or
50 else the key will be the substring between the values of point after
51 STARTKEYFUNC and ENDKEYFUN are called.
53 ENDKEYFUN moves from the start of the sort key to the end of the sort key.
54 ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
57 (message "Finding sort keys...")
58 (let* ((sort-lists (sort-build-lists nextrecfun endrecfun
59 startkeyfun endkeyfun))
60 (old (reverse sort-lists)))
63 (or reverse (setq sort-lists (nreverse sort-lists)))
64 (message "Sorting records...")
66 (if (fboundp 'sortcar)
68 (cond ((numberp (car (car sort-lists)))
70 ((consp (car (car sort-lists)))
71 'buffer-substring-lessp)
75 (cond ((numberp (car (car sort-lists)))
78 (< (car a) (car b)))))
79 ((consp (car (car sort-lists)))
82 (buffer-substring-lessp (car a) (car b)))))
86 (string< (car a) (car b)))))))))
87 (if reverse (setq sort-lists (nreverse sort-lists)))
88 (message "Reordering buffer...")
89 (sort-reorder-buffer sort-lists old)))
90 (message "Reordering buffer... Done"))
93 ;; Parse buffer into records using the arguments as Lisp expressions;
94 ;; return a list of records. Each record looks like (KEY STARTPOS ENDPOS)
95 ;; where KEY is the sort key (a number or string),
96 ;; and STARTPOS and ENDPOS are the bounds of this record in the buffer.
98 ;; The records appear in the list lastmost first!
100 (defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun)
101 (let ((sort-lists ())
104 ;; Loop over sort records.
105 ;(goto-char (point-min)) -- it is the caller's responsibility to
106 ;arrange this if necessary
108 (setq start-rec (point)) ;save record start
110 ;; Get key value, or move to start of key.
111 (setq key (catch 'key
112 (or (and startkeyfun (funcall startkeyfun))
113 ;; If key was not returned as value,
114 ;; move to end of key and get key from the buffer.
115 (let ((start (point)))
116 (funcall (or endkeyfun
117 (prog1 endrecfun (setq done t))))
118 (if (fboundp 'buffer-substring-lessp)
120 (buffer-substring start (point)))))))
121 ;; Move to end of this record (start of next one, or end of buffer).
122 (cond ((prog1 done (setq done nil)))
123 (endrecfun (funcall endrecfun))
124 (nextrecfun (funcall nextrecfun) (setq done t)))
125 (if key (setq sort-lists (cons
126 ;; consing optimization in case in which key
127 ;; is same as record.
129 (equal (car key) start-rec)
130 (equal (cdr key) (point)))
132 (list key start-rec (point)))
134 (and (not done) nextrecfun (funcall nextrecfun)))
137 (defun sort-reorder-buffer (sort-lists old)
138 (let ((inhibit-quit t)
140 (min (point-min)) (max (point-max)))
141 ;; Make sure insertions done for reordering
142 ;; do not go after any markers at the end of the sorted region,
143 ;; by inserting a space to separate them.
144 (goto-char (point-max))
145 (insert-before-markers " ")
146 (narrow-to-region min (1- (point-max)))
148 (goto-char (point-max))
149 (insert-buffer-substring (current-buffer)
152 (goto-char (point-max))
153 (insert-buffer-substring (current-buffer)
154 (nth 1 (car sort-lists))
155 (nth 2 (car sort-lists)))
156 (setq last (nth 2 (car old))
157 sort-lists (cdr sort-lists)
159 (goto-char (point-max))
160 (insert-buffer-substring (current-buffer)
163 ;; Delete the original copy of the text.
164 (delete-region min max)
165 ;; Get rid of the separator " ".
166 (goto-char (point-max))
167 (narrow-to-region min (1+ (point)))
168 (delete-region (point) (1+ (point)))))
170 (defun sort-lines (reverse beg end)
171 "Sort lines in region alphabetically; argument means descending order.
172 Called from a program, there are three arguments:
173 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
177 (narrow-to-region beg end)
178 (goto-char (point-min))
179 (sort-subr reverse 'forward-line 'end-of-line))))
181 (defun sort-paragraphs (reverse beg end)
182 "Sort paragraphs in region alphabetically; argument means descending order.
183 Called from a program, there are three arguments:
184 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
188 (narrow-to-region beg end)
189 (goto-char (point-min))
191 (function (lambda () (skip-chars-forward "\n \t\f")))
192 'forward-paragraph))))
194 (defun sort-pages (reverse beg end)
195 "Sort pages 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)."
201 (narrow-to-region beg end)
202 (goto-char (point-min))
204 (function (lambda () (skip-chars-forward "\n")))
207 (defvar sort-fields-syntax-table nil)
208 (if sort-fields-syntax-table nil
209 (let ((table (make-syntax-table))
212 (modify-syntax-entry i "w" table)
214 (modify-syntax-entry ?\ " " table)
215 (modify-syntax-entry ?\t " " table)
216 (modify-syntax-entry ?\n " " table)
217 (modify-syntax-entry ?\. "_" table) ; for floating pt. numbers. -wsr
218 (setq sort-fields-syntax-table table)))
220 (defun sort-numeric-fields (field beg end)
221 "Sort lines in region numerically by the ARGth field of each line.
222 Fields are separated by whitespace and numbered from 1 up.
223 Specified field must contain a number in each line of the region.
224 With a negative arg, sorts by the -ARG'th field, in decending order.
225 Called from a program, there are three arguments:
226 FIELD, BEG and END. BEG and END specify region to sort."
228 (sort-fields-1 field beg end
230 (sort-skip-fields (1- field))
235 ;; This is just wrong! Even without floats...
236 ;; (skip-chars-forward "[0-9]")
241 (defun sort-fields (field beg end)
242 "Sort lines in region lexicographically by the ARGth field of each line.
243 Fields are separated by whitespace and numbered from 1 up.
244 With a negative arg, sorts by the -ARG'th field, in decending order.
245 Called from a program, there are three arguments:
246 FIELD, BEG and END. BEG and END specify region to sort."
248 (sort-fields-1 field beg end
250 (sort-skip-fields (1- field))
252 (function (lambda () (skip-chars-forward "^ \t\n")))))
254 (defun sort-fields-1 (field beg end startkeyfun endkeyfun)
255 (let ((reverse (< field 0))
256 (tbl (syntax-table)))
257 (setq field (max 1 field (- field)))
261 (narrow-to-region beg end)
262 (goto-char (point-min))
263 (set-syntax-table sort-fields-syntax-table)
265 'forward-line 'end-of-line
266 startkeyfun endkeyfun)))
267 (set-syntax-table tbl))))
269 (defun sort-skip-fields (n)
270 (let ((eol (save-excursion (end-of-line 1) (point))))
273 (error "Line has too few fields: %s"
274 (buffer-substring (save-excursion
275 (beginning-of-line) (point))
277 (skip-chars-forward " \t")))
280 (defun sort-regexp-fields (reverse record-regexp key-regexp beg end)
281 "Sort the region lexicographically as specifed by RECORD-REGEXP and KEY.
282 RECORD-REGEXP specifies the textual units which should be sorted.
283 For example, to sort lines RECORD-REGEXP would be \"^.*$\"
284 KEY specifies the part of each record (ie each match for RECORD-REGEXP)
285 is to be used for sorting.
286 If it is \"\\digit\" then the digit'th \"\\(...\\)\" match field from
287 RECORD-REGEXP is used.
288 If it is \"\\&\" then the whole record is used.
289 Otherwise, it is a regular-expression for which to search within the record.
290 If a match for KEY is not found within a record then that record is ignored.
292 With a negative prefix arg sorts in reverse order.
294 For example: to sort lines in the region by the first word on each line
295 starting with the letter \"f\",
296 RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\=\\<f\\w*\\>\""
297 (interactive "P\nsRegexp specifying records to sort:
298 sRegexp specifying key within record: \nr")
299 (cond ((or (equal key-regexp "") (equal key-regexp "\\&"))
301 ((string-match "\\`\\\\[1-9]\\'" key-regexp)
302 (setq key-regexp (- (aref key-regexp 1) ?0))))
305 (narrow-to-region beg end)
306 (goto-char (point-min))
307 (let (sort-regexp-record-end) ;isn't dynamic scoping wonderful?
308 (re-search-forward record-regexp)
309 (setq sort-regexp-record-end (point))
310 (goto-char (match-beginning 0))
313 (and (re-search-forward record-regexp nil 'move)
314 (setq sort-regexp-record-end (match-end 0))
315 (goto-char (match-beginning 0)))))
317 (goto-char sort-regexp-record-end)))
320 (cond ((numberp key-regexp)
323 key-regexp sort-regexp-record-end t)
325 (t (throw 'key nil)))
327 (if (fboundp 'buffer-substring-lessp)
328 (cons (match-beginning n)
330 (buffer-substring (match-beginning n)
332 ;; if there was no such register
333 (error (throw 'key nil)))))))))))
336 (defvar sort-columns-subprocess t)
338 (defun sort-columns (reverse &optional beg end)
339 "Sort lines in region alphabetically by a certain range of columns.
340 For the purpose of this command, the region includes
341 the entire line that point is in and the entire line the mark is in.
342 The column positions of point and mark bound the range of columns to sort on.
343 A prefix argument means sort into reverse order.
345 Note that `sort-columns' rejects text that contains tabs,
346 because tabs could be split across the specified columns
347 and it doesn't know how to handle that. Also, when possible,
348 it uses the `sort' utility program, which doesn't understand tabs.
349 Use \\[untabify] to convert tabs to spaces before sorting."
352 (let (beg1 end1 col-beg1 col-end1 col-start col-end)
353 (goto-char (min beg end))
354 (setq col-beg1 (current-column))
357 (goto-char (max beg end))
358 (setq col-end1 (current-column))
361 (setq col-start (min col-beg1 col-end1))
362 (setq col-end (max col-beg1 col-end1))
363 (if (search-backward "\t" beg1 t)
364 (error "sort-columns does not work with tabs. Use M-x untabify."))
365 (if (not (eq system-type 'vax-vms))
366 ;; Use the sort utility if we can; it is 4 times as fast.
367 (call-process-region beg1 end1 "sort" t t nil
368 (if reverse "-rt\n" "-t\n")
369 (concat "+0." col-start)
370 (concat "-0." col-end))
371 ;; On VMS, use Emacs's own facilities.
374 (narrow-to-region beg1 end1)
376 (sort-subr reverse 'forward-line 'end-of-line
377 (function (lambda () (move-to-column col-start) nil))
378 (function (lambda () (move-to-column col-end) nil)))))))))