147
161
'org-attach-delete-one))
148
162
((eq c ?D) (call-interactively 'org-attach-delete-all))
149
163
((eq c ?q) (message "Abort"))
164
((memq c '(?s ?\C-s)) (call-interactively
165
'org-attach-set-directory))
166
((memq c '(?i ?\C-i)) (call-interactively
167
'org-attach-set-inherit))
150
168
(t (error "No such attachment command %c" c))))))
152
170
(defun org-attach-dir (&optional create-if-not-exists-p)
153
171
"Return the directory associated with the current entry.
172
This first checks for a local property ATTACH_DIR, and then for an inherited
173
property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
174
using the entry ID will be invoked to access the unique directory for the
154
176
If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
155
the directory and the corresponding ID will be created."
156
(let ((uuid (org-id-get (point) create-if-not-exists-p)))
157
(when (or uuid create-if-not-exists-p)
159
(let ((uuid-string (shell-command-to-string "uuidgen")))
161
(substring uuid-string 0 (1- (length uuid-string))))
162
(org-entry-put (point) "ID" uuid-string)
163
(setf uuid uuid-string)))
164
(let ((attach-dir (expand-file-name
168
(expand-file-name org-attach-directory))))
169
(if (and create-if-not-exists-p
170
(not (file-directory-p attach-dir)))
171
(make-directory attach-dir t))
172
(and (file-exists-p attach-dir)
177
the directory and (if necessary) the corresponding ID will be created."
178
(let (attach-dir uuid inherit)
179
(setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
181
((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
182
(org-attach-check-absolute-path attach-dir))
183
((and org-attach-allow-inheritance
184
(setq inherit (org-entry-get nil "ATTACH_DIR_INHERIT" t)))
189
(goto-char org-entry-property-inherited-from)
190
(let (org-attach-allow-inheritance)
191
(org-attach-dir create-if-not-exists-p)))))
192
(org-attach-check-absolute-path attach-dir)
193
(setq org-attach-inherited t))
195
(org-attach-check-absolute-path nil)
196
(setq uuid (org-id-get (point) create-if-not-exists-p))
197
(when (or uuid create-if-not-exists-p)
198
(unless uuid (error "ID retrieval/creation failed"))
199
(setq attach-dir (expand-file-name
203
(expand-file-name org-attach-directory))))))
205
(if (and create-if-not-exists-p
206
(not (file-directory-p attach-dir)))
207
(make-directory attach-dir t))
208
(and (file-exists-p attach-dir)
211
(defun org-attach-check-absolute-path (dir)
212
"Check if we have enough information to root the atachment directory.
213
When DIR is given, check also if it is already absolute. Otherwise,
214
assume that it will be relative, and check if `org-attach-directory' is
215
absolute, or if at least the current buffer has a file name.
216
Throw an error if we cannot root the directory."
217
(or (and dir (file-name-absolute-p dir))
218
(file-name-absolute-p org-attach-directory)
219
(buffer-file-name (buffer-base-buffer))
220
(error "Need absolute `org-attach-directory' to attach in buffers without filename.")))
222
(defun org-attach-set-directory ()
223
"Set the ATTACH_DIR property of the current entry.
224
The property defines the directory that is used for attachments
227
(let ((dir (org-entry-get nil "ATTACH_DIR")))
228
(setq dir (read-directory-name "Attachment directory: " dir))
229
(org-entry-put nil "ATTACH_DIR" dir)))
231
(defun org-attach-set-inherit ()
232
"Set the ATTACH_DIR_INHERIT property of the current entry.
233
The property defines the directory that is used for attachments
234
of the entry and any children that do not explicitly define (by setting
235
the ATTACH_DIR property) their own attachment directory."
237
(org-entry-put nil "ATTACH_DIR_INHERIT" "t")
238
(message "Children will inherit attachment directory"))
175
240
(defun org-attach-commit ()
176
241
"Commit changes to git if `org-attach-directory' is properly initialized.
335
400
(org-attach-open 'in-emacs))
402
(defun org-attach-expand (file)
403
"Return the full path to the current entry's attachment file FILE.
404
Basically, this adds the path to the attachment directory."
405
(expand-file-name file (org-attach-dir)))
407
(defun org-attach-expand-link (file)
408
"Return a file link pointing to the current entry's attachment file FILE.
409
Basically, this adds the path to the attachment directory, and a \"file:\"
411
(concat "file:" (org-attach-expand file)))
337
413
(provide 'org-attach)
415
;; arch-tag: fce93c2e-fe07-4fa3-a905-e10dcc7a6248
339
416
;;; org-attach.el ends here