1

I am working with emacs24 with cc-mode, I want to know how to make my emacs more "clever". After I type a }, it will auto insert a new line and indent as excepted. I want to know how to switch the point to previous line. For example, when i define a function, Now my emacs behavior is:

void f()
{
}
//point

"//point" is the position of cursor after } was input. But i want is this:

void f()
{
    //point
}

I hope the position of cursor can switch to previous line and indent automatically. I know emacs can do this, but I don't know how to do it, who can help me?

Yongqiang Zhou
  • 322
  • 1
  • 12

4 Answers4

1

I think you are after these.. C-M-u, C-M-d, C-M-f and C-M-b

Practice a bit... They are kind of global and they do behave contextually in almost all modes..

UPDATE:

ohh.. It seems you want to place the cursor automatically.. actually in more general Emacs will help you not to type } at all. I mean emacs can insert closing paran automatically.

There is inbuilt one electric pair mode

third party autopair.el

kindahero
  • 5,817
  • 3
  • 25
  • 32
0

I don't trust anything electric, so I wrote this function.

(defconst insert-logical-brackets-logical-bracket-begin "{")
(defconst insert-logical-brackets-logical-bracket-end "}")
(defconst insert-logical-brackets-default-style 0)
(make-variable-buffer-local 'logical-bracket-begin)
(make-variable-buffer-local 'logical-bracket-end)
(make-variable-buffer-local 'insert-logical-brackets-default-style)
(defun insert-logical-brackets(&optional style)
  "If STYLE = 0(default, according to `insert-logical-brackets-default-style' value), make a newline before opening bracket, if line is not empty. Make a newline after closing bracket, if there is something after this bracket. Make two newlines in the middle.
If STYLE = 1, don't make newlines before opening a bracket(one of c styles).
If STYLE = 2, don't make newlines before opening and after closing bracket.
If STYLE = 3, allways make all newlines.
If STYLE is not nil, don't make newlines between brackets(still makes before/after lines)."
  (interactive "P")
  (when (eq style nil)
    (setq style insert-logical-brackets-default-style))
  (funcall indent-line-function)
  (unless (or (eq 1 style) (eq 2 style))
    (when (or (/= (point) (save-excursion (back-to-indentation) (point))) (eq 3 style))
      (newline)
      (funcall indent-line-function)))
  (unless (and (integerp style) (= 2 style))
    (when (or (not (looking-at "\n")) (eq 3 style))
      (newline)
      (funcall indent-line-function)
      (forward-line -1)
      (goto-char (point-at-eol))))
  (insert logical-bracket-begin)
  (funcall indent-line-function)
  (let ((return-point (point)))
    (when (or (not style) (or (eq 0 style) (eq 1 style) (eq 2 style) (eq 3 style)))
      (newline)
      (funcall indent-line-function)
      (setq return-point (point))
      (newline))
    (insert logical-bracket-end)
    (funcall indent-line-function)
    (goto-char return-point)))
desudesudesu
  • 2,185
  • 1
  • 15
  • 20
0

Take a look at template systems like yasnippet: http://www.emacswiki.org/emacs/CategoryTemplates

atlau
  • 881
  • 1
  • 7
  • 16
0

auto-indent-mode maybe what you want!

hbin
  • 2,657
  • 1
  • 23
  • 23