annotate lisp/prim/sort.el @ 171:929b76928fce r20-3b12

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