diff options
| author | Stefan Monnier | 2003-06-02 21:19:38 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2003-06-02 21:19:38 +0000 |
| commit | e2292b24dbd9f23404b2a524599fbc5a44c9f9f5 (patch) | |
| tree | 242bf15976806836ddea21a899d4ec3c5c3e22d7 | |
| parent | 5435c793fa9db6220a22f5db916e10caaf1df6cd (diff) | |
| download | emacs-e2292b24dbd9f23404b2a524599fbc5a44c9f9f5.tar.gz emacs-e2292b24dbd9f23404b2a524599fbc5a44c9f9f5.zip | |
(sort-subr): Add `predicate' arg. Remove `sortcar' code.
| -rw-r--r-- | lisp/sort.el | 63 |
1 files changed, 26 insertions, 37 deletions
diff --git a/lisp/sort.el b/lisp/sort.el index f0b21cadaa4..59e076ecec0 100644 --- a/lisp/sort.el +++ b/lisp/sort.el | |||
| @@ -40,7 +40,8 @@ | |||
| 40 | :type 'boolean) | 40 | :type 'boolean) |
| 41 | 41 | ||
| 42 | ;;;###autoload | 42 | ;;;###autoload |
| 43 | (defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun) | 43 | (defun sort-subr (reverse nextrecfun endrecfun |
| 44 | &optional startkeyfun endkeyfun predicate) | ||
| 44 | "General text sorting routine to divide buffer into records and sort them. | 45 | "General text sorting routine to divide buffer into records and sort them. |
| 45 | 46 | ||
| 46 | We divide the accessible portion of the buffer into disjoint pieces | 47 | We divide the accessible portion of the buffer into disjoint pieces |
| @@ -74,7 +75,10 @@ starts at the beginning of the record. | |||
| 74 | 75 | ||
| 75 | ENDKEYFUN moves from the start of the sort key to the end of the sort key. | 76 | ENDKEYFUN moves from the start of the sort key to the end of the sort key. |
| 76 | ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the | 77 | ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the |
| 77 | same as ENDRECFUN." | 78 | same as ENDRECFUN. |
| 79 | |||
| 80 | PREDICATE is the function to use to compare keys. If keys are numbers, | ||
| 81 | it defaults to `<', otherwise it defaults to `string<'." | ||
| 78 | ;; Heuristically try to avoid messages if sorting a small amt of text. | 82 | ;; Heuristically try to avoid messages if sorting a small amt of text. |
| 79 | (let ((messages (> (- (point-max) (point-min)) 50000))) | 83 | (let ((messages (> (- (point-max) (point-min)) 50000))) |
| 80 | (save-excursion | 84 | (save-excursion |
| @@ -88,32 +92,18 @@ same as ENDRECFUN." | |||
| 88 | (or reverse (setq sort-lists (nreverse sort-lists))) | 92 | (or reverse (setq sort-lists (nreverse sort-lists))) |
| 89 | (if messages (message "Sorting records...")) | 93 | (if messages (message "Sorting records...")) |
| 90 | (setq sort-lists | 94 | (setq sort-lists |
| 91 | (if (fboundp 'sortcar) | 95 | (sort sort-lists |
| 92 | (sortcar sort-lists | 96 | (cond (predicate |
| 93 | (cond ((numberp (car (car sort-lists))) | 97 | `(lambda (a b) (,predicate (car a) (car b)))) |
| 94 | ;; This handles both ints and floats. | 98 | ((numberp (car (car sort-lists))) |
| 95 | '<) | 99 | 'car-less-than-car) |
| 96 | ((consp (car (car sort-lists))) | 100 | ((consp (car (car sort-lists))) |
| 97 | (function | 101 | (lambda (a b) |
| 98 | (lambda (a b) | 102 | (> 0 (compare-buffer-substrings |
| 99 | (> 0 (compare-buffer-substrings | 103 | nil (car (car a)) (cdr (car a)) |
| 100 | nil (car a) (cdr a) | 104 | nil (car (car b)) (cdr (car b)))))) |
| 101 | nil (car b) (cdr b)))))) | 105 | (t |
| 102 | (t | 106 | (lambda (a b) (string< (car a) (car b))))))) |
| 103 | 'string<))) | ||
| 104 | (sort sort-lists | ||
| 105 | (cond ((numberp (car (car sort-lists))) | ||
| 106 | 'car-less-than-car) | ||
| 107 | ((consp (car (car sort-lists))) | ||
| 108 | (function | ||
| 109 | (lambda (a b) | ||
| 110 | (> 0 (compare-buffer-substrings | ||
| 111 | nil (car (car a)) (cdr (car a)) | ||
| 112 | nil (car (car b)) (cdr (car b))))))) | ||
| 113 | (t | ||
| 114 | (function | ||
| 115 | (lambda (a b) | ||
| 116 | (string< (car a) (car b))))))))) | ||
| 117 | (if reverse (setq sort-lists (nreverse sort-lists))) | 107 | (if reverse (setq sort-lists (nreverse sort-lists))) |
| 118 | (if messages (message "Reordering buffer...")) | 108 | (if messages (message "Reordering buffer...")) |
| 119 | (sort-reorder-buffer sort-lists old))) | 109 | (sort-reorder-buffer sort-lists old))) |
| @@ -150,15 +140,14 @@ same as ENDRECFUN." | |||
| 150 | (cond ((prog1 done (setq done nil))) | 140 | (cond ((prog1 done (setq done nil))) |
| 151 | (endrecfun (funcall endrecfun)) | 141 | (endrecfun (funcall endrecfun)) |
| 152 | (nextrecfun (funcall nextrecfun) (setq done t))) | 142 | (nextrecfun (funcall nextrecfun) (setq done t))) |
| 153 | (if key (setq sort-lists (cons | 143 | (if key (push |
| 154 | ;; consing optimization in case in which key | 144 | ;; consing optimization in case in which key is same as record. |
| 155 | ;; is same as record. | 145 | (if (and (consp key) |
| 156 | (if (and (consp key) | 146 | (equal (car key) start-rec) |
| 157 | (equal (car key) start-rec) | 147 | (equal (cdr key) (point))) |
| 158 | (equal (cdr key) (point))) | 148 | (cons key key) |
| 159 | (cons key key) | 149 | (cons key (cons start-rec (point)))) |
| 160 | (cons key (cons start-rec (point)))) | 150 | sort-lists)) |
| 161 | sort-lists))) | ||
| 162 | (and (not done) nextrecfun (funcall nextrecfun))) | 151 | (and (not done) nextrecfun (funcall nextrecfun))) |
| 163 | sort-lists)) | 152 | sort-lists)) |
| 164 | 153 | ||