Add gpolonkai/numeric-sort-lines

This commit is contained in:
Gergely Polonkai 2023-10-03 10:29:04 +02:00
parent c971892569
commit b77b8d63bd
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
1 changed files with 28 additions and 0 deletions

View File

@ -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