help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Setting the modeline per (type of) buffer


From: Decebal
Subject: Re: Setting the modeline per (type of) buffer
Date: Sat, 17 Jan 2009 12:09:41 -0800 (PST)
User-agent: G2/1.0

On 17 jan, 14:02, Decebal <address@hidden> wrote:
> On 17 jan, 12:39, Decebal <address@hidden> wrote:
>
> > I made something to display the lines, words and characters in the
> > modeline with filling global-mode-string. But what if I wanted to
> > displat different things in different modes/buffers? For example,
> > maybe it would be handy to have the number of functions in my C-code.
> > But that is of no use in a normal text-file.
>
> I found the first part. I made the following code:
>
> (defvar buffer-count-chars
>   nil
>   "*Number of chars in the buffer."
>   )
>
> (defvar buffer-count-lines
>   nil
>   "*Number of lines in the buffer."
>   )
>
> (defvar buffer-count-words
>   nil
>   "*Number of words in the buffer."
>   )
>
> (defvar buffer-mode-line
>   nil
>   "*Extension of modeline in the buffer."
>   )
>
> (defun buffer-count(expression)
>   (how-many expression (point-min) (point-max))
>   )
>
> (defun buffer-default-mode-line()
>   (setq buffer-count-lines
>         (number-to-string
>          (+ (buffer-count "\n") 1)
>         )
>
>         buffer-count-words
>         (number-to-string (buffer-count "\\w+"))
>
>         buffer-count-chars
>         (number-to-string (buffer-count ".\\|\n"))
>         )
>   (concat "Lines: " buffer-count-lines
>           " Words: " buffer-count-words
>           " Chars: " buffer-count-chars
>           " "
>           )
>   )
>
> (defun buffer-update-mode-line()
>   (setq buffer-mode-line (buffer-default-mode-line))
>   (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)
>                 )
>         )
>   )
>
> In the function buffer-update-mode-line I could do things depending on
> the mode.
> One things puzzles me. In the setq global-mode-string I need the "
> " (first parameter of the append), otherwise I get an invalid in my
> modeline. Because of this the first displayed text needs not have a
> space before it (otherwise it would get two). Why is this?
>
> Is this a good way to do this, or is there a better way?

I made an extra for Emacs-Lisp mode. It displays also the number or
functions in the source. It is expecting that every function
definition starts on a line. If that is not the case the number will
be wrong.


(defun buffer-descr-elisp()
  (concat
   "Functions: " (number-to-string (buffer-count "^(defun ")) " "
   )
  )

(defun buffer-update-mode-line()
  (setq buffer-mode-line (buffer-default-mode-line))
  (cond ((string-equal mode-name "Emacs-Lisp")
         (setq buffer-mode-line
               (concat buffer-mode-line (buffer-descr-elisp))
               )
         )
        )
  (force-mode-line-update)
  )



reply via email to

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