如何在emacs org-mode中将函数限制为子树?

时间:2012-12-20 08:20:25

标签: emacs elisp org-mode attachment subtree

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

在worg上我找到了Matt Lundi的一个功能,可以看到所有功能 属于整个文件的附件,并使用ido浏览它们。

我想将此函数限制为可以实现的子树 对我的用例更有用。

因为我不是emacs的新手,但我几乎完全是elisp文盲 问这里。

这是功能:

(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)))

2 个答案:

答案 0 :(得分:3)

也许我错过了一些东西,但是先调用org-narrow-to-subtree应该做你想做的事情(之后再调用widen来恢复它)。

答案 1 :(得分:0)

我认为这对我自己非常有用,所以,在你的问题的启发下,我写了一个版本,可以做你想要的,加上其他一些花里胡哨的东西。要调用它,您必须键入 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)'(16)是神奇的数字,意思是前缀arg一次 Cu 和前缀arg两次 Cu Cu 之前以交互方式调用命令的键序列。