如果区域不活动,请在行上切换评论 [英] Toggle comment on line if region is not active

查看:185
本文介绍了如果区域不活动,请在行上切换评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用它作为参考:
Emacs评论/取消注释当前行



我的问题是我是否可以使用defadvice执行相同的任务(这似乎更适合我)?
沿着

 (defadvice comment-or-uncomment-region(在mark-whole-line(arg beg end)activate之前)
(unless(region-active-p)
(setq beg(line-begin-position)end(line-end-position))) )
(ad-activate'comment-or-uncomment-region)


解决方案

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



defadvice 不太适合另一种方案


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



PERIOD。






请记住,每当您使用 defadvice ,您将从根本上修改开发人员依赖的Emacs API的



当您微妙地更改这些行为时,您会为您生成
的许多问题,最终在报告错误时为开发人员提供了
您的Emacs API被打破了 defadvice



所以当你想在本地更改功能时,
要做的就是使用现有的功能来定义一个新的命令,并将
重新映射到它。



为了(从 answer ):

 (defun comment- or-uncomment-region-or-line()
如果没有活动区域,则注释或取消注释该区域或当前行。
(interactive)
(let(beg end)
(if(region-active-p)
(setq beg(region-beginning)end(region-end))
(setq beg(line-begin-position)end - 位置)))
(注释或取消注释区域请求结束)
(下一行)))

(global-set-key [remap comment-dwim]'comment-or-uncomment-region-or-line)


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.

defadvice is not more appropriate than another solution. It is never more appropriate than another solution.


defadvice is a last resort for when you can't fix your problem any other way.

PERIOD.


Keeping in mind that whenever you use defadvice you are fundamentally modifying the 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):

(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)

这篇关于如果区域不活动,请在行上切换评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆