reddit / emacs

why insert-char function is not working ?

LowCom · 2022-06-06 03:21
I tried (insert-char "LATIN SMALL LETTER A") (insert-char "263A") Both are not working. I am entering them using M-:, eval-lisp.
ieure · 2022-06-06 03:39
The documentation for that function is wrong or misleading. It indicates that you can use it how you're trying to, but in fact, you can only insert a value which satisfies `characterp`, which those strings don't.
LowCom · 2022-06-06 03:40
How do I actually do it then?
ieure · 2022-06-06 03:42
Haven't the slightest. You'd need to tear into `read-char-by-name` to see what it's doing, since that's the one that's used when you enter a Unicode character interactively.
onetom · 2022-06-06 03:45
I'm getting a (wrong-type-argument characterp "263A") error, which is pretty self-explanatory, imho. check the docs for the insert-char. The #x263A works for example. the docs also mentions the read-char-by-name function being used in interactive mode. if u peak into the source of that function, then u can see it using the char-from-name function to process the interactive input. so (insert-char (char-from-name "...")) should work.
onetom · 2022-06-06 03:47
if u don't know how to get docs or the source for functions, then this tutorial can help you (iirc): https://youtu.be/46w9e4GAjsU
onetom · 2022-06-06 03:51
To be clear, I also had no idea how to do this, so I just looked it up. If you want to use Emacs more extensively, you are expected to learn how to answer these kinds of questions, because it's not really feasible to keep asking the community about things, which take a few seconds for yourself to discover.
_viz_ · 2022-06-06 03:53
(insert-char (gethash "LATIN SMALL LETTER A" (ucs-names))) for the first one.
00-11 · 2022-06-06 03:54
`insert-char` needs a character as its arg. If you have the Unicode name of a character then you can get the character that has that name by using function `char-from-name` - you pass it a string with the name. For example: (char-from-name "LATIN SMALL LETTER A") ; Returns char `a` (`?a`), aka integer `97`. That function looks up the name in the hash table that's returned by function `ucs-names` (and is cached in the variable of the same name).
usaoc · 2022-06-06 04:39
Or use one of those obscure [read syntaxes](https://www.gnu.org/software/emacs/manual/html_node/elisp/General-Escape-Syntax.html).
eli-zaretskii · 2022-06-06 12:28
Why don't you use its key binding, `C-x 8 RET`? Then both examples will work for you.
LowCom · 2022-06-08 03:15
I need to understand or use this to program in elistp to change a buffer
eli-zaretskii · 2022-06-08 16:39
From a program, just use the character's code, as in `#x263a`. If you must use Unicode names (but why?), you can use the `\N{NAME}` construct, as in `\N{LATIN SMALL LETTER A}`.
LowCom · 2022-06-08 16:50
What does #x mean? Is it hexadecimal? Where can I find all these character codes for every alphabet?
eli-zaretskii · 2022-06-09 06:08
> What does #x mean? Is it hexadecimal? Yes, hexadecimal. This is standard Emacs notation for integer values, see "Integer Basics" in the ELisp Reference manual. > Where can I find all these character codes for every alphabet? I don't understand. The value 263A hex was from your original post. Only you can tell where and how you found it. As for "every alphabet": there's only one alphabet for this purpose -- the Unicode codepoints of the characters. That is what Emacs uses. For the more general question of finding the codepoint of an arbitrary character -- that depends on what you *do* know about the character. Different starting points will yield different methods of obtaining the corresponding numerical codepoint. So please tell more about what you need to do, specifically.
LowCom · 2022-06-09 07:00
Oh ok, I just want to know how to programmatically insert text so that I write my own functions and key binds to certains things. Like for eg, I want a function which goes to beggining of a word and inserts a space before it. I know the function for going to beggining of a word but I do not know it for inserting space or any other character. I tried the method above by reading "C-h f" for insert char function and could not figure out what to do. So my requirement is that I want to know how to insert any alphabet using insert-char function and the correct syntax for that, that's it. I am not keen on using only unicode types, anything that works is fine.
eli-zaretskii · 2022-06-09 17:54
Inserting a character is simple: use `(insert-char ?c)`, where `c` is the character. That is, when editing the code in Emacs, type the question mark, followed by the character itself, which you can type interactively with `C-x 8 RET`.