Compare commits

...

4 Commits

Author SHA1 Message Date
292c8fc89a
Fix some flycheck warnings 2025-01-17 19:29:40 +01:00
6b6db486a7
Merge branch 'org-item-to-headline' 2025-01-17 18:12:09 +01:00
6e31e4a707
Clean up draft of Org list item conversion function
The template is still fixed, should it be customizable?
2025-01-17 18:10:20 +01:00
2960652d66
Draft function to convert Org items to headlines
This usually happens when I try to convert loose entrie in my refile
file into proper headlines.

Usage of Org capture functionality is inspired by Sacha Chua [1].

[1]: https://sachachua.com/blog/2020/12/org-mode-create-a-quick-timestamped-note-and-capture-a-screenshot-by-prefilling-a-capture-template-via-emacs-lisp/
2025-01-17 17:54:06 +01:00

View File

@ -13,6 +13,7 @@
(require 'cl-lib)
(require 'org)
(require 'org-agenda)
(require 'org-capture)
(require 'org-clock)
(require 'hydra)
(require 'db-customize)
@ -22,6 +23,7 @@
(autoload 'which-function "which-func")
(autoload 'org-element-property "org-element")
(autoload 'db/org-agenda "db-utils")
(declare-function w32-shell-execute "w32fns.c")
@ -112,7 +114,7 @@ deadlines."
(setq buffer-read-only t)
(message ""))))
(defun db/org-agenda-insert-active-filters (&optional match)
(defun db/org-agenda-insert-active-filters (&optional _match)
"Insert string showing the current agenda filters.
The filter display is added after the structural header.
@ -388,6 +390,71 @@ In ~%s~:
(advice-add 'org-capture-finalize
:after #'db/delete-frame-if-capture)
(defun db/org-convert-item-to-headline ()
"Convert list item around point to headline.
Search for the outermost list item enclosing point and convert it
to a Org headline. Use the first line of the item as actual
headline, and move the rest to the body of the headline. A
property CREATED is added with an inactive timestamp capturing
the current time.
The converted headline will be shown in a capture buffer for
further editing. After finishing this buffer, the final headline
will be stored in `db/org-default-refilefile'.
Note that the original list item will be deleted before the
capture buffer will open. If the buffer is aborted, the original
list item is _not_ restored. This has to be done using the undo
history of the buffer."
(interactive)
(let ((element (org-element-at-point))
last-seen-item)
;; Get outermost list item
(while element
(when (eq (org-element-type element) 'item)
(setq last-seen-item element))
(setq element (org-element-parent element)))
(unless last-seen-item
(user-error "Cannot find enclosing list item at point to convert to headline"))
;; Generate headline and store it somewhere
(let* ((body (buffer-substring-no-properties (org-element-contents-begin last-seen-item)
(org-element-end last-seen-item)))
(first-line-of-body (seq-take-while #'(lambda (x) (not (= x ?\n))) body))
(rest-of-body (string-trim (seq-drop-while #'(lambda (x) (not (= x ?\n))) body))))
;; Remove old entry first
(delete-region (org-element-begin last-seen-item)
(org-element-end last-seen-item))
;; Set up capture buffer
(org-capture-put :key "")
(org-capture-put :description "")
(org-capture-put :target '(file db/org-default-refile-file))
(org-capture-put :empty-lines-before 1)
(org-capture-put :empty-lines-after 1)
(org-capture-put :template (org-capture-fill-template
(format "* TODO [#B] %s
:PROPERTIES:
:CREATED: %%U
:END:
Via %%(with-temp-buffer (db/org-add-link-to-current-clock) (string-trim (buffer-string))).
%s
%%?"
first-line-of-body
rest-of-body)))
(org-capture-set-target-location)
(org-capture-place-template)
(indent-region (point-min) (point-max)))))
;;; Refiling
@ -516,7 +583,7 @@ clocked in."
(if at-current-clock-p
;; From `org-clock-get-clocked-time'
(floor (org-time-convert-to-integer
(org-time-since org-clock-start-time))
(time-since org-clock-start-time))
60)
0))))
@ -1183,7 +1250,8 @@ clipboard as with `org-password-manager-get-password', which see.
Otherwise, the password is returned as value from this function
and can be used for further processing."
(require 'org-password-manager)
(eval-when-compile
(require 'org-password-manager))
(let ((pom (org-id-find id 'marker)))
(unless (markerp pom)
(user-error "Cannot find item with id %s" id))
@ -1848,25 +1916,24 @@ linking to any item."
;; nil without any possibility for a custom string.
(unwind-protect
(progn
(fset 'completing-read #'(lambda (_prompt
_table
(fset 'completing-read #'(lambda (prompt
table
&optional
_predicate
_require-match
predicate
require-match
_initial-input
_hist
_def
_inherit-input-method)
(ignore _initial-input)
hist
def
inherit-input-method)
(funcall old-completing-read
_prompt
_table
_predicate
_require-match
prompt
table
predicate
require-match
initial-input
_hist
_def
_inherit-input-method)))
hist
def
inherit-input-method)))
(org-refile-get-location nil default-buffer))
(fset 'completing-read old-completing-read))))
(pom (nth 3 target-pointer)))