Merge branch 'master' into comment-cache
[emacs.git] / lisp / emacs-lisp / chart.el
blobdc108f956c2f375a91686ab2b8cf459db1ebadb3
1 ;;; chart.el --- Draw charts (bar charts, etc) -*- lexical-binding: t -*-
3 ;; Copyright (C) 1996, 1998-1999, 2001, 2004-2005, 2007-2017 Free
4 ;; Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 0.2
8 ;; Keywords: OO, chart, graph
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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'.
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.
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)
45 ;; Customization:
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.
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:
57 ;; (setq x-bitmap-file-path (cons "~/mybitmaps" x-bitmap-file-path))
59 ;; to your .emacs (or wherever) and load the `chart-face-pixmap-list'
60 ;; with all the bitmaps you want to use.
62 (require 'eieio)
63 (eval-when-compile (require 'cl-lib))
64 (eval-when-compile (require 'cl-generic))
66 ;;; Code:
67 (define-obsolete-variable-alias 'chart-map 'chart-mode-map "24.1")
68 (defvar chart-mode-map (make-sparse-keymap) "Keymap used in chart mode.")
70 (defvar chart-local-object nil
71 "Local variable containing the locally displayed chart object.")
72 (make-variable-buffer-local 'chart-local-object)
74 (defvar chart-face-color-list '("red" "green" "blue"
75 "cyan" "yellow" "purple")
76 "Colors to use when generating `chart-face-list'.
77 Colors will be the background color.")
79 (defvar chart-face-pixmap-list
80 (if (and (fboundp 'display-graphic-p)
81 (display-graphic-p))
82 '("dimple1" "scales" "dot" "cross_weave" "boxes" "dimple3"))
83 "If pixmaps are allowed, display these background pixmaps.
84 Useful if new Emacs is used on B&W display.")
86 (defcustom chart-face-use-pixmaps nil
87 "Non-nil to use fancy pixmaps in the background of chart face colors."
88 :group 'eieio
89 :type 'boolean)
91 (declare-function x-display-color-cells "xfns.c" (&optional terminal))
93 (defvar chart-face-list
94 (if (display-color-p)
95 (let ((cl chart-face-color-list)
96 (pl chart-face-pixmap-list)
97 (faces ())
98 nf)
99 (while cl
100 (setq nf (make-face
101 (intern (concat "chart-" (car cl) "-" (car pl)))))
102 (set-face-background nf (if (condition-case nil
103 (> (x-display-color-cells) 4)
104 (error t))
105 (car cl)
106 "white"))
107 (set-face-foreground nf "black")
108 (if (and chart-face-use-pixmaps
110 (fboundp 'set-face-background-pixmap))
111 (condition-case nil
112 (set-face-background-pixmap nf (car pl))
113 (error (message "Cannot set background pixmap %s" (car pl)))))
114 (push nf faces)
115 (setq cl (cdr cl)
116 pl (cdr pl)))
117 faces))
118 "Faces used to colorize charts.
119 List is limited currently, which is ok since you really can't display
120 too much in text characters anyways.")
122 (define-derived-mode chart-mode special-mode "Chart"
123 "Define a mode in Emacs for displaying a chart."
124 (buffer-disable-undo)
125 (set (make-local-variable 'font-lock-global-modes) nil)
126 (font-lock-mode -1) ;Isn't it off already? --Stef
129 (defclass chart ()
130 ((title :initarg :title
131 :initform "Emacs Chart")
132 (title-face :initarg :title-face
133 :initform 'bold-italic)
134 (x-axis :initarg :x-axis
135 :initform nil )
136 (x-margin :initarg :x-margin
137 :initform 5)
138 (x-width :initarg :x-width
140 (y-axis :initarg :y-axis
141 :initform nil)
142 (y-margin :initarg :y-margin
143 :initform 5)
144 (y-width :initarg :y-width
146 (key-label :initarg :key-label
147 :initform "Key")
148 (sequences :initarg :sequences
149 :initform nil)
151 "Superclass for all charts to be displayed in an Emacs buffer.")
153 (defun chart-new-buffer (obj)
154 "Create a new buffer NAME in which the chart OBJ is displayed.
155 Returns the newly created buffer."
156 (with-current-buffer (get-buffer-create (format "*%s*" (oref obj title)))
157 (chart-mode)
158 (setq chart-local-object obj)
159 (current-buffer)))
161 (cl-defmethod initialize-instance :after ((obj chart) &rest _fields)
162 "Initialize the chart OBJ being created with FIELDS.
163 Make sure the width/height is correct."
164 (oset obj x-width (- (window-width) 10))
165 (oset obj y-width (- (window-height) 12)))
167 (defclass chart-axis ()
168 ((name :initarg :name
169 :initform "Generic Axis")
170 (loweredge :initarg :loweredge
171 :initform t)
172 (name-face :initarg :name-face
173 :initform 'bold)
174 (labels-face :initarg :labels-face
175 :initform 'italic)
176 (chart :initarg :chart
177 :initform nil)
179 "Superclass used for display of an axis.")
181 (defclass chart-axis-range (chart-axis)
182 ((bounds :initarg :bounds
183 :initform '(0.0 . 50.0))
185 "Class used to display an axis defined by a range of values.")
187 (defclass chart-axis-names (chart-axis)
188 ((items :initarg :items
189 :initform nil)
191 "Class used to display an axis which represents different named items.")
193 (defclass chart-sequece ()
194 ((data :initarg :data
195 :initform nil)
196 (name :initarg :name
197 :initform "Data")
199 "Class used for all data in different charts.")
201 (defclass chart-bar (chart)
202 ((direction :initarg :direction
203 :initform vertical))
204 "Subclass for bar charts (vertical or horizontal).")
206 (cl-defmethod chart-draw ((c chart) &optional buff)
207 "Start drawing a chart object C in optional BUFF.
208 Erases current contents of buffer."
209 (with-silent-modifications
210 (save-excursion
211 (if buff (set-buffer buff))
212 (erase-buffer)
213 (insert (make-string (window-height (selected-window)) ?\n))
214 ;; Start by displaying the axis
215 (chart-draw-axis c)
216 ;; Display title
217 (chart-draw-title c)
218 ;; Display data
219 (message "Rendering chart...")
220 (sit-for 0)
221 (chart-draw-data c)
222 ;; Display key
223 ; (chart-draw-key c)
224 (message "Rendering chart...done")
227 (cl-defmethod chart-draw-title ((c chart))
228 "Draw a title upon the chart.
229 Argument C is the chart object."
230 (chart-display-label (oref c title) 'horizontal 0 0 (window-width)
231 (oref c title-face)))
233 (cl-defmethod chart-size-in-dir ((c chart) dir)
234 "Return the physical size of chart C in direction DIR."
235 (if (eq dir 'vertical)
236 (oref c y-width)
237 (oref c x-width)))
239 (cl-defmethod chart-draw-axis ((c chart))
240 "Draw axis into the current buffer defined by chart C."
241 (let ((ymarg (oref c y-margin))
242 (xmarg (oref c x-margin))
243 (ylen (oref c y-width))
244 (xlen (oref c x-width)))
245 (chart-axis-draw (oref c y-axis) 'vertical ymarg
246 (if (oref (oref c y-axis) loweredge) nil xlen)
247 xmarg (+ xmarg ylen))
248 (chart-axis-draw (oref c x-axis) 'horizontal xmarg
249 (if (oref (oref c x-axis) loweredge) nil ylen)
250 ymarg (+ ymarg xlen)))
253 (cl-defmethod chart-axis-draw ((a chart-axis) &optional dir margin zone start end)
254 "Draw some axis for A in direction DIR with MARGIN in boundary.
255 ZONE is a zone specification.
256 START and END represent the boundary."
257 (chart-draw-line dir (+ margin (if zone zone 0)) start end)
258 (chart-display-label (oref a name) dir (if zone (+ zone margin 3)
259 (if (eq dir 'horizontal)
260 1 0))
261 start end (oref a name-face)))
263 (cl-defmethod chart-translate-xpos ((c chart) x)
264 "Translate in chart C the coordinate X into a screen column."
265 (let ((range (oref (oref c x-axis) bounds)))
266 (+ (oref c x-margin)
267 (round (* (float (- x (car range)))
268 (/ (float (oref c x-width))
269 (float (- (cdr range) (car range))))))))
272 (cl-defmethod chart-translate-ypos ((c chart) y)
273 "Translate in chart C the coordinate Y into a screen row."
274 (let ((range (oref (oref c y-axis) bounds)))
275 (+ (oref c x-margin)
276 (- (oref c y-width)
277 (round (* (float (- y (car range)))
278 (/ (float (oref c y-width))
279 (float (- (cdr range) (car range)))))))))
282 (cl-defmethod chart-axis-draw ((a chart-axis-range) &optional dir margin zone _start _end)
283 "Draw axis information based upon a range to be spread along the edge.
284 A is the chart to draw. DIR is the direction.
285 MARGIN, ZONE, START, and END specify restrictions in chart space."
286 (cl-call-next-method)
287 ;; We prefer about 5 spaces between each value
288 (let* ((i (car (oref a bounds)))
289 (e (cdr (oref a bounds)))
290 (z (if zone zone 0))
291 (s nil)
292 (rng (- e i))
293 ;; want to jump by units of 5 spaces or so
294 (j (/ rng (/ (chart-size-in-dir (oref a chart) dir) 4)))
296 (if (= j 0) (setq j 1))
297 (while (<= i e)
298 (setq s
299 (cond ((> i 999999)
300 (format "%dM" (/ i 1000000)))
301 ((> i 999)
302 (format "%dK" (/ i 1000)))
304 (format "%d" i))))
305 (if (eq dir 'vertical)
306 (let ((x (+ (+ margin z) (if (oref a loweredge)
307 (- (length s)) 1))))
308 (if (< x 1) (setq x 1))
309 (chart-goto-xy x (chart-translate-ypos (oref a chart) i)))
310 (chart-goto-xy (chart-translate-xpos (oref a chart) i)
311 (+ margin z (if (oref a loweredge) -1 1))))
312 (setq p1 (point))
313 (insert s)
314 (chart-zap-chars (length s))
315 (put-text-property p1 (point) 'face (oref a labels-face))
316 (setq i (+ i j))))
319 (cl-defmethod chart-translate-namezone ((c chart) n)
320 "Return a dot-pair representing a positional range for a name.
321 The name in chart C of the Nth name resides.
322 Automatically compensates for direction."
323 (let* ((dir (oref c direction))
324 (w (if (eq dir 'vertical) (oref c x-width) (oref c y-width)))
325 (m (if (eq dir 'vertical) (oref c y-margin) (oref c x-margin)))
326 (ns (length
327 (oref (if (eq dir 'vertical) (oref c x-axis) (oref c y-axis))
328 items)))
329 (lpn (/ (+ 1.0 (float w)) (float ns)))
331 (cons (+ m (round (* lpn (float n))))
332 (+ m -1 (round (* lpn (+ 1.0 (float n))))))
335 (cl-defmethod chart-axis-draw ((a chart-axis-names) &optional dir margin zone _start _end)
336 "Draw axis information based upon A range to be spread along the edge.
337 Optional argument DIR is the direction of the chart.
338 Optional arguments MARGIN, ZONE, START and END specify boundaries of the drawing."
339 (cl-call-next-method)
340 ;; We prefer about 5 spaces between each value
341 (let* ((i 0)
342 (s (oref a items))
343 (z (if zone zone 0))
344 (r nil)
345 (p nil)
346 (odd nil)
348 (while s
349 (setq odd (= (% (length s) 2) 1))
350 (setq r (chart-translate-namezone (oref a chart) i))
351 (if (eq dir 'vertical)
352 (setq p (/ (+ (car r) (cdr r)) 2))
353 (setq p (- (+ (car r) (/ (- (cdr r) (car r)) 2))
354 (/ (length (car s)) 2))))
355 (if (eq dir 'vertical)
356 (let ((x (+ (+ margin z) (if (oref a loweredge)
357 (- (length (car s)))
358 (length (car s))))))
359 (if (< x 1) (setq x 1))
360 (if (> (length (car s)) (1- margin))
361 (setq x (+ x margin)))
362 (chart-goto-xy x p))
363 (chart-goto-xy p (+ (+ margin z) (if (oref a loweredge)
364 (if odd -2 -1)
365 (if odd 2 1)))))
366 (setq p1 (point))
367 (insert (car s))
368 (chart-zap-chars (length (car s)))
369 (put-text-property p1 (point) 'face (oref a labels-face))
370 (setq i (+ i 1)
371 s (cdr s))))
374 (cl-defmethod chart-draw-data ((c chart-bar))
375 "Display the data available in a bar chart C."
376 (let* ((data (oref c sequences))
377 (dir (oref c direction))
378 (odir (if (eq dir 'vertical) 'horizontal 'vertical))
380 (while data
381 (if (stringp (car (oref (car data) data)))
382 ;; skip string lists...
384 ;; display number lists...
385 (let ((i 0)
386 (seq (oref (car data) data)))
387 (while seq
388 (let* ((rng (chart-translate-namezone c i))
389 (dp (if (eq dir 'vertical)
390 (chart-translate-ypos c (car seq))
391 (chart-translate-xpos c (car seq))))
392 (zp (if (eq dir 'vertical)
393 (chart-translate-ypos c 0)
394 (chart-translate-xpos c 0)))
395 (fc (if chart-face-list
396 (nth (% i (length chart-face-list)) chart-face-list)
397 'default))
399 (if (< dp zp)
400 (progn
401 (chart-draw-line dir (car rng) dp zp)
402 (chart-draw-line dir (cdr rng) dp zp))
403 (chart-draw-line dir (car rng) zp (1+ dp))
404 (chart-draw-line dir (cdr rng) zp (1+ dp)))
405 (if (= (car rng) (cdr rng)) nil
406 (chart-draw-line odir dp (1+ (car rng)) (cdr rng))
407 (chart-draw-line odir zp (car rng) (1+ (cdr rng))))
408 (if (< dp zp)
409 (chart-deface-rectangle dir rng (cons dp zp) fc)
410 (chart-deface-rectangle dir rng (cons zp dp) fc))
412 ;; find the bounds, and chart it!
413 ;; for now, only do one!
414 (setq i (1+ i)
415 seq (cdr seq)))))
416 (setq data (cdr data))))
419 (cl-defmethod chart-add-sequence ((c chart) &optional seq axis-label)
420 "Add to chart object C the sequence object SEQ.
421 If AXIS-LABEL, then the axis stored in C is updated with the bounds of SEQ,
422 or is created with the bounds of SEQ."
423 (if axis-label
424 (let ((axis (eieio-oref c axis-label)))
425 (if (stringp (car (oref seq data)))
426 (let ((labels (oref seq data)))
427 (if (not axis)
428 (setq axis (make-instance 'chart-axis-names
429 :name (oref seq name)
430 :items labels
431 :chart c))
432 (oset axis items labels)))
433 (let ((range (cons 0 1))
434 (l (oref seq data)))
435 (if (not axis)
436 (setq axis (make-instance 'chart-axis-range
437 :name (oref seq name)
438 :chart c)))
439 (dolist (x l)
440 (if (< x (car range)) (setcar range x))
441 (if (> x (cdr range)) (setcdr range x)))
442 (oset axis bounds range)))
443 (if (eq axis-label 'x-axis) (oset axis loweredge nil))
444 (eieio-oset c axis-label axis)
446 (oset c sequences (append (oref c sequences) (list seq))))
448 ;;; Charting optimizers
450 (cl-defmethod chart-trim ((c chart) max)
451 "Trim all sequences in chart C to be at most MAX elements long."
452 (let ((s (oref c sequences)))
453 (dolist (x s)
454 (let ((sl (oref x data)))
455 (if (> (length sl) max)
456 (setcdr (nthcdr (1- max) sl) nil)))))
459 (cl-defmethod chart-sort ((c chart) pred)
460 "Sort the data in chart C using predicate PRED.
461 See `chart-sort-matchlist' for more details."
462 (let* ((sl (oref c sequences))
463 (s1 (car sl))
464 (s2 (car (cdr sl)))
465 (s nil))
466 (if (stringp (car (oref s1 data)))
467 (progn
468 (chart-sort-matchlist s1 s2 pred)
469 (setq s (oref s1 data)))
470 (if (stringp (car (oref s2 data)))
471 (progn
472 (chart-sort-matchlist s2 s1 pred)
473 (setq s (oref s2 data)))
474 (error "Sorting of chart %s not supported" (eieio-object-name c))))
475 (if (eq (oref c direction) 'horizontal)
476 (oset (oref c y-axis) items s)
477 (oset (oref c x-axis) items s)
481 (defun chart-sort-matchlist (namelst numlst pred)
482 "Sort NAMELST and NUMLST (both sequence objects) based on predicate PRED.
483 PRED should be the equivalent of `<', except it must expect two
484 cons cells of the form (NAME . NUM). See `sort' for more details."
485 ;; 1 - create 1 list of cons cells
486 (let ((newlist nil)
487 (alst (oref namelst data))
488 (ulst (oref numlst data)))
489 (while alst
490 ;; this is reversed, but were are sorting anyway
491 (setq newlist (cons (cons (car alst) (car ulst)) newlist))
492 (setq alst (cdr alst)
493 ulst (cdr ulst)))
494 ;; 2 - Run sort routine on it
495 (setq newlist (sort newlist pred)
496 alst nil
497 ulst nil)
498 ;; 3 - Separate the lists
499 (while newlist
500 (setq alst (cons (car (car newlist)) alst)
501 ulst (cons (cdr (car newlist)) ulst))
502 (setq newlist (cdr newlist)))
503 ;; 4 - Store them back
504 (oset namelst data (reverse alst))
505 (oset numlst data (reverse ulst))))
507 ;;; Utilities
509 (defun chart-goto-xy (x y)
510 "Move cursor to position X Y in buffer, and add spaces and CRs if needed."
511 (let ((indent-tabs-mode nil)
512 (num (progn (goto-char (point-min)) (forward-line y))))
513 (if (and (= 0 num) (/= 0 (current-column))) (newline 1))
514 (if (eobp) (newline num))
515 (if (< x 0) (setq x 0))
516 (if (< y 0) (setq y 0))
517 ;; Now, a quicky column moveto/forceto method.
518 (or (= (move-to-column x) x)
519 (let ((p (point)))
520 (indent-to x)
521 (remove-text-properties p (point) '(face))))))
523 (defun chart-zap-chars (n)
524 "Zap up to N chars without deleting EOLs."
525 (if (not (eobp))
526 (if (< n (- (point-at-eol) (point)))
527 (delete-char n)
528 (delete-region (point) (point-at-eol)))))
530 (defun chart-display-label (label dir zone start end &optional face)
531 "Display LABEL in direction DIR in column/row ZONE between START and END.
532 Optional argument FACE is the property we wish to place on this text."
533 (if (eq dir 'horizontal)
534 (let (p1)
535 (chart-goto-xy (+ start (- (/ (- end start) 2) (/ (length label) 2)))
536 zone)
537 (setq p1 (point))
538 (insert label)
539 (chart-zap-chars (length label))
540 (put-text-property p1 (point) 'face face)
542 (let ((i 0)
543 (stz (+ start (- (/ (- end start) 2) (/ (length label) 2)))))
544 (while (< i (length label))
545 (chart-goto-xy zone (+ stz i))
546 (insert (aref label i))
547 (chart-zap-chars 1)
548 (put-text-property (1- (point)) (point) 'face face)
549 (setq i (1+ i))))))
551 (defun chart-draw-line (dir zone start end)
552 "Draw a line using line-drawing characters in direction DIR.
553 Use column or row ZONE between START and END."
554 (chart-display-label
555 (make-string (- end start) (if (eq dir 'vertical) ?| ?\-))
556 dir zone start end))
558 (defun chart-deface-rectangle (dir r1 r2 face)
559 "Colorize a rectangle in direction DIR across range R1 by range R2.
560 R1 and R2 are dotted pairs. Colorize it with FACE."
561 (let* ((range1 (if (eq dir 'vertical) r1 r2))
562 (range2 (if (eq dir 'vertical) r2 r1))
563 (y (car range2)))
564 (while (<= y (cdr range2))
565 (chart-goto-xy (car range1) y)
566 (put-text-property (point) (+ (point) (1+ (- (cdr range1) (car range1))))
567 'face face)
568 (setq y (1+ y)))))
570 ;;; Helpful `I don't want to learn eieio just now' washover functions
572 (defun chart-bar-quickie (dir title namelst nametitle numlst numtitle
573 &optional max sort-pred)
574 "Wash over the complex EIEIO stuff and create a nice bar chart.
575 Create it going in direction DIR [`horizontal' `vertical'] with TITLE
576 using a name sequence NAMELST labeled NAMETITLE with values NUMLST
577 labeled NUMTITLE.
578 Optional arguments:
579 Set the chart's max element display to MAX, and sort lists with
580 SORT-PRED if desired."
581 (let ((nc (make-instance 'chart-bar
582 :title title
583 :key-label "8-m" ; This is a text key pic
584 :direction dir
586 (iv (eq dir 'vertical)))
587 (chart-add-sequence nc
588 (make-instance 'chart-sequece
589 :data namelst
590 :name nametitle)
591 (if iv 'x-axis 'y-axis))
592 (chart-add-sequence nc
593 (make-instance 'chart-sequece
594 :data numlst
595 :name numtitle)
596 (if iv 'y-axis 'x-axis))
597 (if sort-pred (chart-sort nc sort-pred))
598 (if (integerp max) (chart-trim nc max))
599 (switch-to-buffer (chart-new-buffer nc))
600 (chart-draw nc)))
602 ;;; Test code
604 (defun chart-test-it-all ()
605 "Test out various charting features."
606 (interactive)
607 (chart-bar-quickie 'vertical "Test Bar Chart"
608 '( "U1" "ME2" "C3" "B4" "QT" "EZ") "Items"
609 '( 5 -10 23 20 30 -3) "Values")
612 ;;; Sample utility function
614 (defun chart-file-count (dir)
615 "Draw a chart displaying the number of different file extensions in DIR."
616 (interactive "DDirectory: ")
617 (message "Collecting statistics...")
618 (let ((flst (directory-files dir nil nil t))
619 (extlst (list "<dir>"))
620 (cntlst (list 0)))
621 (dolist (f flst)
622 (let* ((x (file-name-extension f))
623 (s (if (file-accessible-directory-p (expand-file-name f dir))
624 "<dir>" x))
625 (m (member s extlst)))
626 (unless (null s)
627 (if m
628 (cl-incf (car (nthcdr (- (length extlst) (length m)) cntlst)))
629 (setq extlst (cons s extlst)
630 cntlst (cons 1 cntlst))))))
631 ;; Let's create the chart!
632 (chart-bar-quickie 'vertical "Files Extension Distribution"
633 extlst "File Extensions"
634 cntlst "# of occurrences"
636 (lambda (a b) (> (cdr a) (cdr b))))
639 (defun chart-space-usage (d)
640 "Display a top usage chart for directory D."
641 (interactive "DDirectory: ")
642 (message "Collecting statistics...")
643 (let ((nmlst nil)
644 (cntlst nil)
645 (b (get-buffer-create " *du-tmp*")))
646 (set-buffer b)
647 (erase-buffer)
648 (insert "cd " d ";du -sk * \n")
649 (message "Running `cd %s;du -sk *'..." d)
650 (call-process-region (point-min) (point-max) shell-file-name t
651 (current-buffer) nil)
652 (goto-char (point-min))
653 (message "Scanning output ...")
654 (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\([^ \n]+\\)$" nil t)
655 (let* ((nam (buffer-substring (match-beginning 2) (match-end 2)))
656 (num (buffer-substring (match-beginning 1) (match-end 1))))
657 (setq nmlst (cons nam nmlst)
658 ;; * 1000 to put it into bytes
659 cntlst (cons (* (string-to-number num) 1000) cntlst))))
660 (if (not nmlst)
661 (error "No files found!"))
662 (chart-bar-quickie 'vertical (format "Largest files in %s" d)
663 nmlst "File Name"
664 cntlst "File Size"
666 (lambda (a b) (> (cdr a) (cdr b))))
669 (defun chart-emacs-storage ()
670 "Chart the current storage requirements of Emacs."
671 (interactive)
672 (let* ((data (garbage-collect)))
673 ;; Let's create the chart!
674 (chart-bar-quickie 'vertical "Emacs Runtime Storage Usage"
675 (mapcar (lambda (x) (symbol-name (car x))) data)
676 "Storage Items"
677 (mapcar (lambda (x) (* (nth 1 x) (nth 2 x)))
678 data)
679 "Bytes")))
681 (defun chart-emacs-lists ()
682 "Chart out the size of various important lists."
683 (interactive)
684 (let* ((names '("buffers" "frames" "processes" "faces"))
685 (nums (list (length (buffer-list))
686 (length (frame-list))
687 (length (process-list))
688 (length (face-list))
690 (if (fboundp 'x-display-list)
691 (setq names (append names '("x-displays"))
692 nums (append nums (list (length (x-display-list))))))
693 ;; Let's create the chart!
694 (chart-bar-quickie 'vertical "Emacs List Size Chart"
695 names "Various Lists"
696 nums "Objects")))
698 (defun chart-rmail-from ()
699 "If we are in an rmail summary buffer, then chart out the froms."
700 (interactive)
701 (if (not (eq major-mode 'rmail-summary-mode))
702 (error "You must invoke chart-rmail-from in an rmail summary buffer"))
703 (let ((nmlst nil)
704 (cntlst nil))
705 (save-excursion
706 (goto-char (point-min))
707 (while (re-search-forward "\\-[A-Z][a-z][a-z] +\\(\\w+\\)@\\w+" nil t)
708 (let* ((nam (buffer-substring (match-beginning 1) (match-end 1)))
709 (m (member nam nmlst)))
710 (message "Scanned username %s" nam)
711 (if m
712 (let ((cell (nthcdr (- (length nmlst) (length m)) cntlst)))
713 (setcar cell (1+ (car cell))))
714 (setq nmlst (cons nam nmlst)
715 cntlst (cons 1 cntlst))))))
716 (chart-bar-quickie 'vertical "Username Occurrence in RMAIL box"
717 nmlst "User Names"
718 cntlst "# of occurrences"
720 (lambda (a b) (> (cdr a) (cdr b))))
724 (provide 'chart)
726 ;;; chart.el ends here