redacted.el icon indicating copy to clipboard operation
redacted.el copied to clipboard

Can it be made to just redact a region of the buffer?

Open Trisk3lion opened this issue 2 years ago • 3 comments

Because that would be a very cool way of hiding parts of your buffer when sharing screen or pair programming.

Trisk3lion avatar Oct 07 '22 11:10 Trisk3lion

First try:

(defun redacted-redact-region (beg end)
  (interactive "r")
  (let ((ov (make-overlay beg end))
        (string (make-string (- end beg) ? ))
        (index 0)
        char)
    (save-excursion
      (goto-char beg)
      (while (<= (point) (1- end))
        (setq char (char-after))
        (pcase (get-char-code-property char 'general-category)
          ((or 'Cc 'Cf 'Zs 'Zl 'Zp) (aset string index char))
          ('Ll (aset string index (make-glyph-code ?▃)))
          (_   (aset string index (make-glyph-code ?▆))))
        (cl-incf index)
        (forward-char 1))
      (overlay-put ov 'display string)
      (overlay-put ov 'redacted t))))

(defun redacted-remove (beg end)
  (interactive "r")
  (remove-overlays beg end 'redacted t))

Not perfect as we get the same face for the whole overlay. I perhaps need to try text-properties instead.

Trisk3lion avatar Oct 07 '22 13:10 Trisk3lion

New version!

(defun redacted-redact-region (beg end)
    (interactive "r")
    (let (char)
      (save-excursion
        (goto-char beg)
        (while (<= (point) (1- end))
          (setq char (char-after))
          (pcase (get-char-code-property char 'general-category)
            ((or 'Cc 'Cf 'Zs 'Zl 'Zp) nil)
            ('Ll (put-text-property (point) (1+ (point)) 'display (char-to-string (make-glyph-code ?▃))))
            (_   (put-text-property (point) (1+ (point)) 'display (char-to-string (make-glyph-code ?▆)))))
          (forward-char 1)))
      (with-silent-modifications
        (add-text-properties beg end '(read-only t)))))
  

  (defun redacted-remove (beg end)
    (interactive "r")
    (let ((inhibit-read-only t)
          (lower (char-to-string (make-glyph-code ?▃)))
          (upper (char-to-string (make-glyph-code ?▆))))
      (save-excursion
        (goto-char beg)
        (while (<= (point) (1- end))
          (setq char (char-after))
          (pcase (get-char-code-property char 'general-category)
            ((or 'Cc 'Cf 'Zs 'Zl 'Zp) nil)
            ('Ll (remove-text-properties (point) (1+ (point)) `(display ,lower)))
            (_   (remove-text-properties (point) (1+ (point)) `(display ,upper))))
          (forward-char 1))
        (with-silent-modifications
          (remove-text-properties beg end '(read-only t))))))

Trisk3lion avatar Oct 08 '22 19:10 Trisk3lion

Because that would be a very cool way of hiding parts of your buffer when sharing screen or pair programming.

That's something I didn't have in mind and a really interesting scenario indeed. I'll have a look at your suggestions during the next weekend, sorry for the slight delay, @Trisk3lion.

That being said, this would deviate a lot from the current "patch" of buffer-display-table, and I would have to ensure that both implementations always stay in sync, but it should be possible. It's only a ~70 lines package, after all :)

bkaestner avatar Oct 11 '22 16:10 bkaestner