| 1 | (defun remove-single-newlines () |
| 2 | ;; Desc: Replace all single newlines with spaces. |
| 3 | ;; Date: 2024-02-24 |
| 4 | "Remove single newlines, joining lines that are separated by a single newline, while preserving paragraphs separated by multiple newlines." |
| 5 | (interactive) |
| 6 | (let ((start (point-min)) (end (point-max))) |
| 7 | (save-excursion |
| 8 | (goto-char start) |
| 9 | (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" end t) |
| 10 | ;; Check if the newline is followed by another newline (indicating a paragraph break) |
| 11 | (unless (looking-at-p "\n") |
| 12 | (replace-match "\\1 \\2" nil nil)))))) |
| 13 | |
| 14 | (defun remove-single-newlines-region (start end) |
| 15 | ;; Desc: Replace all single newlines in a selected region with spaces. |
| 16 | ;; Date: 2024-02-24 |
| 17 | "Remove single newlines in the selected region, joining lines separated by a single newline, while preserving paragraphs separated by multiple newlines." |
| 18 | (interactive "r") ; "r" means this function should use the region start and end as arguments when called interactively |
| 19 | (save-excursion |
| 20 | (goto-char start) |
| 21 | (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" end t) |
| 22 | ;; Check if the newline is followed by another newline (indicating a paragraph break) |
| 23 | (unless (looking-at-p "\n") |
| 24 | (replace-match "\\1 \\2" nil nil))))) |