Browse Source

Add org capture

- config variables : templates and contexts
- function : replace counsel-projectile-org-capture
master
Maxime Wack 3 years ago
parent
commit
62a19156d2
1 changed files with 64 additions and 0 deletions
  1. +64
    -0
      projectorg.el

+ 64
- 0
projectorg.el View File

@@ -41,6 +41,10 @@ The projectorg/org-dir subdirectory contains the notes file for each project, as
Defaults to \"org\"
Contains the general notes files, the projects list files and each project notes file.")

(defvar projectorg/counsel-org-capture-templates-contexts nil)

(defvar projectorg/counsel-org-capture-templates nil)

;;;; Functions

(defun projectorg/project-name ()
@@ -92,6 +96,66 @@ And switch to a perspective for the project."
(projectile-kill-buffers)
(persp-kill proj)))

(defun projectorg/counsel-org-capture (&optional from-buffer)
"Capture into the current project.

This command is a replacement for `org-capture' (or
`counsel-org-capture') offering project-specific capture
templates, in addition to the regular templates available from
`org-capture'. These project templates, which are \"expanded\"
relatively to the current project, are determined by the
variables `projectorg/counsel-org-capture-templates' and
`projectorg/counsel-org-capture-templates-contexts'. See the
former variable in particular for details.

Optional argument FROM-BUFFER specifies the buffer from which to
capture."
(interactive)
(require 'org-capture)
(require 'counsel-projectile)
(setq counsel-projectile--org-capture-templates-backup org-capture-templates)
(let* ((ivy--actions-list (copy-sequence ivy--actions-list))
(root (ignore-errors (projectile-project-root)))
(name (projectorg/project-name))
(org-capture-templates-contexts
(append (when root
projectorg/counsel-org-capture-templates-contexts)
org-capture-templates-contexts))
(org-capture-templates
(append
(unless counsel-projectile-org-capture-templates-first-p
org-capture-templates)
(when root
(cl-loop
with replace-fun = `(lambda (string)
(replace-regexp-in-string
"\\${[^}]+}"
(lambda (s)
(pcase s
("${root}" ,root)
("${name}" ,name)))
string))
for template in projectorg/counsel-org-capture-templates
collect (cl-loop
for item in template
if (= (cl-position item template) 1) ;; template's name
collect (funcall replace-fun item)
else if (= (cl-position item template) 3) ;; template's target
collect (cl-loop
for x in item
if (stringp x)
collect (funcall replace-fun x)
else
collect x)
else
collect item)))
(when counsel-projectile-org-capture-templates-first-p
org-capture-templates))))
(ivy-add-actions
'counsel-org-capture
counsel-projectile-org-capture-extra-actions)
(with-current-buffer (or from-buffer (current-buffer))
(counsel-org-capture))))

(advice-add 'find-file :before 'projectorg/add-to-project-list) ; For every file opened, check if belongs to a project and add that project to the list of projects
(advice-add 'find-file-other-window :before 'projectorg/add-to-project-list)


Loading…
Cancel
Save