Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Trisk3lion opened this issue Oct 7, 2022 · 3 comments
Open

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

Trisk3lion opened this issue Oct 7, 2022 · 3 comments

Comments

@Trisk3lion
Copy link

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

@Trisk3lion
Copy link
Author

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
Copy link
Author

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))))))

@bkaestner
Copy link
Owner

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants