Petter M�hl�n wrote:

Hi,

This seems like a good idea to me, but I had two problems when trying it
out. First, I got an error message saying something like "if: wrong number
of arguments". I then tried the following update which I think is correct
(added parentheses around the else clause):

(define-key jde-mode-map [return]
 (lambda ()
   (interactive)
   (if (save-excursion
         (backward-char)
         (not (looking-at "{}?$")))
       (newline-and-indent)
     ;; else
     ((newline-and-indent)
      (newline-and-indent)
      (when (not (looking-at "}"))
         (insert "}")
         (c-indent-command)
         )
      (previous-line)
      (c-indent-command)
      ))))

But then I got an error message saying: "if: Invalid function:
(newline-and-indent)". Where is newline-and-indent defined?

/ Petter



My emacs (21.3.50) allows multiple else expressions, perhaps earlier elisps don't. You need a progn
around the else expressions


(define-key jde-mode-map [return]
 (lambda ()
   (interactive)
   (if (save-excursion
         (backward-char)
         (not (looking-at "{}?$")))
       (newline-and-indent)
     ;; else
     (progn
   (newline-and-indent)
   (newline-and-indent)
   (when (not (looking-at "}"))
     (insert "}")
     (c-indent-command)
     )
   (previous-line)
   (c-indent-command)
   ))))

This should fix the "if: Invalid function: (newline-and-indent)" problem too. Emacs is trying to call the value returned by (newline-and-indent)
as a function.


If not I have newline-and-indent in emacs/lisp/simple.el:

(defun newline-and-indent ()
 "Insert a newline, then indent according to major mode.
Indentation is done using the value of `indent-line-function'.
In programming language modes, this is the same as TAB.
In some text modes, where TAB inserts a tab, this command indents to the
column specified by the function `current-left-margin'."
 (interactive "*")
 (delete-horizontal-space t)
 (newline)
 (indent-according-to-mode))


-----Original Message-----
From: Suraj Acharya [mailto:[EMAIL PROTECTED] Sent: den 3 mars 2004 13:55
To: [EMAIL PROTECTED]
Subject: auto newline, indent and close brace on open brace, return



Intellij does this thing where if you type a open curly brace and then a return it inserts a matching closing brace and puts point
on a empty line between the braces and indents the two newlines. So if before you had:


 pubic void function () {
                                  ^
You now have:

pubic void function () {
}^


Simple but handy.

(define-key jde-mode-map [return]
 (lambda ()
   (interactive)
   (if (save-excursion
         (backward-char)
         (not (looking-at "{}?$")))
       (newline-and-indent)
     ;; else
     (newline-and-indent)
     (newline-and-indent)
     (when (not (looking-at "}"))
       (insert "}")
       (c-indent-command)
       )
     (previous-line)
     (c-indent-command)
     )))


Suraj


PS: The above is slightly more complicated than needs be because I use Erik Hilsdale's balanced.el which, among other things, automatically
inserts matching close parenthese when you type any kind of open parentheses.













Reply via email to