Commit 6448a6b3 authored by Gerd Moellmann's avatar Gerd Moellmann
Browse files

*** empty log message ***

parent bcdda9a4
......@@ -704,6 +704,11 @@ appropriate for C-style escape sequences in strings.
** New modes and packages
*** bs.el is a new package for buffer selection similar to
list-buffers or electric-buffer-list. Use M-x bs-show to display a
buffer menu with this package. You can use M-x bs-customize to
customize the package.
*** The minor modes cwarn-mode and global-cwarn-mode highlights
suspicious C and C++ constructions. Currently, assignments inside
expressions, semicolon following `if', `for' and `while' (except, of
......
1999-12-28 Gerd Moellmann <gerd@gnu.org>
* bs.el: New file.
1999-12-28 Richard M. Stallman <rms@caffeine.ai.mit.edu>
* textmodes/ispell.el (ispell-process-line):
......
;;; bs.el --- menu for selecting and displaying buffers
;; Copyright (C) 1998, 1999 Free Software Foundation, Inc.
;; Author: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
;; Maintainer: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
;; Keywords: convenience
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Version: 1.17
;; X-URL: http://home.netsurf.de/olaf.sylvester/emacs
;;
;; The bs-package contains a main function bs-show for poping up a
;; buffer in a way similar to `list-buffers' and `electric-buffer-list':
;; The new buffer offers a Buffer Selection Menu for manipulating
;; the buffer list and buffers.
;;
;; -----------------------------------------------------------------------
;; | MR Buffer Size Mode File |
;; | -- ------ ---- ---- ---- |
;; |. bs.el 14690 Emacs-Lisp /home/sun/sylvester/el/bs.e$|
;; | % executable.el 9429 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
;; | % vc.el 104893 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
;; | % test_vc.el 486 Emacs-Lisp /home/sun/sylvester/el/test$|
;; | % vc-hooks.el 43605 Emacs-Lisp /usr/share/emacs/19.34/lisp$|
;; -----------------------------------------------------------------------
;;; Quick Installation und Customization:
;; Use
;; M-x bs-show
;; for buffer selection or optional bind a key to main function `bs-show'
;; (global-set-key "\C-x\C-b" 'bs-show) ;; or another key
;;
;; For customization use
;; M-x bs-customize
;;; More Commentary:
;; bs-show will generate a new buffer named *buffer-selection*, which shows
;; all buffers or a subset of them, and has possibilities for deleting,
;; saving and selecting buffers. For more details see docstring of
;; function `bs-mode'. A current configuration describes which buffers appear
;; in *buffer-selection*. See docstring of variable `bs-configurations' for
;; more details.
;;
;; The package bs combines the advantages of the Emacs functions
;; `list-buffers' and `electric-buffer-list'.
;;
;; Additioal features for Buffer Selection Menu:
;; - configurable list of buffers (show only files etc.).
;; - comfortable way to change displayed subset of all buffers.
;; - show sorted list of buffers.
;; - cyclic navigation:
;; - goes to top of buffer list if you are on last line and press down.
;; - goes to end of buffer list if you are on first line and press up.
;; - Offer an alternative buffer list by prefix key C-u.
;;; Cycling through buffers
;; This package offers two functions for buffer cycling. If you want to cycle
;; through buffer list you can use `bs-cycle-next' or `bs-cycle-previous'.
;; Bind these function to a key like
;; (global-set-key [(f9)] 'bs-cycle-previous)
;; (global-set-key [(f10)] 'bs-cycle-next)
;;
;; Both functions use a special subset of all buffers for cycling to avoid
;; to go through internal buffers like *Messages*.
;;
;; Cycling through buffers ignores sorting because sorting destroys
;; the logical buffer list. If buffer list is sorted by size you
;; won't be able to cycle to the smallest buffer.
;;; Customization:
;; There is a customization group called `bs' in group `convenience'.
;; Start customization by M-x bs-customize
;;
;; Buffer list
;; -----------
;; You can define your own configurations by extending variable
;; `bs-configurations' (see docstring for details).
;;
;; `bs-default-configuration' contains the name of default configuration.
;; The default value is "files" which means to show only files.
;;
;; If you always want to see all buffers, customize variable
;; `bs-default-configuration' in customization group `bs'.
;;
;; Configure sorting
;; -----------------
;; You can define functions for sorting the buffer list.
;; When selecting buffers, you can step through available sorting
;; methods with key 'S'.
;; To define a new way of sorting, customize variable `bs-sort-functions'.
;;
;; There are four basic functions for sorting:
;; by buffer name, by mode, by size, or by filename
;;
;; Configure buffer cycling
;; ------------------------
;; When cycling through buffer list the functions for cycling will use
;; the current configuration of bs to calculate the buffer list.
;; If you want to use a different configuration for cycling you have to set
;; the variable `bs-cycle-configuration-name'. You can customize this variable.
;;
;; For example: If you use the configuration called "files-and-scratch" you
;; can cycle through all file buffers and *scratch* although your current
;; configuration perhaps is "files" which ignores buffer *scratch*.
;;; History:
;;; Code:
;; ----------------------------------------------------------------------
;; Globals for customization
;; ----------------------------------------------------------------------
(defgroup bs nil
"Buffer Selection: Maintaining buffers by buffer menu."
:group 'convenience)
(defgroup bs-appearence nil
"Buffer Selection appearence: Appearence of bs buffer menu."
:group 'bs)
(defcustom bs-attributes-list
'(("" 1 1 left bs--get-marked-string)
("M" 1 1 left bs--get-modified-string)
("R" 2 2 left bs--get-readonly-string)
("Buffer" bs--get-name-length 10 left bs--get-name)
("" 1 1 left " ")
("Size" 8 8 right bs--get-size-string)
("" 1 1 left " ")
("Mode" 12 12 right bs--get-mode-name)
("" 2 2 left " ")
("File" 12 12 left bs--get-file-name)
("" 2 2 left " "))
"*List specifying the layout of a Buffer Selection Menu buffer.
Each entry specifies a column and is a list of the form of:
(HEADER MINIMUM-LENGTH MAXIMUM-LENGTH ALIGNMENT FUN-OR-STRING)
HEADER : string for header for first line or a function
which calculates column title.
MINIMUM-LENGTH : minimum width of column (number or name of function).
The function must return a positive integer.
MAXIMUM-LENGTH : maximum width of column (number or name of function)
(currently ignored)
ALIGNMENT : alignment of column: (`left' `right' `middle')
FUN-OR-STRING : Name of a function for calculating the value or
a string for a constant value.
The function gets as parameter the buffer we have started
buffer selection and the list of all buffers to show. The function must
return a string representing the columns value."
:group 'bs-appearence
:type '(repeat sexp))
(defvar bs--running-in-xemacs (string-match "XEmacs" (emacs-version))
"Non-nil when running under XEmacs.")
(defun bs--make-header-match-string ()
"Return a regexp matching the first line of a Buffer Selection Menu buffer."
(let ((res "^\\(")
(ele bs-attributes-list))
(while ele
(setq res (concat res (car (car ele)) " *"))
(setq ele (cdr ele)))
(concat res "$\\)")))
;;; Font-Lock-Settings
(defvar bs-mode-font-lock-keywords
(list ;; header in font-lock-type-face
(list (bs--make-header-match-string)
'(1 font-lock-type-face append) '(1 'bold append))
;; Buffername embedded by *
(list "^\\(.*\\*.*\\*.*\\)$"
1 (if bs--running-in-xemacs
;; problem in XEmacs with font-lock-constant-face
(if (facep 'font-lock-constant-face)
'font-lock-constant-face
'font-lock-comment-face)
'font-lock-constant-face))
;; Dired-Buffers
'("^..\\(.*Dired by .*\\)$" 1 font-lock-function-name-face)
;; the star for modified buffers
'("^.\\(\\*\\) +[^\\*]" 1 font-lock-comment-face))
"Default font lock expressions for Buffer Selection Menu.")
(defcustom bs-max-window-height 20
"*Maximal window height of Buffer Selection Menu."
:group 'bs-appearence
:type 'integer)
(defvar bs-dont-show-regexp nil
"Regular expression specifying which buffers not to show.
A buffer whose name matches this regular expression will not be
included in the buffer list.")
(defvar bs-must-show-regexp nil
"Regular expression for specifying buffers which must be shown.
A buffer whose name matches this regular expression will be
included in the buffer list.
Note that this variable is temporary: if the configuration is changed
it is reset to nil. Use `bs-must-always-show-regexp' to specify buffers
that must always be shown regardless of the configuration.")
(defcustom bs-must-always-show-regexp nil
"*Regular expression for specifying buffers to show always.
A buffer whose name matches this regular expression will
be shown regardless of current configuration of Buffer Selection Menu."
:group 'bs
:type '(choice (const :tag "Nothing at all" nil) regexp))
(defvar bs-dont-show-function nil
"Function for specifying buffers not to show.
The function gets one argument - the buffer to test. The function must
return a value different from nil to ignore the buffer in
Buffer Selection Menu.")
(defvar bs-must-show-function nil
"Function for specifying buffers which must be shown.
The function gets one argument - the buffer to test.")
(defvar bs-buffer-sort-function nil
"Sort function to sort the buffers that appear in Buffer Selection Menu.
The functions gets two arguments - the buffers to compare.")
(defcustom bs-maximal-buffer-name-column 45
"*Maximum column width for buffer names.
The column for buffer names has dynamic width. The width depends on
maximal and minimal length of names of buffers to show. The maximal
width is bounded by `bs-maximal-buffer-name-column'.
See also `bs-minimal-buffer-name-column'."
:group 'bs-appearence
:type 'integer)
(defcustom bs-minimal-buffer-name-column 15
"*Minimum column width for buffer names.
The column for buffer names has dynamic width. The width depends on
maximal and minimal length of names of buffers to show. The minimal
width is bounded by `bs-minimal-buffer-name-column'.
See also `bs-maximal-buffer-name-column'."
:group 'bs-appearence
:type 'integer)
(defconst bs-header-lines-length 2
"Number of lines for headers in Buffer Selection Menu.")
(defcustom bs-configurations
'(("all" nil nil nil nil nil)
("files" nil nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)
("files-and-scratch" "^\\*scratch\\*" nil nil bs-visits-non-file
bs-sort-buffer-interns-are-last)
("all-intern-last" nil nil nil nil bs-sort-buffer-interns-are-last))
"*List of all configurations you can use in the Buffer Selection Menu.
A configuration describes which buffers appear in Buffer Selection Menu
and describes the order of buffers. A configuration is a list with
six elements. The first element is a string and describes the configuration.
The following five elements represent the values for Buffer Selection Menu
configurations variables `bs-dont-show-regexp', `bs-dont-show-function',
`bs-must-show-regexp', `bs-must-show-function' and `bs-buffer-sort-function'.
By setting these variables you define a configuration."
:group 'bs-appearence
:type '(repeat sexp))
(defcustom bs-default-configuration "files"
"*Name of default configuration used by in the Buffer Selection Menu.
\\<bs-mode-map>
Will be changed using key \\[bs-select-next-configuration].
Must be a string used in `bs-configurations' for naming a configuration."
:group 'bs
:type 'string)
(defcustom bs-alternative-configuration "all"
"*Name of configuration used when calling `bs-show' with \
\\[universal-argument] as prefix key.
Must be a string used in `bs-configurations' for naming a configuration."
:group 'bs
:type 'string)
(defvar bs-current-configuration bs-default-configuration
"Name of current configuration.
Must be a string found in `bs-configurations' for naming a configuration.")
(defcustom bs-cycle-configuration-name nil
"*Name of configuration used when cycling through the buffer list.
A value of nil means to use current configuration `bs-default-configuration'.
Must be a string used in `bs-configurations' for naming a configuration."
:group 'bs
:type '(choice (const :tag "like current configuration" nil)
string))
(defcustom bs-string-show-always "+"
"*String added in column 1 indicating a buffer will always be shown."
:group 'bs-appearence
:type 'string)
(defcustom bs-string-show-never "-"
"*String added in column 1 indicating a buffer will never be shown."
:group 'bs-appearence
:type 'string)
(defcustom bs-string-current "."
"*String added in column 1 indicating the current buffer."
:group 'bs-appearence
:type 'string)
(defcustom bs-string-current-marked "#"
"*String added in column 1 indicating the current buffer when it is marked."
:group 'bs-appearence
:type 'string)
(defcustom bs-string-marked ">"
"*String added in column 1 indicating a marked buffer."
:group 'bs-appearence
:type 'string)
(defcustom bs-string-show-normally " "
"*String added in column 1 indicating a unmarked buffer."
:group 'bs-appearence
:type 'string)
(defvar bs--name-entry-length 20
"Maximum length of all displayed buffer names.
Used internally, only.")
;; ----------------------------------------------------------------------
;; Intern globals
;; ----------------------------------------------------------------------
(defvar bs-buffer-show-mark nil
"Flag for the current mode for showing this buffer.
A value of nil means buffer will be shown depending on the current on
current configuration.
A value of `never' means to never show the buffer.
A value of `always' means to show buffer regardless of the configuration.")
(make-variable-buffer-local 'bs-buffer-show-mark)
;; Make face named region (for XEmacs)
(unless (facep 'region)
(make-face 'region)
(set-face-background 'region "gray75"))
(defun bs--sort-by-name (b1 b2)
"Compare buffers B1 and B2 by buffer name."
(string< (buffer-name b1)
(buffer-name b2)))
(defun bs--sort-by-filename (b1 b2)
"Compare buffers B1 and B2 by file name."
(string< (or (buffer-file-name b1) "")
(or (buffer-file-name b2) "")))
(defun bs--sort-by-mode (b1 b2)
"Compare buffers B1 and B2 by mode name."
(save-excursion
(string< (progn (set-buffer b1) (format "%s" mode-name))
(progn (set-buffer b2) (format "%s" mode-name)))))
(defun bs--sort-by-size (b1 b2)
"Compare buffers B1 and B2 by buffer size."
(save-excursion
(< (progn (set-buffer b1) (buffer-size))
(progn (set-buffer b2) (buffer-size)))))
(defcustom bs-sort-functions
'(("by name" bs--sort-by-name "Buffer" region)
("by size" bs--sort-by-size "Size" region)
("by mode" bs--sort-by-mode "Mode" region)
("by filename" bs--sort-by-filename "File" region)
("by nothing" nil nil nil))
"*List of all possible sorting aspects for Buffer Selection Menu.
You can add a new entry with a call to `bs-define-sort-function'.
Each element is a list of four elements (NAME FUNCTION REGEXP-FOR-SORTING FACE)
NAME specifies the sort order defined by function FUNCTION.
FUNCTION nil means don't sort the buffer list. Otherwise the functions
must have two parameters - the buffers to compare.
REGEXP-FOR-SORTING is a regular expression which describes the
column title to highlight.
FACE is a face used to fontify the sorted column title. A value of nil means
don't highlight."
:group 'bs
:type '(repeat sexp))
(defun bs-define-sort-function (name fun &optional regexp-for-sorting face)
"Define a new function for buffer sorting in Buffer Selection Menu.
NAME specifies the sort order defined by function FUN.
A value of nil for FUN means don't sort the buffer list. Otherwise the
functions must have two parameters - the buffers to compare.
REGEXP-FOR-SORTING is a regular expression which describes the
column title to highlight.
FACE is a face used to fontify the sorted column title. A value of nil means
don't highlight.
The new sort aspect will be inserted into list `bs-sort-functions'."
(let ((tupel (assoc name bs-sort-functions)))
(if tupel
(setcdr tupel (list fun regexp-for-sorting face))
(setq bs-sort-functions
(cons (list name fun regexp-for-sorting face)
bs-sort-functions)))))
(defvar bs--current-sort-function nil
"Description of the current function for sorting the buffer list.
This is an element of `bs-sort-functions'.")
(defcustom bs-default-sort-name "by nothing"
"*Name of default sort behavior.
Must be \"by nothing\" or a string used in `bs-sort-functions' for
naming a sort behavior. Default is \"by nothing\" which means no sorting."
:group 'bs
:type 'string
:set (lambda (var-name value)
(set var-name value)
(setq bs--current-sort-function
(assoc value bs-sort-functions))))
(defvar bs--buffer-coming-from nil
"The buffer in which the user started the current Buffer Selection Menu.")
(defvar bs--show-all nil
"Flag whether showing all buffers regardless of current configuration.
Non nil means to show all buffers. Otherwise show buffers
defined by current configuration `bs-current-configuration'.")
(defvar bs--window-config-coming-from nil
"Window configuration before starting Buffer Selection Menu.")
(defvar bs--intern-show-never "^ \\|\\*buffer-selection\\*"
"Regular expression specifying which buffers never to show.
A buffer whose name matches this regular expression will never be
included in the buffer list.")
(defvar bs-current-list nil
"List of buffers shown in Buffer Selection Menu.
Used internally, only.")
(defvar bs--marked-buffers nil
"Currently marked buffers in Buffer Selection Menu.")
(defvar bs-mode-map ()
"Keymap of `bs-mode'.")
(if bs-mode-map
()
(setq bs-mode-map (make-sparse-keymap))
(define-key bs-mode-map " " 'bs-select)
(define-key bs-mode-map "f" 'bs-select)
(define-key bs-mode-map "v" 'bs-view)
(define-key bs-mode-map "!" 'bs-select-in-one-window)
(define-key bs-mode-map [mouse-2] 'bs-mouse-select) ;; for GNU EMACS
(define-key bs-mode-map [button2] 'bs-mouse-select) ;; for XEmacs
(define-key bs-mode-map "F" 'bs-select-other-frame)
(let ((key ?1))
(while (<= key ?9)
(define-key bs-mode-map (char-to-string key) 'digit-argument)
(setq key (1+ key))))
(define-key bs-mode-map "-" 'negative-argument)
(define-key bs-mode-map "\e-" 'negative-argument)
(define-key bs-mode-map "o" 'bs-select-other-window)
(define-key bs-mode-map "\C-o" 'bs-tmp-select-other-window)
;; for GNU EMACS
(define-key bs-mode-map [mouse-3] 'bs-mouse-select-other-frame)
;; for XEmacs
(define-key bs-mode-map [button3] 'bs-mouse-select-other-frame)
(define-key bs-mode-map [up] 'bs-up)
(define-key bs-mode-map "n" 'bs-down)
(define-key bs-mode-map "p" 'bs-up)
(define-key bs-mode-map [down] 'bs-down)
(define-key bs-mode-map "\C-m" 'bs-select)
(define-key bs-mode-map "b" 'bs-bury-buffer)
(define-key bs-mode-map "s" 'bs-save)
(define-key bs-mode-map "S" 'bs-show-sorted)
(define-key bs-mode-map "a" 'bs-toggle-show-all)
(define-key bs-mode-map "d" 'bs-delete)
(define-key bs-mode-map "\C-d" 'bs-delete-backward)
(define-key bs-mode-map "k" 'bs-delete)
(define-key bs-mode-map "g" 'bs-refresh)
(define-key bs-mode-map "C" 'bs-set-configuration-and-refresh)
(define-key bs-mode-map "c" 'bs-select-next-configuration)
(define-key bs-mode-map "q" 'bs-kill)
;; (define-key bs-mode-map "z" 'bs-kill)
(define-key bs-mode-map "\C-c\C-c" 'bs-kill)
(define-key bs-mode-map "\C-g" 'bs-abort)
(define-key bs-mode-map "\C-]" 'bs-abort)
(define-key bs-mode-map "%" 'bs-toggle-readonly)
(define-key bs-mode-map "~" 'bs-clear-modified)
(define-key bs-mode-map "M" 'bs-toggle-current-to-show)
(define-key bs-mode-map "+" 'bs-set-current-buffer-to-show-always)
;;(define-key bs-mode-map "-" 'bs-set-current-buffer-to-show-never)
(define-key bs-mode-map "t" 'bs-visit-tags-table)
(define-key bs-mode-map "m" 'bs-mark-current)
(define-key bs-mode-map "u" 'bs-unmark-current)
(define-key bs-mode-map ">" 'scroll-right)
(define-key bs-mode-map "<" 'scroll-left)
(define-key bs-mode-map "\e\e" nil)
(define-key bs-mode-map "\e\e\e" 'bs-kill)
(define-key bs-mode-map [escape escape escape] 'bs-kill)
(define-key bs-mode-map "?" 'bs-help))
;; ----------------------------------------------------------------------
;; Functions
;; ----------------------------------------------------------------------
(defun bs-buffer-list (&optional list sort-description)
"Return a list of buffers to be shown.
LIST is a list of buffers to test for appearence in Buffer Selection Menu.
The result list depends on the global variables `bs-dont-show-regexp',
`bs-must-show-regexp', `bs-dont-show-function', `bs-must-show-function'
and `bs-buffer-sort-function'.
If SORT-DESCRIPTION isn't nil the list will be sorted by
a special function. SORT-DESCRIPTION is an element of `bs-sort-functions'."
(setq sort-description (or sort-description bs--current-sort-function)
list (or list (buffer-list)))
(let ((result nil))
(while list
(let* ((buffername (buffer-name (car list)))
(int-show-never (string-match bs--intern-show-never buffername))
(ext-show-never (and bs-dont-show-regexp
(string-match bs-dont-show-regexp
buffername)))
(extern-must-show (or (and bs-must-always-show-regexp
(string-match bs-must-always-show-regexp
buffername))
(and bs-must-show-regexp
(string-match bs-must-show-regexp
buffername))))
(extern-show-never-from-fun (and bs-dont-show-function
(funcall bs-dont-show-function
(car list))))
(extern-must-show-from-fun (and bs-must-show-function
(funcall bs-must-show-function
(car list))))
(show-flag (save-excursion
(set-buffer (car list))
bs-buffer-show-mark)))
(if (or (eq show-flag 'always)
(and (or bs--show-all (not (eq show-flag 'never)))
(not int-show-never)
(or bs--show-all
extern-must-show
extern-must-show-from-fun
(and (not ext-show-never)
(not extern-show-never-from-fun)))))
(setq result (cons (car list)
result)))
(setq list (cdr list))))
(setq result (reverse result))
;; The current buffer which was the start point of bs should be an element
;; of result list, so that we can leave with space and be back in the
;; buffer we started bs-show.
(if (and bs--buffer-coming-from
(buffer-live-p bs--buffer-coming-from)
(not (memq bs--buffer-coming-from result)))
(setq result (cons bs--buffer-coming-from result)))
;; sorting
(if (and sort-description
(nth 1 sort-description))
(setq result (sort result (nth 1 sort-description)))
;; else standard sorting
(bs-buffer-sort result))))
(defun bs-buffer-sort (buffer-list)
"Sort buffers in BUFFER-LIST according to `bs-buffer-sort-function'."
(if bs-buffer-sort-function
(sort buffer-list bs-buffer-sort-function)
buffer-list))
(defun bs--redisplay (&optional keep-line-p sort-description)
"Redisplay whole Buffer Selection Menu.
If KEEP-LINE-P is non nil the point will stay on current line.
SORT-DESCRIPTION is an element of `bs-sort-functions'"
(let ((line (1+ (count-lines 1 (point)))))
(bs-show-in-buffer (bs-buffer-list nil sort-description))
(if keep-line-p
(goto-line line))
(beginning-of-line)))
(defun bs--goto-current-buffer ()
"Goto line which represents the current buffer;
actually the line which begins with character in `bs-string-current' or
`bs-string-current-marked'."
(let (point
(regexp (concat "^"
(regexp-quote bs-string-current)
"\\|^"
(regexp-quote bs-string-current-marked))))
(save-excursion
(goto-char (point-min))
(if (search-forward-regexp regexp nil t)
(setq point (- (point) 1))))
(if point
(goto-char point))))
(defun bs--current-config-message ()
"Return a string describing the current `bs-mode' configuration."
(if bs--show-all
"Show all buffers."
(format "Show buffer by configuration %S"