* emacs-lisp/chart.el:
[bpt/emacs.git] / lisp / emacs-lisp / chart.el
1 ;;; chart.el --- Draw charts (bar charts, etc)
2
3 ;;; Copyright (C) 1996, 1998, 1999, 2001, 2004, 2005, 2007, 2008, 2009
4 ;;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 0.2
8 ;; Keywords: OO, chart, graph
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26 ;;
27 ;; This package is an experiment of mine aiding in the debugging of
28 ;; eieio, and proved to be neat enough that others may like to use
29 ;; it. To quickly see what you can do with chart, run the command
30 ;; `chart-test-it-all'.
31 ;;
32 ;; Chart current can display bar-charts in either of two
33 ;; directions. It also supports ranged (integer) axis, and axis
34 ;; defined by some set of strings or names. These name can be
35 ;; automatically derived from data sequences, which are just lists of
36 ;; anything encapsulated in a nice eieio object.
37 ;;
38 ;; Current example apps for chart can be accessed via these commands:
39 ;; `chart-file-count' - count files w/ matching extensions
40 ;; `chart-space-usage' - display space used by files/directories
41 ;; `chart-emacs-storage' - Emacs storage units used/free (garbage-collect)
42 ;; `chart-emacs-lists' - length of Emacs lists
43 ;; `chart-rmail-from' - who sends you the most mail (in -summary only)
44 ;;
45 ;; Customization:
46 ;;
47 ;; If you find the default colors and pixmaps unpleasant, or too
48 ;; short, you can change them. The variable `chart-face-color-list'
49 ;; contains a list of colors, and `chart-face-pixmap-list' contains
50 ;; all the pixmaps to use. The current pixmaps are those found on
51 ;; several systems I found. The two lists should be the same length,
52 ;; as the long list will just be truncated.
53 ;;
54 ;; If you would like to draw your own stipples, simply create some
55 ;; xbm's and put them in a directory, then you can add:
56 ;;
57 ;; (setq x-bitmap-file-path (cons "~/mybitmaps" x-bitmap-file-path))
58 ;;
59 ;; to your .emacs (or wherever) and load the `chart-face-pixmap-list'
60 ;; with all the bitmaps you want to use.
61
62 (require 'eieio)
63
64 ;;; Code:
65 (defvar chart-map nil "Keymap used in chart mode.")
66 (if chart-map
67 ()
68 (setq chart-map (make-sparse-keymap))
69 )
70
71 (defvar chart-local-object nil
72 "Local variable containing the locally displayed chart object.")
73 (make-variable-buffer-local 'chart-local-object)
74
75 (defvar chart-face-list nil
76 "Faces used to colorize charts.
77 List is limited currently, which is ok since you really can't display
78 too much in text characters anyways.")
79
80 (defvar chart-face-color-list '("red" "green" "blue"
81 "cyan" "yellow" "purple")
82 "Colors to use when generating `chart-face-list'.
83 Colors will be the background color.")
84
85 (defvar chart-face-pixmap-list
86 (if (and (fboundp 'display-graphic-p)
87 (display-graphic-p))
88 '("dimple1" "scales" "dot" "cross_weave" "boxes" "dimple3"))
89 "If pixmaps are allowed, display these background pixmaps.
90 Useful if new Emacs is used on B&W display")
91
92 (defcustom chart-face-use-pixmaps nil
93 "*Non-nil to use fancy pixmaps in the background of chart face colors."
94 :group 'eieio
95 :type 'boolean)
96
97 (if (and (if (fboundp 'display-color-p)
98 (display-color-p)
99 window-system)
100 (not chart-face-list))
101 (let ((cl chart-face-color-list)
102 (pl chart-face-pixmap-list)
103 nf)
104 (while cl
105 (setq nf (make-face (intern (concat "chart-" (car cl) "-" (car pl)))))
106 (if (condition-case nil
107 (> (x-display-color-cells) 4)
108 (error t))
109 (set-face-background nf (car cl))
110 (set-face-background nf "white"))
111 (set-face-foreground nf "black")
112 (if (and chart-face-use-pixmaps
113 pl
114 (fboundp 'set-face-background-pixmap))
115 (condition-case nil
116 (set-face-background-pixmap nf (car pl))
117 (error (message "Cannot set background pixmap %s" (car pl)))))
118 (setq chart-face-list (cons nf chart-face-list))
119 (setq cl (cdr cl)
120 pl (cdr pl)))))
121
122 (defun chart-mode ()
123 "Define a mode in Emacs for displaying a chart."
124 (kill-all-local-variables)
125 (use-local-map chart-map)
126 (setq major-mode 'chart-mode
127 mode-name "CHART")
128 (buffer-disable-undo)
129 (set (make-local-variable 'font-lock-global-modes) nil)
130 (font-lock-mode -1)
131 (run-hooks 'chart-mode-hook)
132 )
133
134 (defun chart-new-buffer (obj)
135 "Create a new buffer NAME in which the chart OBJ is displayed.
136 Returns the newly created buffer"
137 (save-excursion
138 (set-buffer (get-buffer-create (format "*%s*" (oref obj title))))
139 (chart-mode)
140 (setq chart-local-object obj)
141 (current-buffer)))
142
143 (defclass chart ()
144 ((title :initarg :title
145 :initform "Emacs Chart")
146 (title-face :initarg :title-face
147 :initform 'bold-italic)
148 (x-axis :initarg :x-axis
149 :initform nil )
150 (x-margin :initarg :x-margin
151 :initform 5)
152 (x-width :initarg :x-width
153 )
154 (y-axis :initarg :y-axis
155 :initform nil)
156 (y-margin :initarg :y-margin
157 :initform 5)
158 (y-width :initarg :y-width
159 )
160 (key-label :initarg :key-label
161 :initform "Key")
162 (sequences :initarg :sequences
163 :initform nil)
164 )
165 "Superclass for all charts to be displayed in an emacs buffer")
166
167 (defmethod initialize-instance :AFTER ((obj chart) &rest fields)
168 "Initialize the chart OBJ being created with FIELDS.
169 Make sure the width/height is correct."
170 (oset obj x-width (- (window-width) 10))
171 (oset obj y-width (- (window-height) 12)))
172
173 (defclass chart-axis ()
174 ((name :initarg :name
175 :initform "Generic Axis")
176 (loweredge :initarg :loweredge
177 :initform t)
178 (name-face :initarg :name-face
179 :initform 'bold)
180 (labels-face :initarg :lables-face
181 :initform 'italic)
182 (chart :initarg :chart
183 :initform nil)
184 )
185 "Superclass used for display of an axis.")
186
187 (defclass chart-axis-range (chart-axis)
188 ((bounds :initarg :bounds
189 :initform '(0.0 . 50.0))
190 )
191 "Class used to display an axis defined by a range of values")
192
193 (defclass chart-axis-names (chart-axis)
194 ((items :initarg :items
195 :initform nil)
196 )
197 "Class used to display an axis which represents different named items")
198
199 (defclass chart-sequece ()
200 ((data :initarg :data
201 :initform nil)
202 (name :initarg :name
203 :initform "Data")
204 )
205 "Class used for all data in different charts")
206
207 (defclass chart-bar (chart)
208 ((direction :initarg :direction
209 :initform vertical))
210 "Subclass for bar charts. (Vertical or horizontal)")
211
212 (defmethod chart-draw ((c chart) &optional buff)
213 "Start drawing a chart object C in optional BUFF.
214 Erases current contents of buffer"
215 (save-excursion
216 (if buff (set-buffer buff))
217 (erase-buffer)
218 (insert (make-string 100 ?\n))
219 ;; Start by displaying the axis
220 (chart-draw-axis c)
221 ;; Display title
222 (chart-draw-title c)
223 ;; Display data
224 (message "Rendering chart...")
225 (sit-for 0)
226 (chart-draw-data c)
227 ;; Display key
228 ; (chart-draw-key c)
229 (message "Rendering chart...done")
230 ))
231
232 (defmethod chart-draw-title ((c chart))
233 "Draw a title upon the chart.
234 Argument C is the chart object."
235 (chart-display-label (oref c title) 'horizontal 0 0 (window-width)
236 (oref c title-face)))
237
238 (defmethod chart-size-in-dir ((c chart) dir)
239 "Return the physical size of chart C in direction DIR."
240 (if (eq dir 'vertical)
241 (oref c y-width)
242 (oref c x-width)))
243
244 (defmethod chart-draw-axis ((c chart))
245 "Draw axis into the current buffer defined by chart C."
246 (let ((ymarg (oref c y-margin))
247 (xmarg (oref c x-margin))
248 (ylen (oref c y-width))
249 (xlen (oref c x-width)))
250 (chart-axis-draw (oref c y-axis) 'vertical ymarg
251 (if (oref (oref c y-axis) loweredge) nil xlen)
252 xmarg (+ xmarg ylen))
253 (chart-axis-draw (oref c x-axis) 'horizontal xmarg
254 (if (oref (oref c x-axis) loweredge) nil ylen)
255 ymarg (+ ymarg xlen)))
256 )
257
258 (defmethod chart-axis-draw ((a chart-axis) &optional dir margin zone start end)
259 "Draw some axis for A in direction DIR at with MARGIN in boundry.
260 ZONE is a zone specification.
261 START and END represent the boundary."
262 (chart-draw-line dir (+ margin (if zone zone 0)) start end)
263 (chart-display-label (oref a name) dir (if zone (+ zone margin 3)
264 (if (eq dir 'horizontal)
265 1 0))
266 start end (oref a name-face)))
267
268 (defmethod chart-translate-xpos ((c chart) x)
269 "Translate in chart C the coordinate X into a screen column."
270 (let ((range (oref (oref c x-axis) bounds)))
271 (+ (oref c x-margin)
272 (round (* (float (- x (car range)))
273 (/ (float (oref c x-width))
274 (float (- (cdr range) (car range))))))))
275 )
276
277 (defmethod chart-translate-ypos ((c chart) y)
278 "Translate in chart C the coordinate Y into a screen row."
279 (let ((range (oref (oref c y-axis) bounds)))
280 (+ (oref c x-margin)
281 (- (oref c y-width)
282 (round (* (float (- y (car range)))
283 (/ (float (oref c y-width))
284 (float (- (cdr range) (car range)))))))))
285 )
286
287 (defmethod chart-axis-draw ((a chart-axis-range) &optional dir margin zone start end)
288 "Draw axis information based upon a range to be spread along the edge.
289 A is the chart to draw. DIR is the direction.
290 MARGIN, ZONE, START, and END specify restrictions in chart space."
291 (call-next-method)
292 ;; We prefer about 5 spaces between each value
293 (let* ((i (car (oref a bounds)))
294 (e (cdr (oref a bounds)))
295 (z (if zone zone 0))
296 (s nil)
297 (rng (- e i))
298 ;; want to jump by units of 5 spaces or so
299 (j (/ rng (/ (chart-size-in-dir (oref a chart) dir) 4)))
300 p1)
301 (if (= j 0) (setq j 1))
302 (while (<= i e)
303 (setq s
304 (cond ((> i 999999)
305 (format "%dM" (/ i 1000000)))
306 ((> i 999)
307 (format "%dK" (/ i 1000)))
308 (t
309 (format "%d" i))))
310 (if (eq dir 'vertical)
311 (let ((x (+ (+ margin z) (if (oref a loweredge)
312 (- (length s)) 1))))
313 (if (< x 1) (setq x 1))
314 (chart-goto-xy x (chart-translate-ypos (oref a chart) i)))
315 (chart-goto-xy (chart-translate-xpos (oref a chart) i)
316 (+ margin z (if (oref a loweredge) -1 1))))
317 (setq p1 (point))
318 (insert s)
319 (chart-zap-chars (length s))
320 (put-text-property p1 (point) 'face (oref a labels-face))
321 (setq i (+ i j))))
322 )
323
324 (defmethod chart-translate-namezone ((c chart) n)
325 "Return a dot-pair representing a positional range for a name.
326 The name in chart C of the Nth name resides.
327 Automatically compensates for for direction."
328 (let* ((dir (oref c direction))
329 (w (if (eq dir 'vertical) (oref c x-width) (oref c y-width)))
330 (m (if (eq dir 'vertical) (oref c y-margin) (oref c x-margin)))
331 (ns (length
332 (oref (if (eq dir 'vertical) (oref c x-axis) (oref c y-axis))
333 items)))
334 (lpn (/ (+ 1.0 (float w)) (float ns)))
335 )
336 (cons (+ m (round (* lpn (float n))))
337 (+ m -1 (round (* lpn (+ 1.0 (float n))))))
338 ))
339
340 (defmethod chart-axis-draw ((a chart-axis-names) &optional dir margin zone start end)
341 "Draw axis information based upon A range to be spread along the edge.
342 Optional argument DIR the direction of the chart.
343 Optional argument MARGIN , ZONE, START and END specify boundaries of the drawing."
344 (call-next-method)
345 ;; We prefer about 5 spaces between each value
346 (let* ((i 0)
347 (s (oref a items))
348 (z (if zone zone 0))
349 (r nil)
350 (p nil)
351 (odd nil)
352 p1)
353 (while s
354 (setq odd (= (% (length s) 2) 1))
355 (setq r (chart-translate-namezone (oref a chart) i))
356 (if (eq dir 'vertical)
357 (setq p (/ (+ (car r) (cdr r)) 2))
358 (setq p (- (+ (car r) (/ (- (cdr r) (car r)) 2))
359 (/ (length (car s)) 2))))
360 (if (eq dir 'vertical)
361 (let ((x (+ (+ margin z) (if (oref a loweredge)
362 (- (length (car s)))
363 (length (car s))))))
364 (if (< x 1) (setq x 1))
365 (if (> (length (car s)) (1- margin))
366 (setq x (+ x margin)))
367 (chart-goto-xy x p))
368 (chart-goto-xy p (+ (+ margin z) (if (oref a loweredge)
369 (if odd -2 -1)
370 (if odd 2 1)))))
371 (setq p1 (point))
372 (insert (car s))
373 (chart-zap-chars (length (car s)))
374 (put-text-property p1 (point) 'face (oref a labels-face))
375 (setq i (+ i 1)
376 s (cdr s))))
377 )
378
379 (defmethod chart-draw-data ((c chart-bar))
380 "Display the data available in a bar chart C."
381 (let* ((data (oref c sequences))
382 (dir (oref c direction))
383 (odir (if (eq dir 'vertical) 'horizontal 'vertical))
384 )
385 (while data
386 (if (stringp (car (oref (car data) data)))
387 ;; skip string lists...
388 nil
389 ;; display number lists...
390 (let ((i 0)
391 (seq (oref (car data) data)))
392 (while seq
393 (let* ((rng (chart-translate-namezone c i))
394 (dp (if (eq dir 'vertical)
395 (chart-translate-ypos c (car seq))
396 (chart-translate-xpos c (car seq))))
397 (zp (if (eq dir 'vertical)
398 (chart-translate-ypos c 0)
399 (chart-translate-xpos c 0)))
400 (fc (if chart-face-list
401 (nth (% i (length chart-face-list)) chart-face-list)
402 'default))
403 )
404 (if (< dp zp)
405 (progn
406 (chart-draw-line dir (car rng) dp zp)
407 (chart-draw-line dir (cdr rng) dp zp))
408 (chart-draw-line dir (car rng) zp (1+ dp))
409 (chart-draw-line dir (cdr rng) zp (1+ dp)))
410 (if (= (car rng) (cdr rng)) nil
411 (chart-draw-line odir dp (1+ (car rng)) (cdr rng))
412 (chart-draw-line odir zp (car rng) (1+ (cdr rng))))
413 (if (< dp zp)
414 (chart-deface-rectangle dir rng (cons dp zp) fc)
415 (chart-deface-rectangle dir rng (cons zp dp) fc))
416 )
417 ;; find the bounds, and chart it!
418 ;; for now, only do one!
419 (setq i (1+ i)
420 seq (cdr seq)))))
421 (setq data (cdr data))))
422 )
423
424 (defmethod chart-add-sequence ((c chart) &optional seq axis-label)
425 "Add to chart object C the sequence object SEQ.
426 If AXIS-LABEL, then the axis stored in C is updated with the bounds of SEQ,
427 or is created with the bounds of SEQ."
428 (if axis-label
429 (let ((axis (eieio-oref c axis-label)))
430 (if (stringp (car (oref seq data)))
431 (let ((labels (oref seq data)))
432 (if (not axis)
433 (setq axis (make-instance chart-axis-names
434 :name (oref seq name)
435 :items labels
436 :chart c))
437 (oset axis items labels)))
438 (let ((range (cons 0 1))
439 (l (oref seq data)))
440 (if (not axis)
441 (setq axis (make-instance chart-axis-range
442 :name (oref seq name)
443 :chart c)))
444 (while l
445 (if (< (car l) (car range)) (setcar range (car l)))
446 (if (> (car l) (cdr range)) (setcdr range (car l)))
447 (setq l (cdr l)))
448 (oset axis bounds range)))
449 (if (eq axis-label 'x-axis) (oset axis loweredge nil))
450 (eieio-oset c axis-label axis)
451 ))
452 (oset c sequences (append (oref c sequences) (list seq))))
453
454 ;;; Charting optimizers
455
456 (defmethod chart-trim ((c chart) max)
457 "Trim all sequences in chart C to be at most MAX elements long."
458 (let ((s (oref c sequences)))
459 (while s
460 (let ((sl (oref (car s) data)))
461 (if (> (length sl) max)
462 (setcdr (nthcdr (1- max) sl) nil)))
463 (setq s (cdr s))))
464 )
465
466 (defmethod chart-sort ((c chart) pred)
467 "Sort the data in chart C using predicate PRED.
468 See `chart-sort-matchlist' for more details"
469 (let* ((sl (oref c sequences))
470 (s1 (car sl))
471 (s2 (car (cdr sl)))
472 (s nil))
473 (if (stringp (car (oref s1 data)))
474 (progn
475 (chart-sort-matchlist s1 s2 pred)
476 (setq s (oref s1 data)))
477 (if (stringp (car (oref s2 data)))
478 (progn
479 (chart-sort-matchlist s2 s1 pred)
480 (setq s (oref s2 data)))
481 (error "Sorting of chart %s not supported" (object-name c))))
482 (if (eq (oref c direction) 'horizontal)
483 (oset (oref c y-axis) items s)
484 (oset (oref c x-axis) items s)
485 ))
486 )
487
488 (defun chart-sort-matchlist (namelst numlst pred)
489 "Sort NAMELST and NUMLST (both SEQUENCE objects) based on predicate PRED.
490 PRED should be the equivalent of '<, except it must expect two
491 cons cells of the form (NAME . NUM). See SORT for more details."
492 ;; 1 - create 1 list of cons cells
493 (let ((newlist nil)
494 (alst (oref namelst data))
495 (ulst (oref numlst data)))
496 (while alst
497 ;; this is reversed, but were are sorting anyway
498 (setq newlist (cons (cons (car alst) (car ulst)) newlist))
499 (setq alst (cdr alst)
500 ulst (cdr ulst)))
501 ;; 2 - Run sort routine on it
502 (setq newlist (sort newlist pred)
503 alst nil
504 ulst nil)
505 ;; 3 - Separate the lists
506 (while newlist
507 (setq alst (cons (car (car newlist)) alst)
508 ulst (cons (cdr (car newlist)) ulst))
509 (setq newlist (cdr newlist)))
510 ;; 4 - Store them back
511 (oset namelst data (reverse alst))
512 (oset numlst data (reverse ulst))))
513
514 ;;; Utilities
515
516 (defun chart-goto-xy (x y)
517 "Move cursor to position X Y in buffer, and add spaces and CRs if needed."
518 (let ((indent-tabs-mode nil)
519 (num (progn (goto-char (point-min)) (forward-line y))))
520 (if (and (= 0 num) (/= 0 (current-column))) (newline 1))
521 (if (eobp) (newline num))
522 (if (< x 0) (setq x 0))
523 (if (< y 0) (setq y 0))
524 ;; Now, a quicky column moveto/forceto method.
525 (or (= (move-to-column x) x)
526 (let ((p (point)))
527 (indent-to x)
528 (remove-text-properties p (point) '(face))))))
529
530 (defun chart-zap-chars (n)
531 "Zap up to N chars without deleteting EOLs."
532 (if (not (eobp))
533 (if (< n (- (save-excursion (end-of-line) (point)) (point)))
534 (delete-char n)
535 (delete-region (point) (save-excursion (end-of-line) (point))))))
536
537 (defun chart-display-label (label dir zone start end &optional face)
538 "Display LABEL in direction DIR in column/row ZONE between START and END.
539 Optional argument FACE is the property we wish to place on this text."
540 (if (eq dir 'horizontal)
541 (let (p1)
542 (chart-goto-xy (+ start (- (/ (- end start) 2) (/ (length label) 2)))
543 zone)
544 (setq p1 (point))
545 (insert label)
546 (chart-zap-chars (length label))
547 (put-text-property p1 (point) 'face face)
548 )
549 (let ((i 0)
550 (stz (+ start (- (/ (- end start) 2) (/ (length label) 2)))))
551 (while (< i (length label))
552 (chart-goto-xy zone (+ stz i))
553 (insert (aref label i))
554 (chart-zap-chars 1)
555 (put-text-property (1- (point)) (point) 'face face)
556 (setq i (1+ i))))))
557
558 (defun chart-draw-line (dir zone start end)
559 "Draw a line using line-drawing characters in direction DIR.
560 Use column or row ZONE between START and END"
561 (chart-display-label
562 (make-string (- end start) (if (eq dir 'vertical) ?| ?\-))
563 dir zone start end))
564
565 (defun chart-deface-rectangle (dir r1 r2 face)
566 "Colorize a rectangle in direction DIR across range R1 by range R2.
567 R1 and R2 are dotted pairs. Colorize it with FACE."
568 (let* ((range1 (if (eq dir 'vertical) r1 r2))
569 (range2 (if (eq dir 'vertical) r2 r1))
570 (y (car range2)))
571 (while (<= y (cdr range2))
572 (chart-goto-xy (car range1) y)
573 (put-text-property (point) (+ (point) (1+ (- (cdr range1) (car range1))))
574 'face face)
575 (setq y (1+ y)))))
576
577 ;;; Helpful `I don't want to learn eieio just now' washover functions
578
579 (defun chart-bar-quickie (dir title namelst nametitle numlst numtitle
580 &optional max sort-pred)
581 "Wash over the complex eieio stuff and create a nice bar chart.
582 Creat it going in direction DIR ['horizontal 'vertical] with TITLE
583 using a name sequence NAMELST labeled NAMETITLE with values NUMLST
584 labeled NUMTITLE.
585 Optional arguments:
586 Set the charts' max element display to MAX, and sort lists with
587 SORT-PRED if desired."
588 (let ((nc (make-instance chart-bar
589 :title title
590 :key-label "8-m" ; This is a text key pic
591 :direction dir
592 ))
593 (iv (eq dir 'vertical)))
594 (chart-add-sequence nc
595 (make-instance chart-sequece
596 :data namelst
597 :name nametitle)
598 (if iv 'x-axis 'y-axis))
599 (chart-add-sequence nc
600 (make-instance chart-sequece
601 :data numlst
602 :name numtitle)
603 (if iv 'y-axis 'x-axis))
604 (if sort-pred (chart-sort nc sort-pred))
605 (if (integerp max) (chart-trim nc max))
606 (switch-to-buffer (chart-new-buffer nc))
607 (chart-draw nc)))
608
609 ;;; Test code
610
611 (defun chart-test-it-all ()
612 "Test out various charting features."
613 (interactive)
614 (chart-bar-quickie 'vertical "Test Bar Chart"
615 '( "U1" "ME2" "C3" "B4" "QT" "EZ") "Items"
616 '( 5 -10 23 20 30 -3) "Values")
617 )
618
619 ;;; Sample utility function
620
621 (defun chart-file-count (dir)
622 "Draw a chart displaying the number of different file extentions in DIR."
623 (interactive "DDirectory: ")
624 (if (not (string-match "/$" dir))
625 (setq dir (concat dir "/")))
626 (message "Collecting statistics...")
627 (let ((flst (directory-files dir nil nil t))
628 (extlst (list "<dir>"))
629 (cntlst (list 0)))
630 (while flst
631 (let* ((j (string-match "[^\\.]\\(\\.[a-zA-Z]+\\|~\\|#\\)$" (car flst)))
632 (s (if (file-accessible-directory-p (concat dir (car flst)))
633 "<dir>"
634 (if j
635 (substring (car flst) (match-beginning 1) (match-end 1))
636 nil)))
637 (m (member s extlst)))
638 (if (not s) nil
639 (if m
640 (let ((cell (nthcdr (- (length extlst) (length m)) cntlst)))
641 (setcar cell (1+ (car cell))))
642 (setq extlst (cons s extlst)
643 cntlst (cons 1 cntlst)))))
644 (setq flst (cdr flst)))
645 ;; Lets create the chart!
646 (chart-bar-quickie 'vertical "Files Extension Distribution"
647 extlst "File Extensions"
648 cntlst "# of occurances"
649 10
650 '(lambda (a b) (> (cdr a) (cdr b))))
651 ))
652
653 (defun chart-space-usage (d)
654 "Display a top usage chart for directory D."
655 (interactive "DDirectory: ")
656 (message "Collecting statistics...")
657 (let ((nmlst nil)
658 (cntlst nil)
659 (b (get-buffer-create " *du-tmp*")))
660 (set-buffer b)
661 (erase-buffer)
662 (insert "cd " d ";du -sk * \n")
663 (message "Running `cd %s;du -sk *'..." d)
664 (call-process-region (point-min) (point-max) shell-file-name t
665 (current-buffer) nil)
666 (goto-char (point-min))
667 (message "Scanning output ...")
668 (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\([^ \n]+\\)$" nil t)
669 (let* ((nam (buffer-substring (match-beginning 2) (match-end 2)))
670 (num (buffer-substring (match-beginning 1) (match-end 1))))
671 (setq nmlst (cons nam nmlst)
672 ;; * 1000 to put it into bytes
673 cntlst (cons (* (string-to-number num) 1000) cntlst))))
674 (if (not nmlst)
675 (error "No files found!"))
676 (chart-bar-quickie 'vertical (format "Largest files in %s" d)
677 nmlst "File Name"
678 cntlst "File Size"
679 10
680 '(lambda (a b) (> (cdr a) (cdr b))))
681 ))
682
683 (defun chart-emacs-storage ()
684 "Chart the current storage requirements of Emacs."
685 (interactive)
686 (let* ((data (garbage-collect))
687 (names '("strings/2" "vectors"
688 "conses" "free cons"
689 "syms" "free syms"
690 "markers" "free mark"
691 ;; "floats" "free flt"
692 ))
693 (nums (list (/ (nth 3 data) 2)
694 (nth 4 data)
695 (car (car data)) ; conses
696 (cdr (car data))
697 (car (nth 1 data)) ; syms
698 (cdr (nth 1 data))
699 (car (nth 2 data)) ; markers
700 (cdr (nth 2 data))
701 ;(car (nth 5 data)) ; floats are Emacs only
702 ;(cdr (nth 5 data))
703 )))
704 ;; Lets create the chart!
705 (chart-bar-quickie 'vertical "Emacs Runtime Storage Usage"
706 names "Storage Items"
707 nums "Objects")))
708
709 (defun chart-emacs-lists ()
710 "Chart out the size of various important lists."
711 (interactive)
712 (let* ((names '("buffers" "frames" "processes" "faces"))
713 (nums (list (length (buffer-list))
714 (length (frame-list))
715 (length (process-list))
716 (length (face-list))
717 )))
718 (if (fboundp 'x-display-list)
719 (setq names (append names '("x-displays"))
720 nums (append nums (list (length (x-display-list))))))
721 ;; Lets create the chart!
722 (chart-bar-quickie 'vertical "Emacs List Size Chart"
723 names "Various Lists"
724 nums "Objects")))
725
726 (defun chart-rmail-from ()
727 "If we are in an rmail summary buffer, then chart out the froms."
728 (interactive)
729 (if (not (eq major-mode 'rmail-summary-mode))
730 (error "You must invoke chart-rmail-from in an rmail summary buffer"))
731 (let ((nmlst nil)
732 (cntlst nil))
733 (save-excursion
734 (goto-char (point-min))
735 (while (re-search-forward "\\-[A-Z][a-z][a-z] +\\(\\w+\\)@\\w+" nil t)
736 (let* ((nam (buffer-substring (match-beginning 1) (match-end 1)))
737 (m (member nam nmlst)))
738 (message "Scanned username %s" nam)
739 (if m
740 (let ((cell (nthcdr (- (length nmlst) (length m)) cntlst)))
741 (setcar cell (1+ (car cell))))
742 (setq nmlst (cons nam nmlst)
743 cntlst (cons 1 cntlst))))))
744 (chart-bar-quickie 'vertical "Username Occurance in RMAIL box"
745 nmlst "User Names"
746 cntlst "# of occurances"
747 10
748 '(lambda (a b) (> (cdr a) (cdr b))))
749 ))
750
751
752 (provide 'chart)
753
754 ;;; chart.el ends here