Log time spent on emacs org-mode capture items - emacs

I have a capture template set up to file journal entries in an org file.
(setq org-capture-templates
'(("j" "Journal" entry (file+datetree "/home/emil/org/journal_.org")
(file "/home/emil/org/journal-template.org") :prepend :clock-in))
I want to clock out and output the time elapsed on the clock started with :clock-in when the capture is filed, but I don't know how to do this. Can anyone give me some ideas?

I think you got the template slightly wrong.
Here's how I have it:
(setq org-capture-templates
`(("t" "todo" entry
(file+headline (concat org.d "gtd.org") "Tasks")
"* TODO %^{Brief Description} %^g \nAdded: %U %i\n %?\n"
:clock-in t :clock-resume t)))
So that's :clock-in t, not just plain :clock-in.

Related

emacs elisp why won't a variable work here? [duplicate]

I've seen this:
How to type a dynamic file entry for org capture
but cannot get it to work; I get "Invalid file location: nil". Has something changed in org-mode or in Emacs itself to stop this from working? Otherwise: suggestions for how to debug what has gone wrong?
What I'm really trying to get working is what is described on this page:
http://www.howardism.org/Technical/Emacs/journaling-org.html
The capture template I'm interested in is the "Journal Note" one all the way at the bottom of the page:
(setq org-capture-templates '(
;; ...
("j" "Journal Note"
entry (file (get-journal-file-today))
"* Event: %?\n\n %i\n\n From: %a"
:empty-lines 1)
;; ..
))
Thanks for any assistance.
Figured it out ... it is using a backquote instead of a normal quote for the entire capture template block! I missed this because all of the answers I saw had only a single capture template with a backquote in front of it; I tried doing that but this doesn't work if the template is "one of" ...
So here is a snippet a bit richer than those I found; I hope it helps someone else.
(setq org-capture-templates
`(("t" "TODO" entry (file+datetree "~/Documents/org/tasks.org" "Tasks")
"* TODO [#C] %?\n SCHEDULED: <%<%Y-%m-%d %a>>\n [%<%Y-%m-%d %a>]\n %a")
("T" "Travel" entry (file+datetree+prompt "~/Documents/org/travel.org")
"* %?\n :PROPERTIES:\n :LOCATION:\n :END:\n %t\n %a")
("j" "Journal Note" entry (
file+olp+datetree
,(concat
org-journal-dir
(format-time-string "journal-%m-%d.org")))
"* Event: %?\n %i\n From: %a")
)
)
The keys are the backquote ` at the start of the capture template def block, and the comma , before (concat ... ) on the function being called.
It seems like something has changed between Org-mode 8.2.10 and 9.1.9 specifically in the way Org handles template elements. Whereas in earlier version of Org the second value in the pair (file ...) could be a function that Org would evaluate, now it seems only a string (file path) is valid here.
The fix is to use the backquote list form, and explicitly state that the function needs evaluating using the comma:
(setq org-capture-templates `(
;; ...
("j" "Journal Note"
entry (file ,(get-journal-file-today))
"* Event: %?\n\n %i\n\n From: %a"
:empty-lines 1)
;; ..
))

How to add tags completion to org mode capture?

I use org mode's capture functionality to make all my todo's. It is clean and practical and let me add a consistent content to all my todo's, including a prompt for heading, a prompt for tags and an automatic insertion of created date. Here is my code:
(setq org-capture-templates '((
"t" ; key
"Todo" ; description
entry ; type
(file+headline "C:/.../org/notes.org" "tasks") ; target
"* TODO [#B] %^{Todo} :%^{Tags}: \n:PROPERTIES:\n:Created: %U\n:END:\n\n%?" ; template
:prepend t ; properties
:empty-lines 1 ; properties
:created t ; properties
)))
However, my prompt for tags forces me to enter tags from memory. How could I add tags from the tags list set by the following code:
(setq org-tag-alist `(
("OFFICE" . ?o)
("HOME" . ?h)
("ERRAND" . ?e) ))
When my point is in the heading of an already created task, this list pops up when I hit C-c C-c and let me chose the tags by their short cut single letters "o", "h" or "e".
So my question is: is it possible to include this pop-up list of tags inside the code for my capture?
The built in solution is to use %^g. From the help for org-capture-templates:
%^g Prompt for tags, with completion on tags in target file.
%^G Prompt for tags, with completion on all tags in all agenda files.
You can also do this "by hand" by calling some function that adds the tags. Adding tags is generally done with org-set-tags (this is what C-c C-c is doing). So, all we have to do is call that in our template with the %(func) syntax:
(setq org-capture-templates '((
"t" ; key
"Todo" ; description
entry ; type
(file+headline "C:/.../org/notes.org" "tasks") ; target
"* TODO [#B] %^{Todo} %(org-set-tags) \n:PROPERTIES:\n:Created: %U\n:END:\n\n%?" ; template
:prepend t ; properties
:empty-lines 1 ; properties
:created t ; properties
)))
If you have a specific list of tags you want to select from (say org-tag-alist) you can use completing-read to select from it:
(completing-read "Tag: " (mapcar #'first org-tag-persistent-alist) nil t)

Agenda view of the current buffer

EDIT: The solution was simple but "bonus points" for anyone that can explain why my method didn't work.
ORIG:
I would like an org-mode-custom-command to display an agenda which is only made from the current buffer.
The following snippet shows the kind of view that I want.
(setq org-agenda-custom-commands
'(("b" "Buffer summary"
((todo "TODO" ((org-agenda-files '("~/.agenda/notes.org"))))))))
But, I don't want to specify a filename, rather I want to use the current buffer. Here is my stab at it.
(setq org-agenda-custom-commands
'(("b" "Buffer summary"
((todo "TODO" ((org-agenda-files (buffer-file-name))))))))
When I open an org-buffer and run this agenda command the result is just a pretty much blank agenda view. I presume it's because buffer-file-name is being evaluated at point later than when I press the agenda view...?
I'm still beginning to learn elisp, so don't hesitate to point out the obvious. Thank-you.
EDIT:
Following a suggestion in the comments.
(setq org-agenda-custom-commands
'(("b" "Buffer summary"
((todo "TODO" ((org-agenda-files (list (buffer-file-name)))))))))
I receive a backtrace.
Debugger entered--Lisp error: (wrong-type-argument stringp nil)
file-directory-p(nil)
...etc...
For me, the most obvious is NOT to create an extra agenda view, and simply call any existing view on that buffer only (limit the view to the current buffer with a call such as C-c a < a, where < limits on the current buffer).
If you still want to make an extra agenda view for the current buffer, I'm not sure whether that's possible with all commands. For sure, calling occur-tree will work on the current buffer. Not sure about todo and the like.
...The agenda view usually shows the information collected from all the agenda files For more info, first make sure that file you are trying to make agenda-viwable or trackable, by activating it via an org command C-c [ (org-agenda-file-to-front)
you can test the current buffe name by
(message (buffer-file-name))
Normally, I use my agenda view from various files, but I can track different agendas e.g done, on-progress, fixed, bug, just by using hot-keys
Here is mine:
(setq org-agenda-custom-commands
'(
("x" agenda)
("y" agenda*)
("o" todo "ONPROGRESS")
("n" tags-todo "+TIPS")
("d" todo "DONE")
("p" todo "PENDING")
("b" todo "BUG")
("f" todo "FIXED")
;("F" todo-tree "FIXED")
))
So if I want to view e.g done stuffs C-c a d this will show me all done lists regardless of which file they came.

How can I load an Emacs capture template into an open file?

Please excuse my newness to Emacs Lisp. I started using org-mode and love it. In my workflow, I am trying to load a template into an open buffer or prompt the template to ask for a location to where the file is to be saved.
For example, I have a 'meetings' template. When I call that template, I would like to be prompted for a filename and then the template will be loaded into that file and file will be loaded in Emacs.
How can I do this within Emacs?
This is the best I could come up with so far:
(defun caputre-create-meeting-link ()
(let ((new-file (read-file-name "Save meeting info in ")))
(run-with-timer 1 nil (eval `(lambda () (find-file ,new-file))))
(format "[[%s]]" new-file)))
(setq org-capture-templates
'(("a" "Insert a link to meeting" plain
(file "~/org/notes.org")
"Meeting info: %(caputre-create-meeting-link)"
:immediate-finish t)))
to more or less get the effect you describe. But you probably could simply substitute %(capture-create-meeting-link) with %^L and then C-c C-o on the link to open it.

Interactively enter headline under which to place an entry using capture

Using capture templates like the one below, I can add entries to different headlines in a file. How can I manually enter a headline during capture, instead of setting up each headline to a key in the .emacs file like I am now doing?
(setq org-capture-templates
'(
("l" "Log" entry
(file+headline "c:/Org/log.org" "Log")
"\n\n** %?\n<%<%Y-%m-%d %a %T>>"
:empty-lines 1))
It looks like, in newer versions of org at least, that custom functions can be used in capture templates to do this.
Instead of:
entry
(file+headline "~/Work/work.org" "Refile")
You can use:
entry
(file+function "~/Work/work.org" function-finding-location)
Where 'function-finding-location' is a custom function you have written yourself, which could easily prompt you for a headline.
Or, you can go even farther, and define a custom function which will prompt for both file name and headline name (or anything else you can dream up):
entry
(function function-finding-location)
I don't really know enough elisp to write these functions myself, but this looks like the place to start. It'd be nice if someone else could offer up some code. The relevant documentation is here:
http://orgmode.org/manual/Template-elements.html
I wrote a function to be used with file+function which will prompt for a location on capture.
It uses the internal prompting function of org-refile so we get completions of headings in the prompt (with maxlevels overridden to 9). When the user enters an unknown heading, it creates it at the end of the file.
(defun org-ask-location ()
(let* ((org-refile-targets '((nil :maxlevel . 9)))
(hd (condition-case nil
(car (org-refile-get-location nil nil t t))
(error (car org-refile-history)))))
(goto-char (point-min))
(outline-next-heading)
(if (re-search-forward
(format org-complex-heading-regexp-format (regexp-quote hd))
nil t)
(goto-char (point-at-bol))
(goto-char (point-max))
(or (bolp) (insert "\n"))
(insert "* " hd "\n")))
(end-of-line))
In your case, you use it like this:
(setq org-capture-templates
'(("l" "Log" entry
(file+function "c:/Org/log.org" org-ask-location)
"\n\n** %?\n<%<%Y-%m-%d %a %T>>"
:empty-lines 1))
I don't believe you can have it prompt for the headline on capture. You can however refile from within the capture window which should result in the desired behaviour.
I would define a catch-all target headline/file so that if you forget you will always collect them in the same location and then just have to refile them once created. If you also set a category/tag on this headline you will be able to easily see the misfiled capture entry and refile it as desired. (Example below)
Then instead of finishing with C-c C-c choose to refile with C-c C-w and you will be asked to select the headline you want to send the new entry to.
The capture template I use for this catch all is as follows (adapted from Bernt Hansen's capture settings)
("i"
"Incidents"
entry
(file+headline "~/Work/work.org" "Refile")
"* TODO %^{Ticket} - %^{User}\nSCHEDULED: %^t DEADLINE: %^t\n:PROPERTIES:
\n:DATE: %^U\n:END:\n%^{MANAGER}p%^{HOSTNAME}p%^{LOCATION}p%^{TEL}p\n%c"
:empty-lines 1 :clock-in t :clock-resume t)
(Line breaks are added to avoid scrolling when reading here)
The heading is configured as follows
* Refile :refile:
:PROPERTIES:
:CATEGORY: Unsorted
:END:
With this I end up with all non-refiled tasks showing up as
Unsorted: Deadline: TODO <Headline> :refile::
I currently tend to use tags as reference if I'm waiting for coworkers/managers to deal with the ticket, or to remind me to speak to them about it when I see them so the tag at the end stands out clearly, as does Unsorted if I'm trying to remember what the issue is (since I simply have a case number and user name showing, details within the entry).
while capturing a note, after finishing writeup press C-u C-c C-w to refile under desired new headline.
you also need to set this variable
(setq org-refile-allow-creating-parent-nodes (quote confirm))
you can set it to t instead of confirm. But I like it be confirm because I dont often refile to new targets