* writing ledger mode, date picker
@ 2015-11-07 20:30 jenia.ivlev
2015-11-07 21:18 ` jenia.ivlev
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: jenia.ivlev @ 2015-11-07 20:30 UTC (permalink / raw)
To: help-gnu-emacs
I want to insert a date in the current buffer.
I know the function `calendar-cursor-to-date` that when invoked
gives back the date at which the cursor is.
But how do I get that info back to my program?
Here is what I go so far:
(defun insert-new-entry ()
(interactive)
(save-excursion
(goto-char (point-max))
;;next comes mostly pseudocode except *calendar-cursor-to-date*
(calendar)
(let ((x (bind-key (kdb "enter) 'calendar-mode calendar-cursor-to-date)))
(insert x))))
So in english it's:
1. go to the last location in the buffer
2. open calendar
3. store in x whatever is returned where enter is pressed
4. insert it into buffer.
The problem is step 3. How do I store into x whatever is returned from
`calendar-cursor-to-date`?
Thanks
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-07 20:30 writing ledger mode, date picker jenia.ivlev
@ 2015-11-07 21:18 ` jenia.ivlev
2015-11-07 23:49 ` Emanuel Berg
2015-11-08 0:47 ` Drew Adams
2015-11-09 5:39 ` jenia.ivlev
2 siblings, 1 reply; 13+ messages in thread
From: jenia.ivlev @ 2015-11-07 21:18 UTC (permalink / raw)
To: help-gnu-emacs
I found an answer. I'm using `org-read-date`:
(let ((x (org-read-date)))
But I'm reading the function org-read-date and I can't understand how it
binds `calendar-cursor-to-date` to "enter" key press in calendar mode.
What I see is that `org-ans2` is set in `org-eval-in-calendar`
But what I don't understand is how do you bind `org-ans2`,
`calendar-cursor-to-date` to the "enter" key in calendar mode.
Does anyone here know?
Thanks
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-07 21:18 ` jenia.ivlev
@ 2015-11-07 23:49 ` Emanuel Berg
2015-11-08 19:25 ` jenia.ivlev
0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2015-11-07 23:49 UTC (permalink / raw)
To: help-gnu-emacs
jenia.ivlev@gmail.com (jenia.ivlev) writes:
> I found an answer. I'm using `org-read-date`:
>
> (let ((x (org-read-date)))
>
> But I'm reading the function org-read-date and
> I can't understand how it binds
> `calendar-cursor-to-date` to "enter" key press in
> calendar mode.
>
> What I see is that `org-ans2` is set in
> `org-eval-in-calendar`
>
> But what I don't understand is how do you bind
> `org-ans2`, `calendar-cursor-to-date` to the "enter"
> key in calendar mode.
I don't understand your question even 50%, but
assuming `calendar-cursor-to-date' is a command
(interactive function) then this should work:
(define-key calendar-mode-map "\r" #'calendar-cursor-to-date)
If it doesn't, try add this:
(defun calendar-cursor-to-date ()
(interactive)
(message "Calendar Crazy") )
Then it "works", or at least it shows you the
mechanics, so you can get it to work yourself when you
have defined your functions alright.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: writing ledger mode, date picker
2015-11-07 20:30 writing ledger mode, date picker jenia.ivlev
2015-11-07 21:18 ` jenia.ivlev
@ 2015-11-08 0:47 ` Drew Adams
2015-11-08 19:37 ` jenia.ivlev
2015-11-09 5:39 ` jenia.ivlev
2 siblings, 1 reply; 13+ messages in thread
From: Drew Adams @ 2015-11-08 0:47 UTC (permalink / raw)
To: jenia.ivlev, help-gnu-emacs
http://www.emacswiki.org/emacs/InsertingAndUpdatingDates
should help. This, in particular:
http://www.emacswiki.org/emacs/InsertAnyDate
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-07 23:49 ` Emanuel Berg
@ 2015-11-08 19:25 ` jenia.ivlev
2015-11-08 22:38 ` Emanuel Berg
2015-11-09 2:42 ` Yuri Khan
0 siblings, 2 replies; 13+ messages in thread
From: jenia.ivlev @ 2015-11-08 19:25 UTC (permalink / raw)
To: help-gnu-emacs
Hello Emanuel.
I'm trying to write a program that will prompt the user for a
date and insert it into a buffer called "asti" ;)
I'm having some success but also some problems. My problem, I think, is
that I'm using the `(when...)` function wrong.
Here is the newest iteration of the program:
(defun insert-new-entry ()
(interactive)
(save-excursion
(goto-char (point-max))
(print "Asti")
(define-key calendar-mode-map (kbd "RET") 'get-date)
(calendar)
(when (calendar-cursor-to-date)
(let ((x (calendar-cursor-to-date)))
(select-window (get-buffer-window "asti" t))
(insert format-time-string "%Y-%m-%d" x)))))
(defun get-date ()
(interactive)
calendar-cursor-to-date)
The interpreter tells me, though, that `x` is nil basically: "Symbol's
value as a variable is void: format-time-string"
How do I execute this part AFTER the user has pressed "RET".
(let ((x (calendar-cursor-to-date))) <---- execute after "RET" pressed
(select-window (get-buffer-window "asti" t))
(insert format-time-string "%Y-%m-%d" x)))))
Thanks
P.S. To try my prog out, you need to open a buffer called asti by
doing `C-b 4 asti`.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-08 0:47 ` Drew Adams
@ 2015-11-08 19:37 ` jenia.ivlev
0 siblings, 0 replies; 13+ messages in thread
From: jenia.ivlev @ 2015-11-08 19:37 UTC (permalink / raw)
To: help-gnu-emacs
Hi Drew.
I'm having a much more fundamental problem.
I can't execute stuff after an event has happened.
I wrote a function using the `(when (calendar-cursor-to-date) ...)` function that at first I
sincerely hoped will execute the `...` part when
`(calendar-cussor-to-date)` was finished executing but I wasn't so
lucky. Here is my program:
(defun insert-new-entry ()
(interactive)
(save-excursion
(goto-char (point-max))
(print "Asti")
(define-key calendar-mode-map (kbd "RET") 'get-date)
(calendar)
(when (calendar-cursor-to-date)
(let ((x (calendar-cursor-to-date)))
(select-window (get-buffer-window "asti" t))
(insert format-time-string "%Y-%m-%d" x)))))
(defun get-date ()
(interactive)
calendar-cursor-to-date)
How do I execute this part AFTER the user has pressed "RET":
(let ((x (calendar-cursor-to-date))) <------ execute after "RET"
(select-window (get-buffer-window "asti" t))
(insert format-time-string "%Y-%m-%d" x)))))
This is just such a totally basic thing to want to do that I'm in
despair for not being able to do it very simply.
Can you please help me in achieving this? Setting `x` to be
`(calendar-cursor-to-date)` when "RET" has been pressed?
Thanks
P.S. If you decide to test the prog out, please create the buffer "asti"
first by pressing "C-x 4 b asti"
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-08 19:25 ` jenia.ivlev
@ 2015-11-08 22:38 ` Emanuel Berg
2015-11-09 2:42 ` Yuri Khan
1 sibling, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2015-11-08 22:38 UTC (permalink / raw)
To: help-gnu-emacs
jenia.ivlev@gmail.com (jenia.ivlev) writes:
> Hello Emanuel.
>
> I'm trying to write a program that will prompt the
> user for a date and insert it into a buffer called
> "asti" ;)
(require 'calendar)
(defun prompt-date-into-asti ()
(interactive)
(let*((date (calendar-read-date))
(date-string (format-time-string "%Y-%m-%d\n" date))
(buffer (get-buffer-create "asti")) )
(with-current-buffer buffer
(insert date-string) )))
> I'm having some success but also some problems.
> My problem, I think, is that I'm using the
> `(when...)` function wrong.
`when' is equivalent to `if' with no need for `progn',
only `when' doesn't come with an "else" branch, as do
`if'. So this
(when something do)
is the same as
(if something do)
The rule of thumb is:
1. When you need an "else" branch, use `if' (or
`cond'), with `progn' if necessary to scope
the branches.
2. When you don't need an "else" branch, and the
condition is positive, i.e. (if something ... )
then use `when' and there is no need for
a progn (in the `if' sense at least).
3. When you don't need an "else" branch, and the
condition is *negative*, i.e. (if (not
something) ... ) then use `unless', otherwise
it is as with `when'.
> Here is the newest iteration of the program ...
> (define-key calendar-mode-map (kbd "RET") 'get-date)
That isn't anything you'd do in a defun. Do that when
you initialize calendar-mode if so. On the whole, you
code is too complicated, like you try to do to many
things at once. If you can describe what you want to
do, it'll be more easy to help.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-08 19:25 ` jenia.ivlev
2015-11-08 22:38 ` Emanuel Berg
@ 2015-11-09 2:42 ` Yuri Khan
2015-11-09 3:45 ` Emanuel Berg
1 sibling, 1 reply; 13+ messages in thread
From: Yuri Khan @ 2015-11-09 2:42 UTC (permalink / raw)
To: jenia.ivlev; +Cc: help-gnu-emacs@gnu.org
On Mon, Nov 9, 2015 at 1:25 AM, jenia.ivlev <jenia.ivlev@gmail.com> wrote:
> (insert format-time-string "%Y-%m-%d" x)))))
> […]
> The interpreter tells me, though, that `x` is nil basically: "Symbol's
> value as a variable is void: format-time-string"
No. It’s telling you, in its own way, that you forgot parentheses
around the format-time-string function call and are instead trying to
insert the value of a non-existant variable. The fix for that is:
> (insert (format-time-string "%Y-%m-%d" x))))))
To your other question, the “when” form has no relation to time or
circumstances. It does not mean “please remember to execute this form
when this condition becomes true, and let’s go on with the following
forms”. It means “every time you come to interpret this expression,
test this condition right then and there, and when it is true, execute
this form”.
You want to ask a user to select a date on the calendar and then
insert that date into a buffer. The steps to that are, in a typical
“asynchronous call with continuation” pattern:
1. Cause the next press of RET in a calendar-mode buffer to call a
function specified by you. (By adding a binding to calendar-mode-map,
for example.)
2. Open the calendar.
When (and if) your code you specified in step 1 gets called:
101. Figure out the currently selected date. (By calling
calendar-cursor-to-date.)
102. Activate the buffer you want to insert into.
103. Insert the date from step 101, formatted suitably.
104. Do any cleanup work you might need. (Bury or close the calendar
buffer, and/or restore the previous binding of RET in
calendar-mode-map, if any.)
Looking at your code, it seems to me that it can be achieved by moving
the whole (when …) form from your insert-new-entry into get-date to
replace the reference to the undefined calendar-cursor-to-date
variable and fixing the missing parentheses on the format-time-string
call.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-09 2:42 ` Yuri Khan
@ 2015-11-09 3:45 ` Emanuel Berg
2015-11-09 4:35 ` jenia.ivlev
0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2015-11-09 3:45 UTC (permalink / raw)
To: help-gnu-emacs
Yuri Khan <yuri.v.khan@gmail.com> writes:
> You want to ask a user to select a date on the
> calendar and then insert that date into a buffer.
Aha, that is what the OP wants to do!
Then what I provided should be modified somewhat - see
the code below.
The problem is, `calendar-cursor-to-date' only gives
me dates in February 1970... So what is that, Diane
Keaton and Mariel Hemingway?
Maybe the OP has better luck (?) with that!
(require 'calendar)
(defun calendar-cursor-date-into-asti ()
(interactive)
(let*((date (calendar-cursor-to-date))
(date-string (format-time-string "%Y-%m-%d\n" date))
(buffer (get-buffer-create "asti")) )
(with-current-buffer
buffer (insert date-string) )))
(define-key calendar-mode-map "\r" 'calendar-cursor-date-into-asti)
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-09 3:45 ` Emanuel Berg
@ 2015-11-09 4:35 ` jenia.ivlev
0 siblings, 0 replies; 13+ messages in thread
From: jenia.ivlev @ 2015-11-09 4:35 UTC (permalink / raw)
To: help-gnu-emacs
Emanuel Berg <embe8573@student.uu.se> writes:
> Yuri Khan <yuri.v.khan@gmail.com> writes:
>
>> You want to ask a user to select a date on the
>> calendar and then insert that date into a buffer.
>
> Aha, that is what the OP wants to do!
>
> Then what I provided should be modified somewhat - see
> the code below.
>
> The problem is, `calendar-cursor-to-date' only gives
> me dates in February 1970... So what is that, Diane
> Keaton and Mariel Hemingway?
>
> Maybe the OP has better luck (?) with that!
>
Yes I did. I'll copy paste the answer.
(defun insert-new-entry ()
(interactive)
(save-excursion
(goto-char (point-max))
(print "Asti")
(define-key calendar-mode-map (kbd "RET") 'get-date)
(calendar)
(let* ((x (calendar-cursor-to-date))
(time (encode-time 0 0 0 (nth 1 x) (nth 0 x) (nth 2 x))))
(select-window (get-buffer-window "asti" t))
(insert (format-time-string "%Y-%m-%d" time)))))
I didn't yet use the `calendar-mode-map`. Too tired. But I'm confident
that it'll work.
So now I have all the pieces ;))))
Thanks you so much Emanuel, Yuri and Drew!!
P.S. I got this from org-read-date ;)
> (require 'calendar)
>
> (defun calendar-cursor-date-into-asti ()
> (interactive)
> (let*((date (calendar-cursor-to-date))
> (date-string (format-time-string "%Y-%m-%d\n" date))
> (buffer (get-buffer-create "asti")) )
> (with-current-buffer
> buffer (insert date-string) )))
>
> (define-key calendar-mode-map "\r" 'calendar-cursor-date-into-asti)
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-07 20:30 writing ledger mode, date picker jenia.ivlev
2015-11-07 21:18 ` jenia.ivlev
2015-11-08 0:47 ` Drew Adams
@ 2015-11-09 5:39 ` jenia.ivlev
2015-11-10 1:15 ` Emanuel Berg
2015-11-10 1:22 ` Emanuel Berg
2 siblings, 2 replies; 13+ messages in thread
From: jenia.ivlev @ 2015-11-09 5:39 UTC (permalink / raw)
To: help-gnu-emacs
The program is amazing in it's simplicity.
Thanks very much everyone for your kind help.
(defun insert-new-entry ()
(interactive)
(goto-char (point-max))
(define-key calendar-mode-map "\r" 'get-date)
(calendar))
(defun get-date ()
(interactive)
(let* ((x (calendar-cursor-to-date))
(time (encode-time 0 0 0 (nth 1 x) (nth 0 x) (nth 2 x))))
(select-window (get-buffer-window "asti" t))
(insert (format-time-string "%Y-%m-%d" time))))
P.S. This assumes you have an "asti" buffer opened (C-x 4 b asti).
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-09 5:39 ` jenia.ivlev
@ 2015-11-10 1:15 ` Emanuel Berg
2015-11-10 1:22 ` Emanuel Berg
1 sibling, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2015-11-10 1:15 UTC (permalink / raw)
To: help-gnu-emacs
jenia.ivlev@gmail.com (jenia.ivlev) writes:
> (defun insert-new-entry ()
> (interactive)
> (goto-char (point-max))
> (define-key calendar-mode-map "\r" 'get-date)
> (calendar))
That function first moves point to the end of the
buffer, than changes the key-map for the calendar
mode, then brings up the calendar.
1. Why is that called "insert-new-entry"?
2. Why move point? That happens *before* `calendar' is
invoked. Remember your function can be called from
anywhere in Emacs, i.e. from all modes and all
buffers. Why move point in any and all of those?
3. If you want to change the key-map for a mode, don't
do that in a defun. Just put this
(require 'calendar)
(define-key calendar-mode-map "\r" #'get-date)
in an initiation file, i.e. most often ~/.emacs or
some other file that is `load'ed from ~/.emacs.
Also, at least according to my Lisp-vision, there
shouldn't be indentation after the (interactive) line.
That is, the `goto-char' line should be align on the
same vertical level as the `interactive' line.
> (defun get-date ()
> (interactive)
> (let* ((x (calendar-cursor-to-date))
> (time (encode-time 0 0 0 (nth 1 x) (nth 0 x) (nth 2 x))))
> (select-window (get-buffer-window "asti" t))
> (insert (format-time-string "%Y-%m-%d" time))))
Even with the `encode-time' I still only get 1970s
dates! But if time works for you, still, check out
a previous post and the function I provided.
Some hints:
1. Be more pedantic with indentation!
Use `indent-for-tab-command' (TAB) in the
`emacs-lisp-mode' and let it be done for you.
2. Think of better names than `x'.
3. Instead of (nth 0 x), you can use `car'.
> P.S. This assumes you have an "asti" buffer opened
> (C-x 4 b asti).
Use `get-buffer-create' which will "[r]eturn the
buffer specified ... creating a new one if needed.
..." (from the help). get-buffer-create is what I used
in the function in my previous post, and that will
work regardless so you don't have to assume anything.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: writing ledger mode, date picker
2015-11-09 5:39 ` jenia.ivlev
2015-11-10 1:15 ` Emanuel Berg
@ 2015-11-10 1:22 ` Emanuel Berg
1 sibling, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2015-11-10 1:22 UTC (permalink / raw)
To: help-gnu-emacs
jenia.ivlev@gmail.com (jenia.ivlev) writes:
> (defun insert-new-entry ()
> (interactive)
> (goto-char (point-max))
> (define-key calendar-mode-map "\r" 'get-date)
> (calendar))
That function first moves point to the end of the
buffer, than changes the key-map for the calendar
mode, then brings up the calendar.
1. Why is that called "insert-new-entry"?
2. Why move point? That happens *before* `calendar' is
invoked. Remember your function can be called from
anywhere in Emacs, i.e. from all modes and all
buffers. Why move point in any and all of those?
3. If you want to change the key-map for a mode, don't
do that in a defun. Just put this
(require 'calendar)
(define-key calendar-mode-map "\r" #'get-date)
in an initiation file, i.e. most often ~/.emacs or
some other file that is `load'ed from ~/.emacs.
Also, at least according to my Lisp-vision, there
shouldn't be indentation after the (interactive) line.
That is, the `goto-char' line should be align on the
same vertical level as the `interactive' line.
> (defun get-date ()
> (interactive)
> (let* ((x (calendar-cursor-to-date))
> (time (encode-time 0 0 0 (nth 1 x) (nth 0 x) (nth 2 x))))
> (select-window (get-buffer-window "asti" t))
> (insert (format-time-string "%Y-%m-%d" time))))
Even with the `encode-time' I still only get 1970s
dates! But if time works for you, still, check out
a previous post and the function I provided.
Some hints:
1. Be more pedantic with indentation!
Use `indent-for-tab-command' (TAB) in the
`emacs-lisp-mode' and let it be done for you.
2. Think of better names than `x'.
3. Instead of (nth 0 x), you can use `car'.
> P.S. This assumes you have an "asti" buffer opened
> (C-x 4 b asti).
Use `get-buffer-create' which will "[r]eturn the
buffer specified ... creating a new one if needed.
..." (from the help). get-buffer-create is what I used
in the function in my previous post, and that will
work regardless so you don't have to assume anything.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2015-11-10 1:22 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-07 20:30 writing ledger mode, date picker jenia.ivlev
2015-11-07 21:18 ` jenia.ivlev
2015-11-07 23:49 ` Emanuel Berg
2015-11-08 19:25 ` jenia.ivlev
2015-11-08 22:38 ` Emanuel Berg
2015-11-09 2:42 ` Yuri Khan
2015-11-09 3:45 ` Emanuel Berg
2015-11-09 4:35 ` jenia.ivlev
2015-11-08 0:47 ` Drew Adams
2015-11-08 19:37 ` jenia.ivlev
2015-11-09 5:39 ` jenia.ivlev
2015-11-10 1:15 ` Emanuel Berg
2015-11-10 1:22 ` Emanuel Berg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).