Some questions about emacs mode - emacs

As we know, mode is very important in emacs. But I feel I am not very clear about how to set it. For example, I often see something like (***-mode 1) or (***-mode) in .emacs file. And some tutorials also say that a mode can be set by M-x ***-mode. Could you tell me what's the differences between them and how to use them? Thanks!

A Lisp function is a piece of code which declares a name for another piece of code to be executed later.
(defun hello ()
(message "Hiya!"))
Now, you can invoke the named code from anywhere else in Lisp.
(hello)
Only at this point does the message form get executed.
Many Lisp functions contain an interactive form which specifies how they should behave when called interactively (for example, should it prompt for an argument, or use the cursor or mouse position as the argument, etc). Those which do can be invoked with M-x and the function name.
A major mode specifies a function which sets up some variables to exclusively control the behavior of Emacs. For example, M-x text-mode sets a (very basic) regime for word wrapping and cursor movement which is suitable for text files. When you are in text mode, you cannot be in C++ mode, or Lisp mode, or fundamental mode. These are other major modes which define different or additional functionality suitable for editing other types of text.
Because a major mode is exclusive, it is usually a function which doesn't take any arguments. So to put the current buffer in text mode, the Lisp code is simply
(text-mode)
Minor modes, by contrast, specify additional behavior which is independent from the major mode. For example, Overwrite mode specifies a different behavior when inserting text before some other text -- normally, Emacs pushes any existing text ahead, but when overwrite mode is active, existing text in front of the cursor will be replaced as you type.
You can have multiple minor modes active at any time -- you could have flyspell (spell checking as you type), tool bar mode, menu bar mode, and line number mode active at the same time as you are in text mode and overwrite mode.
Because of this, a common (though not universal) convention for minor modes is to perform a toggle. When you are already in toolbar mode, M-x toolbar-mode will disable this minor mode. To unambiguously disable the mode, pass it a negative numeric argument;
(toolbar-mode -1)
Without the argument, the code will toggle -- the result will depend on whether the mode was already active, or not.
(As noted in a comment, this changed in Emacs 24; I'm describing the historical behavior.)

Related

emacs: How do I disable relative indent [duplicate]

I recently ran into some trouble with cron and crontab because the text editor I use, emacs, inserts several spaces instead of a tab, when I press the tab key. This issue persists throughout all the major modes, regardless of the tab width set for each mode.
How can emacs be forced to use the tab character instead of tabs composed of multiple spaces?
If you want to insert a TAB character, then use C-q TAB. The TAB key is used for a different purpose (mostly to indent/align text/code according to various rules, tho it's also used for various other purposes such as performing completion, skipping from one field to another, etc...).
Set the variable indent-tabs-mode. You can do that by customizing the variable with M-x customize-variable, on the fly with M-x set-variable, or in Elisp with the setq function.

How to enter recursive edit mode and abort command inside it?

I used to be confident of returning to buffer from mini-buffer by just pressing C-g once, until I learned there is a "recursive-edit-mode" inside Emacs Here, which seems to make cancellation action inconsistent. Therefore, I am reading the entire section in hope of finding a more consistent / stateless command to return to mini-buffer.
However, I encountered below problems when following and trying to replicate what the manual says:
Entering Recursive-edit-mode: After "M-x query-replace RET C-r" (in Fundamental-mode), I could not replicate "a pair of square brackets" in mode-line. However, now I DO have to press C-g TWICE to return back to buffer.
Question: Does recursive-edit-mode still have square brackets now? (I am using ver 24.3)
Quitting: The manual directs me to the Quitting section for quitting command. It says Aborting with C-] (abort-recursive-edit) is used to get out of a recursive editing level and cancel the command which invoked it. So I tried it out within "M-x query-replace RET C-r", hoping one C-] will get me out from minibuffer to buffer, but it still takes 2 shots to do that.
Question: Is this normal? If yes, any other command to guarantee command abortion and return to buffer with 1 execution?
Just want to brush frequently used command such as C-g. If made consistent, it may help avoid inputs being eaten (sometimes unnoticed) by the minibuffer due to trapped inside recursive-edit-mode.
Visually confirming point each time inside the buffer makes keyboard input an closed-loop feedback process and thus consumes more attention, though.
You definitely will see square brackets in the mode line for a recursive edit -- but not for a recursive minibuffer.
C-g does act consistently, but it does different things in different contexts (always the same thing for a given context, however). That can be confusing. It can take some getting used to - a bit like it takes getting used to the various behaviors of C-g during Isearch, depending on the current search state.
If you use recursive edits or recursive minibuffers then you need to pay attention to clues wrt the current state/context. Again, this is like learning Isearch. minibuffer-depth-indicate-mode is a must, to provide feedback about what state you are in.
FWIW, beyond minibuffer-depth-indicate-mode, I use a standalone minibuffer frame, and with each recursive edit or recursive minibuffer the background hue is changed slightly, to better indicate the level etc. (The background color changes slightly also for (a) active minibuffer and (b) Isearch. I use library oneonone.el for these things.)
If you don't use a standalone minibuffer frame then you might consider highlighting the square brackets or something else in the mode line, to help you tell where you are at.
C-r during query-replace is a good example of when a recursive edit can be useful. There are a few others. But generally I do not make much use of recursive edits, FWIW. (I do, however, use recursive minibuffers a lot, because I use keys bound in minibuffer maps that do invoke commands that themselves might prompt for input etc.)
Wrt your question about query-replace with C-r: There is no minibuffer involved at all, here. And C-] after C-r does end not only the recursive edit but also the query-replace (whereas C-M-c ends the recursive edit and returns to the query-replace.
FWIW - In Icicles interactions, you might find yourself within any number of recursive minibuffers. (For example, you might use progressive completion to successively narrow the set of completion candidates, and each narrowing opens a recursive minibuffer.)
C-g always aborts the current command, or if there is none then the current minibuffer, and C-] always aborts the current minibuffer. Repeating C-g (or C-]) pops back up the minibuffer chain, a level at a time. But (in Icicles) you can use C-M-S-t (aka C-M-T) to pop directly back to the top level (exit all minibuffers).
Similarly, answering a minibuffer prompt (e.g. hitting RET or using mouse-2 on a candidate in *Completions*) exits the current minibuffer, popping up a level (to the parent minibuffer or to the top level if there is none). (In Icicles RET can optionally put you back at top level.)

auto complete in command minibuffer of emacs

Which setting needs to be done in init.el file, which allows completing the rest of command if one hit M-x and initial letter of the command.
Infact in need something similar as ido-mode for minibuffer too
The ido-mode for the "M-x minifuffer" is called smex (smex use ido).
Available on the main package repos of Emacs. Homepage here
I think you are talking about incremental completion, i.e., having Emacs automatically complete what you type in the minibuffer, without your having to explicitly request completion (e.g., using TAB).
Incremental completion is available in Icicles, as well as Ido and IswitchB. And icomplete-mode shows you completion candidates in a similar way to Ido and IswitchB.
Icicles incremental completion has two aspects:
When buffer *Completions* is displayed and updated, showing you the candidates matching your input -- how soon that happens and what triggers updating
Whether and how much your minibuffer input is expanded (completed) to reflect the set of matching completions
Wrt *Completions* display (#1):
You can use C-# to cycle among the levels (normal, eager, off) at any time.
Normal means that *Completions* is not displayed until you ask for it, but thereafter it is automatically updated as you type/edit your input.
Eager means that *Completions* is displayed as soon as you type something that matches at least two candidates.
(There is also an option to show *Completions* from the outset, before you type anything -- useful as a kind of menu.) You can also specify how long to wait after you type or delete a character before updating *Completions*.
Wrt input expansion (#2):
Icicles is unique in expanding your input to (typically) the longest common match among all completions, even when completion uses apropos matching (that is, regexp or substring -- S-TAB), not just prefix matching (TAB).
There are 4 levels/behaviors for this expansion, plus off (no expansion):
Off -- this is like Ido and IswitchB: completions are shown, but your input is not completed
On request -- expand your input only when requested (TAB or S-TAB)
On request or sole candidate -- on request or when there is only one match
Always for TAB, on request for S-TAB -- TAB expands whenever possible; S-TAB is like previous
Always -- expand input whenever possible
C-" toggles between two of the input-expansion behaviors that you choose (a user option), and C-M-" cycles among all of the behaviors.

Return to previous mode in Emacs

Is there a key binding to quit a mode and return to the previous mode in emacs?
For example suppose I entered line number mode using the following command:
Alt+x linum-mode
How can I quickly disable this mode and return to the mode which I was in before (not using the same command again)?
Why wouldn't you want to use the same command again?
M-xM-pRET
It doesn't get much simpler than that.
Edit: You can repeat M-p in that sequence to step back further in the command history, and you can search the command history with M-xC-r.
Also, when you disable a minor mode you're not "returning to the mode you were in before"; you're just disabling one (of many) minor modes which are all active at the same time.
Tangentially, the concept of "returning to the mode you were in before" could apply to major modes (as there's only ever one active major mode in a given buffer), but strictly speaking there's no notion of 'disabling' a major mode -- only of 'enabling' the one you wish to change to -- so to 'toggle' between two major modes, you would need to call them alternately.
Just repeat the same command: M-x linum-mode. Such minor-mode commands are toggles: on/off.
C-x z calls repeat - repeat last command.
Repeatedly calling a minor-mode enables/disables it.
One more way to do it is with smex: the last command
that you called with M-x kinda sticks around.
So you can enable linum-mode with smex, do a bunch of editing
with usual shortcuts and then disable linum-mode with
M-x RET.
A solution might follow the path kill-ring-save works: store the modes being active as current-modes-listing in a previous-modes-ring.
The code needed therefor exists basically inside describe-mode, see upward from "Enabled minor modes" - respective for the major mode.
Then a hook should check if this-command has "-mode" in it's name. If yes, check, if current modes-listing equals car of previous-modes-ring. If not, add new setting.
Finally write a command which sets current modes according to selected listing from previous-modes-ring.

Code folding for LaTeX in Emacs

Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in LaTeX mode? For instance, I would like to move to the beginning of a long \begin{figure} block, hit a keystroke, and have the contents of that figure environment hidden from view. Similarly with \begin{proof} and so on, and ideally even with \subsections.
Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize LaTeX's environments, e.g. hs-minor-mode fails with "scan error: unbalanced parentheses". I would prefer not to have to enter explicit folding marks like {{{ as in folding-mode.
[Ideally it would be great if the folding were persistent, but I see that that question doesn't have an accepted answer yet.]
AUCTeX does folding: http://www.gnu.org/software/auctex/manual/auctex.html#Folding
A popular complaint about markup languages like TeX and LaTeX is that there is too much clutter in the source text and that one cannot focus well on the content. There are macros where you are only interested in the content they are enclosing, like font specifiers where the content might already be fontified in a special way by font locking. Or macros the content of which you only want to see when actually editing it, like footnotes or citations. Similarly you might find certain environments or comments distracting when trying to concentrate on the body of your document.
With AUCTeX’s folding functionality you can collapse those items and replace them by a fixed string, the content of one of their arguments, or a mixture of both. If you want to make the original text visible again in order to view or edit it, move point sideways onto the placeholder (also called display string) or left-click with the mouse pointer on it. (The latter is currently only supported on Emacs.) The macro or environment will unfold automatically, stay open as long as point is inside of it and collapse again once you move point out of it. (Note that folding of environments currently does not work in every AUCTeX mode.)
In order to use this feature, you have to activate TeX-fold-mode which will activate the auto-reveal feature and the necessary commands to hide and show macros and environments. You can activate the mode in a certain buffer by typing the command M-x TeX-fold-mode RET or using the keyboard shortcut C-c C-o C-f. If you want to use it every time you edit a LaTeX document, add it to a hook:
(add-hook 'LaTeX-mode-hook (lambda ()
(TeX-fold-mode 1)))
If it should be activated in all AUCTeX modes, use TeX-mode-hook instead of LaTeX-mode-hook.
Once the mode is active there are several commands available to hide and show macros, environments and comments...