What does ^L in (Emacs Lisp) source code mean? - emacs

Several times I see ^L in (mostly Emacs Lisp) source codes that looks like are separators of larger logical groups. Is it their real purpose? And if so, how can I use them? Is there a built-in Emacs functionality that utilize it?

The Emacs commands backward-page and forward-page (C-x [ and C-x ]), among others, take advantage of ^Ls placed in the code as separators.
The habit did not propagate much to languages other than Emacs-lisp, but most languages treat ^L as a blank, so you can use these separators in your favorite language if you like the idea. You can type your own ^Ls with C-q C-l.

This is a page break.
[...]
A page break can also be used for a logical separation of source-code
sections. Emacs has commands and key bindings that use page breaks, such
as ‘forward-page’ (C-x ] or C-]), ‘backward-page’
(C-x [ or C-[), and 'narrow-to-page' (C-x n p). Other functions, such as
‘mark-page’, operate on the content of a page. See also PageMode.

When exploring a big file with multiple of such "pages" the function "narrow-to-page" (C-x n p) is handy: it hides everything not in the current page. Then for example searching for a function name to see the callers only leads to matches in that section, so you can really focus on understanding the narrowed region.
Use widen (C-x n w) to see the whole file again.

See also Pretty Control-L if you want to change how Control-l characters appear -- e.g., use a highlighted line instead of just ^L.

That is indeed a page break character, which on older line printers skipped to the next page or paper. Code-wise, it does nothing; it is only there to split the code into larger sections. There are convenient Emacs commands to jump to the next and previous "page", and inserting these characters takes advantage of that.

Related

How to format clojure in emacs [duplicate]

I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.

How do I intelligently re-indent Clojure in Emacs?

I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.

Retaining tab-to-tab-stop with Emacs replace-regexp

I have a very large text file with nearly 20 columns. Unfortunately the program which produced the file did not properly handle columns which began with a -10 (it only properly spaced data which had a total of 5 characters, not 6). Essentially I ended up with something that looks like this:
-10.072-10.179-10.2190.002
I want it to look like:
-10.072 -10.179 -10.219 0.002
and was almost able to do so with:
M-x replace-regexp RET -10\.... RET \&TAB RET
When I use TAB however, it only replaces with a space rather than tab- ideally a tab-to-tab-stop. If I manually go to one of these situations in the file and type TAB it properly does a tab-to-tab-stop to align the data with the proper column. How do I retain the tab-to-stop function within the replace-regexp?
The search and replace certainly ought to be inserting a tab. The apparent size of a tab can vary, of course, which is the only reason I can think of for it appearing to be a space. You could use whitespace-mode to make the difference more obvious.
As for tab-to-tab-stop, that's a dynamic function rather than a special kind of tab character, so you can't do that with a search and replace1. I would suggest using a keyboard macro instead, to get the same dynamic behaviour as manual typing.
F3
M-C-s -10.... RET
M-x tab-to-tab-stop RET
F4
or perhaps just: F3M-C-s -10.... RETTABF4
Then you can run the macro until it fails with C-0F4
(If you only want to run it on a portion of the buffer, you can simply narrow to the relevant region first.)
1 Not strictly true, as Emacs lets you evaluate arbitrary elisp as part of a replacement pattern; but it's not just a case of calling the tab-to-tab-stop function, so the macro is really much simpler.

Emacs: help me understand file/buffer management

I've been using emacs for all of my text editing needs for the past three years now. When I'm in a single file, working on code or whatnot, I'm fairly efficient. With two files, I can "C-x b RET" between them and I do fine. However, whenever I need to be working on more than two files at a time, I tend to get lost.
Here are some of the problems that I'd like to work on:
I forget what some of my buffers are called, but I don't understand why C-x C-b splits my window into two buffers and exits the mini buffer. Sure I can switch buffers and choose a buffer to visit, but this feels unintuitive, and leaves me with two buffers open.
When I visit a directory rather than a file, I have a convenient list of all of the files and directories. I usually want to do one of two things with this: 1) Open a single file and never see this buffer again OR 2) Open a bunch of files and never see this buffer again. I don't really know how to do this, as moving the point to a file and hitting return doesn't do either of these things.
I know that my buffers aren't like tabs, but I have an inclination to want to scroll through them to find what I want. I don't know of any key-bindings for this, but I'd like it to be M-n / M-p or the like. Then again, this may be a horribly inefficient way to switch buffers.
When I open interactive help of any kind (for example in ESS), I have a habit of switching back to the buffer I was working in and using C-x 1 to get back to a single buffer. When I do this, however, the help buffer hangs around in my buffer list, further confusing me. I know I can switch to that buffer, kill it, switch back, and then go back to a single buffer, but this seems wrong.
The way I've dealt with this so far involves using a tiling window manager and a few emacs windows in different work-spaces, rather than actually learn the best way to manage a number of files in emacs. I don't necessarily want to change emacs to better fit my needs (although I am open to that if it fits in with what I'm about to say), instead, I'd like to grok the thought process behind handling files/buffers the way that emacs does, and how I can be more efficient with it.
Any answer that would help me understand the correct way, or a more efficient way to manage my buffers or files would be greatly appreciated.
Bind C-x C-b to ibuffer. This is a better buffer listing facility with many advanced features, and its default behaviour is to replace the current buffer with the buffer listing, and then bury the listing when you select a buffer (leaving you with the newly-selected buffer in place of the original one).
You can simply use C-x b to enter your selection in the mini-buffer, of course; however the tab-completion (which is needed to make this a viable option, IMO) does open a new window temporarily, at which point I think you might as well familiarise yourself with something with more features.
Use a instead of RET when selecting from dired. This kills the dired buffer instead of leaving it behind. C-h m in any buffer will show you the help for its major mode (followed by help for the minor modes), and you can read about all the available dired key bindings there.
http://www.emacswiki.org/cgi-bin/wiki/TabBarMode ? (edit: I prefer Rémi's answer for this one, but TabBarMode would give you the visual tab element if you were particularly keen on that.)
q is bound to a 'quit' function in a great many major modes. Generally it buries the buffer rather than killing it, but I certainly find that fine.
To elaborate a little on #1, ibuffer has lots of nice features, and M-x customize-group ibuffer RET will give you some idea of how you can customise it to your liking.
Furthermore, you can filter the buffer list by many criteria (again, use C-h m to see its help page), and then generate a 'group' definition from the current filters, and save your custom filters and groups for future usage.
For example:
/ f ^/var/www/ RET: filter buffer list to show only filenames starting with /var/www/.
/ s Web filters RET: name and save active filter set to your init file.
/ g Web development RET: create a named group from the active filters.
/ S My groups RET: name and save group definitions to your init file.
/ r Web filters RET: invoke the "Web filters" filters.
/ R My groups RET: invoke the "My groups" groups.
RET on a group name to collapse or expand it.
C-k and C-y to kill and yank groups, to re-arrange them.
C-h m for more information...
This way you can have a single Emacs instance running, and create filters and groups for different types of task, and easily switch between them.
I think you will really enjoy Ido for dealing with multiple buffers who's names you can't exactly remember. When you type C-x b it shows a list of open buffers in most used order. As you type some of the characters in a buffer name the list is filtered. The characters you type don't have to be at the begging of the name or contiguous. Using C-f, C-b or left/right arrow keys cycles through the buffer choices.
Also see Smex for Ido like functionality for M-x
Closing windows is done with C-x 0. Intentionally splitting the window is done with C-x 2 for horizontal, C-x 3 for vertical. I love this feature, since it allows me to have test and production code visible at the same time. C-x o takes me to the other window.
I use C-x right (or C-x C-right) and C-x left (or C-x C-right) to go to the next and previous buffer. I don't mind anymore off the few buffer that lay around in Emacs but you could use k in the buffer list to kill the buffer you don't use anymore.
You can also try Iswitchb mode which provides auto-completion for buffer names when you switch buffers via C-x b.
To activate:
M-x iswitchb-mode
Or add to your .emacs file:
(iswitchb-mode)
It is similar to Ido mode for buffer switching but a bit more lightweight.
Also, if you want a more customizable listing of your buffers then use M-x bs-show as an alternative to C-x C-b. In that buffer type ? to get a list of actions you can perform.
I think the number one most useful extension for flipping through buffers is Anything. It lets you start typing part of a buffer (or file!) name and it will figure out what you want. I've rebound C-x b to anything-for-buffers. It makes life so much better.
As always, there are many ways to help you with this; it depends a bit on personal preference what works best, here are some links with explanations:
ibuffer; which is an updated buffer menu (C-x C-b)
ido, which let's you have more powerful autocompletion to switch through buffers. It's a kind-of 'better iswitchb'.
These two are enough for me; but you may also be interested in the tabbar-mode, which gives you rudimentary tabs (like firefox has them).

Emacs remember text selection

I decided that I was ready to try something new, after a few years of using gEdit for most of my coding needs, and try to learn using Emacs. I knew this would be difficult, as I have heard how complex Emacs can be, but I was lured by its power. The hardest thing has been getting used to writing ELisp in the .emacs file to change things about the editor. I can't currently do it myself, but I have found a few helpful snippets here and there to change some options.
One thing I have been having a lot of problems with is getting Emacs to remember the text I have selected after a command. For instance, I commonly highlight a section of code to mass indent it. However, if I do this in Emacs, it will move the selected text only once before unselecting all of the text. Does anyone know a way around this?
Anyway, I apologize for what seems to me to be an easy question, but after an hour of Google searching and looking around here on SO, I thought it was worth asking. I have a few more questions about Emacs, but I will save them and ask separately after I get this straightened out. Thanks!
UPDATE
A few people have asked about what mod I am using and what type of text I am entering. While I don't know much about Emacs modes, I am editing a pure text file at the moment. Something like this:
Hello, I am a simple text file
that is made up of three separate
lines.
If I highlight all three lines and hit TAB, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
This is great, however, if I use C-x C-x like some suggest below to reselect the text and hit TAB again, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
I hope this helps!
FWIW, here is the reason for the behaviour of your newly-added example. (I'm not 'solving' the issue here, but I'm posting it to demystify what you're seeing.)
This was determined with emacs -q which disables my customisations, so the following is default behaviour for emacs 23.2.
You are in text-mode. You should see (Text) or similar in the mode line at the bottom of the screen, and C-h m will tell you (under the list of minor modes) "Text mode: Major mode for editing text written for humans to read." Emacs decides (by way of the auto-mode-alist variable) that it should switch to text-mode if you visit a filename matching certain extensions (such as .txt).
In text-mode pressing TAB with a region highlighted causes indent-according-to-mode to be called on each line of the region in sequence. The slightly convoluted path to finding this out starts at C-h k TAB, which tells us that TAB is bound to indent-for-tab-command, which in this instance calls indent-region -- that function name is not stated explicitly in the help, but can be seen in the code -- which checks the buffer-local indent-region-function variable, which is nil, and: "A value of nil means really run indent-according-to-mode on each line."
indent-according-to-mode checks the indent-line-function variable, which has the buffer-local value indent-relative.
Use C-h f indent-relative RET to see the help for this function. (Read this).
Although you probably won't yet have had the experience to know how to check all that (or necessarily even want to!), and fully understand everything it tells you, this is an example of how the self-documenting aspect of Emacs enables a user to figure out what is going on (which then makes it feasible to change things). I essentially just used C-h k (describe-key), C-h f (describe-function), and C-h v (describe-variable) to follow the documentation. Looking at the source code for indent-for-tab-command was as simple as clicking the file name shown as part of its help page.
I suggest doing the following to help see what is happening when indent-relative runs on each line:
M-x set-variable x-stretch-cursor t
M-x set-variable ruler-mode-show-tab-stops t
M-x ruler-mode
Now for each line in turn, put the cursor at the very start of the line and press TAB. You'll end up with all three lines indented to the first tab-stop ('T' in the ruler).
Now repeat this -- again, ensure you are at the very start of each line, in front of the existing indentation.
The first character of the first line (which is currently a tab) is once again indented to the first tab-stop, as there is no preceding line for it to examine.
Next, the first character of the second line is indented to match the position of the first non-white-space character of the preceding line. Because the first character of the second line is also a tab, the actual text of the second line is pushed one tab further along.
The third line follows suit. Its first tab character is lined up with the first non-white-space character of the second line, with the same relative effect as before, giving you the final state in your example.
To emphasise, note what happens if you now put enter the line "a b c" above the existing lines, then move back to the start of the next line (what was previously the first line) and press TAB. The first tab character will now be indented in line with the 'b'. Provided that the indent-tabs-mode variable is true (meaning you have actual tab characters), then this will have no practical effect on the position of the words in the line, as 'indenting' a tab with spaces will not have an effect until the number of spaces exceeds the width of the tab (but that's another kettle of fish entirely!)
All this really means is that text-mode in Emacs doesn't behave the way you'd like it to in this situation. Other major modes can do completely different things when you press TAB, of course.
As is invariably the case with Emacs, things you don't like can be changed or circumvented with elisp. Some searching (especially at the Emacs Wiki) will frequently turn up useful solutions to problems you encounter.
Try typing C-x C-x after Emacs unselects it.
Then, instead of hitting tab (I never knew that tab does what you said! That's totally whacked.), do M-8 C-x C-i. Pity it's so many keys, but it ought to do what you want -- namely, shove everything over 8 columns. Obviously replace the M-8 with something else if you want some other number of columns.
What I usually do is simply type C-x C-x (exchange-point-and-mark) after a command that deactives the region.
How are you indenting, and in which mode?
The indentation rules in any programming mode should generally just get it right. (If they don't, that's probably more indicative that you want to configure the rules for that mode differently, but I suspect that's a different question which has been asked already).
If you're in text-mode or similar and just using TAB, then I can see the problem.
Note that if you're using indent-rigidly (C-x C-i, or C-x TAB which is the same thing) then you can repeatedly indent the same region simply by repeating the command, even if the highlighting has disappeared from view.
You can also use a prefix arg to indent-rigidly to make it indent many times. e.g. C-u C-u C-x C-i (easier to type than it looks) will indent 16 spaces (4 x 4, as the prefix arg defaults to 4, and it multiplies on each repeat). Similarly, M-8 C-x C-i indents 8 spaces. This is fine in some circumstances, and way too cumbersome in others.
Personally I suggest putting (cua-selection-mode 1) into your .emacs and using that for rigid indentation. Trey Jackson made a handy blog about it. With this, you can C-RET to start rectangle selection, down as many lines as you need, TAB repeatedly to indent the lines, and C-RET to exit the mode.
While the rectangle is active, RET cycles through the corners. For left-hand corners, typing inserts in front. For right-hand corners, typing inserts after. For the single-column rectangle, bottom counts as 'left' and top counts as 'right' for this purpose.
Trey's blog lists all the available features (or look in the source file: cua-base.el)
Be warned that indentation in Emacs is generally an unexpectedly complicated topic.
You can do this with something like the following:
(add-hook 'text-mode-hook (lambda ()
(set (make-local-variable 'indent-region-function)
(lambda (s e)
(indent-rigidly s e tab-width)))))
Then selecting a region and hitting TAB. will indent the region by a tab-width. You can then exchange point and mark with C-x C-x and hit TAB again to repeat.
I do, however, agree with the previous answers that suggest using indent-rigidly directly.