1 |
;;; sort.el --- commands to sort text in an Emacs buffer |
;;; sort.el --- commands to sort text in an Emacs buffer |
2 |
|
|
3 |
;; Copyright (C) 1986, 1987, 1994, 1995 Free Software Foundation, Inc. |
;; Copyright (C) 1986, 1987, 1994, 1995, 2003 Free Software Foundation, Inc. |
4 |
|
|
5 |
;; Author: Howie Kaye |
;; Author: Howie Kaye |
6 |
;; Maintainer: FSF |
;; Maintainer: FSF |
42 |
;;;###autoload |
;;;###autoload |
43 |
(defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun) |
(defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun) |
44 |
"General text sorting routine to divide buffer into records and sort them. |
"General text sorting routine to divide buffer into records and sort them. |
|
Arguments are REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN. |
|
45 |
|
|
46 |
We divide the accessible portion of the buffer into disjoint pieces |
We divide the accessible portion of the buffer into disjoint pieces |
47 |
called sort records. A portion of each sort record (perhaps all of |
called sort records. A portion of each sort record (perhaps all of |
96 |
((consp (car (car sort-lists))) |
((consp (car (car sort-lists))) |
97 |
(function |
(function |
98 |
(lambda (a b) |
(lambda (a b) |
99 |
(> 0 (compare-buffer-substrings |
(> 0 (compare-buffer-substrings |
100 |
nil (car a) (cdr a) |
nil (car a) (cdr a) |
101 |
nil (car b) (cdr b)))))) |
nil (car b) (cdr b)))))) |
102 |
(t |
(t |
107 |
((consp (car (car sort-lists))) |
((consp (car (car sort-lists))) |
108 |
(function |
(function |
109 |
(lambda (a b) |
(lambda (a b) |
110 |
(> 0 (compare-buffer-substrings |
(> 0 (compare-buffer-substrings |
111 |
nil (car (car a)) (cdr (car a)) |
nil (car (car a)) (cdr (car a)) |
112 |
nil (car (car b)) (cdr (car b))))))) |
nil (car (car b)) (cdr (car b))))))) |
113 |
(t |
(t |
192 |
(set-buffer old-buffer) |
(set-buffer old-buffer) |
193 |
(let ((inhibit-quit t)) |
(let ((inhibit-quit t)) |
194 |
;; Make sure insertions done for reordering |
;; Make sure insertions done for reordering |
195 |
;; do not go after any markers at the end of the sorted region, |
;; saves any markers at the end of the sorted region, |
196 |
;; by inserting a space to separate them. |
;; by leaving the last character of the region. |
197 |
(goto-char max) |
(delete-region min (1- max)) |
198 |
(insert-before-markers " ") |
;; Now replace the one remaining old character with the sorted text. |
199 |
;; Delete the original copy of the text. |
(goto-char (point-min)) |
|
(delete-region min max) |
|
|
;; Now replace the separator " " with the sorted text. |
|
|
(goto-char (point-max)) |
|
200 |
(insert-buffer-substring temp-buffer) |
(insert-buffer-substring temp-buffer) |
201 |
(delete-region min (1+ min)))))) |
(delete-region max (1+ max)))))) |
202 |
|
|
203 |
;;;###autoload |
;;;###autoload |
204 |
(defun sort-lines (reverse beg end) |
(defun sort-lines (reverse beg end) |
205 |
"Sort lines in region alphabetically; argument means descending order. |
"Sort lines in region alphabetically; argument means descending order. |
206 |
Called from a program, there are three arguments: |
Called from a program, there are three arguments: |
207 |
REVERSE (non-nil means reverse order), BEG and END (region to sort). |
REVERSE (non-nil means reverse order), BEG and END (region to sort). |
425 |
;; using negative prefix arg to mean "reverse" is now inconsistent with |
;; using negative prefix arg to mean "reverse" is now inconsistent with |
426 |
;; other sort-.*fields functions but then again this was before, since it |
;; other sort-.*fields functions but then again this was before, since it |
427 |
;; didn't use the magnitude of the arg to specify anything. |
;; didn't use the magnitude of the arg to specify anything. |
428 |
(interactive "P\nsRegexp specifying records to sort: |
(interactive "P\nsRegexp specifying records to sort: |
429 |
sRegexp specifying key within record: \nr") |
sRegexp specifying key within record: \nr") |
430 |
(cond ((or (equal key-regexp "") (equal key-regexp "\\&")) |
(cond ((or (equal key-regexp "") (equal key-regexp "\\&")) |
431 |
(setq key-regexp 0)) |
(setq key-regexp 0)) |
437 |
(goto-char (point-min)) |
(goto-char (point-min)) |
438 |
(let (sort-regexp-record-end |
(let (sort-regexp-record-end |
439 |
(sort-regexp-fields-regexp record-regexp)) |
(sort-regexp-fields-regexp record-regexp)) |
440 |
(re-search-forward sort-regexp-fields-regexp) |
(re-search-forward sort-regexp-fields-regexp nil t) |
441 |
(setq sort-regexp-record-end (point)) |
(setq sort-regexp-record-end (point)) |
442 |
(goto-char (match-beginning 0)) |
(goto-char (match-beginning 0)) |
443 |
(sort-subr reverse |
(sort-subr reverse |