[Top][All Lists]

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

Furna's fisheye minor mode

From: Juha Vierinen
Subject: Furna's fisheye minor mode
Date: Tue, 20 Jan 2004 17:00:01 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030805


I have attached a minor mode for emacs 21 that dynamically filters out stuff that is indented. It is a bit similar to hideshow, hidelines and outline, but it works on the indentation level instead of the semantics of some programming language. The mode is slow with very large files, because I couldn't figure out how to check if the point is in the visible region of the buffer. Also, the only way I could detect that the point had moved was by saving the point position and checking if it has changed in the post-command-hook.

The question is, what is the right way to check that the point has moved? How do I know if the point is in the visible region of the buffer?

;;; fisheye.el --- 
;; Furna's fisheye mode. Nice for editing large source code files 
;; Author: Juha Vierinen <jvierine @ tml spot hut spot fi>
;;                               (use your blind spot)
;;; Commentary
;; The simplest way to make fisheye work is to add the following
;; lines to your .emacs file:
;; (autoload 'fisheye "fisheye" "Fisheye mode" t)
;; and 'fisheye-minor-mode' to toggle on/off
;; Internal functions mapped to key-strokes
;; I recommend using spaces for intendation 
;; (setq indent-tabs-mode nil)
;; Tested on Emacs 21.2.1 and 21.3.1
;; todo 
;; - better doi and lod functions (language specific)
;; GPL

(defcustom fisheye-minor-mode-hook nil
  "*Hook called when fisheye minor mode is activated or deactivated."
  :type 'hook
  :group 'fisheye
  :version "21.1")

;(defvar fisheye-minor-mode nil
;  "Non-nil if using fisheye mode as a minor mode of some other mode.
;Use the command `fisheye-minor-mode' to toggle or set this variable.")

(defvar fisheye-minor-mode-map nil
  "Keymap for fisheye minor mode.")

(define-minor-mode fisheye-minor-mode
  "Toggle fisheye minor mode.
   With ARG, turn fisheye minor mode on if ARG is positive, off otherwise.
   When fisheye minor mode is on, the menu bar is augmented with fisheye
   commands and the fisheye commands are enabled.
   The value '(fe) is added to `buffer-invisibility-spec'.

   The main commands are: `fisheye-point-moved' and 

   Turning fisheye minor mode off reverts the menu bar and the
   variables to default values and disables the fisheye commands.

    Lastly, the normal hook `fisheye-minor-mode-hook' is run using `run-hooks'.

    Key bindings:
  nil " Fisheye" nil
  (if fisheye-minor-mode
        (set (make-local-variable 'line-move-ignore-invisible) t)
        ;; use '(fe . t) if you want ellipses for hidden overlays
        (add-to-invisibility-spec '(fe)) 
        (add-hook 'post-command-hook 'fisheye-yoho))
    (remove-from-invisibility-spec '(fe))
    (remove-hook 'post-command-hook 'fisheye-yoho))
  (run-hooks 'fisheye-minor-mode-hook))

;; internal variables
(defvar fisheye-old-point 1 "keeps track of the previous point placement.")
;(defvar fisheye-minor-mode 1 "is fisheye mode on or not")
(defvar fisheye-min-level-visible 1 "The level that cannot be hidden")
(defvar fisheye-max-level-detected 10 "The largest level of detail recognized")
(defvar fisheye-doi-visible 7 "The level that cannot be hidden")
(defvar fisheye-visible-area 20 "The visible area that is not folded (in 
(defvar fisheye-inside-lodcount 0)
(defvar fisheye-tab-spaces 8 "tab equals eight spaces") 
(defvar fisheye-invisible-list-cursor 0 "An index indicating the position of 
the point in the invisible areas list") 
(defvar fisheye-invisible-areas-list () "List of invisible overlays used by 

(defun fisheye-add-invisible-overlay (start end)
  "Add an overlay from `start' to `end' in the current buffer.  Push the
overlay onto the fisheye-invisible-areas-list list"
  (let ((overlay (make-overlay start end)))
    (setq fisheye-invisible-areas-list 
          (fisheye-addn (+ (length fisheye-invisible-areas-list) 1) 
    (overlay-put overlay 'invisible '(fe))))

(defun fisheye-add-invisible-overlay-at (n start end)
  "Add an overlay from `start' to `end' in the current buffer.  Push the
overlay onto the fisheye-invisible-areas-list list"
  (let ((overlay (make-overlay start end)))
    (setq fisheye-invisible-areas-list (fisheye-addn n 
fisheye-invisible-areas-list overlay))
    (overlay-put overlay 'invisible '(fe))))

(defun fisheye-toggle ()
  "Toggle fisheye mode, show all hidden lines"
  (if (eq fisheye-minor-mode nil)
      (setq fisheye-minor-mode 1)
    (setq fisheye-minor-mode nil)

(defun fisheye-yoho ()
  (if (and (not (equal fisheye-old-point (point)))
           (not (eq fisheye-minor-mode nil)))
  (setq fisheye-old-point 

(defun fisheye-move (direction)
  "Move one step in some direction" 
  (let ((new-point (point))
      (goto-char fisheye-old-point)
      (forward-line (* direction (+ fisheye-visible-area 1)))
      (if (< (fisheye-doi (point) fisheye-old-point)
          (if (= direction 1)
              (fisheye-delete-overlay-after fisheye-invisible-list-cursor)
            (fisheye-delete-overlay-before fisheye-invisible-list-cursor)
            (setq fisheye-invisible-list-cursor (- 
fisheye-invisible-list-cursor 1))))

      (goto-char fisheye-old-point)
      (forward-line (* direction (- fisheye-visible-area)))
      (if (>= (fisheye-doi (point) new-point)
        (setq start-position (point))
        (fisheye-add-invisible-overlay-at (+ fisheye-invisible-list-cursor 1) 
start-position (+ 1 (point)))
        (if (= direction 1)
            (setq fisheye-invisible-list-cursor (+ 
fisheye-invisible-list-cursor 1)))))))

(defun fisheye-delete-overlay-after (cursor)
  (if (> cursor (length fisheye-invisible-areas-list))
    (delete-overlay (nth cursor fisheye-invisible-areas-list))
    (setq fisheye-invisible-areas-list (fisheye-remn (+ cursor 1) 

(defun fisheye-delete-overlay-before (cursor)
  (if (< cursor 1)
    (delete-overlay (nth (- cursor 1) fisheye-invisible-areas-list))
    (setq fisheye-invisible-areas-list (fisheye-remn cursor 

(defun fisheye-point-moved ()
  "The point has moved, so we brute force the buffer, this is done initially, 
and also if the point has radically moved"
  (if (and (not (eq fisheye-invisible-areas-list nil))
           (= (fisheye-count-lines (point) fisheye-old-point) 1))
      (if  (> (- (point) fisheye-old-point) 0)
          (fisheye-move 1)
        (fisheye-move -1))
    (if (> (fisheye-count-lines (point) fisheye-old-point) 0)
        (let ((focus-point (point))
              (end-point (point-max)))
            (setq fisheye-invisible-list-cursor 0)
            (goto-char (point-min))
            (while (and (save-excursion (end-of-line)
                                        (not (eobp)))
                        (< (point) end-point))
              (if (>= (fisheye-doi (point) focus-point) 
                  (forward-line 1)
                (setq start-position (point))
                (fisheye-add-invisible-overlay start-position (+ 1 (point)))
                (if (< (point) focus-point)
                    (setq fisheye-invisible-list-cursor
                          (+ fisheye-invisible-list-cursor 1)))
                (forward-line 1))))))))

(defun fisheye-lod (current-point)
  "The relevance the level of detail of the current line. Override, if you want 
something special"
  (let ((level 1)
      (goto-char current-point)
      ;; if plain newline, then level of detail is highest (should be filtered 
      (if (eq (char-after (point)) 10)
          (setq level fisheye-max-level-detected)
        (while (and (or (eq (char-after (point)) 32)
                        (eq (char-after (point)) 9))
                    (< level fisheye-max-level-detected)
                    (not (eolp)))
          (if (eq (char-after (point)) 32)
              (setq level (+ level 1))
            (setq level (+ level fisheye-tab-spaces)))
          (goto-char (forward-point 1)))))

(defun fisheye-doi (current-point focus-point)
  "Degree of interest function. Simple window, with fixed size, also we display 
all sufficiently low level details (lines that have small enough lod, defined 
in fisheye-min-level-visible" 
  (let ((doi nil)
        (distance nil))
    (setq distance (fisheye-count-lines focus-point current-point))
    (if (< distance fisheye-visible-area)
        (setq doi fisheye-doi-visible)
      (setq doi (- (+ fisheye-doi-visible fisheye-min-level-visible)
                   (fisheye-lod current-point))))

(defun fisheye-show-all-invisible ()
  "Show all hidden area, when fisheye is toggled off"
  (mapcar (lambda (overlay) (delete-overlay overlay)) 
  (setq fisheye-invisible-areas-list ()))

(defun fisheye-remn (n l)
  (if (= n 1)
      (setq l (cdr l))
    (setcdr (nthcdr (- n 2) l)
            (nthcdr n l)))

(defun fisheye-addn (n l o)
  (if (< n 2)
      (setq l (cons o l))
    (setcdr (nthcdr (- n 2) l)
            (cons o (nthcdr (- n 1) l))))

(defun fisheye-count-lines (a b) 
  (let ((point-a)
      (goto-char a)
      (setq point-a (point))
      (goto-char b)
      (setq point-b (point))
      (setq lines (count-lines point-a point-b)))

(provide 'fisheye)

reply via email to

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