请教一个函数--智能注释

基础函数来源stackoverflow

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

现在我想要修改,当前行如果有折叠则先选择当前行,然后注释

(defun comment-or-uncomment-region-or-line ()
  "Comments or uncomments the region or the current line if there's no active region."
  (interactive)
  (when (hs-already-hidden-p)
    (evil-visual-line))
  (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)))

但一直得不到想要的效果,折叠使用的是hideshow,有人知道该怎么写吗?另外evil-visual-line能否转成emacs自带的函数

不是很理解什么叫选择当前行,再注释,选择了怎么注释。。。可能和你的需求不太一样

这是选择当前行, 其实我觉得在Emacs里的选择并不好,因为势必会改变光标位置

(defun comment-or-uncomment-region-or-line ()
  "Comments or uncomments the region or the current line if there's no active region."
  (interactive)
  (let ((p (point)))
    (forward-line 0)
    (if (hs-already-hidden-p) (progn (hs-toggle-hiding) (set-mark (point)) (end-of-line))
      (let ((b (if (region-active-p) (region-beginning) (line-beginning-position)))
          (e (if (region-active-p) (region-end) (line-end-position))))
        (goto-char p) (comment-or-uncomment-region b e)))))

这是要注释

(defun comment-or-uncomment-region-or-line ()
  "Comments or uncomments the region or the current line if there's no active region."
  (interactive)
  (save-excursion
    (forward-line 0)
    (when (hs-already-hidden-p) (hs-toggle-hiding)))
  (let ((b (if (region-active-p) (region-beginning) (line-beginning-position)))
      (e (if (region-active-p) (region-end) (line-end-position))))
    (comment-or-uncomment-region b e)))

因为这里有一个折叠,如果是按照原来的方式,只会注释第一行,而不会将整个折叠都注释了

我试过,如果先选中整行 evil-visual-line,那么就会把整个折叠当做一个代码块来注释

自己实现了一个

(defun comment-or-uncomment-region-or-line ()
  "Comments or uncomments the region or the current line if there's no active region."
  (interactive)
  (when (hs-already-hidden-p)
      (end-of-visual-line)
      (evil-visual-state)
      (beginning-of-visual-line))
  (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)))

我也写了一个,我不用Evil的。。。

(defun comment-or-uncomment-region-or-line ()
  "Comments or uncomments the region or the current line if there's no active region."
  (interactive)
  (save-excursion
    (forward-line 0)
    (when (hs-already-hidden-p)
      (put 'comment-or-uncomment-region-or-line 'zzz t)
      (goto-char (hs-find-block-beginning))
      (set-mark (point))
      (forward-sexp 1))
    (let ((b (if (region-active-p) (region-beginning) (line-beginning-position)))
        (e (if (region-active-p) (region-end) (line-end-position))))
      (comment-or-uncomment-region b e)
      (when (get 'comment-or-uncomment-region-or-line 'zzz)
        (put 'comment-or-uncomment-region-or-line 'zzz nil)
        (hs-toggle-hiding)(hs-toggle-hiding)))))

PS when 后面不用写 progn, 自带的

嗯,谢谢啦,我把progn去掉了