Skip to content

Instantly share code, notes, and snippets.

@ainame
Created November 4, 2013 09:50
Show Gist options
  • Save ainame/7300382 to your computer and use it in GitHub Desktop.
Save ainame/7300382 to your computer and use it in GitHub Desktop.
(require 'cl-lib)
(defvar newline-or-open-line/open-line-count nil)
(make-variable-buffer-local 'newline-or-open-line/open-line-count)
(defun newline-or-open-line/increment-open-line ()
(push 'open-line newline-or-open-line/open-line-count))
(defun newline-or-open-line/clear-open-line-count ()
(progn
(setq newline-or-open-line/open-line-count nil)))
(defun newline-or-open-line/post-command-hook ()
(if (not (eq last-command-event 13))
(newline-or-open-line/clear-open-line-count)
nil))
(add-hook 'post-command-hook 'newline-or-open-line/post-command-hook)
(defun newline-or-open-line ()
"newline-or-openline is a new command for merging C-m and C-o"
(interactive)
(let ((string-exists-before-cursor (string-match "[^\\\s\\\n\\\t]" (buffer-substring (point-at-bol) (point))))
(string-exists-after-cursor (string-match "[^\\\s\\\n\\\t]" (buffer-substring (point) (point-at-eol)))))
(cond ((or (and (eq (length newline-or-open-line/open-line-count) 0)
(eolp))
(and (>= (length newline-or-open-line/open-line-count) 2)
(or (not string-exists-after-cursor)
(and string-exists-before-cursor string-exists-after-cursor)))
(and (eq (length newline-or-open-line/open-line-count) 0)
(and string-exists-before-cursor string-exists-after-cursor)))
(progn
(newline)
(indent-according-to-mode)
(newline-or-open-line/clear-open-line-count)))
(t (progn
(open-line 1)
(indent-according-to-mode)
(save-excursion
(forward-line)
(indent-according-to-mode)
(previous-line))
(newline-or-open-line/increment-open-line))))))
(define-key global-map (kbd "C-m") 'newline-or-open-line)
(provide 'newline-or-open-line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment