From the documentation of string-width string
:
This function returns the width in columns of the string
string
, if it were displayed in the current buffer and the selected window.
For me this imply that (string-width "1\n12\n123")
should return 3
, while in reality it returns 6
.
I can accept this (even though in my opinion this behavior contradicts documentation), but I also would like to know if there is an easy way to find
the width in columns of the string
string
, if it were displayed in the current buffer and the selected window.
I can imagine implementation when I first split string with \n
, then find string-width
for each piece and take maximum, but, somehow, I don't like this solution very much.
6
is correct. Absent a modification of thebuffer-display-table
, you have1
, which is one column; you have12
, which are two columns, and you have123
, which are three columns, giving a grand total of6
columns.(length (split-string "1\n12\n123" "\n"))
if the result desired is3
?the width in columns of the string, if it were displayed
is the length (in columns) of the longest line. Here the desired number is3
, because when you display that string the width of the text will be3
, I mean, you can't cross the sixth column in the buffer. So, no,(length (split-string "1\n12\n123" "\n"))
is not what I want, as it count the number of lines.\n
, thus you can't split the string by\n
in this situation.