Function: c-electric-lt-gt

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

Signature

(c-electric-lt-gt ARG)

Documentation

Insert a "<" or ">" character.

If the current language uses angle bracket parens (e.g. template arguments in C++), try to find out if the inserted character is a paren and give it paren syntax if appropriate.

If c-electric-flag and c-syntactic-indentation are both non-nil, the line will be reindented if the inserted character is a paren or if it finishes a C++ style stream operator in C++ mode. Exceptions are when a numeric argument is supplied, or the point is inside a literal.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-cmds.el.gz
(defun c-electric-lt-gt (arg)
  "Insert a \"<\" or \">\" character.
If the current language uses angle bracket parens (e.g. template
arguments in C++), try to find out if the inserted character is a
paren and give it paren syntax if appropriate.

If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
line will be reindented if the inserted character is a paren or if it
finishes a C++ style stream operator in C++ mode.  Exceptions are when a
numeric argument is supplied, or the point is inside a literal."

  (interactive "*P")
  (let (template-delim include-delim
	(c-echo-syntactic-information-p nil)
	final-pos found-delim case-fold-search)

    (c-with-string-fences
     (let (post-self-insert-hook)	; Disable random functionality.
       (self-insert-command (prefix-numeric-value arg)))
     (setq final-pos (point))

;;;;  2010-01-31: There used to be code here to put a syntax-table text
;;;;  property on the new < or > and its mate (if any) when they are template
;;;;  parens.  This is now done in an after-change function.

     (when (and (not arg)
		(not (c-save-buffer-state () (c-in-literal))))
       ;; Have we got a delimiter on a #include directive?
       (beginning-of-line)
       (setq include-delim
	     (and
	      (looking-at c-cpp-include-key)
	      (if (eq (c-last-command-char) ?<)
		  (eq (match-end 0) (1- final-pos))
		(goto-char (1- final-pos))
		(skip-chars-backward "^<>" (c-point 'bol))
		(eq (char-before) ?<))))
       (goto-char final-pos)

       ;; Indent the line if appropriate.
       (when (and c-electric-flag c-syntactic-indentation c-recognize-<>-arglists)
	 (setq found-delim
	       (if (eq (c-last-command-char) ?<)
		   ;; If a <, basically see if it's got "template" before it .....
		   (or (and (progn
			      (backward-char)
			      (= (point)
				 (progn (c-beginning-of-current-token) (point))))
			    (progn
			      (c-backward-token-2)
			      (looking-at c-opt-<>-sexp-key))
			    (setq template-delim t))
		       ;; ..... or is a C++ << operator.
		       (and (c-major-mode-is 'c++-mode)
			    (progn
			      (goto-char (1- final-pos))
			      (c-beginning-of-current-token)
			      (looking-at "<<"))
			    (>= (match-end 0) final-pos)))

		 ;; It's a >.  Either a template/generic terminator ...
		 (or (and (c-get-char-property (1- final-pos) 'syntax-table)
			  (setq template-delim t))
		     ;; or a C++ >> operator.
		     (and (c-major-mode-is 'c++-mode)
			  (progn
			    (goto-char (1- final-pos))
			    (c-beginning-of-current-token)
			    (looking-at ">>"))
			  (>= (match-end 0) final-pos)))))
	 (goto-char final-pos)

	 (when found-delim
	   (indent-according-to-mode)))))

    ;; On the off chance that < and > are configured as pairs in
    ;; electric-pair-mode.
    (when (and (boundp 'electric-pair-mode) electric-pair-mode
	       (or template-delim include-delim))
      (let (post-self-insert-hook)
	(electric-pair-post-self-insert-function)))

    (when found-delim
      (when (and (eq (char-before) ?>)
		 (not executing-kbd-macro)
		 blink-paren-function)
	;; From now (2016-01-01), the syntax-table text properties on < and >
	;; are applied in an after-change function, not during redisplay.  Hence
	;; we no longer need to call (sit-for 0) for blink paren to work.
	(funcall blink-paren-function))))
  (c--call-post-self-insert-hook-more-safely))