1

I want to input this. and get this-> in c++-mode.

I tried abbrev-mode and yasnippet, but failed.

In abbrev-mode, I defined an abbrev this to get this using . as a trigger key, but soon as I typed this. I got this->..

In yasnippet, I defined a snippet

# -*- mode: snippet -*-
# name: this
# key: this
# binding: .
# condition: (eq 0 (string-match "this" (buffer-substring (- (point) 4) (point))))
# --
this->

but got thisthis-> after input this..

If I remove the this in the last line of the snippet

# -*- mode: snippet -*-
# name: this
# key: this
# binding: .
# condition: (eq 0 (string-match "this" (buffer-substring (- (point) 4) (point))))
# --
->

everything seems fine but why we don't need this here ? Also, under this situation, the expansion broke the completion of company-irony even I've added yas-expand to irony-completion-trigger-commands.

What is the simplest way to achieve this ? Thanks !

1

From abbrev doc (emphasis mine):

A defined “abbrev” is a word which “expands”, if you insert it, into some different text.

this. is not a word, at least in c++-mode so this can't work.

My suggestion is to rebind in c++-mode the . key to a command testing if the current word is this, and inserting -> if so, . otherwise.

As pointed by @npostavs it's better to call self-insert-command in the second case, rather than simply calling (insert ".") as this ensures the processing is exactly what it was before we changed the binding. For example a prefix argument, if any, is handled as expected.

(defun this-dot-replace ()
  (interactive)
  (if (string= (thing-at-point 'word) "this")
      (insert "->")
    (call-interactively 'self-insert-command)))

(add-hook 'c++-mode-hook
  (lambda () (define-key c++-mode-map "." 'this-dot-replace)))

I make no guarantee this (pun intended?) is the simplest possible way.

  • This is basically what the 2nd yasnippet in the question does (but maybe this method won't have problems with company-irony?), which explains why this needs to be removed from the snippet body. You might want to (call-interactively 'self-insert-command) in the else branch. – npostavs Aug 22 '16 at 15:15
  • @npostavs Yes indeed. I don't use yasnippet so I didn't realize that. What would be the advantage of calling self-insert-command? (I understand this is what happens when the key is not rebound). – JeanPierre Aug 22 '16 at 17:21
  • It probably doesn't matter much, but self-insert-command handles a prefix argument. (Also, I think some extensions look at this-command to see if it's self-insert-command and might do different things in that case) – npostavs Aug 22 '16 at 18:21
  • @npostavs Good point. I updated the answer. – JeanPierre Aug 22 '16 at 21:48
  • @JeanPierre Thanks for you answer and sorry for late reply ! I mean this is an abbrev and use . as a trigger key. I try your function but it seems that it also has problem with company-irony and won't trigger completion even I 've added insert(which is used in the if case) to irony-completion-trigger-commands. – Edityouprofile Aug 24 '16 at 8:35

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.