4.4BSD/usr/src/contrib/emacs-18.57/lisp/sort.elc


(provide (quote sort))

(defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun) "\
General text sorting routine to divide buffer into records and sort them.
Arguments are REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN.

We consider this portion of the buffer to be divided 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 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.
The first record is assumed to start at the position of point when sort-subr
is called.

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

STARTKEYFUN may moves from the start of the record to the start of the key.
It may return either return a non-nil value to be used as the key, or
else the key will be 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.
ENDRECFUN may be nil if STARTKEYFUN returns a value or if it would be the
same as ENDRECFUN." (byte-code "!	
$!?Ƃ}#!!!L@@!<тH@@:G҂H\"g@@!ZՂf@@:eւf\"t!!
\"*!)" [sort-lists nextrecfun endrecfun startkeyfun endkeyfun old nil reverse t message "Finding sort keys..." sort-build-lists nreverse "Sorting records..." fboundp sortcar numberp < buffer-substring-lessp string< sort (lambda (a b) (byte-code "@	@W" [a b] 2)) (lambda (a b) (byte-code "@	@\"" [a b buffer-substring-lessp] 3)) (lambda (a b) (byte-code "@	@\"" [a b string<] 3)) "Reordering buffer..." sort-reorder-buffer "Reordering buffer... Done"] 15))

(defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun) (byte-code "m?u`ȍ:
-
!::!ljb:Q@
\"QA`\"ZB^
`EB?qq!," [sort-lists nil start-rec done key endrecfun nextrecfun t (byte-code "!)`
ʼn!!$	`B(	`\")" [startkeyfun start endkeyfun endrecfun done t funcall fboundp buffer-substring-lessp buffer-substring] 6) funcall equal] 8))

(defun sort-reorder-buffer (sort-lists old) (byte-code "eed
;dbp
@8#dbp
@8
@8#@8
AAdbp
#\"," [inhibit-quit t last min max sort-lists old insert-buffer-substring 1 2 delete-region] 7))

(defun sort-lines (reverse beg end) "\
Sort lines in region alphabetically; argument means descending order.
Called from a program, there are three arguments:
REVERSE (non-nil means reverse order), BEG and END (region to sort)." (interactive "P
r") (byte-code "È	\"eb
#)" [beg end reverse nil narrow-to-region sort-subr forward-line end-of-line] 5))

(defun sort-paragraphs (reverse beg end) "\
Sort paragraphs in region alphabetically; argument means descending order.
Called from a program, there are three arguments:
REVERSE (non-nil means reverse order), BEG and END (region to sort)." (interactive "P
r") (byte-code "È	\"eb
#)" [beg end reverse nil narrow-to-region sort-subr (lambda nil (byte-code "!" [skip-chars-forward "
 	"] 2)) forward-paragraph] 5))

(defun sort-pages (reverse beg end) "\
Sort pages in region alphabetically; argument means descending order.
Called from a program, there are three arguments:
REVERSE (non-nil means reverse order), BEG and END (region to sort)." (interactive "P
r") (byte-code "È	\"eb
#)" [beg end reverse nil narrow-to-region sort-subr (lambda nil (byte-code "!" [skip-chars-forward "
"] 2)) forward-page] 5))

(defvar sort-fields-syntax-table nil)

(if sort-fields-syntax-table nil (let ((table (make-syntax-table)) (i 0)) (while (< i 256) (modify-syntax-entry i "w" table) (setq i (1+ i))) (modify-syntax-entry 32 " " table) (modify-syntax-entry 9 " " table) (modify-syntax-entry 10 " " table) (setq sort-fields-syntax-table table)))

(defun sort-numeric-fields (field beg end) "\
Sort lines in region numerically by the ARGth field of each line.
Fields are separated by whitespace and numbered from 1 up.
Specified field must contain a number in each line of the region.
With a negative arg, sorts by the -ARG'th field, in reverse order.
Called from a program, there are three arguments:
FIELD, BEG and END.  BEG and END specify region to sort." (interactive "p
r") (byte-code "È	
%" [field beg end nil sort-fields-1 (lambda nil (byte-code "S!`!`)\"!" [field sort-skip-fields string-to-int buffer-substring skip-chars-forward "-0-9"] 6))] 6))

(defun sort-fields (field beg end) "\
Sort lines in region lexicographically by the ARGth field of each line.
Fields are separated by whitespace and numbered from 1 up.
With a negative arg, sorts by the -ARG'th field, in reverse order.
Called from a program, there are three arguments:
FIELD, BEG and END.  BEG and END specify region to sort." (interactive "p
r") (byte-code "È	
%" [field beg end nil sort-fields-1 (lambda nil (byte-code "S!" [field nil sort-skip-fields] 2)) (lambda nil (byte-code "!" [skip-chars-forward "^ 	
"] 2))] 6))

(defun sort-fields-1 (field beg end startkeyfun endkeyfun) (byte-code "	W 		[#̎\"eb
!%))*" [reverse field tbl beg end sort-fields-syntax-table startkeyfun endkeyfun 0 syntax-table max 1 ((byte-code "!" [tbl set-syntax-table] 2)) narrow-to-region set-syntax-table sort-subr forward-line end-of-line] 10))

(defun sort-skip-fields (n) (byte-code "!`)	!`VNJ `)\"\"!)" [eol n end-of-line 1 forward-word error "Line has too few fields: %s" buffer-substring beginning-of-line skip-chars-forward " 	"] 8))

(defun sort-regexp-fields (reverse record-regexp key-regexp beg end) "\
Sort the region lexicographically as specifed by RECORD-REGEXP and KEY.
RECORD-REGEXP specifies the textual units which should be sorted.
  For example, to sort lines RECORD-REGEXP would be \"^.*$\"
KEY specifies the part of each record (ie each match for RECORD-REGEXP)
  is to be used for sorting.
  If it is \"\\digit\" then the digit'th \"\\(...\\)\" match field from
  RECORD-REGEXP is used.
  If it is \"\\&\" then the whole record is used.
  Otherwise, it is a regular-expression for which to search within the record.
If a match for KEY is not found within a record then that record is ignored.

With a negative prefix arg sorts in reverse order.

For example: to sort lines in the region by the first word on each line
 starting with the letter \"f\",
 RECORD-REGEXP would be \"^.*$\" and KEY \"\\<f\\w*\\>\"" (interactive "P
sRegexp specifying records to sort: 
sRegexp specifying key within record: 
r") (byte-code "ƈ\"
\"ʉ$\"$HZ	
\"eb!`!b
$))" [key-regexp beg end sort-regexp-record-end record-regexp reverse nil equal "" "\\&" 0 string-match "\\`\\\\[1-9]\\'" 1 48 narrow-to-region re-search-forward match-beginning sort-subr (lambda nil (byte-code "#!!b" [record-regexp nil sort-regexp-record-end re-search-forward move match-end 0 match-beginning] 4)) (lambda nil (byte-code "b" [sort-regexp-record-end] 1)) (lambda nil (byte-code "	!	 	
#ʼn \"ˏ)" [n key-regexp sort-regexp-record-end t nil 0 numberp re-search-forward throw key (byte-code "!!!B!!\"" [n fboundp buffer-substring-lessp match-beginning match-end buffer-substring] 8) ((error (byte-code "\"" [nil throw key] 3)))] 6))] 11))

(defun sort-columns (reverse &optional beg end) "\
Sort lines in region alphabetically by a certain range of columns.
For the purpose of this command, the region includes
the entire line that point is in and the entire line the mark is in.
The column positions of point and mark bound the range of columns to sort on.
A prefix argument means sort into reverse order.

Note that sort-columns uses the sort utility program and therefore
cannot work on text containing TAB characters.  Use M-x untabify
to convert tabs to spaces before sorting." (interactive "P
r") (byte-code "Ɉ^bi `]bi `
^
]#J!	
[ӂ\\P
P&	.)" [beg1 end1 col-beg1 col-end1 col-start col-end beg end t nil reverse beginning-of-line forward-line search-backward "	" error "sort-columns does not work with tabs.  Use M-x untabify." call-process-region "sort" "-rt
" "-t
" "+0." "-0."] 15))