2

What is the proper way to compute the actual string length in emacs? By actual I mean I want the number of chars inserted into a buffer after doing something like (insert "\nhi") or (insert "\x100").

For example, I have a snippet for writing expand-abbrev-hook's but it doesn't get the string length right when there are escaped characters, eg.

# -*- mode: snippet -*-
# name: expand abbrev
# key: eah
# --
("${1:abbrev}" ["${2:pre}${3:post}" ${3:$(length yas-text)} nil nil] expand-abbrev-hook :system t)

The value computed in $3 should land the cursor back between $2 and $3 after expansion, but if there are strings with "\n" for example, then length is off a bit.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • `length` applied to a string gives you the number of its characters. `string-bytes` gives you the number bytes in its string argument. `string-width` gives you the number of columns used to display the string in the current buffer (with TAB chars interpreted as occupying `tab-width` columns). – Drew Jan 20 '17 at 20:00
  • 1
    @Drew ok thanks, so I would have to roll my own I suppose since `(string-bytes "\x100")` => 2 and `(string-width "\n")` => 0 – Rorschach Jan 20 '17 at 20:29

0 Answers0