doomemacs/modules/defuns/defuns-org-notes.el

113 lines
4.3 KiB
EmacsLisp
Raw Normal View History

2016-08-21 16:53:38 +02:00
;;; defuns-org-notes.el
;;;###autoload
2016-05-20 22:37:30 -04:00
(defun doom/org ()
(interactive)
(find-file (f-expand "inbox.org" org-directory)))
;;;###autoload
2016-05-20 22:37:30 -04:00
(defun doom/org-notebook-new ()
(interactive)
(projectile-invalidate-cache nil)
(let* ((default-directory org-directory)
(dir (projectile-complete-dir))
2016-05-20 22:37:30 -04:00
(doom-org-quicknote-dir dir))
(when dir
2016-05-20 22:37:30 -04:00
(doom/org-notebook-quick-note))))
;;;###autoload
2016-05-20 22:37:30 -04:00
(defun doom/org-notebook-quick-note ()
(interactive)
(let (text)
(when (evil-visual-state-p)
(setq text (buffer-substring-no-properties evil-visual-beginning evil-visual-end)))
(switch-to-buffer (generate-new-buffer "*quick-note*"))
2016-05-20 22:37:30 -04:00
(setq default-directory doom-org-quicknote-dir)
(erase-buffer)
(insert text)))
;;;###autoload
2016-05-20 22:37:30 -04:00
(defun doom/org-download-dnd (uri action)
(if (eq major-mode 'org-mode)
2016-05-20 22:37:30 -04:00
(doom:org-attach uri)
(let ((dnd-protocol-alist
2016-05-20 22:37:30 -04:00
(rassq-delete-all 'doom/org-download-dnd (copy-alist dnd-protocol-alist))))
(dnd-handle-one-url nil action uri))))
2016-08-21 16:53:38 +02:00
;;;###autoload (autoload 'doom:org-attach "defuns-org-notes" nil t)
2016-05-20 22:37:30 -04:00
(evil-define-command doom:org-attach (&optional uri)
(interactive "<a>")
(unless (eq major-mode 'org-mode)
(user-error "Not in an org-mode buffer"))
(if uri
(let* ((rel-path (org-download--fullname uri))
(new-path (f-expand rel-path))
(image-p (image-type-from-file-name uri)))
(cond ((string-match-p (concat "^" (regexp-opt '("http" "https" "nfs" "ftp" "file")) ":/") uri)
(url-copy-file uri new-path))
(t (copy-file uri new-path)))
(unless new-path
(user-error "No file was provided"))
(if (evil-visual-state-p)
(org-insert-link nil (format "./%s" rel-path)
(concat (buffer-substring-no-properties (region-beginning) (region-end))
2016-08-21 16:53:38 +02:00
" " (doom--org-attach-icon rel-path)))
(insert (if image-p
2016-08-21 16:53:38 +02:00
(format "[[./%s]] " rel-path)
(format "%s [[./%s][%s]] "
(doom--org-attach-icon rel-path)
rel-path (f-filename rel-path)))))
(when (string-match-p (regexp-opt '("jpg" "jpeg" "gif" "png")) (f-ext rel-path))
2016-08-21 16:53:38 +02:00
(org-redisplay-inline-images)))
(let ((default-directory ".attach/"))
(if (file-exists-p default-directory)
(call-interactively 'find-file)
(user-error "No attachments")))))
2016-08-21 16:53:38 +02:00
(defun doom--org-attach-icon (path)
(char-to-string (pcase (downcase (f-ext path))
("jpg" ?) ("jpeg" ?) ("png" ?) ("gif" ?)
("pdf" ?)
("ppt" ?) ("pptx" ?)
("xls" ?) ("xlsx" ?)
("doc" ?) ("docx" ?)
("ogg" ?) ("mp3" ?) ("wav" ?)
("mp4" ?) ("mov" ?) ("avi" ?)
("zip" ?) ("gz" ?) ("tar" ?) ("7z" ?) ("rar" ?)
2016-08-21 16:53:38 +02:00
(_ ?))))
;;;###autoload
2016-08-21 16:53:38 +02:00
(defun doom/org-cleanup-attachments ()
;; "Deletes any attachments that are no longer present in the org-mode buffer."
(let* ((attachments-local (doom-org-attachments))
(attachments (f-entries org-attach-directory))
(to-delete (-difference attachments-local attachments)))
;; TODO
to-delete))
(defun doom-org-attachments ()
(unless (eq major-mode 'org-mode)
(user-error "Not an org buffer"))
(org-save-outline-visibility nil
(let ((attachments '())
element
file)
(when (and (f-dir? org-attach-directory)
(> (length (f-glob (concat (f-slash org-attach-directory) "*"))) 0))
(save-excursion
(goto-char (point-min))
(while (progn (org-next-link) (not org-link-search-failed))
(setq element (org-element-lineage (org-element-context) '(link) t))
(when element
(setq file (expand-file-name (org-element-property :path element)))
(when (and (string= (org-element-property :type element) "file")
(string= (concat (f-base (f-dirname file)) "/") org-attach-directory)
(file-exists-p file))
(push file attachments))))))
(-distinct attachments))))
2016-08-21 16:53:38 +02:00
(provide 'defuns-org-notes)
;;; defuns-org-notes.el ends here