What is the t letter at the end of code is for ?
like here:
(load-theme 'seti t)
and here:
(global-linum-mode t)
In Emacs Lisp, non-nil value is treated as true, however, t is the preferred way to represent the truth value true. Perhaps you might be got the same result if you put 1 or hello instead of t.
Perhaps you want to read 1.3.2 nil and t
Related
I'm trying to write an emacs LISP function to un-indent the region
(rigidly). I can pass prefix arguments to indent-code-rigidly or
indent-rigidly or indent-region and they all work fine, but I don't
want to always have to pass a negative prefix argument to shift things
left.
My current code is as below but it seems to do nothing:
(defun undent ()
"un-indent rigidly."
(interactive)
(list
(setq fline (line-number-at-pos (region-beginning)))
(setq lline (line-number-at-pos (region-end)))
(setq curIndent (current-indentation))
;;(indent-rigidly fline lline (- curIndent 1))
(indent-region fline lline 2)
;;(message "%d %d" curIndent (- curIndent 1))
)
)
I gather that (current-indentation) won't get me the indentation of the first line
of the region, but of the first line following the region (so a second quesiton is
how to get that!). But even when I just use a constant for the column (as shown,
I don't see this function do any change.
Though if I uncomment the (message) call, it displays reasonable numbers.
GNU Emacs 24.3.1, on Ubuntu. And in case it matters, I use
(setq-default indent-tabs-mode nil) and (cua-mode).
I must be missing something obvious... ?
All of what Tim X said is true, but if you just need something that works, or an example to show you what direction to take your own code, I think you're looking for something like this:
(defun unindent-rigidly (start end arg &optional interactive)
"As `indent-rigidly', but reversed."
(interactive "r\np\np")
(indent-rigidly start end (- arg) interactive))
All this does is call indent-rigidly with an appropriately transformed prefix argument. If you call this with a prefix argument n, it will act as if you had called indent-rigidly with the argument -n. If you omit the prefix argument, it will behave as if you called indent-rigidly with the argument -1 (instead of going into indent-rigidly's interactive mode).
There are a number of problems with your function, including some vary
fundamental elisp requirements. Highly recommend reading the Emacs Lisp
Reference Manual (bundled with emacs). If you are new to programming and lisp,
you may also find An Introduction to Emacs Lisp useful (also bundled with
Emacs).
A few things to read about which will probably help
Read the section on the command loop from the elisp reference. In particular,
look at the node which describes how to define a new command and the use of
'interactive', which you will need if you want to bind your function to a key
or call it with M-x.
Read the section on variables from the lisp reference
and understand variable scope (local v global). Look at using 'let' rather
than 'setq' and what the difference is.
Read the section on 'positions' in the elisp reference. In particular, look at
'save-excursion' and 'save-restriction'. Understanding how to define and use
the region is also important.
It isn't clear if your writing this function just as a learning exercise or
not. However, just in case you are doing it because it is something you need to
do rather than just something to learn elisp, be sure to go through the Emacs
manual and index. What you appear to need is a common and fairly well supported
requirement. It can get a little complicated if programming modes are involved
(as opposed to plain text). However, with emacs, if what you need seems like
something which would be a common requirement, you can be fairly confident it is
already there - you just need to find it (which can be a challenge at first).
A common convention is for functions/commands to be defined which act 'in
reverse' when supplied with a negative or universal argument. Any command which
has this ability can also be called as a function in elisp code with the
argument necessary to get that behaviour, so understanding the inter-play
between commands, functions and calling conventions is important.
Very simple question but confuse me for some time:
(setq visible-bell t)
and
(visible-bell t)
both seem work.
But
(desktop-save-mode 1)
works, while
(setq desktop-save-mode 1)
not.
May I ask why is this?
They're different because they're different :)
(setq visible-bell t)
is assigning the value t to a variable named visible-bell.
(visible-bell t)
is calling a function1 named visible-bell (and passing the value t as a parameter).
(Although FYI there is no visible-bell function by default in current versions of Emacs, so it's not obvious to me that this is actually working the way you think? However, assuming for the moment that you do indeed have such a function...)
Emacs Lisp is a 'Lisp-2' meaning it has separate name spaces for variables and functions, and therefore you can -- and commonly do -- have a variable and a function with the same name. Which one is being referred to is always implicit in the context of the code (e.g. setq always refers to a variable).
In short, the two pieces of code are doing very different things. This doesn't mean they couldn't have an equivalent effect (e.g. the function might simply set the value of the variable); but whether or not that's actually the case is entirely up to the definition of the function.
1 In fact the first line of code is also calling a function2: it's calling setq and passing it two parameters visible-bell and t, and setq then sets the value in accordance with its parameters. Hopefully you're now starting to see how lisp syntax works?
2 Strictly speaking, setq is actually a "special form" rather than a function, and special forms are closer to macros than to functions; but these distinctions are not important for this Q&A.
Others have told you the basic points about what setq does and about variables versus functions.
Wrt visible-bell itself:
There is no function visible-bell delivered with Emacs, in any Emacs version I know of, and there never has been. (I've checked back through Emacs 20, and by memory I believe the same was true from the beginning. There is only the variable visible-bell.
So as #phils suggested, is not clear that what you said is true: "both seem to work". Unless some extra code you are loading defines a function of that name (and then there is no way for us to comment on it, not having it to see), evaluating (visible-bell t) raises an undefined (void) function error.
Variable visible-bell is not just a variable. It is a user option, and it has been, again, since at least Emacs 20.
You should not, in general, just use setq to change the value of a user option. In many cases you won't get into trouble if you do that, but sometimes you will, and it is not a good habit to get into.
setq does not perform any special initialization or updating actions that might be appropriate for a given user option. It is not intended for user options. Or rather, user options are not intended for setq - they can be more complex than what setq can offer.
What you should use instead of setq is Customize. Either interactively (M-x customize-option RET visible-bell RET, or C-h v RET visible-bell RET followed by clicking the customize link) or using Lisp code in your init file.
If you use Lisp code then use one of these functions (not setq):
customize-set-variable
customize-set-value
custom-set-variables
Use C-h f followed by each of those function names, to see what (minor) differences there are.
There are 3 issues here.
In Emacs Lisp, the same symbol can be both variable and function.
in the case of desktop-save-mode, it's a function but also a variable.
Because it's a function, so you can call
(desktop-save-mode 1)
Because it's a variable, so you set value to it
(setq desktop-save-mode t)
You can define your own function and also a variable of the same name to test it.
Note: exactly what a function's arguments should be or what the value of a variable makes sense depends on the function or variable.
Now, a second issue. In general, for function (commands) to activate a minor mode, the convention is that a positive integer should mean to turn it on, and otherwise turn off.
Also, for command to activate a minor mode, typically there's a variable of the same name, with value of t or nil, to indicate if the mode is on.
Now, there's third issue. For command to activate the mode, before emacs 24 or so, by convention, if no arg is given, the command toggle current state.
Because all of the above, the issue is confusing. You might see in init things like this:
(desktop-save-mode 1) ; correct. To turn on.
(desktop-save-mode) ; Confusing. Should take value 1 to turn on. Usually works because by default it's off.
(desktop-save-mode t) ; wrong. Take value of positive integer to turn on.
(desktop-save-mode nil) ; Confusing. Value should be integer
(setq desktop-save-mode t) ; wrong. Shoud call function instead
(setq desktop-save-mode nil) ; wrong. Shoud call function instead
(setq desktop-save-mode 1) ; wrong. Shoud call function instead. Besides, only t and nil make sense
So, there's a lot confusion. In emacs 24 (or 23.x), the convention changed so that, if it receives no value, it will turn on, if called in elisp code. (when called interactively as command, it toggles.)
In the end, always call describe-function or describe-variable to read the doc.
Well setq (which is "set" with an auto-quoting feature) is used in assigning a value to a variable. In this example, it's obviously not required because as you mentioned, omitting it works for the first set of examples.
Basically, visible-bell is a variable, and you assign it the value "t" to enable visible bells.
However, desktop-save-mode is an interactive function, so you don't use setq to assign it a value, you call it with parameters.
One good thing to do when you're not sure what something is, is to use the built-in help function:
C-h v visible-bell RET
This will return the information for visible bell -- notice the "v" in the command is because it's a variable. If you wanted to search for information on a function, you would do this:
C-h f desktop-save-mode RET
Incidentally in this case, desktop-save-mode is also a variable, but it's a read-only variable to determine whether or not desktop-save-mode is enabled, so trying to alter it will not work.
I am learning LISP using PC Scheme TI implementation and from the book 'Structure and Interpretation of Computer Programs'. PC scheme doesn't seem to have a 'nil' variable
[VM ERROR encountered!] Variable not defined in current environment
NIL
Instead '()' seems to work.
[55] (define one-through-four (list 1 2 3 4 ()))
ONE-THROUGH-FOUR
Do both have the same meaning? What is the correct way to use a sequence of no elements in the PC scheme dialect?
Thanks.
When you say that this dialect "does not have a NIL variable", you are right in two ways. Not only does not nil exist, but it seems to be treated as an ordinary symbol that you can use as a variable. It says "Variable not defined".
In Lisp dialects where nil has a special meaning, nil usually cannot be used as a variable.
nil is equivalent to () only in "classical" Lisps (my term). By this I mean Lisps that are related by ancestry or imitation to the original John MacCarthy Lisp. Emacs Lisp and Common Lisp are that way. The classical Lisps use nil as the list terminator: i.e. (cons 1 nil) producing the list (1)
Scheme is not a classical Lisp in this sense. There is no nil. It uses () as the printed notation for an empty list and when you want that value in an expression, it is quoted, just like a non-empty list literal: '(). In Scheme, (cons 1 nil) is written (cons 1 '()). Boolean false is represented by an object which is notated #f. An empty list is true not false, so (if '() 1 2) yields 1, not 2 like in a classical Lisp. Neither '() nor #f are symbols.
Scheme is also case-sensitive. Even if you do have a variable NIL in Scheme, it has nothing to do with nil. You can use both as variable names.
The SICP text causes confusion on this topic by, in its early chapters, making references to a variable called nil which holds the value of '(). There is no such variable in Scheme; you have to make it yourself if you want those examples to work. This is an unfortunate aspect of the textbook.
If you define a variable nil whose value is the object '(), so that that Chapter 2 code from SICP works, it is still not equivalent to an empty list the way it is in a classical Lisp, because in a classical Lisp, nil and () are equivalent notations for a symbol. The symbol nil itself is the empty list, and vice versa. It is not the name of a variable which merely holds a list. And so the quote expression 'nil also evaluates to the empty list! Whereas in scheme 'nil evaluates to the ordinary symbol nil even if you've defined it as a variable holding the empty list.
Regarding the second question, "() seems to work, do both have the same meaning?". The "same meaning" aspect is covered above: no, not the same meaning in Scheme. As far as it working, consider the following quote from the R5RS Scheme Report:
Note: In many dialects of Lisp, the empty combination, (), is a legitimate expression. In Scheme, combinations must have at least one subexpression, so () is not a syntactically valid expression. [4.1.3 Procedure Calls]
Therefore this loose treatment of () as valid expression that produces the empty list appears to be a PC Scheme extension of behavior, not standard Scheme. It is accepting an expression which is not a syntactically valid Scheme expression.
How do I set face for specified keywords but only ones in first line?
For example having this file
--- cut here ---
hello world <-- this "hello" should have face set
hello world <-- while this "hello" should not
--- cut here ---
only first hello should have face set
I tried this
(defun first-line-hello(limit)
(and (save-excursion (beginning-of-line)
(bobp))
(re-search-forward "hello" limit)))
(font-lock-add-keywords 'emacs-lisp-mode
'((first-line-hello . font-lock-warning-face)))
but it seems that for some reason (bobp) returns always true when used in font-lock-keywords. I also tried using line-number-at-pos with the same result.
You're close, but there are a few problems. 'limit' could be well beyond the end of the first line, you need to not have an error if the search fails, and you need to move point regardless of the search pass/fail. Which all boils down to changing one line in your function:
(defun first-line-hello(limit)
(and (save-excursion (beginning-of-line)
(bobp))
(re-search-forward "hello" (min (point-at-eol) limit) 'go)))
Emacs has a regexp construct that matches the empty string only at the beginning of the buffer, so try this:
(font-lock-add-keywords 'emacs-lisp-mode '(("\\`hello" . font-lock-warning-face)))
The docs say:
‘\`’
matches the empty string, but only at the beginning of the buffer or string being matched against.
First line of a buffer? First line of a paragraph?
Regardless, I think you could do it with your own minor mode, in which you scan the buffer/region the way you like. In your scan you would need to start from the beginning, note the location of the first time you saw each keyword, highlight it, and continue scanning. if you see a repeated keyword during the scan, then don't highlight that one.
I believe you cannot do it with simple font-lock-keywords. They are always keywords, regardless where they appear. But you could do it with custom matchers for font-lock. Get help on font-lock-keywords and you will see:
Each element in a user-level keywords list should have one of these forms:
MATCHER
...
where MATCHER can be either the regexp to search for, or the function name to
call to make the search (called with one argument, the limit of the search;
it should return non-nil, move point, and set match-data appropriately if
it succeeds; like re-search-forward would).
These matcher functions or regexps get called or evaluated at arbitrary positions in the buffer, at seemingly arbitrary times. Using a regexp wouldn't work for that reason, but using a function, you could scan back to beginning-of-buffer, re-search-forward for the keyword, and find out if it's the first time you've seen the keyword, and then take appropriate action. You could also cache the first-seen location of a keyword, and then manage the cache in a after-change hook, but that's getting pretty complicated.
EDIT on re-reading I see you'ver tried this latter idea and it isn't working for you. strange.
This is rather queer. I can't set any value to a variable if it is named 's' in an interactive session:
(setq s 'foo)
=> foo
s
=> nil
Why?
Update 1:
Here is the output from describe-variable on s:
s is void as a variable.
Documentation:
Not documented as a variable.
Why is it that s is kept void in emacs lisp as a global variable?
Update 2:
Turned out, it doesn't happen on a vanilla emacs (meaning one of the modules I load in .emacs or some code in .emacs is causing this).
So the question now is:
What would the original source look like when describe-variable yields "<var> is void as a variable"?
I tried it with setq, defconst, defvar, and defcustom, but none of those produced the message I'm showing.
Update 3:
The message shown above is produced when the variable is literally not bound (though it can be fbound).
(describe-variable 'non-existent)
=> "non-existent is void as a variable.
Documentation:
Not documented as a variable."
So latest question is: Is there any way to prevent a certain variable name
from being bound?
An answer to your revised question:
(defvar s)
The only thing is that this won't let you use describe-variable on it interactively.
(You could then do something like (setplist 's '(variable-documentation "Meh")) to set a description for it without going through defvar.
Just bisect your init file (~/.emacs) recursively until you find the sexp that causes the problem. If it is a sexp that loads another library then either don't use that library or fix it by first finding out what its problem is, the same way: bisect the code in that library recursively, etc.
This is a binary search, and it is very quick. You can quickly comment out half, then 3/4, 7/8, etc. of your file, using M-x comment-region (I bind it to C-x ;). with a prefix arg, comment-region uncomments.
With Emacs 23.1, running the following code makes C-h v s RET show “s is void as a variable.”, but I can't reproduce the inconsistency between setq and retrieving the value of the variable (which I agree is weird).
(setq s t)
(make-local-variable 's)
(makunbound 's)
I suspect an Emacs 24-specific feature or bug.