* Displaying a string as another one
@ 2020-03-16 10:05 Marcin Borkowski
2020-03-16 15:58 ` Stefan Monnier
0 siblings, 1 reply; 3+ messages in thread
From: Marcin Borkowski @ 2020-03-16 10:05 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
Hi all,
I have a buffer with long lines, containing many instances of strings
"\\n" and "\\t" (i.e., a literal backslash followed by "n" or "t"). I'd
like Emacs to display them as a newline and a tab, respectively. These
instances occur inside strings (i.e., between quotes, which delimit
strings in JavaScript).
I tried
(setq prettify-symbols-alist '(("\\n" . ?\n) ("\\t" . ?\t)))
but this does not seem to work inside strings.
I suspect font-lock could do it, but I'm not sure how (and frankly,
I don't have time now to do many experiments).
Does anyone have a ready-made solution?
TIA,
--
Marcin Borkowski
http://mbork.pl
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Displaying a string as another one
2020-03-16 10:05 Displaying a string as another one Marcin Borkowski
@ 2020-03-16 15:58 ` Stefan Monnier
2020-03-21 14:11 ` Marcin Borkowski
0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2020-03-16 15:58 UTC (permalink / raw)
To: help-gnu-emacs
> Does anyone have a ready-made solution?
You could start with something like
(defun my-decode-\n-\t ()
(font-lock-add-keywords nil
'(("\\\\[nt]"
(0 (my-apply-decode (match-beginning 0) (match-end 0)))))))
(defun my-apply-decode (beg end)
(when (= 0 (mod (save-excursion (goto-char beg)
(skip-chars-backward "\\\\"))
2))
`(face nil
display ,(string (if (eq (char-before end) ?n) ?\n ?\t)))))
(add-hook 'foo-mode-hook #'my-decode-\n-\t)
-- Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Displaying a string as another one
2020-03-16 15:58 ` Stefan Monnier
@ 2020-03-21 14:11 ` Marcin Borkowski
0 siblings, 0 replies; 3+ messages in thread
From: Marcin Borkowski @ 2020-03-21 14:11 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
On 2020-03-16, at 16:58, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> Does anyone have a ready-made solution?
>
> You could start with something like
>
> (defun my-decode-\n-\t ()
> (font-lock-add-keywords nil
> '(("\\\\[nt]"
> (0 (my-apply-decode (match-beginning 0) (match-end 0)))))))
>
> (defun my-apply-decode (beg end)
> (when (= 0 (mod (save-excursion (goto-char beg)
> (skip-chars-backward "\\\\"))
> 2))
> `(face nil
> display ,(string (if (eq (char-before end) ?n) ?\n ?\t)))))
>
> (add-hook 'foo-mode-hook #'my-decode-\n-\t)
Thanks a lot. This looks more complicated than I expected... I'll
experiment with this and see how it works.
Best,
--
Marcin Borkowski
http://mbork.pl
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-03-21 14:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-16 10:05 Displaying a string as another one Marcin Borkowski
2020-03-16 15:58 ` Stefan Monnier
2020-03-21 14:11 ` Marcin Borkowski
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.