Wednesday, April 6, 2016

Re: Revert keys values in dict

2016-04-06 16:00 GMT+03:00 Ni Va <nivaemail@gmail.com>:
> On Wednesday, April 6, 2016 at 1:36:08 PM UTC+2, lith wrote:
>> > Such tricks are usually used in
>> > plugins where you e.g. have mappings like "escape sequence to escaped
>> > character" for decoder and "escaped character to escape sequence" for
>> > encoder or something like this
>>
>> I didn't question the intent per se but the "cool one-liner" requirement.
>
> Thank you all for your response,
>
> I don't happen to replace each &#xa; and &#xd; ISO 8859-1 Characters Named HTML Entities respectively by a linefeed and carriage return and show visually the result in my buffer.
>
> This command is working but it does not satisfy me because it replace both of them by linefeed : :silent! %s/&#x[AD];/^M/g
>
> I would like something like :
> silent! %s/&#xA;/\n/g
> silent! %s/&#xD;/\r/g
> but it fails.
>
> And then, when replacing will work, how replace correctly \n by &#xA; and \r by &#xD;

Using \= you may compute any expression:

```
function s:Rep(match)
if a:match[1] is# '#' && a:match[2] is# 'x'
return nr2char(str2nr(a:match[3:-2], 16))
else
return get(mydict, a:match, a:match)
endif
endfunction
%s/&[^;];/\=s:Rep(submatch(0))/g
```

. Same technique for replacing everything back: either use
`printf('&#x%x;', char2nr(a:match))` or take value from reverted
dictionary. Also \n in replacement part is CR (0x0D), not LF. And \r
is line break, not CR. In every place that is not replacement \n is
either LF or line break, \r is CR. Issue is kept for compatibility
reasons.

>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment