]> code.delx.au - gnu-emacs/blobdiff - lisp/hexl.el
Declare and define in one step various mode maps.
[gnu-emacs] / lisp / hexl.el
index c0ec9aa6ed884f9db2f85ea440d7d47d50a32cde..ebc43e43d2509a642a7b61f71e75183f726761f6 100644 (file)
@@ -1,7 +1,7 @@
 ;;; hexl.el --- edit a file in a hex dump format using the hexl filter
 
 ;; Copyright (C) 1989, 1994, 1998, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 ;; Author: Keith Gabryelski <ag@wheaties.ai.mit.edu>
 ;; Maintainer: FSF
@@ -9,10 +9,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, 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
@@ -20,9 +20,7 @@
 ;; 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., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -44,6 +42,7 @@
 ;;; Code:
 
 (require 'eldoc)
+(eval-when-compile (require 'cl))
 
 ;;
 ;; vars here
@@ -98,7 +97,99 @@ Quoting cannot be used, so the arguments cannot themselves contain spaces."
 (defvar hexl-max-address 0
   "Maximum offset into hexl buffer.")
 
-(defvar hexl-mode-map nil)
+(defvar hexl-mode-map
+  (let ((map (make-keymap)))
+    ;; Make all self-inserting keys go through hexl-self-insert-command,
+    ;; because we need to convert them to unibyte characters before
+    ;; inserting them into the buffer.
+    (define-key map [remap self-insert-command] 'hexl-self-insert-command)
+
+    (define-key map "\C-m" 'hexl-self-insert-command)
+    (define-key map [left] 'hexl-backward-char)
+    (define-key map [right] 'hexl-forward-char)
+    (define-key map [up] 'hexl-previous-line)
+    (define-key map [down] 'hexl-next-line)
+    (define-key map [M-left] 'hexl-backward-short)
+    (define-key map [?\e left] 'hexl-backward-short)
+    (define-key map [M-right] 'hexl-forward-short)
+    (define-key map [?\e right] 'hexl-forward-short)
+    (define-key map [next] 'hexl-scroll-up)
+    (define-key map [prior] 'hexl-scroll-down)
+    (define-key map [home] 'hexl-beginning-of-line)
+    (define-key map [end] 'hexl-end-of-line)
+    (define-key map [C-home] 'hexl-beginning-of-buffer)
+    (define-key map [C-end] 'hexl-end-of-buffer)
+    (define-key map [deletechar] 'undefined)
+    (define-key map [deleteline] 'undefined)
+    (define-key map [insertline] 'undefined)
+    (define-key map [S-delete] 'undefined)
+    (define-key map "\177" 'undefined)
+
+    (define-key map "\C-a" 'hexl-beginning-of-line)
+    (define-key map "\C-b" 'hexl-backward-char)
+    (define-key map "\C-d" 'undefined)
+    (define-key map "\C-e" 'hexl-end-of-line)
+    (define-key map "\C-f" 'hexl-forward-char)
+
+    (if (not (memq (key-binding (char-to-string help-char))
+                  '(help-command ehelp-command)))
+       (define-key map (char-to-string help-char) 'undefined))
+
+    (define-key map "\C-k" 'undefined)
+    (define-key map "\C-n" 'hexl-next-line)
+    (define-key map "\C-o" 'undefined)
+    (define-key map "\C-p" 'hexl-previous-line)
+    (define-key map "\C-q" 'hexl-quoted-insert)
+    (define-key map "\C-t" 'undefined)
+    (define-key map "\C-v" 'hexl-scroll-up)
+    (define-key map "\C-w" 'undefined)
+    (define-key map "\C-y" 'undefined)
+
+    (fset 'hexl-ESC-prefix (copy-keymap 'ESC-prefix))
+    (define-key map "\e" 'hexl-ESC-prefix)
+    (define-key map "\e\C-a" 'hexl-beginning-of-512b-page)
+    (define-key map "\e\C-b" 'hexl-backward-short)
+    (define-key map "\e\C-d" 'hexl-insert-decimal-char)
+    (define-key map "\e\C-e" 'hexl-end-of-512b-page)
+    (define-key map "\e\C-f" 'hexl-forward-short)
+    (define-key map "\e\C-i" 'undefined)
+    (define-key map "\e\C-j" 'undefined)
+    (define-key map "\e\C-k" 'undefined)
+    (define-key map "\e\C-o" 'hexl-insert-octal-char)
+    (define-key map "\e\C-q" 'undefined)
+    (define-key map "\e\C-t" 'undefined)
+    (define-key map "\e\C-x" 'hexl-insert-hex-char)
+    (define-key map "\eb" 'hexl-backward-word)
+    (define-key map "\ec" 'undefined)
+    (define-key map "\ed" 'undefined)
+    (define-key map "\ef" 'hexl-forward-word)
+    (define-key map "\eg" 'hexl-goto-hex-address)
+    (define-key map "\ei" 'undefined)
+    (define-key map "\ej" 'hexl-goto-address)
+    (define-key map "\ek" 'undefined)
+    (define-key map "\el" 'undefined)
+    (define-key map "\eq" 'undefined)
+    (define-key map "\es" 'undefined)
+    (define-key map "\et" 'undefined)
+    (define-key map "\eu" 'undefined)
+    (define-key map "\ev" 'hexl-scroll-down)
+    (define-key map "\ey" 'undefined)
+    (define-key map "\ez" 'undefined)
+    (define-key map "\e<" 'hexl-beginning-of-buffer)
+    (define-key map "\e>" 'hexl-end-of-buffer)
+
+    (fset 'hexl-C-c-prefix (copy-keymap mode-specific-map))
+    (define-key map "\C-c" 'hexl-C-c-prefix)
+    (define-key map "\C-c\C-c" 'hexl-mode-exit)
+
+    (fset 'hexl-C-x-prefix (copy-keymap 'Control-X-prefix))
+    (define-key map "\C-x" 'hexl-C-x-prefix)
+    (define-key map "\C-x[" 'hexl-beginning-of-1k-page)
+    (define-key map "\C-x]" 'hexl-end-of-1k-page)
+    (define-key map "\C-x\C-p" 'undefined)
+    (define-key map "\C-x\C-s" 'hexl-save-buffer)
+    (define-key map "\C-x\C-t" 'undefined)
+    map))
 
 ;; Variable declarations for suppressing warnings from the byte-compiler.
 (defvar ruler-mode)
@@ -120,6 +211,7 @@ Quoting cannot be used, so the arguments cannot themselves contain spaces."
 (defvar hexl-mode-old-require-final-newline)
 (defvar hexl-mode-old-syntax-table)
 (defvar hexl-mode-old-font-lock-keywords)
+(defvar hexl-mode-old-eldoc-documentation-function)
 
 (defvar hexl-ascii-overlay nil
   "Overlay used to highlight ASCII element corresponding to current point.")
@@ -288,6 +380,10 @@ You can use \\[hexl-find-file] to visit a file in Hexl mode.
     (add-hook 'change-major-mode-hook 'hexl-maybe-dehexlify-buffer nil t)
 
     ;; Set a callback function for eldoc.
+    (make-local-variable 'hexl-mode-old-eldoc-documentation-function)
+    (setq hexl-mode-old-eldoc-documentation-function
+         (bound-and-true-p eldoc-documentation-function))
+
     (set (make-local-variable 'eldoc-documentation-function)
         'hexl-print-current-point-info)
     (eldoc-add-command-completions "hexl-")
@@ -362,8 +458,8 @@ and edit the file in `hexl-mode'."
    (list
     (let ((completion-ignored-extensions nil))
       (read-file-name "Filename: " nil nil 'ret-must-match))))
-  ;; Ignore the user's setting of default-major-mode.
-  (let ((default-major-mode 'fundamental-mode))
+  ;; Ignore the user's setting of default major-mode.
+  (letf (((default-value 'major-mode) 'fundamental-mode))
     (find-file-literally filename))
   (if (not (eq major-mode 'hexl-mode))
       (hexl-mode)))
@@ -405,6 +501,10 @@ With arg, don't unhexlify buffer."
   (when (boundp 'hexl-mode-old-hl-line-face)
     (setq hl-line-face hexl-mode-old-hl-line-face))
 
+  (when (boundp 'hexl-mode-old-eldoc-documentation-function)
+    (setq eldoc-documentation-function
+         hexl-mode-old-eldoc-documentation-function))
+
   (setq require-final-newline hexl-mode-old-require-final-newline)
   (setq mode-name hexl-mode-old-mode-name)
   (setq isearch-search-fun-function hexl-mode-old-isearch-search-fun-function)
@@ -440,7 +540,7 @@ Ask the user for confirmation."
             (if (>= current-column 41)
                 (- current-column 41)
               (/ (- current-column  (/ current-column 5)) 2))))
-    (when (interactive-p)
+    (when (called-interactively-p 'interactive)
       (message "Current address is %d/0x%08x" hexl-address hexl-address))
     hexl-address))
 
@@ -771,11 +871,11 @@ This discards the buffer's undo information."
 
 (defun hexl-printable-character (ch)
   "Return a displayable string for character CH."
-  (format "%c" (if hexl-iso
-                  (if (or (< ch 32) (and (>= ch 127) (< ch 160)))
+  (format "%c" (if (equal hexl-iso "")
+                  (if (or (< ch 32) (>= ch 127))
                       46
                     ch)
-                (if (or (< ch 32) (>= ch 127))
+                (if (or (< ch 32) (and (>= ch 127) (< ch 160)))
                     46
                   ch))))
 
@@ -788,7 +888,7 @@ and their encoded form is inserted byte by byte."
        (coding (if (or (null buffer-file-coding-system)
                        ;; coding-system-type equals t means undecided.
                        (eq (coding-system-type buffer-file-coding-system) t))
-                   default-buffer-file-coding-system
+                   (default-value 'buffer-file-coding-system)
                  buffer-file-coding-system)))
     (cond ((and (> ch 0) (< ch 256))
           (hexl-insert-char ch num))
@@ -828,7 +928,7 @@ Interactively, with a numeric argument, insert this character that many times.
 Non-ASCII characters are first encoded with `buffer-file-coding-system',
 and their encoded form is inserted byte by byte."
   (interactive "p")
-  (hexl-insert-multibyte-char last-command-char arg))
+  (hexl-insert-multibyte-char last-command-event arg))
 
 (defun hexl-insert-char (ch num)
   "Insert the character CH NUM times in a hexl buffer.
@@ -1009,100 +1109,6 @@ This function is assumed to be used as callback function for `hl-line-mode'."
 
 ;; startup stuff.
 
-(if hexl-mode-map
-    nil
-  (setq hexl-mode-map (make-keymap))
-  ;; Make all self-inserting keys go through hexl-self-insert-command,
-  ;; because we need to convert them to unibyte characters before
-  ;; inserting them into the buffer.
-  (define-key hexl-mode-map [remap self-insert-command] 'hexl-self-insert-command)
-
-  (define-key hexl-mode-map "\C-m" 'hexl-self-insert-command)
-  (define-key hexl-mode-map [left] 'hexl-backward-char)
-  (define-key hexl-mode-map [right] 'hexl-forward-char)
-  (define-key hexl-mode-map [up] 'hexl-previous-line)
-  (define-key hexl-mode-map [down] 'hexl-next-line)
-  (define-key hexl-mode-map [M-left] 'hexl-backward-short)
-  (define-key hexl-mode-map [?\e left] 'hexl-backward-short)
-  (define-key hexl-mode-map [M-right] 'hexl-forward-short)
-  (define-key hexl-mode-map [?\e right] 'hexl-forward-short)
-  (define-key hexl-mode-map [next] 'hexl-scroll-up)
-  (define-key hexl-mode-map [prior] 'hexl-scroll-down)
-  (define-key hexl-mode-map [home] 'hexl-beginning-of-line)
-  (define-key hexl-mode-map [end] 'hexl-end-of-line)
-  (define-key hexl-mode-map [C-home] 'hexl-beginning-of-buffer)
-  (define-key hexl-mode-map [C-end] 'hexl-end-of-buffer)
-  (define-key hexl-mode-map [deletechar] 'undefined)
-  (define-key hexl-mode-map [deleteline] 'undefined)
-  (define-key hexl-mode-map [insertline] 'undefined)
-  (define-key hexl-mode-map [S-delete] 'undefined)
-  (define-key hexl-mode-map "\177" 'undefined)
-
-  (define-key hexl-mode-map "\C-a" 'hexl-beginning-of-line)
-  (define-key hexl-mode-map "\C-b" 'hexl-backward-char)
-  (define-key hexl-mode-map "\C-d" 'undefined)
-  (define-key hexl-mode-map "\C-e" 'hexl-end-of-line)
-  (define-key hexl-mode-map "\C-f" 'hexl-forward-char)
-
-  (if (not (memq (key-binding (char-to-string help-char))
-                 '(help-command ehelp-command)))
-      (define-key hexl-mode-map (char-to-string help-char) 'undefined))
-
-  (define-key hexl-mode-map "\C-k" 'undefined)
-  (define-key hexl-mode-map "\C-n" 'hexl-next-line)
-  (define-key hexl-mode-map "\C-o" 'undefined)
-  (define-key hexl-mode-map "\C-p" 'hexl-previous-line)
-  (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert)
-  (define-key hexl-mode-map "\C-t" 'undefined)
-  (define-key hexl-mode-map "\C-v" 'hexl-scroll-up)
-  (define-key hexl-mode-map "\C-w" 'undefined)
-  (define-key hexl-mode-map "\C-y" 'undefined)
-
-  (fset 'hexl-ESC-prefix (copy-keymap 'ESC-prefix))
-  (define-key hexl-mode-map "\e" 'hexl-ESC-prefix)
-  (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page)
-  (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short)
-  (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char)
-  (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page)
-  (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short)
-  (define-key hexl-mode-map "\e\C-i" 'undefined)
-  (define-key hexl-mode-map "\e\C-j" 'undefined)
-  (define-key hexl-mode-map "\e\C-k" 'undefined)
-  (define-key hexl-mode-map "\e\C-o" 'hexl-insert-octal-char)
-  (define-key hexl-mode-map "\e\C-q" 'undefined)
-  (define-key hexl-mode-map "\e\C-t" 'undefined)
-  (define-key hexl-mode-map "\e\C-x" 'hexl-insert-hex-char)
-  (define-key hexl-mode-map "\eb" 'hexl-backward-word)
-  (define-key hexl-mode-map "\ec" 'undefined)
-  (define-key hexl-mode-map "\ed" 'undefined)
-  (define-key hexl-mode-map "\ef" 'hexl-forward-word)
-  (define-key hexl-mode-map "\eg" 'hexl-goto-hex-address)
-  (define-key hexl-mode-map "\ei" 'undefined)
-  (define-key hexl-mode-map "\ej" 'hexl-goto-address)
-  (define-key hexl-mode-map "\ek" 'undefined)
-  (define-key hexl-mode-map "\el" 'undefined)
-  (define-key hexl-mode-map "\eq" 'undefined)
-  (define-key hexl-mode-map "\es" 'undefined)
-  (define-key hexl-mode-map "\et" 'undefined)
-  (define-key hexl-mode-map "\eu" 'undefined)
-  (define-key hexl-mode-map "\ev" 'hexl-scroll-down)
-  (define-key hexl-mode-map "\ey" 'undefined)
-  (define-key hexl-mode-map "\ez" 'undefined)
-  (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer)
-  (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer)
-
-  (fset 'hexl-C-c-prefix (copy-keymap mode-specific-map))
-  (define-key hexl-mode-map "\C-c" 'hexl-C-c-prefix)
-  (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit)
-
-  (fset 'hexl-C-x-prefix (copy-keymap 'Control-X-prefix))
-  (define-key hexl-mode-map "\C-x" 'hexl-C-x-prefix)
-  (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page)
-  (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page)
-  (define-key hexl-mode-map "\C-x\C-p" 'undefined)
-  (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer)
-  (define-key hexl-mode-map "\C-x\C-t" 'undefined))
-
 (easy-menu-define hexl-menu hexl-mode-map "Hexl Mode menu"
   `("Hexl"
     :help "Hexl-specific Features"