Emacs mode 1, 2 and t (what t stands for)? - emacs

For instance, global-linum-mode 1 enables line numbers global-linum-mode 0 disables line numbers, what global-linum-mode t would do? and I've seen another parameter: nil. What do they do?

t and nil in boolean context are the (Emacs) Lisp truth values. nil is false. Any non-nil value is considered to be true, but t is customarily used. Source.
As for global-linum-mode, any non-negative parameter will enable it, including t, "foo",... etcetera.
For such questions it is very useful to play in the scratch buffer with elisp: type your expression there and press C-j.

t is true, nil is false.

Related

Call a command with arguments in Emacs minibuffer

I know that M-x (execute-extended-command) allows one to call a command in Emacs by typing its name. That however doesn't allow me to call command with arguments, e.g "backward-word 5"
I am aware that C-5 M-b produces the desired result but I am looking for a general method.
Does anyone know how to do this?
Thanks,
Danny
Eval it: M-: (backward-word 5) RET.
#abo-abo provided the general answer: use M-:.
However, if you are interested not only in obtaining the side effects of the evaluation, and not only in a cursory overview of the return value, but in the full return value (regardless of its size and complexity), then vanilla Emacs M-: is not what you want.
For that, I substitute pp-eval-expression for eval-expression wrt its key bindings (including M-:), and I recommend this practice for others too:
(substitute-key-definition 'eval-expression 'pp-eval-expression global-map)
That pretty-prints the return value, and you can make it print the complete value (no ellipses: ...).
In addition, I offer a modified version of pp-eval-expression in library pp+.el. It has these advantages:
Reads with completion, using pp-read-expression-map.
Emacs-Lisp mode indentation and completion key bindings are available during input.
With a prefix arg, inserts the resulting value into the current buffer at point.
With a negative prefix arg, if the return value is a string, inserts it into the buffer without double-quotes (").
Font-locks the return value (syntax highlighting) for Emacs-Lisp mode. (And during display emacs-lisp-mode-hook and change-major-mode-hook are inhibited.)
Respects new user options pp-eval-expression-print-length,
pp-eval-expression-print-level, and standard option eval-expression-debug-on-error. The former are separate from the similar, standard options eval-expression-print-length and
eval-expression-print-level because the use cases are typically different.
If you use library Icicles then you get the same advantages as library pp+.el -- no need to load pp+.el also.

How to completely erase Emacs Calc's state?

This has got to be a stupid simple question, but I haven't been able to find an answer despite searching Google multiple times in multiple ways, and digging through the Calc documentation.
GIVEN an instance of Emacs, with Calc running, and having performed multiple calculations including storing variables and equations, some of which may have come from the "~/.emacs.d/calc.el" file,
HOW do you return Calc to a pristine state without restarting Emacs?
Pristine: Nothing on the stack. Nothing in the trail. No stored variables or equations. Etc.
M-x calc-reset, which is also bound to C-x * 0, is what you need. From the info manual:
The C-x * 0' command ('calc-reset'; that's 'C-x *' followed by a
zero) resets the Calculator to its initial state. This clears the
stack, resets all the modes to their initial values (the values that
were saved withm m' (calc-save-modes')), clears the caches (*note
Caches::), and so on. (It does _not_ erase the values of any
variables.) With an argument of 0, Calc will be reset to its default
state; namely, the modes will be given their default values. With a
positive prefix argument,C-x * 0' preserves the contents of the stack
but resets everything else to its initial state; with a negative prefix
argument, `C-x * 0' preserves the contents of the stack but resets
everything else to its default state.
EDIT: Oops. Even that doesn't clear variables. I'm not sure if there is a straightforward way to get all the way back to pristine :(
EDIT 2: It looks like Calc stores all variables, including 'built-ins' like pi and e, as global variables with the prefix 'var-'. As far as I can tell, it doesn't keep track of which variables were set by the mode (like pi), and which were set by users. Furthermore, the default user variables are stored as var-q0, var-q1 etc. So in order to clear out all the variables, you'd need to compile a list of variables and states present at startup, erase everything not in that list, and then restore the original values of the variables in that list. That's certainly possible, but a little tedious.
Edit 3: Here's my attempt. I took another look at calc-mode, and at start up it defines the variables I've added to my-calc-builtin-vars below. The second line will remove all variables in Emacs that start with the prefix 'var-' and are not in this list. This will include any variables defined by you, or in another package. So let's hope no-one else uses the prefix 'var-'. And it will not reset the value of the built-in variables, so if you have redefined pi to 3, it will remain 3.
(setq my-calc-builtin-vars
'("var-nan" "var-uinf" "var-sym" "var-lines" "var-Modes"
"var-EvalRules" "var-inf" "var-phi" "var-pi" "var-gamma" "var-π"
"var-φ" "var-γ" "var-spec" "var-e" "var-i"))
(defun really-reset-calc ()
(interactive)
(calc-reset nil)
(mapc #'(lambda (el) (unintern el))
(remove nil (mapcar
#'(lambda (el) (unless (member el my-calc-builtin-vars) el))
(all-completions "var-" obarray)))))
UPDATE: August 6, 2016
Current built-in vars list:
(setq my-calc-builtin-vars
'("var-CommuteRules" "var-Decls" "var-DistribRules" "var-EvalRules"
"var-FactorRules" "var-FitRules" "var-Holidays" "var-IntegAfterRules"
"var-IntegLimit" "var-InvertRules" "var-JumpRules" "var-MergeRules"
"var-Modes" "var-NegateRules" "var-e" "var-gamma" "var-i" "var-phi"
"var-pi" "var-γ" "var-π" "var-φ"))
M-# 0
That should be all you need to do.

Prevent completing-read from exiting with nil

I'm reading the documentation on completing-read, but I can't find a way to do what I need.
It says that:
(completing-read PROMPT COLLECTION &optional PREDICATE REQUIRE-MATCH
INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)
. . .
REQUIRE-MATCH can take the following values:
- t means that the user is not allowed to exit unless the input is (or completes to) an element of COLLECTION or is null.
- nil means that the user can exit with any input.
- `confirm' means that the user can exit with any input, but she needs to confirm her choice if the input is not an element of COLLECTION.
- `confirm-after-completion' means that the user can exit with any input, but she needs to confirm her choice if she called
`minibuffer-complete' right before `minibuffer-complete-and-exit'
and the input is not an element of COLLECTION.
- anything else behaves like t except that typing RET does not exit if it does non-null completion.
What I need to do is something like:
(completing-read "What kind of project should I create? "
haxe-project-kinds
(lambda (x) (message "predicate: %s" x)) t)
This shouldn't return nil, because if it does, it's an error - but I don't want to run the user through all other options until she discovers that she got the very first one wrong.
More than that, the behaviour advertised in the documentation doesn't match what really happens. It makes absolutely no difference what I put in the 4'th argument's position, the behaviour is unchanged.
I'm not sure exactly which part of what you want is not satisfied by your sample code, so it's hard to give a good answer. My guess is that you want to prevent the user from hitting RET with an empty answer. Indeed completing-read does not prevent that, even with require-match set. The way this is usually handled is by using a non-nil value for the default argument, in which case this value is returned when the user just hits RET.
If that's not good enough, then you're probably going to have to use minibiffer-with-setup-hook and in the hook, setup a special keymap you've created for this purpose where RET is bound to a new function that signals an error if the minibuffer is empty and calls minibuffer-complete-and-exit otherwise.

How to copy some content in a system clipboard not a kill ring with elisp/emacs?

I learned that copy something to kill buffer, I can use the kill-new buffer
(kill-new (file-truename buffer-file-name))
How about copying the content to clipboard? Does emacs/elisp provide the function?
ADDED
I got 2 answers, but the answer seems to apply X windows system. As I use Aquamacs, it may not be a universal solution that can be applied to my problem.
(x-set-selection nil (file-truename buffer-file-name))
Documentation for x-set-selection says:
This function sets a “selection” in
the X server. It takes two arguments:
a selection type type, and the value
to assign to it, data. If data is nil,
it means to clear out the selection.
Otherwise, data may be a string, a
symbol, an integer (or a cons of two
integers or list of two integers), an
overlay, or a cons of two markers
pointing to the same buffer. An
overlay or a pair of markers stands
for text in the overlay or between the
markers.
The argument data may also be a vector
of valid non-vector selection values.
Each possible type has its own
selection value, which changes
independently. The usual values of
type are PRIMARY, SECONDARY and
CLIPBOARD; these are symbols with
upper-case names, in accord with X
Window System conventions. If type is
nil, that stands for PRIMARY.
You can configure Emacs to automatically copy to the system clipboard as well as to the kill ring. The function kill-new calls interprogram-cut-function, which typically copies to a system clipboard if there is one. Under X Window, the variable x-select-enable-clipboard controls whether the primary selection or the clipboard is used.

emacs/elisp: What is the hash (pound, number sign, octothorp) symbol used for?

What does this do?
(add-hook 'compilation-mode-hook #'my-setup-compile-mode)
...and is it different than
(add-hook 'compilation-mode-hook 'my-setup-compile-mode)
There is no difference:
(eq 'my-add #'my-add)
yields t
The # can be used in front of a lambda expression indicating to the byte-compiler that the following expression can be byte compiled, see the docs for Anonymous Functions. But there's nothing to compile in the case of a symbol.
In general, it is used in the printed representation along with the left angle bracket (<) to indicate that the object printed is a description (but cannot be read). For example:
#<buffer foo.txt>
It is also used in constructs by the reader to represent circular structures. See the docs for Read Syntax for Circular Objects.
And then you have its use for denoting the base for integers, e.g. #x2c -> 44.
Plus more I'm sure.
The should-be-comprehensive list can be found at the top of the Emacs lisp reference index.
Edit: Or even more conveniently, from within Emacs itself:
M-x info RET (open the info browser)
d m elisp RET (open the elisp manual)
I # RET (list the entries for # in the index)
I found this question while searching for what the hash meant in something I found while hacking mode-line-format:
#("-%-" 0 3
(help-echo "Display as tooltip when mouse hovers or with display-local-help."))
which is a format used for text properties in strings where:
"-%-", text to be propertized: one dash and a %-construct that results in "dashes sufficient to fill the remainder of the mode line", resulting in the famous Emacs ------.
0, the first character upon which the text properties apply.
3, the last character upon which the text properties apply, i.e. the entire "-%-".
(help-echo "..."), a property and a string as its argument.
This can be created with the propertize function:
(propertize "Hover over me!" 'help-echo '"congratulations!")
would be the same as #("Hover over me!" 0 14 (help-echo "Congratulations!")):
If you're using font lock mode, using the buffer-substring command might produce something like this:
(buffer-substring 1 28) ; First 27 characters in the current buffer
⇒ #(";; This buffer is for notes"
0 3
(fontified t face font-lock-comment-delimiter-face)
3 27
(fontified t face font-lock-comment-face))
So you could create something like: