I use this as reference:
Emacs comment/uncomment current line
My question is whether I can perform the same task using defadvice (which seems more appropriate to me)?
Something along the lines of
(defadvice comment-or-uncomment-region (before mark-whole-line (arg beg end) activate)
(unless (region-active-p)
(setq beg (line-beginning-position) end (line-end-position))))
(ad-activate 'comment-or-uncomment-region)
This answer is based on my comments above.
defadviceis not more appropriate than another solution. It is never moreappropriate than another solution.
defadviceis a last resort for when you can’t fix your problem any otherway.
PERIOD.
Keeping in mind that whenever you use
defadviceyou are fundamentally modifyingthe Emacs API which package developers rely upon.
When you subtly changing these behaviours, you cause lots of problems for you
and eventually for the package developers when you report “bugs” because
your Emacs API was broken with
defadvice.So when you want to change functionality locally, the
way to do it is to define a new command using existing functionality and remap
to it.
To wit (from the answer you referred to):