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