comparison lisp/hexl.el @ 38917:7f8686d21c31

(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.
author Eli Zaretskii <eliz@gnu.org>
date Thu, 23 Aug 2001 10:54:55 +0000
parents a2845b455cbd
children c59671f19daa
comparison
equal deleted inserted replaced
38916:36b64873fb6b 38917:7f8686d21c31
613 (setq address hexl-max-address)) 613 (setq address hexl-max-address))
614 address))) 614 address)))
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.
618 Useful for inserting control characters. 618 Useful for inserting control characters and non-ASCII characters given their
619 You may also type up to 3 octal digits, to insert a character with that code" 619 numerical code.
620 (interactive "p") 620 You may also type octal digits, to insert a character with that code."
621 (hexl-insert-char (read-quoted-char) arg)) 621 (interactive "p")
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
625 ;;;###autoload 626 ;;;###autoload
626 (defun hexlify-buffer () 627 (defun hexlify-buffer ()
685 ch) 686 ch)
686 (if (or (< ch 32) (>= ch 127)) 687 (if (or (< ch 32) (>= ch 127))
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
694 Non-ASCII characters are first encoded with `buffer-file-coding-system',
695 and 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.
692 (interactive "p") 735 Interactively, with a numeric argument, insert this character that many times.
693 (hexl-insert-char last-command-char arg)) 736
737 Non-ASCII characters are first encoded with `buffer-file-coding-system',
738 and their encoded form is inserted byte by byte."
739 (interactive "p")
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
745 CH 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
700 (+ (* (/ address 16) 68) 751 (+ (* (/ address 16) 68)
701 11 752 11
723 (setq num (1- num))))) 774 (setq num (1- num)))))
724 775
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.
737 Embedded whitespace, dashes, and periods in the string are ignored." 788 Embedded whitespace, dashes, and periods in the string are ignored."
738 (interactive "sHex string: \np") 789 (interactive "sHex string: \np")
756 (hexl-insert-char (car chars) 1) 807 (hexl-insert-char (car chars) 1)
757 (setq chars (cdr chars)))) 808 (setq chars (cdr chars))))
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.
778 With prefix ARG, turn on following if and only if ARG is positive. 829 With prefix ARG, turn on following if and only if ARG is positive.
779 When following is enabled, the ASCII character corresponding to the 830 When following is enabled, the ASCII character corresponding to the
813 864
814 ;; startup stuff. 865 ;; startup stuff.
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)
822 (define-key hexl-mode-map [up] 'hexl-previous-line) 878 (define-key hexl-mode-map [up] 'hexl-previous-line)
823 (define-key hexl-mode-map [down] 'hexl-next-line) 879 (define-key hexl-mode-map [down] 'hexl-next-line)
842 (define-key hexl-mode-map "\C-f" 'hexl-forward-char) 898 (define-key hexl-mode-map "\C-f" 'hexl-forward-char)
843 899
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)
854 (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert) 907 (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert)
855 (define-key hexl-mode-map "\C-t" 'undefined) 908 (define-key hexl-mode-map "\C-t" 'undefined)
856 (define-key hexl-mode-map "\C-v" 'hexl-scroll-up) 909 (define-key hexl-mode-map "\C-v" 'hexl-scroll-up)
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)
868 (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page) 918 (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page)
869 (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short) 919 (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short)
891 (define-key hexl-mode-map "\ey" 'undefined) 941 (define-key hexl-mode-map "\ey" 'undefined)
892 (define-key hexl-mode-map "\ez" 'undefined) 942 (define-key hexl-mode-map "\ez" 'undefined)
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)
901 (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer) 955 (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer)
902 (define-key hexl-mode-map "\C-x\C-t" 'undefined)) 956 (define-key hexl-mode-map "\C-x\C-t" 'undefined))