70
|
1 ;; Commands to sort text in an Emacs buffer.
|
|
2 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; This file is part of GNU Emacs.
|
|
5
|
|
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)
|
|
9 ;; any later version.
|
|
10
|
|
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.
|
|
15
|
|
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.
|
|
19
|
|
20 ;; Original version of most of this contributed by Howie Kaye
|
|
21
|
|
22 (defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun)
|
|
23 "General text sorting routine to divide buffer into records and sort them.
|
|
24 Arguments are REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN.
|
|
25
|
|
26 We consider this portion of the buffer to be divided into disjoint pieces
|
|
27 called sort records. A portion of each sort record (perhaps all of it)
|
|
28 is designated as the sort key. The records are rearranged in the buffer
|
|
29 in order by their sort keys. The records may or may not be contiguous.
|
|
30
|
|
31 Usually the records are rearranged in order of ascending sort key.
|
|
32 If REVERSE is non-nil, they are rearranged in order of descending sort key.
|
|
33
|
|
34 The next four arguments are functions to be called to move point
|
|
35 across a sort record. They will be called many times from within sort-subr.
|
|
36
|
|
37 NEXTRECFUN is called with point at the end of the previous record.
|
|
38 It moves point to the start of the next record.
|
|
39 It should move point to the end of the buffer if there are no more records.
|
|
40 The first record is assumed to start at the position of point when sort-subr
|
|
41 is called.
|
|
42
|
|
43 ENDRECFUN is is called with point within the record.
|
|
44 It should move point to the end of the record.
|
|
45
|
|
46 STARTKEYFUN may moves from the start of the record to the start of the key.
|
|
47 It may return either return a non-nil value to be used as the key, or
|
|
48 else the key will be the substring between the values of point after
|
135
|
49 STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key
|
|
50 starts at the beginning of the record.
|
70
|
51
|
|
52 ENDKEYFUN moves from the start of the sort key to the end of the sort key.
|
|
53 ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
|
|
54 same as ENDRECFUN."
|
|
55 (save-excursion
|
|
56 (message "Finding sort keys...")
|
|
57 (let* ((sort-lists (sort-build-lists nextrecfun endrecfun
|
|
58 startkeyfun endkeyfun))
|
|
59 (old (reverse sort-lists)))
|
|
60 (if (null sort-lists)
|
|
61 ()
|
|
62 (or reverse (setq sort-lists (nreverse sort-lists)))
|
|
63 (message "Sorting records...")
|
|
64 (setq sort-lists
|
|
65 (if (fboundp 'sortcar)
|
|
66 (sortcar sort-lists
|
135
|
67 (cond ((numberp (car (car sort-lists)))
|
|
68 ;; This handles both ints and floats.
|
70
|
69 '<)
|
|
70 ((consp (car (car sort-lists)))
|
|
71 'buffer-substring-lessp)
|
|
72 (t
|
|
73 'string<)))
|
|
74 (sort sort-lists
|
135
|
75 (cond ((numberp (car (car sort-lists)))
|
70
|
76 (function
|
|
77 (lambda (a b)
|
|
78 (< (car a) (car b)))))
|
|
79 ((consp (car (car sort-lists)))
|
|
80 (function
|
|
81 (lambda (a b)
|
|
82 (buffer-substring-lessp (car a) (car b)))))
|
|
83 (t
|
|
84 (function
|
|
85 (lambda (a 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"))
|
|
91 nil)
|
|
92
|
|
93 ;; Parse buffer into records using the arguments as Lisp expressions;
|
136
|
94 ;; return a list of records. Each record looks like (KEY STARTPOS . ENDPOS)
|
70
|
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.
|
|
97
|
|
98 ;; The records appear in the list lastmost first!
|
|
99
|
|
100 (defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun)
|
|
101 (let ((sort-lists ())
|
|
102 (start-rec nil)
|
|
103 done key)
|
|
104 ;; Loop over sort records.
|
|
105 ;(goto-char (point-min)) -- it is the caller's responsibility to
|
|
106 ;arrange this if necessary
|
|
107 (while (not (eobp))
|
|
108 (setq start-rec (point)) ;save record start
|
|
109 (setq done nil)
|
|
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)
|
|
119 (cons start (point))
|
|
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.
|
|
128 (if (and (consp key)
|
|
129 (equal (car key) start-rec)
|
|
130 (equal (cdr key) (point)))
|
|
131 (cons key key)
|
135
|
132 (cons key (cons start-rec (point))))
|
|
133 sort-lists)))
|
70
|
134 (and (not done) nextrecfun (funcall nextrecfun)))
|
|
135 sort-lists))
|
|
136
|
|
137 (defun sort-reorder-buffer (sort-lists old)
|
|
138 (let ((inhibit-quit t)
|
|
139 (last (point-min))
|
|
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)))
|
|
147 (while sort-lists
|
|
148 (goto-char (point-max))
|
|
149 (insert-buffer-substring (current-buffer)
|
|
150 last
|
|
151 (nth 1 (car old)))
|
|
152 (goto-char (point-max))
|
|
153 (insert-buffer-substring (current-buffer)
|
|
154 (nth 1 (car sort-lists))
|
135
|
155 (cdr (cdr (car sort-lists))))
|
|
156 (setq last (cdr (cdr (car old)))
|
70
|
157 sort-lists (cdr sort-lists)
|
|
158 old (cdr old)))
|
|
159 (goto-char (point-max))
|
|
160 (insert-buffer-substring (current-buffer)
|
|
161 last
|
|
162 max)
|
|
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)))))
|
|
169
|
258
|
170 ;;;###autoload
|
70
|
171 (defun sort-lines (reverse beg end)
|
|
172 "Sort lines in region alphabetically; argument means descending order.
|
|
173 Called from a program, there are three arguments:
|
|
174 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
|
|
175 (interactive "P\nr")
|
|
176 (save-excursion
|
|
177 (save-restriction
|
|
178 (narrow-to-region beg end)
|
|
179 (goto-char (point-min))
|
|
180 (sort-subr reverse 'forward-line 'end-of-line))))
|
|
181
|
258
|
182 ;;;###autoload
|
70
|
183 (defun sort-paragraphs (reverse beg end)
|
|
184 "Sort paragraphs in region alphabetically; argument means descending order.
|
|
185 Called from a program, there are three arguments:
|
|
186 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
|
|
187 (interactive "P\nr")
|
|
188 (save-excursion
|
|
189 (save-restriction
|
|
190 (narrow-to-region beg end)
|
|
191 (goto-char (point-min))
|
|
192 (sort-subr reverse
|
|
193 (function (lambda () (skip-chars-forward "\n \t\f")))
|
|
194 'forward-paragraph))))
|
|
195
|
258
|
196 ;;;###autoload
|
70
|
197 (defun sort-pages (reverse beg end)
|
|
198 "Sort pages in region alphabetically; argument means descending order.
|
|
199 Called from a program, there are three arguments:
|
|
200 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
|
|
201 (interactive "P\nr")
|
|
202 (save-excursion
|
|
203 (save-restriction
|
|
204 (narrow-to-region beg end)
|
|
205 (goto-char (point-min))
|
|
206 (sort-subr reverse
|
|
207 (function (lambda () (skip-chars-forward "\n")))
|
|
208 'forward-page))))
|
|
209
|
|
210 (defvar sort-fields-syntax-table nil)
|
|
211 (if sort-fields-syntax-table nil
|
|
212 (let ((table (make-syntax-table))
|
|
213 (i 0))
|
|
214 (while (< i 256)
|
|
215 (modify-syntax-entry i "w" table)
|
|
216 (setq i (1+ i)))
|
|
217 (modify-syntax-entry ?\ " " table)
|
|
218 (modify-syntax-entry ?\t " " table)
|
|
219 (modify-syntax-entry ?\n " " table)
|
|
220 (modify-syntax-entry ?\. "_" table) ; for floating pt. numbers. -wsr
|
|
221 (setq sort-fields-syntax-table table)))
|
|
222
|
258
|
223 ;;;###autoload
|
70
|
224 (defun sort-numeric-fields (field beg end)
|
|
225 "Sort lines in region numerically by the ARGth field of each line.
|
|
226 Fields are separated by whitespace and numbered from 1 up.
|
|
227 Specified field must contain a number in each line of the region.
|
86
|
228 With a negative arg, sorts by the ARGth field counted from the right.
|
70
|
229 Called from a program, there are three arguments:
|
|
230 FIELD, BEG and END. BEG and END specify region to sort."
|
|
231 (interactive "p\nr")
|
|
232 (sort-fields-1 field beg end
|
|
233 (function (lambda ()
|
|
234 (sort-skip-fields (1- field))
|
|
235 (string-to-int
|
|
236 (buffer-substring
|
|
237 (point)
|
|
238 (save-excursion
|
|
239 ;; This is just wrong! Even without floats...
|
|
240 ;; (skip-chars-forward "[0-9]")
|
|
241 (forward-sexp 1)
|
|
242 (point))))))
|
|
243 nil))
|
|
244
|
86
|
245 (defun sort-float-fields (field beg end)
|
|
246 "Sort lines in region numerically by the ARGth field of each line.
|
|
247 Fields are separated by whitespace and numbered from 1 up. Specified field
|
|
248 must contain a floating point number in each line of the region. With a
|
|
249 negative arg, sorts by the ARGth field counted from the right. Called from a
|
|
250 program, there are three arguments: FIELD, BEG and END. BEG and END specify
|
|
251 region to sort."
|
|
252 (interactive "p\nr")
|
|
253 (sort-fields-1 field beg end
|
|
254 (function (lambda ()
|
|
255 (sort-skip-fields (1- field))
|
|
256 (string-to-float
|
|
257 (buffer-substring
|
|
258 (point)
|
|
259 (save-excursion
|
|
260 (re-search-forward
|
|
261 "[+-]?[0-9]*\.?[0-9]*\\([eE][+-]?[0-9]+\\)?")
|
|
262 (point))))))
|
|
263 nil))
|
|
264
|
258
|
265 ;;;###autoload
|
70
|
266 (defun sort-fields (field beg end)
|
|
267 "Sort lines in region lexicographically by the ARGth field of each line.
|
|
268 Fields are separated by whitespace and numbered from 1 up.
|
86
|
269 With a negative arg, sorts by the ARGth field counted from the right.
|
70
|
270 Called from a program, there are three arguments:
|
|
271 FIELD, BEG and END. BEG and END specify region to sort."
|
|
272 (interactive "p\nr")
|
|
273 (sort-fields-1 field beg end
|
|
274 (function (lambda ()
|
|
275 (sort-skip-fields (1- field))
|
|
276 nil))
|
|
277 (function (lambda () (skip-chars-forward "^ \t\n")))))
|
|
278
|
|
279 (defun sort-fields-1 (field beg end startkeyfun endkeyfun)
|
86
|
280 (let ((tbl (syntax-table)))
|
|
281 (if (zerop field) (setq field 1))
|
70
|
282 (unwind-protect
|
|
283 (save-excursion
|
|
284 (save-restriction
|
|
285 (narrow-to-region beg end)
|
|
286 (goto-char (point-min))
|
|
287 (set-syntax-table sort-fields-syntax-table)
|
86
|
288 (sort-subr nil
|
70
|
289 'forward-line 'end-of-line
|
|
290 startkeyfun endkeyfun)))
|
|
291 (set-syntax-table tbl))))
|
|
292
|
|
293 (defun sort-skip-fields (n)
|
86
|
294 (let ((bol (point))
|
|
295 (eol (save-excursion (end-of-line 1) (point))))
|
|
296 (if (> n 0) (forward-word n)
|
|
297 (end-of-line)
|
|
298 (forward-word (1+ n)))
|
|
299 (if (or (and (>= (point) eol) (> n 0))
|
|
300 ;; this is marginally wrong; if the first line of the sort
|
|
301 ;; at bob has the wrong number of fields the error won't be
|
|
302 ;; reported until the next short line.
|
|
303 (and (< (point) bol) (< n 0)))
|
70
|
304 (error "Line has too few fields: %s"
|
86
|
305 (buffer-substring bol eol)))
|
70
|
306 (skip-chars-forward " \t")))
|
|
307
|
|
308
|
258
|
309 ;;;###autoload
|
70
|
310 (defun sort-regexp-fields (reverse record-regexp key-regexp beg end)
|
|
311 "Sort the region lexicographically as specifed by RECORD-REGEXP and KEY.
|
|
312 RECORD-REGEXP specifies the textual units which should be sorted.
|
|
313 For example, to sort lines RECORD-REGEXP would be \"^.*$\"
|
|
314 KEY specifies the part of each record (ie each match for RECORD-REGEXP)
|
|
315 is to be used for sorting.
|
|
316 If it is \"\\digit\" then the digit'th \"\\(...\\)\" match field from
|
|
317 RECORD-REGEXP is used.
|
|
318 If it is \"\\&\" then the whole record is used.
|
|
319 Otherwise, it is a regular-expression for which to search within the record.
|
|
320 If a match for KEY is not found within a record then that record is ignored.
|
|
321
|
|
322 With a negative prefix arg sorts in reverse order.
|
|
323
|
|
324 For example: to sort lines in the region by the first word on each line
|
|
325 starting with the letter \"f\",
|
|
326 RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\=\\<f\\w*\\>\""
|
86
|
327 ;; using negative prefix arg to mean "reverse" is now inconsistent with
|
|
328 ;; other sort-.*fields functions but then again this was before, since it
|
|
329 ;; didn't use the magnitude of the arg to specify anything.
|
70
|
330 (interactive "P\nsRegexp specifying records to sort:
|
|
331 sRegexp specifying key within record: \nr")
|
|
332 (cond ((or (equal key-regexp "") (equal key-regexp "\\&"))
|
|
333 (setq key-regexp 0))
|
|
334 ((string-match "\\`\\\\[1-9]\\'" key-regexp)
|
|
335 (setq key-regexp (- (aref key-regexp 1) ?0))))
|
|
336 (save-excursion
|
|
337 (save-restriction
|
|
338 (narrow-to-region beg end)
|
|
339 (goto-char (point-min))
|
|
340 (let (sort-regexp-record-end) ;isn't dynamic scoping wonderful?
|
|
341 (re-search-forward record-regexp)
|
|
342 (setq sort-regexp-record-end (point))
|
|
343 (goto-char (match-beginning 0))
|
|
344 (sort-subr reverse
|
|
345 (function (lambda ()
|
|
346 (and (re-search-forward record-regexp nil 'move)
|
|
347 (setq sort-regexp-record-end (match-end 0))
|
|
348 (goto-char (match-beginning 0)))))
|
|
349 (function (lambda ()
|
|
350 (goto-char sort-regexp-record-end)))
|
|
351 (function (lambda ()
|
|
352 (let ((n 0))
|
|
353 (cond ((numberp key-regexp)
|
|
354 (setq n key-regexp))
|
|
355 ((re-search-forward
|
|
356 key-regexp sort-regexp-record-end t)
|
|
357 (setq n 0))
|
|
358 (t (throw 'key nil)))
|
|
359 (condition-case ()
|
|
360 (if (fboundp 'buffer-substring-lessp)
|
|
361 (cons (match-beginning n)
|
|
362 (match-end n))
|
|
363 (buffer-substring (match-beginning n)
|
|
364 (match-end n)))
|
|
365 ;; if there was no such register
|
|
366 (error (throw 'key nil)))))))))))
|
|
367
|
|
368
|
|
369 (defvar sort-columns-subprocess t)
|
|
370
|
258
|
371 ;;;###autoload
|
70
|
372 (defun sort-columns (reverse &optional beg end)
|
|
373 "Sort lines in region alphabetically by a certain range of columns.
|
|
374 For the purpose of this command, the region includes
|
|
375 the entire line that point is in and the entire line the mark is in.
|
|
376 The column positions of point and mark bound the range of columns to sort on.
|
|
377 A prefix argument means sort into reverse order.
|
|
378
|
|
379 Note that `sort-columns' rejects text that contains tabs,
|
|
380 because tabs could be split across the specified columns
|
|
381 and it doesn't know how to handle that. Also, when possible,
|
|
382 it uses the `sort' utility program, which doesn't understand tabs.
|
|
383 Use \\[untabify] to convert tabs to spaces before sorting."
|
|
384 (interactive "P\nr")
|
|
385 (save-excursion
|
|
386 (let (beg1 end1 col-beg1 col-end1 col-start col-end)
|
|
387 (goto-char (min beg end))
|
|
388 (setq col-beg1 (current-column))
|
|
389 (beginning-of-line)
|
|
390 (setq beg1 (point))
|
|
391 (goto-char (max beg end))
|
|
392 (setq col-end1 (current-column))
|
|
393 (forward-line)
|
|
394 (setq end1 (point))
|
|
395 (setq col-start (min col-beg1 col-end1))
|
|
396 (setq col-end (max col-beg1 col-end1))
|
|
397 (if (search-backward "\t" beg1 t)
|
|
398 (error "sort-columns does not work with tabs. Use M-x untabify."))
|
|
399 (if (not (eq system-type 'vax-vms))
|
|
400 ;; Use the sort utility if we can; it is 4 times as fast.
|
|
401 (call-process-region beg1 end1 "sort" t t nil
|
|
402 (if reverse "-rt\n" "-t\n")
|
|
403 (concat "+0." col-start)
|
|
404 (concat "-0." col-end))
|
|
405 ;; On VMS, use Emacs's own facilities.
|
|
406 (save-excursion
|
|
407 (save-restriction
|
|
408 (narrow-to-region beg1 end1)
|
|
409 (goto-char beg1)
|
|
410 (sort-subr reverse 'forward-line 'end-of-line
|
|
411 (function (lambda () (move-to-column col-start) nil))
|
|
412 (function (lambda () (move-to-column col-end) nil)))))))))
|
86
|
413
|
258
|
414 ;;;###autoload
|
86
|
415 (defun reverse-region (beg end)
|
|
416 "Reverse the order of lines in a region.
|
|
417 From a program takes two point or marker arguments, BEG and END."
|
|
418 (interactive "r")
|
|
419 (if (> beg end)
|
|
420 (let (mid) (setq mid end end beg beg mid)))
|
|
421 (save-excursion
|
|
422 ;; put beg at the start of a line and end and the end of one --
|
|
423 ;; the largest possible region which fits this criteria
|
|
424 (goto-char beg)
|
|
425 (or (bolp) (forward-line 1))
|
|
426 (setq beg (point))
|
|
427 (goto-char end)
|
|
428 ;; the test for bolp is for those times when end is on an empty line;
|
|
429 ;; it is probably not the case that the line should be included in the
|
|
430 ;; reversal; it isn't difficult to add it afterward.
|
|
431 (or (and (eolp) (not (bolp))) (progn (forward-line -1) (end-of-line)))
|
|
432 (setq end (point-marker))
|
|
433 ;; the real work. this thing cranks through memory on large regions.
|
|
434 (let (ll (do t))
|
|
435 (while do
|
|
436 (goto-char beg)
|
|
437 (setq ll (cons (buffer-substring (point) (progn (end-of-line) (point)))
|
|
438 ll))
|
|
439 (setq do (/= (point) end))
|
|
440 (delete-region beg (if do (1+ (point)) (point))))
|
|
441 (while (cdr ll)
|
|
442 (insert (car ll) "\n")
|
|
443 (setq ll (cdr ll)))
|
|
444 (insert (car ll)))))
|
584
|
445
|
|
446 (provide 'sort)
|
|
447
|