Last active
April 13, 2017 16:12
-
-
Save alphapapa/61c1015f7d1f0d446bc7fd652b7ec4fe to your computer and use it in GitHub Desktop.
Better org-return
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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