purcell / whole-line-or-region

In Emacs, operate on current line if no region is active
114 stars 12 forks source link

comment-dwim on empty line does nothing #16

Open purcell opened 3 years ago

purcell commented 3 years ago

When on an empty line with no region active, comment-dwim would normally insert the comment prefix before point. But with a region active - selecting the empty line - it does nothing. This means that it doesn't work as expected in this situation when wrapped by whole-line-or-region.

thomasheartman commented 3 years ago

I've been running into this for a while. Created a workaround that seems to work for now.

  (defun heartman/comment-whole-line-or-region (prefix)
    "Override comment-whole-line-or-region.

The original function doesn't work well with empty lines, so let's change create a wrapper."
    (interactive "*p")
    (if (heartman/current-line-empty-p)
        (funcall-interactively 'comment-dwim prefix)
      (funcall-interactively 'whole-line-or-region-comment-dwim-2 prefix)))

(defun heartman/current-line-empty-p ()
  "Check whether the current line is empty."
  (save-excursion
    (beginning-of-line)
    (looking-at-p "[[:space:]]*$")))

There may be easier ways to do it and this may not cover everything, but it seems to work right now, at least.