我使用这个作为参考:Emacs comment/uncomment current line
我的问题是,我是否可以使用defadvice执行相同的任务(这对我来说似乎更合适)?一些类似的东西
(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) 发布于 2012-10-27 22:35:52
这个答案是基于我上面的评论。
defadvice并不比另一种解决方案更合适。它永远不会比另一个解决方案更合适。
当您无法通过其他方式解决问题时,defadvice是的最后一招。
期间。
请记住,无论何时使用defadvice,您都是在从根本上修改包开发人员所依赖的Emacs API。
当你巧妙地改变这些行为时,你会给你带来很多问题,最终也会给包开发人员带来很多问题,因为你的Emacs API被defadvice破坏了。
因此,当您想要在本地更改功能时,方法是使用现有功能定义一个新命令并重新映射到该命令。
也就是说(来自您提到的answer ):
(defun comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)
(next-line)))
(global-set-key [remap comment-dwim] 'comment-or-uncomment-region-or-line)https://stackoverflow.com/questions/13095971
复制相似问题