2

如何检查点之前的字符是否等于“ \

(defun comment-latex () 
  (interactive)
  (if (region-active-p) 
      (comment-region (region-beginning) (region-end))
     (if (= (char-before) ("\\")                 ;; how to fix this sentence?     
       (insert "%")
      (if (= (point) (line-beginning-position)) ;; this part works
        (insert "% ")
       (end-of-line)
       (insert " % "))))
)
(global-set-key LaTeX-mode-map (kbd "%") 'comment-latex)

作为示例的代码应该执行以下操作:

1)如果选择了区域,则应注释该区域。

2)如果点之前的字符是“ \”,则应在点处插入正常的%。

3)如果点在行首,插入一个“ %

4) 如果点在其他任何地方,则转到句末并插入“ SPC % SPC

4

3 回答 3

3

单个字符的语法是 ? 字符。所以:

(= (char-before) ?\\)
于 2012-10-14T15:09:46.360 回答
1

将其替换为

(= (char-before) 92)

92 是 \ 字符的 ASCII 码。

于 2012-10-14T15:07:08.067 回答
1

在 e-lisp 中,字符与字符串不同。获取字符的 ascii 值的语法是?a. 尝试类似:

(if (= (char-before) ?\\))
    DO-SOMETHING
  DO-SOMETHING-ELSE)
于 2012-10-14T15:07:22.107 回答