Emacs: Matching parenthesis when cursor is ON closing parenthesis - emacs

{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.

Related

Emacs lisp code indentation

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.

Close HTML tags as soon as one opens them

I'd like the corresponding, closing HTML tag to be automatically inserted whenever I open one.
So if I type
<div>
I should get
<div></div>
Without having to call to sgml-close-tag myself.
How to achieve this?
Rather than calling a hook function after every single key-stroke, it makes sense to only call it after a > was typed. This can be achieved by rebinding the > character in the keymap that sgml-mode uses.
In addition, sgml-close-tag shouldn't get called if the tag is already closed. Therefore, the following code adds a simple regexp check for that:
(defun my-sgml-insert-gt ()
"Inserts a `>' character and calls
`my-sgml-close-tag-if-necessary', leaving point where it is."
(interactive)
(insert ">")
(save-excursion (my-sgml-close-tag-if-necessary)))
(defun my-sgml-close-tag-if-necessary ()
"Calls sgml-close-tag if the tag immediately before point is
an opening tag that is not followed by a matching closing tag."
(when (looking-back "<\\s-*\\([^</> \t\r\n]+\\)[^</>]*>")
(let ((tag (match-string 1)))
(unless (and (not (sgml-unclosed-tag-p tag))
(looking-at (concat "\\s-*<\\s-*/\\s-*" tag "\\s-*>")))
(sgml-close-tag)))))
(eval-after-load "sgml-mode"
'(define-key sgml-mode-map ">" 'my-sgml-insert-gt))
If you like paredit (and if you're an emacs user, chances are you do), you may be interested in tagedit, an emacs package written by Magnar Sveen that provides paredit-like features for editing html.
The library is here: https://github.com/magnars/tagedit, and can be installed through Melpa/Marmalade (package-install tagedit).
If you enable the experimental features (tagedit-add-experimental-features), then it will automatically close tags for you and keep the corresponding closing tag text matching the opening tag text. That's on top of being able to splice, slurp, barf and all the other crazy things that paredit lets you do when working with balanced expressions...I think it's great!
I'm using yasnippet for this purpose.
To type shortcuts this answer, like <kbd>C-o</kbd>, I have the following snippet:
# -*- mode: snippet -*-
# name: kbd
# key: kbd
# --
<kbd>$0</kbd>
So I type kbdC-o and it get's expanded to <kbd></kbd> with cursor
right in the middle. You can have the same behavior for div.
You may eval this on your sgml-buffer or add ii to your sgml-hook:
(add-hook 'post-self-insert-hook
(lambda () (and (eq (char-before) ?>) (sgml-close-tag))) nil t)
Whenever you insert a ">", the function sgml-close-tag will be run for you

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.

Does SLIME have a shortcut key to comment a block of Lisp code in slime-mode?

I'd rather not have to manually add semicolons to every line.
Specs:
Aquamacs 2.1 (Emacs 23.2)
SLIME 2010-11-16
MacPorts CLISP 2.49
Mac OS X 10.6.4
MacBook Pro 5,1
If the block of the code is a Lisp form and you would like to comment this form out, you can use slime-insert-balanced-comments (I use M-x s-i-b-c and SLIME expands the command automatically).
To uncomment it use slime-remove-balanced-comments (M-x s-r-b-c).
I found these commands very useful.
Also I put the following block in my .emacs file:
;; Comment function
(defun comment-or-uncomment-this (&optional lines)
(interactive "P")
(if mark-active
(if (< (mark) (point))
(comment-or-uncomment-region (mark) (point))
(comment-or-uncomment-region (point) (mark)))
(comment-or-uncomment-region
(line-beginning-position)
(line-end-position lines))))
(global-set-key (kbd "C-;") 'comment-or-uncomment-this)
I guess, it was from here.
UPD: I forgot to mention that despite the fact that slime-insert/remove-balanced-comments works just fine with paredit, the C-; command can be a big pain to use on lines that have uneven number of parentheses on them. In case of lines like
((blah|-blah)))))))
(where | means the point), I first press ) as many times as necessary to break the line in the correct place and to detach outer closing parentheses from this line (in this case it would be two times). Paredit helps here: it reorganizes the s-exp so that closing parentheses are split in two parts and thus you can comment the line out without breaking outer s-exps. In the last example the line turns into:
((blah-blah))
|)))))
and the first line can be safely commented out with C-;.
Look here:
http://wwwx.cs.unc.edu/~sud/tips/Emacs_Tips.html
It's M-x comment-region, but there's no default key binding for it.