From 1b0a2712c00fb63418a8cc72f1fd4ccca395942d Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Sat, 24 Feb 2024 01:34:38 +0000 Subject: [PATCH] feat(user/elisp/remove-single-newlines.el):Add Emacs function - Note: Useful for cleaning up Mediawiki wikicode --- user/elisp/remove-single-newlines.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 user/elisp/remove-single-newlines.el diff --git a/user/elisp/remove-single-newlines.el b/user/elisp/remove-single-newlines.el new file mode 100644 index 0000000..22da713 --- /dev/null +++ b/user/elisp/remove-single-newlines.el @@ -0,0 +1,24 @@ +(defun remove-single-newlines () + ;; Desc: Replace all single newlines with spaces. + ;; Date: 2024-02-24 + "Remove single newlines, joining lines that are separated by a single newline, while preserving paragraphs separated by multiple newlines." + (interactive) + (let ((start (point-min)) (end (point-max))) + (save-excursion + (goto-char start) + (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" end t) + ;; Check if the newline is followed by another newline (indicating a paragraph break) + (unless (looking-at-p "\n") + (replace-match "\\1 \\2" nil nil)))))) + +(defun remove-single-newlines-region (start end) + ;; Desc: Replace all single newlines in a selected region with spaces. + ;; Date: 2024-02-24 + "Remove single newlines in the selected region, joining lines separated by a single newline, while preserving paragraphs separated by multiple newlines." + (interactive "r") ; "r" means this function should use the region start and end as arguments when called interactively + (save-excursion + (goto-char start) + (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" end t) + ;; Check if the newline is followed by another newline (indicating a paragraph break) + (unless (looking-at-p "\n") + (replace-match "\\1 \\2" nil nil))))) -- 2.30.2