unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how to use "if" in lisp
@ 2007-11-15 18:01 Teng Wang
  2007-11-15 18:08 ` Mirko
  2007-11-15 18:30 ` Joel J. Adamson
  0 siblings, 2 replies; 11+ messages in thread
From: Teng Wang @ 2007-11-15 18:01 UTC (permalink / raw)
  To: help-gnu-emacs


Hi there,

I just began to learn lisp a couple of weeks ago. It is easy to use,
but sometimes it is really hard to know what variables are in
lisp. And today I want to write a code doing the function as below:

(if (_the major mode is something_)
    (some function)
    (another funcion))

But I don't know how to write down the condition. 

Any suggestions. Thank you all in advance.

---

Teng 

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

* Re: how to use "if" in lisp
  2007-11-15 18:01 how to use "if" in lisp Teng Wang
@ 2007-11-15 18:08 ` Mirko
  2007-11-15 18:30 ` Joel J. Adamson
  1 sibling, 0 replies; 11+ messages in thread
From: Mirko @ 2007-11-15 18:08 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 15, 1:01 pm, Teng Wang <twang....@gmail.com> wrote:
> Hi there,
>
> I just began to learn lisp a couple of weeks ago. It is easy to use,
> but sometimes it is really hard to know what variables are in
> lisp. And today I want to write a code doing the function as below:
>
> (if (_the major mode is something_)
>     (some function)
>     (another funcion))
>
> But I don't know how to write down the condition.
>
> Any suggestions. Thank you all in advance.
>
> ---
>
> Teng

The variable mode-name contains the name of the major mode (as a
string)

Mirko

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

* Re: how to use "if" in lisp
  2007-11-15 18:01 how to use "if" in lisp Teng Wang
  2007-11-15 18:08 ` Mirko
@ 2007-11-15 18:30 ` Joel J. Adamson
  2007-11-15 18:37   ` Joel J. Adamson
  1 sibling, 1 reply; 11+ messages in thread
From: Joel J. Adamson @ 2007-11-15 18:30 UTC (permalink / raw)
  To: help-gnu-emacs

Teng Wang <twang.umn@gmail.com> writes:

> Hi there,
>
> I just began to learn lisp a couple of weeks ago. It is easy to use,
> but sometimes it is really hard to know what variables are in
> lisp. And today I want to write a code doing the function as below:
>
> (if (_the major mode is something_)
>     (some function)
>     (another funcion))
>
> But I don't know how to write down the condition. 

Since major-mode is local to a buffer, your function should be local to
the buffer.

(symbol-value 'major-mode)

gets the major mode, but there may be a simpler way (i.e. a function
specifically for that).  Read up on symbols and plists.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

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

* Re: how to use "if" in lisp
  2007-11-15 18:30 ` Joel J. Adamson
@ 2007-11-15 18:37   ` Joel J. Adamson
  2007-11-15 19:05     ` Teng Wang
  2007-11-15 19:32     ` Sebastian Tennant
  0 siblings, 2 replies; 11+ messages in thread
From: Joel J. Adamson @ 2007-11-15 18:37 UTC (permalink / raw)
  To: help-gnu-emacs

jadamson@partners.org (Joel J. Adamson) writes:

> Teng Wang <twang.umn@gmail.com> writes:
>
>> Hi there,
>>
>> I just began to learn lisp a couple of weeks ago. It is easy to use,
>> but sometimes it is really hard to know what variables are in
>> lisp. And today I want to write a code doing the function as below:
>>
>> (if (_the major mode is something_)
>>     (some function)
>>     (another funcion))
>>
>> But I don't know how to write down the condition. 
>
> Since major-mode is local to a buffer, your function should be local to
> the buffer.

Sorry, that isn't clear: you may need to specify the buffer with
(current-buffer) or otherwise make sure you are dealing with the current
buffer, and to ensure that your function does not affect other buffers.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

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

* Re: how to use "if" in lisp
  2007-11-15 18:37   ` Joel J. Adamson
@ 2007-11-15 19:05     ` Teng Wang
  2007-11-15 19:16       ` Teng Wang
                         ` (3 more replies)
  2007-11-15 19:32     ` Sebastian Tennant
  1 sibling, 4 replies; 11+ messages in thread
From: Teng Wang @ 2007-11-15 19:05 UTC (permalink / raw)
  To: help-gnu-emacs

Thank you for your help.

I think about it for a while. I think my design is sort of
nonsense. Since .emacs only be loaded once when start up. When I
change the buffer, it cannot run again.

So now, I am wondering: is there any way to make it run on fly?

----

Teng Wang 

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

* Re: how to use "if" in lisp
  2007-11-15 19:05     ` Teng Wang
@ 2007-11-15 19:16       ` Teng Wang
  2007-11-16  1:04       ` Xah Lee
                         ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Teng Wang @ 2007-11-15 19:16 UTC (permalink / raw)
  To: help-gnu-emacs


Thank you for all your help.

I think I get a workaround.

I can just 
(add-hook '_somemode-hook_
          (my_function)
)

This will do my job.

---
Teng

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

* Re: how to use "if" in lisp
  2007-11-15 18:37   ` Joel J. Adamson
  2007-11-15 19:05     ` Teng Wang
@ 2007-11-15 19:32     ` Sebastian Tennant
  1 sibling, 0 replies; 11+ messages in thread
From: Sebastian Tennant @ 2007-11-15 19:32 UTC (permalink / raw)
  To: help-gnu-emacs

>>> (if (_the major mode is something_)
>>>     (some function)
>>>     (another funcion))
>>>
>>> But I don't know how to write down the condition. 

This works when evaluated in the scratch buffer:

  (if (equal major-mode 'lisp-interaction-mode)
      (message "yup")
    (message "nope"))

And you could wrap it in a:

 (with-current-buffer STRING-OR-FUNCTION-THAT-RETURNS-A-BUFFER-OR-BUFFER-NAME)

form if you like?

Sebastian

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

* Re: how to use "if" in lisp
  2007-11-15 19:05     ` Teng Wang
  2007-11-15 19:16       ` Teng Wang
@ 2007-11-16  1:04       ` Xah Lee
  2007-11-16 14:35       ` Joel J. Adamson
  2007-11-17  1:19       ` Tim X
  3 siblings, 0 replies; 11+ messages in thread
From: Xah Lee @ 2007-11-16  1:04 UTC (permalink / raw)
  To: help-gnu-emacs

not sure what you mean by "load on the fly". But you can load a elisp
code by a give file with

(load "~/.emacs")

For more emacs lisp lessons, see:

* Emacs Lisp Basics
http://xahlee.org/emacs/elisp_basics.html

* Simple ELisp Grab Bag
http://xahlee.org/emacs/elisp_examples.html

  Xah
  xah@xahlee.org
\xAD\xF4 http://xahlee.org/

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

* Re: how to use "if" in lisp
  2007-11-15 19:05     ` Teng Wang
  2007-11-15 19:16       ` Teng Wang
  2007-11-16  1:04       ` Xah Lee
@ 2007-11-16 14:35       ` Joel J. Adamson
  2007-11-17  1:19       ` Tim X
  3 siblings, 0 replies; 11+ messages in thread
From: Joel J. Adamson @ 2007-11-16 14:35 UTC (permalink / raw)
  To: help-gnu-emacs

Teng Wang <twang.umn@gmail.com> writes:

> Thank you for your help.
>
> I think about it for a while. I think my design is sort of
> nonsense. Since .emacs only be loaded once when start up. When I
> change the buffer, it cannot run again.
>
> So now, I am wondering: is there any way to make it run on fly?

(defun this-be-a-function ()
  (interactive)
  (message "This be an interactive function"))

The "(interactive)" in the second line makes it callable by name,
i.e. "M-x this-be-a-function" prints "This be an interactive function"
in the minibuffer.

The Introduction to Emacs Lisp and Elisp manuals are really helpful, and
easy to read.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

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

* Re: how to use "if" in lisp
  2007-11-15 19:05     ` Teng Wang
                         ` (2 preceding siblings ...)
  2007-11-16 14:35       ` Joel J. Adamson
@ 2007-11-17  1:19       ` Tim X
  2007-11-17  4:56         ` Teng Wang
  3 siblings, 1 reply; 11+ messages in thread
From: Tim X @ 2007-11-17  1:19 UTC (permalink / raw)
  To: help-gnu-emacs

Teng Wang <twang.umn@gmail.com> writes:

> Thank you for your help.
>
> I think about it for a while. I think my design is sort of
> nonsense. Since .emacs only be loaded once when start up. When I
> change the buffer, it cannot run again.
>
> So now, I am wondering: is there any way to make it run on fly?
>

Can I suggest you let us know what it is you want to do as then we can
possibly give you better alternative solutions. I'm guessing that what you
are really looking for is how to make some elisp run when you enter a
particular mode. If this is the case, what you are looking for is known in
emacs as a 'hook'. Many modes provide various hooks that are run a certain
times, such as wehn a mode is first loaded. Using hooks is one of the
standard ways to customize a mode.

Tim
-- 
tcross (at) rapttech dot com dot au

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

* Re: how to use "if" in lisp
  2007-11-17  1:19       ` Tim X
@ 2007-11-17  4:56         ` Teng Wang
  0 siblings, 0 replies; 11+ messages in thread
From: Teng Wang @ 2007-11-17  4:56 UTC (permalink / raw)
  To: help-gnu-emacs

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

> Teng Wang <twang.umn@gmail.com> writes:
>
>> Thank you for your help.
>>
>> I think about it for a while. I think my design is sort of
>> nonsense. Since .emacs only be loaded once when start up. When I
>> change the buffer, it cannot run again.
>>
>> So now, I am wondering: is there any way to make it run on fly?
>>
>
> Can I suggest you let us know what it is you want to do as then we can
> possibly give you better alternative solutions. I'm guessing that what you
> are really looking for is how to make some elisp run when you enter a
> particular mode. If this is the case, what you are looking for is known in
> emacs as a 'hook'. Many modes provide various hooks that are run a certain
> times, such as wehn a mode is first loaded. Using hooks is one of the
> standard ways to customize a mode.
Yes, I just want to run some function to particular mode, for example:
run abbrev-mode when entering LaTeX-mode. And yesterday I read the
manual again and I found there is LaTeX-mode-hook variable. So now, I
am happy.

But Thank you all the same.
>
> Tim


Best,

----

Teng

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

end of thread, other threads:[~2007-11-17  4:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-15 18:01 how to use "if" in lisp Teng Wang
2007-11-15 18:08 ` Mirko
2007-11-15 18:30 ` Joel J. Adamson
2007-11-15 18:37   ` Joel J. Adamson
2007-11-15 19:05     ` Teng Wang
2007-11-15 19:16       ` Teng Wang
2007-11-16  1:04       ` Xah Lee
2007-11-16 14:35       ` Joel J. Adamson
2007-11-17  1:19       ` Tim X
2007-11-17  4:56         ` Teng Wang
2007-11-15 19:32     ` Sebastian Tennant

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