Emacs: Join lines even in comments

| emacs

I like to dictate one sentence per line. My voice activity detection sometimes adds extra breaks in between lines if I pause too much in the middle of a sentence. sacha-whisper-insert takes care of inserting the comment markers using comment-indent-new-line. After I edit my dictation, I usually want to make the comments more compact. The following function overrides delete-indentation so that join-line and fill-paragraph all manage the common prefix properly, which means sacha-reformat-paragraph-or-region can cycle through wrapped, one line per sentence, or everything on one line.

From Smartly join comments with join-line ยท Tony Zorman

;;;###autoload
(defun sacha-delete-indentation (&optional arg beg end)
  "Join this line to previous and fix up whitespace at join.
If there is a fill prefix, delete it from the beginning of this
line.
With prefix ARG, join the current line to the following line.
When BEG and END are non-nil, join all lines in the region they
define.  Interactively, BEG and END are, respectively, the start
and end of the region if it is active, else nil.  (The region is
ignored if prefix ARG is given.)

When joining lines, smartly delete comment beginnings, such that one
does not have to do this by oneself.

From https://tony-zorman.com/posts/join-lines-comments.html"
  (interactive
   (progn (barf-if-buffer-read-only)
          (cons current-prefix-arg
                (and (use-region-p)
                     (list (region-beginning) (region-end))))))
  ;; Consistently deactivate mark even when no text is changed.
  (setq deactivate-mark t)
  (if (and beg (not arg))
      ;; Region is active.  Go to END, but only if region spans
      ;; multiple lines.
      (and (goto-char beg)
           (> end (line-end-position))
           (goto-char end))
    ;; Region is inactive.  Set a loop sentinel
    ;; (subtracting 1 in order to compare less than BOB).
    (setq beg (1- (line-beginning-position (and arg 2))))
    (when arg (forward-line)))
  (let* ((comment (string-trim-right comment-start))
         (prefix-start (and (> (length comment-start) 0)
                            (regexp-quote comment)))
         ;; A continuation of a comment. This is important for
         ;; languages such as Haskell, where -- starts a comment
         ;; and --- still is one.
         (prefix-cont (and prefix-start
                           (regexp-quote (substring comment 0 1))))
         (prev-comment?                 ; Comment on previous line?
          (save-excursion
            (forward-line -1)
            (back-to-indentation)
            (search-forward prefix-start (pos-eol) 'no-error))))
    (while (and (> (line-beginning-position) beg)
                (forward-line 0)
                (= (preceding-char) ?\n))
      (delete-char -1)
      (unless (save-excursion (forward-line -1) (eolp))
        (delete-horizontal-space)
        ;; Delete the start of a comment once.
        (when (and prev-comment? prefix-start (looking-at prefix-start))
          (replace-match "" t t)
          ;; Look for continuations.
          (while (and prefix-cont (looking-at prefix-cont))
            (replace-match "" t t)))
        (fixup-whitespace)))))

Let's use advice to override it so that it lasts even if I reevaluate the original.

(advice-add 'delete-indentation :override #'sacha-delete-indentation)
This is part of my EmacsConfiguration.
View Org source for this post