aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2001-08-23 10:55:30 +0000
committerEli Zaretskii2001-08-23 10:55:30 +0000
commit02aec07b6ef1e4f20ce9577e99e644c6774dbd77 (patch)
treeea98f9abb628470ba02a81735a5992947501c99b
parentf704264eb9ebde6e2dcdde3d018c8808b9fbc17c (diff)
downloademacs-02aec07b6ef1e4f20ce9577e99e644c6774dbd77.tar.gz
emacs-02aec07b6ef1e4f20ce9577e99e644c6774dbd77.zip
(hexl-insert-multibyte-char) New function.
(hexl-quoted-insert, hexl-self-insert-command) (hexl-insert-hex-char, hexl-insert-decimal-char) (hexl-insert-octal-char): Call it instead of hexl-insert-char. Fix the doc strings accordingly. (hexl-insert-char): Reject characters whose code is above 255. Doc fix. (hexl-mode-map): Copy the global keymap instead of creating a sparse keymap, and bind all self-inserting characters to hexl-self-insert-command.
-rw-r--r--lisp/ChangeLog13
-rw-r--r--lisp/hexl.el102
2 files changed, 91 insertions, 24 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 0c1aa626f47..4dd91885d05 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,16 @@
12001-08-23 Eli Zaretskii <eliz@is.elta.co.il>
2
3 * hexl.el (hexl-insert-multibyte-char) New function.
4 (hexl-quoted-insert, hexl-self-insert-command)
5 (hexl-insert-hex-char, hexl-insert-decimal-char)
6 (hexl-insert-octal-char): Call it instead of hexl-insert-char.
7 Fix the doc strings accordingly.
8 (hexl-insert-char): Reject characters whose code is above 255.
9 Doc fix.
10 (hexl-mode-map): Copy the global keymap instead of creating a
11 sparse keymap, and bind all self-inserting characters to
12 hexl-self-insert-command.
13
12001-08-22 Stefan Monnier <monnier@cs.yale.edu> 142001-08-22 Stefan Monnier <monnier@cs.yale.edu>
2 15
3 * mail/sendmail.el (mail-mode): Use adaptive-fill-regexp. 16 * mail/sendmail.el (mail-mode): Use adaptive-fill-regexp.
diff --git a/lisp/hexl.el b/lisp/hexl.el
index ae3b88c61e2..ca08405cd10 100644
--- a/lisp/hexl.el
+++ b/lisp/hexl.el
@@ -615,10 +615,11 @@ If there's no byte at the target address, move to the first or last line."
615 615
616(defun hexl-quoted-insert (arg) 616(defun hexl-quoted-insert (arg)
617 "Read next input character and insert it. 617 "Read next input character and insert it.
618Useful for inserting control characters. 618Useful for inserting control characters and non-ASCII characters given their
619You may also type up to 3 octal digits, to insert a character with that code" 619numerical code.
620You may also type octal digits, to insert a character with that code."
620 (interactive "p") 621 (interactive "p")
621 (hexl-insert-char (read-quoted-char) arg)) 622 (hexl-insert-multibyte-char (read-quoted-char) arg))
622 623
623;00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789ABCDEF 624;00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789ABCDEF
624 625
@@ -687,13 +688,63 @@ This discards the buffer's undo information."
687 46 688 46
688 ch)))) 689 ch))))
689 690
691(defun hexl-insert-multibyte-char (ch num)
692 "Insert a possibly multibyte character CH NUM times.
693
694Non-ASCII characters are first encoded with `buffer-file-coding-system',
695and their encoded form is inserted byte by byte."
696 (let ((charset (char-charset ch))
697 (coding (if (or (null buffer-file-coding-system)
698 ;; coding-system-type equals t means undecided.
699 (eq (coding-system-type buffer-file-coding-system) t))
700 default-buffer-file-coding-system
701 buffer-file-coding-system)))
702 (cond ((and (> ch 0) (< ch 256))
703 (hexl-insert-char ch num))
704 ((eq charset 'unknown)
705 (error
706 "0x%x -- invalid character code; use \\[hexl-insert-hex-string]."
707 ch))
708 (t
709 (let ((encoded (encode-coding-char ch coding))
710 (internal (string-as-unibyte (char-to-string ch)))
711 internal-hex)
712 ;; If encode-coding-char returns nil, it means our character
713 ;; cannot be safely encoded with buffer-file-coding-system.
714 ;; In that case, we offer to insert the internal representation
715 ;; of that character, byte by byte.
716 (when (null encoded)
717 (setq internal-hex
718 (mapconcat (function (lambda (c) (format "%x" c)))
719 internal " "))
720 (if (yes-or-no-p
721 (format
722 "Insert char 0x%x's internal representation \"%s\"? "
723 ch internal-hex))
724 (setq encoded internal)
725 (error
726 "Can't encode `0x%x' with this buffer's coding system; try \\[hexl-insert-hex-string]."
727 ch)))
728 (while (> num 0)
729 (mapc
730 (function (lambda (c) (hexl-insert-char c 1))) encoded)
731 (setq num (1- num))))))))
732
690(defun hexl-self-insert-command (arg) 733(defun hexl-self-insert-command (arg)
691 "Insert this character." 734 "Insert this character.
735Interactively, with a numeric argument, insert this character that many times.
736
737Non-ASCII characters are first encoded with `buffer-file-coding-system',
738and their encoded form is inserted byte by byte."
692 (interactive "p") 739 (interactive "p")
693 (hexl-insert-char last-command-char arg)) 740 (hexl-insert-multibyte-char last-command-char arg))
694 741
695(defun hexl-insert-char (ch num) 742(defun hexl-insert-char (ch num)
696 "Insert a character in a hexl buffer." 743 "Insert the character CH NUM times in a hexl buffer.
744
745CH must be a unibyte character whose value is between 0 and 255."
746 (if (or (< ch 0) (> ch 255))
747 (error "Invalid character 0x%x -- must be in the range [0..255]."))
697 (let ((address (hexl-current-address t))) 748 (let ((address (hexl-current-address t)))
698 (while (> num 0) 749 (while (> num 0)
699 (let ((hex-position 750 (let ((hex-position
@@ -725,12 +776,12 @@ This discards the buffer's undo information."
725;; hex conversion 776;; hex conversion
726 777
727(defun hexl-insert-hex-char (arg) 778(defun hexl-insert-hex-char (arg)
728 "Insert a ASCII char ARG times at point for a given hexadecimal number." 779 "Insert a character given by its hexadecimal code ARG times at point."
729 (interactive "p") 780 (interactive "p")
730 (let ((num (hexl-hex-string-to-integer (read-string "Hex number: ")))) 781 (let ((num (hexl-hex-string-to-integer (read-string "Hex number: "))))
731 (if (or (> num 255) (< num 0)) 782 (if (< num 0)
732 (error "Hex number out of range") 783 (error "Hex number out of range")
733 (hexl-insert-char num arg)))) 784 (hexl-insert-multibyte-char num arg))))
734 785
735(defun hexl-insert-hex-string (str arg) 786(defun hexl-insert-hex-string (str arg)
736 "Insert hexadecimal string STR at point ARG times. 787 "Insert hexadecimal string STR at point ARG times.
@@ -758,20 +809,20 @@ Embedded whitespace, dashes, and periods in the string are ignored."
758 (setq arg (- arg 1))))) 809 (setq arg (- arg 1)))))
759 810
760(defun hexl-insert-decimal-char (arg) 811(defun hexl-insert-decimal-char (arg)
761 "Insert a ASCII char ARG times at point for a given decimal number." 812 "Insert a character given by its decimal code ARG times at point."
762 (interactive "p") 813 (interactive "p")
763 (let ((num (string-to-int (read-string "Decimal Number: ")))) 814 (let ((num (string-to-int (read-string "Decimal Number: "))))
764 (if (or (> num 255) (< num 0)) 815 (if (< num 0)
765 (error "Decimal number out of range") 816 (error "Decimal number out of range")
766 (hexl-insert-char num arg)))) 817 (hexl-insert-multibyte-char num arg))))
767 818
768(defun hexl-insert-octal-char (arg) 819(defun hexl-insert-octal-char (arg)
769 "Insert a ASCII char ARG times at point for a given octal number." 820 "Insert a character given by its octal code ARG times at point."
770 (interactive "p") 821 (interactive "p")
771 (let ((num (hexl-octal-string-to-integer (read-string "Octal Number: ")))) 822 (let ((num (hexl-octal-string-to-integer (read-string "Octal Number: "))))
772 (if (or (> num 255) (< num 0)) 823 (if (< num 0)
773 (error "Decimal number out of range") 824 (error "Decimal number out of range")
774 (hexl-insert-char num arg)))) 825 (hexl-insert-multibyte-char num arg))))
775 826
776(defun hexl-follow-ascii (&optional arg) 827(defun hexl-follow-ascii (&optional arg)
777 "Toggle following ASCII in Hexl buffers. 828 "Toggle following ASCII in Hexl buffers.
@@ -815,7 +866,12 @@ Customize the variable `hexl-follow-ascii' to disable this feature."
815 866
816(if hexl-mode-map 867(if hexl-mode-map
817 nil 868 nil
818 (setq hexl-mode-map (make-sparse-keymap)) 869 (setq hexl-mode-map (copy-keymap (current-global-map)))
870 ;; Make all self-inserting keys go through hexl-self-insert-command,
871 ;; because we need to convert them to unibyte characters before
872 ;; inserting them into the buffer.
873 (substitute-key-definition 'self-insert-command 'hexl-self-insert-command
874 hexl-mode-map)
819 875
820 (define-key hexl-mode-map [left] 'hexl-backward-char) 876 (define-key hexl-mode-map [left] 'hexl-backward-char)
821 (define-key hexl-mode-map [right] 'hexl-forward-char) 877 (define-key hexl-mode-map [right] 'hexl-forward-char)
@@ -844,10 +900,7 @@ Customize the variable `hexl-follow-ascii' to disable this feature."
844 (if (not (eq (key-binding (char-to-string help-char)) 'help-command)) 900 (if (not (eq (key-binding (char-to-string help-char)) 'help-command))
845 (define-key hexl-mode-map (char-to-string help-char) 'undefined)) 901 (define-key hexl-mode-map (char-to-string help-char) 'undefined))
846 902
847 (define-key hexl-mode-map "\C-i" 'hexl-self-insert-command)
848 (define-key hexl-mode-map "\C-j" 'hexl-self-insert-command)
849 (define-key hexl-mode-map "\C-k" 'undefined) 903 (define-key hexl-mode-map "\C-k" 'undefined)
850 (define-key hexl-mode-map "\C-m" 'hexl-self-insert-command)
851 (define-key hexl-mode-map "\C-n" 'hexl-next-line) 904 (define-key hexl-mode-map "\C-n" 'hexl-next-line)
852 (define-key hexl-mode-map "\C-o" 'undefined) 905 (define-key hexl-mode-map "\C-o" 'undefined)
853 (define-key hexl-mode-map "\C-p" 'hexl-previous-line) 906 (define-key hexl-mode-map "\C-p" 'hexl-previous-line)
@@ -857,11 +910,8 @@ Customize the variable `hexl-follow-ascii' to disable this feature."
857 (define-key hexl-mode-map "\C-w" 'undefined) 910 (define-key hexl-mode-map "\C-w" 'undefined)
858 (define-key hexl-mode-map "\C-y" 'undefined) 911 (define-key hexl-mode-map "\C-y" 'undefined)
859 912
860 (let ((ch 32)) 913 (fset 'hexl-ESC-prefix (copy-keymap 'ESC-prefix))
861 (while (< ch 127) 914 (define-key hexl-mode-map "\e" 'hexl-ESC-prefix)
862 (define-key hexl-mode-map (format "%c" ch) 'hexl-self-insert-command)
863 (setq ch (1+ ch))))
864
865 (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page) 915 (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page)
866 (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short) 916 (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short)
867 (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char) 917 (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char)
@@ -893,8 +943,12 @@ Customize the variable `hexl-follow-ascii' to disable this feature."
893 (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer) 943 (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer)
894 (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer) 944 (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer)
895 945
946 (fset 'hexl-C-c-prefix (copy-keymap mode-specific-map))
947 (define-key hexl-mode-map "\C-c" 'hexl-C-c-prefix)
896 (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit) 948 (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit)
897 949
950 (fset 'hexl-C-x-prefix (copy-keymap 'Control-X-prefix))
951 (define-key hexl-mode-map "\C-x" 'hexl-C-x-prefix)
898 (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page) 952 (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page)
899 (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page) 953 (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page)
900 (define-key hexl-mode-map "\C-x\C-p" 'undefined) 954 (define-key hexl-mode-map "\C-x\C-p" 'undefined)