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