0

The title contains the crux of my question. In go-mode, tabs and auto-indentation generally works well, except when I am editing strings.

When inside of a string, I'd like the tab key to self-insert. Right now, it does nothing.

  • 3
    C-q TAB. In general, whenever you need to input something that causes self-insert-command to do something other than just appending whatever you typed to the buffer, you could avoid that behavior by prefixing your input with C-q. – wvxvw Jan 24 '16 at 16:49
  • @wvxvw Yeah, I am familiar with that. It's just tedious. It seems like there should be a way to automatically turn self-insertion back on, being as that the syntax highlighter recognizes that I am in a string. – dave mankoff Jan 24 '16 at 16:50
  • 1
    Mmmm... not really. What if you actually wanted to indent your code? I typically don't pay attention to where the point is, when I'm indenting the code. Maybe, specifically in Go one could reason that since saving the file will re-indent it anyway, this isn't very important, yet I do it regardless, especially when file is in the middle of adding changes to it. – wvxvw Jan 24 '16 at 17:03
  • 1
    Try (setq-local tab-always-indent nil) in go-mode-hook. This is not quite what you asked for (does not check if you are in a string), but it would make it easier to insert tabs. (It should be pretty easy to check if you are in a string using syntax-ppss, but I don't have the time to test it right now.) – Constantine Jan 24 '16 at 17:11
3

The function below checks if the point is in a string using syntax-ppss and inserts a TAB if it is, otherwise calls indent-for-tab-command, which is what TAB is bound to in go-mode.

(defun my-indent-or-insert-tab (arg)
  "Insert TAB if point is in a string, otherwise call
`indent-for-tab-command'."
  (interactive "P")
  (if (nth 3 (syntax-ppss (point)))
      (insert "\t")
    (indent-for-tab-command arg)))

Here's one way to get the behavior you need with the help of this function:

(add-hook 'go-mode-hook (lambda () (local-set-key (kbd "TAB") #'my-indent-or-insert-tab)))

Let me know if this works for you.

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.