Skip to content

Instantly share code, notes, and snippets.

@alphapapa
Last active April 13, 2017 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphapapa/61c1015f7d1f0d446bc7fd652b7ec4fe to your computer and use it in GitHub Desktop.
Save alphapapa/61c1015f7d1f0d446bc7fd652b7ec4fe to your computer and use it in GitHub Desktop.
Better org-return
(defun ap/org-return (&optional ignore)
"Add new list item, heading or table row with RET.
A double return on an empty element deletes it. Use a prefix arg
to get regular RET. "
;; See https://gist.github.com/alphapapa/61c1015f7d1f0d446bc7fd652b7ec4fe and
;; http://kitchingroup.cheme.cmu.edu/blog/2017/04/09/A-better-return-in-org-mode/
(interactive "P")
(if ignore
(org-return)
(cond ((eq 'link (car (org-element-context)))
;; Open links like usual
(org-open-at-point-global))
((and (fboundp 'org-inlinetask-in-task-p) (org-inlinetask-in-task-p))
;; It doesn't make sense to add headings in inline tasks. Thanks Anders
;; Johansson!
(org-return))
((org-at-item-checkbox-p)
;; Add checkboxes
(org-insert-todo-heading nil))
((and (org-in-item-p) (not (bolp)))
;; Lists end with two blank lines, so we need to make sure we are also not
;; at the beginning of a line to avoid a loop where a new entry gets
;; created with only one blank line.
(if (org-element-property :contents-begin (org-element-context))
(org-insert-heading)
(beginning-of-line)
(delete-region (line-beginning-position) (line-end-position))
(org-return)))
((org-at-heading-p)
(if (s-present? (org-element-property :title (org-element-context)))
(progn
(org-end-of-meta-data)
(org-insert-heading))
(beginning-of-line)
(delete-region (line-beginning-position) (line-end-position))))
((org-at-table-p)
(if (--any? (string-empty-p it)
(nth (- (org-table-current-dline) 1) (org-table-to-lisp)))
(org-return)
;; Empty row
(beginning-of-line)
(delete-region (line-beginning-position) (line-end-position))
(org-return)))
(t
(org-return)))))
(define-key org-mode-map (kbd "RET") 'ap/org-return)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment