config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

sort-words.el (2706B)


      1 ;;; sort-words.el --- Sort words in a selected region
      2 
      3 ;; Filename: sort-words.el
      4 ;; Description: Sort words in a selected region
      5 ;; Author: "Aleksandar Simic" <asimic@gmail.com>
      6 ;; License: BSD
      7 ;; Created: 2016-09-07
      8 ;; Version: 0.0.5
      9 ;; Homepage: http://github.org/dotemacs/sort-words.el
     10 ;; Keywords: tools
     11 
     12 ;;; This file is NOT part of GNU Emacs
     13 ;;
     14 ;; Copyright (c) 2016, Aleksandar Simic
     15 ;; All rights reserved.
     16 ;;
     17 ;; Redistribution and use in source and binary forms, with or without
     18 ;; modification, are permitted provided that the following conditions
     19 ;; are met:
     20 ;; 1. Redistributions of source code must retain the above copyright
     21 ;;    notice, this list of conditions and the following disclaimer.
     22 ;; 2. Redistributions in binary form must reproduce the above
     23 ;;    copyright notice, this list of conditions and the following
     24 ;;    disclaimer in the documentation and/or other materials provided
     25 ;;    with the distribution.
     26 ;;
     27 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     28 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     29 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     30 ;; FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     31 ;; COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     32 ;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     33 ;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     34 ;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35 ;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     36 ;; STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37 ;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     38 ;; OF THE POSSIBILITY OF SUCH DAMAGE.
     39 
     40 ;;; Commentary:
     41 ;;
     42 ;; Usage: select a region and then
     43 ;; M-x sort-words RET
     44 
     45 ;;; Code:
     46 
     47 (defun sort-words-in-region (start end)
     48   "Sort the words in a given region (START and END) and return them as a list."
     49    (sort (split-string (buffer-substring-no-properties start end)) #'string<))
     50 
     51 (defun sort-words-sorted (start end)
     52   "Sort the words in a given region (START and END) and return them as a string."
     53   (mapconcat 'identity (sort-words-in-region start end) " "))
     54 
     55 ;;;###autoload
     56 (defun sort-words (start end)
     57   "Sort words in region alphabetically.
     58 Then insert them replacing the existing region.
     59 START and END are boundries of the selected region."
     60   (interactive "r")
     61   (save-excursion
     62     (save-restriction
     63       (narrow-to-region start end)
     64       (let ((words (sort-words-sorted (point-min) (point-max))))
     65         (delete-region (point-min) (point-max))
     66         (goto-char (point-min))
     67         (insert words)))))
     68 
     69 (provide 'sort-words)
     70 
     71 ;;; sort-words.el ends here