[net.emacs] sort-region function for GNU

neves@uwai.UUCP (12/12/85)

Here is a function I find very useful.  I use it to sort sections of
a file using a column as a key.  For example, I can sort a class file by
student name, number, or grade.
It works for version 17 of GNU Emacs.  If you are using version 16 do
a global replace of "dot" for "point".
----------------------------------------------------------------

(defun sort-region (point mark)
  "Replace the region by its sort.
  The sort key is the column between the point and mark.
  The region is the line with the point through the line with the mark." 
  (interactive "r")
  (save-excursion
    (let
        (temp region-start region-end col-start col-end)
      (if (<= point mark) nil
        (setq temp mark)
        (setq mark point)
        (setq point temp))
      (setq region-start (progn (goto-char point)(beginning-of-line)(point)))
      (setq region-end (progn (goto-char mark) (next-line 1)
                              (beginning-of-line)(point)))
      (setq col-start (- point region-start))
      (setq col-end (- mark
                       (progn (goto-char mark) (beginning-of-line) (point))))
      (if (>= col-end col-start) nil
        (setq temp col-start) (setq col-start col-end) (setq col-end temp))
      (call-process-region region-start region-end "sort" t t nil "-b"
                           (concat "+0." col-start)
                           (concat "-0." col-end)))))
-- 
David Neves
Computer Sciences Department
University of Wisconsin-Madison

Usenet:  {allegra,heurikon,ihnp4,seismo,uwm-evax}!uwvax!neves
Arpanet: neves@uwvax