reddit / emacs
How to type pointer arrow -> in C quickly?
J-ky · 2022-08-02 02:28
We often need to insert the arrow (->) to dereference the pointer and access its member.
Is there a way to do this quickly?
CorysInTheHouse69 · 2022-08-02 03:05
How would you want this done? Like through a key bind or you press a key like “-“ and it inserts “->”?
Schievel1 · 2022-08-02 04:25
I type - and then >.
But honestly, you can put that on a key chord. Is it worth it?
azfrak · 2022-08-02 04:36
Using [key chords](https://github.com/emacsorphanage/key-chord) (untested though)
(require 'key-chord)
(key-chord-mode 1)
(key-chord-define c-mode-map "--" "->")
then type `--` quickly and will insert pointer arrow, but of course could change to any key chord that you like.
J-ky · 2022-08-02 04:36
Since using shift for the ">" is a bit slow for me, I thought it would be good to convert two consecutive dots into an arrow in C/C++.
I quickly put up a minor mode for that.
```
(defun shapeless-c-arrow-insert-dot ()
(interactive)
(if (looking-back "\\.")
(progn
(backward-delete-char 1)
(insert "->"))
(insert ".")))
(define-minor-mode shapeless-c-arrow-mode
"Quickly insert arrow operator. ->"
:keymap
(let ((map (make-sparse-keymap)))
(keymap-set map "." #'shapeless-c-arrow-insert-dot)
map))
(add-hook 'c-mode-hook #'shapeless-c-arrow-mode)
(add-hook 'c++-mode-hook #'shapeless-c-arrow-mode)
```
Maybe it is a bit overkill.
ndamee · 2022-08-02 05:59
Some people don't like shifted keys, because it slows down typing. I too avoid unnecessary shifting if possible. It's more convenient, especially in case of such operators which are used a lot.
Schievel1 · 2022-08-02 07:29
Understandable.
You could put -> on - instead.
I think - alone isn’t used very often anyway in C.
pajuch · 2022-08-02 10:11
Have no use for this currently but have added to my config.. Thanks
nitroll · 2022-08-02 12:00
These lines has been in my configuration for a long time, there might be smarter ways to do the same:
(global-set-key (kbd "H-C-.") '(lambda () (interactive) (if (char-equal (char-before) ?\s) (insert "->") (insert " ->")) (if (not (equal (char-after) ?\s )) (insert " ") (forward-char))))
(global-set-key (kbd "H-C-,") '(lambda () (interactive) (if (char-equal (char-before) ?\s) (insert "<-") (insert " <-")) (if (not (equal (char-after) ?\s )) (insert " ") (forward-char))))
(global-set-key (kbd "H-C->") '(lambda () (interactive) (if (char-equal (char-before) ?\s) (insert "=>") (insert " =>")) (if (not (equal (char-after) ?\s )) (insert " ") (forward-char))))
(global-set-key (kbd "H-C-<") '(lambda () (interactive) (if (char-equal (char-before) ?\s) (insert "<=") (insert " <=")) (if (not (equal (char-after) ?\s )) (insert " ") (forward-char))))
ideasman_42 · 2022-08-02 12:02
You might want to exclude this when in strings/comments, (see `(syntax-ppss)`), also, use \`save-match-data\` to avoid modifying that, or better, use \`char-before\` since \`looking-back\` is slow.
J-ky · 2022-08-02 12:57
Thank you so much. But I don't understand the `save-match-data` part, how to use that?
```
(defun shapeless-c-arrow-insert-dot ()
(interactive)
(let ((sp (syntax-ppss)))
(if (and (not (nth 4 sp))
(not (nth 3 sp))
(eq (char-before) ?\.))
(progn
(backward-delete-char 1)
(insert "->"))
(insert "."))))
```
J-ky · 2022-08-02 12:59
What? Isn't a single '-' the minus sign in C?
Phil-Hudson · 2022-08-02 15:58
Don't forget that `define-key` etc allow you to specify text to insert. You don't have to call a command.
(define-key c-mode "`" "->")
I chose `\`` because it's almost the only unshifted character (on a US or UK keyboard) that isn't a C/C++ operator.
BTW, obviously `--` is not available in C/C++! It's one of the most commonly used operators.
Schievel1 · 2022-08-02 18:32
Yes, but it’s not like this is used everywhere. Like _ is in every other variable name. Or {.
If you need a - and it would give you ->, you can just type backspace
ideasman_42 · 2022-08-02 18:53
If you remove the `looking-at` / `looking-back` you don't need `save-match-data`, otherwise you could do:
`(save-match-data (looking-back "."))`