2015-11-25 17:24:06 -05:00
|
|
|
;;; defuns-org-crm.el --- for my custom org-based CRM
|
|
|
|
|
|
|
|
(defun narf--helm-org (&optional directory)
|
2015-11-30 16:47:31 -05:00
|
|
|
(let ((helm-deft-dir-list `(,(or directory default-directory))))
|
|
|
|
(helm-deft)))
|
2015-11-25 17:24:06 -05:00
|
|
|
|
2015-11-25 21:21:17 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun narf/helm-org ()
|
|
|
|
(interactive)
|
|
|
|
(let ((default-directory org-directory))
|
|
|
|
(helm-projectile-find-file)))
|
|
|
|
|
2015-11-25 17:24:06 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun narf/helm-org-crm-projects ()
|
|
|
|
(interactive)
|
|
|
|
(narf--helm-org org-directory-projects))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun narf/helm-org-crm-contacts ()
|
|
|
|
(interactive)
|
|
|
|
(narf--helm-org org-directory-contacts))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun narf/helm-org-crm-invoices ()
|
|
|
|
(interactive)
|
|
|
|
(narf--helm-org org-directory-invoices))
|
|
|
|
|
|
|
|
;;;###autoload
|
2015-11-30 16:47:31 -05:00
|
|
|
(defun narf/helm-org-writing ()
|
2015-11-25 17:24:06 -05:00
|
|
|
(interactive)
|
|
|
|
(let ((narf--helm-org-params '()))
|
|
|
|
(narf--helm-org (expand-file-name "writing/" org-directory))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun narf/org-crm-link-contact (id)
|
2015-12-04 03:35:06 -05:00
|
|
|
(org-open-file (narf--org-crm-id-to-path id 'contact) t))
|
2015-11-25 17:24:06 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun narf/org-crm-link-project (id)
|
2015-12-04 03:35:06 -05:00
|
|
|
(org-open-file (narf--org-crm-id-to-path id 'project) t))
|
2015-11-25 17:24:06 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun narf/org-crm-link-invoice (id)
|
2015-12-04 03:35:06 -05:00
|
|
|
(org-open-file (narf--org-crm-id-to-path id 'invoice) t))
|
2015-11-25 17:24:06 -05:00
|
|
|
|
|
|
|
(defun narf--org-complete (type)
|
|
|
|
(let ((default-directory (symbol-value (intern (format "org-directory-%ss" type)))))
|
|
|
|
(let* ((file (org-iread-file-name ">>> "))
|
|
|
|
(match (s-match "^\\([0-9]+\\)[-.]" (f-filename file))))
|
|
|
|
(unless (file-exists-p file)
|
|
|
|
(message "Created %s" file)
|
|
|
|
(write-region "" nil file))
|
|
|
|
(unless match
|
|
|
|
(user-error "Invalid file ID"))
|
|
|
|
(format "%s:%s" type (cadr match)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
2015-12-04 03:35:06 -05:00
|
|
|
(defun org-contact-complete-link () (narf--org-complete 'contact))
|
2015-11-25 17:24:06 -05:00
|
|
|
;;;###autoload
|
2015-12-04 03:35:06 -05:00
|
|
|
(defun org-project-complete-link () (narf--org-complete 'project))
|
2015-11-25 17:24:06 -05:00
|
|
|
;;;###autoload
|
2015-12-04 03:35:06 -05:00
|
|
|
(defun org-invoice-complete-link () (narf--org-complete 'invoice))
|
2015-11-25 17:24:06 -05:00
|
|
|
|
2015-12-04 03:35:06 -05:00
|
|
|
(defun narf--org-crm-assert-type (type)
|
2015-11-30 16:47:31 -05:00
|
|
|
(unless (memq type '(project contact invoice))
|
2015-12-04 03:35:06 -05:00
|
|
|
(user-error "Not a valid type: %s" type)))
|
|
|
|
|
|
|
|
(defun narf--org-crm-new-path (name type)
|
|
|
|
(narf--org-crm-assert-type type)
|
|
|
|
(let* ((prefix
|
2015-11-30 16:47:31 -05:00
|
|
|
(replace-regexp-in-string
|
2015-12-04 03:35:06 -05:00
|
|
|
"/+$" "" (symbol-value (intern (format "org-directory-%ss" type)))))
|
|
|
|
(last-file (car-safe (sort (f-glob "*.org" prefix) 'string>))))
|
|
|
|
(when last-file
|
|
|
|
(let* ((old-id (narf--org-crm-path-to-id last-file type))
|
|
|
|
(new-id (format "%04X" (1+ old-id))))
|
|
|
|
(if (eq type 'invoice)
|
|
|
|
(format "%s/%s-%s.org" prefix (format-time-string "%y%m") new-id)
|
|
|
|
(format "%s/%s-%s.org" prefix
|
|
|
|
new-id (replace-regexp-in-string "[][ !@#$%^&*()]" "-" name)))))))
|
|
|
|
|
|
|
|
(defun narf--org-crm-path-to-id (path type)
|
|
|
|
(narf--org-crm-assert-type type)
|
|
|
|
(let ((base (f-filename path)))
|
|
|
|
(string-to-number
|
|
|
|
(if (eq type 'invoice)
|
|
|
|
(substring base (1+ (string-match-p "-" base)) (string-match-p ".org" base))
|
|
|
|
(substring base 0 (string-match-p "[-.]" base)))
|
|
|
|
16)))
|
|
|
|
|
|
|
|
(defun narf--org-crm-id-to-path (id type)
|
|
|
|
(narf--org-crm-assert-type type)
|
|
|
|
(let* ((prefix
|
2015-11-30 16:47:31 -05:00
|
|
|
(replace-regexp-in-string
|
2015-12-04 03:35:06 -05:00
|
|
|
"/+$" "" (symbol-value (intern (format "org-directory-%ss" type))))))
|
|
|
|
(car-safe
|
|
|
|
(f-glob (format (if (eq type 'invoice) "*-%04X.org" "%04X*.org")
|
|
|
|
(string-to-number id 16))
|
|
|
|
prefix))))
|
2015-11-30 16:47:31 -05:00
|
|
|
|
|
|
|
(defun narf--org-crm (&optional id type new-p)
|
2015-12-04 03:35:06 -05:00
|
|
|
(let ((file (if new-p
|
|
|
|
(or (narf--org-crm-new-path id type)
|
|
|
|
(user-error "path could not be resolved: type(%s) name(%s)" type name))
|
|
|
|
(or (narf--org-crm-id-to-path id type)
|
|
|
|
(user-error "id %s could not be resolved in %s" id type))))
|
|
|
|
(old-buffer (current-buffer)))
|
|
|
|
(find-file file)
|
|
|
|
(with-current-buffer old-buffer
|
2015-11-30 16:47:31 -05:00
|
|
|
(when (evil-visual-state-p)
|
|
|
|
(org-insert-link
|
2015-12-04 03:35:06 -05:00
|
|
|
nil (format "%s:%s" (symbol-name type) (narf--org-crm-path-to-id file type))
|
|
|
|
(buffer-substring-no-properties (region-beginning) (region-end)))))))
|
2015-11-30 16:47:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
;;;###autoload (autoload 'narf:org-crm-project "defuns-org-crm" nil t)
|
|
|
|
(evil-define-command narf:org-crm-project (&optional bang name)
|
|
|
|
(interactive "<!><a>")
|
2015-12-04 03:35:06 -05:00
|
|
|
(if bang
|
|
|
|
(narf--org-crm name 'project t)
|
|
|
|
(narf/helm-org-crm-projects)))
|
2015-11-30 16:47:31 -05:00
|
|
|
;;;###autoload (autoload 'narf:org-crm-contact "defuns-org-crm" nil t)
|
|
|
|
(evil-define-command narf:org-crm-contact (&optional bang name)
|
|
|
|
(interactive "<!><a>")
|
2015-12-04 03:35:06 -05:00
|
|
|
(if bang
|
|
|
|
(narf--org-crm name 'contact t)
|
|
|
|
(narf/helm-org-crm-contacts)))
|
2015-11-30 16:47:31 -05:00
|
|
|
;;;###autoload (autoload 'narf:org-crm-invoice "defuns-org-crm" nil t)
|
|
|
|
(evil-define-command narf:org-crm-invoice (&optional bang)
|
|
|
|
(interactive "<!>")
|
2015-12-04 03:35:06 -05:00
|
|
|
(if bang
|
|
|
|
(narf--org-crm nil 'invoice t)
|
|
|
|
(narf/helm-org-crm-invoices)))
|
2015-11-25 17:24:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
(provide 'defuns-org-crm)
|
|
|
|
;;; defuns-org-crm.el ends here
|