Emacs lisp code indentation - emacs

In emacs-lisp mode whenever i insert a closing brace i prefer to have indented to the same column like the corresponding opening brace. How is that possible? If i have eg in my init.el
(defadvice isearch-forward-regexp (before kill-ring-save-before-search activate)
"Save region (if active) to kill-ring before starting isearch. So that region
can be inserted into isearch easily with C-y."
(when (region-active-p)
(kill-ring-save (region-beginning) (region-end))
) ;; this should be under (when
) ;; this should be under (defadvice

It seems that you want to align the close parens to be able to
visually match them to the opening ones. You can do that with
show-paren-mode instead - it's much better at that job.
As pointed out by others, and I fully agree, the hanging parens are
very annoying and painful to look at - don't make a habit out of using
them. I've authored a minor mode for editing Elisp which might be
interesting for you - lispy-mode:
Pressing i will auto-indent an s-expression, eliminating
the hanging parens.
Pressing d will switch from one side of s-expression to
the other: a quick way to see what the current list contains.
Pressing m will toggle the region selection on the
current list: you can see what it contains even more clearly.

Related

Emacs ispell goto previous word?

As I'm using M-x ispell to check LaTeX code
I use SPC to skip a lot of entries that should not be corrected.
But then I sometimes skip an actual misspelled word.
What's the key to go back with ispell to previous word?
I checked the source code of ispell.el, and unfortunately, there seems to be no key-binding for this (the keys are actually hardcoded in the function ispell-command-loop). As a quick hack, if you don't mind your buffer-local mark-ring to be cluttered, you could do something like this:
(defadvice ispell-command-loop (after leave-breadcrumbs activate)
"Leave a trail in the mark-ring when waiting for user input"
(push-mark (point) t))
Then you can always go back to previous errors with C-u C-SPC. Alternatively, you can create your own mark-ring for this function.

Emacs close paren jumps to already existing close-paren when editing clojure code

If having this piece of code in an emacs buffer:
(if (> x 5
true
false))
When I try to edit it in order to fix the parenthesis, something very annoying is happening! When I try to add a closing parenthesis to the if condition, emacs is making the cursor jump to the closing parenthesis after 'false' instead of adding a new parenthesis after 5.
Is this part of some mode, maybe clojure-mode? Do you know how may I fix this? What is this useful for?
It sounds like you're using paredit. Did you install it like recommended on the project page?
As to what it's good for? It's good for editing lists. But you have to buy
into whe whole system, or you'll end up really confused. See the wiki
page.
Do you have this section in your ~/.emacs.el? Just remove it.
;; (require 'paredit) if you didn't install via package.el
(defun turn-on-paredit () (paredit-mode 1))
(add-hook 'clojure-mode-hook 'turn-on-paredit)
Yes, paredit is "different". It will always make sure your parenthesis balance. See http://www.emacswiki.org/emacs/PareditCheatsheet .
For your code, place the cursor beneath the first closing parenthesis and press C-left. Repeat the exercise and it will have moved to where you want it.
Cut&paste (kill & yank in emacs lingo) also allow you to manually screw with the balanced parenthesis, so until you get used to paredit it may be easier to use. Good luck!
how to verify that paredit is the offender
You can type C-h k ) while in your Lisp buffer to see what ) is bound to. If it is bound to paredit-close-round then yes paredit is the offender.
how to disable paredit when you don't know what's triggering it
Try auramo's answer in another thread
or if that doesn't work, try this:
(eval-after-load 'paredit
'(defalias 'paredit-mode 'ignore))
If you are curious about what is triggering paredit-mode in your Emacs, use M-x debug-on-entry RET paredit-mode RET
learning to live with paredit
But still I must encourage you to continue using paredit. Let's keep using paredit and let's see solutions for problems you posed. You asked "Do you know how may I fix this?" I'll just assume you are asking how to fix that if form. Marius Kjeldahl gave you solution which uses paredit-forward-barf-sexp, now in general, if you have some Lisp code where you see that some parens are in wrong places and you want to fix that, you can simply temporarily disable paredit-mode in that buffer (by typing M-x paredit-mode) and then fix your code and then enable paredit-mode again (by typing M-x paredit-mode again). Another thing to consider is that Emacs has undo, so if you arrive at (if (> x 5 true false)) through some action, you can undo that action and start over. Undo is bound to C-z if you use CUA mode.
Still you might find bindings of C-left, C-right to be weird, so you might want to use the following setup:
(eval-after-load 'paredit
'(progn
;; paredit-forward-barf-sexp is usually bound to <C-left>, C-}.
;; here we unbind it from <C-left>
;; so that one can continue to use <C-left> for movement.
(define-key paredit-mode-map (kbd "<C-left>") nil)
;; paredit-forward-slurp-sexp is usually bound to <C-right>, C-).
;; here we unbind it from <C-right>
;; so that one can continue to use <C-right> for movement.
(define-key paredit-mode-map (kbd "<C-right>") nil)
;; paredit-backward-kill-word is bound to M-DEL but not to <C-backspace>.
;; here we bind it to <C-backspace> as well
;; because most people prefer <C-backspace> to M-DEL.
(define-key paredit-mode-map (kbd "<C-backspace>") 'paredit-backward-kill-word)))
You asked "What is this useful for?" by which you may be asking two things:
Why is paredit-close-round useful?
Why does paredit have to bind paredit-close-round to ) when it could have bound it to better keys?
Best way to think of paredit-close-round is to think of it as a counterpart to C-M-u. Move point to | in the following code and try press C-M-u several times to see what happens, and then move point to | again and try press C-M-- C-M-u (i.e. type -u while Control and Alt are on) several times to see what happens.
(when t
(when t
(blah)
(blah))
(when t
(blah | blah)
(blah))
(when t
(blah)
(blah)))
C-M-u is useful for selecting expressions; in order to select an enclosing form or forms, you press C-M-u several times and then C-M-SPC several times. C-M-- C-M-u is useful for evaluating an enclosing form; you press C-M-- C-M-u several times and then C-x C-e to eval the enclosing form.
paredit-close-round basically does what C-M-- C-M-u does.
Why is it an OK thing that paredit binds ) to a command that does something other than simply inserting a close paren? Because you are not supposed to insert a closing parenthesis by yourself. Whenever you insert an open paren, a close paren is also inserted automatically. Whenever you want to change (blah) (blah) to ((blah) (blah)), you simply select the two blah forms and press (.

Decrease indentation on a couple of lines

Is there some way I can mark text in emacs and shift it left (removing starting spaces) by space/Tab granularity?
Same way I would do on some other editor with Shift+Tab.
Select your region;
Type C-u followed by the number of spaces you want to indent (negative number if you want to decrease indentation);
Use C-x TAB (by default bound to indent-rigidly) to apply the indentation to the region.
This is much more cumbersome than S-TAB, but it is IMHO some kind of last resort in case Emacs formatting doesn't solve your problem.
EDIT: much better solution: Shift a region or line in emacs (accepted answer). This is what I'm currently using in Emacs for changing indentation. WARNING: involves some Emacs Lisp.
This might be simpler and more visually intuitive: first make sure cua-mode is enabled (M-x cua-mode toggles it). Then go to the start of the line and press C-return. A red rectangle appears. Now move your cursor down and right to grow the rectangle as needed. Then press C-d to delete it. That's it.
I come across this problem often when the major-mode doesn't dictate any automatic indentation (or when it messes up).
There is a lot more you can do with cua-mode's rectangles, see http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
Generally emacs places things where the current style dictates when you hit <TAB>, so naturally it's a little different here. The closest thing that comes to mind is M-\ which collapses horizontal whitespace around point. If you want to remove a "rectangle" of space before the lines, then delete-rectangle might be more appropriate, which you can do by setting mark and moving point to select the rectangle and then using C-x r d.
It sounds like the problem you're trying to solve is incorrect indentation of code when you're cutting/pasting. You can solve that by automatically re-indenting the text with something like the following.
Note: Using a prefix argument forces no re-indentation (C-u C-y), plus there's the size threshold variable.
;; automatically indenting yanked text if in programming-modes
(defvar yank-indent-modes '(emacs-lisp-mode
c-mode c++-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
lisp-interaction-mode
LaTeX-mode TeX-mode)
"Modes in which to indent regions that are yanked (or yank-popped)")
(defvar yank-advised-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur.")
(defun yank-advised-indent-function (beg end)
"Do indentation, as long as the region isn't too large."
(if (<= (- end beg) yank-advised-indent-threshold)
(indent-region beg end nil)))
(defadvice yank (after yank-indent activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(member major-mode yank-indent-modes))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

emacs: evaluate blink-matching-open when cursor highlights a parenthesis

Recently while editing lisp code in emacs, I have been frustrated in tracking matching parenthesis. (show-paren-mode t) helps when the matching parenthesis is visable within the buffer along with its match, and (setq blink-matching-paren t) is helpful when writing the matching parenthesis. Is there a way to hook show-paren-mode so that the blink-mathing-open function evaluates as part of the "show" process? In this manner, I can place the cursor up to a parenthesis and know what it matches against without deleting and retyping it.
Thanks,
SetJmp
Try this
(defadvice show-paren-function (after blink activate)
(when (= ?\) (char-before (point)))
(blink-matching-open)))
Or, just use C-M-b and C-M-f to move back and forth between the point and the corresponding parenthesis.
My favorite paren package is mic-paren, which shows you the matching paren like you describe - it even works when the matching paren is offscreen (it shows some info in the echo area).
Download and put somewhere in your load-path, and add this to your .emacs:
(require 'mic-paren)
(paren-activate)
There are a number of configuration options you can choose from, read the comments at the top of the package.

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.