annotate lisp/prim/sort.el @ 7:c153ca296910

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