;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/sort-incal.el (require 'dwim) (require 'erc) (require 'sort) (setq sort-fold-case t) (defun sort-region (&optional rev beg end) (interactive (cons current-prefix-arg (use-region))) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (sort-lines rev beg end)) (defun sort-paragraph (&optional rev) (interactive "P") (let ((beg (progn (start-of-paragraph-text) (point))) (end (progn (end-of-paragraph-text) (point)))) (sort-lines rev beg end))) (defun sort-year-field (&optional rev beg end) (interactive (cons current-prefix-arg (use-region))) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (sort-regexp-fields rev "^.*$" "[[:digit:]]\\{4\\}" beg end)) (defun sort-second-field (&optional beg end) (interactive (use-region)) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (sort-numeric-fields 2 beg end)) (defun sort-lines-len (&optional rev beg end) (interactive (cons current-prefix-arg (use-region))) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (save-mark-and-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (sort-subr rev #'forward-line #'end-of-line nil nil (lambda (a b) (> (- (cdr a) (car a)) (- (cdr b) (car b)))))))) (defun sort-lines-random (&optional beg end) (interactive (use-region)) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (save-mark-and-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (sort-subr nil #'forward-line #'end-of-line nil nil (lambda (_ __) (zerop (random 2))))))) (defun sort-lines-whole (&optional rev beg end) (interactive (cons current-prefix-arg (use-region))) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (save-mark-and-excursion (let ((srt (progn (goto-char beg) (pos-bol))) (fin (progn (goto-char end) (pos-eol)))) (sort-lines rev srt fin)))) (defun insert-string-list (strs) (when strs (insert (format "%s" (car strs))) (dolist (s (cdr strs)) (insert (format " %s" s))))) (defun sort-line-words (&optional beg end delim) (interactive `(,@(use-region) ,(and current-prefix-arg (read-string "delim: ")))) (or beg (setq beg (pos-bol))) (or end (setq end (pos-eol))) (let* ((str (buffer-substring-no-properties beg end)) (strs (split-string str delim t)) (sorted (erc-sort-strings strs))) (kill-region beg end) (if (not delim) (insert-string-list sorted) (dolist (s (nreverse (cdr (nreverse sorted)))) (insert (format "%s%s" s delim))) (insert (format "%s" (car (last sorted))))))) ;; sort me: a is just string test this ;; dash delim: this-is-just-a-test-string (defun reverse-region-chars (&optional beg end) "Shuffle chars in region from BEG to END." (interactive (use-region)) (or beg (point-min)) (or end (point-max)) (save-mark-and-excursion (let ((str (buffer-substring-no-properties beg end))) (delete-region beg end) (insert (reverse str))))) (defun scramble (&optional beg end) "Shuffle chars in region from BEG to END." (interactive (use-region)) (or beg (point-min)) (or end (point-max)) (save-mark-and-excursion (let* ((str (buffer-substring-no-properties beg end)) (chrs (split-string str "" t)) (rand (sort chrs (lambda (_ __) (zerop (random 2)))))) (delete-region beg end) (dolist (c rand) (insert c))))) (provide 'sort-incal)