在组织模式下以字符串形式获取文件路径

get file path as string in org-mode

我希望能够在 emacs org-mode 中将 link 的文件路径作为字符串获取,然后我可以用各种方式解析它,然后 return 到 org-open-file。因此,例如 link [[file:/path/to/file.org]][link text] 将 return 字符串 /path/to/file.org。我打赌这是基本的 elisp,但我是 elisp 的新手。

您可以从 Org element API 访问此信息。这里 是获取路径并在Dired缓冲区中打开它的示例。

(defun km/org-link-dired-jump ()
  "Open Dired for directory of file link at point."
  (interactive)
  (let ((el (org-element-lineage (org-element-context) '(link) t)))
    (unless (and el (equal (org-element-property :type el) "file"))
      (user-error "Not on file link"))
    (dired-jump 'other-window
                (expand-file-name (org-element-property :path el)))))

(这取决于 Org 版本 8.3 或更高版本。)