summaryrefslogtreecommitdiffstats
path: root/ravi-init-cpp.el
diff options
context:
space:
mode:
Diffstat (limited to 'ravi-init-cpp.el')
-rw-r--r--ravi-init-cpp.el82
1 files changed, 66 insertions, 16 deletions
diff --git a/ravi-init-cpp.el b/ravi-init-cpp.el
index dcea098..f9ca601 100644
--- a/ravi-init-cpp.el
+++ b/ravi-init-cpp.el
@@ -301,23 +301,73 @@ this to 3 makes header-protection define KIG_MISC_NEWTYPE_H for a file named
:config
(progn
- (defun insert-comma (arg)
- (interactive "*P")
- (let* ((ch (char-after))
- (spacep (not (or (eq ch ? )
- (c-in-literal)
- arg))))
- (self-insert-command (prefix-numeric-value arg))
- (if spacep
- (insert " "))))
-
- (defun insert-semicolon (arg)
- (interactive "*P")
- (self-insert-command (prefix-numeric-value arg))
- (newline-and-indent))
-
- (bind-key "," 'insert-comma c-mode-base-map)
+ (defun ravi/add-space-before-paren-c (&rest ignored)
+ (message "Handler invoked")
+ (when (looking-back "\\b\\(for\\|while\\|if\\|switch\\)(")
+ (save-excursion
+ (backward-char)
+ (insert-char ?\s)
+ )
+ ))
+
+ (defadvice c-electric-paren (after ravi/add-space-around-parens activate)
+ (unless (c-in-literal)
+ (cond
+ ((looking-back "([[:space:]]*")
+ (progn
+ ;; Open parenthesis was the last command; add a space before it if
+ ;; necessary, and ensure that the point is placed after a space
+ (save-excursion
+ (re-search-backward "([[:space:]]*")
+ (unless (looking-at "(") (message "Logic error; opening paren not found"))
+ (unless (or (c-on-identifier)
+ (looking-back "[[:space:]]+"))
+ (insert-char ?\s)
+ )
+ )
+ (if (looking-at " ") (forward-char) (insert-char ?\s))
+ )
+ )
+ ((looking-back ")[[:space:]]*")
+ ;; Closing parenthesis was the last command; add spaces if necessary
+ (progn
+ ;; Compact empty function calls
+ (when (looking-back "([[:space:]]+)[[:space:]]*")
+ (save-excursion
+ (re-search-backward ")[[:space:]]*")
+ (delete-horizontal-space)
+ )
+ )
+
+ ;; Add space before closing parenthesis
+ (unless (looking-back "()[[:space:]]*")
+ (save-excursion
+ (re-search-backward ")[[:space:]]*")
+ (unless (looking-at ")") (message "Logic error; closing paren not found"))
+ (unless (looking-back " ") (insert-char ?\s)))
+ )
+
+ (let (need-to-add-newline)
+ (save-excursion
+ (c-backward-sexp)
+ (c-backward-syntactic-ws)
+ ;; FIXME: do-while loops are not handled correctly
+ (setq need-to-add-newline (looking-back "\\b\\(for\\|while\\|if\\|switch\\|catch\\)"))
+ )
+ (if need-to-add-newline
+ (c-newline-and-indent)
+ ;; Add space after paren if necessary
+ (if (looking-at " ") (forward-char) (insert-char ?\s))))
+ )
+ )
+ ))
+ )
+
+ (defadvice c-electric-semi&comma (after ravi/add-space-after-comma activate)
+ (when (looking-back ",")
+ (if (looking-at " [^)]") (forward-char) (insert-char ?\s))))
+ (bind-key "<return>" 'c-newline-and-indent c-mode-base-map)
(bind-key "<M-f8>" 'compile-dwim c-mode-base-map)
(bind-key "<M-f5>" 'hs-hide-block c-mode-base-map)