emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Re: function for cleaning org-attach directories
Date: Sat, 01 Aug 2015 17:23:40 +0800	[thread overview]
Message-ID: <874mkjfjkz.fsf_-_@ericabrahamsen.net> (raw)
In-Reply-To: m2fv4ihnvh.fsf@polytechnique.org

[-- Attachment #1: Type: text/plain, Size: 1026 bytes --]

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> On 2015-07-18 05:11, Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>>
>>> I gave this a try and it seems that `org-attach-directory' needs to be
>>> defined for it to work. I'm surprised because I never configured this
>>> and with gnorb I have had files attached using org-attach. Does gnorb
>>> use a default value for this?
>>
>> Gnorb has calls to (require 'org-attach) in certain places -- unless
>> you've loaded and used gnorb in your current session, you'll probably
>> want to require that yourself.
>
> Ah, yes, of course. It seems to be working, but it’s awfully silent. It
> would be great if it gave some feedback (I don’t know if it actually
> deleted anything, as there was nothing in the *Messages* buffer).

Whoops, forgot about this! Feedback is a very good idea. I'm sending
another version, this time as a patch in case this is something that
would be welcome in Org proper.

Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-attach.el-New-function-to-delete-unused-dirs.patch --]
[-- Type: text/x-diff, Size: 1835 bytes --]

From 03a8ddacf004d49a51eb7b0b48660fc31da955ac Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Sat, 1 Aug 2015 17:19:49 +0800
Subject: [PATCH] org-attach.el: New function to delete unused dirs

* lisp/org-attach.el (org-attach-clean-dirs): New function.
---
 lisp/org-attach.el | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 7f61910..84dc5a0 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -498,6 +498,39 @@ This function is called by `org-archive-hook'.  The option
 
 (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
 
+(defun org-attach-clean-dirs (&optional attach-dir clean-archived)
+  "Delete attach directories (and their contents) for headings
+which no longer exist."
+  (interactive)
+  (let ((attach-dir
+	 (if attach-dir
+	     (file-name-as-directory attach-dir)
+	   (concat (file-name-as-directory org-directory)
+		   org-attach-directory)))
+	(valid-dir-re "\\`[0-9a-z-]+\\'")
+	(org-id-search-archives (if clean-archived nil org-id-search-archives))
+	dead-dirs)
+    (dolist (d (directory-files attach-dir nil valid-dir-re))
+      (dolist (d+ (directory-files
+		   (concat attach-dir d) nil valid-dir-re))
+	(let ((id (format "%s%s" d d+))
+	      (full-path (concat
+			  attach-dir
+			  (file-name-as-directory d)
+			  d+)))
+	  (unless (org-id-find id)
+	    (push full-path dead-dirs)))))
+    (if dead-dirs
+	(progn
+	  (message "Deleting %d dead attach directories..." (length dead-dirs))
+	  (mapcar
+	   (lambda (d)
+	     (with-demoted-errors
+		 (delete-directory d t)))
+	   dead-dirs)
+	  (message "Deleting %d dead attach directories... done"))
+      (message "No dead directories to delete."))))
+
 (provide 'org-attach)
 
 ;; Local variables:
-- 
2.5.0


      reply	other threads:[~2015-08-01  9:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-16  8:57 function for cleaning org-attach directories Eric Abrahamsen
2015-07-16  9:38 ` Alan Schmitt
2015-07-16 12:43   ` Eric Abrahamsen
2015-07-17  5:19     ` Eric Abrahamsen
2015-07-17 15:01       ` Alan Schmitt
2015-07-18  3:11         ` Eric Abrahamsen
2015-07-20 15:04           ` Alan Schmitt
2015-08-01  9:23             ` Eric Abrahamsen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874mkjfjkz.fsf_-_@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).