* Elisp Question...
@ 2009-09-27 14:32 magicus
2009-09-27 15:26 ` Giorgos Keramidas
0 siblings, 1 reply; 33+ messages in thread
From: magicus @ 2009-09-27 14:32 UTC (permalink / raw)
To: help-gnu-emacs
Hi,
I posted this to comp.emacs when this might be a more useful newgroup.
I am new to using elisp and I put together the following due to ignorance
about how to do it in one function:
==================================================================== (fset
'flp-cr
"Copyright © 2009 Furlan Lawrence Primus. All rights reserved.")
(global-set-key (kbd "<f9>") 'flp-cr)
(fset 'flp-copyright
[f9 home ?\C- ?\C-e ?\M-w])
(global-set-key (kbd "<f8>") 'flp-copyright)
====================================================================
The main point here is that I wanted to be able to have it such that when
I press F8 it displays the text AND copys it so that I can then use it
outside of Emacs. The above seems to work and I'd like to make it a lot
more compact. In addition, while it works in version 22.2.1 it stops after
printing "Copyright" in version GNU Emacs 23.1.50.24 (i686-pc-linux- gnu,
GTK+ Version 2.16.1) of 2009-09-27 on magicbox which I compile earlier
from CVS.
The error message post was:
After 0 kbd macro iterations: up-list: Scan error: "Unbalanced
parentheses", 202, 202
TIA for any comments and suggestions.
ciao,
f
--
We are here to laugh at the odds and live our lives so well that Death
will tremble to take us.
-- Charles Bukowski
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: Elisp Question...
2009-09-27 14:32 Elisp Question magicus
@ 2009-09-27 15:26 ` Giorgos Keramidas
2009-09-27 17:10 ` magicus
0 siblings, 1 reply; 33+ messages in thread
From: Giorgos Keramidas @ 2009-09-27 15:26 UTC (permalink / raw)
To: help-gnu-emacs
On Sun, 27 Sep 2009 14:32:22 +0000 (UTC), magicus <REMOVEmagicus23THIS@gmail.com> wrote:
> Hi,
>
> I posted this to comp.emacs when this might be a more useful newgroup.
>
> I am new to using elisp and I put together the following due to ignorance
> about how to do it in one function:
>
> ==================================================================== (fset
> 'flp-cr
> "Copyright © 2009 Furlan Lawrence Primus. All rights reserved.")
>
> (global-set-key (kbd "<f9>") 'flp-cr)
>
>
> (fset 'flp-copyright
> [f9 home ?\C- ?\C-e ?\M-w])
>
> (global-set-key (kbd "<f8>") 'flp-copyright)
> ====================================================================
>
> The main point here is that I wanted to be able to have it such that
> when I press F8 it displays the text AND copys it so that I can then
> use it outside of Emacs. The above seems to work and I'd like to make
> it a lot more compact. In addition, while it works in version 22.2.1
> it stops after printing "Copyright" in version GNU Emacs 23.1.50.24
> (i686-pc-linux- gnu, GTK+ Version 2.16.1) of 2009-09-27 on magicbox
> which I compile earlier from CVS.
With a small bit of Emacs Lisp you can do something like this:
(defun flp-insert-copyright (copy-text)
(let ((start (point)))
(insert "Copyright © 2009 Furlan Lawrence Primus. All rights reserved.")
(when copy-text
(copy-region-as-kill start (point)))))
(global-set-key (kbd "<f9>") (lambda ()
(interactive)
(flp-insert-copyright nil)))
(global-set-key (kbd "<f8>") (lambda ()
(interactive)
(flp-insert-copyright t)))
This should work fine, AFAICT. The same basic function can do both of
the things you described. Wrapping it in a `lambda' form that calls the
basic function with different arguments depending on the key you typed
is relatively easy (but it does require a bit of Emacs Lisp code, as you
can see the `global-set-key' calls I wrote).
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: Elisp Question...
2009-09-27 15:26 ` Giorgos Keramidas
@ 2009-09-27 17:10 ` magicus
0 siblings, 0 replies; 33+ messages in thread
From: magicus @ 2009-09-27 17:10 UTC (permalink / raw)
To: help-gnu-emacs
On Sun, 27 Sep 2009 18:26:17 +0300, Giorgos Keramidas
<keramida@ceid.upatras.gr> wrote:
> On Sun, 27 Sep 2009 14:32:22 +0000 (UTC), magicus
> <REMOVEmagicus23THIS@gmail.com> wrote:
>> Hi,
>>
>> I posted this to comp.emacs when this might be a more useful newgroup.
>>
>> I am new to using elisp and I put together the following due to
>> ignorance about how to do it in one function:
>>
>> ====================================================================
>> (fset 'flp-cr
>> "Copyright © 2009 Furlan Lawrence Primus. All rights reserved.")
>>
>> (global-set-key (kbd "<f9>") 'flp-cr)
>>
>>
>> (fset 'flp-copyright
>> [f9 home ?\C- ?\C-e ?\M-w])
>>
>> (global-set-key (kbd "<f8>") 'flp-copyright)
>> ====================================================================
>>
>> The main point here is that I wanted to be able to have it such that
>> when I press F8 it displays the text AND copys it so that I can then
>> use it outside of Emacs. The above seems to work and I'd like to make
>> it a lot more compact. In addition, while it works in version 22.2.1 it
>> stops after printing "Copyright" in version GNU Emacs 23.1.50.24
>> (i686-pc-linux- gnu, GTK+ Version 2.16.1) of 2009-09-27 on magicbox
>> which I compile earlier from CVS.
>
> With a small bit of Emacs Lisp you can do something like this:
>
> (defun flp-insert-copyright (copy-text)
> (let ((start (point)))
> (insert "Copyright © 2009 Furlan Lawrence Primus. All rights
> reserved.") (when copy-text
> (copy-region-as-kill start (point)))))
>
> (global-set-key (kbd "<f9>") (lambda ()
> (interactive)
> (flp-insert-copyright nil)))
>
> (global-set-key (kbd "<f8>") (lambda ()
> (interactive)
> (flp-insert-copyright t)))
>
> This should work fine, AFAICT. The same basic function can do both of
> the things you described. Wrapping it in a `lambda' form that calls the
> basic function with different arguments depending on the key you typed
> is relatively easy (but it does require a bit of Emacs Lisp code, as you
> can see the `global-set-key' calls I wrote).
Thank you. I was only using the F9 so I could get it to work. I 'assume'
that I can safely use either function key to do what I want so I can
remove one w/o any problems.
I'll dump it into my .emacs.
ciao,
f
--
The most wasted of all days is one without laughter.
-- e.e. cummings
^ permalink raw reply [flat|nested] 33+ messages in thread
[parent not found: <mailman.941.1223918781.25473.help-gnu-emacs@gnu.org>]
* Re: elisp question
[not found] <mailman.941.1223918781.25473.help-gnu-emacs@gnu.org>
@ 2008-10-13 18:11 ` Niels Giesen
2008-10-13 19:09 ` Seweryn Kokot
[not found] ` <mailman.948.1223924333.25473.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 33+ messages in thread
From: Niels Giesen @ 2008-10-13 18:11 UTC (permalink / raw)
To: help-gnu-emacs
Seweryn Kokot <sewkokot@gmail.com> writes:
> Hello,
>
> I would like to get a list of functions for keybindings from C-x C-a to
> C-x C-z etc.
>
> Since (key-binding (kbd "C-x C-f")) gives find-file
>
> I try to write something like this:
>
> (defun my-list-keybindings ()
> (interactive)
> (let* (keyb
> (char-a 97)
> (char-z 122)
> (n (- char-z char-a)))
> (dotimes (i n)
> (setq keyb (concat "C-x C-" (char-to-string (+ char-a i))))
> (key-binding (kbd keyb)))))
>
> but unfortunately it doesn't work because kbd doesn't accept variable
> string. How to modify this function to get it work?
Dig harder:
(defmacro kbd (keys)
"Convert KEYS to the internal Emacs key representation.
KEYS should be a string constant in the format used for
saving keyboard macros (see `edmacro-mode')."
(read-kbd-macro keys))
^^^^^^^^^^^^^^
;)
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2008-10-13 18:11 ` elisp question Niels Giesen
@ 2008-10-13 19:09 ` Seweryn Kokot
[not found] ` <mailman.948.1223924333.25473.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 33+ messages in thread
From: Seweryn Kokot @ 2008-10-13 19:09 UTC (permalink / raw)
To: help-gnu-emacs
Niels Giesen <niels.giesen@gmail.com> writes:
> Dig harder:
>
> (defmacro kbd (keys)
> "Convert KEYS to the internal Emacs key representation.
> KEYS should be a string constant in the format used for
> saving keyboard macros (see `edmacro-mode')."
> (read-kbd-macro keys))
> ^^^^^^^^^^^^^^
>
> ;)
Thanks for the hint. This is what I wanted.
Now the following function
(defun my-list-keybindings ()
(interactive)
(let* (keyb
(char-a 97)
(char-z 122)
(n (- char-z char-a)))
(dotimes (i n)
(setq keyb (concat "C-x C-" (char-to-string (+ char-a i))))
(insert keyb)
(insert (format " %s\n" (key-binding (read-kbd-macro keyb))))
)))
gives
C-x C-a nil
C-x C-b bs-show
C-x C-c save-buffers-kill-emacs
C-x C-d ffap-list-directory
C-x C-e eval-last-sexp
C-x C-f ffap
C-x C-g nil
C-x C-h nil
C-x C-i dabbrev-expand
C-x C-j dired-jump
... etc.
--
regards,
Seweryn
^ permalink raw reply [flat|nested] 33+ messages in thread
[parent not found: <mailman.948.1223924333.25473.help-gnu-emacs@gnu.org>]
* Re: elisp question
[not found] ` <mailman.948.1223924333.25473.help-gnu-emacs@gnu.org>
@ 2008-10-13 19:40 ` Pascal J. Bourguignon
2008-10-13 20:47 ` Drew Adams
2008-10-13 21:11 ` Seweryn Kokot
0 siblings, 2 replies; 33+ messages in thread
From: Pascal J. Bourguignon @ 2008-10-13 19:40 UTC (permalink / raw)
To: help-gnu-emacs
Seweryn Kokot <sewkokot@gmail.com> writes:
> Thanks for the hint. This is what I wanted.
>
> Now the following function
> [...]
> (defun my-list-keybindings ()
> gives
>
> C-x C-a nil
> C-x C-b bs-show
> [...]
Notice that C-h m also gives a list of keybinding in effect for the
current mode...
Otherwise, in http://darcs.informatimago.com/darcs/emacs/pjb-emacs.el
there's a command named all-bindings that will show all the key
bindings, including A-, S-, s-, M-, H- and combinations.
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
^ permalink raw reply [flat|nested] 33+ messages in thread
* RE: elisp question
2008-10-13 19:40 ` Pascal J. Bourguignon
@ 2008-10-13 20:47 ` Drew Adams
2008-10-13 21:11 ` Seweryn Kokot
1 sibling, 0 replies; 33+ messages in thread
From: Drew Adams @ 2008-10-13 20:47 UTC (permalink / raw)
To: 'Pascal J. Bourguignon', help-gnu-emacs
> Otherwise, in http://darcs.informatimago.com/darcs/emacs/pjb-emacs.el
> there's a command named all-bindings that will show all the key
> bindings, including A-, S-, s-, M-, H- and combinations.
FWIW, `C-h b' is also helpful, if you don't care about having that level of
detail.
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2008-10-13 19:40 ` Pascal J. Bourguignon
2008-10-13 20:47 ` Drew Adams
@ 2008-10-13 21:11 ` Seweryn Kokot
1 sibling, 0 replies; 33+ messages in thread
From: Seweryn Kokot @ 2008-10-13 21:11 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> Seweryn Kokot <sewkokot@gmail.com> writes:
>> Thanks for the hint. This is what I wanted.
>>
>> Now the following function
>> [...]
>> (defun my-list-keybindings ()
>> gives
>>
>> C-x C-a nil
>> C-x C-b bs-show
>> [...]
>
> Notice that C-h m also gives a list of keybinding in effect for the
> current mode...
>
>
> Otherwise, in http://darcs.informatimago.com/darcs/emacs/pjb-emacs.el
> there's a command named all-bindings that will show all the key
> bindings, including A-, S-, s-, M-, H- and combinations.
This was just an exercise to learn elisp. Anyway thanks for the .el
file!
--
regards,
Seweryn
^ permalink raw reply [flat|nested] 33+ messages in thread
* elisp question
@ 2008-10-13 17:36 Seweryn Kokot
0 siblings, 0 replies; 33+ messages in thread
From: Seweryn Kokot @ 2008-10-13 17:36 UTC (permalink / raw)
To: help-gnu-emacs
Hello,
I would like to get a list of functions for keybindings from C-x C-a to
C-x C-z etc.
Since (key-binding (kbd "C-x C-f")) gives find-file
I try to write something like this:
(defun my-list-keybindings ()
(interactive)
(let* (keyb
(char-a 97)
(char-z 122)
(n (- char-z char-a)))
(dotimes (i n)
(setq keyb (concat "C-x C-" (char-to-string (+ char-a i))))
(key-binding (kbd keyb)))))
but unfortunately it doesn't work because kbd doesn't accept variable
string. How to modify this function to get it work?
Thanks in advance.
--
regards,
Seweryn
^ permalink raw reply [flat|nested] 33+ messages in thread
* elisp question
@ 2008-04-30 21:23 harven
2008-05-01 13:08 ` Kevin Rodgers
` (2 more replies)
0 siblings, 3 replies; 33+ messages in thread
From: harven @ 2008-04-30 21:23 UTC (permalink / raw)
To: help-gnu-emacs
I am trying to device a short command to
interactively resize the current window:
(defun resize-window (key)
"resize interactively the window"
(interactive "c- widen, _ shrink")
(cond
((eq key (string-to-char "-"))
(enlarge-window 1)
(call-interactively 'resize-window))
((eq key (string-to-char "_"))
(enlarge-window -1)
(call-interactively 'resize-window))
(t (insert key))))
I type -/_ a number of times, the window enlarges/shrinks, and if I
type another character, the window resizing stops and the character is
inserted.
However, I would like the following effect.
Any entry other than -/_ should end the resizing and be executed. How
can I achieve such effect ?
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2008-04-30 21:23 harven
@ 2008-05-01 13:08 ` Kevin Rodgers
[not found] ` <mailman.11020.1209647315.18990.help-gnu-emacs@gnu.org>
2008-05-03 15:38 ` Bastien
2 siblings, 0 replies; 33+ messages in thread
From: Kevin Rodgers @ 2008-05-01 13:08 UTC (permalink / raw)
To: help-gnu-emacs
harven wrote:
> I am trying to device a short command to
> interactively resize the current window:
>
> (defun resize-window (key)
> "resize interactively the window"
> (interactive "c- widen, _ shrink")
> (cond
> ((eq key (string-to-char "-"))
> (enlarge-window 1)
> (call-interactively 'resize-window))
> ((eq key (string-to-char "_"))
> (enlarge-window -1)
> (call-interactively 'resize-window))
> (t (insert key))))
>
> I type -/_ a number of times, the window enlarges/shrinks, and if I
> type another character, the window resizing stops and the character is
> inserted.
>
> However, I would like the following effect.
> Any entry other than -/_ should end the resizing and be executed. How
> can I achieve such effect ?
See the unread-command-events variable. Here's an iterative version to
compare to your recursive implementation:
(defvar enlarge-window-char ?_) ; or ?+
(defvar shrink-window-char ?-)
(defun resize-window (&optional arg)
"Interactively resize the selected window.
Repeatedly prompt whether to enlarge or shrink the window until the
response is neither `enlarge-window-char' or `shrink-window-char'.
When called with a prefix arg, resize the window by ARG lines."
(interactive "p")
(let ((prompt (format "Enlarge/Shrink window (%c/%c)? "
enlarge-window-char shrink-window-char))
response)
(while (progn
(setq response (read-event prompt))
(cond ((equal response enlarge-window-char)
(enlarge-window arg)
t)
((equal response shrink-window-char)
(enlarge-window (- arg))
t)
(t nil)))
;; no loop body needed
)
(push response unread-command-events)))
--
Kevin Rodgers
Denver, Colorado, USA
^ permalink raw reply [flat|nested] 33+ messages in thread
[parent not found: <mailman.11020.1209647315.18990.help-gnu-emacs@gnu.org>]
* Re: elisp question
2008-04-30 21:23 harven
2008-05-01 13:08 ` Kevin Rodgers
[not found] ` <mailman.11020.1209647315.18990.help-gnu-emacs@gnu.org>
@ 2008-05-03 15:38 ` Bastien
2 siblings, 0 replies; 33+ messages in thread
From: Bastien @ 2008-05-03 15:38 UTC (permalink / raw)
To: help-gnu-emacs; +Cc: harven
harven <harven@free.fr> writes:
> I am trying to device a short command to
> interactively resize the current window:
You might play with windresize.el:
http://lumiere.ens.fr/~guerry/u/windresize.el
M-x windresize RET
--
Bastien
^ permalink raw reply [flat|nested] 33+ messages in thread
* elisp question
@ 2005-11-30 23:13 Tim McNamara
2005-11-30 23:22 ` Lennart Borgman
` (3 more replies)
0 siblings, 4 replies; 33+ messages in thread
From: Tim McNamara @ 2005-11-30 23:13 UTC (permalink / raw)
I am looking to write a small program for Emacs which will create a
new buffer and read a randomly selected text file into it after
launching Emacs- rather like yow or fortune, except the files would be
Dharma snippets. I have a very little experience with programmming
back in college and some with elisp since using Emacs, but it's
limited and so I'm stretching myself to figure this out. Kind of fun,
actually, and it seems like a high level language like elisp ought to
make it fairly simple.
Creating the buffer works:
(add-hook 'after-init-hook
(function
(lambda ()
(pop-to-buffer (get-buffer-create " *Dharma*"))
After this, I'm having trouble figuring out how to randomly select a
file in a directory and to read the file into the buffer.
The files are in a subdirectory (called kaya), numbered 1 to whatever,
so I want to randomly pick an integer from 1 to whatever. I thought
that (find-file) or perhaps (find-file-noselect) could be used in
conjunction with (random), but I can't figure out how to do that.
I've probably missed someting really bloody obvious in the elisp
manual...
Seems to me that there are two steps. One is to count how many files
are in the directory, and to use that count as the limit for
(random). Then to take the "random" number generated by (random) and
use it with find-file.
Pointers to relevant information are much appreciated! I am probably
not even starting in the right place.
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-11-30 23:13 Tim McNamara
@ 2005-11-30 23:22 ` Lennart Borgman
2005-12-01 5:07 ` Stefan Monnier
` (2 subsequent siblings)
3 siblings, 0 replies; 33+ messages in thread
From: Lennart Borgman @ 2005-11-30 23:22 UTC (permalink / raw)
Cc: help-gnu-emacs
Tim McNamara wrote:
>Seems to me that there are two steps. One is to count how many files
>are in the directory, and to use that count as the limit for
>(random). Then to take the "random" number generated by (random) and
>use it with find-file.
>
>Pointers to relevant information are much appreciated! I am probably
>not even starting in the right place.
>
>
Maybe you can use the function `directory-files'?
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-11-30 23:13 Tim McNamara
2005-11-30 23:22 ` Lennart Borgman
@ 2005-12-01 5:07 ` Stefan Monnier
2005-12-01 19:29 ` Tim McNamara
2005-12-02 17:51 ` Kevin Rodgers
2005-12-01 6:24 ` N. Raghavendra
[not found] ` <mailman.17404.1133392955.20277.help-gnu-emacs@gnu.org>
3 siblings, 2 replies; 33+ messages in thread
From: Stefan Monnier @ 2005-12-01 5:07 UTC (permalink / raw)
> The files are in a subdirectory (called kaya), numbered 1 to whatever,
> so I want to randomly pick an integer from 1 to whatever. I thought
> that (find-file) or perhaps (find-file-noselect) could be used in
> conjunction with (random), but I can't figure out how to do that.
> I've probably missed someting really bloody obvious in the elisp
> manual...
(pop-to-buffer
(find-file-noselect (expand-file-name (number-to-string (random N))
"foo/bar/kaya")))
This assumes the file names go from 0 to N-1.
You could also do
(let ((files (directory-files "foo/bar/kaya" 'full "[^.]\\|...")))
(pop-to-buffer (find-file-noselect (nth (random (length files)) files))))
and just select any random file in the directory, without any assumption on
the file names used.
Stefan
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 5:07 ` Stefan Monnier
@ 2005-12-01 19:29 ` Tim McNamara
2005-12-01 21:00 ` Stefan Monnier
2005-12-02 17:51 ` Kevin Rodgers
1 sibling, 1 reply; 33+ messages in thread
From: Tim McNamara @ 2005-12-01 19:29 UTC (permalink / raw)
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> The files are in a subdirectory (called kaya), numbered 1 to
>> whatever, so I want to randomly pick an integer from 1 to whatever.
>> I thought that (find-file) or perhaps (find-file-noselect) could be
>> used in conjunction with (random), but I can't figure out how to do
>> that. I've probably missed something really bloody obvious in the
>> elisp manual...
>
> (pop-to-buffer
> (find-file-noselect (expand-file-name (number-to-string (random N))
> "foo/bar/kaya")))
>
> This assumes the file names go from 0 to N-1.
>
> You could also do
>
> (let ((files (directory-files "foo/bar/kaya" 'full "[^.]\\|...")))
> (pop-to-buffer (find-file-noselect (nth (random (length files)) files))))
>
> and just select any random file in the directory, without any assumption on
> the file names used.
That looks simpler than what I was thinking, although I'll have to go
through it argument by argument to make sure I understand it. I was
expecting to have to count the number of files in the directory, then
use that number as the limit to (random), and somehow be able to make
(find-files) use the result from (random) as the name of the file to
be read into the buffer. Simpler is better!
I'll test and post the results. Thanks!
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 19:29 ` Tim McNamara
@ 2005-12-01 21:00 ` Stefan Monnier
2005-12-02 1:02 ` Tim McNamara
0 siblings, 1 reply; 33+ messages in thread
From: Stefan Monnier @ 2005-12-01 21:00 UTC (permalink / raw)
>> (let ((files (directory-files "foo/bar/kaya" 'full "[^.]\\|...")))
>> (pop-to-buffer (find-file-noselect (nth (random (length files)) files))))
> That looks simpler than what I was thinking, although I'll have to go
> through it argument by argument to make sure I understand it. I was
> expecting to have to count the number of files in the directory, then
(length files) counts the number of files.
> use that number as the limit to (random),
Indeed, that's what it does.
> and somehow be able to make (find-files) use the result from (random) as
> the name of the file to be read into the buffer.
Indeed (nth ... files) selects the randomly chosen file name from the list
of file names.
Stefan
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 21:00 ` Stefan Monnier
@ 2005-12-02 1:02 ` Tim McNamara
0 siblings, 0 replies; 33+ messages in thread
From: Tim McNamara @ 2005-12-02 1:02 UTC (permalink / raw)
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>> (let ((files (directory-files "foo/bar/kaya" 'full "[^.]\\|...")))
>>> (pop-to-buffer (find-file-noselect (nth (random (length files)) files))))
>
>> That looks simpler than what I was thinking, although I'll have to
>> go through it argument by argument to make sure I understand it. I
>> was expecting to have to count the number of files in the
>> directory, then
>
> (length files) counts the number of files.
>
>> use that number as the limit to (random),
>
> Indeed, that's what it does.
>
>> and somehow be able to make (find-files) use the result from (random) as
>> the name of the file to be read into the buffer.
>
> Indeed (nth ... files) selects the randomly chosen file name from the list
> of file names.
Well, there it is. It works well in early testing. Sheesh, it'd have
taken me a month to work up something like that! Thanks lots. Now I
get to go through the elisp manual and learn from the example- and to
populate the data directory with files.
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 5:07 ` Stefan Monnier
2005-12-01 19:29 ` Tim McNamara
@ 2005-12-02 17:51 ` Kevin Rodgers
1 sibling, 0 replies; 33+ messages in thread
From: Kevin Rodgers @ 2005-12-02 17:51 UTC (permalink / raw)
Stefan Monnier wrote:
> (pop-to-buffer
> (find-file-noselect (expand-file-name (number-to-string (random N))
> "foo/bar/kaya")))
>
> This assumes the file names go from 0 to N-1.
>
> You could also do
>
> (let ((files (directory-files "foo/bar/kaya" 'full "[^.]\\|...")))
> (pop-to-buffer (find-file-noselect (nth (random (length files)) files))))
>
> and just select any random file in the directory, without any assumption on
> the file names used.
Since the original code already popped to the *Dharma* buffer, I think
(erase-buffer) (insert-file-contents ...) would be better than
(pop-to-buffer (find-file-noselect ...))
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-11-30 23:13 Tim McNamara
2005-11-30 23:22 ` Lennart Borgman
2005-12-01 5:07 ` Stefan Monnier
@ 2005-12-01 6:24 ` N. Raghavendra
2005-12-01 19:32 ` Tim McNamara
[not found] ` <mailman.17404.1133392955.20277.help-gnu-emacs@gnu.org>
3 siblings, 1 reply; 33+ messages in thread
From: N. Raghavendra @ 2005-12-01 6:24 UTC (permalink / raw)
At 2005-11-30T17:13:33-06:00, Tim McNamara wrote:
> I am looking to write a small program for Emacs which will create a
> new buffer and read a randomly selected text file into it after
> launching Emacs- rather like yow or fortune, except the files would be
> Dharma snippets. ...
>
> The files are in a subdirectory (called kaya)
You could use the `fortune.el' which is part of Emacs, and uses
fortune(6) to randomly select a cookie. The relevant init file
expressions are like this:
(setq fortune-buffer-name "*Dharma*")
(add-hook 'after-init-hook
(lambda ()
(fortune (expand-file-name "~/kaya"))))
HTH,
Raghavendra.
--
N. Raghavendra <raghu@mri.ernet.in> | See message headers for contact
Harish-Chandra Research Institute | and OpenPGP details.
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 6:24 ` N. Raghavendra
@ 2005-12-01 19:32 ` Tim McNamara
2005-12-01 21:37 ` N. Raghavendra
0 siblings, 1 reply; 33+ messages in thread
From: Tim McNamara @ 2005-12-01 19:32 UTC (permalink / raw)
"N. Raghavendra" <raghu@mri.ernet.in> writes:
> At 2005-11-30T17:13:33-06:00, Tim McNamara wrote:
>
>> I am looking to write a small program for Emacs which will create a
>> new buffer and read a randomly selected text file into it after
>> launching Emacs- rather like yow or fortune, except the files would
>> be Dharma snippets. ...
>>
>> The files are in a subdirectory (called kaya)
>
> You could use the `fortune.el' which is part of Emacs, and uses
> fortune(6) to randomly select a cookie. The relevant init file
> expressions are like this:
>
> (setq fortune-buffer-name "*Dharma*")
> (add-hook 'after-init-hook
> (lambda ()
> (fortune (expand-file-name "~/kaya"))))
Thank you, I will look into that further. I didn't realize that Emacs
had its own fortune program and had errantly thought that the standard
Unix fortune was being called. Dang- I've been using Emacs for 2-3
years and am still a newbie!
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 19:32 ` Tim McNamara
@ 2005-12-01 21:37 ` N. Raghavendra
2005-12-01 23:40 ` Thien-Thi Nguyen
2005-12-02 1:05 ` Tim McNamara
0 siblings, 2 replies; 33+ messages in thread
From: N. Raghavendra @ 2005-12-01 21:37 UTC (permalink / raw)
At 2005-12-01T13:32:02-06:00, Tim McNamara wrote:
> Thank you, I will look into that further. I didn't realize that Emacs
> had its own fortune program and had errantly thought that the standard
> Unix fortune was being called.
`fortune.el' does use the standard Unix command fortune(6) to do the
actual work. However, the cookie files used by fortune(6) need not be
the standard ones. They could, for instance, be the Dhammapada verses
you want to use.
I forgot to point out that before you can do what I had suggested, you
need to create the data files for your fortunes, using strfile(8):
cd ~/kaya; for i in [0-9]*; do strfile $i; done
Raghavendra.
--
N. Raghavendra <raghu@mri.ernet.in> | See message headers for contact
Harish-Chandra Research Institute | and OpenPGP details.
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 21:37 ` N. Raghavendra
@ 2005-12-01 23:40 ` Thien-Thi Nguyen
2005-12-02 7:44 ` N. Raghavendra
2005-12-02 1:05 ` Tim McNamara
1 sibling, 1 reply; 33+ messages in thread
From: Thien-Thi Nguyen @ 2005-12-01 23:40 UTC (permalink / raw)
"N. Raghavendra" <raghu@mri.ernet.in> writes:
> using strfile(8)
fyi, the emacs lisp reference manual in cvs emacs, node "Bindat Examples",
has code that does cookie file composition and extraction in elisp (i.e.,
emulating the common-case behaviors of fortune(1) and strfile(8), rendering
them unnecessary).
thi
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 23:40 ` Thien-Thi Nguyen
@ 2005-12-02 7:44 ` N. Raghavendra
2005-12-02 14:43 ` Thien-Thi Nguyen
0 siblings, 1 reply; 33+ messages in thread
From: N. Raghavendra @ 2005-12-02 7:44 UTC (permalink / raw)
At 2005-12-02T00:40:14+01:00, Thien-Thi Nguyen wrote:
> fyi, the emacs lisp reference manual in cvs emacs, node "Bindat
> Examples", has code that does cookie file composition and extraction
> in elisp (i.e., emulating the common-case behaviors of fortune(1)
> and strfile(8), rendering them unnecessary).
Thank you for the information. I checked out the Emacs Lisp manual
from CVS, and saw the examples. Since I use Emacs 21.3, I checked out
`bindat.el' from CVS, and tried the examples from the manual. They
function perfectly.
There seems to be some incompatibility between the index files created
from the same fortune file by strfile(8) and `fcookie-create-index'.
In particular, if I create an index file with `fcookie-create-index'
and apply fortune(1) to it, I sometimes get the first cookie in the
file, or no cookie at all. OTOH, the same index file works well with
`fcookie'.
Similarly, if I create an index file with strfile(8) and evaluate
`fcookie' on the corresponding cookie file, a buffer containing the
entire cookie file is displayed.
Raghavendra.
--
N. Raghavendra <raghu@mri.ernet.in> | See message headers for contact
Harish-Chandra Research Institute | and OpenPGP details.
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-02 7:44 ` N. Raghavendra
@ 2005-12-02 14:43 ` Thien-Thi Nguyen
0 siblings, 0 replies; 33+ messages in thread
From: Thien-Thi Nguyen @ 2005-12-02 14:43 UTC (permalink / raw)
"N. Raghavendra" <raghu@mri.ernet.in> writes:
> There seems to be some incompatibility between the index files created
> from the same fortune file by strfile(8) and `fcookie-create-index'.
> In particular, if I create an index file with `fcookie-create-index'
> and apply fortune(1) to it, I sometimes get the first cookie in the
> file, or no cookie at all. OTOH, the same index file works well with
> `fcookie'.
>
> Similarly, if I create an index file with strfile(8) and evaluate
> `fcookie' on the corresponding cookie file, a buffer containing the
> entire cookie file is displayed.
the index file format and/or the programs that read/write it have
mutated slightly over the years. tracking the variations is not worth
it for the elisp manual where the priority, as i see it, of conciseness
and clarity in the example would be defeated by the lengthenend code
required for completeness.
however, leaving such weirdness to fester outside the manual[1] is
rather uncool. at the moment i have no cycles for debugging this, but
if someone digs into the details and sends me a patch that includes not
only a fix to the code but an explanation of the format drift, i would
be glad to install it and give credit. on the other hand, if the patch
reveals an actual bug in my understanding (reverse-engineering) of the
index file format, i suppose i would install it and give credit
(anyway), but only begrudgingly (don't let that dissuade anyone ;-).
thi
[1] http://www.glug.org/people/ttn/software/personal-elisp/
(look for fcookie.el in dist-lisp-index.html)
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2005-12-01 21:37 ` N. Raghavendra
2005-12-01 23:40 ` Thien-Thi Nguyen
@ 2005-12-02 1:05 ` Tim McNamara
1 sibling, 0 replies; 33+ messages in thread
From: Tim McNamara @ 2005-12-02 1:05 UTC (permalink / raw)
"N. Raghavendra" <raghu@mri.ernet.in> writes:
> At 2005-12-01T13:32:02-06:00, Tim McNamara wrote:
>
>> Thank you, I will look into that further. I didn't realize that
>> Emacs had its own fortune program and had errantly thought that the
>> standard Unix fortune was being called.
>
> `fortune.el' does use the standard Unix command fortune(6) to do the
> actual work. However, the cookie files used by fortune(6) need not
> be the standard ones. They could, for instance, be the Dhammapada
> verses you want to use.
>
> I forgot to point out that before you can do what I had suggested,
> you need to create the data files for your fortunes, using
> strfile(8):
>
> cd ~/kaya; for i in [0-9]*; do strfile $i; done
Thank you for that!
^ permalink raw reply [flat|nested] 33+ messages in thread
[parent not found: <mailman.17404.1133392955.20277.help-gnu-emacs@gnu.org>]
* elisp question
@ 2003-08-20 7:37 Mike Ballard
2003-08-20 8:07 ` Joakim Hove
2003-08-20 15:07 ` Peter Lee
0 siblings, 2 replies; 33+ messages in thread
From: Mike Ballard @ 2003-08-20 7:37 UTC (permalink / raw)
Hi -
I don't know much about elisp but was able to piece together a little
snippet that pretty much does what I want. And that's to determine if a
post is to Usenet and write a copy to a file.
The problem is if I post again to the same group (w/o restarting Gnus) I
get the mb msg '<file> has changed on disk - really edit the buffer?' I'd
like to eliminate that (I can post to other groups fine so long as I don't
try more than one post to any group during a single Gnus session).
If there was some way to add code which re-reads the disk file before it's
written to a second time that (apparently) would fix it. Or there's
probably a better way that I have no idea about. (The "touch" is in there
in case the file doesn't already exist and I wonder if it's the problem?
I tried using file-exists-p but have sort of been going in circles
(backwards) without success).
Can someone tell me how to alter the code below either by re-reading the
disk file before it's written again or if "touch" is the problem maybe
someone could tell me how to use file-exists-p? I think I'd still need
the "touch" (or something) for the occasions when the file does not yet
exist.
(start-process-shell-command "foo" "bar"
"/bin/touch" (format "/dd/Gnus/posts/%s.posts" group))
(if (not (message-news-p))
"nnfolder:../mail/mail_cc"
(format "nnfolder:../posts/%s.posts" group)))
Mike
--
mike.ballard--at--earthlink.net
"Roses are red, violets are blue,
I'm schizophrenic and so am I"
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2003-08-20 7:37 Mike Ballard
@ 2003-08-20 8:07 ` Joakim Hove
2003-08-22 5:52 ` Mike Ballard
2003-08-20 15:07 ` Peter Lee
1 sibling, 1 reply; 33+ messages in thread
From: Joakim Hove @ 2003-08-20 8:07 UTC (permalink / raw)
Mike Ballard <dont_w@nt_spam.org> writes:
> Hi -
>
> I don't know much about elisp but was able to piece together a little
> snippet that pretty much does what I want. And that's to determine if a
> post is to Usenet and write a copy to a file.
>
> The problem is if I post again to the same group (w/o restarting Gnus) I
> get the mb msg '<file> has changed on disk - really edit the buffer?' I'd
> like to eliminate that (I can post to other groups fine so long as I don't
> try more than one post to any group during a single Gnus session).
>
> If there was some way to add code which re-reads the disk file before it's
> written to a second time that (apparently) would fix it.
Look into the function (revert-buffer). However, if this is a
buffer/file entirely managed by gnus/emacs I can not understand how it
changes under your feet - the mb msg '<file> has changed on disk -
really edit the buffer?' generally comes when some *other* program
has altered a file on disk while you have a buffer mapped to this file
emacs. Actually I suspect that the touch code below works, and by that
modifies the file such that emacs thinks the file is newer than the
buffer (which is in this case formally correct, but not what you
want), and the present with the '<file> has ...' is displayed. So
altough I think (revert-buffer) is answer to your question, I *think*
you are asking the wrong question, and should rather focus on *why*
the file is modified.
> I tried using file-exists-p but have sort of been going in circles
> (backwards) without success).
Why do you have to ensure that the file exists? You can manually set
the file to store a fresh buffer to by (setq buffer-file-name "filename").
> Can someone tell me how to alter the code below either by re-reading the
> disk file before it's written again or if "touch" is the problem maybe
> someone could tell me how to use file-exists-p? I think I'd still need
> the "touch" (or something) for the occasions when the file does not yet
> exist.
The touch is probably OK, but simpler:
(shell-command (format "/bin/touch %s" file))
>
>
> (start-process-shell-command "foo" "bar"
> "/bin/touch" (format "/dd/Gnus/posts/%s.posts" group))
> (if (not (message-news-p))
> "nnfolder:../mail/mail_cc"
> (format "nnfolder:../posts/%s.posts" group)))
HTH - Joakim
--
/--------------------------------------------------------------------\
/ Joakim Hove / hove@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2003-08-20 8:07 ` Joakim Hove
@ 2003-08-22 5:52 ` Mike Ballard
2003-08-22 6:38 ` Joakim Hove
0 siblings, 1 reply; 33+ messages in thread
From: Mike Ballard @ 2003-08-22 5:52 UTC (permalink / raw)
On Fri Aug 22, Joakim Hove disturbed my nap when he said:
> Mike Ballard <dont_w@nt_spam.org> writes:
>
> > Hi -
> >
> > I don't know much about elisp but was able to piece together a little
> > snippet that pretty much does what I want. And that's to determine if a
> > post is to Usenet and write a copy to a file.
> >
> > The problem is if I post again to the same group (w/o restarting Gnus) I
> > get the mb msg '<file> has changed on disk - really edit the buffer?' I'd
> > like to eliminate that (I can post to other groups fine so long as I don't
> > try more than one post to any group during a single Gnus session).
> >
>
>
> > If there was some way to add code which re-reads the disk file before it's
> > written to a second time that (apparently) would fix it.
>
> Look into the function (revert-buffer). However, if this is a
> buffer/file entirely managed by gnus/emacs I can not understand how it
> changes under your feet - the mb msg '<file> has changed on disk -
> really edit the buffer?' generally comes when some *other* program
> has altered a file on disk while you have a buffer mapped to this file
> emacs. Actually I suspect that the touch code below works, and by that
> modifies the file such that emacs thinks the file is newer than the
> buffer (which is in this case formally correct, but not what you
> want), and the present with the '<file> has ...' is displayed. So
> altough I think (revert-buffer) is answer to your question, I *think*
> you are asking the wrong question, and should rather focus on *why*
> the file is modified.
>
> > I tried using file-exists-p but have sort of been going in circles
> > (backwards) without success).
>
> Why do you have to ensure that the file exists? You can manually set
> the file to store a fresh buffer to by (setq buffer-file-name "filename").
>
The "touch" is my beginner way of non-interactively creating a var-based
filename (the usenet group name).
Your "other" program comment got me thinking that the way I used
start-process-shell-command:touch in my func definitely was the problem
('really edit?'). The shell proc touch's the file, I post and Emacs
writes to disk (and for the first post it didn't matter if I touch'd an
existing file or not). After the first write to disk apparently Emacs now
knows the state of the file.
But the way my beginner-code was written it touch'd the file whether or
not it existed. So what I think happens is that the 2d time through
(after Emacs wrote first post to disk) my func touch'd it again and when
Emacs tried to write my second post to disk it said 'hey, this file's
different than I left it.'
My problem using file-exists (to use touch conditionally) was a misplaced
paren. I had marked off, simplified, (defun* (if test then1 if (then2))).
then2 did not belong with the first 'if' so I thought file-exists wasn't
working. I got it straightened out and is working acceptably now.
Thanks for the help.
Mike
--
mike.ballard--at--earthlink.net
"Roses are red, violets are blue,
I'm schizophrenic and so am I"
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2003-08-22 5:52 ` Mike Ballard
@ 2003-08-22 6:38 ` Joakim Hove
0 siblings, 0 replies; 33+ messages in thread
From: Joakim Hove @ 2003-08-22 6:38 UTC (permalink / raw)
Hello,
> So what I think happens is that the 2d time through (after Emacs
> wrote first post to disk) my func touch'd it again and when Emacs
> tried to write my second post to disk it said 'hey, this file's
> different than I left it.'
Yes - that was what I tried to express, in my obfuscated english.
Joakim
--
/--------------------------------------------------------------------\
/ Joakim Hove / hove@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: elisp question
2003-08-20 7:37 Mike Ballard
2003-08-20 8:07 ` Joakim Hove
@ 2003-08-20 15:07 ` Peter Lee
1 sibling, 0 replies; 33+ messages in thread
From: Peter Lee @ 2003-08-20 15:07 UTC (permalink / raw)
>>>> Mike Ballard writes:
Mike> I don't know much about elisp but was able to piece together
Mike> a little snippet that pretty much does what I want. And
Mike> that's to determine if a post is to Usenet and write a copy
Mike> to a file.
This doesn't answer your elisp question, but another way to archive
your posts is like so:
(setq gnus-message-archive-group
'((if (message-news-p)
"nnml:news.sent"
"nnml:mail.sent")))
You can view and manage them from the group buffer this way.
^ permalink raw reply [flat|nested] 33+ messages in thread
end of thread, other threads:[~2009-09-27 17:10 UTC | newest]
Thread overview: 33+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-27 14:32 Elisp Question magicus
2009-09-27 15:26 ` Giorgos Keramidas
2009-09-27 17:10 ` magicus
[not found] <mailman.941.1223918781.25473.help-gnu-emacs@gnu.org>
2008-10-13 18:11 ` elisp question Niels Giesen
2008-10-13 19:09 ` Seweryn Kokot
[not found] ` <mailman.948.1223924333.25473.help-gnu-emacs@gnu.org>
2008-10-13 19:40 ` Pascal J. Bourguignon
2008-10-13 20:47 ` Drew Adams
2008-10-13 21:11 ` Seweryn Kokot
-- strict thread matches above, loose matches on Subject: below --
2008-10-13 17:36 Seweryn Kokot
2008-04-30 21:23 harven
2008-05-01 13:08 ` Kevin Rodgers
[not found] ` <mailman.11020.1209647315.18990.help-gnu-emacs@gnu.org>
2008-05-01 13:54 ` harven
2008-05-03 15:38 ` Bastien
2005-11-30 23:13 Tim McNamara
2005-11-30 23:22 ` Lennart Borgman
2005-12-01 5:07 ` Stefan Monnier
2005-12-01 19:29 ` Tim McNamara
2005-12-01 21:00 ` Stefan Monnier
2005-12-02 1:02 ` Tim McNamara
2005-12-02 17:51 ` Kevin Rodgers
2005-12-01 6:24 ` N. Raghavendra
2005-12-01 19:32 ` Tim McNamara
2005-12-01 21:37 ` N. Raghavendra
2005-12-01 23:40 ` Thien-Thi Nguyen
2005-12-02 7:44 ` N. Raghavendra
2005-12-02 14:43 ` Thien-Thi Nguyen
2005-12-02 1:05 ` Tim McNamara
[not found] ` <mailman.17404.1133392955.20277.help-gnu-emacs@gnu.org>
2005-12-01 19:25 ` Tim McNamara
2003-08-20 7:37 Mike Ballard
2003-08-20 8:07 ` Joakim Hove
2003-08-22 5:52 ` Mike Ballard
2003-08-22 6:38 ` Joakim Hove
2003-08-20 15:07 ` Peter Lee
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.