11

Emacs-24.4's electric-pair-mode does one part of what you want already (this mode is very similar to autopair.el, not sure how it compares to smartparens). And c-toggle-auto-newline does the other part. But sadly they don't work right together. Please M-x report-emacs-bug so we can fix that. Instead of c-toggle-auto-newline, you can also use electric-...


10

Rather than up-list I'd recommend you use (syntax-ppss) which will return to you some parsing state. This will include info about whether you're inside a string or a comment, the position of the last open "paren" etc... E.g. you can find the kind of paren with (let ((ppss (syntax-ppss))) (when (nth 1 ppss) (char-after (nth 1 ppss)))) and it should ...


10

Use expand-region.el: Expand region increases the selected region by semantic units. Just keep pressing the key until it selects what you want. An example: (setq alphabet-start "abc def") With the cursor at the c, it starts by marking the entire word abc, then expand to the contents of the quotes abc def, then to the entire quote "abc def", then ...


8

If you install smartparens, then you can use the function sp-splice-sexp, which does exactly what you are asking for. It works on all valid types of braces and quotes, according to the current major mode. See the docs for more details. Edit: The principal advantage of smartparens over paredit (see rekado's answer) is that smartparens has better support ...


7

There's paredit which has a function paredit-splice-sexp, which might do what you want. In this example | indicates point: (hello world "no |way") ; run paredit-splice-sexp (hello world no way) Here's another example: (let ((a b)) (fo|rmat #t "meh")) ; run paredit-splice-sexp (let ((a b)) format #t "meh") It does not work on the top level, however,...


7

The correct way: fill a bug report through the proper channel of your project, e.g. org-submit-bug-report or report-emacs-bug and argue why the syntax class of your favourite character should be changed. Alternatively, you could modify the proper syntax-table, (info "(elisp) Syntax Tables"), in your init.el. Let's try Org: (with-eval-after-load 'org (...


7

smartparens has a superset of the functionality provided by autopair. Apart from providing pair insertion (which is what autopair does) , it supports manipulating what it calls "hybrid sexps" or sexps in non-lisp modes. It provides commands for navigating, marking, joining, splitting, absorbing and more for sexps among other things. I personally use ...


7

Solved. The sequence of cursor movements from my first version (in the original post) was funky. As reference for future readers, the following code should work. It obv needs the Smartparens package (which you can get from git-hub). I'm running Emacs 24.4. It works with electric-indent mode enabled or disabled. (defun my-create-newline-and-allman-...


6

An easy way to do this is by making electric-pair-pairs and electric-pair-text-pairs buffer-local and customizing them in hooks for relevant modes. Working example for org-mode: (defvar org-electric-pairs '((?/ . ?/) (?= . ?=)) "Electric pairs for org-mode.") (defun org-add-electric-pairs () (setq-local electric-pair-pairs (append electric-pair-pairs ...


5

I think you want to adjust the parameters for Blinking Parentheses. In particular, you can adjust blink-matching-delay, which: This variable specifies the number of seconds to keep indicating the matching parenthesis. A fraction of a second often gives good results, but the default is 1, which works on all systems.


5

Try this: (save-excursion (up-list) (char-before)) Note that up-list can throw, so you need to handle errors as well.


5

That message is printed when the beginning of the sexp is not visible in the window; otherwise, the opening paren is temporarily highlighted. This behavior is implemented in simple.el and is not associated with any minor mode. It is implemented using a hook that runs when a character is inserted. The actual blinking/message comes from command blink-...


4

You can use M-x blink-matching-open RET to display the message if you're right after a paren. To do it more "on the fly" you might like to try M-x show-paren-mode, tho it doesn't actually give you this message. Otherwise, you could use something like (add-hook 'post-command-hook (lambda () (if (and (not (bobp)) (...


4

You can try reading the sexp and then printing with pp. The read function expects elisp syntax, so it's going to stumble on a few special chars like ,'#. So you'll have to work around that. Since your example included commas, here's a hacky snippet that works with commas. (defun endless/pretty-print-sexp () "Pretty print the sexp after point. This ...


4

I'm not very familiar with the innards of Company, but here's how to do it with Ivy (you can re-use some code for Company as well, if you know how): (defun counsel-jedi () "Python completion at point." (interactive) (let ((bnd (bounds-of-thing-at-point 'symbol))) (if bnd (progn (setq counsel-completion-beg (car bnd)) (...


4

Emacs comes with the package show-paren-mode. Instead of temporarily blinking the corresponding parenthesis when one is written, the parenthesis corresponding to the one under the cursor is highlighted. There are also other packages with some additional bells and whistles. I prefer mic-paren. This package is faster and it can highlight both forward and ...


4

I use the code below and bind yf/replace-or-delete-pair to M-D. Example usage : with point on (, I hit M-D [ and the () pair becomes a [] pair. If you hit M-D RET instead, the pair will be removed. This code uses the syntax table, which means that for some pairs you'll have to specify the closing paren yourself. e.g. in html-mode, () can be replaced by <...


4

For those using evil you can use evil-surround which gives you the c s motion (change, surround). For your example then just do c s ( [ (motion, from type of paren, to type of paren)


3

Add this line to your emacs init file (setq electric-pair-preserve-balance nil) Reference: https://stackoverflow.com/questions/27142996/electric-pair-mode-dont-pair-if-cursor-precedes-a-non-whitespace-character


3

I agree you should try expand-region as suggested in another answer (that I'm voting for). But just in case you aren't familiar with the relevant built-in commands: C-M-u, backward-up-list C-M-SPC, mark-sexp In your first example where there is no region you could useC-M-u C-M-SPC to mark the enclosing list. If there is already a selected region these ...


3

In the case of active region, h in lispy will do what you want. See my recent intro post. In the first case, you need to M-m h. update: all options that you have with lispy Starting with: (defn foo [x] (+ x ~42|)) Here, ~ denotes the mark (the start of the region), and | denotes the point (the end of the region). With h you get: (defn foo [x] |(+ x 42)...


3

Another, terser, way is to use a smartparens insertion spec. (sp-local-pair 'c-mode "{" nil :post-handlers '(:add ("||\n[i]" "RET")))


3

Have a look at the package smartparens. It can be installed from melpa. You can enable pairs of <> with: (sp-with-modes '(c-mode c++-mode) (sp-local-pair "<" ">")) after (require 'smartparens-config) But be sure to finetune the rule. Have a look at the pair documentation and the complete documentation.


3

I use sp-unwrap-sexp. In your example, having sp-unwrap-sexp bind to M-Backspace sp-next-sexp bind to C-M-n sp-down-sexp bind to C-M-d and a cursor in the beginning of the text, I do: C-M-d to go to {text} M-Backspace to remove outer {} C-M-n 3 times to go to { { {some {extra} unneeded { M-Backspace 2 times to get {some {extra} unneeded { of course ...


2

One basic difference is that smartparens can match escaped pairs and non-punctuation mark, like if and fi; you can also customize your pairs. smartparens is really great when working regex where there are lots of escaped pairs like \(...\), since it has its own highlighting for such pairs, making working with sed, awk... much more pleasant.


2

This answer doesn't answer your question on how to configure electric-pair-mode. But it might lead you to the results you want. The wrap-region package available on Melpa might be the answer to your problem. Here is its brief description from its github: Wrap Region is a minor mode for Emacs that wraps a region with punctuations. For "tagged" markup ...


2

You can advise LaTeX-indent-level-count (defadvice LaTeX-indent-level-count (around advice-LaTeX-indent-level-count activate) (let ((LaTeX-begin-regexp (if (equal (LaTeX-current-environment) "verse") "begin\\b" "begin\\b\\|\\[")) (LaTeX-end-regexp (if (equal (LaTeX-current-environment) "verse") "end\\b" "...


2

ar-parentized2bracketed-atpt would do the task. It comes along with ar-braced2parentized-atpt and basically all respective combinations. Get it from thingatpt-transform-delimited.el of URL: https://github.com/andreas-roehler/thing-at-point-utils An abstracted class of commands transforms all delimited forms, for example: ar-delimited2bracketed-atpt ...


2

You can try using sp-raise-sexp. From the documentation (C-h f sp-raise-sexp): Unwrap the current list and kill everything inside except next expression. For example, with this: {{text}} You can: place point/cursor on the second { M-x sp-raise-sexp And you will get: {text}


2

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()") (with-temp-buffer (insert str) (goto-char (point-min)) (let (end) (when (and (...


Only top voted, non community-wiki answers of a minimum length are eligible