emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I can see invisible characters


From: Werner LEMBERG
Subject: I can see invisible characters
Date: Tue, 19 May 2009 08:16:12 +0200 (CEST)

[Emacs CVS 2009-05-13]

0. Start `emacs -Q'.

1. Do `M-x load-file line-invisible.el' (this assigns
   `make-lines-invisible' to `C-c z').

2. Load file `w' with `C-x C-f w'.

3. Do `C-c z ยท' (this is, make all lines invisible which contain a
   middle dot).

4. Do `C-u C-x =': The first visible character in the buffer has set
   the `invisible' property.  It seems to be a bug that I can see that
   character.  It has also set the `intangible' property, and I can
   move the cursor on top of it (with the arrow keys, but not with
   C-a).  This doesn't look right too...

   An off-by-one error?


     Werner

Attachment: w
Description: Binary data

;;; line-invisible.el
;;;
;;; Werner Lemberg, April 2009


;; from font-lock.el
(defmacro save-buffer-state (varlist &rest body)
  "Bind variables according to VARLIST and eval BODY restoring
buffer state."
  (declare (indent 1) (debug let))
  (let ((modified (make-symbol "modified")))
    `(let* ,(append varlist
                    `((,modified (buffer-modified-p))
                      (buffer-undo-list t)
                      (inhibit-read-only t)
                      (inhibit-point-motion-hooks t)
                      (inhibit-modification-hooks t)
                      deactivate-mark
                      buffer-file-name
                      buffer-file-truename))
       (unwind-protect
           (progn
             ,@body)
         (unless ,modified
           (restore-buffer-modified-p nil))))))

(defun make-lines-invisible (regexp &optional arg)
  "Make all lines matching a regexp invisible and intangible.
With a prefix arg, make them visible again.  It is not necessary
that REGEXP matches the whole line; if a hit is found, the
affected line gets automatically selected.

This function always applies to the whole buffer.

Note that this function modifies the `invisible' and `intangible'
text properties; it may thus interfere with modes which use them.
Due to implementation restrictions in current Emacs versions it
is not possible to use overlays -- which would avoid text
property modifications -- without becoming unbearably slow for
large buffers with many matches."
  (interactive "MLine matching regexp: \nP")
  (save-excursion
    (cond
     (arg
      (save-buffer-state ((next-pos (point-min)))
        (while (setq next-pos
                     (text-property-any next-pos
                                        (point-max)
                                        'make-lines-invisible
                                        t))
          (goto-char next-pos)
          (setq next-pos (or (next-single-property-change
                              (point)
                              'make-lines-invisible)
                             (point-max)))
          (remove-list-of-text-properties (point)
                                          next-pos
                                          '(make-lines-invisible
                                            invisible
                                            intangible)))))
    (t
     (save-buffer-state ((start 0)
                         (end 0))
       (goto-char (point-min))
       (while (re-search-forward regexp nil t)
         (setq start (match-beginning 0))
         (goto-char start)
         (setq start (line-beginning-position))
         (setq end (match-end 0))
         (goto-char end)
         (setq end (1+ (line-end-position))) ; handle \n
         (add-text-properties start
                              end
                              '(make-lines-invisible t
                                invisible t
                                intangible t))
         (goto-char (line-end-position))))))))

(defun make-other-lines-invisible (regexp)
  "Make all lines not matching a regexp invisible and intangible.
It is not necessary that REGEXP matches the whole line; if a hit
is found, the affected line gets automatically unselected.

This function always applies to the whole buffer.

Note that this function modifies the `invisible' and `intangible'
text properties; it may thus interfere with modes which use them.
Due to implementation restrictions in current Emacs versions it
is not possible to use overlays -- which would avoid text
property modifications -- without becoming unbearably slow for
large buffers with many matches."
  (interactive "MLine matching regexp: \n")
  (save-excursion
    (save-buffer-state ((start 1)
                        (end 1))
      (goto-char (point-min))
      (while (re-search-forward regexp nil t)
        (setq end (match-beginning 0))
        (goto-char end)
        (setq end (1- (line-beginning-position))) ; handle \n
        (if (<= start end)
            (add-text-properties start
                                 end
                                 '(make-lines-invisible t
                                   invisible t
                                   intangible t)))
        (setq start (match-end 0))
        (goto-char start)
        (setq start (line-end-position))
        (goto-char (line-end-position)))
      (setq end (point-max))
      (if (< start end)
          (add-text-properties start
                               end
                               '(make-lines-invisible t
                                 invisible t
                                 intangible t))))))

(global-set-key "\C-cz" 'make-lines-invisible)
(global-set-key "\C-cZ" 'make-other-lines-invisible)

;; eof

reply via email to

[Prev in Thread] Current Thread [Next in Thread]