From b77b8d63bdcc431bb87723aec5690ea0482bc06c Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 3 Oct 2023 10:29:04 +0200 Subject: [PATCH] Add gpolonkai/numeric-sort-lines --- configuration.org | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/configuration.org b/configuration.org index 8395d22..4c525fd 100644 --- a/configuration.org +++ b/configuration.org @@ -353,6 +353,18 @@ directory." (message location))))) #+end_src +*** Find the first number on line + +#+begin_src emacs-lisp +(defun gpolonkai/find-number-on-line () + "Find the first number on the current line." + (save-excursion + (without-restriction + (goto-char (pos-bol)) + (when (re-search-forward "[0-9]+" (pos-eol) t) + (number-at-point))))) +#+end_src + ** Check if we are running under Termux We need to do things differently, if so. @@ -607,6 +619,22 @@ With prefix argument, use full path." (error (format "Buffer %s is not visiting a file" (buffer-name buffer)))))) #+END_SRC +*** Sort lines by the first number on it + +#+begin_src emacs-lisp +(defun gpolonkai/numeric-sort-lines (reverse beg end) + "Sort lines in region by version. +Interactively, REVERSE is the prefix argument, and BEG and END are the region. +Non-nil REVERSE means to sort in reverse order." + (interactive "P\nr") + (save-excursion + (save-restriction + (narrow-to-region beg end) + (goto-char (point-min)) + (let ((indibit-field-text-motion t)) + (sort-subr reverse 'forward-line 'end-of-line #'gpolonkai/find-number-on-line))))) +#+end_src + ** Navigation *** Move to different beginnings of the current line