From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Decebal Newsgroups: gmane.emacs.help Subject: Extra info in modeline (tip and questions) Date: Tue, 14 Apr 2009 07:03:11 -0700 (PDT) Organization: http://groups.google.com Message-ID: <533fdd2a-8d26-47ce-9413-1bd2300ee2d1@s20g2000yqh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1239720144 17462 80.91.229.12 (14 Apr 2009 14:42:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 14 Apr 2009 14:42:24 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Apr 14 16:43:43 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LtjrG-00045u-5x for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 16:43:30 +0200 Original-Received: from localhost ([127.0.0.1]:44642 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ltjpr-0007Q3-50 for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Apr 2009 10:42:03 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!s20g2000yqh.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 200 Original-NNTP-Posting-Host: 84.53.123.169 Original-X-Trace: posting.google.com 1239717791 24888 127.0.0.1 (14 Apr 2009 14:03:11 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 14 Apr 2009 14:03:11 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s20g2000yqh.googlegroups.com; posting-host=84.53.123.169; posting-account=K-cdeAoAAAD_0d505kUtHXJaT5LFIu-3 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; nl; rv:1.9.0.8) Gecko/2009032600 SUSE/3.0.8-1.1.1 Firefox/3.0.8,gzip(gfe),gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:168453 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:63731 Archived-At: I would like to have some more info in my modeline. Default the number of lines and the number of characters of the current buffer and in text-mode also the number of words. I made something that is easy to extend. I defined a vector whith the possible things to display and pro buffer there is a vector with what to display. The default is only lines and characters. This is done with: ;;; default lines and chars are displayed ;;; for nothing use nil (defvar mode-line-types (vector "lines" "chars") "*Types to dsiplay in the modeline." ) (make-variable-buffer-local 'mode-line-types) To display also words in text-mode, I use: (dolist (hook '(text-mode-hook)) (add-hook hook (lambda () (setq mode-line-types (vector "lines" "words" "chars")) ) ) ) At the end of the post is the code. I have a few questions: - Can the code be written better? (I am relative new to elisp.) - I tried to generalize. For example there is a function buffer-count- words, which I would like to use also on a region of the buffer. But when I call it interactively, it does not return anything. What do I need to change? - I defined two ways for counting words ("\\w+" and "[[:word:]\-]+"). Anyone a good idea for extending? (Almost every system counts words diferently. It would be nice to have a mode which would give the same count as the program that the other side is using. ;-] ) Ideas to make it better are offcourse welcome. The code: ;;; includes (require 'cl) ;;; definitions (defstruct (mode-line-struct (:constructor nil) (:constructor new-mode-line-struct (type description display function)) ) (type nil :read-only t) (description nil :read-only t) (display nil :read-only t) (function nil :read-only t) ) ;;; variables (setq mode-line-array (vector (new-mode-line-struct "chars" "Display number of chars" "C" 'buffer-count-chars ) (new-mode-line-struct "functions" "Display number of functions" "F" 'buffer-count-functions ) (new-mode-line-struct "lines" "Display number of lines" "L" 'buffer-count-lines ) (new-mode-line-struct "words" "Display number of words" "W" 'buffer-count-words ) ) ) (defvar buffer-mode-line nil "*Extension of modeline in the buffer." ) (defvar word-type nil "*Type of word." ) (make-variable-buffer-local 'word-type) ;;; default lines and chars are displayed ;;; for nothing use nil (defvar mode-line-types (vector "lines" "chars") "*Types to dsiplay in the modeline." ) (make-variable-buffer-local 'mode-line-types) ;;; functions (defun buffer-count(expression &optional start end) (interactive "sExpression: \nr") (setq start (or start (point-min))) (setq end (or end (point-max))) (how-many expression start end) ) (defun buffer-count-chars(&optional start end) (interactive "r") (number-to-string (buffer-count ".\\|\n" start end)) ) (defun buffer-count-functions(&optional start end) (interactive "r") (number-to-string (buffer-count "^(defun " start end)) ) (defun buffer-count-lines(&optional start end) (interactive "r") (number-to-string (+ (buffer-count "\n" start end) 1) ) ) (defun buffer-count-words(&optional start end) (interactive "r") (let ((regexp "\\w+")) (cond ((equal word-type "type1") (setq regexp "[[:word:]\-]+") ) ) (number-to-string (buffer-count regexp start end)) ) ) (defun buffer-mode-line-extra() (let ((i 0) (mode-line " ") (total (length mode-line-types)) ) (while (< i total) (setq mode-line (concat mode-line (get-mode-line-field (aref mode-line- types i))) ) (incf i) ) mode-line ) ) (defun get-mode-line-field(type) (let ((i 0) (field (format "*%s*" type)) (total (length mode-line-array)) ) (catch 'break (while (< i total) (if (equal type (mode-line-struct-type (aref mode-line- array i))) (progn (setq field (format "%s: %s " (mode-line-struct-display (aref mode-line-array i)) (funcall (mode-line-struct- function (aref mode-line-array i))) ) ) (throw 'break nil) ) ) (incf i) ) ) field ) ) (defun buffer-update-mode-line() (setq buffer-mode-line (buffer-mode-line-extra)) (force-mode-line-update) ) (unless buffer-mode-line (run-with-idle-timer 1 t 'buffer-update-mode-line) (buffer-update-mode-line) ) (unless (memq 'buffer-mode-line global-mode-string) (setq global-mode-string (append global-mode-string '(" " buffer-mode-line) ) ) )