diff --git a/configuration.org b/configuration.org index d09b354..1611c10 100644 --- a/configuration.org +++ b/configuration.org @@ -2,141 +2,6 @@ This is a collection of functions and commands i wrote or stole from all around the internet. -** Text manipulation - -*** Fill or unfill a paragraph - -From Sacha Chua’s [[http://pages.sachachua.com/.emacs.d/Sacha.html][blog]]. - -#+begin_src emacs-lisp -(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))) -#+end_src - -*** Toggle case of character at point - -Based on [[http://ergoemacs.org/emacs/modernization_upcase-word.html][Xah’s toggle letter case defun version 2015-12-22]] - -#+begin_src emacs-lisp -(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))))) -#+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 - -*** Open a new line above - -#+begin_src emacs-lisp -(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))) -#+end_src - -*** Open a new line below - -Copied from [[http://whattheemacsd.com/editing-defuns.el-01.html][whattheemacsd.com]]. - -#+begin_src emacs-lisp -(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))) -#+end_src - -*** Insert the current file’s name at point - -From [[http://mbork.pl/2019-02-17_Inserting_the_current_file_name_at_point][Marcin Borkowski]]. - -#+begin_src emacs-lisp -(defun mbork/insert-current-file-name-at-point (&optional full-path) - "Insert the current filename at point. -With prefix argument, use full path." - (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)))))) -#+end_src - -*** Swap occurences of strings - -From [[http://emacs.stackexchange.com/a/27170/507][Emacs SE]]. - -#+begin_src emacs-lisp -(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)) -#+end_src - ** Navigation *** Move to different beginnings/ends of the current line diff --git a/init.el b/init.el index 18da6d2..dbe3c6b 100644 --- a/init.el +++ b/init.el @@ -60,6 +60,7 @@ (load "gpolonkai/utilities") (load "gpolonkai/windows") (load "gpolonkai/org-utils") +(load "gpolonkai/text-utils") ;; I started moving my configuration to this Org file. It’s easier to document this way. (org-babel-load-file (expand-file-name "configuration.org" user-emacs-directory)) diff --git a/lisp/gpolonkai/text-utils.el b/lisp/gpolonkai/text-utils.el new file mode 100644 index 0000000..6884ed1 --- /dev/null +++ b/lisp/gpolonkai/text-utils.el @@ -0,0 +1,136 @@ +;;; 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