Set column width for visual lines in Emacs - emacs

Is there an equivalent to the fill-column variable for the "Wrap" mode (visual-line-mode) in Emacs? If not, how could I set a limit on the length of visual lines when the window/frame of the buffer is wider ?

In response to this question I created a minor mode called window-margin that accomplishes what #Stefan suggested in his answer.
Turn on window-margin-mode with:
(add-hook 'text-mode-hook 'turn-on-window-margin-mode)
The way you can still do it without installing window-margin is to use the longlines-mode that ships with Emacs, but is being phased out since there are some problems with longlines-mode, but here's the old way to do it if you want:
Turn on longlines-mode with something like:
(add-hook 'text-mode-hook 'longlines-mode)
which wraps text at the fill-column.

longlines-mode has been removed. For visual-line-mode, the simplest way is to make the window as narrow as you want it to be. You can do that with C-x 3 and then adjusting the size of the window. Or you can set a wide margin or wide fringes.

Related

What is the use of a fill-column if most modern text editors can word-wrap?

I have my emacs fill-column set to almost 10 trillion (999999999999). Is that bad, or is there another way to make emacs have a virtually infinite fill-column?
What is the use of a fill-column anyways? Why would one want that sort of line wrapping?
Yes, you can have a gigantic fill-column. Or yes, you can turn off auto-fill-mode (use (auto-fill-mode -1) or set auto-fill-function to nil).
"That sort of line wrapping" is useful if you want text that has lines of limited length. (Duh.)
See also visual-line-mode, which can give you a similar visual effect but without the insertion of hard newlines.

Re-filling entire paragraph automatically with auto-fill-mode?

Motivation: Using the defaults, Auto Fill mode seems not as useful as I might
have hoped: If I insert a sentence in the middle of a paragraph, only the current
line is re-filled. When I insert a sentence, I want the entire paragraph to be re-filled.
Question: How can I set auto-fill-function (or perhaps
normal-auto-fill-function) in my .emacs
file so that the paragraph is re-filled whenever a single line overflows?
I tried setting it to fill-paragraph, but then I cannot insert any spaces at the end of a paragraph (e.g., to add another word).
More details: I primarily use Auto Fill mode in the AUCTeX major mode for LaTeX.
The built-in Emacs documentation for auto-fill-mode states:
When auto-fill-mode is on, the auto-fill-function variable is
non-nil.
The value of normal-auto-fill-function specifies the function to use
for auto-fill-function when turning Auto Fill mode on.
The documentation for the normal-auto-fill-function variable
says that it is the function to use for auto-fill-function if Auto Fill mode is
turned on, and that the initial value is do-auto-fill.
You might like to try refill-mode. But in general, it's just tricky to make such a feature work well. Another approach is to only do the refill as part of the redisplay (i.e. without affecting the buffer's actual content). For that, try setting word-wrap or enabling visual-line-mode.
For LaTeX files you can try (requires AUCTeX)
(add-hook 'LaTeX-mode-hook '(lambda ()
(setq auto-fill-function 'LaTeX-fill-paragraph)))
but use it with caution.

Aquamacs 2.4 -- WordWrap / Visual Line Mode -- bug workaround?

Has anyone found a workaround to the inability of Aquamacs 2.4 to correctly WordWrap at the window boundary?
The modes I use most often are text-mode and latex-mode.
I've removed the built-in hooks of text mode to try and isolate the bug and come up with a workaround:
(remove-hook 'text-mode-hook 'smart-spacing-mode)
(remove-hook 'text-mode-hook 'auto-detect-wrap)
The following code gives me a fancy indicator in the status bar WordWrap, but words are still fragmented at the window boundary.
(add-hook 'latex-mode-hook 'set-word-wrap)
(add-hook 'text-mode-hook 'set-word-wrap)
The following code gives me a pretty status bar indicator of vl WordWrap, but the words are still fragmented at the window boundary.
(setq-default global-visual-line-mode t)
The following code seems to have no appreciable effect:
(setq longlines-wrap-follows-window-size t)
I've really never seen this feature working, so I'm not sure it is even possible with Aquamacs 2.4.
The expected behavior is that words should stay together by automatically wrapping to the next line at the window boundary. As I resize the window, this should happen on the fly. As I type a line, this should happen automatically when I reach the window edge.
Just in case this is a conflict, I'll mention that I have Yasnippets, local and global whitespace, and flyspell all enabled. I also have line numbers, highlight the current line, size indication, and column numbers.
Perhaps someone could just give me the name of the el / elc file that is responsible for this behavior, and I'll take it from a 24.3 version and paste it into Aquamacs 2.4. I am primarily interested in this version because of the ease of which it handles LaTeX documents.
Have you tried in the "Options" menu the submenu "Line wrapping in this buffer"?
Alternatively,
(add-hook 'text-mode-hook #'visual-line-mode)
or
(global-visual-line-mode 1)
The nightly builds of Emacs / Aquamacs have fixed this issue.

how to change variables for specific fundamental-mode buffers

Goal: I want to have show-trailing-whitespace enabled for all buffers save a few. Exceptions posing a problem are *Shell Command Output* and its cousin *Async Shell Command*.
I usually have show-trailing-whitespace customized to t. Therefore it is active in all new buffers.
I would also like to have it turned off for certain buffers, foremost amongst them *Shell Command Output*. This poses a problem for me:
The output buffer doesn't use a special mode; it is still in fundamental-mode. There is no fundamental-mode-hook that I could hook this setting into.
There is the after-major-mode-change-hook which is run when the major mode is changed to fundamental-mode, but the buffer starts out in that mode and therefore this hook is not run.
There doesn't seem to be a way to hook into get-buffer-create.
I know I can always advise the function get-buffer-create for this particular example, but I try to avoid that as much as possible.
Any hints?
You might be better off looking at the problem from the other side, and only set the var in those modes where you want to see trailing whitespace.
But I think you have a good point: these shell output buffers should not use fundamental-mode. It's probably time for M-x report-emacs-bug
In accordance with the accepted answer, here's a code snippet that enables trailing whitespaces highlighting for specific modes only:
(setq-default show-trailing-whitespace nil)
(defun namespace/show-trailing-whitespace ()
"Highlight trailing whitespaces in this buffer."
(setq-local show-trailing-whitespace t))
(dolist (hook '(prog-mode-hook text-mode-hook))
(add-hook hook 'namespace/show-trailing-whitespace))
This snippet is essentially taken from Steve Purcell's configuration.

Emacs word wrap at a specific column number

I like to run my editor full-screen. The only thing is, though, that when I do this, the word wrap only kicks in when the line hits the right edge of the screen. I would like it to do so, already when the line hits, say, column number 200.
How do I do that?
I would like it to happen in all modes, e.g., Org-mode. I added the line (global-visual-line-mode t) to my .emacs file, in order for the word wrapping also to work in org-mode.
I'm running Emacs 23.
I got it working! Here is what I added to my .emacs file to make it happen:
(add-hook 'text-mode-hook 'turn-on-auto-fill)
(add-hook 'text-mode-hook
'(lambda() (set-fill-column 80)))
Type M-x auto-fill-mode to activate automatic line-wrapping after a certain column. Then set the actual line width through the variable fill-column as described by user choroba (C-x f).
Note though that this works a bit differently from what other text editors do. M-q will re-format the current paragraph.
You can set the line width with C-xf (set-fill-column).
Afterwards, you might need to hit M-q to reformat the current paragraph (fill-paragraph), or select text to be justified and run fill-region.
The suggestion for turn-on-auto-fill will work if you want hard newlines in the files you're editing. If not, and you just want word-wrap, consider instead visual-fill-column-mode, which just does the normal word-wrap that would happen at the edge of the window, but at the specified fill-column.
See the Emacs manual (C-h r), node Filling. See in particular the first subnode in the menu, Auto Fill.