【提问】能否使 org-id-get-create 在创建 ID 的同时创建 CUSTOM_ID ?

能否使 org-id-get-create 在创建 ID 的同时创建 CUSTOM_ID ?或者直接用 CUSTOM_ID 代替ID?

1 个赞

覆盖原有函数不很明智,自己写一个生成 custom id 的函数绑定快捷键不就行了?

这是我最常用的 ISO 8601 时间戳供参考

(defun my/date-and-time-iso8601 ()
  (interactive)
  (insert (format-time-string "%FT%T%z"))
  )

(keymap-global-set "C-c D" #'my/date-and-time-iso8601)

效果

2023-01-19T01:07:24+0800

ID 和 CUSTOM_ID 用法上有区别哦,你最好明确一下自己的需求才好给建议。暴力的改变 org-id-get-create 行为可能破坏某些功能。

这可能是个 X-Y Problem,最好描述下你这么做的目的是啥

目的:我想使用 CUSTOM_ID 从 A 文件链接到 B 文件

遇到的问题:使用 org-id-get-create 创建的 ID 无法跨文件链接,使用 org-set-property 创建 CUSTOM_ID 又无法自动生成 ID 那一串东西,于是就冒出来这个想法

不知有没有方法可以简单的直接创建 CUSTOM_ID 并自动生成 ID 那一串东西,不需要手动填写 CUSTOM_ID ?

文件之间的链接是需要一套机制的,你完全可以用 org-roam/denote 啊 :thinking:

你确定 ID 不能跨文件链接?

我映像中可以通过 [[ID:xxxxxxxxx][description]] 的方式使用,可以进行跳转。

file 或者是 headline,你试试。

我猜你是想用 Include Files,好像只有这里是使用 filelink ,导致 ID 无法工作。

普通的链接用 ID 是可以的。

可以举个具体的例子吗?

没其他使用需求,就是普通的链接。

ID 要怎么输入才能跨文件链接的?

(setq org-id-link-to-org-use-id 'create-if-interactive)

然后在想要创建 link 的地方 org-store-link,插入链接用 org-insert-link

尝试 (org-id-update-id-locations (directory-files-recursively org-file-directory ".org$\\|.org.gpg$")) from Org-Roam issue #1702

或者

  (defun force-org-rebuild-cache ()
    "Rebuild the `org-mode' and `org-roam' cache."
    (interactive)
    (org-id-update-id-locations)
    (org-roam-db-sync)
    (org-roam-update-org-id-locations))

可以试试这个函数:

(defun my/copy-idlink ()
  "Copy idlink to clipboard."
  (interactive)
  (when (eq major-mode 'org-agenda-mode) ;switch to orgmode
    (org-agenda-show)
    (org-agenda-goto))
  (when (eq major-mode 'org-mode) ; do this only in org-mode buffers
    (let* ((mytmphead (nth 4 (org-heading-components)))
           (mytmpid (funcall 'org-id-get-create))
           (mytmplink (format "- [ ] [[id:%s][%s]]" mytmpid mytmphead)))
      (kill-new mytmplink)
      (message "Copied %s to killring (clipboard)" mytmplink))))
(global-set-key (kbd "<f7>") 'my/copy-idlink)

format那里改成你需要的格式。

Debugger entered--Lisp error: (error "Cannot find entry with ID \"bf04f0c2-5f66-439c-b576...")
  signal(error ("Cannot find entry with ID \"bf04f0c2-5f66-439c-b576..."))
  error("Cannot find entry with ID \"%s\"" "bf04f0c2-5f66-439c-b576-b6fa551dd5ef")
  org-id-open("bf04f0c2-5f66-439c-b576-b6fa551dd5ef")
  org-link-open((link (:type "id" :path "bf04f0c2-5f66-439c-b576-b6fa551dd5ef" :format bracket :raw-link "id:bf04f0c2-5f66-439c-b576-b6fa551dd5ef" :application nil :search-option nil :begin 17 :end 72 :contents-begin 60 :contents-end 70 :post-blank 0 :parent (paragraph (:begin 17 :end 73 :contents-begin 17 :contents-end 73 :post-blank 0 :post-affiliated 17 :parent (item (:bullet "- " :begin 11 :end 73 :contents-begin 17 :contents-end 73 :checkbox off :counter nil :structure (...) :pre-blank 0 :post-blank 0 :post-affiliated 11 :tag nil :parent (plain-list ...))))))) nil)
  org-open-at-point()
  org-open-at-mouse((mouse-2 (#<window 3 on org.org> 65 (200 . 42) 79423493 nil 65 (17 . 1) nil (3 . 22) (19 . 29))))
  funcall-interactively(org-open-at-mouse (mouse-2 (#<window 3 on org.org> 65 (200 . 42) 79423493 nil 65 (17 . 1) nil (3 . 22) (19 . 29))))
  call-interactively(org-open-at-mouse nil nil)
  command-execute(org-open-at-mouse)

用 ``` 格式化下你的代码。

已经重新编辑

你用 emacs -Q 编辑两个最简单的org 文件试试,看看是否能重现。

你的系统,Emacs版本及org 的版本。

Debugger entered--Lisp error: (void-variable org-file-directory)
  (directory-files-recursively org-file-directory ".org$\\|.org.gpg$")
  (org-id-update-id-locations (directory-files-recursively org-file-directory ".org$\\|.org.gpg$"))
  eval-buffer(#<buffer  *load*-629039> nil "/home/user/.emacs.d/lisp/init-org-id.el" nil t)  ; Reading at buffer position 215
  load-with-code-conversion("/home/user/.emacs.d/lisp/init-org-id.el" "/home/user/.emacs.d/lisp/init-org-id.el" nil t)
  require(init-org-id)
  eval-buffer(#<buffer  *load*> nil "/home/user/.emacs.d/init.el" nil t)  ; Reading at buffer position 969
  load-with-code-conversion("/home/user/.emacs.d/init.el" "/home/user/.emacs.d/init.el" t t)
  load("/home/user/.emacs.d/init" noerror nomessage)
  startup--load-user-init-file(#f(compiled-function () #<bytecode 0x15859f10d675>) #f(compiled-function () #<bytecode 0x15859f10d689>) t)
  command-line()
  normal-top-level()

把 org-file-directory 改成你的 org-roam-directory 或者 org-directory

emacs版本:27.1 org版本:9.3

org-id-open: Cannot find entry with ID "4091e71a-2ae2-4e38-8638-8416956112c1"

多谢大佬,是我的org-file-directory设置有问题