From: Steven Baltakatei Sandoval Date: Sat, 24 Feb 2024 01:34:38 +0000 (+0000) Subject: feat(user/elisp/remove-single-newlines.el):Add Emacs function X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/1b0a2712c00fb63418a8cc72f1fd4ccca395942d?hp=9543c9b58294b642655483d078dbe5e594c66ddb feat(user/elisp/remove-single-newlines.el):Add Emacs function - Note: Useful for cleaning up Mediawiki wikicode --- 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)))))