(defvar jde-auto-indent t "Automatically indent new lines.")
(defvar jde-gen-embrace t "Typing enter after a close brace at the end of a line will insert a matching closing brace (if required) and put point on a empty line between the braces and indent the new lines.
So if before you had:
pubic void function () {
^
You now have:pubic void function () {
} ^
Point must be at the end of the line, or at a } character followed by the end of the line.
If it thinks a matching close brace already exists a new one is not inserted.
Before:
pubic void function () {
} ^
After:
pubic void function () {
} ^
It does this by checking if the line containing the matching close brace is already correctly indented. If it is not then this close brace probabaly does not match the open brace at point and so a new close brace is inserted.")
(defvar jde-gen-complete-multi-line-string t
"Typing enter at the end of a line with an unterminated string
constant will result in the generation of the required string by
adding a \\n character to the end of the line, closing the string,
adding the string append operator (+) and starting a new string
on the next line. Before:
String a = \"This is a multi-line string
^
After:
String a = \"This is a multi-line string\\n\" +
\"
^")
(defun jde-mode-return ()
"Check if variables 'jde-gen-embrace or
'jde-gen-complete-multi-line-string are set and handle them
appropriately. Automatically indents the new line if jde-auto-indent is true or if invoked by C-j"
(interactive)
(let ((jde-auto-indent (or jde-auto-indent (equal last-command-char 10))))
(cond
((and jde-gen-complete-multi-line-string (jde-line-has-incomplete-string))
(jde-multi-line-string-enter))
((and jde-gen-embrace (jde-line-end-at-open-brace))
(jde-gen-embrace))
(t (jde-newline))))
)
(defun jde-line-end-at-open-brace ()
(and (= (preceding-char) ?{) (looking-at "}?$")))(defun jde-gen-embrace () "Adds a new line and inserts a matching close brace if required. See variable 'jde-gen-embrace.
Assumes that 'jde-line-end-at-open-brace has been called and it
returned true."
(interactive)
(let ((open-indent (current-indentation)))
(when (looking-at "}")
;; if there is a } after point, put it on a new line and come back
(jde-newline)
(end-of-line 0) ;; move to end of previous line
)
;; make a blank line
(jde-newline)
;; Look for the matching close brace; if it is indented at the
;; level as the open brace or if indent-according-to-mode thinks
;; it is correctly indented, don't add a new close brace.
(unless (save-excursion
(condition-case err
(progn
(up-list 1)
(let* ((old-close-indent (current-indentation))
(dummy (indent-according-to-mode))
(new-close-indent (current-indentation))
;; save return value
(ret (and (= (preceding-char) ?})
(or (= open-indent old-close-indent) (= old-close-indent new-close-indent)))))
;; set indent back to what it was
(indent-line-to old-close-indent)
;; return result
ret
))
(error nil)))
;; add the } on a new indented line
(insert "}")
(indent-according-to-mode)
(end-of-line 0) ;; move to end of previous line
(jde-newline))))
(defun jde-line-has-incomplete-string ()
"Returns true if point is at the end of the line and the
current line has a mismatched doublequote character."
(let* ((end (point))
(beg (save-excursion (beginning-of-line) (point)))
(count 0))
(save-excursion
(while (search-backward "\"" beg t)
(unless (or (= (preceding-char) ?\\) (and (= (preceding-char) ?') (looking-at "\"'")))
(incf count)
)
))
(= (mod count 2) 1)
))
(defun jde-multi-line-string-enter ()
"Ends an unterminated string constant and coninues it on the
next line. See variable 'jde-gen-complete-multi-line-string.
Assumes that 'jde-line-has-incomplete-string was called and it returned true. "
(interactive)
(insert "\\n\" +")
(jde-newline)
(insert "\"")
)
(defun jde-newline () (if jde-auto-indent (newline-and-indent) (newline)))
(define-key jde-mode-map [return] 'jde-mode-return) (define-key jde-mode-map [(control j)] 'jde-mode-return)
