How can I mark Org habits as done in the past - org-mode

I regularly mark habits as DONE the day after the activity was done. The habits module automatically updates a bunch of dates in the metadata when I do this, but the date is always today. So then, I have to manually edit the SCHEDULED, LOGBOOK, and LAST_REPEAT states.
Is there a way to mark a habit as DONE for a day in the past? So instead of doing C-c C-t d (I have "d" set up as "DONE") I could get a prompt which asked me for a date.

I've wanted to do this for some time, too, and your question inspired me to finally figure it out.
There is a function called "org-todo-yesterday." By default, it's not mapped to any keys, but you could always call it with M-x org-todo-yesterday (or map it if you're using it a lot). For me, it breaks because it calls "third" which isn't a defined function in my install.
For a more generic function that prompts us for the date and marks things as done at that time, we can add this function (inspired by org-todo-yesterday) to our emacs init file. It will act as if you finished things at 23:59 on the target date, which is hopefully good enough.
(defun dk/org-todo-custom-date (&optional arg)
"Like org-todo-yesterday, but prompt the user for a date. The time
of change will be 23:59 on that day"
(interactive "P")
(let* ((hour (nth 2 (decode-time
(org-current-time))))
(daysback (- (date-to-day (current-time-string)) (org-time-string-to-absolute (org-read-date))))
(org-extend-today-until (+ 1 (* 24 (- daysback 1)) hour))
(org-use-effective-time t)) ; use the adjusted timestamp for logging
(if (eq major-mode 'org-agenda-mode)
(org-agenda-todo arg)
(org-todo arg))))

Will this work? This simply completes the habit and reschedule it for today
(defun org-todo-yesterday2 (&optional arg)
(interactive "P")
(org-todo-yesterday arg)
(org-schedule arg (format-time-string "%Y-%m-%d")
)
)

Related

how to jump (c) from org-calendar to org-agenda showing whole day

When I am in calendar and want to jump to the entry in org-agenda, I press 'c'.
Would it be possible to have the DAY view as the standard so when I jump I only see one day, and not the whole week which is more cluttered? Thanks!
You can do it in various ways. In the spirit of "teaching someone to fish", the first thing that you have to do is figure out what c in the calendar does. So open the calendar, place the cursor on a date and say C-h c c which will tell you that c in the calendar runs the command
org-calendar-goto-agenda. So the next step is to do C-h f org-calendar-goto-agenda and read its documentation. Then click on the link in that documentation to go to the code and read it.
So here are three alternatives:
The code of the function is pretty simple:
(defun org-calendar-goto-agenda ()
"Compute the Org agenda for the calendar date displayed at the cursor.
This is a command t (org-agenda-span 1))hat has to be installed in `calendar-mode-map'."
(interactive)
;; Temporarily disable sticky agenda since user clearly wants to
;; refresh view anyway.
(let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
(org-agenda-sticky nil))
(org-agenda-list nil (calendar-absolute-from-gregorian
(calendar-cursor-to-date))
nil)))
You can modify the function to make the agenda view to be just one day, by setting the variable org-agenda-span to 1 locally:
(defun org-calendar-goto-agenda ()
"Compute the Org agenda for the calendar date displayed at the cursor.
This is a command t (org-agenda-span 1))hat has to be installed in `calendar-mode-map'."
(interactive)
;; Temporarily disable sticky agenda since user clearly wants to
;; refresh view anyway.
(let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
(org-agenda-sticky nil)
(org-agenda-span 1))
(org-agenda-list nil (calendar-absolute-from-gregorian
(calendar-cursor-to-date))
nil)))
The trouble with this is that you have now forked the Org mode code and you will need to make this change to the code every time you update. That might be appropriate in some cases, but you should avoid it if at all possible.
You can define your own command and bind it to c in the calendar:
(defun my-org-calendar-goto-agenda ()
(interactive)
(let ((org-agenda-span 1))
(org-calendar-goto-agenda)))
(define-key calendar-mode-map (kbd "c") #'my-org-calendar-goto-agenda)
That works pretty well. You have to remember to undo the keymap modification to restore the original state if you want to try the third method below:
(define-key calendar-mode-map (kbd "c") #'org-calendar-goto-agenda)
You can add an advice to the org-calendar-goto-agenda command. This is a formalization of the second option above: it is slightly more "advanced" but it is a powerful way of dealing with questions of this sort: you are given a function that does almost what you want and you want to customize it so that it does exactly what you want. This method cannot always be used, but it can be used in this case and it provides a simple example:
(defun org-calendar-goto-agenda-around (orig-fun &rest args)
(let ((org-agenda-span 1))
(apply orig-fun args)))
(advice-add 'org-calendar-goto-agenda :around #'org-calendar-goto-agenda-around)
The idea is that you define an around function that does something around the call of the original function (in this case, it let-binds org-agenda-span to 1, just like the other methods and then calls the original function in this new let-bound environment). Then we modify the original function by installing the advice. If you do C-h f org-calendar-goto-agenda now, you will see that there is some added information:
This function has :around advice: ‘org-calendar-goto-agenda-around’.
This is probably the cleanest way to do what you want. You can also easily remove the advice with
(advice-remove 'org-calendar-goto-agenda #'org-calendar-goto-agenda-around)
so this method allows you to experiment fairly easily. This is my recommended solution.

Bulk reschedule org agenda items preserving date

I sometimes forget to do one or more of my daily habits that happen at the same time each day and need to bring them current so I can get orgzly notifications of them.
I am able to shift a single item in org agenda preserving the date with M-x org-agenda-date-later and it reschedules that to the current date with whatever time existed.
If I try to use it as a bulk action by marking an item and doing B f org-agenda-date-later I get:
Debugger entered--Lisp error: (wrong-number-of-arguments #f(compiled-function (arg &optional what) (interactive "p") #<bytecode 0x1ad5eed>) 0)
org-agenda-date-later()
org-agenda-bulk-action(nil)
funcall-interactively(org-agenda-bulk-action nil)
call-interactively(org-agenda-bulk-action nil nil)
command-execute(org-agenda-bulk-action)
This tells me that org-agenda-date-later wasn't written to be used as a bulk action. One solution for this could be writing a bulk action that calls org-agenda-date-later multiple times but I do not know how to do that.
I searched for other solutions and I found something recommended by the author of org mode from the org mode FAQ:
(defun org-agenda-reschedule-to-today ()
(interactive)
(flet ((org-read-date (&rest rest) (current-time)))
(call-interactively 'org-agenda-schedule)))
This works as a bulk action but it loses the time the item was scheduled. That means I would have to go through my habits and reschedule the time for each one after doing this which is inconvenient.
There is the org-agenda-bulk-custom-functions to which you can add a key-binding and an associated function. So, for example, adding a binding for D after calling org-agenda-bulk-action, you could use
(setq org-agenda-bulk-custom-functions
`((?D (lambda () (call-interactively 'org-agenda-date-later)))
,#org-agenda-bulk-custom-functions))
It will pass the prefix on, allowing C-u -1 B D to reshedule the tasks a day earlier at the same time, for example.

Custom diary-sunrise function not working. `autoload-diary`? (Emacs.)

Intro:
Based on the existing code for Emacs' diary-sunrise-sunset, I attempted to create two new functions diary-sunrise and diary-sunset.
My reasons for this are included below under the heading "XY-description".
I have code below which seems to work, except when I restart with a new Emacs. I can fix this by momentarily using the original built-in diary-sunrise-sunset. From then on, my functions perform beautifully.
In other words, I have to use the built-in %%(diary-sunrise-sunset) just a one time before my %%(diary-sunrise) and %%(diary-sunset) will work.
Question:
Can you help me fix my use of these functions so that I do not have to take the awkward step of first getting the built-in function called?
The lines of code that seem suspicious to me are the ones that go
;;;###diary-autoload
While I have some idea of the necessity of loading programs, I am not sure what's going on here, or if this is where the issue lies. (I've never seen that particular syntax.)
I have tried M-: (require 'solar) and M-: (require 'diary), but neither have worked (and just now calendar). I have tried putting my code both in my .emacs and in the built-in .../lisp/calendar/solar.el (and byte-recompiling), but neither have worked.
My functions:
(They are each slight modifications of solar-sunrise-sunset-string and diary-sunrise-sunset, which are both defined in .../lisp/calendar/solar.el).
Sunrise:
(defun solar-sunrise-string (date &optional nolocation)
"String of *local* time of sunrise and daylight on Gregorian DATE."
(let ((l (solar-sunrise-sunset date)))
(format
"%s (%s hours daylight)"
(if (car l)
(concat "Sunset " (apply 'solar-time-string (car l)))
"no sunset")
(nth 2 l)
)))
;; To be called from diary-list-sexp-entries, where DATE is bound.
;;;###diary-autoload
(defun diary-sunrise ()
"Local time of sunrise as a diary entry.
Accurate to a few seconds."
(or (and calendar-latitude calendar-longitude calendar-time-zone)
(solar-setup))
(solar-sunrise-string date))
Sunset:
(defun solar-sunset-string (date &optional nolocation)
"String of *local* time of sunset and daylight on Gregorian DATE."
(let ((l (solar-sunrise-sunset date)))
(format
"%s (%s hours daylight)"
(if (cadr l)
(concat "Sunset " (apply 'solar-time-string (cadr l)))
"no sunset")
(nth 2 l)
)))
;; To be called from diary-list-sexp-entries, where DATE is bound.
;;;###diary-autoload
(defun diary-sunset ()
"Local time of sunset as a diary entry.
Accurate to a few seconds."
(or (and calendar-latitude calendar-longitude calendar-time-zone)
(solar-setup))
(solar-sunset-string date))
XY description:
I am using Emacs' Org-mode, and just starting to use agenda views. I like the builtin diary-sunrise-sunset function, but wanted to make some minor tweaks to make it more to my liking.
Basically, Org-mode's agenda view will extract the first time it sees from the diary sexp %%(diary-sunrise-sunset), for instance
Sat, Apr 5, 2014
Sunrise 6:43am (PDT), sunset 7:42pm (PDT) at Springfield, OH (12:59 hours daylight)
and thus make an entry of
6:43am........ Sunrise (PDT), sunset 7:42pm (PDT) at Springfield, OH (12:59 hours daylight)
in the agenda view.
What I would like it to do is something more like,
6:43am........ Sunrise (PDT) (12:59 hours daylight)
8:00am........ ----------------
10:00am........ ----------------
12:00pm........ ----------------
2:00pm........ ----------------
4:00pm........ ----------------
5:51pm........ now - - - - - - - - - - - - - - - - - - - - - - - - -
6:00pm........ ----------------
7:42pm........ Sunset (PDT) (12:59 hours daylight)
Where the data is split into the two times, rather than all written only at the sunrise time.
Bonus:
a snippet so that C-c a d will give you a nice day agenda view:
(setq org-agenda-custom-commands
'(("d" "day's agenda"
agenda ""
(
(org-agenda-files '("/e/org/agendatest.org"))
(org-agenda-prefix-format "%t %s")
(org-agenda-span 'day)
(org-agenda-timegrid-use-ampm t)
)
)
))
In the last paragraph of my question (eighth paragraph?), I lied, (require 'solar) was the answer... I did not try it hard enough as it didn't respond to auto-completion.
The scenic route to the answer took me to these distractions:
Maintaining Emacs autoload files for user-installed elisp?
elisp - Where should I add autoload cookies in my Emacs Lisp package? Is there a definitive guide?
C-h i g (elisp) Autoload
a search through the lisp directory for any update-file-autoloads
I did some Edebugging wondering if anything I was using had an autoload cookie (not completely understanding it yet), but then noticed there was a (provide 'solar) at the end of solar.el, so then I figured (require 'solar) had to work. It did and that was that.
From the code in my question, I remove the autoload cookies (not sure if that's necessary), and add (require 'solar), and it works! Enjoy!

Leave region selected after emacs operation

My question builds on this answer by Trey Jackson to this question from SyntaxT3rr0r.
Trey proposes the following function for incrementing each two-digit number in the selected region of an Emacs buffer.
(defun add-1-to-2-digits (b e)
"add 1 to every 2 digit number in the region"
(interactive "r")
(goto-char b)
(while (re-search-forward "\\b[0-9][0-9]\\b" e t)
(replace-match (number-to-string (+ 1 (string-to-int (match-string 0)))))))
I would like to use this function for my own purposes. However, I would like to increment the numbers many times successively. The problem with the function, in its current form, is that before each invocation, I have to select the region again with my mouse.
My question is: How can Trey's function be modified so that it leaves the region selected after invocation? (My ultimate aim is to assign this function to a keyboard shortcut (something like ctrl+↑) so that I if I keep the ctrl and ↑ keys held down, all the two-digit numbers in my selected region will continuously increase.)
By the way, I am aware of this answer by Brian Campbell, which suggests using exchange-point-and-mark to re-select a previously-selected region. However, I tried that, and it doesn't seem to help in this instance.
Here's your function modified to use let (deactivate-mark) wrapped inside save-excursion as suggested in the answer user event_jr linked to:
https://stackoverflow.com/a/11080667/903943
(defun add-1-to-2-digits (b e)
"add 1 to every 2 digit number in the region"
(interactive "r")
(save-excursion
(let (deactivate-mark)
(goto-char b)
(while (re-search-forward "\\b[0-9][0-9]\\b" e t)
(replace-match (number-to-string (+ 1 (string-to-int (match-string 0)))))))))
You need to bind deactivate-mark to prevent it from being set see:
https://stackoverflow.com/a/11080667/903943
manual: http://www.gnu.org/software/emacs/manual/html_node/elisp/The-Mark.html#index-deactivate_002dmark-2801

Copy Character Down in Emacs

I wrote an interactive function which inserts the "character above the point" in to the current line. For instance, given a line containing "12345" followed by a line "abcdef" and the point sitting at the letter "c", copy-down would make the second line become "ab3cdef". copy-down again would make the second line become "ab34cdef".
My function fails (using GNU Emacs 23.3.1 under windows 7) the second time I invoke it by inserting the text from the first invocation and not advancing properly. If I put any emacs "manipulations" in-between invocations, it works fine. (For instance if I do a copy-down, "left arrow", "right arrow", copy-down it works fine for both invocations.)
Here's my function:
(defun copy-down ()
"Grab the character in the line above and insert at the current location."
(interactive)
(let ((beg (progn (previous-line 1) (point)))
(end (progn (forward-char) (point))))
(backward-char)
(next-line 1)
(insert-buffer-substring (current-buffer) beg end)))
If it matters, I usually tie my function to a key: (global-set-key [f5] 'copy-down)
PS. I got used to using this capability in the editor I used before switching to emacs many years ago and I miss it in GNU Emacs. :-(
What you have works just fine for me. That said, previous-line has interaction with other settings (specifically goal-column) and generally shouldn't be used when writing elisp. Instead you should use (forward-line -1). But, of course, your code relies on the goal-column... You can test this by running Emacs without your other configurations, ala emacs -q.
Here's a slightly different version of your code that doesn't rely on goal-column:
(defun copy-down ()
"Grab the character in the line above and insert at the current location."
(interactive)
(let* ((col (current-column))
(to-insert (save-excursion
(forward-line -1)
(move-to-column col)
(buffer-substring-no-properties (point) (1+ (point))))))
(insert to-insert)))
If the problem isn't with using previous-line, then I don't imagine my code would make much of a difference.
Another option you have is to try running it in the debugger to see where your code breaks down. Move the point inside the defun for copy-down and type M-x edebug-defun, and the next time you run it you'll be able to step through the code. Docs for edebug can be found here.
You need to use let* instead of let. The former allows you to use earlier values in later forms in the same statement.
BTW, that's an unconventional way to write elisp, you might want to look at some other code samples.
EDIT:
Hey, someone completely rearranged your function! It might work now.
Try
(defun copy-down (arg)
(interactive "p")
(let ((p (+ (current-column) (point-at-bol 0))))
(insert-buffer-substring (current-buffer) p (+ p arg))))
which has the additional functionality of taking a prefix argument to copy n (default to 1) characters down.