From eafd96e67b157fe35de497bb0e2617171c55d879 Mon Sep 17 00:00:00 2001 From: Arthur Gleckler Date: Sun, 1 Nov 1992 07:11:48 +0000 Subject: [PATCH] Initial revision --- v7/src/edwin/sort.scm | 222 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 v7/src/edwin/sort.scm diff --git a/v7/src/edwin/sort.scm b/v7/src/edwin/sort.scm new file mode 100644 index 000000000..282814283 --- /dev/null +++ b/v7/src/edwin/sort.scm @@ -0,0 +1,222 @@ +;;; -*-Scheme-*- +;;; +;;; $Id: sort.scm,v 1.1 1992/11/01 07:11:48 arthur Exp $ +;;; +;;; Copyright (c) 1992 Massachusetts Institute of Technology +;;; +;;; This material was developed by the Scheme project at the +;;; Massachusetts Institute of Technology, Department of +;;; Electrical Engineering and Computer Science. Permission to +;;; copy this software, to redistribute it, and to use it for any +;;; purpose is granted, subject to the following restrictions and +;;; understandings. +;;; +;;; 1. Any copy made of this software must include this copyright +;;; notice in full. +;;; +;;; 2. Users of this software agree to make their best efforts (a) +;;; to return to the MIT Scheme project any improvements or +;;; extensions that they make, so that these may be included in +;;; future releases; and (b) to inform MIT of noteworthy uses of +;;; this software. +;;; +;;; 3. All materials developed as a consequence of the use of this +;;; software shall duly acknowledge such use, in accordance with +;;; the usual standards of acknowledging credit in academic +;;; research. +;;; +;;; 4. MIT has made no warrantee or representation that the +;;; operation of this software will be error-free, and MIT is +;;; under no obligation to provide any services, by way of +;;; maintenance, update, or otherwise. +;;; +;;; 5. In conjunction with products arising from the use of this +;;; material, there shall be no use of the name of the +;;; Massachusetts Institute of Technology nor of any adaptation +;;; thereof in any advertising, promotional, or sales literature +;;; without prior written consent from MIT in each case. +;;; +;;; NOTE: Parts of this program (Edwin) were created by translation +;;; from corresponding parts of GNU Emacs. Users should be aware that +;;; the GNU GENERAL PUBLIC LICENSE may apply to these parts. A copy +;;; of that license should have been included along with this file. +;;; + +;;;; Sorting + +(declare (usual-integrations)) + +(define (sort-region region reverse? + forward-record record-end + key-start key-end + compare) + (let* ((start (region-start region)) + (end (region-end region)) + (delete-end (mark-right-inserting-copy end)) + (unsorted-list + (identify-records region forward-record record-end)) + (sorted-list + (sort + unsorted-list + (let ((order (if reverse? + not + identity-procedure))) + (lambda (element1 element2) + (order + (let ((start1 (key-start (car element1))) + (start2 (key-start (car element2)))) + (compare start1 + (key-end start1) + start2 + (key-end start2))))))))) + (insert-reordered-region start end sorted-list unsorted-list) + (kill-string start delete-end))) + +(define (identify-records region forward-record record-end) + (let ((limit (region-end region))) + (let next-record ((start (region-start region))) + (if start + (let ((end (record-end start))) + (if (and end (mark< end limit)) + (cons (cons start end) + (next-record (forward-record end))) + (list (cons start (region-end region))))) + '())))) + +(define (insert-reordered-region start end sorted-list unsorted-list) + (let ((insert-mark (mark-left-inserting-copy end))) + (let next-element ((previous start) + (sorted-list sorted-list) + (unsorted-list unsorted-list)) + (if (not (null? sorted-list)) + (begin + (insert-string + (extract-string previous + (caar unsorted-list)) + insert-mark) + (insert-string + (extract-string (caar sorted-list) + (cdar sorted-list)) + insert-mark) + (next-element (cdar unsorted-list) + (cdr sorted-list) + (cdr unsorted-list))))))) + +(define (sort-textual-comparison start1 end1 start2 end2) + (stringnumber string1)) + (value2 (string->number string2))) + (if (or (not value1) (not value2)) + (string