How to completely erase Emacs Calc's state? - emacs

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.

Related

Emacs: setting "comment-column" for all buffers

I am trying to set the comment-column local variable to 70 by default for every buffer I am opening.
I thought that the only thing I had to do is to put the following line in my .emacs file, and to reload Emacs:
(setq-default comment-column 70)
However, when I restart Emacs, it is still to set to 40.
I also tried equivalent like:
(set-default 'comment-column 70)
I am obviously missing something obvious, but I can't find what. I think there is something I don't know about the local variables such as comment-column.
I cite the help on comment-column:
Documentation: Column to indent right-margin comments to. Each mode
may establish a different default value for this variable; you can set
the value for a particular mode using that mode's hook. Comments might
be indented to a different value in order not to go beyond
`comment-fill-column' or in order to align them with surrounding
comments.
Could the bold-faced part be the problem?
I have tried it at my place. There, most programming modes use the global default value whereas modelica-mode uses its own default value.

Lisp: creating a macro to cycle through a created list per document

Let's say I want to create a new document, and cycle quickly through a list.
If it's only for one "word", I think there should be a general way to do this.
For instance:
"blue orange red yellow black white"
Does anyone know a way how to cycle through those items when I create:
\begin{orange}
... and I want to press a key to cycle through this list, replacing orange with the next item on the list (doing this procedure in the opposite direction wouldn't be hard then)?
I tried many different ideas with macro's (placing the list on the top of the document, and doing a whole bunch of i-searches), but that doesn't cut it.
I'd be willing to put the list in an elisp file, though I have no clue how to use that variable from elisp in, let's say, a LaTeX document (.tex).
Well, this might be possible, but depends on how much effort you are willing to put into writing eLisp code to make it work. It's not possible by just some configuration option. I would be looking into extending autocomplete by adding new sources to it, something like:
(defvar tex-tag-ac-sources
'((init . tex-tag-ac-init)
(requires . 0)
(candidates . tex-tag-ac-candidates)
(document . tex-tag-ac-documentation)
(match . tex-tag-completion-filter)
(prefix . tex-tag-ac-prefix-matcher)
(symbol . "s"))
"The source generator for autocompletion needed for interaction
with auto-complete")
Where tex-tag-ac-candidates, tex-tag-ac-documentation, tex-tag-completion-filter and tex-tag-ac-prefix-matcher are function that do autocompletion. I.e. init function is called once when the autocompletion process starts for a specified prefix. It's called w/o arguments. The candidates is the function that is responsible for showing the filtered list of candidates, it's called w/o arguments, you would filter the candidates in the filter function, it is called with the prefix collected so far and the list of candidates so far. Lastly, the matcher function is invoked on the text of the file to see if the completion is needed at point. So, if it returns t, the init is called, and then loops through filter-candidates as you type.
While this is a bit involved... you'd definitely have a completion for anything you want. Obviously, if those functions in source are defined by you, then, if you wanted to, you could read completion arguments dynamically or have them generated dynamically in some way.
Ah, you would add the sources to autocomplete by something like:
(auto-complete (list tex-tag-ac-sources))
if doing it on per call basis, or
(setq ac-sources (list tex-tag-ac-sources <other sources>))
You can find more info here: http://cx4a.org/software/auto-complete/manual.html#Using_Source
EDIT: I translated the macro into a function.
Here is a way I did it. I created a file called "list.list" where my "lists" are saved. I saved the LaTeX templates for Beamer in there. I inserted them like this:
Antibes Bergen Berkeley Berlin ..... Antibes
Note that you should always put the first entry in twice to allow it to loop.
Here is the code:
(defun cycle-list-word ()
(interactive)
(right-word)
(backward-kill-word 1)
(find-file "/emacs-24.1/list.list")
(search-forward (substring-no-properties (car kill-ring)) nil t)
(right-word)
(backward-kill-word 1)
(bury-buffer)
(yank)
)

Emacs: abbrev-mode not expanding simple abbrev, possible causes?

I would like to assign the string 'target="_blank"' to the abbreviation 'tgt' as I use this a lot and it's annoying to have to type out each time.
The string above probably needs escapes and so on, so as a simpler starting point I have tried to assign 'target' to 'tgt'. Despite switching on abbrev-mode and defining this as a global abbrev, if I type 'tgt' and hit space, nothing happens. If I do M-x list-abbrevs I get this:
(global-abbrev-table)
"target" 1 "tgt"
So it seems to be recorded. I would have expected tgt to be expanded to target when I hit a space after tgt, but that doesn't happen. What am I missing? Have I completely misunderstood the nature of abbrevs? I have looked at the Emacs wiki page but like many Emacs pages it gives a number of complex solutions and annoyingly sparse coverage of the basics.
EDIT: embarassingly enough this looks like it was a combination of the wrong abbrev file and an assignment that was reversed, so that typing 'target' produced 'tgt'. I now have 'tgt' producing 'target="_blank"' as desired. Apologies everybody.
However, I now have a related question. This abbreviation expands when I hit space and typically I do not want a space after the 'target="_blank"' string. Is there a way to automatically remove the space?
You have to define the abbrev the other way around. The way you did it will expand "target" into "tgt".
To do the right thing, first type into a buffer what you want the abbrev to expand into. In your case that would be "target". Then, with the point right after the word, type C-x a g. This will prompt you for an abbrev for which you would type "tgt".
Et voilà: if abbrev-mode is turned on, typing tgt will now expand into "target".
There are other ways to define an abbrev, e.g. via M-x define-global-abbrev, thus it's best to check out the documentation.
The problem with removing the space after the abbrev is that Emacs will insert it after the abbrev has been expanded. So hitting space will basically two things: trigger the expansion of the abbrev and then run the normal self-insert command.
One simple way to avoid this is to type C-x ' or C-x a e to explicitly expand an abbrev rather then turning on abbrev-mode. Except, that's a bit annoying. If we look at the documentation again, however, we find:
Function: define-abbrev table name expansion &optional hook count
[...]
If hook is a non-nil symbol whose no-self-insert property is
non-nil, hook can explicitly control whether to insert the
self-inserting input character that triggered the expansion. If hook
returns non-nil in this case, that inhibits insertion of the
character. By contrast, if hook returns nil, expand-abbrev also
returns nil, as if expansion had not really occurred.
This means that if you put the following lines in, say, your ".emacs" file:
(defun my-after-abbrev-expand ()
(when (looking-back "\"\"\\|''\\|()\\|\\[\\]\\|{}")
(backward-char 1))
t)
(put 'my-after-abbrev-expand 'no-self-insert t)
then you can define an abbrev like so:
(define-abbrev global-abbrev-table "tgt" "target=\"\"" 'my-after-abbrev-expand)
to avoid the insertion of the space character. Also, the my-after-abbrev-expand function will move the point one position to the left if the expansion ends in two double quotes, two single quotes, or a pair of round, square or curly braces.

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.

Why might my Emacs use spaces instead of tabs?

I am trying to diagnose this problem. TAB creates 4 spaces instead of a 4 col TAB like I want. But I don't think it should because C-h v indent-tabs-mode on the buffer in question says it is set to t. When I check my keybindings, TAB is set to c-indent-line-or-region. Does this function ignore my tabs-mode?
Tabs and indentation in Emacs is a considerably more complex subject than most people anticipate. I highly recommend spending some time reading about it -- it will almost certainly save you some confusion in the long run.
The following page at the Emacs Wiki groups together most of the relevant discussion:
http://www.emacswiki.org/emacs/CategoryIndentation
There's quite a lot there, but it's worth looking through.
One or other of the TabsAreEvil and SmartTabs configurations is quite likely to be appealing to you, btw, depending on your personal opinions on the subject!
Make sure you read the page on the tab-stop-list variable. It's tucked away near the bottom of that list of links, but it's critical to understanding the behaviour of tabs in the absence of automated-indentation rules, along with things like 'tabify'.
ruler-mode is useful here as well. I enable it automatically with text-mode:
;; Use ruler in text-mode
(add-hook 'text-mode-hook
(function (lambda ()
(setq ruler-mode-show-tab-stops t)
(ruler-mode 1))))
I figured out the problem. It was inserting a tab character after all. It turns out I thought it wasn't because when I hit backspace that key is bound to c-electric-backspace, which looks at the variable c-backspace-function which was set to backward-delete-char-untabify, which IMO defeats the purpose of having tabs.
Check tab-width variable. If it is 8 (the default), then Emacs of course has to insert four spaces since a tab would be "too much".
Check of the file for Emacs "File Local Variables". These specially formatted lines can override your settings when that file is loaded.
Here is an example from the bottom of a bit of Ruby code, forcing indent to 2 spaces, and tabs converted to spaces:
# Local Variables:
# tab-width: 2
# ruby-indent-level: 2
# indent-tabs-mode: nil
# End:
Be sure to take a look at the first line of the file as well. If you see something like
// -- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil --
This line will override any global or mode settings.