Org-mode: Output Weekly agenda using -batch and -eval - emacs

I am trying to display my org agenda on the background of my desktop even when Emacs is closed or minimized. I have made some progress using this command:
emacs -batch -l ~/.emacs -eval '(org-batch-agenda "t")' 2> /dev/null
It outputs:
Global list of TODO items of type: ALL
Available with `N r': (0)[ALL]
remind: TODO Garbage
remind: TODO Garbage and Recycling
remind: TODO Refill Prescription
remind: TODO Vitamins
remind: TODO Water Indoor Plants
remind: TODO Wake up!
remind: TODO Go to Sleep!
My .emacs contains the following relevant lines:
;; org-agenda
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-cb" 'org-iswitchb)
(setq org-agenda-files (list "~/Code/remind.org"
"~/Code/todo.org"
))
;; show org-agenda each time Emacs is opened
(add-hook 'after-init-hook 'org-agenda-list)
My question is this. How do I customize this output to not display the following?
Global list of TODO items of type: ALL
Available with `N r': (0)[ALL]

Use a different command key argument in the call to org-batch-agenda, e.g.:
(org-batch-agenda "a")
The doc string for org-batch-agenda says:
If CMD-KEY is a string of length 1, it is used as a key in
‘org-agenda-custom-commands’ and triggers this command.
If you don't like any of the existing ones, you can always define your own agenda custom command and attach a key to it; then you can call org-batch-agenda with that key.

Related

How to setup a key in org-agenda to mark a task as DONE and then ARCHIVE it?

How can one setup a key in org-agenda to mark a task as DONE and then ARCHIVE it?
In my org-agenda, I have a key to mark as done (courtesy to Sacha Chua):
(defun sacha/org-agenda-done (&optional arg)
"Mark current TODO as done.
This changes the line at point, all other lines in the agenda referring to
the same tree node, and the headline of the tree node in the Org-mode file."
(interactive "P")
(org-agenda-todo "DONE"))
;; Override the key definition for org-exit
(define-key org-agenda-mode-map "x" 'sacha/org-agenda-done)
I have a key to archive an entry (in the archive file while keeping context, courtesy of #codefalling in https://gist.github.com/CodeFalling/87b116291aa87fde72cb):
(define-key org-agenda-mode-map "z" 'org-agenda-archive)
But when I tried to combine both into a single key, I partially failed, because emacs does not return back to the original buffer in org-agenda mode after archiving the entry:
(defun jyby/org-agenda-mark-done-and-archive ()
"Mark the current TODO as done and archive it."
(interactive)
(org-agenda-todo "DONE")
(save-excursion
(org-agenda-switch-to)
(org-archive-subtree-hierarchical)
)
)
(define-key org-agenda-mode-map "D" 'jyby/org-agenda-mark-done-and-archive)
Any idea why?
(For the record, I did try with the function save-current-buffer, to no avail.)
OF course, I would also like to cancel and archive in a single key:
(defun jyby/org-agenda-cancel-and-archive ()
"Mark the current TODO as done and archive it."
(interactive)
(save-current-buffer
(org-agenda-todo "CANC")
(org-agenda-switch-to)
(org-archive-subtree-hierarchical)
)
)
(define-key org-agenda-mode-map "C" 'jyby/org-agenda-cancel-and-archive)

How to create a pdf from a Rmd file using Emacs, ESS, pandoc-mode and polymode?

This is an adaptation of a "classic" Rmd file that I want to knit as a pdf using Emacs (Emacs Speak Statistics) and polymode. I can't find the right commands to do that. There is little documentation about polymode. I am using Emacs Starter Kit for the Social Sciences.
---
title: "Untitled"
author: "SB"
date: "Wednesday, February 04, 2015"
output: pdf_document
---
You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
As the doc says use M-n w and M-n W to set/change the weaver. With ESS you probably should use knitr-ESS weaver as it uses current *R* process.
You can use rmarkdown::render() from the rmarkdown package to knit an .Rmd file to markdown and render the output file (PDF, Word, HTML, etc.) with a single command!
I wasn't sure if support for an rmarkdown workflow was already included in ESS (and I'm trying to dabble in elisp) so I wrote a function that calls rmarkdown::render() and allows customizing the inputs to rmarkdown::render() function call with a prefix arg (e.g., C-u).
;; spa/rmd-render
;; Global history list allows Emacs to "remember" the last
;; render commands and propose as suggestions in the minibuffer.
(defvar rmd-render-history nil "History list for spa/rmd-render.")
(defun spa/rmd-render (arg)
"Render the current Rmd file to PDF output.
With a prefix arg, edit the R command in the minibuffer"
(interactive "P")
;; Build the default R render command
(setq rcmd (concat "rmarkdown::render('" buffer-file-name "',"
"output_dir = '../reports',"
"output_format = 'pdf_document')"))
;; Check for prefix argument
(if arg
(progn
;; Use last command as the default (if non-nil)
(setq prev-history (car rmd-render-history))
(if prev-history
(setq rcmd prev-history)
nil)
;; Allow the user to modify rcmd
(setq rcmd
(read-from-minibuffer "Run: " rcmd nil nil 'rmd-render-history))
)
;; With no prefix arg, add default rcmd to history
(setq rmd-render-history (add-to-history 'rmd-render-history rcmd)))
;; Build and evaluate the shell command
(setq command (concat "echo \"" rcmd "\" | R --vanilla"))
(compile command))
(define-key polymode-mode-map (kbd "C-c r") 'spa/rmd-render)
Note that I have some specific parameter settings like output_dir = '../reports' but the elisp can be easily customized to suit your needs.
With this in your init file, you only need to enter C-c r from inside your .Rmd file (or C-u C-c r to render to a different format, location, etc.). The command will open a new window with a buffer called *compilation* where any errors will appear.
This could definitely be improved and I'd love to hear suggestions.

Emacs Org-mode: Quickly mark TODO as DONE

I'd like to have a shortcut to change the state of a TODO straight to DONE (and back) with the CLOSED time tag folded when I'm at any position on the line (not like speed-keys requiring to be before the first asterisk).
Currently I have 2 options:
C-c C-t d TAB (with org-use-fast-todo-selection set to t, d is my DONE state shortcut and TAB hides the subtree), or
S-right TAB (using org-shiftright, DONE is the first state after TODO).
Can you please help me bind this to a single shortcut like C-c C-d. Please note I have more states than TODO and DONE but this shortcut should just toggle between those too.
Bonus points: Additional command that also starts a new TODO item on the next line at the same level as the previous task.
Thank you very much!
Not sure what you mean by "time tag", but based on the workflows you listed, the following command should do what you want:
(defun org-toggle-todo-and-fold ()
(interactive)
(save-excursion
(org-back-to-heading t) ;; Make sure command works even if point is
;; below target heading
(cond ((looking-at "\*+ TODO")
(org-todo "DONE")
(hide-subtree))
((looking-at "\*+ DONE")
(org-todo "TODO")
(hide-subtree))
(t (message "Can only toggle between TODO and DONE.")))))
(define-key org-mode-map (kbd "C-c C-d") 'org-toggle-todo-and-fold)
As for inserting new TODO items on the same level as the current task, org-mode has built-in commands for this. You can read up on them by doing
C-h f org-insert-todo-heading RET
C-h f org-insert-todo-heading-respect-content RET
A simple toggle command could look like the following
(defun my-org-todo-toggle ()
(interactive)
(let ((state (org-get-todo-state))
post-command-hook)
(if (string= state "TODO")
(org-todo "DONE")
(org-todo "TODO"))
(run-hooks 'post-command-hook)
(org-flag-subtree t)))
(define-key org-mode-map (kbd "C-c C-d") 'my-org-todo-toggle)
The post-command-hook is a bit tricky, but is required since otherwise notes are added (and revealed) after the command, which makes the time log partially unfolded.
In order to start a new TODO item, you might have a look at the existing org-insert-todo-heading (bound to <M-S-return>)

Add CREATED date property to TODOs in org-mode

I read the org-mode manual but couldn't find an easy way to add a CREATED field to newly created TODOs. In combination with org-log-done one could then compute the time it took to close a particular TODO. This is especially useful when using archive files.
Example:
* TODO Do something
CREATED: [2012-09-02 Sun 23:02]
* DONE Do something else
CREATED: [2012-09-02 Sun 20:02]
CLOSED: [2012-09-02 Sun 22:02]
I would expect the CREATED field to be added to new tasks (tasks which don't have that field) whenever the file is saved.
Any suggestions on how to achieve this? Using something like Git is not a solution for me to track the creations of TODOS.
I use org-expiry to implement that functionality, which is in the contrib directory of org.
The base configuration I use is:
;; Allow automatically handing of created/expired meta data.
(require 'org-expiry)
;; Configure it a bit to my liking
(setq
org-expiry-created-property-name "CREATED" ; Name of property when an item is created
org-expiry-inactive-timestamps t ; Don't have everything in the agenda view
)
(defun mrb/insert-created-timestamp()
"Insert a CREATED property using org-expiry.el for TODO entries"
(org-expiry-insert-created)
(org-back-to-heading)
(org-end-of-line)
(insert " ")
)
;; Whenever a TODO entry is created, I want a timestamp
;; Advice org-insert-todo-heading to insert a created timestamp using org-expiry
(defadvice org-insert-todo-heading (after mrb/created-timestamp-advice activate)
"Insert a CREATED property using org-expiry.el for TODO entries"
(mrb/insert-created-timestamp)
)
;; Make it active
(ad-activate 'org-insert-todo-heading)
If you are using capture it does not automatically work and needs a little glue. I have posted the complete config here: https://gist.github.com/4037694
A more lightweight solution would be to add ! flag to the TODO state:
(setq org-todo-keywords '((sequence "TODO(!)" "DONE")))
Then:
* TODO get of your ass
- State "TODO" from [2016-06-03 to. 10:35]
It isn't very pretty though.
Ref: http://orgmode.org/org.html#Tracking-TODO-state-changes
You don't need to modify functions with 'defadvice' to run expiry code on capture.
You should use hook:
(add-hook 'org-capture-before-finalize-hook
(lambda()
(save-excursion
(org-back-to-heading)
(org-expiry-insert-created))))
Same for 'org-insert-todo-heading'. There is a hook:
(add-hook 'org-insert-todo-heading-hook
(lambda()
(save-excursion
(org-back-to-heading)
(org-expiry-insert-created))))
Org provides a hook org-after-todo-state-change-hook which you can use here:
org-after-todo-state-change-hook is a variable defined in ‘org.el’.
Documentation:
Hook which is run after the state of a TODO item was changed.
The new state (a string with a TODO keyword, or nil) is available in the
Lisp variable ‘org-state’.
Use it as follows:
(require 'org-expiry)
(add-hook 'org-after-todo-state-change-hook
(lambda ()
(when (string= org-state "TODO")
(save-excursion
(org-back-to-heading)
(org-expiry-insert-created)))))
org-expiry is part of org-contrib, which is included in the org-plus-contrib package on the org ELPA.
Here's a buried treasure:
(setq org-treat-insert-todo-heading-as-state-change t)
I found it here, in response to someone saying they wanted an org-insert-todo-heading-hook.
Just tried it out and, true to form, when you org-insert-todo-heading, it counts as a state change, so ex: #+TODO: TODO(t!) | ... will add a log.
If you create all your TODOs with org-capture the following capture template does the trick:
(setq org-capture-templates
'(
("t" "TODO Task" entry (file+headline "~/inbox.org" "Tasks")
"* TODO %?\nCREATED: %u\nSRC: %a\n%i\n")
))
The result will look something like this:
* Tasks
** TODO Dummy task
CREATED: [2015-05-08 Fri]
SRC: [[file:~/path/to/file/where/you/created/the/task.org::*heading"][heading]]
Here is a lightweight solution that does not require an external package. I got it from the answer by #MarcinAntczak, the comments by #Clément, and this similar thread. It works with org-capture and with M-S-RET. Put this in your Emacs initialization file (e.g. ~/.emacs):
(defun insert-created-date(&rest ignore)
(insert (format-time-string
(concat "\nCREATED: "
(cdr org-time-stamp-formats))
))
(org-back-to-heading) ; in org-capture, this folds the entry; when inserting a heading, this moves point back to the heading line
(move-end-of-line()) ; when inserting a heading, this moves point to the end of the line
)
; add to the org-capture hook
(add-hook 'org-capture-before-finalize-hook
#'insert-created-date
)
; hook it to adding headings with M-S-RET
; do not add this to org-insert-heading-hook, otherwise this also works in non-TODO items
; and Org-mode has no org-insert-todo-heading-hook
(advice-add 'org-insert-todo-heading :after #'insert-created-date)
I did not add this function to state changes (e.g., from plain heading to TODO) because it would need to be in a properties drawer and I prefer to not have those extra lines. If you prefer to have it in properties, use the function defined in see this thread.
You can add a time stamp at creation time with zero config, but it won't be labeled CREATED. Rather than manually typing TODO, use C-c C-t. It will then be logged as "state changed to TODO from """ and time stamped.

Remember-mode-hook not working in emacs

I am using GNU Emacs 23.1.1. I used M-x org-version to confirm that I have
Org-mode version 6.34c
When I execute M-x remember, the remember buffer opens but I get the message "Symbol's function value is void: nil". Therefore I think that the remember-mode-hook fails because when I enter text in the remember buffer and hit C-c C-c, the text doesn't get written to todo.org (specified in the template definition below).
Instead I get the message "Target files for notes must be in Org-mode if not filing to top/bottom". Please help. The relevant entries from my .emacs are below:
(require 'remember)
(require 'org-remember)
(org-remember-insinuate)
(setq org-directory "~/")
(define-key global-map "\C-cr" 'org-remember)
(setq remember-annotation-functions '(org-remember-annotation))
(setq remember-handler-functions '(org-remember-handler))
(add-hook 'remember-mode-hook 'org-remember-apply-template)
(setq org-remember-templates
'(("Todo" ?t "* TODO %?\n %i\n %a" "~/todo.org" "Tasks")))
Instead of fiddling with remember, it's probably better for you to upgrade org-mode. After org-mode v6.36 capturing is done by org-capture. Have a look at the info node '9.1 Capture' in the org-manual.