我正在研究将下一行拉到当前行的这个小功能。我想添加一个功能,以便如果当前行是行注释,而下一行也是行注释,则在“上拉”操作后将删除注释字符。
例:
之前
;; comment 1▮
;; comment 2
呼唤 M-x modi/pull-up-line
后
;; comment 1▮comment 2
请注意,;;删除了以前的字符comment 2。
(defun modi/pull-up-line ()
"Join the following line onto the current one (analogous to `C-e', `C-d') or
`C-u M-^' or `C-u M-x join-line'.
If the current line is a comment and the pulled-up line is also a comment,
remove the comment characters from that line."
(interactive)
(join-line -1)
;; If the current line is a comment
(when (nth 4 (syntax-ppss))
;; Remove the comment prefix chars from the pulled-up line if present
(save-excursion
(forward-char)
(while (looking-at "/\\|;\\|#")
(delete-forward-char 1))
(when (looking-at "\\s-")
(delete-forward-char 1)))))
上述功能的作品,但就目前而言,无论是主要模式,它将考虑/或;或#作为注释字符:(looking-at "/\\|;\\|#")。
我想使这条线更聪明;特定于主要模式。
解
感谢@ericstokes的解决方案,我相信以下内容可以涵盖我的所有使用案例:)
(defun modi/pull-up-line ()
"Join the following line onto the current one (analogous to `C-e', `C-d') or
`C-u M-^' or `C-u M-x join-line'.
If the current line is a comment and the pulled-up line is also a comment,
remove the comment characters from that line."
(interactive)
(join-line -1)
;; If the current line is a comment
(when (nth 4 (syntax-ppss))
;; Remove the comment prefix chars from the pulled-up line if present
(save-excursion
(forward-char)
;; Delete all comment-start or space characters
(while (looking-at (concat "\\s<" ; comment-start char as per syntax table
"\\|" (substring comment-start 0 1) ; first char of `comment-start'
"\\|" "\\s-")) ; extra spaces
(delete-forward-char 1)))))
comment-start和comment-end字符串在中c-mode(但不是c++-mode)设置为“ / *”和“ * /” 。而且这c-comment-start-regexp两种风格都匹配。加入后,先删除结尾字符,然后删除开头。但我认为我的解决办法是uncomment-region,join-line在comment-region让什么注释符是什么Emacs的担心。
/* ... */)?