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.
Related
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.
{It has been asked before: Emacs: highlight matching paren when cursor is on it, not after it but none of the answers are satisfactory}
I am using mic-paren with following .emacs settings (although the problem exists with all similar emacs packages, so it seems to be some kind of default emacs behavior)
(paren-activate)
(setq paren-match-face 'highlight)
(setq paren-sexp-mode t)
which highlight the all the text between two parenthesis. It works well when the cursor is ON opening parenthesis but from the other side, I have to put my cursor AFTER the closing parenthesis. This results in strange behavior when used with slime (which requires the cursor to be put ON the closing parenthesis to display general usage information and such). Is there any way to change this behavior and make emacs match parenthesis when the cursor is ON closing parenthesis?
EDIT: Minor grammar fix
Don't know about mic-paren, but using the built-in show-paren-mode, you can get what you want in Emacs-24.4 with:
(defun my-show-paren-any (orig-fun)
(or (funcall orig-fun)
(if (looking-at "\\s)")
(save-excursion (forward-char 1) (funcall orig-fun)))))
(add-function :around show-paren-data-function #'my-show-paren-any)
The following works for `mic-paren'. But, it has some afterglow;-). The opening delimiter is highlighted if the cursor is on the closing delimiter or just behind it.
(defadvice mic-paren-highlight (around cursorOnClosing activate)
"Dirty hack to highlight sexps with closing delim below cursor"
(if (eq (char-syntax (following-char)) ?\) )
(let ((paren-priority 'close))
(save-excursion
(forward-char)
ad-do-it))
ad-do-it))
Naturally, to make this work you need to install mic-paren correctly. Just follow the installation guide in mic-paren.el cited here:
Installation:
Place this file in a directory in your 'load-path and byte-compile
it. You can surely ignore the warnings.
Put the following in your .emacs file:
(GNU Emacs supports mic-paren only within a window-system but XEmacs
supports mic-paren also without X)
(when (or (featurep 'xemacs) window-system)
(require 'mic-paren) ; loading
(paren-activate) ; activating
; set here any of the customizable variables of mic-paren:
; ...
)
Restart your Emacs. mic-paren is now installed and activated!
To list the possible customizations enter C-h f paren-activate' or
go to the customization groupmic-paren-matching'.
EDITS:
follow Stefan's hint about (featurep 'xemacs)
What's important to remember here is that Emacs's point is between two characters, not on a character. For the show-paren facility to trigger, the point must be immediately outside a paren, whether opening or closing. The observed dissymmetry is caused by the block cursor being placed, arbitrarily, on the character after (rather than before) the point.
If this disturbs you, then a workaround would be to use a line cursor rather than a block cursor.
show-paren-mode is being enhanced for the next but one release, such that it will trigger also with the point immediately inside a paren.
The following advice does what you want. When the block cursor is "on" the opening parenthesis, the closing parenthesis is highlighted. When the block cursor is "on" the closing parenthesis, the opening parenthesis is highlighted.
(advice-add show-paren-data-function
:around
(lambda (orig-fun)
(cond ((looking-at "\\s(")
(funcall orig-fun))
((looking-at "\\s)")
(save-excursion (forward-char 1) (funcall orig-fun))))))
Refer to my answer on the Emacs Stack Exchange site: https://emacs.stackexchange.com/a/63458/17507. In that answer, I provide a full explanation of the code above, and also a suggested variant.
looking for an equivalent cut and paste strategy that would replicate vim's 'cut til'. I'm sure this is googleable if I actually knew what it was called in vim, but heres what i'm looking for:
if i have a block of text like so:
foo bar (baz)
and I was at the beginning of the line and i wanted to cut until the first paren, in visual mode, I'd do:
ct (
I think there is probably a way to look back and i think you can pass more specific regular expressions. But anyway, looking for some emacs equivalents to doing this kind of text replacement. Thanks.
Here are three ways:
Just type M-dM-d to delete two words. This will leave the final space, so you'll have to delete it yourself and then add it back if you paste the two words back elsewhere.
M-z is zap-to-char, which deletes text from the cursor up to and including a character you specify. In this case you'd have to do something like M-2M-zSPC to zap up to and including the second space character.
Type C-SPC to set the mark, then go into incremental search with C-s, type a space to jump to the first space, then C-s to search forward for the next space, RET to terminate the search, and finally C-w to kill the text you selected.
Personally I'd generally go with #1.
as ataylor said zap-to-char is the way to go, The following modification to the zap-to-char is what exactly you want
(defun zap-up-to-char (arg char)
"Like standard zap-to-char, but stops just before the given character."
(interactive "p\ncZap up to char: ")
(kill-region (point)
(progn
(search-forward (char-to-string char) nil nil arg)
(forward-char (if (>= arg 0) -1 1))
(point))))
(define-key global-map [(meta ?z)] 'zap-up-to-char) ; Rebind M-z to our version
BTW don't forget that it has the ability to go backward with a negative prefix
That sounds like zap-to-char in emacs, bound to M-z by default. Note that zap-to-char will cut all the characters up to and including the one you've selected.
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)))))
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.