[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
randomize-region.el
From: |
Joe Corneli |
Subject: |
randomize-region.el |
Date: |
Tue, 26 Apr 2005 15:29:10 -0500 |
;;; randomize-region.el --- randomize line order in a region
;; Copyright (C) 2005 Joe Corneli <address@hidden>
;; Copyright (C) 1986, 1987, 1993, 1994, 1995, 2003 Free Software Foundation,
Inc.
;; Time-stamp: <jac -- Tue Apr 26 15:30:27 CDT 2005>
;; This file is not part of GNU Emacs, but it is distributed under
;; the same terms as GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 2, or (at your
;; option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Uses copied & modified versions of `shuffle-vector'
;; from cookie1.el and `reverse-region' from sort.el
;;; Code:
(defun randomize-region (beg end)
(interactive "r")
(if (> beg end)
(let (mid) (setq mid end end beg beg mid)))
(save-excursion
;; put beg at the start of a line and end and the end of one --
;; the largest possible region which fits this criteria
(goto-char beg)
(or (bolp) (forward-line 1))
(setq beg (point))
(goto-char end)
;; the test for bolp is for those times when end is on an empty
;; line; it is probably not the case that the line should be
;; included in the reversal; it isn't difficult to add it
;; afterward.
(or (and (eolp) (not (bolp)))
(progn (forward-line -1) (end-of-line)))
(setq end (point-marker))
(let ((strs (shuffle-list
(split-string (buffer-substring-no-properties beg end)
"\n"))))
(delete-region beg end)
(dolist (str strs)
(insert (concat str "\n"))))))
(defun shuffle-list (list)
"Randomly permute the elements of LIST.
All permutations equally likely."
(let ((i 0)
j
temp
(len (length list)))
(while (< i len)
(setq j (+ i (random (- len i))))
(setq temp (nth i list))
(setcar (nthcdr i list) (nth j list))
(setcar (nthcdr j list) temp)
(setq i (1+ i))))
list)
;;; randomize-region.el
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- randomize-region.el,
Joe Corneli <=