tags:

views:

62

answers:

2

Suppose you visit a file F in an emacs buffer B, let r and R be some replacement regular expressions. Now I want to replace all occurrences of strings r_i which matches r (in some region) by the corresponding replacement-string R_i defined by R such that the following conditions are fulfiled:

  • if I save F, the above replacement must not change the content of F
  • if the cursor is over some text which was replaced via R, it should show the original r_1, which matched r. If I do this, edit r_1 to r_2 and move the cursor away, r_2 should be replaced by the corresponding R_2 iff it matches r.

It would be nice if it would be possible to highlight (e.g. different color, or underline ...) the replacements and control this highlighting depending on r.

I guess that such a functionality already exists but I don't know how it is called. What I described above is in some way similar to preview mode for editing latex-files.

+1  A: 

I'm not aware of any user-level feature to do this. There's hi-lock-mode which lets you highlight some text but not show a replacement text instead.

The programmer-level feature used by preview mode and other packages that cause a different text to be displayed from what is in the buffer is called overlays. If you want to code this up, it would look something like this: for every occurrence of r, put an overlay on this occurence of r with the original text r_1 as the help-echo property, r_2 as the display property, a modification-hooks property that reacts to any change, plus probably a face or category property.

Gilles
+3  A: 

It looks like you just want some strings displayed differently, not any actual replacement. I think font-lock-mode can handle your requirements. For example, you can have all occurrences of the word "pi" in a buffer displayed as the greek letter π by evaluating this in the buffer:

(font-lock-add-keywords 
  nil `(("\\<pi\\>" (0 `(face default display "π")))))
(push 'display font-lock-extra-managed-props)

C-h v font-lock-keywords will give more details.

huaiyuan