2017-07-05 02:33:41 +02:00
|
|
|
;;; org/org/autoload/org.el -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode +org-pretty-mode
|
|
|
|
"TODO"
|
|
|
|
:init-value nil
|
|
|
|
:lighter " *"
|
|
|
|
:group 'evil-org
|
|
|
|
(setq org-hide-emphasis-markers +org-pretty-mode)
|
|
|
|
(org-toggle-pretty-entities)
|
2017-09-28 18:08:20 +02:00
|
|
|
(org-with-silent-modifications
|
|
|
|
;; In case the above un-align tables
|
|
|
|
(org-table-map-tables 'org-table-align t)))
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org|realign-table-maybe ()
|
2017-09-07 17:36:33 +02:00
|
|
|
"Auto-align table under cursor and re-calculate formulas."
|
2017-07-05 02:33:41 +02:00
|
|
|
(when (org-at-table-p)
|
|
|
|
(save-excursion
|
2017-09-07 17:36:33 +02:00
|
|
|
(quiet! (org-table-recalculate)))))
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org|update-cookies ()
|
|
|
|
"Update counts in headlines (aka \"cookies\")."
|
|
|
|
(when (and buffer-file-name (file-exists-p buffer-file-name))
|
|
|
|
(org-update-statistics-cookies t)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/dwim-at-point ()
|
2017-09-07 17:35:09 +02:00
|
|
|
"Do-what-I-mean at point.
|
|
|
|
|
|
|
|
If on a:
|
|
|
|
- checkbox list item or todo heading: toggle it.
|
|
|
|
- clock: update its time.
|
|
|
|
- headline: toggle latex fragments and inline images underneath.
|
|
|
|
- footnote definition: jump to the footnote
|
|
|
|
- table-row or a TBLFM: recalculate the table's formulas
|
|
|
|
- table-cell: clear it and go into insert mode. If this is a formula cell,
|
|
|
|
recaluclate it instead.
|
|
|
|
- babel-call: execute the source block
|
|
|
|
- statistics-cookie: update it.
|
|
|
|
- latex fragment: toggle it.
|
|
|
|
- link: follow it
|
|
|
|
- otherwise, refresh all inline images in current tree."
|
2017-07-05 02:33:41 +02:00
|
|
|
(interactive)
|
|
|
|
(let* ((scroll-pt (window-start))
|
|
|
|
(context (org-element-context))
|
|
|
|
(type (org-element-type context)))
|
2017-09-30 17:06:09 +02:00
|
|
|
;; skip over unimportant contexts
|
2017-10-05 01:18:03 +02:00
|
|
|
(while (and context (memq type '(verbatim code bold italic underline strike-through subscript superscript)))
|
2017-09-30 17:06:09 +02:00
|
|
|
(setq context (org-element-property :parent context)
|
|
|
|
type (org-element-type context)))
|
2017-09-07 17:35:09 +02:00
|
|
|
(pcase type
|
|
|
|
((guard (org-element-property :checkbox (org-element-lineage context '(item) t)))
|
|
|
|
(let ((match (and (org-at-item-checkbox-p) (match-string 1))))
|
|
|
|
(org-toggle-checkbox (if (equal match "[ ]") '(16)))))
|
|
|
|
|
|
|
|
(`headline
|
|
|
|
(cond ((org-element-property :todo-type context)
|
|
|
|
(org-todo
|
|
|
|
(if (eq (org-element-property :todo-type context) 'done) 'todo 'done)))
|
|
|
|
((string= "ARCHIVE" (car-safe (org-get-tags)))
|
|
|
|
(org-force-cycle-archived))
|
|
|
|
(t
|
|
|
|
(org-remove-latex-fragment-image-overlays)
|
|
|
|
(org-toggle-latex-fragment '(4)))))
|
|
|
|
|
|
|
|
(`clock (org-clock-update-time-maybe))
|
|
|
|
|
|
|
|
(`footnote-definition
|
|
|
|
(goto-char (org-element-property :post-affiliated context))
|
|
|
|
(call-interactively #'org-footnote-action))
|
|
|
|
|
2017-11-09 00:35:53 +01:00
|
|
|
((or `planning `timestamp)
|
|
|
|
(org-follow-timestamp-link))
|
2017-09-07 17:35:09 +02:00
|
|
|
|
|
|
|
((or `table `table-row)
|
|
|
|
(if (org-at-TBLFM-p)
|
|
|
|
(org-table-calc-current-TBLFM)
|
|
|
|
(ignore-errors
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (org-element-property :contents-begin context))
|
|
|
|
(org-call-with-arg 'org-table-recalculate (or arg t))))))
|
|
|
|
|
|
|
|
(`table-cell
|
|
|
|
(org-table-blank-field)
|
|
|
|
(org-table-recalculate)
|
|
|
|
(when (and (string-empty-p (string-trim (org-table-get-field)))
|
|
|
|
(bound-and-true-p evil-mode))
|
|
|
|
(evil-change-state 'insert)))
|
|
|
|
|
|
|
|
(`babel-call
|
|
|
|
(org-babel-lob-execute-maybe))
|
|
|
|
|
|
|
|
(`statistics-cookie
|
|
|
|
(save-excursion (org-update-statistics-cookies nil)))
|
|
|
|
|
|
|
|
((or `src-block `inline-src-block)
|
|
|
|
(org-babel-execute-src-block))
|
|
|
|
|
|
|
|
((or `latex-fragment `latex-environment)
|
|
|
|
(org-toggle-latex-fragment))
|
|
|
|
|
|
|
|
(`link
|
|
|
|
(let ((path (org-element-property :path (org-element-lineage context '(link) t))))
|
|
|
|
(if (and path (image-type-from-file-name path))
|
|
|
|
(+org/refresh-inline-images)
|
|
|
|
(org-open-at-point))))
|
|
|
|
|
|
|
|
(_ (+org/refresh-inline-images)))
|
2017-07-05 02:33:41 +02:00
|
|
|
(set-window-start nil scroll-pt)))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/indent ()
|
|
|
|
"Indent the current item (header or item). Otherwise, forward to
|
|
|
|
`self-insert-command'."
|
|
|
|
(interactive)
|
|
|
|
(cond ((org-at-item-p)
|
|
|
|
(org-indent-item-tree))
|
|
|
|
((org-at-heading-p)
|
|
|
|
(ignore-errors (org-demote)))
|
2017-04-11 08:30:09 -04:00
|
|
|
((org-in-src-block-p t)
|
|
|
|
(doom/dumb-indent))
|
2017-05-19 03:29:00 +02:00
|
|
|
(t
|
|
|
|
(call-interactively #'self-insert-command))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-04-10 01:10:42 -04:00
|
|
|
(defun +org/indent-or-next-field-or-yas-expand ()
|
|
|
|
"Depending on the context either a) indent the current line, b) go the next
|
|
|
|
table field or c) run `yas-expand'."
|
2017-02-19 19:01:47 -05:00
|
|
|
(interactive)
|
2017-04-10 01:10:42 -04:00
|
|
|
(call-interactively
|
|
|
|
(cond ((and (bound-and-true-p yas-minor-mode)
|
|
|
|
(yas--templates-for-key-at-point))
|
2017-05-19 03:29:00 +02:00
|
|
|
#'yas-expand)
|
2017-04-10 01:10:42 -04:00
|
|
|
((org-at-table-p)
|
2017-05-19 03:29:00 +02:00
|
|
|
#'org-table-next-field)
|
2017-04-10 01:10:42 -04:00
|
|
|
(t
|
2017-05-19 03:29:00 +02:00
|
|
|
#'+org/indent))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/dedent ()
|
|
|
|
"Dedent the current item (header or item). Otherwise, forward to
|
|
|
|
`self-insert-command'."
|
|
|
|
(interactive)
|
|
|
|
(cond ((org-at-item-p)
|
2017-05-19 03:29:00 +02:00
|
|
|
(org-list-indent-item-generic
|
|
|
|
-1 nil
|
|
|
|
(save-excursion
|
|
|
|
(when (org-region-active-p)
|
|
|
|
(goto-char (region-beginning)))
|
|
|
|
(org-list-struct))))
|
2017-02-19 19:01:47 -05:00
|
|
|
((org-at-heading-p)
|
|
|
|
(ignore-errors (org-promote)))
|
2017-05-19 03:29:00 +02:00
|
|
|
(t
|
|
|
|
(call-interactively #'self-insert-command))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/dedent-or-prev-field ()
|
|
|
|
"Depending on the context either dedent the current item or go the previous
|
|
|
|
table field."
|
|
|
|
(interactive)
|
2017-05-19 03:29:00 +02:00
|
|
|
(call-interactively
|
|
|
|
(if (org-at-table-p)
|
|
|
|
#'org-table-previous-field
|
|
|
|
#'+org/dedent)))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/insert-item (direction)
|
|
|
|
"Inserts a new heading, table cell or item, depending on the context.
|
|
|
|
DIRECTION can be 'above or 'below.
|
|
|
|
|
|
|
|
I use this instead of `org-insert-item' or `org-insert-heading' which are too
|
|
|
|
opinionated and perform this simple task incorrectly (e.g. whitespace in the
|
|
|
|
wrong places)."
|
|
|
|
(interactive)
|
2017-03-08 14:39:55 -05:00
|
|
|
(let* ((context (org-element-lineage
|
|
|
|
(org-element-context)
|
|
|
|
'(table table-row headline inlinetask item plain-list)
|
|
|
|
t))
|
2017-02-19 19:01:47 -05:00
|
|
|
(type (org-element-type context)))
|
2017-09-03 20:59:52 +02:00
|
|
|
(cond ((memq type '(item plain-list))
|
2017-05-19 17:17:39 +02:00
|
|
|
(let ((marker (org-element-property :bullet context))
|
|
|
|
(pad (save-excursion
|
|
|
|
(back-to-indentation)
|
|
|
|
(- (point) (line-beginning-position)))))
|
2017-03-02 19:49:19 -05:00
|
|
|
(pcase direction
|
2017-02-19 19:01:47 -05:00
|
|
|
('below
|
2017-10-05 01:18:31 +02:00
|
|
|
(org-end-of-item)
|
|
|
|
(goto-char (line-beginning-position))
|
|
|
|
(insert (make-string pad 32) (or marker ""))
|
|
|
|
(save-excursion (insert "\n")))
|
2017-02-19 19:01:47 -05:00
|
|
|
('above
|
|
|
|
(goto-char (line-beginning-position))
|
2017-09-03 20:59:52 +02:00
|
|
|
(insert (make-string pad 32) (or marker ""))
|
2017-02-19 19:01:47 -05:00
|
|
|
(save-excursion (insert "\n")))))
|
|
|
|
(when (org-element-property :checkbox context)
|
|
|
|
(insert "[ ] ")))
|
2017-05-19 03:29:00 +02:00
|
|
|
|
2017-02-19 19:01:47 -05:00
|
|
|
((memq type '(table table-row))
|
2017-06-10 01:52:44 +02:00
|
|
|
(pcase direction
|
2017-02-19 19:01:47 -05:00
|
|
|
('below (org-table-insert-row t))
|
2017-11-06 00:09:15 +01:00
|
|
|
('above (org-shiftmetadown))))
|
2017-05-19 03:29:00 +02:00
|
|
|
|
2017-09-03 20:59:52 +02:00
|
|
|
((memq type '(headline inlinetask))
|
2017-10-08 01:50:57 +02:00
|
|
|
(let ((level (if (eq (org-element-type context) 'headline)
|
|
|
|
(org-element-property :level context)
|
|
|
|
1)))
|
2017-06-10 01:52:44 +02:00
|
|
|
(pcase direction
|
2017-02-19 19:01:47 -05:00
|
|
|
('below
|
|
|
|
(let ((at-eol (= (point) (1- (line-end-position)))))
|
|
|
|
(goto-char (line-end-position))
|
|
|
|
(org-end-of-subtree)
|
|
|
|
(insert (concat "\n"
|
|
|
|
(when (= level 1)
|
|
|
|
(if at-eol
|
|
|
|
(ignore (cl-incf level))
|
|
|
|
"\n"))
|
|
|
|
(make-string level ?*)
|
|
|
|
" "))))
|
|
|
|
('above
|
|
|
|
(org-back-to-heading)
|
|
|
|
(org-insert-heading)
|
|
|
|
(when (= level 1)
|
|
|
|
(save-excursion (insert "\n")))))
|
|
|
|
(when (org-element-property :todo-type context)
|
2017-02-24 23:28:01 -05:00
|
|
|
(org-todo 'todo))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
2017-05-19 14:01:38 +02:00
|
|
|
(t (user-error "Not a valid list, heading or table")))
|
|
|
|
|
|
|
|
(when (bound-and-true-p evil-mode)
|
|
|
|
(evil-append-line 1))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-07-05 02:33:41 +02:00
|
|
|
(defun +org-get-property (name &optional _file) ; TODO Add FILE
|
|
|
|
"Get a propery from an org file."
|
2017-05-19 17:08:49 +02:00
|
|
|
(save-excursion
|
2017-07-05 02:33:41 +02:00
|
|
|
(goto-char 1)
|
|
|
|
(re-search-forward (format "^#\\+%s:[ \t]*\\([^\n]+\\)" (upcase name)) nil t)
|
|
|
|
(buffer-substring-no-properties (match-beginning 1) (match-end 1))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/refresh-inline-images ()
|
|
|
|
"Refresh image previews in the current heading/tree."
|
|
|
|
(interactive)
|
|
|
|
(if (> (length org-inline-image-overlays) 0)
|
|
|
|
(org-remove-inline-images)
|
|
|
|
(org-display-inline-images
|
|
|
|
t t
|
|
|
|
(if (org-before-first-heading-p)
|
|
|
|
(line-beginning-position)
|
|
|
|
(save-excursion (org-back-to-heading) (point)))
|
|
|
|
(if (org-before-first-heading-p)
|
|
|
|
(line-end-position)
|
|
|
|
(save-excursion (org-end-of-subtree) (point))))))
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/toggle-checkbox ()
|
|
|
|
"Toggle the presence of a checkbox in the current item."
|
|
|
|
(interactive)
|
|
|
|
(org-toggle-checkbox '(4)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/toggle-fold ()
|
|
|
|
"Toggle the local fold at the point (as opposed to cycling through all levels
|
2017-09-07 17:36:18 +02:00
|
|
|
with `org-cycle'). Also:
|
|
|
|
|
|
|
|
+ If in a babel block, removes result blocks.
|
|
|
|
+ If in a table, realign it, if necessary."
|
2017-07-05 02:33:41 +02:00
|
|
|
(interactive)
|
|
|
|
(save-excursion
|
|
|
|
(org-beginning-of-line)
|
2017-11-09 00:34:49 +01:00
|
|
|
(cond ((org-at-table-p)
|
2017-09-07 17:36:18 +02:00
|
|
|
(org-table-align))
|
|
|
|
((org-in-src-block-p)
|
2017-07-05 02:33:41 +02:00
|
|
|
(org-babel-remove-result))
|
|
|
|
((org-at-heading-p)
|
|
|
|
(outline-toggle-children))
|
|
|
|
((org-at-item-p)
|
|
|
|
(let ((window-beg (window-start)))
|
|
|
|
(org-cycle)
|
|
|
|
(set-window-start nil window-beg))))))
|
2017-10-05 01:23:56 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +org/remove-link ()
|
|
|
|
"Unlink the text at point."
|
|
|
|
(interactive)
|
|
|
|
(unless (org-in-regexp org-bracket-link-regexp 1)
|
|
|
|
(user-error "No link at point"))
|
|
|
|
(save-excursion
|
|
|
|
(let ((remove (list (match-beginning 0) (match-end 0)))
|
|
|
|
(description (if (match-end 3)
|
2017-12-16 23:48:29 -05:00
|
|
|
(match-string-no-properties 3)
|
|
|
|
(match-string-no-properties 1))))
|
2017-10-05 01:23:56 +02:00
|
|
|
(apply #'delete-region remove)
|
|
|
|
(insert description))))
|
|
|
|
|