记录一下第一次编写的插件 org文件提取一个tree到新文件

功能是在一个 org文件中,将一个 tree 抽出来到新的文件中去

其实是在我查找到的一个代码 subtree-to-new-file 的基础上改的,但哪看到的忘了

功能就是增加了一个链接

(defun subtree-to-new-file ()
  (interactive)
  (setq origin-file-name (buffer-file-name))
  (setq saved-point (point))
  "sloppily assists in moving an org subtree to a new file"
  (org-copy-subtree nil t)
  

;;; This long setq statement gets the title of the first heading, to use as a default filename for the new .org file.
(setq first-heading
  (with-temp-buffer
    (yank)
    (beginning-of-buffer)
    (search-forward " " nil nil 1)
    (setq title-start (point))
    (end-of-visual-line)
    (setq title-end (point))
    (setq first-heading (buffer-substring title-start title-end))
  ))
(setq def-filename (concat first-heading ".org"))
(message def-filename)
(let ((insert-default-directory nil))
  (find-file-other-window  
    (read-file-name "Move subtree to file:" nil def-filename)
  ))
(org-paste-subtree)
(save-buffer)
;;; this final command adds the new .org file to the agenda
(org-agenda-file-to-front)
(let ((new-file-link (call-interactively 'org-store-link)))
  (progn
    (find-file-other-window origin-file-name)
    (goto-char saved-point)
    (previous-line)
    (end-of-line)
    (newline-and-indent)
    (insert "* ")
    (org-insert-link)))

)

但是在执行的时候 最后(org-insert-link)还要在prompt中选择,不知道怎么弄成默认第一个。

另外代码有没有可以改进的地方,大佬多提提意见

2023-12-31 18-10-37.2023-12-31 18_13_06