Function: sort-subr

sort-subr is an autoloaded and byte-compiled function defined in sort.el.gz.

Signature

(sort-subr REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN PREDICATE)

Documentation

General text sorting routine to divide buffer into records and sort them.

We divide the accessible portion of the buffer into disjoint pieces called sort records. A portion of each sort record (perhaps all of it) is designated as the sort key. The records are rearranged in the buffer in order by their sort keys. The records may or may not be contiguous.

Usually the records are rearranged in order of ascending sort key. If REVERSE is non-nil, they are rearranged in order of descending sort key. The variable sort-fold-case determines whether alphabetic case affects the sort order.

The next four arguments are functions to be called to move point across a sort record. They will be called many times from within sort-subr.

NEXTRECFUN is called with point at the end of the previous record. It moves point to the start of the next record. It should move point to the end of the buffer if there are no more records. The first record is assumed to start at the position of point when sort-subr is called.

ENDRECFUN is called with point within the record. It should move point to the end of the record.

STARTKEYFUN moves from the start of the record to the start of the key. It may return either a non-nil value to be used as the key, or else the key is the substring between the values of point after STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key starts at the beginning of the record.

ENDKEYFUN moves from the start of the sort key to the end of the sort key. ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the same as ENDRECFUN.

PREDICATE, if non-nil, is the predicate function for comparing keys; it is called with two arguments, the keys to compare, and should return non-nil if the first key should sort before the second key. If PREDICATE is nil, comparison is done with < if the keys are numbers, with compare-buffer-substrings if the keys are cons cells (the car and cdr of each cons cell are taken as start and end positions), and with string< otherwise.

View in manual

Source Code

;; Defined in /usr/src/emacs/lisp/sort.el.gz
;;;###autoload(put 'sort-fold-case 'safe-local-variable 'booleanp)

;;;###autoload
(defun sort-subr (reverse nextrecfun endrecfun
			  &optional startkeyfun endkeyfun predicate)
  "General text sorting routine to divide buffer into records and sort them.

We divide the accessible portion of the buffer into disjoint pieces
called sort records.  A portion of each sort record (perhaps all of
it) is designated as the sort key.  The records are rearranged in the
buffer in order by their sort keys.  The records may or may not be
contiguous.

Usually the records are rearranged in order of ascending sort key.
If REVERSE is non-nil, they are rearranged in order of descending sort key.
The variable `sort-fold-case' determines whether alphabetic case affects
the sort order.

The next four arguments are functions to be called to move point
across a sort record.  They will be called many times from within `sort-subr'.

NEXTRECFUN is called with point at the end of the previous record.
It moves point to the start of the next record.
It should move point to the end of the buffer if there are no more records.
The first record is assumed to start at the position of point when `sort-subr'
is called.

ENDRECFUN is called with point within the record.
It should move point to the end of the record.

STARTKEYFUN moves from the start of the record to the start of the key.
It may return either a non-nil value to be used as the key, or
else the key is the substring between the values of point after
STARTKEYFUN and ENDKEYFUN are called.  If STARTKEYFUN is nil, the key
starts at the beginning of the record.

ENDKEYFUN moves from the start of the sort key to the end of the sort key.
ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
same as ENDRECFUN.

PREDICATE, if non-nil, is the predicate function for comparing
keys; it is called with two arguments, the keys to compare, and
should return non-nil if the first key should sort before the
second key.  If PREDICATE is nil, comparison is done with `<' if
the keys are numbers, with `compare-buffer-substrings' if the
keys are cons cells (the car and cdr of each cons cell are taken
as start and end positions), and with `string<' otherwise."
  ;; Heuristically try to avoid messages if sorting a small amount of text.
  (let ((messages (> (- (point-max) (point-min)) 50000)))
    (save-excursion
      (if messages (message "Finding sort keys..."))
      (let* ((sort-lists (sort-build-lists nextrecfun endrecfun
					   startkeyfun endkeyfun))
	     (old (reverse sort-lists))
	     (case-fold-search sort-fold-case))
	(if (null sort-lists)
	    ()
	  (or reverse (setq sort-lists (nreverse sort-lists)))
	  (if messages (message "Sorting records..."))
	  (setq sort-lists
		(sort sort-lists
		      (cond (predicate
			     `(lambda (a b) (,predicate (car a) (car b))))
			    ((numberp (car (car sort-lists)))
			     'car-less-than-car)
			    ((consp (car (car sort-lists)))
			     (lambda (a b)
			       (> 0 (compare-buffer-substrings
				     nil (car (car a)) (cdr (car a))
				     nil (car (car b)) (cdr (car b))))))
			    (t
			     (lambda (a b) (string< (car a) (car b)))))))
	  (if reverse (setq sort-lists (nreverse sort-lists)))
	  (if messages (message "Reordering buffer..."))
          (with-buffer-unmodified-if-unchanged
	    (sort-reorder-buffer sort-lists old))))
      (if messages (message "Reordering buffer... Done"))))
  nil)