xah talk show 2022-06-08 emacs lisp tutorial, write command to change space to newline

vidthumb x3tu4d9xGzc
(defun xah-space-to-newline-2022-06-08 ()
  "Replace space sequence to a newline char in current block or selection.

If `universal-argument' is called first, ask user to enter a character to replace.

URL `http://xahlee.info/emacs/emacs/emacs_space_to_newline.html'
Version: 2017-08-19 2021-11-28"
  (interactive)
  (let* (($bds (xah-get-bounds-of-block-or-region))
         ($p1 (car $bds))
         ($p2 (cdr $bds))
         $charToLinebreak
         )
    (if current-prefix-arg
        (setq
         $charToLinebreak
         (read-string "Type a regex where you want linebreaks after:"))
      (setq $charToLinebreak " "))
    (save-restriction
      (narrow-to-region $p1 $p2)
      (goto-char (point-min))
      (while (search-forward $charToLinebreak nil t)
        (replace-match (format "%s\n" $charToLinebreak))))))