2019-07-27 17:06:55 +02:00
|
|
|
|
;;; lang/org/autoload/org.el -*- lexical-binding: t; -*-
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
2019-07-23 18:33:00 +02:00
|
|
|
|
;;
|
|
|
|
|
;;; Helpers
|
|
|
|
|
|
2020-07-24 19:25:57 -04:00
|
|
|
|
(defun +org--toggle-inline-images-in-subtree (&optional beg end refresh)
|
|
|
|
|
"Refresh inline image previews in the current heading/tree."
|
2022-02-03 23:26:27 -08:00
|
|
|
|
(let* ((beg (or beg
|
|
|
|
|
(if (org-before-first-heading-p)
|
|
|
|
|
(save-excursion (point-min))
|
|
|
|
|
(save-excursion (org-back-to-heading) (point)))))
|
|
|
|
|
(end (or end
|
|
|
|
|
(if (org-before-first-heading-p)
|
|
|
|
|
(save-excursion (org-next-visible-heading 1) (point))
|
|
|
|
|
(save-excursion (org-end-of-subtree) (point)))))
|
|
|
|
|
(overlays (cl-remove-if-not (lambda (ov) (overlay-get ov 'org-image-overlay))
|
|
|
|
|
(ignore-errors (overlays-in beg end)))))
|
2020-07-24 19:25:57 -04:00
|
|
|
|
(dolist (ov overlays nil)
|
|
|
|
|
(delete-overlay ov)
|
|
|
|
|
(setq org-inline-image-overlays (delete ov org-inline-image-overlays)))
|
|
|
|
|
(when (or refresh (not overlays))
|
|
|
|
|
(org-display-inline-images t t beg end)
|
|
|
|
|
t)))
|
2019-10-25 20:00:06 -04:00
|
|
|
|
|
|
|
|
|
(defun +org--insert-item (direction)
|
2020-07-26 15:42:50 -04:00
|
|
|
|
(let ((context (org-element-lineage
|
|
|
|
|
(org-element-context)
|
|
|
|
|
'(table table-row headline inlinetask item plain-list)
|
|
|
|
|
t)))
|
|
|
|
|
(pcase (org-element-type context)
|
|
|
|
|
;; Add a new list item (carrying over checkboxes if necessary)
|
|
|
|
|
((or `item `plain-list)
|
2022-03-30 18:06:40 +00:00
|
|
|
|
(let ((orig-point (point)))
|
|
|
|
|
;; Position determines where org-insert-todo-heading and `org-insert-item'
|
|
|
|
|
;; insert the new list item.
|
|
|
|
|
(if (eq direction 'above)
|
|
|
|
|
(org-beginning-of-item)
|
|
|
|
|
(end-of-line))
|
|
|
|
|
(let* ((ctx-item? (eq 'item (org-element-type context)))
|
|
|
|
|
(ctx-cb (org-element-property :contents-begin context))
|
|
|
|
|
;; Hack to handle edge case where the point is at the
|
|
|
|
|
;; beginning of the first item
|
|
|
|
|
(beginning-of-list? (and (not ctx-item?)
|
|
|
|
|
(= ctx-cb orig-point)))
|
|
|
|
|
(item-context (if beginning-of-list?
|
|
|
|
|
(org-element-context)
|
|
|
|
|
context))
|
|
|
|
|
;; Horrible hack to handle edge case where the
|
|
|
|
|
;; line of the bullet is empty
|
|
|
|
|
(ictx-cb (org-element-property :contents-begin item-context))
|
|
|
|
|
(empty? (and (eq direction 'below)
|
|
|
|
|
;; in case contents-begin is nil, or contents-begin
|
|
|
|
|
;; equals the position end of the line, the item is
|
|
|
|
|
;; empty
|
|
|
|
|
(or (not ictx-cb)
|
|
|
|
|
(= ictx-cb
|
|
|
|
|
(1+ (point))))))
|
|
|
|
|
(pre-insert-point (point)))
|
|
|
|
|
;; Insert dummy content, so that `org-insert-item'
|
|
|
|
|
;; inserts content below this item
|
|
|
|
|
(when empty?
|
|
|
|
|
(insert " "))
|
|
|
|
|
(org-insert-item (org-element-property :checkbox context))
|
|
|
|
|
;; Remove dummy content
|
|
|
|
|
(when empty?
|
|
|
|
|
(delete-region pre-insert-point (1+ pre-insert-point))))))
|
2020-07-26 15:42:50 -04:00
|
|
|
|
;; Add a new table row
|
|
|
|
|
((or `table `table-row)
|
|
|
|
|
(pcase direction
|
|
|
|
|
('below (save-excursion (org-table-insert-row t))
|
|
|
|
|
(org-table-next-row))
|
|
|
|
|
('above (save-excursion (org-shiftmetadown))
|
|
|
|
|
(+org/table-previous-row))))
|
2019-10-25 20:00:06 -04:00
|
|
|
|
|
2020-07-26 15:42:50 -04:00
|
|
|
|
;; Otherwise, add a new heading, carrying over any todo state, if
|
|
|
|
|
;; necessary.
|
|
|
|
|
(_
|
|
|
|
|
(let ((level (or (org-current-level) 1)))
|
|
|
|
|
;; I intentionally avoid `org-insert-heading' and the like because they
|
|
|
|
|
;; impose unpredictable whitespace rules depending on the cursor
|
|
|
|
|
;; position. It's simpler to express this command's responsibility at a
|
|
|
|
|
;; lower level than work around all the quirks in org's API.
|
|
|
|
|
(pcase direction
|
|
|
|
|
(`below
|
|
|
|
|
(let (org-insert-heading-respect-content)
|
|
|
|
|
(goto-char (line-end-position))
|
|
|
|
|
(org-end-of-subtree)
|
|
|
|
|
(insert "\n" (make-string level ?*) " ")))
|
|
|
|
|
(`above
|
|
|
|
|
(org-back-to-heading)
|
|
|
|
|
(insert (make-string level ?*) " ")
|
|
|
|
|
(save-excursion (insert "\n"))))
|
2022-09-16 13:14:36 +02:00
|
|
|
|
(run-hooks 'org-insert-heading-hook)
|
2020-07-26 15:42:50 -04:00
|
|
|
|
(when-let* ((todo-keyword (org-element-property :todo-keyword context))
|
|
|
|
|
(todo-type (org-element-property :todo-type context)))
|
|
|
|
|
(org-todo
|
|
|
|
|
(cond ((eq todo-type 'done)
|
|
|
|
|
;; Doesn't make sense to create more "DONE" headings
|
|
|
|
|
(car (+org-get-todo-keywords-for todo-keyword)))
|
|
|
|
|
(todo-keyword)
|
|
|
|
|
('todo)))))))
|
2019-10-25 20:00:06 -04:00
|
|
|
|
|
|
|
|
|
(when (org-invisible-p)
|
|
|
|
|
(org-show-hidden-entry))
|
2020-01-25 19:38:29 +02:00
|
|
|
|
(when (and (bound-and-true-p evil-local-mode)
|
|
|
|
|
(not (evil-emacs-state-p)))
|
2019-10-25 20:00:06 -04:00
|
|
|
|
(evil-insert 1))))
|
2018-06-04 18:43:51 +02:00
|
|
|
|
|
2018-10-19 15:43:24 -04:00
|
|
|
|
;;;###autoload
|
2019-10-25 20:00:06 -04:00
|
|
|
|
(defun +org-get-todo-keywords-for (&optional keyword)
|
|
|
|
|
"Returns the list of todo keywords that KEYWORD belongs to."
|
2018-10-19 15:43:24 -04:00
|
|
|
|
(when keyword
|
2019-10-25 20:00:06 -04:00
|
|
|
|
(cl-loop for (type . keyword-spec)
|
|
|
|
|
in (cl-remove-if-not #'listp org-todo-keywords)
|
|
|
|
|
for keywords =
|
|
|
|
|
(mapcar (lambda (x) (if (string-match "^\\([^(]+\\)(" x)
|
|
|
|
|
(match-string 1 x)
|
|
|
|
|
x))
|
|
|
|
|
keyword-spec)
|
2018-10-19 15:43:24 -04:00
|
|
|
|
if (eq type 'sequence)
|
|
|
|
|
if (member keyword keywords)
|
|
|
|
|
return keywords)))
|
|
|
|
|
|
2018-02-13 18:19:35 -05:00
|
|
|
|
|
|
|
|
|
;;
|
2019-04-10 18:47:21 -04:00
|
|
|
|
;;; Modes
|
2018-02-13 18:19:35 -05:00
|
|
|
|
|
2017-07-05 02:33:41 +02:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(define-minor-mode +org-pretty-mode
|
2018-06-04 18:48:54 +02:00
|
|
|
|
"Hides emphasis markers and toggles pretty entities."
|
2017-07-05 02:33:41 +02:00
|
|
|
|
:init-value nil
|
|
|
|
|
:lighter " *"
|
|
|
|
|
:group 'evil-org
|
|
|
|
|
(setq org-hide-emphasis-markers +org-pretty-mode)
|
|
|
|
|
(org-toggle-pretty-entities)
|
2019-01-08 20:54:03 -05:00
|
|
|
|
(with-silent-modifications
|
2017-09-28 18:08:20 +02:00
|
|
|
|
;; In case the above un-align tables
|
|
|
|
|
(org-table-map-tables 'org-table-align t)))
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
|
|
|
|
|
2018-02-13 18:19:35 -05:00
|
|
|
|
;;
|
2019-04-10 18:47:21 -04:00
|
|
|
|
;;; Commands
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
2022-02-10 20:21:04 +01:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/return ()
|
|
|
|
|
"Call `org-return' then indent (if `electric-indent-mode' is on)."
|
|
|
|
|
(interactive)
|
|
|
|
|
(org-return electric-indent-mode))
|
|
|
|
|
|
2017-07-05 02:33:41 +02:00
|
|
|
|
;;;###autoload
|
2020-05-15 01:44:53 -04:00
|
|
|
|
(defun +org/dwim-at-point (&optional arg)
|
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.
|
2022-02-03 23:26:55 -08:00
|
|
|
|
- citation: follow it
|
2020-07-24 19:25:57 -04:00
|
|
|
|
- headline: cycle ARCHIVE subtrees, toggle latex fragments and inline images in
|
|
|
|
|
subtree; update statistics cookies/checkboxes and ToCs.
|
2022-02-03 23:26:55 -08:00
|
|
|
|
- clock: update its time.
|
2018-03-06 18:39:34 -05:00
|
|
|
|
- footnote reference: jump to the footnote's definition
|
|
|
|
|
- footnote definition: jump to the first reference of this footnote
|
2022-02-03 23:26:55 -08:00
|
|
|
|
- timestamp: open an agenda view for the time-stamp date/range at point.
|
2017-09-07 17:35:09 +02:00
|
|
|
|
- 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.
|
2022-02-03 23:26:55 -08:00
|
|
|
|
- src block: execute it
|
2017-09-07 17:35:09 +02:00
|
|
|
|
- latex fragment: toggle it.
|
|
|
|
|
- link: follow it
|
|
|
|
|
- otherwise, refresh all inline images in current tree."
|
2020-05-15 01:44:53 -04:00
|
|
|
|
(interactive "P")
|
2021-03-22 01:06:36 -04:00
|
|
|
|
(if (button-at (point))
|
|
|
|
|
(call-interactively #'push-button)
|
|
|
|
|
(let* ((context (org-element-context))
|
|
|
|
|
(type (org-element-type context)))
|
|
|
|
|
;; skip over unimportant contexts
|
|
|
|
|
(while (and context (memq type '(verbatim code bold italic underline strike-through subscript superscript)))
|
|
|
|
|
(setq context (org-element-property :parent context)
|
|
|
|
|
type (org-element-type context)))
|
|
|
|
|
(pcase type
|
2021-11-25 01:15:15 +01:00
|
|
|
|
((or `citation `citation-reference)
|
|
|
|
|
(org-cite-follow context arg))
|
|
|
|
|
|
2021-03-22 01:06:36 -04:00
|
|
|
|
(`headline
|
|
|
|
|
(cond ((memq (bound-and-true-p org-goto-map)
|
|
|
|
|
(current-active-maps))
|
|
|
|
|
(org-goto-ret))
|
|
|
|
|
((and (fboundp 'toc-org-insert-toc)
|
|
|
|
|
(member "TOC" (org-get-tags)))
|
|
|
|
|
(toc-org-insert-toc)
|
|
|
|
|
(message "Updating table of contents"))
|
|
|
|
|
((string= "ARCHIVE" (car-safe (org-get-tags)))
|
|
|
|
|
(org-force-cycle-archived))
|
|
|
|
|
((or (org-element-property :todo-type context)
|
|
|
|
|
(org-element-property :scheduled context))
|
|
|
|
|
(org-todo
|
|
|
|
|
(if (eq (org-element-property :todo-type context) 'done)
|
|
|
|
|
(or (car (+org-get-todo-keywords-for (org-element-property :todo-keyword context)))
|
|
|
|
|
'todo)
|
|
|
|
|
'done))))
|
|
|
|
|
;; Update any metadata or inline previews in this subtree
|
|
|
|
|
(org-update-checkbox-count)
|
|
|
|
|
(org-update-parent-todo-statistics)
|
|
|
|
|
(when (and (fboundp 'toc-org-insert-toc)
|
|
|
|
|
(member "TOC" (org-get-tags)))
|
|
|
|
|
(toc-org-insert-toc)
|
|
|
|
|
(message "Updating table of contents"))
|
|
|
|
|
(let* ((beg (if (org-before-first-heading-p)
|
|
|
|
|
(line-beginning-position)
|
|
|
|
|
(save-excursion (org-back-to-heading) (point))))
|
|
|
|
|
(end (if (org-before-first-heading-p)
|
|
|
|
|
(line-end-position)
|
|
|
|
|
(save-excursion (org-end-of-subtree) (point))))
|
|
|
|
|
(overlays (ignore-errors (overlays-in beg end)))
|
|
|
|
|
(latex-overlays
|
|
|
|
|
(cl-find-if (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
|
|
|
|
|
overlays))
|
|
|
|
|
(image-overlays
|
|
|
|
|
(cl-find-if (lambda (o) (overlay-get o 'org-image-overlay))
|
|
|
|
|
overlays)))
|
|
|
|
|
(+org--toggle-inline-images-in-subtree beg end)
|
|
|
|
|
(if (or image-overlays latex-overlays)
|
|
|
|
|
(org-clear-latex-preview beg end)
|
|
|
|
|
(org--latex-preview-region beg end))))
|
|
|
|
|
|
|
|
|
|
(`clock (org-clock-update-time-maybe))
|
|
|
|
|
|
|
|
|
|
(`footnote-reference
|
|
|
|
|
(org-footnote-goto-definition (org-element-property :label context)))
|
|
|
|
|
|
|
|
|
|
(`footnote-definition
|
|
|
|
|
(org-footnote-goto-previous-reference (org-element-property :label context)))
|
|
|
|
|
|
|
|
|
|
((or `planning `timestamp)
|
|
|
|
|
(org-follow-timestamp-link))
|
|
|
|
|
|
|
|
|
|
((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 arg)
|
|
|
|
|
(when (and (string-empty-p (string-trim (org-table-get-field)))
|
|
|
|
|
(bound-and-true-p evil-local-mode))
|
|
|
|
|
(evil-change-state 'insert)))
|
|
|
|
|
|
|
|
|
|
(`babel-call
|
|
|
|
|
(org-babel-lob-execute-maybe))
|
|
|
|
|
|
|
|
|
|
(`statistics-cookie
|
|
|
|
|
(save-excursion (org-update-statistics-cookies arg)))
|
|
|
|
|
|
|
|
|
|
((or `src-block `inline-src-block)
|
|
|
|
|
(org-babel-execute-src-block arg))
|
|
|
|
|
|
|
|
|
|
((or `latex-fragment `latex-environment)
|
|
|
|
|
(org-latex-preview arg))
|
|
|
|
|
|
|
|
|
|
(`link
|
|
|
|
|
(let* ((lineage (org-element-lineage context '(link) t))
|
|
|
|
|
(path (org-element-property :path lineage)))
|
|
|
|
|
(if (or (equal (org-element-property :type lineage) "img")
|
|
|
|
|
(and path (image-type-from-file-name path)))
|
|
|
|
|
(+org--toggle-inline-images-in-subtree
|
|
|
|
|
(org-element-property :begin lineage)
|
|
|
|
|
(org-element-property :end lineage))
|
|
|
|
|
(org-open-at-point arg))))
|
|
|
|
|
|
2024-02-17 07:11:48 -05:00
|
|
|
|
((guard (org-element-property :checkbox (org-element-lineage context '(item) t)))
|
|
|
|
|
(org-toggle-checkbox))
|
|
|
|
|
|
2022-02-03 23:26:27 -08:00
|
|
|
|
(`paragraph
|
|
|
|
|
(+org--toggle-inline-images-in-subtree))
|
|
|
|
|
|
2021-03-22 01:06:36 -04:00
|
|
|
|
(_
|
|
|
|
|
(if (or (org-in-regexp org-ts-regexp-both nil t)
|
|
|
|
|
(org-in-regexp org-tsr-regexp-both nil t)
|
|
|
|
|
(org-in-regexp org-link-any-re nil t))
|
|
|
|
|
(call-interactively #'org-open-at-point)
|
|
|
|
|
(+org--toggle-inline-images-in-subtree
|
|
|
|
|
(org-element-property :begin context)
|
|
|
|
|
(org-element-property :end context))))))))
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
2020-08-05 18:02:40 -04:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/shift-return (&optional arg)
|
|
|
|
|
"Insert a literal newline, or dwim in tables.
|
|
|
|
|
Executes `org-table-copy-down' if in table."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(if (org-at-table-p)
|
|
|
|
|
(org-table-copy-down arg)
|
|
|
|
|
(org-return nil arg)))
|
|
|
|
|
|
2017-02-19 19:01:47 -05:00
|
|
|
|
|
2020-07-26 15:42:50 -04:00
|
|
|
|
;; I use these instead of `org-insert-item' or `org-insert-heading' because they
|
|
|
|
|
;; impose bizarre whitespace rules depending on cursor location and many
|
|
|
|
|
;; settings. These commands have a much simpler responsibility.
|
2019-06-28 16:53:26 +02:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/insert-item-below (count)
|
2019-10-25 20:00:06 -04:00
|
|
|
|
"Inserts a new heading, table cell or item below the current one."
|
2019-06-28 16:53:26 +02:00
|
|
|
|
(interactive "p")
|
2019-10-25 20:00:06 -04:00
|
|
|
|
(dotimes (_ count) (+org--insert-item 'below)))
|
2019-06-28 16:53:26 +02:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/insert-item-above (count)
|
2019-10-25 20:00:06 -04:00
|
|
|
|
"Inserts a new heading, table cell or item above the current one."
|
2019-06-28 16:53:26 +02:00
|
|
|
|
(interactive "p")
|
2019-10-25 20:00:06 -04:00
|
|
|
|
(dotimes (_ count) (+org--insert-item 'above)))
|
|
|
|
|
|
2019-06-28 16:53:26 +02:00
|
|
|
|
|
2017-02-19 19:01:47 -05:00
|
|
|
|
;;;###autoload
|
2020-07-26 15:55:11 -04:00
|
|
|
|
(defun +org/toggle-last-clock (arg)
|
|
|
|
|
"Toggles last clocked item.
|
2019-12-06 13:38:57 -05:00
|
|
|
|
|
2020-07-26 15:55:11 -04:00
|
|
|
|
Clock out if an active clock is running (or cancel it if prefix ARG is non-nil).
|
2019-12-06 13:38:57 -05:00
|
|
|
|
|
2020-07-26 15:55:11 -04:00
|
|
|
|
If no clock is active, then clock into the last item. See `org-clock-in-last' to
|
|
|
|
|
see how ARG affects this command."
|
2019-12-06 13:38:57 -05:00
|
|
|
|
(interactive "P")
|
2020-11-06 02:29:37 -05:00
|
|
|
|
(require 'org-clock)
|
2020-07-26 15:55:11 -04:00
|
|
|
|
(cond ((org-clocking-p)
|
|
|
|
|
(if arg
|
|
|
|
|
(org-clock-cancel)
|
|
|
|
|
(org-clock-out)))
|
|
|
|
|
((and (null org-clock-history)
|
|
|
|
|
(or (org-on-heading-p)
|
|
|
|
|
(org-at-item-p))
|
|
|
|
|
(y-or-n-p "No active clock. Clock in on current item?"))
|
|
|
|
|
(org-clock-in))
|
|
|
|
|
((org-clock-in-last arg))))
|
2019-12-06 13:38:57 -05:00
|
|
|
|
|
2024-07-06 19:58:27 -04:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/reformat-at-point ()
|
|
|
|
|
"Reformat the element at point.
|
|
|
|
|
|
|
|
|
|
If in an org src block, invokes `+format/org-block' if the ':editor format'
|
|
|
|
|
module is enabled.
|
|
|
|
|
If in an org table, realign the cells with `org-table-align'.
|
|
|
|
|
Otherwise, falls back to `org-fill-paragraph' to reflow paragraphs."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((element (org-element-at-point)))
|
2024-07-14 01:48:11 -04:00
|
|
|
|
(cond ((doom-region-active-p)
|
|
|
|
|
;; TODO Perform additional formatting?
|
|
|
|
|
;; (save-restriction
|
|
|
|
|
;; (narrow-to-region beg end)
|
|
|
|
|
;; (org-table-recalculate t)
|
|
|
|
|
;; (org-table-map-tables #'org-table-align)
|
|
|
|
|
;; (org-align-tags t)
|
|
|
|
|
;; (org-update-statistics-cookies t)
|
|
|
|
|
;; ...)
|
|
|
|
|
(if (modulep! :editor format)
|
2024-07-14 02:00:24 -04:00
|
|
|
|
(call-interactively #'+format/org-blocks-in-region)
|
2024-07-14 01:48:11 -04:00
|
|
|
|
(message ":editor format is disabled, skipping reformatting of org-blocks")))
|
|
|
|
|
((org-in-src-block-p nil element)
|
2024-07-06 19:58:27 -04:00
|
|
|
|
(unless (modulep! :editor format)
|
|
|
|
|
(user-error ":editor format module is disabled, ignoring reformat..."))
|
|
|
|
|
(call-interactively #'+format/org-block))
|
|
|
|
|
((org-at-table-p)
|
|
|
|
|
(save-excursion (org-table-align)))
|
|
|
|
|
((call-interactively #'org-fill-paragraph)))))
|
|
|
|
|
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
2019-10-25 20:00:06 -04:00
|
|
|
|
;;; Folds
|
2018-06-04 17:46:39 +02:00
|
|
|
|
;;;###autoload
|
2019-07-21 02:38:42 +02:00
|
|
|
|
(defalias #'+org/toggle-fold #'+org-cycle-only-current-subtree-h)
|
2018-06-04 17:46:39 +02:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/open-fold ()
|
|
|
|
|
"Open the current fold (not but its children)."
|
|
|
|
|
(interactive)
|
|
|
|
|
(+org/toggle-fold t))
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defalias #'+org/close-fold #'outline-hide-subtree)
|
|
|
|
|
|
2020-05-13 19:29:10 -04:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/close-all-folds (&optional level)
|
|
|
|
|
"Close all folds in the buffer (or below LEVEL)."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(outline-hide-sublevels (or level 1)))
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun +org/open-all-folds (&optional level)
|
|
|
|
|
"Open all folds in the buffer (or up to LEVEL)."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(if (integerp level)
|
|
|
|
|
(outline-hide-sublevels level)
|
|
|
|
|
(outline-show-all)))
|
|
|
|
|
|
2018-06-04 17:46:39 +02:00
|
|
|
|
(defun +org--get-foldlevel ()
|
|
|
|
|
(let ((max 1))
|
|
|
|
|
(save-restriction
|
|
|
|
|
(narrow-to-region (window-start) (window-end))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(while (not (eobp))
|
|
|
|
|
(org-next-visible-heading 1)
|
2023-09-06 07:27:35 -05:00
|
|
|
|
(when (memq (get-char-property (line-end-position)
|
|
|
|
|
'invisible)
|
|
|
|
|
'(outline org-fold-outline))
|
2018-06-04 17:46:39 +02:00
|
|
|
|
(let ((level (org-outline-level)))
|
|
|
|
|
(when (> level max)
|
|
|
|
|
(setq max level))))))
|
|
|
|
|
max)))
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
2020-05-13 19:29:10 -04:00
|
|
|
|
(defun +org/show-next-fold-level (&optional count)
|
2018-06-04 17:46:39 +02:00
|
|
|
|
"Decrease the fold-level of the visible area of the buffer. This unfolds
|
|
|
|
|
another level of headings on each invocation."
|
2020-05-13 19:29:10 -04:00
|
|
|
|
(interactive "p")
|
|
|
|
|
(let ((new-level (+ (+org--get-foldlevel) (or count 1))))
|
2018-06-04 17:46:39 +02:00
|
|
|
|
(outline-hide-sublevels new-level)
|
|
|
|
|
(message "Folded to level %s" new-level)))
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
2020-05-13 19:29:10 -04:00
|
|
|
|
(defun +org/hide-next-fold-level (&optional count)
|
2018-06-04 17:46:39 +02:00
|
|
|
|
"Increase the global fold-level of the visible area of the buffer. This folds
|
|
|
|
|
another level of headings on each invocation."
|
2020-05-13 19:29:10 -04:00
|
|
|
|
(interactive "p")
|
|
|
|
|
(let ((new-level (max 1 (- (+org--get-foldlevel) (or count 1)))))
|
2018-06-04 17:46:39 +02:00
|
|
|
|
(outline-hide-sublevels new-level)
|
|
|
|
|
(message "Folded to level %s" new-level)))
|
|
|
|
|
|
2018-02-13 18:19:35 -05:00
|
|
|
|
|
|
|
|
|
;;
|
2019-04-10 18:47:21 -04:00
|
|
|
|
;;; Hooks
|
2018-02-13 18:19:35 -05:00
|
|
|
|
|
2017-10-05 01:23:56 +02:00
|
|
|
|
;;;###autoload
|
2019-07-21 02:38:42 +02:00
|
|
|
|
(defun +org-indent-maybe-h ()
|
2019-04-04 18:47:40 -04:00
|
|
|
|
"Indent the current item (header or item), if possible.
|
|
|
|
|
Made for `org-tab-first-hook' in evil-mode."
|
2017-10-05 01:23:56 +02:00
|
|
|
|
(interactive)
|
2019-06-25 21:38:16 +02:00
|
|
|
|
(cond ((not (and (bound-and-true-p evil-local-mode)
|
2021-05-31 14:22:54 -04:00
|
|
|
|
(evil-insert-state-p)))
|
2018-02-13 18:33:36 -05:00
|
|
|
|
nil)
|
2022-01-03 16:41:37 +01:00
|
|
|
|
((and (bound-and-true-p org-cdlatex-mode)
|
|
|
|
|
(or (org-inside-LaTeX-fragment-p)
|
|
|
|
|
(org-inside-latex-macro-p)))
|
|
|
|
|
nil)
|
2018-02-13 18:33:36 -05:00
|
|
|
|
((org-at-item-p)
|
2018-07-30 04:04:13 +02:00
|
|
|
|
(if (eq this-command 'org-shifttab)
|
|
|
|
|
(org-outdent-item-tree)
|
|
|
|
|
(org-indent-item-tree))
|
2018-02-13 18:19:35 -05:00
|
|
|
|
t)
|
|
|
|
|
((org-at-heading-p)
|
2018-07-30 04:04:13 +02:00
|
|
|
|
(ignore-errors
|
|
|
|
|
(if (eq this-command 'org-shifttab)
|
|
|
|
|
(org-promote)
|
|
|
|
|
(org-demote)))
|
2018-02-13 18:19:35 -05:00
|
|
|
|
t)
|
|
|
|
|
((org-in-src-block-p t)
|
2022-02-10 21:28:55 +01:00
|
|
|
|
(save-window-excursion
|
|
|
|
|
(org-babel-do-in-edit-buffer
|
|
|
|
|
(call-interactively #'indent-for-tab-command)))
|
2020-01-01 21:00:59 -05:00
|
|
|
|
t)
|
|
|
|
|
((and (save-excursion
|
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
|
(bolp))
|
|
|
|
|
(org-in-subtree-not-table-p))
|
|
|
|
|
(call-interactively #'tab-to-tab-stop)
|
2018-02-13 18:19:35 -05:00
|
|
|
|
t)))
|
2017-10-05 01:23:56 +02:00
|
|
|
|
|
2018-02-13 18:19:35 -05:00
|
|
|
|
;;;###autoload
|
2019-07-21 02:38:42 +02:00
|
|
|
|
(defun +org-yas-expand-maybe-h ()
|
2020-07-26 16:03:32 -04:00
|
|
|
|
"Expand a yasnippet snippet, if trigger exists at point or region is active.
|
|
|
|
|
Made for `org-tab-first-hook'."
|
2022-08-12 20:29:19 +02:00
|
|
|
|
(when (and (modulep! :editor snippets)
|
2021-07-24 19:43:51 -04:00
|
|
|
|
(require 'yasnippet nil t)
|
|
|
|
|
(bound-and-true-p yas-minor-mode))
|
|
|
|
|
(and (let ((major-mode (cond ((org-in-src-block-p t)
|
|
|
|
|
(org-src-get-lang-mode (org-eldoc-get-src-lang)))
|
|
|
|
|
((org-inside-LaTeX-fragment-p)
|
|
|
|
|
'latex-mode)
|
|
|
|
|
(major-mode)))
|
2020-04-17 00:50:11 -04:00
|
|
|
|
(org-src-tab-acts-natively nil) ; causes breakages
|
|
|
|
|
;; Smart indentation doesn't work with yasnippet, and painfully slow
|
|
|
|
|
;; in the few cases where it does.
|
|
|
|
|
(yas-indent-line 'fixed))
|
|
|
|
|
(cond ((and (or (not (bound-and-true-p evil-local-mode))
|
2020-12-11 17:36:12 -05:00
|
|
|
|
(evil-insert-state-p)
|
|
|
|
|
(evil-emacs-state-p))
|
2021-07-24 19:43:51 -04:00
|
|
|
|
(or (and (bound-and-true-p yas--tables)
|
|
|
|
|
(gethash major-mode yas--tables))
|
|
|
|
|
(progn (yas-reload-all) t))
|
2020-04-17 00:50:11 -04:00
|
|
|
|
(yas--templates-for-key-at-point))
|
|
|
|
|
(yas-expand)
|
|
|
|
|
t)
|
|
|
|
|
((use-region-p)
|
|
|
|
|
(yas-insert-snippet)
|
|
|
|
|
t)))
|
|
|
|
|
;; HACK Yasnippet breaks org-superstar-mode because yasnippets is
|
|
|
|
|
;; overzealous about cleaning up overlays.
|
|
|
|
|
(when (bound-and-true-p org-superstar-mode)
|
|
|
|
|
(org-superstar-restart)))))
|
2018-04-04 06:56:56 -04:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-21 02:38:42 +02:00
|
|
|
|
(defun +org-cycle-only-current-subtree-h (&optional arg)
|
2020-07-26 16:03:32 -04:00
|
|
|
|
"Toggle the local fold at the point, and no deeper.
|
|
|
|
|
`org-cycle's standard behavior is to cycle between three levels: collapsed,
|
|
|
|
|
subtree and whole document. This is slow, especially in larger org buffer. Most
|
|
|
|
|
of the time I just want to peek into the current subtree -- at most, expand
|
|
|
|
|
*only* the current subtree.
|
|
|
|
|
|
|
|
|
|
All my (performant) foldings needs are met between this and `org-show-subtree'
|
|
|
|
|
(on zO for evil users), and `org-cycle' on shift-TAB if I need it."
|
2018-06-04 17:46:39 +02:00
|
|
|
|
(interactive "P")
|
2022-01-03 16:41:37 +01:00
|
|
|
|
(unless (or (eq this-command 'org-shifttab)
|
|
|
|
|
(and (bound-and-true-p org-cdlatex-mode)
|
|
|
|
|
(or (org-inside-LaTeX-fragment-p)
|
|
|
|
|
(org-inside-latex-macro-p))))
|
2018-04-05 17:28:30 -04:00
|
|
|
|
(save-excursion
|
|
|
|
|
(org-beginning-of-line)
|
2018-08-13 15:21:03 +02:00
|
|
|
|
(let (invisible-p)
|
|
|
|
|
(when (and (org-at-heading-p)
|
|
|
|
|
(or org-cycle-open-archived-trees
|
|
|
|
|
(not (member org-archive-tag (org-get-tags))))
|
|
|
|
|
(or (not arg)
|
2023-09-06 07:27:35 -05:00
|
|
|
|
(setq invisible-p
|
|
|
|
|
(memq (get-char-property (line-end-position)
|
|
|
|
|
'invisible)
|
|
|
|
|
'(outline org-fold-outline)))))
|
2018-08-13 15:21:03 +02:00
|
|
|
|
(unless invisible-p
|
|
|
|
|
(setq org-cycle-subtree-status 'subtree))
|
|
|
|
|
(org-cycle-internal-local)
|
|
|
|
|
t)))))
|
2018-05-08 15:36:42 +02:00
|
|
|
|
|
2019-12-30 00:07:19 -05:00
|
|
|
|
;;;###autoload
|
2020-05-11 02:50:45 -04:00
|
|
|
|
(defun +org-make-last-point-visible-h ()
|
2020-07-26 16:03:32 -04:00
|
|
|
|
"Unfold subtree around point if saveplace places us in a folded region."
|
2020-08-05 15:00:34 -04:00
|
|
|
|
(and (not org-inhibit-startup)
|
|
|
|
|
(not org-inhibit-startup-visibility-stuff)
|
2021-04-20 20:30:43 -04:00
|
|
|
|
;; Must be done on a timer because `org-show-set-visibility' (used by
|
|
|
|
|
;; `org-reveal') relies on overlays that aren't immediately available
|
|
|
|
|
;; when `org-mode' first initializes.
|
2023-11-24 02:21:08 +00:00
|
|
|
|
(let ((buf (current-buffer)))
|
|
|
|
|
(unless (doom-temp-buffer-p buf)
|
|
|
|
|
(run-at-time 0.1 nil (lambda ()
|
2023-11-29 11:28:28 +01:00
|
|
|
|
(when (buffer-live-p buf)
|
|
|
|
|
(with-current-buffer buf
|
|
|
|
|
(org-reveal '(4))))))))))
|
2019-04-10 18:47:21 -04:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
2019-10-25 20:00:06 -04:00
|
|
|
|
(defun +org-remove-occur-highlights-h ()
|
|
|
|
|
"Remove org occur highlights on ESC in normal mode."
|
|
|
|
|
(when org-occur-highlights
|
|
|
|
|
(org-remove-occur-highlights)
|
|
|
|
|
t))
|