On Tuesday, November 2, 2021 at 1:17:07 AM UTC-4, H.Singh wrote:
> Likely, that this is a basic question to experts from a newbie like myself.
>
> Suppose, I have a list of text and I want to transliterate the characters.
> I want to do it in emacs so unix utilities like tr and sed might not be desirable for the moment.
>
> Also, I want to build it incrementally and write this as a lisp function.
> For example, I have this text
>
> Figure
> and I want to replace it with any kind of mapping and to make example easy for the moment it is
>
> Figure -> fIGURE
>
> and I have cooked this and tested it in emacs.
>
> (mapcar* '(lambda (x y) (save-excursion (replace-string (format "%s" x) (format "%s" y))))
> '[F i g u r e]
> '[f I G U R E])
>
> I just tested it again and it works on the textg below it by C-x C-e.
>
> But now, I have control characters in the mapping.
> I cannot write the control characters but the place where I get them by copy-paste gives me the replacement text as control characters and I do not want to manually be converting them into Hex or Octal or Decimal character codes.
>
> This is the main reason I have built this above function based on mapcar* so it repeatedly car's the [] (list in C and vector in lisp) and here I can write a macro to put spaces between the characters.
>
> I do not even want to quote the pasted text characters.
>
> Have a read at a similar problem and undesirable solution at this webpage
>
>
https://unix.stackexchange.com/questions/148837/replace-control-characters-in-emacs
>
> where it requires you to discover the encoding and I do not want to do that. I want strictly a simple emacs transliteration function to do this along the lines above.
> At some stage, I probably also want to add special characters in my tranformation table.
As someone who has used emacs on MS-Dog and Winblows machines, the control-M character used to terminate lines was the goad for me to grope around for a means to do what you presently want to do.
So for the example I'm about to present, image a file tainted with those dumbass ^M characters (EG as they are displayed via emacs, although only a single control character)
Emacs has query-replace, which allows one to replace on string with another.
(query-replace FROM-STRING TO-STRING &optional DELIMITED START END
BACKWARD REGION-NONCONTIGUOUS-P)
By default, I believe this function is mapped to Alt-Shft-% if you want to do a Ctrl-h-k to get help on that key binding.
As every character may be thought of as a string of length 1, this option seems viable, right?
The next trick is input or specify the control character ^M, the extraneous end-of-line character (mis)used in MS-Dog text files.
As a rookie it took me quite some time to discover a magic key sequence which would allow me to specify a control character.
The key sequence maps onto
(quoted-insert ARG)
... which by default, I believe, is mapped onto C-q
So when I open a file with obnoxious ^M characters terminating every line I use these 2 emacs functions.
And when I've wanted to automate things, I use command-history AFTER having used those two functions; the quoted-insert function exploited in the UI results in a delightfully re-useable sexp being inserted in command-history for re-use.
(query-replace "
" "" nil nil nil nil nil)
note that viewing this in command history the ^M is visible in emacs, whereas above the interface of the mail app I'm using to compose this interpreted the ^M as a newline, thus displaying the single-line sexp on two lines instead of one as you'll see it in emacs.
So if one wants to exploit this method by generalizing, one may use the lambda calculus by turning a specific case into a `parametric equation' cum anonymous parametric function as follows:
((lambda (from-string to-string) (query-replace from-string to-string nil nil nil nil nil)) <your `before'> <your `after'>)
... or something to this effect.
As there are many string substitution and character substitution functions available in emacs lisp, the permutations are myriad.
The variation I presented is something which has worked for me to accomplish a variation on your central theme.
Best of luck with hacking together something which suits your needs and temperament.
Cheers!
Gene