* Why didn't setq work here?
@ 2011-01-07 2:05 Eric Lilja
2011-01-07 3:40 ` Deniz Dogan
0 siblings, 1 reply; 10+ messages in thread
From: Eric Lilja @ 2011-01-07 2:05 UTC (permalink / raw)
To: help-gnu-emacs
Hello, I'm running "GNU Emacs 24.0.50.1 (i386-mingw-nt6.1.7600) of
2011-01-03 on 3249CTO" (a build provided by Mr Sean Sieger) under
Windows 7 x64.
I don't really know how to configure emacs so please excuse any
stupidity found below and improper use terminology.
For LaTeX buffers, I wanted to turn on the minor mode auto-fill-mode,
with a fill-column of 76.
After a bit of investigating, I found that I could run "commands" (if
that's what they're called) when major mode latex is loaded/activated
for a buffer by using hooks, like this:
(defun my-latex-hook ()
(message "Running my-latex-hook")
)
(add-hook 'latex-mode-hook 'my-latex-hook)
I added the statement above and launched emacs like this:
$ emacs foo.tex&
and I could see from the message output that my hook was indeed working.
Good. Just what I wanted.
So now I just need to turn on the minor mode auto-fill-mode with
fill-column set to 76 when this hook is "executed".
In my naivety, I tried this:
(defun my-latex-hook ()
(message "Running my-latex-hook")
(setq auto-fill-mode 1)
(setq fill-column 76)
)
(add-hook 'latex-mode-hook 'my-latex-hook)
I saw a warning when I byte-compiled my .emacs:
In my-latex-hook:
.emacs:42:9:Warning: assignment to free variable `auto-fill-mode'
but tried anyway. Closed emacs and restarted as I did above ($ emacs
foo.tex&)
I noticed straight away that the minor mode auto-fill-mode had not been
turned on.
However, C-h v fill-column showed that my fill-column had been set
though since its value was now the desired 76 instead of the default 70.
In order to solve the problem of auto-fill-mode not being turned on and
getting rid of the byte compiler warning, I rewrote my hook like this:
(defun my-latex-hook ()
(message "Running my-latex-hook")
(auto-fill-mode 1)
(set-fill-column 76)
)
(add-hook 'latex-mode-hook 'my-latex-hook)
Now auto-fill-mode is indeed turned on and I don't get a warning by the
byte compiler when I set fill column. Ok, so I am happy. Problem solved! :)
But here's my question: Why didn't setq work as I thought it would? By
not work I meant I the minor mode auto-fill-mode was not turned on and
even though the fill column was set as I desired, the statement produced
a warning as I showed above.
Thanks for any replies! :)
- EL
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
[not found] <mailman.23.1294365997.1330.help-gnu-emacs@gnu.org>
@ 2011-01-07 2:54 ` Pascal J. Bourguignon
2011-01-07 3:50 ` rusi
1 sibling, 0 replies; 10+ messages in thread
From: Pascal J. Bourguignon @ 2011-01-07 2:54 UTC (permalink / raw)
To: help-gnu-emacs
Eric Lilja <mindcooler@gmail.com> writes:
> But here's my question: Why didn't setq work as I thought it would? By
> not work I meant I the minor mode auto-fill-mode was not turned on and
> even though the fill column was set as I desired, the statement
> produced a warning as I showed above.
I have quite a difficulty in trying to explain you.
Do you know the difference between a variable and a function?
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
2011-01-07 2:05 Why didn't setq work here? Eric Lilja
@ 2011-01-07 3:40 ` Deniz Dogan
2011-01-07 12:43 ` Eric Lilja
0 siblings, 1 reply; 10+ messages in thread
From: Deniz Dogan @ 2011-01-07 3:40 UTC (permalink / raw)
To: Eric Lilja; +Cc: help-gnu-emacs
2011/1/7 Eric Lilja <mindcooler@gmail.com>:
> Hello, I'm running "GNU Emacs 24.0.50.1 (i386-mingw-nt6.1.7600) of
> 2011-01-03 on 3249CTO" (a build provided by Mr Sean Sieger) under Windows 7
> x64.
>
> I don't really know how to configure emacs so please excuse any stupidity
> found below and improper use terminology.
>
> For LaTeX buffers, I wanted to turn on the minor mode auto-fill-mode, with a
> fill-column of 76.
>
> After a bit of investigating, I found that I could run "commands" (if that's
> what they're called) when major mode latex is loaded/activated for a buffer
> by using hooks, like this:
>
> (defun my-latex-hook ()
> (message "Running my-latex-hook")
> )
> (add-hook 'latex-mode-hook 'my-latex-hook)
>
> I added the statement above and launched emacs like this:
> $ emacs foo.tex&
> and I could see from the message output that my hook was indeed working.
> Good. Just what I wanted.
>
> So now I just need to turn on the minor mode auto-fill-mode with fill-column
> set to 76 when this hook is "executed".
>
> In my naivety, I tried this:
>
> (defun my-latex-hook ()
> (message "Running my-latex-hook")
> (setq auto-fill-mode 1)
> (setq fill-column 76)
> )
> (add-hook 'latex-mode-hook 'my-latex-hook)
>
> I saw a warning when I byte-compiled my .emacs:
> In my-latex-hook:
> .emacs:42:9:Warning: assignment to free variable `auto-fill-mode'
>
> but tried anyway. Closed emacs and restarted as I did above ($ emacs
> foo.tex&)
>
> I noticed straight away that the minor mode auto-fill-mode had not been
> turned on.
> However, C-h v fill-column showed that my fill-column had been set though
> since its value was now the desired 76 instead of the default 70.
>
> In order to solve the problem of auto-fill-mode not being turned on and
> getting rid of the byte compiler warning, I rewrote my hook like this:
>
> (defun my-latex-hook ()
> (message "Running my-latex-hook")
> (auto-fill-mode 1)
> (set-fill-column 76)
> )
> (add-hook 'latex-mode-hook 'my-latex-hook)
>
> Now auto-fill-mode is indeed turned on and I don't get a warning by the byte
> compiler when I set fill column. Ok, so I am happy. Problem solved! :)
>
> But here's my question: Why didn't setq work as I thought it would? By not
> work I meant I the minor mode auto-fill-mode was not turned on and even
> though the fill column was set as I desired, the statement produced a
> warning as I showed above.
>
> Thanks for any replies! :)
>
> - EL
>
>
>
`setq' only sets the value of a variable. The variable you tried to
set, auto-fill-mode, has not been defined -- it doesn't "exist" yet.
The warning about a "free variable" in this context means that the
variable is not defined in the current scope.
The correct way to turn auto-fill-mode on or off is to call the
function with the same name, which you figured out later. The
_variable_ auto-fill-mode doesn't exist in this case, though in many
other modes there is a variable named identically to the function to
turn it on or off. In some modes where that is the case, setting the
variable has effect. In some other modes, it doesn't.
By the way, in Emacs a function is a function and a "command" is a
function that can be called interactively, i.e. using e.g. M-x.
I hope that makes things a bit clearer.
--
Deniz Dogan
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
[not found] <mailman.23.1294365997.1330.help-gnu-emacs@gnu.org>
2011-01-07 2:54 ` Pascal J. Bourguignon
@ 2011-01-07 3:50 ` rusi
1 sibling, 0 replies; 10+ messages in thread
From: rusi @ 2011-01-07 3:50 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 7, 7:05 am, Eric Lilja <mindcoo...@gmail.com> wrote:
> Hello, I'm running "GNU Emacs 24.0.50.1 (i386-mingw-nt6.1.7600) of
> 2011-01-03 on 3249CTO" (a build provided by Mr Sean Sieger) under
> Windows 7 x64.
>
> I don't really know how to configure emacs so please excuse any
> stupidity found below and improper use terminology.
Newbies cant be oldbies and are welcome!
> I noticed straight away that the minor mode auto-fill-mode had not been
> turned on.
> Now auto-fill-mode is indeed turned on and I don't get a warning by the
> byte compiler when I set fill column. Ok, so I am happy. Problem solved! :)
>
> But here's my question: Why didn't setq work as I thought it would? By
> not work I meant I the minor mode auto-fill-mode was not turned on and
> even though the fill column was set as I desired, the statement produced
> a warning as I showed above.
This is a standard newbie error see
http://en.wikipedia.org/wiki/Lisp-1_vs._Lisp-2#The_function_namespace.
[Only addition to above is: elisp follows (somewhat buggily) the
common lisp model]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
2011-01-07 3:40 ` Deniz Dogan
@ 2011-01-07 12:43 ` Eric Lilja
2011-01-07 14:26 ` Tassilo Horn
0 siblings, 1 reply; 10+ messages in thread
From: Eric Lilja @ 2011-01-07 12:43 UTC (permalink / raw)
To: help-gnu-emacs
On 2011-01-07 04:40, Deniz Dogan wrote:
> 2011/1/7 Eric Lilja<mindcooler@gmail.com>:
>> Hello, I'm running "GNU Emacs 24.0.50.1 (i386-mingw-nt6.1.7600) of
>> 2011-01-03 on 3249CTO" (a build provided by Mr Sean Sieger) under Windows 7
>> x64.
>>
>> I don't really know how to configure emacs so please excuse any stupidity
>> found below and improper use terminology.
>>
>> For LaTeX buffers, I wanted to turn on the minor mode auto-fill-mode, with a
>> fill-column of 76.
>>
>> After a bit of investigating, I found that I could run "commands" (if that's
>> what they're called) when major mode latex is loaded/activated for a buffer
>> by using hooks, like this:
>>
>> (defun my-latex-hook ()
>> (message "Running my-latex-hook")
>> )
>> (add-hook 'latex-mode-hook 'my-latex-hook)
>>
>> I added the statement above and launched emacs like this:
>> $ emacs foo.tex&
>> and I could see from the message output that my hook was indeed working.
>> Good. Just what I wanted.
>>
>> So now I just need to turn on the minor mode auto-fill-mode with fill-column
>> set to 76 when this hook is "executed".
>>
>> In my naivety, I tried this:
>>
>> (defun my-latex-hook ()
>> (message "Running my-latex-hook")
>> (setq auto-fill-mode 1)
>> (setq fill-column 76)
>> )
>> (add-hook 'latex-mode-hook 'my-latex-hook)
>>
>> I saw a warning when I byte-compiled my .emacs:
>> In my-latex-hook:
>> .emacs:42:9:Warning: assignment to free variable `auto-fill-mode'
>>
>> but tried anyway. Closed emacs and restarted as I did above ($ emacs
>> foo.tex&)
>>
>> I noticed straight away that the minor mode auto-fill-mode had not been
>> turned on.
>> However, C-h v fill-column showed that my fill-column had been set though
>> since its value was now the desired 76 instead of the default 70.
>>
>> In order to solve the problem of auto-fill-mode not being turned on and
>> getting rid of the byte compiler warning, I rewrote my hook like this:
>>
>> (defun my-latex-hook ()
>> (message "Running my-latex-hook")
>> (auto-fill-mode 1)
>> (set-fill-column 76)
>> )
>> (add-hook 'latex-mode-hook 'my-latex-hook)
>>
>> Now auto-fill-mode is indeed turned on and I don't get a warning by the byte
>> compiler when I set fill column. Ok, so I am happy. Problem solved! :)
>>
>> But here's my question: Why didn't setq work as I thought it would? By not
>> work I meant I the minor mode auto-fill-mode was not turned on and even
>> though the fill column was set as I desired, the statement produced a
>> warning as I showed above.
>>
>> Thanks for any replies! :)
>>
>> - EL
>>
>>
>>
>
> `setq' only sets the value of a variable. The variable you tried to
> set, auto-fill-mode, has not been defined -- it doesn't "exist" yet.
> The warning about a "free variable" in this context means that the
> variable is not defined in the current scope.
>
> The correct way to turn auto-fill-mode on or off is to call the
> function with the same name, which you figured out later. The
> _variable_ auto-fill-mode doesn't exist in this case, though in many
> other modes there is a variable named identically to the function to
> turn it on or off. In some modes where that is the case, setting the
> variable has effect. In some other modes, it doesn't.
>
> By the way, in Emacs a function is a function and a "command" is a
> function that can be called interactively, i.e. using e.g. M-x.
>
> I hope that makes things a bit clearer.
>
> --
> Deniz Dogan
>
>
Hello Mr Dogan, and thank you for your polite and informative reply.
Using your information, I added the following comment to my .emacs:
(defun my-latex-hook ()
(message "Running my-latex-hook")
;; Using setq does not work, because setq is for setting values of
;; variables and there is no variable called auto-fill-mode (as can
;; be observed by performing C-h v auto-fill-mode). The correct way
;; to turn on auto-fill-mode is to call the funcion auto-fill-mode
;; with a positive argument. In some modes there exists a variable
;; with the same name as the function which disables or enables that
;; mode (a variable that can be used for the same purpose), but
;; auto-fill-mode is not one of those modes.
(auto-fill-mode 1)
(set-fill-column 76)
)
(add-hook 'latex-mode-hook 'my-latex-hook)
I have one final question though. fill-column does seem to be a
variable. It can be described using C-h v and set using M-x
set-variable. However, if I replace (set-fill-column 76) with (setq
fill-column 76) the variable value is never changed (and no byte
compiler warning btw). I see now that I was mistaken in my first post,
btw, I thought that using setq worked for fill-column but produced a
warning, but this is not the case. It didn't work and the byte compiler
warning was for auto-fill-mode.
So my final question, before I can lay this entire topic to rest, is:
why didn't setq work for fill-column, which seems to satisfy the
requirements of being a variable?
Thanks!
- EL
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
2011-01-07 12:43 ` Eric Lilja
@ 2011-01-07 14:26 ` Tassilo Horn
2011-01-07 16:05 ` Eric Lilja
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Tassilo Horn @ 2011-01-07 14:26 UTC (permalink / raw)
To: help-gnu-emacs
Eric Lilja <mindcooler@gmail.com> writes:
> I have one final question though. fill-column does seem to be a
> variable.
It is.
> It can be described using C-h v and set using M-x
> set-variable. However, if I replace (set-fill-column 76) with (setq
> fill-column 76) the variable value is never changed (and no byte
> compiler warning btw).
I presume, your observation is false. In fact, `set-fill-column' does
nothing except reading its argument and then doing (setq fill-column
arg) itself.
Bye,
Tassilo
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
2011-01-07 14:26 ` Tassilo Horn
@ 2011-01-07 16:05 ` Eric Lilja
2011-01-07 16:59 ` Peter Dyballa
[not found] ` <mailman.6.1294416372.29236.help-gnu-emacs@gnu.org>
2 siblings, 0 replies; 10+ messages in thread
From: Eric Lilja @ 2011-01-07 16:05 UTC (permalink / raw)
To: help-gnu-emacs
On 2011-01-07 15:26, Tassilo Horn wrote:
> Eric Lilja<mindcooler@gmail.com> writes:
>
>> I have one final question though. fill-column does seem to be a
>> variable.
>
> It is.
>
>> It can be described using C-h v and set using M-x
>> set-variable. However, if I replace (set-fill-column 76) with (setq
>> fill-column 76) the variable value is never changed (and no byte
>> compiler warning btw).
>
> I presume, your observation is false. In fact, `set-fill-column' does
> nothing except reading its argument and then doing (setq fill-column
> arg) itself.
>
> Bye,
> Tassilo
>
>
>
Thanks for your reply Mr Horn. Either my mind is playing tricks on me or
there is some stochastic error here which makes setq sometimes not
stick. I tried it ten times in a row and one time it claimed fill-column
was at the default value. Anyway, I documented my .emacs like the
following and I now consider this case closed. I learned quite a bit
about configuring emacs in this little thread so I am happy! I will look
out for set-fill-column not "sticking" as it should if my brain was
indeed not fooling me.
(defun my-latex-hook ()
(message "Running my-latex-hook")
;; Using setq does not work, because setq is for setting values of
;; variables and there is no variable called auto-fill-mode (as can
;; be observed by performing C-h v auto-fill-mode). The correct way
;; to turn on auto-fill-mode is to call the funcion auto-fill-mode
;; with a positive argument. In some modes there exists a variable
;; with the same name as the function which disables or enables that
;; mode (a variable that can be used for the same purpose), but
;; auto-fill-mode is not one of those modes.
(auto-fill-mode 1)
;; fill-column is a variable so we can use setq on it. However, we
;; have decided to use the function set-fill-column which performs
;; setq fill-column itself but also displays in the messages buffer
;; what it's doing.
;; Sometimes it seemed like setq fill-column did not take effect
;; as it should but it could just be my imagination...
(set-fill-column 76)
)
(add-hook 'latex-mode-hook 'my-latex-hook)
- EL
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
2011-01-07 14:26 ` Tassilo Horn
2011-01-07 16:05 ` Eric Lilja
@ 2011-01-07 16:59 ` Peter Dyballa
2011-01-08 17:29 ` Tassilo Horn
[not found] ` <mailman.6.1294416372.29236.help-gnu-emacs@gnu.org>
2 siblings, 1 reply; 10+ messages in thread
From: Peter Dyballa @ 2011-01-07 16:59 UTC (permalink / raw)
To: Tassilo Horn, Eric Lilja; +Cc: help-gnu-emacs
Am 07.01.2011 um 15:26 schrieb Tassilo Horn:
> I presume, your observation is false. In fact, `set-fill-column' does
> nothing except reading its argument and then doing (setq fill-column
> arg) itself.
And isn't fill-column also buffer-local, when changed?
--
Greetings
Pete
Almost anything is easier to get into than out of.
– Allen's Law
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
[not found] ` <mailman.6.1294416372.29236.help-gnu-emacs@gnu.org>
@ 2011-01-07 22:50 ` Tim X
0 siblings, 0 replies; 10+ messages in thread
From: Tim X @ 2011-01-07 22:50 UTC (permalink / raw)
To: help-gnu-emacs
Eric Lilja <mindcooler@gmail.com> writes:
> On 2011-01-07 15:26, Tassilo Horn wrote:
>> Eric Lilja<mindcooler@gmail.com> writes:
>>
>>> I have one final question though. fill-column does seem to be a
>>> variable.
>>
>> It is.
>>
>>> It can be described using C-h v and set using M-x
>>> set-variable. However, if I replace (set-fill-column 76) with (setq
>>> fill-column 76) the variable value is never changed (and no byte
>>> compiler warning btw).
>>
>> I presume, your observation is false. In fact, `set-fill-column' does
>> nothing except reading its argument and then doing (setq fill-column
>> arg) itself.
>>
>> Bye,
>> Tassilo
>>
>>
>>
>
> Thanks for your reply Mr Horn. Either my mind is playing tricks on me or there
> is some stochastic error here which makes setq sometimes not stick. I tried it
> ten times in a row and one time it claimed fill-column was at the default
> value. Anyway, I documented my .emacs like the following and I now consider
> this case closed. I learned quite a bit about configuring emacs in this little
> thread so I am happy! I will look out for set-fill-column not "sticking" as it
> should if my brain was indeed not fooling me.
>
Something to be aware of is that emacs has what are called buffer local
variables, that behave a little differently from normal global
variables.
Essentially, a buffer will use the global value for a buffer local
variable until that variable is modified/changed. From that point
onwards, the buffer will have its own copy of that variable with a new
value and will use that value instead of the global value.
What this means in practice is that the value you see for a buffer local
variable may be different depending on what buffer you are in when you
examine the value. So, if you are changing the value of fill-column,
which is a buffer local variable, in a specific mode via a mode hook,
you need to make sure you are in a buffer of that mode before you look
at the value of fill-column to see what it is now set to. This could
possibly explain why you observed what appeared to be inconsistent
behavior. Note also that if you use a function to set a buffer local
variable, you need to ensure it is associated with the appropriate
buffer, either via a mode hook or by switching to that buffer and doing
it via the interactive form of set variable.
HTH
Tim
P.S. Newer versions of emacs come with "An Introduction to Emacs Lisp"
by Robert J. Chassell (normally in info format). This is an excellent
starting point on emacs lisp and is not too taxing - very gentle in
fact. You can also get it on-line via the FSF website. I would strongly
recommend a quick read. It will clarify some of the terminology and key
concepts, saving you lots of time in the long run.
--
tcross (at) rapttech dot com dot au
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Why didn't setq work here?
2011-01-07 16:59 ` Peter Dyballa
@ 2011-01-08 17:29 ` Tassilo Horn
0 siblings, 0 replies; 10+ messages in thread
From: Tassilo Horn @ 2011-01-08 17:29 UTC (permalink / raw)
To: Peter Dyballa; +Cc: Eric Lilja, help-gnu-emacs
Peter Dyballa <Peter_Dyballa@Web.DE> writes:
>> I presume, your observation is false. In fact, `set-fill-column'
>> does nothing except reading its argument and then doing (setq
>> fill-column arg) itself.
>
> And isn't fill-column also buffer-local, when changed?
Yes, it is. Ah, you mean Eric might have checked the value in another
buffer, and there he saw the old default value and so thought setq
didn't work? That might be.
Bye,
Tassilo
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2011-01-08 17:29 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-07 2:05 Why didn't setq work here? Eric Lilja
2011-01-07 3:40 ` Deniz Dogan
2011-01-07 12:43 ` Eric Lilja
2011-01-07 14:26 ` Tassilo Horn
2011-01-07 16:05 ` Eric Lilja
2011-01-07 16:59 ` Peter Dyballa
2011-01-08 17:29 ` Tassilo Horn
[not found] ` <mailman.6.1294416372.29236.help-gnu-emacs@gnu.org>
2011-01-07 22:50 ` Tim X
[not found] <mailman.23.1294365997.1330.help-gnu-emacs@gnu.org>
2011-01-07 2:54 ` Pascal J. Bourguignon
2011-01-07 3:50 ` rusi
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).