Function: c-electric-slash

c-electric-slash is an interactive and byte-compiled function defined in cc-cmds.el.gz.

Signature

(c-electric-slash ARG)

Documentation

Insert a slash character.

If the slash is inserted immediately after the comment prefix in a c-style comment, the comment might get closed by removing whitespace and possibly inserting a "*". See the variable c-cleanup-list.

Indent the line as a comment, if:

  1. The slash is second of a "//" line oriented comment introducing
     token and we are on a comment-only-line, or

  2. The slash is part of a "*/" token that closes a block oriented
     comment.

If a numeric ARG is supplied, point is inside a literal, or c-syntactic-indentation is nil or c-electric-flag is nil, indentation is inhibited.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-cmds.el.gz
(defun c-electric-slash (arg)
  "Insert a slash character.

If the slash is inserted immediately after the comment prefix in a c-style
comment, the comment might get closed by removing whitespace and possibly
inserting a \"*\".  See the variable `c-cleanup-list'.

Indent the line as a comment, if:

  1. The slash is second of a \"//\" line oriented comment introducing
     token and we are on a comment-only-line, or

  2. The slash is part of a \"*/\" token that closes a block oriented
     comment.

If a numeric ARG is supplied, point is inside a literal, or
`c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
is inhibited."
  (interactive "*P")
  (let ((literal (c-save-buffer-state () (c-in-literal)))
	indentp
	;; shut this up
	(c-echo-syntactic-information-p nil))

    ;; comment-close-slash cleanup?  This DOESN'T need `c-electric-flag' or
    ;; `c-syntactic-indentation' set.
    (when (and (not arg)
	       (eq literal 'c)
	       (memq 'comment-close-slash c-cleanup-list)
	       (eq (c-last-command-char) ?/)
	       (looking-at (concat "[ \t]*\\("
				   (regexp-quote comment-end) "\\)?$"))
	; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
	       (save-excursion
		 (save-restriction
		   (narrow-to-region (point-min) (point))
		   (back-to-indentation)
		   (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
      (delete-region (progn (forward-line 0) (point))
		     (progn (end-of-line) (point)))
      (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?

    (setq indentp (and (not arg)
		       c-syntactic-indentation
		       c-electric-flag
		       (eq (c-last-command-char) ?/)
		       (eq (char-before) (if literal ?* ?/))))
    (let (post-self-insert-hook)	; Disable random functionality.
      (self-insert-command (prefix-numeric-value arg)))
    (if indentp
	(indent-according-to-mode))
    (c--call-post-self-insert-hook-more-safely)))