From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.devel Subject: Re: master 4b79c80c999 1/2: New function 'sort-on' Date: Fri, 02 Feb 2024 18:55:39 +0100 Message-ID: <87ttmq92ec.fsf@dataswamp.org> References: <170688047526.14693.2994051491358257471@vcs2.savannah.gnu.org> <20240202132756.4272CC0EFE7@vcs2.savannah.gnu.org> <87cytej4hy.fsf@daniel-mendler.de> <86zfwi52m1.fsf@gnu.org> <87plxe28as.fsf@web.de> <86wmrm50i5.fsf@gnu.org> <878r4224s2.fsf@web.de> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37575"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: emacs-devel@gnu.org Cancel-Lock: sha1:+fWdvNmsMhIYyvjjc1+cz/3gzo0= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Feb 02 19:35:13 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rVyNd-0009Tg-CB for ged-emacs-devel@m.gmane-mx.org; Fri, 02 Feb 2024 19:35:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rVyMt-0007Gn-OS; Fri, 02 Feb 2024 13:34:27 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rVxlc-0008Rt-7P for emacs-devel@gnu.org; Fri, 02 Feb 2024 12:55:56 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rVxlT-00019q-ES for emacs-devel@gnu.org; Fri, 02 Feb 2024 12:55:55 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1rVxlQ-0007vJ-TP for emacs-devel@gnu.org; Fri, 02 Feb 2024 18:55:44 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: emacs-devel@gnu.org Mail-Copies-To: never Received-SPF: pass client-ip=116.202.254.214; envelope-from=ged-emacs-devel@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_TEMPERROR=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 02 Feb 2024 13:34:26 -0500 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:315770 Archived-At: Michael Heerdegen via "Emacs development discussions." wrote: >> I'm sorry that it doesn't. It does to me, so let's agree >> to disagree. > > Hmm - ok. sort.el is the only built-in library whose names > constantly confuses [...] OT: I wrote a whole mini-library on sorting, 10 defuns, because I got so confused. No, not really. But in the other thread we were discussing how to not re-write code that is already written by someone else and made public - be it in core Emacs, ELPA, on the Emacs wiki or even on personal web pages. I posted some examples but this file is a much better example! As is evident, a lot of that stuff is really basic. Surely, it already exists somewhere in various forms - like what, half or two-thirds of it? But I don't understand how one is supposed to find out. ;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/sort-incal.el (require 'dwim) (require 'edit) (require 'erc) (require 'seq) (require 'sort) (setq sort-fold-case t) (defun sort-region (&optional beg end) (interactive (use-region)) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (sort-lines nil beg end) ) (defalias 'sortb #'sort-region) (defun sort-paragraph () (interactive) (let ((beg (progn (backward-paragraph) (point))) (end (progn (forward-paragraph) (point))) ) (sort-lines nil beg end) )) (defalias 'sort-block #'sort-paragraph) (defun sort-year-field (&optional beg end) (interactive (use-region)) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (sort-regexp-fields nil "^.*$" "[[:digit:]]\\{4\\}" beg end) (save-buffer) ) (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) ) (defalias 's2f #'sort-second-field) (defun sort-lines-length (&optional beg end) (interactive (use-region)) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (sort-subr nil #'forward-line #'end-of-line nil nil (lambda (a b) (> (- (cdr a) (car a)) (- (cdr b) (car b)) )))))) (defalias 'sll #'sort-lines-length) (defun sort-lines-random (&optional beg end) (interactive (use-region)) (or beg (setq beg (point-min))) (or end (setq end (point-max))) (save-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)) ))))) (defalias 'r #'sort-lines-random) ;; test here: ;; aaa ;; ccc ;; bbb ;; ddd (defun sort-whole-lines (beg end) (interactive "r") (save-excursion (let ((s (progn (goto-char beg) (line-beginning-position))) (e (progn (goto-char end) (line-end-position))) ) (sort-lines nil s e) ))) (defun insert-string-list (string-list) (when string-list (insert (format "%s" (car string-list))) (dolist (s (cdr string-list)) (insert (format " %s" s))))) (defun sort-line-words (beg end &optional set-delim) (interactive "r\nP") (let*((str (region-to-string)) (delim-str (when set-delim (read-string "delimiter: ") )) (str-list (split-string str delim-str)) (sorted (erc-sort-strings str-list)) ) (kill-region beg end) (if set-delim (progn (dolist (s (nreverse (cdr (nreverse sorted)))) (insert (format "%s%s" s delim-str))) (insert (format "%s" (car (last sorted))))) (insert-string-list sorted) ))) ;; sort me: a is just string test this ;; sorted: a is just string test this ;; ;; and me with a dash delim: this-is-just-a-test-string (defun scramble (beg end) "Shuffle chars in region from BEG to END." (interactive "r") (when (use-region-p) (save-excursion (let*((str (region-to-string)) (chars (delete "" (split-string str ""))) (rand-chars (sort chars (lambda (_ __) (zerop (random 2))))) ) (delete-region beg end) (dolist (c rand-chars) (insert c) ))))) (provide 'sort-incal) -- underground experts united https://dataswamp.org/~incal