0

I recently added a modification to my Enter keybinding based on an answer (if you can't see it, this is a link) on this SE. It was good and I liked it. So of course...

Now I want to add MORE dwim behavior. Specifically, while programming, if I'm typing a comment, I'd like to press the Enter key and the next line automatically also be a comment. E.g. in Ruby, if I have # blah blah| where | is the cursor, I want to press Enter and see

# blah blah
# |

So the question - what is preferred / philosophically correct with emacs?

Do I:

  1. Expand my enter-key-dwim function to just check for more things?
  2. Do I switch to two small functions that work as a local-buffer post-command-hook and only run if the last command was the Enter key?
  3. Put an advice around the Enter key behavior?
  4. Something else?

One comment says that I generally want to avoid post-command-hook because it gets run after every keystroke as well, which seems to be a vote against option 2.

  • 1
    Could you please clarify what you'd like to do? Please also reframe the question to make it a little more concrete. "Philosophically correct" will probably elicit a lot of opinion-based answers, which don't fit well with the SE format and which we try to avoid. – Dan Feb 16 '16 at 19:16
  • 1
    FWIW, a useful rule of thumb is to avoid post-command-hook if you reasonably can. Anything in that hook will run after every single command you enter. Doing so is probably going to be overkill, and depending on what you're asking Emacs to do, could be a big performance issue as well. – Dan Feb 16 '16 at 19:18
  • 1
    Out of curiosity could you say what your current enter-key-dwim does? Maybe just edit a link to the Emacs SE answer you mentioned into your question... – Omar Feb 16 '16 at 20:02
  • @OmarAntolín-Camarena Done – Trevoke Feb 17 '16 at 1:30
  • @Dan sounds like I shouldn't use option 2. – Trevoke Feb 17 '16 at 1:31
3

Just make your own hook! Here's an example that does what the answer you linked does: add an extra newline if you are between brackets. You can add other hooks without changing newline-dwim.

(defun newline-dwim ()
  (interactive)
  (run-hooks 'newline-hooks))

(defun basic-newline ()
  (newline)
  (indent-for-tab-command))

(defun extra-newline-inside-braces ()
  (when (and (eq ?\( (char-syntax (char-before)))
             (eq ?\) (char-syntax (char-after))))
    (save-excursion (basic-newline))))

(add-hook 'newline-hooks #'basic-newline)
(add-hook 'newline-hooks #'extra-newline-inside-braces)
| improve this answer | |
  • I love it. I don't know why I didn't think of it. Thanks! – Trevoke Feb 18 '16 at 15:56
1

Behavior of several commands is customizable. The features in question often are called "electric". python-mode.el would provide that comment-insert when py-electric-comment-p is set to t.

| improve this answer | |
  • Thanks. I guess my problem is that I want this for every mode that inherits from prog-mode and some that don't (like rust-mode, IIRC). So I'd rather not depend on whether the major mode implemented that or not. – Trevoke Feb 17 '16 at 14:57
0

Well, since I already had a tool that went most of the way (my current enter-key-dwim) and emacs already has a tool that did what I want, I sidestepped the philosophical question and did the pragmatic thing; I replaced "newline and indenting" with "newline and indenting also with special behavior if inside a comment" (indent-new-comment-line).

This is acceptable for me right now. I don't mind having to press C-j for a non-comment-newline ... Yet.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.