Skip to content

Fixed org-roam-promote-entire-buffer structure errors#2091

Merged
jethrokuan merged 9 commits intoorg-roam:masterfrom
jmay:fix-promote-buffer
Apr 17, 2022
Merged

Fixed org-roam-promote-entire-buffer structure errors#2091
jethrokuan merged 9 commits intoorg-roam:masterfrom
jmay:fix-promote-buffer

Conversation

@jmay
Copy link
Contributor

@jmay jmay commented Feb 14, 2022

Motivation for this change

Fixes #1857 and #2058

Changes

Run safety checks before executing org-roam-promote-entire-buffer. These are unnecessary when it is called from org-roam-extract-subtree, but provide some extract protection if invoked directly.

Replaced an invocation of (org-roam-set-keyword "title" title) with explicit code, to avoid the situation where org-roam-set-keyword steps over a line and injects the #+title: line into the middle of body text instead of at the top.

Added a call to save-buffer to prevent org-map-entries from triggering a confusing & unwanted org-agenda prompt "Non-existent agenda file. [R]emove from list or [A]bort?".

- Safety checks before executing the transformation.
- Insert the new TITLE line directly below the file-level properties drawer.
@jmay
Copy link
Contributor Author

jmay commented Feb 14, 2022

This might also fix #1870

jmay added 4 commits March 28, 2022 14:03
- buffer must be saved for org operations to be reliable
- don't use org-roam-set-keyword here, it shifts point
- ensure that database is updated with changes afterwards
Copy link
Member

@jethrokuan jethrokuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR definitely fixes a bunch of issues existing in org-roam-extract-subtree and will make it strictly better, but fundamentally I think the approach of cut-paste-promote is brittle and needs to change. Open to merging after the comments have been addressed, but I'll need some time to rethink the right way to go about extraction.

org-roam-node.el Outdated

(defun org-roam--heading-levels ()
"Extract the heading level structure from the current file."
(org-map-entries (lambda () (org-current-level))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want to be using org-map-entries here since it introduces dependencies on org-agenda related configuration. Use org-map-region istead as per line 848 to 854.

org-roam-node.el Outdated
"Verify that this buffer is promoteable:
There is a single level-1 heading
and no extra content before the first heading."
(let ((level-1-headings-count (-count (lambda (level) (eq 1 level)) (org-roam--heading-levels)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not use anything from dash here, we have no explicit dependency on dash at the moment, although we do, I think, pull it in transitively.

org-roam-node.el Outdated
(and
(eq level-1-headings-count 1)
is-top-content-empty)))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no my/ prefix here.

org-roam-node.el Outdated
(kill-line 1)
(org-roam-set-keyword "title" title)
(when tags (org-roam-set-keyword "filetags" tags)))))
(save-buffer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as possible I want to avoid calling save-buffer here, is it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed it, but I'm worried that I ran into issues earlier where an unsaved buffer had node ID values in some state that broke things when the buffer was promoted. I can't reproduce the situation now, but I'll keep watching in case I can cause errors again. The org-roam-db-update-file is definitely needed to ensure that node state is persisted properly to the database.

org-roam-node.el Outdated
(insert "#+title: " title "\n")
(when tags (insert "#+filetags: " tags "\n"))
(org-roam-db-update-file)))
(user-error "Cannot promote: Multiple root headings or there is extra file-level text")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's error at the beginning in a when block instead.

org-roam-node.el Outdated
;;(org-roam-set-keyword "title" title)
;;(when tags (org-roam-set-keyword "filetags" tags))))
(insert "#+title: " title "\n")
(when tags (insert "#+filetags: " tags "\n"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use org-roam-tags-add here.

@jmay
Copy link
Contributor Author

jmay commented Apr 4, 2022

I agree that this warrants a proper rethink. Maybe this should really be treated as a special case of refile.

- use org-map-region instead of org-map-entries
- remove save-buffer
- use org-roam-tag-add and org-get-tags to transfer tags from heading to file
- avoid use of other packages (-count from dash)
@stig
Copy link
Contributor

stig commented Apr 13, 2022

I wonder if this fixes #2154 as well?

@jethrokuan jethrokuan merged commit d8985aa into org-roam:master Apr 17, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

=org-roam-extract-subtree= prompts for a confirmation that is not related to the function itself

3 participants