HideIfDef mode bug fixes and enhancements. This is #2 of 3 patches based
[bpt/emacs.git] / lisp / bs.el
index 2945f26..73b3684 100644 (file)
@@ -1,7 +1,6 @@
-;;; bs.el --- menu for selecting and displaying buffers
+;;; bs.el --- menu for selecting and displaying buffers -*- lexical-binding: t -*-
 
-;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
-;;   2007, 2008, 2009, 2010, 2011, 2012  Free Software Foundation, Inc.
+;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
 ;; Author: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
 ;; Maintainer: Olaf Sylvester <Olaf.Sylvester@netsurf.de>
 ;; Keywords: convenience
@@ -26,7 +25,7 @@
 ;; Version: 1.17
 ;; X-URL: http://www.geekware.de/software/emacs
 ;;
-;; The bs-package contains a main function bs-show for poping up a
+;; The bs-package contains a main function bs-show for popping 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.
 ;; |  % vc-hooks.el     43605  Emacs-Lisp    /usr/share/emacs/19.34/lisp$|
 ;; -----------------------------------------------------------------------
 
-;;; Quick Installation und Customization:
+;;; Quick Installation and Customization:
 
-;; Use
+;; To display the bs menu, do
 ;;   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
-
+;; To customize its behavior, do
+;;   M-x bs-customize
 
 ;;; More Commentary:
 
 
 ;;; Code:
 
-(eval-when-compile (require 'cl))
-
 ;; ----------------------------------------------------------------------
 ;; Globals for customization
 ;; ----------------------------------------------------------------------
@@ -343,15 +336,13 @@ Used internally, only.")
 ;; Internal globals
 ;; ----------------------------------------------------------------------
 
-(defvar bs-buffer-show-mark nil
+(defvar-local 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
 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)
@@ -623,7 +614,7 @@ Used from `window-size-change-functions'."
 (put 'bs-mode 'mode-class 'special)
 
 (define-derived-mode bs-mode nil "Buffer-Selection-Menu"
-  "Major mode for editing a subset of Emacs' buffers.
+  "Major mode for editing a subset of Emacs's buffers.
 \\<bs-mode-map>
 Aside from two header lines each line describes one buffer.
 Move to a line representing the buffer you want to edit and select
@@ -655,17 +646,14 @@ available Buffer Selection Menu configuration.
 to show always.
 \\[bs-visit-tags-table] -- call `visit-tags-table' on current line's buffer.
 \\[bs-help] -- display this help text."
-  (make-local-variable 'font-lock-defaults)
-  (make-local-variable 'font-lock-verbose)
-  (make-local-variable 'font-lock-global-modes)
   (buffer-disable-undo)
   (setq buffer-read-only t
        truncate-lines t
-       show-trailing-whitespace nil
-       font-lock-global-modes '(not bs-mode)
-       font-lock-defaults '(bs-mode-font-lock-keywords t)
-       font-lock-verbose nil)
-  (set (make-local-variable 'revert-buffer-function) 'bs-refresh)
+       show-trailing-whitespace nil)
+  (setq-local font-lock-defaults '(bs-mode-font-lock-keywords t))
+  (setq-local font-lock-verbose nil)
+  (setq-local font-lock-global-modes '(not bs-mode))
+  (setq-local revert-buffer-function 'bs-refresh)
   (add-hook 'window-size-change-functions 'bs--track-window-changes)
   (add-hook 'kill-buffer-hook 'bs--remove-hooks nil t)
   (add-hook 'change-major-mode-hook 'bs--remove-hooks nil t))
@@ -698,7 +686,7 @@ Refresh whole Buffer Selection Menu."
   (call-interactively 'bs-set-configuration)
   (bs--redisplay t))
 
-(defun bs-refresh (&rest ignored)
+(defun bs-refresh (&rest _ignored)
   "Refresh whole Buffer Selection Menu.
 Arguments are IGNORED (for `revert-buffer')."
   (interactive)
@@ -835,10 +823,10 @@ See `visit-tags-table'."
   (interactive)
   (let ((res
          (with-current-buffer (bs--current-buffer)
-           (setq bs-buffer-show-mark (case bs-buffer-show-mark
-                                       ((nil)   'never)
-                                       ((never) 'always)
-                                       (t       nil))))))
+           (setq bs-buffer-show-mark (pcase bs-buffer-show-mark
+                                       (`nil   'never)
+                                       (`never 'always)
+                                       (_       nil))))))
     (bs--update-current-line)
     (bs--set-window-height)
     (bs--show-config-message res)))
@@ -870,7 +858,7 @@ the status of buffer on current line."
 (defun bs--mark-unmark (count fun)
   "Call FUN on COUNT consecutive buffers of *buffer-selection*."
   (let ((dir (if (> count 0) 1 -1)))
-    (dotimes (i (abs count))
+    (dotimes (_i (abs count))
       (let ((buffer (bs--current-buffer)))
        (when buffer (funcall fun buffer))
        (bs--update-current-line)
@@ -969,7 +957,7 @@ Default is `bs--current-sort-function'."
 Uses function `toggle-read-only'."
   (interactive)
   (with-current-buffer (bs--current-buffer)
-    (toggle-read-only))
+    (read-only-mode 'toggle))
   (bs--update-current-line))
 
 (defun bs-clear-modified ()
@@ -981,7 +969,7 @@ Uses function `toggle-read-only'."
 
 (defun bs--nth-wrapper (count fun &rest args)
   "Call COUNT times function FUN with arguments ARGS."
-  (dotimes (i (or count 1))
+  (dotimes (_i (or count 1))
     (apply fun args)))
 
 (defun bs-up (arg)
@@ -1022,7 +1010,7 @@ A value of t means BUFFER belongs to no file.
 A value of nil means BUFFER belongs to a file."
   (not (buffer-file-name buffer)))
 
-(defun bs-sort-buffer-interns-are-last (b1 b2)
+(defun bs-sort-buffer-interns-are-last (_b1 b2)
   "Function for sorting internal buffers at the end of all buffers."
   (string-match-p "^\\*" (buffer-name b2)))
 
@@ -1088,7 +1076,7 @@ configuration."
                  bs-dont-show-regexp     (nth 3 list)
                  bs-dont-show-function   (nth 4 list)
                  bs-buffer-sort-function (nth 5 list))
-         ;; for backward compability
+         ;; for backward compatibility
          (funcall (cdr list)))
       ;; else
       (ding)
@@ -1152,10 +1140,10 @@ and move point to current buffer."
     (dolist (buffer list)
       (bs--insert-one-entry buffer)
       (insert "\n"))
-    (delete-backward-char 1)
+    (delete-char -1)
     (bs--set-window-height)
     (bs--goto-current-buffer)
-    (font-lock-fontify-buffer)
+    (font-lock-ensure)
     (bs-apply-sort-faces)
     (set-buffer-modified-p nil)))
 
@@ -1217,11 +1205,10 @@ by buffer configuration `bs-cycle-configuration-name'."
                                        bs--cycle-list)))
             (next (car tupel))
             (cycle-list (cdr tupel)))
-       (unless (window-dedicated-p (selected-window))
-         ;; We don't want the frame iconified if the only window in the frame
-         ;; happens to be dedicated; let's get the error from switch-to-buffer
-         (bury-buffer))
-       (switch-to-buffer next)
+        ;; We don't want the frame iconified if the only window in the frame
+        ;; happens to be dedicated.
+        (bury-buffer (current-buffer))
+       (switch-to-buffer next nil t)
        (setq bs--cycle-list (append (cdr cycle-list)
                                     (list (car cycle-list))))
        (bs-message-without-log "Next buffers: %s"
@@ -1250,7 +1237,7 @@ by buffer configuration `bs-cycle-configuration-name'."
                                            bs--cycle-list)))
             (prev-buffer (car tupel))
             (cycle-list (cdr tupel)))
-       (switch-to-buffer prev-buffer)
+       (switch-to-buffer prev-buffer nil t)
        (setq bs--cycle-list (append (last cycle-list)
                                     (reverse (cdr (reverse cycle-list)))))
        (bs-message-without-log "Previous buffers: %s"
@@ -1267,7 +1254,7 @@ or a string."
         fun)
        (t (apply fun args))))
 
-(defun bs--get-marked-string (start-buffer all-buffers)
+(defun bs--get-marked-string (start-buffer _all-buffers)
   "Return a string which describes whether current buffer is marked.
 START-BUFFER is the buffer where we started buffer selection.
 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu.
@@ -1292,25 +1279,25 @@ The result string is one of `bs-string-current', `bs-string-current-marked',
    (t
     bs-string-show-always)))
 
-(defun bs--get-modified-string (start-buffer all-buffers)
+(defun bs--get-modified-string (_start-buffer _all-buffers)
   "Return a string which describes whether current buffer is modified.
 START-BUFFER is the buffer where we started buffer selection.
 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
   (if (buffer-modified-p) "*" " "))
 
-(defun bs--get-readonly-string (start-buffer all-buffers)
+(defun bs--get-readonly-string (_start-buffer _all-buffers)
   "Return a string which describes whether current buffer is read only.
 START-BUFFER is the buffer where we started buffer selection.
 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
   (if buffer-read-only "%" " "))
 
-(defun bs--get-size-string (start-buffer all-buffers)
+(defun bs--get-size-string (_start-buffer _all-buffers)
   "Return a string which describes the size of current buffer.
 START-BUFFER is the buffer where we started buffer selection.
 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
   (int-to-string (buffer-size)))
 
-(defun bs--get-name (start-buffer all-buffers)
+(defun bs--get-name (_start-buffer _all-buffers)
   "Return name of current buffer for Buffer Selection Menu.
 The name of current buffer gets additional text properties
 for mouse highlighting.
@@ -1320,13 +1307,13 @@ ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
               'help-echo "mouse-2: select this buffer, mouse-3: select in other frame"
               'mouse-face 'highlight))
 
-(defun bs--get-mode-name (start-buffer all-buffers)
+(defun bs--get-mode-name (start-buffer _all-buffers)
   "Return the name of mode of current buffer for Buffer Selection Menu.
 START-BUFFER is the buffer where we started buffer selection.
 ALL-BUFFERS is the list of buffers appearing in Buffer Selection Menu."
   (format-mode-line mode-name nil nil start-buffer))
 
-(defun bs--get-file-name (start-buffer all-buffers)
+(defun bs--get-file-name (_start-buffer _all-buffers)
   "Return string for column 'File' in Buffer Selection Menu.
 This is the variable `buffer-file-name' of current buffer.
 If not visiting a file, `list-buffers-directory' is returned instead.
@@ -1419,24 +1406,24 @@ for buffer selection."
          (select-window active-window)
        (bs--restore-window-config)
        (setq bs--window-config-coming-from (current-window-configuration))
-       (when (> (window-height (selected-window)) 7)
-          (split-window-vertically)
-          (other-window 1)))
+       (when (> (window-height) 7)
+          ;; Errors would mess with the window configuration (bug#10882).
+          (ignore-errors (select-window (split-window-below)))))
       (bs-show-in-buffer liste)
       (bs-message-without-log "%s" (bs--current-config-message)))))
 
-(defun bs--configuration-name-for-prefix-arg (prefix-arg)
-  "Convert prefix argument PREFIX-ARG to a name of a buffer configuration.
-If PREFIX-ARG is nil return `bs-default-configuration'.
-If PREFIX-ARG is an integer return PREFIX-ARG element of `bs-configurations'.
+(defun bs--configuration-name-for-prefix-arg (prefix)
+  "Convert prefix argument PREFIX to a name of a buffer configuration.
+If PREFIX is nil return `bs-default-configuration'.
+If PREFIX is an integer return PREFIX element of `bs-configurations'.
 Otherwise return `bs-alternative-configuration'."
   (cond ;; usually activation
-   ((null prefix-arg)
+   ((null prefix)
     bs-default-configuration)
    ;; call with integer as prefix argument
-   ((integerp prefix-arg)
-    (if (and (< 0 prefix-arg) (<= prefix-arg (length bs-configurations)))
-       (car (nth (1- prefix-arg) bs-configurations))
+   ((integerp prefix)
+    (if (and (< 0 prefix) (<= prefix (length bs-configurations)))
+       (car (nth (1- prefix) bs-configurations))
       bs-default-configuration))
    ;; call by prefix argument C-u
    (t bs-alternative-configuration)))
@@ -1486,5 +1473,4 @@ name of buffer configuration."
 ;; Now provide feature bs
 (provide 'bs)
 
-;; arch-tag: c0d9ab34-bf06-4368-ae9d-af88878e6802
 ;;; bs.el ends here