Function: hexl-insert-multibyte-char

hexl-insert-multibyte-char is a byte-compiled function defined in hexl.el.gz.

Signature

(hexl-insert-multibyte-char CH NUM)

Documentation

Insert a possibly multibyte character CH NUM times.

Non-ASCII characters are first encoded with buffer-file-coding-system, and their encoded form is inserted byte by byte. Note that if the hexl buffer was produced by hexl-find-file, its coding-system is no-conversion.

Inserting non-ASCII characters requires caution: the buffer's coding-system should correspond to the encoding on disk, and multibyte characters should be inserted with cursor on the first byte of a multibyte sequence whose length is identical to the length of the multibyte sequence to be inserted, otherwise this could produce invalid multibyte sequences. Non-ASCII characters in ISO-2022 encodings should preferably inserted byte by byte, to avoid problems caused by the designation sequences before the actual characters.

Source Code

;; Defined in /usr/src/emacs/lisp/hexl.el.gz
(defun hexl-insert-multibyte-char (ch num)
  "Insert a possibly multibyte character CH NUM times.

Non-ASCII characters are first encoded with `buffer-file-coding-system',
and their encoded form is inserted byte by byte.  Note that if the
hexl buffer was produced by `hexl-find-file', its coding-system
is no-conversion.

Inserting non-ASCII characters requires caution: the buffer's
coding-system should correspond to the encoding on disk, and
multibyte characters should be inserted with cursor on the first
byte of a multibyte sequence whose length is identical to the
length of the multibyte sequence to be inserted, otherwise this
could produce invalid multibyte sequences.  Non-ASCII characters
in ISO-2022 encodings should preferably inserted byte by byte, to
avoid problems caused by the designation sequences before the
actual characters."
  (let ((charset (char-charset ch))
	(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-value 'buffer-file-coding-system)
		  buffer-file-coding-system)))
    (cond ((and (>= ch 0) (< ch 256)
                (coding-system-get coding :ascii-compatible-p))
	   (hexl-insert-char ch num))
	  ((eq charset 'unknown)
	   (error
	    "0x%x -- invalid character code; use \\[hexl-insert-hex-string]"
	    ch))
	  (t
           (let ((encoded (encode-coding-char ch coding))
                 (internal (char-to-string ch))
                 internal-hex)
             ;; If encode-coding-char returns nil, it means our character
             ;; cannot be safely encoded with buffer-file-coding-system.
             ;; In that case, we offer to insert the internal representation
             ;; of that character, byte by byte.
             (when (null encoded)
	       (setq internal (encode-coding-string internal 'utf-8-emacs)
                     internal-hex
                     (mapconcat (lambda (c) (format "%x" c))
                                internal " "))
	       (if (yes-or-no-p
		    (format-message
		     "Insert char 0x%x's internal representation \"%s\"? "
		     ch internal-hex))
		   (setq encoded internal)
		 (error
		  "Can't encode `0x%x' with this buffer's coding system; %s"
		  ch
		  (substitute-command-keys "try \\[hexl-insert-hex-string]"))))
	     (while (> num 0)
	       (mapc
                (lambda (c) (hexl-insert-char c 1)) encoded)
	       (setq num (1- num))))))))