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