137 lines
4.7 KiB
EmacsLisp
137 lines
4.7 KiB
EmacsLisp
;;; gpolonkai/text-utils.el --- Text manipulation utilities
|
||
;;;
|
||
;;; SPDX-License-Identifier: GPL-3.0-or-later
|
||
;;; Copyright © 2025 Gergely Polonkai
|
||
;;; This library is free software; you can redistribute it and/or
|
||
;;; modify it under the terms of the GNU Lesser General Public
|
||
;;; License as published by the Free Software Foundation; either
|
||
;;; version 3 of the License, or (at your option) any later version.
|
||
;;;
|
||
;;; This library is distributed in the hope that it will be useful,
|
||
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
;;; Lesser General Public License for more details.
|
||
;;;
|
||
;;; You should have received a copy of the GNU Lesser General Public
|
||
;;; License along with this library; if not, write to the
|
||
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||
;;; Boston, MA 02111-1307, USA.
|
||
;;;
|
||
;;; Commentary:
|
||
;;;
|
||
;;; Utility functions stolen from the Internet or written by me
|
||
;;;
|
||
;;; Code:
|
||
|
||
;; From Sacha Chua’s blog: http://pages.sachachua.com/.emacs.d/Sacha.html
|
||
|
||
(defun sachachua/fill-or-unfill-paragraph (&optional unfill region)
|
||
"Fill (or unfill, if UNFILL is non-nil) paragraph (or REGION)."
|
||
(interactive (progn
|
||
(barf-if-buffer-read-only)
|
||
(list (if current-prefix-arg 'unfill) t)))
|
||
(let ((fill-column (if unfill (point-max) fill-column)))
|
||
(fill-paragraph nil region)))
|
||
|
||
;; Based on Xah’s toggle letter case defun (version 2015-12-22):
|
||
;; http://ergoemacs.org/emacs/modernization_upcase-word.html
|
||
|
||
(defun gpolonkai/toggle-char-case (arg-move-point)
|
||
"Toggle the case of the char after point.
|
||
|
||
If prefix argument ARG-MOVE-POINT is non-nil, move point after the char."
|
||
(interactive "P")
|
||
(let ((case-fold-search nil))
|
||
(cond
|
||
((looking-at "[[:lower:]]") (upcase-region (point) (1+ (point))))
|
||
((looking-at "[[:upper:]]") (downcase-region (point) (1+ (point)))))
|
||
(cond
|
||
(arg-move-point (right-char)))))
|
||
|
||
;; The next two functions are from whattheemacsd.com: http://whattheemacsd.com/editing-defuns.el-01.html
|
||
|
||
(defun gpolonkai/numeric-sort-lines (reverse beg end)
|
||
"Sort lines in region by \"version\".
|
||
|
||
\"version\" is the first number found on a line.
|
||
|
||
If REVERSE is set, sort in reverse order.
|
||
|
||
BEG and END are the two ends of the region."
|
||
(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)))))
|
||
|
||
(defun wted/open-line-above ()
|
||
"Open a new line above point."
|
||
|
||
(interactive)
|
||
(beginning-of-line)
|
||
(newline)
|
||
(forward-line -1)
|
||
(let ((tab-always-indent t)
|
||
(c-tab-always-indent t))
|
||
(indent-for-tab-command)))
|
||
|
||
(defun wted/open-line-below ()
|
||
"Open a new line below point."
|
||
|
||
(interactive)
|
||
|
||
(end-of-line)
|
||
(newline)
|
||
(let ((tab-always-indent t)
|
||
(c-tab-always-indent t))
|
||
(indent-for-tab-command)))
|
||
|
||
;; From Marcin Borkowski’s blog: http://mbork.pl/2019-02-17_Inserting_the_current_file_name_at_point
|
||
|
||
(defun mbork/insert-current-file-name-at-point (&optional full-path)
|
||
"Insert the current filename at point.
|
||
|
||
With prefix argument, or with FULL-PATH set to non-nil, use full path of the file."
|
||
(interactive "P")
|
||
(let* ((buffer
|
||
(if (minibufferp)
|
||
(window-buffer
|
||
(minibuffer-selected-window))
|
||
(current-buffer)))
|
||
(filename (buffer-file-name buffer)))
|
||
(if filename
|
||
(insert (if full-path filename (file-name-nondirectory filename)))
|
||
(error (format "Buffer %s is not visiting a file" (buffer-name buffer))))))
|
||
|
||
;; From Emacs SE: http://emacs.stackexchange.com/a/27170/507
|
||
|
||
(defun e-se/query-swap-strings (from-string to-string &optional delimited start end)
|
||
"Swap occurrences of FROM-STRING and TO-STRING.
|
||
|
||
DELIMITED, START, and END are passed down verbatim to `perform-replace'."
|
||
(interactive
|
||
(let ((common
|
||
(query-replace-read-args
|
||
(concat "Query swap"
|
||
(if current-prefix-arg
|
||
(if (eq current-prefix-arg '-) " backward" " word")
|
||
"")
|
||
(if (use-region-p) " in region" ""))
|
||
nil)))
|
||
(list (nth 0 common) (nth 1 common) (nth 2 common)
|
||
(if (use-region-p) (region-beginning))
|
||
(if (use-region-p) (region-end)))))
|
||
(perform-replace
|
||
(concat "\\(" (regexp-quote from-string) "\\)\\|" (regexp-quote to-string))
|
||
`(replace-eval-replacement replace-quote
|
||
(if (match-string 1)
|
||
,to-string
|
||
,from-string))
|
||
t t delimited nil nil start end))
|
||
|
||
(provide 'gpolonkai/text-utils)
|
||
|
||
;;; text-utils.el ends here
|