From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sander Boer Subject: Assets Packing Date: Fri, 27 May 2011 15:53:27 +0200 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([140.186.70.92]:46009) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPxU2-000710-Ei for emacs-orgmode@gnu.org; Fri, 27 May 2011 09:53:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QPxU1-0007FM-Ij for emacs-orgmode@gnu.org; Fri, 27 May 2011 09:53:46 -0400 Received: from lo.gmane.org ([80.91.229.12]:56969) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPxU1-0007F9-AH for emacs-orgmode@gnu.org; Fri, 27 May 2011 09:53:45 -0400 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QPxTz-0007bU-CY for emacs-orgmode@gnu.org; Fri, 27 May 2011 15:53:43 +0200 Received: from 195-240-16-70.ip.telfort.nl ([195.240.16.70]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 27 May 2011 15:53:43 +0200 Received: from sanderboer by 195-240-16-70.ip.telfort.nl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 27 May 2011 15:53:43 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hi all, Maybe this already exists in org, but I needed a function that looks at all the image links, signals if the link is broken or not, copies the files into a folder and relinks it. Comments and crits are greatly appreciated, I started coding elisp 2 weeks ago, so any and all pointers and tips are welcome. -------8<------------------------------------- cut here (defcustom sndr-assets-dir nil "the directory org linked assets are moved to, do not forget trailing slash..." :type '(string) :group'sndr ) (defun sndr-assets-package() "finds file links and moves them to a dir specified, or ./assets , relative to the org file" (interactive) (let (p0 p1) (save-excursion (goto-char (point-min)) (while (search-forward-regexp "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]" nil t) (match-beginning 1) (setq p0 (match-beginning 1)) (setq p1 (match-end 1)) (sndr-link-check-and-move p0 p1) ) ))) (defun sndr-link-check-and-move (p0 p1) "gets a buffer section, checks if it is a file, moves the file to a dir specified (or ./assets) and updates the buffer accordingly" (let (file newdir newfile) (setq file (buffer-substring-no-properties p0 p1 )) (file-exists-p file) (setq newdir (or sndr-assets-dir (concat (file-name-directory buffer-file-name) "assets/"))) (setq newfile (concat newdir(file-name-nondirectory file))) (if (not (file-exists-p newdir)) (make-directory newdir t)) (if (file-exists-p file) (progn (if (not(file-exists-p newfile)) (copy-file file newfile)) (goto-char p0) (delete-region p0 p1) (insert newfile) ) (progn (goto-char p0) (delete-region p0 p1) (insert (concat "BROKEN#" file "#BROKEN")) )))) -------8<------------------------------------- cut here sander.