aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Oteiza2016-11-11 19:05:23 -0500
committerMark Oteiza2016-11-11 19:05:23 -0500
commit058816441840b5229b4437ff486a243e05fc5c93 (patch)
tree59479b41dc8e2c4deac6568721d80a1597bee344
parent06b7e73b3e3189143163f87506e5a262b657d57a (diff)
downloademacs-058816441840b5229b4437ff486a243e05fc5c93.tar.gz
emacs-058816441840b5229b4437ff486a243e05fc5c93.zip
Update chart.el
* lisp/emacs-lisp/chart.el (chart-mode): Derive from special-mode. (chart-draw): Wrap in with-silent-modifications. Instead of inserting a fixed number of newlines, use window-height. (chart-bar): (chart-trim): Use dolist. (chart-file-count): The previous implementation was buggy and missed extensions. Use file-name-extension instead to detect file extensions. Also use dolist and cl-incf to reduce verbosity.
-rw-r--r--lisp/emacs-lisp/chart.el73
1 files changed, 33 insertions, 40 deletions
diff --git a/lisp/emacs-lisp/chart.el b/lisp/emacs-lisp/chart.el
index be93c776287..962a85e90e7 100644
--- a/lisp/emacs-lisp/chart.el
+++ b/lisp/emacs-lisp/chart.el
@@ -60,6 +60,7 @@
60;; with all the bitmaps you want to use. 60;; with all the bitmaps you want to use.
61 61
62(require 'eieio) 62(require 'eieio)
63(eval-when-compile (require 'cl-lib))
63(eval-when-compile (require 'cl-generic)) 64(eval-when-compile (require 'cl-generic))
64 65
65;;; Code: 66;;; Code:
@@ -118,7 +119,7 @@ Useful if new Emacs is used on B&W display.")
118List is limited currently, which is ok since you really can't display 119List is limited currently, which is ok since you really can't display
119too much in text characters anyways.") 120too much in text characters anyways.")
120 121
121(define-derived-mode chart-mode fundamental-mode "CHART" 122(define-derived-mode chart-mode special-mode "Chart"
122 "Define a mode in Emacs for displaying a chart." 123 "Define a mode in Emacs for displaying a chart."
123 (buffer-disable-undo) 124 (buffer-disable-undo)
124 (set (make-local-variable 'font-lock-global-modes) nil) 125 (set (make-local-variable 'font-lock-global-modes) nil)
@@ -205,22 +206,23 @@ Make sure the width/height is correct."
205(cl-defmethod chart-draw ((c chart) &optional buff) 206(cl-defmethod chart-draw ((c chart) &optional buff)
206 "Start drawing a chart object C in optional BUFF. 207 "Start drawing a chart object C in optional BUFF.
207Erases current contents of buffer." 208Erases current contents of buffer."
208 (save-excursion 209 (with-silent-modifications
209 (if buff (set-buffer buff)) 210 (save-excursion
210 (erase-buffer) 211 (if buff (set-buffer buff))
211 (insert (make-string 100 ?\n)) 212 (erase-buffer)
212 ;; Start by displaying the axis 213 (insert (make-string (window-height (selected-window)) ?\n))
213 (chart-draw-axis c) 214 ;; Start by displaying the axis
214 ;; Display title 215 (chart-draw-axis c)
215 (chart-draw-title c) 216 ;; Display title
216 ;; Display data 217 (chart-draw-title c)
217 (message "Rendering chart...") 218 ;; Display data
218 (sit-for 0) 219 (message "Rendering chart...")
219 (chart-draw-data c) 220 (sit-for 0)
220 ;; Display key 221 (chart-draw-data c)
221 ; (chart-draw-key c) 222 ;; Display key
222 (message "Rendering chart...done") 223 ; (chart-draw-key c)
223 )) 224 (message "Rendering chart...done")
225 )))
224 226
225(cl-defmethod chart-draw-title ((c chart)) 227(cl-defmethod chart-draw-title ((c chart))
226 "Draw a title upon the chart. 228 "Draw a title upon the chart.
@@ -434,11 +436,10 @@ or is created with the bounds of SEQ."
434 (setq axis (make-instance 'chart-axis-range 436 (setq axis (make-instance 'chart-axis-range
435 :name (oref seq name) 437 :name (oref seq name)
436 :chart c))) 438 :chart c)))
437 (while l 439 (dolist (x l)
438 (if (< (car l) (car range)) (setcar range (car l))) 440 (if (< x (car range)) (setcar range x))
439 (if (> (car l) (cdr range)) (setcdr range (car l))) 441 (if (> x (cdr range)) (setcdr range x)))
440 (setq l (cdr l))) 442 (oset axis bounds range)))
441 (oset axis bounds range)))
442 (if (eq axis-label 'x-axis) (oset axis loweredge nil)) 443 (if (eq axis-label 'x-axis) (oset axis loweredge nil))
443 (eieio-oset c axis-label axis) 444 (eieio-oset c axis-label axis)
444 )) 445 ))
@@ -449,11 +450,10 @@ or is created with the bounds of SEQ."
449(cl-defmethod chart-trim ((c chart) max) 450(cl-defmethod chart-trim ((c chart) max)
450 "Trim all sequences in chart C to be at most MAX elements long." 451 "Trim all sequences in chart C to be at most MAX elements long."
451 (let ((s (oref c sequences))) 452 (let ((s (oref c sequences)))
452 (while s 453 (dolist (x s)
453 (let ((sl (oref (car s) data))) 454 (let ((sl (oref x data)))
454 (if (> (length sl) max) 455 (if (> (length sl) max)
455 (setcdr (nthcdr (1- max) sl) nil))) 456 (setcdr (nthcdr (1- max) sl) nil)))))
456 (setq s (cdr s))))
457 ) 457 )
458 458
459(cl-defmethod chart-sort ((c chart) pred) 459(cl-defmethod chart-sort ((c chart) pred)
@@ -614,27 +614,20 @@ SORT-PRED if desired."
614(defun chart-file-count (dir) 614(defun chart-file-count (dir)
615 "Draw a chart displaying the number of different file extensions in DIR." 615 "Draw a chart displaying the number of different file extensions in DIR."
616 (interactive "DDirectory: ") 616 (interactive "DDirectory: ")
617 (if (not (string-match "/$" dir))
618 (setq dir (concat dir "/")))
619 (message "Collecting statistics...") 617 (message "Collecting statistics...")
620 (let ((flst (directory-files dir nil nil t)) 618 (let ((flst (directory-files dir nil nil t))
621 (extlst (list "<dir>")) 619 (extlst (list "<dir>"))
622 (cntlst (list 0))) 620 (cntlst (list 0)))
623 (while flst 621 (dolist (f flst)
624 (let* ((j (string-match "[^\\.]\\(\\.[a-zA-Z]+\\|~\\|#\\)$" (car flst))) 622 (let* ((x (file-name-extension f))
625 (s (if (file-accessible-directory-p (concat dir (car flst))) 623 (s (if (file-accessible-directory-p (expand-file-name f dir))
626 "<dir>" 624 "<dir>" x))
627 (if j
628 (substring (car flst) (match-beginning 1) (match-end 1))
629 nil)))
630 (m (member s extlst))) 625 (m (member s extlst)))
631 (if (not s) nil 626 (unless (null s)
632 (if m 627 (if m
633 (let ((cell (nthcdr (- (length extlst) (length m)) cntlst))) 628 (cl-incf (car (nthcdr (- (length extlst) (length m)) cntlst)))
634 (setcar cell (1+ (car cell))))
635 (setq extlst (cons s extlst) 629 (setq extlst (cons s extlst)
636 cntlst (cons 1 cntlst))))) 630 cntlst (cons 1 cntlst))))))
637 (setq flst (cdr flst)))
638 ;; Let's create the chart! 631 ;; Let's create the chart!
639 (chart-bar-quickie 'vertical "Files Extension Distribution" 632 (chart-bar-quickie 'vertical "Files Extension Distribution"
640 extlst "File Extensions" 633 extlst "File Extensions"