How to configure indentation in emacs lua-mode? - emacs

Complete emacs newbie here.
I'm using emacs 23.1.1 on Ubuntu with emacs starter kit. I primarily work in the lua-mode (installed with package-install lua-mode).
I need to tune how indentation works, so it would match my coding guidelines.
The guidelines are:
tabs-to-spaces;
two spaces per indent;
80 chars per line maximum, without trailing spaces.
Example:
local foo = function()
print("Hello, world!")
end
What I get with emacs if I don't try to fight with its auto-indent:
local foo = function()
print("Hello, world")
end
Update:
(This belongs to a comment, but since it needs extra formatting, I have to place it here.)
If I try solution by Thomas, I get this:
local foo = function()
print("Hello, world")
end
Note that end is indented with a tab and four spaces.
Does not quite work...
Update 2:
This thing is also gets indented in the wrong way:
local bar = foo(
"one",
"two",
baz(), -- Note three spaces
"quo"
)
It should be:
local bar = foo(
"one",
"two",
baz(),
"quo"
)
Update 3:
Third case of the wrong indentation:
local bar = foo(
"one",
"two"
)
local t = 5 -- This line should not be indented,
-- also note tab between local and t.
Update 4:
Here is what I get with the current version from Thomas:
local foo = function()
print("Hello, world")
end
local bar = 5 -- Emacs put \t before 5
local zzz = foo( -- Emacs put \t before foo
"one", -- Pressed TAB here twice
"two",
three(),
"four"
)
Except where explicitly noted, I did not do anything for indentation, only typed in the code and pressed RETURN at the end of each line. I did not actually type any comments.
It should look as follows:
local foo = function()
print("Hello, world")
end
local bar = 5
local zzz = foo(
"one",
"two",
three(),
"four"
)
Update 5:
One more wrong indentation case:
local foo =
{
bar(); -- Did press a TAB here, but closing brace killed it
baz;
}
Should be:
local foo =
{
bar();
baz;
}
Update 6:
For the sake of completeness, here is what I get with the current Git HEAD of lua-mode, without Thomas's configuration tuning:
local foo = function()
print("Hello, world!")
end
local bar = 5
local foo = bar(
bar,
baz(),
quo(),
aaa
)
local t =
{
"one",
two(),
}
With tuning:
local foo = function()
print("Hello, world!")
end
local bar = 5
local foo = bar(
bar,
baz(),
quo(),
aaa
)
local t =
{
"one",
two(),
}
To match my coding guidelines, it should look as follows:
local foo = function()
print("Hello, world!")
end
local bar = 5
local foo = bar(
bar,
baz(),
quo(),
aaa
)
local t =
{
"one",
two(),
}

Okay, let's give this another try... After browsing through the source code of lua-mode, I've come up with the following approach.
The reason for the admittedly strange default indentation is a function called "lua-calculate-indentation" which computes the column to which to indent the current line. Unfortunately, the values returned by it do not match your desired specification.
For instance, if you enter a single line into a fresh .lua file like this one:
local foo = function()
and hit enter to move the point to the second line, you can invoke the above function by typing M-: (lua-calculate-indentation). The result is 15, which means that lua-mode will indent the second to column 15. This is the reason for the unorthodox indentation you've described and exemplified in your original question.
Now, to fix this I suggest re-defining the function "lua-calculate-indentation" so that it returns the indentation you want. For this, put the following code into an otherwise empty file, and save it under the name "my-lua.el" in the same directory where "lua-mode.el" lives.
;; use an indentation width of two spaces
(setq lua-indent-level 2)
;; Add dangling '(', remove '='
(setq lua-cont-eol-regexp
(eval-when-compile
(concat
"\\((\\|\\_<"
(regexp-opt '("and" "or" "not" "in" "for" "while"
"local" "function") t)
"\\_>\\|"
"\\(^\\|[^" lua-operator-class "]\\)"
(regexp-opt '("+" "-" "*" "/" "^" ".." "==" "<" ">" "<=" ">=" "~=") t)
"\\)"
"\\s *\\=")))
(defun lua-calculate-indentation (&optional parse-start)
"Overwrites the default lua-mode function that calculates the
column to which the current line should be indented to."
(save-excursion
(when parse-start
(goto-char parse-start))
;; We calculate the indentation column depending on the previous
;; non-blank, non-comment code line. Also, when the current line
;; is a continuation of that previous line, we add one additional
;; unit of indentation.
(+ (if (lua-is-continuing-statement-p) lua-indent-level 0)
(if (lua-goto-nonblank-previous-line)
(+ (current-indentation) (lua-calculate-indentation-right-shift-next))
0))))
(defun lua-calculate-indentation-right-shift-next (&optional parse-start)
"Assuming that the next code line is not a block ending line,
this function returns the column offset that line should be
indented to with respect to the current line."
(let ((eol)
(token)
(token-info)
(shift 0))
(save-excursion
(when parse-start
(goto-char parse-start))
; count the balance of block-opening and block-closing tokens
; from the beginning to the end of this line.
(setq eol (line-end-position))
(beginning-of-line)
(while (and (lua-find-regexp 'forward lua-indentation-modifier-regexp)
(<= (point) eol)
(setq token (match-string 0))
(setq token-info (assoc token lua-block-token-alist)))
; we found a token. Now, is it an opening or closing token?
(if (eq (nth 2 token-info) 'open)
(setq shift (+ shift lua-indent-level))
(when (or (> shift 0)
(string= token ")"))
(setq shift (- shift lua-indent-level))))))
shift))
This code sets the indentation level to two spaces (instead of 3), modifies a regular expression that detects if a statement stretches over multiple lines, and finally redefines the indentation function using an auxiliary.
All that's left to do is make sure this code is actually loaded. That must happen after the original lua-mode is loaded, or else that code would re-install the original indentation function.
The way we do that here is a little hacky: we install a call-back function that is invoked each time a buffer changes its major-mode to lua-mode. It then checks if the auxiliary function mentioned before is defined - if not, it loads "my-lua.el". That is a little fragile, but as long as you don't play around with the lua source code, you should be fine.
Add the following lines to your ~/emacs.d/agladysh.el file (assuming that "agladysh" is your username):
(add-hook 'lua-mode-hook
(lambda () (unless (fboundp 'lua-calculate-indentation-right-shift-next)
(load-file (locate-file "my-lua.el" load-path)))))
I assume that lua-mode is on your load-path which it should be if you followed lua-mode's installation instructions.
I hope that it works for you this time, if not, let me know.

I know it's been a while since this was asked, but I just wanted to point out that this is still an issue, with lua-mode installed via the Emacs package system.
However, the latest version on GitHub works very well, didn't notice any indentation weirdness. All you have to do to conform with the Lua style guide is to set indent-tabs-mode to nil and lua-indent-level to 2.

If you enter the following code into .emacs file in your home directory, it will make lua-mode (and only lua-mode) behave the following way:
If you press ENTER, a newline will be inserted and by default the next line will be indented like the previous line.
Whenever you press TAB to indent the line, point either jumps to the first non-whitespace character of the line or, if the line is empty of point is already at that character, two spaces are inserted.
Especially the latter may not be what you want, but perhaps its a first approximation.
(defvar my-lua-indent 2
"The number of spaces to insert for indentation")
(defun my-lua-enter ()
"Inserts a newline and indents the line like the previous
non-empty line."
(interactive)
(newline)
(indent-relative-maybe))
(defun my-lua-indent ()
"Moves point to the first non-whitespace character of the
line if it is left of it. If point is already at that
position, or if it is at the beginning of an empty line,
inserts two spaces at point."
(interactive)
(when (looking-back "^\\s *")
(if (looking-at "[\t ]")
(progn (back-to-indentation)
(when (looking-at "$")
(kill-line 0)
(indent-relative-maybe)
(insert (make-string my-lua-indent ? ))))
(insert (make-string my-lua-indent ? )))))
(defun my-lua-setup ()
"Binds ENTER to my-lua-enter and configures indentation the way
I want it. Makes sure spaces are used for indentation, not tabs."
(setq indent-tabs-mode nil)
(local-set-key "\r" 'my-lua-enter)
(setq indent-line-function 'my-lua-indent))
;; add `my-lua-setup' as a call-back that is invoked whenever lua-mode
;; is activated.
(add-hook 'lua-mode-hook 'my-lua-setup)
Restart Emacs for these changes to take effect.

A cleaner way to do this was added in 2019, in the form of two lua-indent- variables. This gets us almost there, but it still double-indents nested blocks for some reason. Adding a little advice hack finishes the job.
(setq lua-indent-nested-block-content-align nil)
(setq lua-indent-close-paren-align nil)
(defun lua-at-most-one-indent (old-function &rest arguments)
(let ((old-res (apply old-function arguments)))
(if (> old-res lua-indent-level) lua-indent-level old-res)))
(advice-add #'lua-calculate-indentation-block-modifier
:around #'lua-at-most-one-indent)

I can't help much right now - I have a deadline in two days 8-( - but
here is what I use in my .emacs to make lua-mode usable for me...
(setq lua-indent-level 2)
(setq lua-electric-flag nil)
(defun lua-abbrev-mode-off () (abbrev-mode 0))
(add-hook 'lua-mode-hook 'lua-abbrev-mode-off)
(setq save-abbrevs nil) ;; is this still needed?
I indent my code in an unusual way - see the example below - and so
I've disciplined myself to only press TAB when lua-mode can infer the
right indentation correctly from the lines above...
map = function (f, A, n)
local B = {} -- TAB here doesn't work
for i=1,(n or #A) do -- TAB here works
table.insert(B, f(A[i])) -- TAB here works
end -- TAB here works
return B -- TAB here works
end -- TAB here works

I'm the maintainer (but not author) of lua-mode.el. As I'm a lot less fluent in Emacs Lisp than other contributors to this thread, I welcome patches. I'd just like to point out that there's nothing weird or incorrect about the default rules: the idea, as far as I can see, is simply that when you're in an anonymous function, the indentation should take the function keyword as its left margin. This makes sense when you consider using function expressions in other places, e.g. as function parameters.
So, one simple workaround is not to write
local f = function...
but
local function f...
Unless perhaps you're working with a version of Lua that predates the "local function" syntax.
Having said that, I can see why you might want to indent differently. In this case it seems to me reasonable to have a configuration variable lua-indent-function-from-function-keyword (better name, anyone?), and I'd be happy to accept a patch that implemented it.

I think a lot of what you're looking for can be found in the emacs manual on custom C indentation definitions which falls under general indentation engine descriptions.
You can make it do anything you can imagine, which would be strongly preferable to just doing anything you imagine.

Related

A quick way to repeatedly enter a variable name in Emacs?

I was just typing in this sort of code for Nth time:
menu.add_item(spamspamspam, "spamspamspam");
And I'm wondering if there's a faster way to do it.
I'd like a behavior similar to yasnippet's mirrors, except
I don't want to create a snippet: the argument order varies from
project to project and from language to language.
The only thing that's constant is the variable name that needs to be
repeated several times on the same line.
I'd like to type in
menu.add_item($,"")
and with the point between the quotes, call the shortcut and start typing,
and finally exit with C-e.
This seems advantageous to me, since there's zero extra cursor movement.
I have an idea of how to do this, but I'm wondering if it's already done,
or if something better/faster can be done.
UPD The yasnippet way after all.
Thanks to thisirs for the answer. This is indeed the yasnippet code I had initially in mind:
(defun yas-one-line ()
(interactive)
(insert "$")
(let ((snippet
(replace-regexp-in-string
"\\$" "$1"
(substring-no-properties
(delete-and-extract-region
(line-beginning-position)
(line-end-position))))))
(yas/expand-snippet snippet)))
But I'm still hoping to see something better/faster.
yasnippet can actually be used to create a snippet on-the-fly:
(defun yas-one-line ()
(interactive)
(let ((snippet (delete-and-extract-region
(line-beginning-position)
(line-end-position))))
(yas-expand-snippet snippet)))
Now just type:
menu.add_item($1,"$1")
and call yas-one-line. The above snippet is expanded by yasnippet!
You could try
(defvar sm-push-id-last nil)
(defun sm-push-id ()
(interactive)
(if (not sm-push-id-last)
(setq sm-push-id-last (point))
(text-clone-create sm-push-id-last sm-push-id-last
t "\\(?:\\sw\\|\\s_\\)*")
(setq sm-push-id-last nil)))
after which you can do M-x sm-push-id RET , SPC M-x sm-push-id RET toto and that will insert toto, toto. Obviously, this would make more sense if you bind sm-push-id to a convenient key-combo. Also this only works to insert a duplicate pair of identifiers. If you need to insert something else, you'll have to adjust the regexp. Using too lax a regexp means that the clones will tend to overgrow their intended use, so they may become annoying (e.g. you type foo") and not only foo but also ") gets mirrored on the previous copy).
Record a macro. Hit F3 (or possibly C-x (, it depends) to begin recording. Type whatever you want and run whatever commands you need, then hit F4 (or C-x )) to finish. Then hit F4 again the next time you want to run the macro. See chapter 17 of the Emacs manual for more information (C-h i opens the info browser, the Emacs manual is right at the top of the list).
So, for example, you could type the beginning of the line:
menu.add_item(spamspamspam
Then, with point at the end of that line, record this macro:
F3 C-SPC C-left M-w C-e , SPC " C-y " ) ; RET F4
This copies the last word on the line and pastes it back in, but inside of the quotes.

difference between calling command directly and using keybinding

I'm new to elisp, so please forgive me if the following approach is totally clumsy.
In the team I'm currently working with, there is an usual convention of closing python blocks with a pass statement (if they aren't ended by closing keywords like else or except or such). While unusual, this has the advantage that one can always recover the original indentation of the program if it is unintentionally changed (using emacs indent-region).
To get existing code in line with this convention, I wrote a small elisp function:
(defun python-check-indent ()
"Check if automatic indentation changes current indent, insert pass keyword if it does."
(interactive)
(move-beginning-of-line 1)
(skip-chars-forward " ")
(if
(< 0
(let (original)
(setq original (point))
(indent-for-tab-command)
(- (point) original)
)
)
(progn
(insert "pass")
(newline)
(indent-for-tab-command)
)
)
(next-line)
)
(global-set-key (kbd "C-`") 'python-check-indent)
The idea is simply to test whether hitting TAB would change the indentation, and insert a pass statement in that case. To facilitate processing longer blocks of code, it then advances to the next line.
When I run it using M-x python-check-indent, it does what I want (except that it moves around empty lines slightly), also when running it repeatedly to process several lines. However, when I run it repeatedly using the C-` keybinding, it starts messing up the code from the second invocation on.
So here are my questions: What is the difference between invoking a command with M-x ... and using its keybinding? And how could I change the function to be not affected by this difference?
emacs-version: GNU Emacs 23.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.35) of 2011-03-10 on black.porkrind.org
(edit) current workaround: I'm now wrapping it inside a keyboard-macro, so it's "bound" to C-x e, and behaves properly.
The general rule is that it is best to avoid complex interactive
commands in your functions because they could be affected by all sorts
of options.
(defun python-check-indent ()
"Check if automatic indentation changes current indent, insert pass keyword if it does."
(interactive)
(goto-char (line-beginning-position))
(skip-chars-forward " ")
(when (< 0
(let (original)
(setq original (point))
(python-indent-line)
(- (point) original)))
(insert "pass\n")
(python-indent-line))
(forward-line))
However, even this is probably not good because python-indent-line's behavior depends on last-command and python-indent-trigger-commands. I think it would be best if you replaced the first invocation of python-indent-line with the code which computes the target indentation instead of actually indenting, something like (nth python-indent-current-level python-indent-levels).
PS. If you still have problems, I suggest that you use edebug and step through the function.

yasnippets with % ending the line after $0 acts strange when used with AUCTeX

Yasnippet snippets that has a percent sign, %, ending a line with the last point of the snippet, $0, before the percent sign acts strange in that the cursor gets placed after the percent sign and not before it. I wonder how I can avoid this strange behavior.
Consider the following snippet:
# -*- mode: snippet -*-
# name: test snippet
# key: ts
# --
{
$0%
}
I take it that as it's activated it should insert three lines where the first contains {, the last line } and the second line % and place the cursor before % on the second line as in the following example:
{
[cursor]%
}
But what happens is the following:
{
% [cursor]
}
How can I make it so that the snippet behaves as I think it should?
My guess is that this is due to something in AUCTeX because it happens with AUCTeX activated but not in the major mode Lisp Interaction.
It works right with my configuration, but I suspect it has to do with auto indenting (mine is heavily customized so that may be the difference). Do you still see the problem if you add
# expand-env: ((yas/indent-line 'fixed))
or
# expand-env: ((yas/indent-line t))
to the snippet's header? You can also try adding $> to the line(s) that you want indented to see if that makes a difference (if it does that would narrow things down a lot). There is a note in the yasnippet code about some problems with markers changing places, but that looks like it was fixed a few years ago.
You should also check that indent-line-function has the proper value namely LaTeX-indent-line.
You could add some sit-for's to the definition of yas/indent-according-to-mode to see where point is at different stages. For example put the following in a scratch buffer, position your cursor after the end of it and type C-x C-e. Then insert your snippet as usual and it will pause for 1 second every where in the code you see a (sit-for 1). So if the cursor starts out in the wrong place, then you know the problem is before indentation, etc. You will have to watch it for every line that is indented, so you may wish to turn off indentation except for the problematic line via $>. Adding or removing sit-for's will allow you to narrow it down.
(defun yas/indent-according-to-mode (snippet-markers)
"Indent current line according to mode, preserving
SNIPPET-MARKERS."
(sit-for 1)
(goto-char (yas/real-line-beginning))
(sit-for 1)
(let ((trouble-markers (remove-if-not #'(lambda (marker)
(= marker (point)))
snippet-markers)))
(save-restriction
(widen)
(sit-for 1)
(condition-case err
(indent-according-to-mode)
(error (message "[yas] warning: yas/indent-according-to-mode habing problems running %s" indent-line-function)
nil)))
(sit-for 1)
(mapc #'(lambda (marker)
(set-marker marker (point)))
trouble-markers)))

How to make emacs behave closer to the regular editors?

I'm using Emacs 23.1.1 on Ubuntu with Emacs starter kit. I primarily work in the lua-mode.
Is there a way to stop Emacs being so smart about indentation? I'm used to the dumb editors, and press all the required keys manually.
I want to use two spaces per indent, tabs-to-spaces.
When I press RETURN, the new line indentation must match the previous line.
When I press TAB on the leading whitespace, the line contents must be indented by one indentation unit.
When I press TAB on the beginning of empty line, the cursor must move one indentation unit to the right.
Oh, and I'd like to get soft word wrap on 80th column and trim-trailing-spaces on save as well.
Update:
(Would put this in a comment, but it needs formatting)
If I use Thomas's solution, auto-indent on RETURN is "fixed", but TAB still indents weirdly:
local run = function(...)
x
"x" marks the spot where cursor appears after I type the first line and hit RETURN, TAB.
Emacs has a concept of modes, which means that depending on what type of file you're editing it provides special functionality that is useful for that file. Every buffer has one major mode associated and optionally a number of minor modes.
Indentation is one of the things that is typically mode-dependent. That is, you may have to configure indentation separately for every major-mode, because otherwise when you load a new file, its associated major mode may override your indentation settings. It's possible though to write a function that configures indentation and set up Emacs in a way that the function is invoked whenever a new major-mode is started.
In order to realize the settings you want, you'll need to run a few lines of elisp code. (Unfortunately your description of what should happen when you hit TAB leaves out some details, I've implemented the simplest version I could think of below -- if it's not what you want, that can be changed, of course.)
Put the following code in the file named .emacs in your home directory (~):
(setq-default indent-tabs-mode nil) ; use spaces for indentation
(defvar my-indentation-width 2
"The number of spaces I prefer for line indentation.")
(defun my-enter ()
"Inserts a newline character then indents the new line just
like the previous line"
(interactive)
(newline)
(indent-relative-maybe))
(defun my-indent ()
"When point is on leading white-space of a non-empty line, the
line is indented `my-indentation-width' spaces. If point is at
the beginning of an empty line, inserts `my-indentation-width'
spaces."
(interactive)
(insert (make-string my-indentation-width ? )))
(defun my-indentation-setup ()
"Binds RETURN to the function `my-enter' and TAB to call
`my-indent'"
(local-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent))
(defun delete-trailing-whitespace-and-blank-lines ()
"Deletes all whitespace at the end of a buffer (or, rather, a
buffer's accessible portion, see `Narrowing'), including blank
lines."
(interactive)
(let ((point (point)))
(delete-trailing-whitespace)
(goto-char (point-max))
(delete-blank-lines)
(goto-char (min point (point-max)))))
;; make sure trailing whitespace is removed every time a buffer is saved.
(add-hook 'before-save-hook 'delete-trailing-whitespace-and-blank-lines)
;; globally install my indentation setup
(global-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent)
;; also override key setting of major-modes, if any
(add-hook 'after-change-major-mode-hook 'my-indentation-setup)
This works for me in Emacs 23, although I may have missed some edge cases. However, these changes are so fundamental that I predict you will run into incompatibilities sooner or later with some major-modes that expect indentation to work they set it up. If you really want to get into Emacs it's worthwhile adapting the habits you inherited from other editors to the way Emacs does things.
For soft word-wrap there is a minor-mode called "longlines" which you can download from here: http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el I haven't used it so I can't tell you how well it works.
Fixing TAB and RETURN:
(global-set-key "\t" 'self-insert-command)
(global-set-key "\r" 'newline-and-indent)
Fill column (haven't tried): say ESC x customize-var, enter fill-column, set to 80.

How to get Emacs to unwrap a block of code?

Say I have a line in an emacs buffer that looks like this:
foo -option1 value1 -option2 value2 -option3 value3 \
-option4 value4 ...
I want it to look like this:
foo -option1 value1 \
-option2 value2 \
-option3 value3 \
-option4 value4 \
...
I want each option/value pair on a separate line. I also want those subsequent lines indented appropriately according to mode rather than to add a fixed amount of whitespace. I would prefer that the code work on the current block, stopping at the first non-blank line or line that does not contain an option/value pair though I could settle for it working on a selected region.
Anybody know of an elisp function to do this?
Nobody had what I was looking for so I decided to dust off my elisp manual and do it myself. This seems to work well enough, though the output isn't precisely what I asked for. In this version the first option goes on a line by itself instead of staying on the first line like in my original question.
(defun tcl-multiline-options ()
"spread option/value pairs across multiple lines with continuation characters"
(interactive)
(save-excursion
(tcl-join-continuations)
(beginning-of-line)
(while (re-search-forward " -[^ ]+ +" (line-end-position) t)
(goto-char (match-beginning 0))
(insert " \\\n")
(goto-char (+(match-end 0) 3))
(indent-according-to-mode)
(forward-sexp))))
(defun tcl-join-continuations ()
"join multiple continuation lines into a single physical line"
(interactive)
(while (progn (end-of-line) (char-equal (char-before) ?\\))
(forward-line 1))
(while (save-excursion (end-of-line 0) (char-equal (char-before) ?\\))
(end-of-line 0)
(delete-char -1)
(delete-char 1)
(fixup-whitespace)))
In this case I would use a macro. You can start recording a macro with C-x (, and stop recording it with C-x ). When you want to replay the macro type C-x e.
In this case, I would type, C-a C-x ( C-s v a l u e C-f C-f \ RET SPC SPC SPC SPC C-x )
That would record a macro that searches for "value", moves forward 2, inserts a slash and newline, and finally spaces the new line over to line up. Then you could repeat this macro a few times.
EDIT: I just realized, your literal text may not be as easy to search as "value1". You could also search for spaces and cycle through the hits. For example, hitting, C-s a few times after the first match to skip over some of the matches.
Note: Since your example is "ad-hoc" this solution will be too. Often you use macros when you need an ad-hoc solution. One way to make the macro apply more consistently is to put the original statement all on one line (can also be done by a macro or manually).
EDIT: Thanks for the comment about ( versus C-(, you were right my mistake!
Personally, I do stuff like this all the time.
But I don't write a function to do it unless I'll be doing it
every day for a year.
You can easily do it with query-replace, like this:
m-x (query-replace " -option" "^Q^J -option")
I say ^Q^J as that is what you'll type to quote a newline and put it in
the string.
Then just press 'y' for the strings to replace, and 'n' to skip the wierd
corner cases you'd find.
Another workhorse function is query-replace-regexp that can do
replacements of regular expressions.
and also grep-query-replace, which will perform query-replace by parsing
the output of a grep command. This is useful because you can search
for "foo" in 100 files, then do the query-replace on each occurrence
skipping from file to file.
Your mode may support this already. In C mode and Makefile mode, at least, M-q (fill-paragraph) will insert line continuations in the fill-column and wrap your lines.
What mode are you editing this in?