unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Basic Emacs Lisp question
@ 2008-04-29 15:49 Matthias Pfeifer
  2008-04-29 17:56 ` Giorgos Keramidas
  2008-04-29 21:14 ` tyler
  0 siblings, 2 replies; 29+ messages in thread
From: Matthias Pfeifer @ 2008-04-29 15:49 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

What is the difference between

(list 0 nil -1)

and

'(0 nil -1)

?

Matthias Pfeifer


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-29 15:49 Basic Emacs Lisp question Matthias Pfeifer
@ 2008-04-29 17:56 ` Giorgos Keramidas
  2008-04-30  1:21   ` Giorgos Keramidas
  2008-04-30 15:26   ` David Kastrup
  2008-04-29 21:14 ` tyler
  1 sibling, 2 replies; 29+ messages in thread
From: Giorgos Keramidas @ 2008-04-29 17:56 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer <pfemat@web.de> wrote:
> Hello,
>
> What is the difference between
>
> (list 0 nil -1)
>
> and
>
> '(0 nil -1)

In Common Lisp (list 0 nil -1) is required to 'cons' a new list every
time it is called.  Quoting the list as in '(0 nil -1) is not required
to build a new list.  In fact, in compiled code it may reuse the same
static object over and over again.



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-29 15:49 Basic Emacs Lisp question Matthias Pfeifer
  2008-04-29 17:56 ` Giorgos Keramidas
@ 2008-04-29 21:14 ` tyler
  2008-04-30  8:31   ` Tim X
  1 sibling, 1 reply; 29+ messages in thread
From: tyler @ 2008-04-29 21:14 UTC (permalink / raw)
  To: help-gnu-emacs

Matthias Pfeifer <pfemat@web.de> writes:

> What is the difference between
>
> (list 0 nil -1)
>
> and
>
> '(0 nil -1)
>

In this instance, nothing. However, the '(...) form leaves it's
arguments unevaluated (quoted), while (list ...) evaluates them. Since
0, nil and -1 all evaluate to themselves there is no difference in this
case. However, the following two statements are different:

(list 0 fill-column -1)

'(0 fill-column -1)

A good place to start learning is the Introduction to Programming in
Emacs Lisp, which is available from gnu.org, if it isn't already present
in your info directory (i.e., C-h i m emacs lisp intro <ret>).

HTH,

Tyler


-- 
Tired of spyware? Try Firefox:

www.firefox.com


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-29 17:56 ` Giorgos Keramidas
@ 2008-04-30  1:21   ` Giorgos Keramidas
  2008-04-30  8:37     ` Tim X
  2008-04-30 15:26   ` David Kastrup
  1 sibling, 1 reply; 29+ messages in thread
From: Giorgos Keramidas @ 2008-04-30  1:21 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 29 Apr 2008 20:56:17 +0300, Giorgos Keramidas <keramida@ceid.upatras.gr> wrote:
> On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer <pfemat@web.de> wrote:
>> Hello,
>>
>> What is the difference between
>>
>> (list 0 nil -1)
>>
>> and
>>
>> '(0 nil -1)
>
> In Common Lisp (list 0 nil -1) is required to 'cons' a new list every
> time it is called.  Quoting the list as in '(0 nil -1) is not required
> to build a new list.  In fact, in compiled code it may reuse the same
> static object over and over again.

Reading my own post reveals that I may have been too terse.  To clarify
the point I was trying to make, here's a small test in Common Lisp, and
the equivalent test in Emacs Lisp.

1. Common Lisp test
-------------------

* Save the following Lisp code to a file called "foo.lisp":

  (defun foo-quoted ()
    '(0 nil -1))

  (defun foo-list ()
    (list 0 nil -1))

* Then compile the file, and load it.  Here's the output from loading
  the compiled file in SBCL:

  CL-USER> (compile-file "foo")

  ; compiling file "/home/keramida/foo.lisp" (written 30 APR 2008 01:48:02 AM):
  ; compiling (DEFUN FOO-QUOTED ...)
  ; compiling (DEFUN FOO-LIST ...)

  ; /home/keramida/foo.fasl written
  ; compilation finished in 0:00:00
  #P"/home/keramida/foo.fasl"
  NIL
  NIL
  CL-USER> (load "foo")          ;; This actually loads "foo.fasl" in SBCL.
  T
  CL-USER>

* Every time the `foo-quoted' function runs it returns exactly the same
  compiled object.  The object returned by separate calls to
  `foo-quoted' is all of EQ, EQL and EQUAL to any previous call, as you
  can see in:

  CL-USER> (let ((one-list (foo-quoted))
                 (another-list (foo-quoted)))
             (mapcar (lambda (test)
                       (funcall test one-list another-list))
                     (list #'eq #'eql #'equal)))
  (T T T)
  CL-USER>

* In contrast, the object returned by the `foo-list' function is a newly
  CONS-ed list every time the function runs:

  CL-USER> (let ((one-list (foo-list))
                 (another-list (foo-list)))
             (mapcar (lambda (test)
                       (funcall test one-list another-list))
                     (list #'eq #'eql #'equal)))
  (NIL NIL T)
  CL-USER>

The lists returned by `foo-list' are EQUAL, but they are neither EQ nor
EQL to each other.  They are created from scratch by allocating new
storage for the value of the expression every time the `foo-list'
function is called.

2. Emacs Lisp test
------------------

* Save the same two functions in a file called "foo.el".

* Fire up Emacs, and byte-compile the file by typing

  M-x byte-compile-file RET foo.el RET

* Load the byte-compiled file by typing

  M-x load-file RET foo.elc RET

* Now evaluate the same two LET forms in your scratch buffer, by pasting
  them in the buffer and typing `C-x C-e' after each expression.

  Emacs Lisp should also evaluate them as:

  (let ((one-list (foo-quoted))
        (another-list (foo-quoted)))
    (mapcar (lambda (test)
              (funcall test one-list another-list))
            (list #'eq #'eql #'equal)))
  => (t t t)

  (let ((one-list (foo-list))
        (another-list (foo-list)))
    (mapcar (lambda (test)
              (funcall test one-list another-list))
            (list #'eq #'eql #'equal)))
  => (nil nil t)

I hope this makes what I initially wrote a bit easier to grasp :-)

Giorgos



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-29 21:14 ` tyler
@ 2008-04-30  8:31   ` Tim X
  2008-05-05 13:38     ` tyler
  0 siblings, 1 reply; 29+ messages in thread
From: Tim X @ 2008-04-30  8:31 UTC (permalink / raw)
  To: help-gnu-emacs

tyler <tyler.smith@mail.mcgill.ca> writes:

> Matthias Pfeifer <pfemat@web.de> writes:
>
>> What is the difference between
>>
>> (list 0 nil -1)
>>
>> and
>>
>> '(0 nil -1)
>>
>
> In this instance, nothing. However, the '(...) form leaves it's
> arguments unevaluated (quoted), while (list ...) evaluates them. Since
> 0, nil and -1 all evaluate to themselves there is no difference in this
> case. However, the following two statements are different:
>
> (list 0 fill-column -1)
>
> '(0 fill-column -1)
>
> A good place to start learning is the Introduction to Programming in
> Emacs Lisp, which is available from gnu.org, if it isn't already present
> in your info directory (i.e., C-h i m emacs lisp intro <ret>).
>

Are you sure there is no difference? In many lisp dialects, the second
form is more like a constant and cannot be modified in a reliable
manner - IIRC this is due to how memory is allocated for the
quoted form. In the first one, memory is allocated dynamically and so
can be safely modified. 

Tim

-- 
tcross (at) rapttech dot com dot au


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-30  1:21   ` Giorgos Keramidas
@ 2008-04-30  8:37     ` Tim X
  0 siblings, 0 replies; 29+ messages in thread
From: Tim X @ 2008-04-30  8:37 UTC (permalink / raw)
  To: help-gnu-emacs

Giorgos Keramidas <keramida@ceid.upatras.gr> writes:

> On Tue, 29 Apr 2008 20:56:17 +0300, Giorgos Keramidas <keramida@ceid.upatras.gr> wrote:
>> On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer <pfemat@web.de> wrote:
>>> Hello,
>>>
>>> What is the difference between
>>>
>>> (list 0 nil -1)
>>>
>>> and
>>>
>>> '(0 nil -1)
>>
>> In Common Lisp (list 0 nil -1) is required to 'cons' a new list every
>> time it is called.  Quoting the list as in '(0 nil -1) is not required
>> to build a new list.  In fact, in compiled code it may reuse the same
>> static object over and over again.
>
> Reading my own post reveals that I may have been too terse.  To clarify
> the point I was trying to make, here's a small test in Common Lisp, and
> the equivalent test in Emacs Lisp.
>
> 1. Common Lisp test
> -------------------
>
> * Save the following Lisp code to a file called "foo.lisp":
>
>   (defun foo-quoted ()
>     '(0 nil -1))
>
>   (defun foo-list ()
>     (list 0 nil -1))
>
> * Then compile the file, and load it.  Here's the output from loading
>   the compiled file in SBCL:
>
>   CL-USER> (compile-file "foo")
>
>   ; compiling file "/home/keramida/foo.lisp" (written 30 APR 2008 01:48:02 AM):
>   ; compiling (DEFUN FOO-QUOTED ...)
>   ; compiling (DEFUN FOO-LIST ...)
>
>   ; /home/keramida/foo.fasl written
>   ; compilation finished in 0:00:00
>   #P"/home/keramida/foo.fasl"
>   NIL
>   NIL
>   CL-USER> (load "foo")          ;; This actually loads "foo.fasl" in SBCL.
>   T
>   CL-USER>
>
> * Every time the `foo-quoted' function runs it returns exactly the same
>   compiled object.  The object returned by separate calls to
>   `foo-quoted' is all of EQ, EQL and EQUAL to any previous call, as you
>   can see in:
>
>   CL-USER> (let ((one-list (foo-quoted))
>                  (another-list (foo-quoted)))
>              (mapcar (lambda (test)
>                        (funcall test one-list another-list))
>                      (list #'eq #'eql #'equal)))
>   (T T T)
>   CL-USER>
>
> * In contrast, the object returned by the `foo-list' function is a newly
>   CONS-ed list every time the function runs:
>
>   CL-USER> (let ((one-list (foo-list))
>                  (another-list (foo-list)))
>              (mapcar (lambda (test)
>                        (funcall test one-list another-list))
>                      (list #'eq #'eql #'equal)))
>   (NIL NIL T)
>   CL-USER>
>
> The lists returned by `foo-list' are EQUAL, but they are neither EQ nor
> EQL to each other.  They are created from scratch by allocating new
> storage for the value of the expression every time the `foo-list'
> function is called.
>
> 2. Emacs Lisp test
> ------------------
>
> * Save the same two functions in a file called "foo.el".
>
> * Fire up Emacs, and byte-compile the file by typing
>
>   M-x byte-compile-file RET foo.el RET
>
> * Load the byte-compiled file by typing
>
>   M-x load-file RET foo.elc RET
>
> * Now evaluate the same two LET forms in your scratch buffer, by pasting
>   them in the buffer and typing `C-x C-e' after each expression.
>
>   Emacs Lisp should also evaluate them as:
>
>   (let ((one-list (foo-quoted))
>         (another-list (foo-quoted)))
>     (mapcar (lambda (test)
>               (funcall test one-list another-list))
>             (list #'eq #'eql #'equal)))
>   => (t t t)
>
>   (let ((one-list (foo-list))
>         (another-list (foo-list)))
>     (mapcar (lambda (test)
>               (funcall test one-list another-list))
>             (list #'eq #'eql #'equal)))
>   => (nil nil t)
>
> I hope this makes what I initially wrote a bit easier to grasp :-)
>

I think you expressed the difference quite clearly. In another post, I
tried to point out a possible trap that many fall into - attempting to
modify the list returned by the quoted version. This is why I tend to
think of '(a b c) more like a constant, while (list a b c) is able to be
modified with expected results. 

Tim


-- 
tcross (at) rapttech dot com dot au


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-29 17:56 ` Giorgos Keramidas
  2008-04-30  1:21   ` Giorgos Keramidas
@ 2008-04-30 15:26   ` David Kastrup
  2008-04-30 18:36     ` Giorgos Keramidas
  1 sibling, 1 reply; 29+ messages in thread
From: David Kastrup @ 2008-04-30 15:26 UTC (permalink / raw)
  To: help-gnu-emacs

Giorgos Keramidas <keramida@ceid.upatras.gr> writes:

> On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer <pfemat@web.de> wrote:
>> Hello,
>>
>> What is the difference between
>>
>> (list 0 nil -1)
>>
>> and
>>
>> '(0 nil -1)
>
> In Common Lisp (list 0 nil -1) is required to 'cons' a new list every
> time it is called.  Quoting the list as in '(0 nil -1) is not required
> to build a new list.  In fact, in compiled code it may reuse the same
> static object over and over again.

Wrong word choice.  Not "may", but "must".  ' produces a list in the
Lisp reader.  Nothing may afterwards create gratuitious unannounced
copies.  So whether your code is compiled or interpreted: if it is not
reread, no new object is created.

I can write (interpreted)

(progn (setq x '(5)) (dotimes (i 5) (push i x)) x)
and get
(4 3 2 1 0 5)

and that is the only permitted behavior.

In contrast, list must always create a fresh object.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-30 15:26   ` David Kastrup
@ 2008-04-30 18:36     ` Giorgos Keramidas
  2008-05-01  9:25       ` Johan Bockgård
  0 siblings, 1 reply; 29+ messages in thread
From: Giorgos Keramidas @ 2008-04-30 18:36 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, 30 Apr 2008 17:26:25 +0200, David Kastrup <dak@gnu.org> wrote:
>Giorgos Keramidas <keramida@ceid.upatras.gr> writes:
>>On Tue, 29 Apr 2008 17:49:19 +0200, Matthias Pfeifer <pfemat@web.de> wrote:
>>> Hello,
>>>
>>> What is the difference between
>>>
>>> (list 0 nil -1)
>>>
>>> and
>>>
>>> '(0 nil -1)
>>
>> In Common Lisp (list 0 nil -1) is required to 'cons' a new list every
>> time it is called.  Quoting the list as in '(0 nil -1) is not required
>> to build a new list.  In fact, in compiled code it may reuse the same
>> static object over and over again.
>
> Wrong word choice.  Not "may", but "must".  ' produces a list in the
> Lisp reader.  Nothing may afterwards create gratuitious unannounced
> copies.  So whether your code is compiled or interpreted: if it is not
> reread, no new object is created.

Ah!  Many thanks for the explanation, David.

I am only beginning to grasp Lisp myself too, so I wasn't aware that
'-quoting produces the list in the reader :)



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-30 18:36     ` Giorgos Keramidas
@ 2008-05-01  9:25       ` Johan Bockgård
  0 siblings, 0 replies; 29+ messages in thread
From: Johan Bockgård @ 2008-05-01  9:25 UTC (permalink / raw)
  To: help-gnu-emacs

Giorgos Keramidas <keramida@ceid.upatras.gr> writes:

> I am only beginning to grasp Lisp myself too, so I wasn't aware that
> '-quoting produces the list in the reader :)

It doesn't.  The parentheses produce the list in the reader.  The
`quote' special form returns its argument without evaluating it.

    (read "'(0 1 2)")  =>  (quote (0 1 2))
    (quote (0 1 2))    =>  (0 1 2)    ; Not a copy

-- 
Johan Bockgård


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic Emacs Lisp question
  2008-04-30  8:31   ` Tim X
@ 2008-05-05 13:38     ` tyler
  0 siblings, 0 replies; 29+ messages in thread
From: tyler @ 2008-05-05 13:38 UTC (permalink / raw)
  To: help-gnu-emacs

Tim X <timx@nospam.dev.null> writes:

> tyler <tyler.smith@mail.mcgill.ca> writes:
>
>> Matthias Pfeifer <pfemat@web.de> writes:
>>
>>> What is the difference between
>>>
>>> (list 0 nil -1)
>>>
>>> and
>>>
>>> '(0 nil -1)
>>>
>>
>> In this instance, nothing. However, ...

> Are you sure there is no difference? In many lisp dialects, the second
> form is more like a constant and cannot be modified in a reliable
> manner - IIRC this is due to how memory is allocated for the
> quoted form. In the first one, memory is allocated dynamically and so
> can be safely modified. 
>

Ah, thanks. Reading over the other posts in this thread, I can see how
this distinction could lead to some hard-to-detect bugs. At least,
they'd be hard for me to detect.

Tyler

-- 
By protecting Net Neutrality, we guarantee that pro-union sites do not
get blocked, that ISPs do not charge anti-competitive 'preference'
fees and that independent media can compete based on content, not
pocketbook, with the largest of publishers.

                                    neutrality.ca -- it's your internet


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Basic emacs lisp question
@ 2014-09-09 19:35 Ken
  2014-09-09 19:52 ` Thorsten Jolitz
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Ken @ 2014-09-09 19:35 UTC (permalink / raw)
  To: help-gnu-emacs


I want to capture whatever is at point in a file into a variable
something like the following, but it doesn't seem to work. Can any one
suggest what I an doing wrong. It is probably a silly mistake I am
unable to see. I am just learning Emacs lisp.

(defun process-diary-file ()
  "Perform some manipulation of the diary file"
  (interactive)
  (find-file "~/diary")
  (goto-char 1)
  (set a (thing-at-point))
  (message a))

Thanks in advance for your assistance,
Ken


--
Q:	What do you call a WASP who doesn't work for his father, isn't a
	lawyer, and believes in social causes?
A:	A failure.



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-09 19:35 Basic emacs lisp question Ken
@ 2014-09-09 19:52 ` Thorsten Jolitz
  2014-09-09 21:05   ` Thorsten Jolitz
       [not found]   ` <mailman.8562.1410296811.1147.help-gnu-emacs@gnu.org>
       [not found] ` <mailman.8561.1410292382.1147.help-gnu-emacs@gnu.org>
  2014-09-10 13:12 ` Phillip Lord
  2 siblings, 2 replies; 29+ messages in thread
From: Thorsten Jolitz @ 2014-09-09 19:52 UTC (permalink / raw)
  To: help-gnu-emacs

Ken <kensubuntu@gmail.com> writes:

> I want to capture whatever is at point in a file into a variable
> something like the following, but it doesn't seem to work. Can any one
> suggest what I an doing wrong. It is probably a silly mistake I am
> unable to see. I am just learning Emacs lisp.
>
> (defun process-diary-file ()
>   "Perform some manipulation of the diary file"
>   (interactive)
>   (find-file "~/diary")
>   (goto-char 1)
>   (set a (thing-at-point))
>   (message a))

try

#+BEGIN_SRC emacs-lisp
(defun process-diary-file ()
  "Perform some manipulation of the diary file"
  (interactive)
  (find-file (expand-file-name "~/diary"))
  (goto-char (point-min))
  (setq a (thing-at-point))
  (message "%s" a))
#+END_SRC

>
> Thanks in advance for your assistance,
> Ken
>
>
> --
> Q:	What do you call a WASP who doesn't work for his father, isn't a
> 	lawyer, and believes in social causes?
> A:	A failure.
>
>

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
       [not found] ` <mailman.8561.1410292382.1147.help-gnu-emacs@gnu.org>
@ 2014-09-09 20:07   ` Emanuel Berg
       [not found]     ` <874mwgv93m.fsf@gmail.com>
  0 siblings, 1 reply; 29+ messages in thread
From: Emanuel Berg @ 2014-09-09 20:07 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> (setq a (thing-at-point)) ...
> (message "%s" a))

If you just want to store it in a variable to be able
to echo it (?), you can use `let' instead. It has
better karma than `setq'.

-- 
underground experts united


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
       [not found]     ` <874mwgv93m.fsf@gmail.com>
@ 2014-09-09 20:44       ` Emanuel Berg
       [not found]         ` <87y4tsts4t.fsf@gmail.com>
  0 siblings, 1 reply; 29+ messages in thread
From: Emanuel Berg @ 2014-09-09 20:44 UTC (permalink / raw)
  To: help-gnu-emacs

Ken <kensubuntu@gmail.com> writes:

>> If you just want to store it in a variable to be
>> able to echo it (?), you can use `let' instead. It
>> has better karma than `setq'.
> Actually, my plan is to do more than just echo it. I
> want to be able to perform tests on it. I want to
> place the %% entries to the beginning of the file
> (this necessitates creating a new file and then
> renaming it to the original file name) Dates that are
> older than today, I want to move to a diary archive
> file. Weekly events will also be lumped together.
>
> Basically to place some order to the file without
> manually editing the file.

OK, well it sounds you may still be able to use let
instead of setq. If you are coming from another
language, be it C or Perl or whatever, it often looks
like this:

int yada_yada (int whatever) {
   int temp_var = whatever*2;
   int temp_var_2 = ... // etc.
}

My experience, which is totally unscientific (on the
other hand I don't know if Stalin ever read
Das Kapital) - nevertheless it seems to work great if
you just substitute all those local variables with let
(and `let*') - you get a really neat, well-defined,
all-but functional Elisp world to play with.

-- 
underground experts united


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-09 19:52 ` Thorsten Jolitz
@ 2014-09-09 21:05   ` Thorsten Jolitz
       [not found]   ` <mailman.8562.1410296811.1147.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 29+ messages in thread
From: Thorsten Jolitz @ 2014-09-09 21:05 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> Ken <kensubuntu@gmail.com> writes:
>
>> I want to capture whatever is at point in a file into a variable
>> something like the following, but it doesn't seem to work. Can any one
>> suggest what I an doing wrong. It is probably a silly mistake I am
>> unable to see. I am just learning Emacs lisp.
>>
>> (defun process-diary-file ()
>>   "Perform some manipulation of the diary file"
>>   (interactive)
>>   (find-file "~/diary")
>>   (goto-char 1)
>>   (set a (thing-at-point))
>>   (message a))
>
> try
>
> #+BEGIN_SRC emacs-lisp
> (defun process-diary-file ()
>   "Perform some manipulation of the diary file"
>   (interactive)
>   (find-file (expand-file-name "~/diary"))
>   (goto-char (point-min))
>   (setq a (thing-at-point))
>   (message "%s" a))
> #+END_SRC

that was untested and has room for improvement (as mentioned in this
thread).
This is untested again, but hopefully better ;)

#+BEGIN_SRC emacs-lisp
  (defun process-diary-file ()
    "Perform some manipulation of the diary file"
    (interactive)
    (let (a)
      (find-file (expand-file-name "~/diary"))
      (goto-char (point-min))
      (setq a (thing-at-point 'symbol))   
      (message "%s" a)))
#+END_SRC

see 

,----[ C-h f thing-at-point RET ]
| thing-at-point is an autoloaded compiled Lisp function in
| `thingatpt.el'.
| 
| (thing-at-point THING)
| 
| Return the THING at point.
| THING should be a symbol specifying a type of syntactic entity.
| Possibilities include `symbol', `list', `sexp', `defun',
| `filename', `url', `email', `word', `sentence', `whitespace',
| `line', `number', and `page'.
| 
| See the file `thingatpt.el' for documentation on how to define
| a symbol as a valid THING.
| 
| [back]
`----


-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
       [not found]         ` <87y4tsts4t.fsf@gmail.com>
@ 2014-09-09 22:31           ` Emanuel Berg
  2014-09-10  0:49             ` Robert Thorpe
  2014-09-10  1:21             ` Ken
  0 siblings, 2 replies; 29+ messages in thread
From: Emanuel Berg @ 2014-09-09 22:31 UTC (permalink / raw)
  To: help-gnu-emacs

Ken <kensubuntu@gmail.com> writes:

> OK, I will try changing it to let, but I don't think
> it will improve the functionality. :-)

Well, perhaps not in isolation. The advantage of let is
rather when you have several functions (even hundreds),
all available at the same time, at the base level. And,
it might be wise to practice good form from the very
start with Elisp.

PS. Note how you reply to my posts - reply to the
    list/newsgroup, not to my e-mail. DS.

-- 
underground experts united


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
       [not found]   ` <mailman.8562.1410296811.1147.help-gnu-emacs@gnu.org>
@ 2014-09-09 23:02     ` Emanuel Berg
  0 siblings, 0 replies; 29+ messages in thread
From: Emanuel Berg @ 2014-09-09 23:02 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> (defun process-diary-file () ...

Yeah, +1 for thing-at-point, very useful - but, that
function opens a file and echoes the first symbol. If
that is the purpose (?) of the function I think it
should close the buffer (if opened), and it should
save-excursion if there was such a buffer opened - and
the name/docstring should be changed to reflect the
purpose of the defun.

We can discuss programming all night long but it makes
more sense if we do it in the context of defuns that do
sensible things, not just for the sake of it...

let:

(let (a-var)
  (setq a-var 5)
  (message "%s" a-var) ) ; "5"

;; I think this is better:
(let ((a-var 5))
  ;; here, do something else that involves a-var
  (message "%s" a-var) ) ; "5"

(message "%s" a-var)     ; error, no a-var (good)

-- 
underground experts united


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-09 22:31           ` Emanuel Berg
@ 2014-09-10  0:49             ` Robert Thorpe
  2014-09-10  1:21             ` Ken
  1 sibling, 0 replies; 29+ messages in thread
From: Robert Thorpe @ 2014-09-10  0:49 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> Ken <kensubuntu@gmail.com> writes:
>
>> OK, I will try changing it to let, but I don't think
>> it will improve the functionality. :-)
>
> Well, perhaps not in isolation. The advantage of let is
> rather when you have several functions (even hundreds),
> all available at the same time, at the base level. And,
> it might be wise to practice good form from the very
> start with Elisp.

Yes.  It's also useful if the code could be running in two different
buffers at once.  By default variables created with setq on it's own
have global scope, so if the code is running in two different buffers
things will get confused.

A useful half-way house is to make variables buffer local using
make-local-variable.

BR,
Robert Thorpe



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-09 22:31           ` Emanuel Berg
  2014-09-10  0:49             ` Robert Thorpe
@ 2014-09-10  1:21             ` Ken
  2014-09-10  6:00               ` Glyn Millington
  1 sibling, 1 reply; 29+ messages in thread
From: Ken @ 2014-09-10  1:21 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> PS. Note how you reply to my posts - reply to the
>     list/newsgroup, not to my e-mail. DS.

Sorry about that. I use gnus and I haven't trained my fingers to use F
rather than R yet.

Ken

--
The difference between a Miracle and a Fact is exactly the difference
between a mermaid and a seal.
		-- Mark Twain



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10  1:21             ` Ken
@ 2014-09-10  6:00               ` Glyn Millington
  2014-09-10 14:31                 ` Ken
  0 siblings, 1 reply; 29+ messages in thread
From: Glyn Millington @ 2014-09-10  6:00 UTC (permalink / raw)
  To: help-gnu-emacs

Ken <kensubuntu@gmail.com> writes:

> Emanuel Berg <embe8573@student.uu.se> writes:
>
>> PS. Note how you reply to my posts - reply to the
>>     list/newsgroup, not to my e-mail. DS.
>
> Sorry about that. I use gnus and I haven't trained my fingers to use F
> rather than R yet.
>
> Ken
>
> --
> The difference between a Miracle and a Fact is exactly the difference
> between a mermaid and a seal.
> 		-- Mark Twain

Hi Ken,

Bung this snippet into your .gnus file. Gnus will then ask you if you
REALLY want to send an email when you are in a news group.



;; prompt for mail replies to newsgroups!!!!!
(defadvice gnus-summary-reply (around reply-in-news activate)
  (interactive)
  (when (or (not (gnus-news-group-p gnus-newsgroup-name))
	    (y-or-n-p "REALLY reply with a personal mail? "))
    ad-do-it))

atb

Glyn




^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-09 19:35 Basic emacs lisp question Ken
  2014-09-09 19:52 ` Thorsten Jolitz
       [not found] ` <mailman.8561.1410292382.1147.help-gnu-emacs@gnu.org>
@ 2014-09-10 13:12 ` Phillip Lord
  2014-09-10 14:06   ` Thien-Thi Nguyen
                     ` (2 more replies)
  2 siblings, 3 replies; 29+ messages in thread
From: Phillip Lord @ 2014-09-10 13:12 UTC (permalink / raw)
  To: Ken; +Cc: help-gnu-emacs

Ken <kensubuntu@gmail.com> writes:

> I want to capture whatever is at point in a file into a variable
> something like the following, but it doesn't seem to work. Can any one
> suggest what I an doing wrong. It is probably a silly mistake I am
> unable to see. I am just learning Emacs lisp.
>
> (defun process-diary-file ()
>   "Perform some manipulation of the diary file"
>   (interactive)
>   (find-file "~/diary")
>   (goto-char 1)
>   (set a (thing-at-point))
>   (message a))

Immediately solution is let. 

(defun process-diary-file ()
  "Perform some manipulation of the diary file"
  (interactive)
  (find-file "~/.signature")
  (goto-char 1)
  (let ((a (thing-at-point 'word)))
    (message a)))

Nicer solution is to not use a variable at all which works in this case.

(defun process-diary-file ()
  "Perform some manipulation of the diary file"
  (interactive)
  (find-file "~/.signature")
  (goto-char 1)
  (message
   (thing-at-point 'word)))

Do you want to capture the value into some *existing* variable a or is
it just for use in this function. In the former case you need setq
rather than set (let won't work!). However, if you can avoid or minimize
using global state in this way, it will make your life easier.

Phil





^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10 13:12 ` Phillip Lord
@ 2014-09-10 14:06   ` Thien-Thi Nguyen
  2014-09-10 14:26   ` Stefan Monnier
       [not found]   ` <mailman.8619.1410357764.1147.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 29+ messages in thread
From: Thien-Thi Nguyen @ 2014-09-10 14:06 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 818 bytes --]

() phillip.lord@newcastle.ac.uk (Phillip Lord)
() Wed, 10 Sep 2014 14:12:33 +0100

   (defun process-diary-file ()
     "Perform some manipulation of the diary file"
     (interactive)
     (find-file "~/.signature")
     (goto-char 1)
     (message
      (thing-at-point 'word)))

   [...] However, if you can avoid or minimize using
   global state in this way, it will make your life
   easier.

Another piece of global state is the list of buffers.
The command above has a side effect that you can avoid
by using ‘with-temp-buffer’ and ‘insert-file-contents’.

-- 
Thien-Thi Nguyen
   GPG key: 4C807502
   (if you're human and you know it)
      read my lisp: (responsep (questions 'technical)
                               (not (via 'mailing-list)))
                     => nil

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10 13:12 ` Phillip Lord
  2014-09-10 14:06   ` Thien-Thi Nguyen
@ 2014-09-10 14:26   ` Stefan Monnier
  2014-09-10 15:45     ` Phillip Lord
       [not found]   ` <mailman.8619.1410357764.1147.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 29+ messages in thread
From: Stefan Monnier @ 2014-09-10 14:26 UTC (permalink / raw)
  To: help-gnu-emacs

>   (let ((a (thing-at-point 'word)))
>     (message a)))

And if the word at point includes % you'll get an error.
*Always* pass a format string to `message':

   (message "%s" (thing-at-point 'word))))


-- Stefan




^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10  6:00               ` Glyn Millington
@ 2014-09-10 14:31                 ` Ken
  0 siblings, 0 replies; 29+ messages in thread
From: Ken @ 2014-09-10 14:31 UTC (permalink / raw)
  To: glyn.millington; +Cc: help-gnu-emacs

Glyn Millington <glyn.millington@gmail.com> writes:

>
> Hi Ken,
>
> Bung this snippet into your .gnus file. Gnus will then ask you if you
> REALLY want to send an email when you are in a news group.
>
>
>
> ;; prompt for mail replies to newsgroups!!!!!
> (defadvice gnus-summary-reply (around reply-in-news activate)
>   (interactive)
>   (when (or (not (gnus-news-group-p gnus-newsgroup-name))
> 	    (y-or-n-p "REALLY reply with a personal mail? "))
>     ad-do-it))
>
Thanks, Glyn. It's added so it should fix that problem. :-)

Ken


-- 
Q:	How do you catch a unique rabbit?
A:	Unique up on it!

Q:	How do you catch a tame rabbit?
A:	The tame way!



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10 14:26   ` Stefan Monnier
@ 2014-09-10 15:45     ` Phillip Lord
  2014-09-10 16:59       ` Stefan Monnier
  0 siblings, 1 reply; 29+ messages in thread
From: Phillip Lord @ 2014-09-10 15:45 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>   (let ((a (thing-at-point 'word)))
>>     (message a)))
>
> And if the word at point includes % you'll get an error.
> *Always* pass a format string to `message':
>
>    (message "%s" (thing-at-point 'word))))


I *think* (thing-at-point 'word) doesn't normally return a %, at least
not from .signature, so I'm in the clear!

But, generally, yes, it's a good call.

Phil




^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10 15:45     ` Phillip Lord
@ 2014-09-10 16:59       ` Stefan Monnier
  2014-09-11 11:10         ` Phillip Lord
  2014-09-11 11:10         ` Phillip Lord
  0 siblings, 2 replies; 29+ messages in thread
From: Stefan Monnier @ 2014-09-10 16:59 UTC (permalink / raw)
  To: Phillip Lord; +Cc: help-gnu-emacs

>>> (let ((a (thing-at-point 'word)))
>>>   (message a)))
>> And if the word at point includes % you'll get an error.
>> *Always* pass a format string to `message':
>> (message "%s" (thing-at-point 'word))))
> I *think* (thing-at-point 'word) doesn't normally return a %, at least
> not from .signature, so I'm in the clear!

Might be the case, but why think when you can just use "%s" is move on?


        Stefan



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
       [not found]   ` <mailman.8619.1410357764.1147.help-gnu-emacs@gnu.org>
@ 2014-09-10 21:39     ` Emanuel Berg
  0 siblings, 0 replies; 29+ messages in thread
From: Emanuel Berg @ 2014-09-10 21:39 UTC (permalink / raw)
  To: help-gnu-emacs

Thien-Thi Nguyen <ttn@gnu.org> writes:

> Another piece of global state is the list of buffers.

Good point.

-- 
underground experts united


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10 16:59       ` Stefan Monnier
@ 2014-09-11 11:10         ` Phillip Lord
  2014-09-11 11:10         ` Phillip Lord
  1 sibling, 0 replies; 29+ messages in thread
From: Phillip Lord @ 2014-09-11 11:10 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


-- 
Phillip Lord,                           Phone: +44 (0) 191 222 7827
Lecturer in Bioinformatics,             Email: phillip.lord@newcastle.ac.uk
School of Computing Science,            http://homepages.cs.ncl.ac.uk/phillip.lord
Room 914 Claremont Tower,               skype: russet_apples
Newcastle University,                   twitter: phillord
NE1 7RU                                 



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: Basic emacs lisp question
  2014-09-10 16:59       ` Stefan Monnier
  2014-09-11 11:10         ` Phillip Lord
@ 2014-09-11 11:10         ` Phillip Lord
  1 sibling, 0 replies; 29+ messages in thread
From: Phillip Lord @ 2014-09-11 11:10 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>>> (let ((a (thing-at-point 'word)))
>>>>   (message a)))
>>> And if the word at point includes % you'll get an error.
>>> *Always* pass a format string to `message':
>>> (message "%s" (thing-at-point 'word))))
>> I *think* (thing-at-point 'word) doesn't normally return a %, at least
>> not from .signature, so I'm in the clear!
>
> Might be the case, but why think when you can just use "%s" is move on?


Can't argue with that. Spend too much of my life thinking, so less is
welcome.

Phil



^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2014-09-11 11:10 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-09 19:35 Basic emacs lisp question Ken
2014-09-09 19:52 ` Thorsten Jolitz
2014-09-09 21:05   ` Thorsten Jolitz
     [not found]   ` <mailman.8562.1410296811.1147.help-gnu-emacs@gnu.org>
2014-09-09 23:02     ` Emanuel Berg
     [not found] ` <mailman.8561.1410292382.1147.help-gnu-emacs@gnu.org>
2014-09-09 20:07   ` Emanuel Berg
     [not found]     ` <874mwgv93m.fsf@gmail.com>
2014-09-09 20:44       ` Emanuel Berg
     [not found]         ` <87y4tsts4t.fsf@gmail.com>
2014-09-09 22:31           ` Emanuel Berg
2014-09-10  0:49             ` Robert Thorpe
2014-09-10  1:21             ` Ken
2014-09-10  6:00               ` Glyn Millington
2014-09-10 14:31                 ` Ken
2014-09-10 13:12 ` Phillip Lord
2014-09-10 14:06   ` Thien-Thi Nguyen
2014-09-10 14:26   ` Stefan Monnier
2014-09-10 15:45     ` Phillip Lord
2014-09-10 16:59       ` Stefan Monnier
2014-09-11 11:10         ` Phillip Lord
2014-09-11 11:10         ` Phillip Lord
     [not found]   ` <mailman.8619.1410357764.1147.help-gnu-emacs@gnu.org>
2014-09-10 21:39     ` Emanuel Berg
  -- strict thread matches above, loose matches on Subject: below --
2008-04-29 15:49 Basic Emacs Lisp question Matthias Pfeifer
2008-04-29 17:56 ` Giorgos Keramidas
2008-04-30  1:21   ` Giorgos Keramidas
2008-04-30  8:37     ` Tim X
2008-04-30 15:26   ` David Kastrup
2008-04-30 18:36     ` Giorgos Keramidas
2008-05-01  9:25       ` Johan Bockgård
2008-04-29 21:14 ` tyler
2008-04-30  8:31   ` Tim X
2008-05-05 13:38     ` tyler

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).