How to check whether the character before point equals “\“
(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)
The code as example should be doing the following:
1) If a region is selected, the region should be commented.
2) If the character before point is a “\“, a normal % should be inserted at point.
3) If point is at the beginning of line, insert a “%”
4) If point is anywhere else, go to the end of the sentence and insert “SPC % SPC“
The syntax for individual characters is ?char. So: