0

我以此作为参考: Emacs 注释/取消注释当前行

我的问题是我是否可以使用 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) 
4

1 回答 1

2

这个答案是基于我上面的评论。

defadvice并不比另一种解决方案更合适。它永远不会比另一种解决方案更合适。


defadvice当您无法以任何其他方式解决问题时,这是最后的手段。

时期。


请记住,无论何时使用defadvice,您都在从根本上修改包开发人员所依赖的 Emacs API。

当你巧妙地改变这些行为时,当你报告“错误”时,你会给你带来很多问题,最终给包开发者带来很多问题,因为你的 Emacs API 被defadvice.

因此,当您想在本地更改功能时,方法是使用现有功能定义一个新命令并重新映射到它。

也就是说(根据您提到的答案):

(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)
于 2012-10-27T14:35:52.153 回答