diff options
| author | Ravi R Kiran <aine.marina@gmail.com> | 2014-01-06 05:41:26 (GMT) |
|---|---|---|
| committer | Ravikiran Rajagopal <aine.marina@gmail.com> | 2014-01-06 05:41:26 (GMT) |
| commit | f046c731abbc8634b49df6c9d4ef99889260bf9d (patch) | |
| tree | bd7b65ea6b9da815b102d4834a7b064e888fd937 /ravi-init-cpp.el | |
| parent | db5d0a6ef360ce752143acdd4760b8ad88b83757 (diff) | |
| download | dotemacs-f046c731abbc8634b49df6c9d4ef99889260bf9d.zip dotemacs-f046c731abbc8634b49df6c9d4ef99889260bf9d.tar.gz dotemacs-f046c731abbc8634b49df6c9d4ef99889260bf9d.tar.bz2 | |
Add spaces after/before parens like I want
Diffstat (limited to 'ravi-init-cpp.el')
| -rw-r--r-- | ravi-init-cpp.el | 82 |
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) |
