Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Last active December 21, 2015 22:38
Show Gist options
  • Save tam17aki/6376168 to your computer and use it in GitHub Desktop.
Save tam17aki/6376168 to your computer and use it in GitHub Desktop.
(require 'smartchr nil t)
(defun smartchr-braces ()
"Insert a pair of braces like below.
\n {\n `!!'\n}"
;; foo {
;; `!!'
;; }
(lexical-let (beg end)
(smartchr-make-struct
:insert-fn (lambda ()
(setq beg (point))
(insert "{\n\n}")
(indent-region beg (point))
(forward-line -1)
(indent-according-to-mode)
(goto-char (point-at-eol))
(setq end (save-excursion
(re-search-forward
"[[:space:][:cntrl:]]+}" nil t))))
:cleanup-fn (lambda ()
(delete-region beg end)))))
(defun smartchr-comment ()
"Insert a multiline comment like below.
\n/*\n * `!!'\n */"
;; /*
;; * `!!'
;; */
(lexical-let (beg end)
(smartchr-make-struct
:insert-fn (lambda ()
(setq beg (point))
(insert "/*\n* \n*/")
(indent-region beg (point))
(setq end (point))
(forward-line -1)
(goto-char (point-at-eol)))
:cleanup-fn (lambda ()
(delete-region beg end)))))
(defun smartchr-semicolon ()
"Insert a semicolon at end of line."
(smartchr-make-struct
:insert-fn (lambda ()
(save-excursion
(goto-char (point-at-eol))
(insert ";")))
:cleanup-fn (lambda ()
(save-excursion
(goto-char (point-at-eol))
(delete-char -1)))))
(defun smartchr-keybindings-generic ()
(local-set-key (kbd "=") (smartchr '("=" " = " " == ")))
(local-set-key (kbd "+") (smartchr '("+" " + " "++" " += ")))
(local-set-key (kbd "-") (smartchr '("-" " - " "--" " -= ")))
(local-set-key (kbd "(") (smartchr '("(`!!')" "(")))
(local-set-key (kbd "[") (smartchr '("[`!!']" "[[`!!']]" "[")))
(local-set-key (kbd "{") (smartchr '("{" "{`!!'}" smartchr-braces)))
(local-set-key (kbd "`") (smartchr '("\``!!''" "\`")))
(local-set-key (kbd "\"") (smartchr '("\"`!!'\"" "\"")))
(local-set-key (kbd "\'") (smartchr '("\'`!!'\'" "\'")))
(local-set-key (kbd ">") (smartchr '(">" " > " " >= " " >> " " => ")))
(local-set-key (kbd "<") (smartchr '("<" " < " " <= " "<`!!'>" " << " )))
(local-set-key (kbd ",") (smartchr '(", " ",")))
(local-set-key (kbd ".") (smartchr '("." " . " "... ")))
(local-set-key (kbd "?") (smartchr '("?" "? `!!' " "<?`!!'?>")))
(local-set-key (kbd "!") (smartchr '("!" " != ")))
(local-set-key (kbd "&") (smartchr '("&" " && ")))
(local-set-key (kbd "|") (smartchr '("|" " || ")))
(local-set-key (kbd "/") (smartchr '("/" "/* `!!' */" smartchr-comment)))
(local-set-key (kbd ";") (smartchr '(";" "; " smartchr-semicolon))))
(defun smartchr-keybindings-lisp ()
(local-set-key (kbd "(") (smartchr '("(`!!')" "(")))
(local-set-key (kbd "[") (smartchr '("[`!!']" "[[`!!']]" "[")))
(local-set-key (kbd "`") (smartchr '("\``!!''" "\`")))
(local-set-key (kbd "\"") (smartchr '("\"`!!'\"" "\"")))
(local-set-key (kbd ".") (smartchr '("." " . " "... "))))
(defun smartchr-keybindings-perl ()
(local-set-key (kbd "=") (smartchr '("=" " = " " == ")))
(local-set-key (kbd "+") (smartchr '("+" " + " "++" " += ")))
(local-set-key (kbd "-") (smartchr '("-" " - " "--" " -= ")))
(local-set-key (kbd "(") (smartchr '("(`!!')" "(")))
(local-set-key (kbd "[") (smartchr '("[`!!']" "[[`!!']]" "[")))
(local-set-key (kbd "{") (smartchr '("{" "{`!!'}" smartchr-braces)))
(local-set-key (kbd "`") (smartchr '("\``!!''" "\`")))
(local-set-key (kbd "\"") (smartchr '("\"`!!'\"" "\"")))
(local-set-key (kbd "\'") (smartchr '("\'`!!'\'" "\'")))
(local-set-key (kbd ">") (smartchr '(">" " > " " => " " >> ")))
(local-set-key (kbd "<") (smartchr '("<" " < " " << " "<`!!'>")))
(local-set-key (kbd ",") (smartchr '(", " ",")))
(local-set-key (kbd ".") (smartchr '("." " . " "... ")))
(local-set-key (kbd "?") (smartchr '("?" "? `!!' " "<?`!!'?>")))
(local-set-key (kbd "!") (smartchr '("!" " != ")))
(local-set-key (kbd "&") (smartchr '("&" " && ")))
(local-set-key (kbd "|") (smartchr '("|" " || ")))
(local-set-key (kbd "/") (smartchr '("/" " / ")))
(local-set-key (kbd ";") (smartchr '(";" "; " smartchr-semicolon))))
(defun smartchr-keybindings-objc ()
(local-set-key (kbd "@") (smartchr '("@\"`!!'\"" "@")))
(local-set-key (kbd "[") (smartchr '("[`!!']" "[[`!!']]" "[[[`!!']]]" "["))))
(defun smartchr-keybindings-html ()
(local-set-key (kbd "=") (smartchr '("=" " = " " == ")))
(local-set-key (kbd "(") (smartchr '("(`!!')" "(")))
(local-set-key (kbd "[") (smartchr '("[`!!']" "[[`!!']]" "[")))
(local-set-key (kbd "{") (smartchr '("{" "{`!!'}" smartchr-braces)))
(local-set-key (kbd "\"") (smartchr '("\"`!!'\"" "\"")))
(local-set-key (kbd "\'") (smartchr '("\'`!!'\'" "\'")))
(local-set-key (kbd "<") (smartchr '("<`!!'>" "</`!!'>" "<")))
(local-set-key (kbd ",") (smartchr '(", " ",")))
(local-set-key (kbd ".") (smartchr '("." " . ")))
(local-set-key (kbd "?") (smartchr '("?" "? `!!' " "<?`!!'?>")))
(local-set-key (kbd "!") (smartchr '("!" "<!-- `!!' -->")))
(local-set-key (kbd ";") (smartchr '(";" smartchr-semicolon)))
(local-set-key (kbd ":") (smartchr '(": " ":")))
(local-set-key (kbd "/") (smartchr '("/" "/* `!!' */" smartchr-comment))))
(dolist (hook (list
'c-mode-common-hook
'c++-mode-hook
'php-mode-hook
'ruby-mode-hook
'javascript-mode-hook
'js-mode-hook
'js2-mode-hook
'octave-mode-hook
'matlab-mode-hook))
(add-hook hook 'smartchr-keybindings-generic))
(dolist (hook (list
'lisp-mode-hook
'emacs-lisp-mode-hook
'lisp-interaction-mode-hook
'inferior-gauche-mode-hook
'scheme-mode-hook))
(add-hook hook 'smartchr-keybindings-lisp))
(add-hook 'objc-mode-hook 'smartchr-keybindings-objc)
(add-hook 'cperl-mode-hook 'smartchr-keybindings-perl)
(dolist (hook (list
'html-mode-hook
'css-mode-hook))
(add-hook hook 'smartchr-keybindings-html))
(eval-after-load "yasnippet"
'(progn
(remove-hook 'c-mode-common-hook
'(lambda ()
(dolist (k '(":" ">" ";" "<" "{" "}"))
(define-key (symbol-value
(make-local-variable 'yas-keymap))
k 'self-insert-command))))))
(provide 'smartchr-config)
;; smartchr-config.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment