9

我广泛使用 org-mode 和org-attach,这意味着可以有许多附件目录与一个 org 文件相关联。

在 worg 上,我发现了 Matt Lundi 的一个功能,它允许查看属于整个文件的所有附件并使用 ido 浏览它们。

我想将此函数限制为一个子树,这将使它对我的用例更有用。

由于我对emacs并不陌生,但几乎完全是文盲,所以我在这里问。

这是功能:

(defun my-ido-find-org-attach ()
  "Find files in org-attachment directory"
  (interactive)
  (let* ((enable-recursive-minibuffers t)
         (files (find-lisp-find-files org-attach-directory "."))
         (file-assoc-list
          (mapcar (lambda (x)
                    (cons (file-name-nondirectory x)
                          x))
                  files))
         (filename-list
          (remove-duplicates (mapcar #'car file-assoc-list)
                             :test #'string=))
         (filename (ido-completing-read "Org attachments: " filename-list nil t))
         (longname (cdr (assoc filename file-assoc-list))))
    (ido-set-current-directory
     (if (file-directory-p longname)
         longname
       (file-name-directory longname)))
    (setq ido-exit 'refresh
          ido-text-init ido-text
          ido-rotate-temp t)
    (exit-minibuffer)))
4

2 回答 2

3

Maybe I'm missing something, but calling org-narrow-to-subtree first should do what you want (call widen afterwards to revert that).

于 2012-12-22T10:04:30.917 回答
1

我认为这对我自己非常有用,因此,受您的问题的启发,我编写了一个可以满足您需求的版本以及其他一些花里胡哨。要调用它,您必须键入C-c o. 注意:这不在通常的org-attach键前缀上,因为奇怪地编写了没有键映射的函数,因此很难将功能添加到键前缀上。

(autoload 'org-attach-dir "org-attach")
(autoload 'find-lisp-find-files "find-lisp")
(defcustom ido-locate-org-attach-all-files nil
  "Non-nil means `ido-locate-org-attach' returns all files.
Otherwise the default behavior only returns files attached to the
current entry."
  :group 'ido
  :type 'boolean)

(defun ido-locate-org-attach (&optional find-all)
  "Find files in org-attachment directory for current entry.
When called with a prefix argument, include all files in
`org-attach-directory'. With a double `C-u' prefix arg the value
of `ido-locate-org-attach-all-files' will be toggled for the
session. If you want to save it permanently for future session
then customize the variable `ido-locate-org-attach-all-files'."
  (interactive "P")
  (when (org-attach-dir nil)
    (when (equal find-all '(16))
      (setq ido-locate-org-attach-all-files
        (not ido-locate-org-attach-all-files)))
    (let* ((enable-recursive-minibuffers t)
       (dir (if (org-xor ido-locate-org-attach-all-files
                 (equal find-all '(4)))
            org-attach-directory
          (org-attach-dir nil)))
       (files (find-lisp-find-files dir "."))
       (file-assoc-list
        (mapcar (lambda (x)
              (cons (file-name-nondirectory x)
                x))
            files))
       (filename-list
        (remove-duplicates (mapcar #'car file-assoc-list)
                   :test #'string=))
       (filename (ido-completing-read "Org attachments: " filename-list nil t))
       (longname (cdr (assoc filename file-assoc-list))))
      (ido-set-current-directory
       (if (file-directory-p longname)
       longname
     (file-name-directory longname)))
      (setq ido-exit 'refresh
        ido-text-init ido-text
        ido-rotate-temp t)
      (exit-minibuffer))))

;; Run ido-locate-org-attach when using org-open-at-point (C-c C-o) in
;; the current entry (except if you're on the header line itself it
;; will use the default behavior to open/close the entry.
(add-hook 'org-open-at-point-functions 'ido-locate-org-attach)

;; C-c o           will locate files for the current entry
;; C-u C-c o       will locate files for the whole file
;; C-u C-u C-c o   will toggle the default current entry / whole file
(define-key org-mode-map "\C-co" 'ido-locate-org-attach)

我会考虑将其提交为org-attach.el.

顺便说一句,'(4)and'(16)是幻数,表示在交互式调用命令的键序列之前添加一次C-u前缀 arg 和两次前缀 arg 。C-u C-u

于 2012-12-23T06:30:48.667 回答