A bit long in the tooth, but still seems to work fine, highlight-sexp will do what you want. Move out of your inner if, it highlights the entire for block. Out again, the entire outer if. Out again, your function. Note that by default it highlights inside the "parens" of each scope.
It should work like this: [...] Use cursor keys. Highlighting disappears immediately!
I want show-paren-delay only when navigating the code. If I actually type a new parenthesis, I want immediate highlighting.
You can make it work like that with the following code. show-paren-clear-highlight should be added to Emacs since it's accessing internal ...
Use up-list or backward-up-list to move forward or backward to the first enclosing bracket, then forward-list or backward-list to locate the matching bracket. Delete the closing bracket then the opening bracket (in this order, because deleting the opening bracket moves the position of the closing bracket.
(defun delete-enclosing-parentheses (&optional ...
You can use ignore-errors. You should also use let instead of setq for c to keep the scope of c local.
(defun count-and-move ()
(let ((c 0))
For down-list and its worker scan-lists there doesn't appear to be an option to prevent the error, so we would need to handle it somehow.
In some instances, a simplistic use of ignore-errors is fine. E.g.:
In this instance that's problematic, because down-list will return nil even when it works, so we probably want to handle ...
The reason for the mismatch is the code contains an angled bracket (">") which is treated as a matching paren.
I don't know the solution for how to specify that orgmode emacs-elisp should ignore angled brackets when matching parens (but I would like to know a solution).
Note: I posted this as an "answer" because I don't have enough reputation points to ...
It is more usual to search for sexps than for embraced expressions.
Maybe that is the reason why you did not get a fast answer.
The usual way to search for stuff within program code avoiding comments and strings is to test (nth 8 (syntax-ppss)):
The string or comment start position. While inside a comment, this is the position where the comment began; ...
Here is a potential solution:
(defun org-mode-<>-syntax-fix (start end)
(when (eq major-mode 'org-mode)
(while (re-search-forward "<\\|>" end t)
(when (get-text-property (point) 'src-block)
;; This is a < or > in an org-src block
(put-text-property (point) (1- (point))
As phils already pointed out in his comment forward-sexp is the function that parses balanced expressions.
You could modify your example as follows to match the balanced expression with vanilla emacs:
#!/usr/bin/env emacs --script
(setq str "foo(bar(),hee()) out()")
(when (and (...
I'd probably just use this. But all of the answers provided so far are pretty much the same. The idea is, in each iteration, to increment and return the counter but "handle" a down-list error by returning nil. ignore-errors returns nil for an error or whatever evaluating its arg returns - in this case, the counter value, c.
(defun count-and-move ()
Adding an answer based on @gilles-so-stop-being-evil 's current answer which works when the point is on the opening bracket as well as the last bracket, it also error checks and prints useful status upon completion.
(defun find-surrounding-brackets (pos)
"Return a pair of buffer positions for the opening & closing bracket positions.
Or nil when ...
The following mutilating shortening of show-paren--locate-near-paren does what you want.
Copy these lines into your init file and restart Emacs.
(defun show-paren--locate-near-paren-ad ()
"Locate an unescaped paren \"near\" point to show.
If one is found, return the cons (DIR . OUTSIDE), where DIR is 1
for an open paren, -1 for a close paren, and OUTSIDE ...
You can see a list with all the colors set by emacs with M-xlist-faces-display RET.
There you can search for the faces corresponding to parenthesis matching: show-paren-match in this case. If you click on it to customize it, test it or even save it. If you choose to save, this setting will be saved to your custom-file (C-hvcustom-fileRET - for more info).
Try invoking emacs --debug-init and see if you get a backtrace. The symptom you are describing is probably an error half way through the init file which is aborting the rest of the load.
EDIT in response to the comment: emacs looks first for ~/.emacs (or ~/.emacs.el) and if it does not find that it looks for ~/.emacs.d/init.el (where ~ stands for your home ...
If you don't want such highlighting at all then turn off show-paren-mode.
If you do want it, but you want it to use a different color:
Put your cursor on that highlighted text and hit C-u C-x =. Near the bottom of buffer *Help* it tells you what face is used for the highlighting.
M-x customize-face and enter that face. Then change its attributes (e.g. ...