Function: c-electric-colon
c-electric-colon is an interactive and byte-compiled function defined
in cc-cmds.el.gz.
Signature
(c-electric-colon ARG)
Documentation
Insert a colon.
If c-electric-flag is non-nil, the colon is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:
(a) If the auto-newline feature is turned on (indicated by "/la" on
the mode line) newlines are inserted before and after the colon based on
the settings in c-hanging-colons-alist.
(b) Any auto-newlines are indented. The original line is also
reindented unless c-syntactic-indentation is nil.
(c) If auto-newline is turned on, whitespace between two colons will be
"cleaned up" leaving a scope operator, if this action is set in
c-cleanup-list.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/cc-cmds.el.gz
(defun c-electric-colon (arg)
"Insert a colon.
If `c-electric-flag' is non-nil, the colon is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:
\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
the mode line) newlines are inserted before and after the colon based on
the settings in `c-hanging-colons-alist'.
\(b) Any auto-newlines are indented. The original line is also
reindented unless `c-syntactic-indentation' is nil.
\(c) If auto-newline is turned on, whitespace between two colons will be
\"cleaned up\" leaving a scope operator, if this action is set in
`c-cleanup-list'."
(interactive "*P")
(c-with-string-fences
(let* ((bod (c-point 'bod))
(literal (c-save-buffer-state () (c-in-literal bod)))
newlines is-scope-op
;; shut this up
(c-echo-syntactic-information-p nil))
(let (post-self-insert-hook) ; Disable random functionality.
(self-insert-command (prefix-numeric-value arg)))
;; Any electric action?
(if (and c-electric-flag (not literal) (not arg))
;; Unless we're at EOL, only re-indentation happens.
(if (not (looking-at "[ \t]*\\\\?$"))
(if c-syntactic-indentation
(indent-according-to-mode))
;; scope-operator clean-up?
(let ((pos (- (point-max) (point)))
(here (point)))
(if (c-save-buffer-state () ; Why do we need this? [ACM, 2003-03-12]
(and c-auto-newline
(memq 'scope-operator c-cleanup-list)
(eq (char-before) ?:)
(progn
(forward-char -1)
(c-skip-ws-backward)
(eq (char-before) ?:))
(not (c-in-literal))
(not (eq (char-after (- (point) 2)) ?:))))
(progn
(delete-region (point) (1- here))
(setq is-scope-op t)))
(goto-char (- (point-max) pos)))
;; indent the current line if it's done syntactically.
(if c-syntactic-indentation
;; Cannot use the same syntax analysis as we find below,
;; since that's made with c-syntactic-indentation-in-macros
;; always set to t.
(indent-according-to-mode))
;; Calculate where, if anywhere, we want newlines.
(c-save-buffer-state
((c-syntactic-indentation-in-macros t)
(c-auto-newline-analysis t)
;; Turn on syntactic macro analysis to help with auto newlines
;; only.
(syntax (c-guess-basic-syntax))
(elem syntax))
;; Translate substatement-label to label for this operation.
(while elem
(if (eq (car (car elem)) 'substatement-label)
(setcar (car elem) 'label))
(setq elem (cdr elem)))
;; some language elements can only be determined by checking
;; the following line. Let's first look for ones that can be
;; found when looking on the line with the colon
(setq newlines
(and c-auto-newline
(or (c-lookup-lists '(case-label label access-label)
syntax c-hanging-colons-alist)
(c-lookup-lists '(member-init-intro inher-intro)
(progn
(insert ?\n)
(unwind-protect
(c-guess-basic-syntax)
(delete-char -1)))
c-hanging-colons-alist)))))
;; does a newline go before the colon? Watch out for already
;; non-hung colons. However, we don't unhang them because that
;; would be a cleanup (and anti-social).
(if (and (memq 'before newlines)
(not is-scope-op)
(save-excursion
(skip-chars-backward ": \t")
(not (bolp))))
(let ((pos (- (point-max) (point))))
(forward-char -1)
(c-newline-and-indent)
(goto-char (- (point-max) pos))))
;; does a newline go after the colon?
(if (and (memq 'after (cdr-safe newlines))
(not is-scope-op))
(c-newline-and-indent))))))
(c--call-post-self-insert-hook-more-safely))