-;;; mule-util.el --- Utility functions for mulitilingual environment (mule)
+;;; mule-util.el --- utility functions for mulitilingual environment (mule)
;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
;; Licensed to the Free Software Foundation.
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
+;;; Commentary:
+
;;; Code:
;;; String manipulations while paying attention to multibyte
(defun string-to-sequence (string type)
"Convert STRING to a sequence of TYPE which contains characters in STRING.
TYPE should be `list' or `vector'."
- (let ((len (length string))
- (i 0)
- val)
+;;; (let ((len (length string))
+;;; (i 0)
+;;; val)
(cond ((eq type 'list)
- (setq val (make-list len 0))
- (let ((l val))
- (while (< i len)
- (setcar l (aref string i))
- (setq l (cdr l) i (1+ i)))))
+ ;; Applicable post-Emacs 20.2 and asymptotically ~10 times
+ ;; faster than the code below:
+ (append string nil))
+;;; (setq val (make-list len 0))
+;;; (let ((l val))
+;;; (while (< i len)
+;;; (setcar l (aref string i))
+;;; (setq l (cdr l) i (1+ i))))))
((eq type 'vector)
- (setq val (make-vector len 0))
- (while (< i len)
- (aset val i (aref string i))
- (setq i (1+ i))))
+ ;; As above.
+ (vconcat string))
+;;; (setq val (make-vector len 0))
+;;; (while (< i len)
+;;; (aset val i (aref string i))
+;;; (setq i (1+ i))))
(t
(error "Invalid type: %s" type)))
- val))
+;;; val)
+)
+
+;;;###autoload
+(make-obsolete 'string-to-sequence
+ "use `string-to-list' or `string-to-vector'."
+ "21.4")
;;;###autoload
(defsubst string-to-list (string)
"Return a list of characters in STRING."
- (string-to-sequence string 'list))
+ (append string nil))
;;;###autoload
(defsubst string-to-vector (string)
"Return a vector of characters in STRING."
- (string-to-sequence string 'vector))
+ (vconcat string))
;;;###autoload
(defun store-substring (string idx obj)
string)
;;;###autoload
-(defun truncate-string-to-width (str end-column &optional start-column padding)
+(defun truncate-string-to-width (str end-column
+ &optional start-column padding ellipsis)
"Truncate string STR to end at column END-COLUMN.
-The optional 2nd arg START-COLUMN, if non-nil, specifies
-the starting column; that means to return the characters occupying
-columns START-COLUMN ... END-COLUMN of STR.
-
-The optional 3rd arg PADDING, if non-nil, specifies a padding character
-to add at the end of the result if STR doesn't reach column END-COLUMN,
-or if END-COLUMN comes in the middle of a character in STR.
-PADDING is also added at the beginning of the result
-if column START-COLUMN appears in the middle of a character in STR.
+The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
+column; that means to return the characters occupying columns
+START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
+are specified in terms of character display width in the current
+buffer; see also `char-width'.
+
+The optional 4th arg PADDING, if non-nil, specifies a padding
+character (which should have a display width of 1) to add at the end
+of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
+comes in the middle of a character in STR. PADDING is also added at
+the beginning of the result if column START-COLUMN appears in the
+middle of a character in STR.
If PADDING is nil, no padding is added in these cases, so
-the resulting string may be narrower than END-COLUMN."
+the resulting string may be narrower than END-COLUMN.
+
+If ELLIPSIS is non-nil, it should be a string which will replace the
+end of STR (including any padding) if it extends beyond END-COLUMN,
+unless the display width of STR is equal to or less than the display
+width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
+defaults to \"...\"."
(or start-column
(setq start-column 0))
- (let ((len (length str))
+ (when (and ellipsis (not (stringp ellipsis)))
+ (setq ellipsis "..."))
+ (let ((str-len (length str))
+ (str-width (string-width str))
+ (ellipsis-len (if ellipsis (length ellipsis) 0))
+ (ellipsis-width (if ellipsis (string-width ellipsis) 0))
(idx 0)
(column 0)
(head-padding "") (tail-padding "")
(setq ch (aref str idx)
column (+ column (char-width ch))
idx (1+ idx)))
- (args-out-of-range (setq idx len)))
+ (args-out-of-range (setq idx str-len)))
(if (< column start-column)
(if padding (make-string end-column padding) "")
- (if (and padding (> column start-column))
- (setq head-padding (make-string (- column start-column) padding)))
+ (when (and padding (> column start-column))
+ (setq head-padding (make-string (- column start-column) padding)))
(setq from-idx idx)
- (if (< end-column column)
- (setq idx from-idx)
+ (when (>= end-column column)
+ (if (and (< end-column str-width)
+ (> str-width ellipsis-width))
+ (setq end-column (- end-column ellipsis-width))
+ (setq ellipsis ""))
(condition-case nil
(while (< column end-column)
(setq last-column column
ch (aref str idx)
column (+ column (char-width ch))
idx (1+ idx)))
- (args-out-of-range (setq idx len)))
- (if (> column end-column)
- (setq column last-column idx last-idx))
- (if (and padding (< column end-column))
- (setq tail-padding (make-string (- end-column column) padding))))
- (setq str (substring str from-idx idx))
- (if padding
- (concat head-padding str tail-padding)
- str))))
+ (args-out-of-range (setq idx str-len)))
+ (when (> column end-column)
+ (setq column last-column
+ idx last-idx))
+ (when (and padding (< column end-column))
+ (setq tail-padding (make-string (- end-column column) padding))))
+ (concat head-padding (substring str from-idx idx)
+ tail-padding ellipsis))))
+
+;;; Test suite for truncate-string-to-width
+;; (dolist (test '((("" 0) . "")
+;; (("x" 1) . "x")
+;; (("xy" 1) . "x")
+;; (("xy" 2 1) . "y")
+;; (("xy" 0) . "")
+;; (("xy" 3) . "xy")
+;; (("\e$AVP\e(B" 0) . "")
+;; (("\e$AVP\e(B" 1) . "")
+;; (("\e$AVP\e(B" 2) . "\e$AVP\e(B")
+;; (("\e$AVP\e(B" 1 nil ? ) . " ")
+;; (("\e$AVPND\e(B" 3 1 ? ) . " ")
+;; (("x\e$AVP\e(Bx" 2) . "x")
+;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
+;; (("x\e$AVP\e(Bx" 3) . "x\e$AVP\e(B")
+;; (("x\e$AVP\e(Bx" 4 1) . "\e$AVP\e(Bx")
+;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 8 1 ? ) . "or\e$(CGQ\e(Be\e$(C1[\e(B")
+;; (("kor\e$(CGQ\e(Be\e$(C1[\e(Ban" 7 2 ? ) . "r\e$(CGQ\e(Be ")
+;; (("" 0 nil nil "...") . "")
+;; (("x" 3 nil nil "...") . "x")
+;; (("\e$AVP\e(B" 3 nil nil "...") . "\e$AVP\e(B")
+;; (("foo" 3 nil nil "...") . "foo")
+;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure?
+;; (("foobar" 6 0 nil "...") . "foobar")
+;; (("foobarbaz" 6 nil nil "...") . "foo...")
+;; (("foobarbaz" 7 2 nil "...") . "ob...")
+;; (("foobarbaz" 9 3 nil "...") . "barbaz")
+;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 15 1 ? t) . " h\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo")
+;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 14 1 ? t) . " h\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(B...")
+;; (("x" 3 nil nil "\e$(0GnM$\e(B") . "x")
+;; (("\e$AVP\e(B" 2 nil nil "\e$(0GnM$\e(B") . "\e$AVP\e(B")
+;; (("\e$AVP\e(B" 1 nil ?x "\e$(0GnM$\e(B") . "x") ;; XEmacs error
+;; (("\e$AVPND\e(B" 3 nil ? "\e$(0GnM$\e(B") . "\e$AVP\e(B ") ;; XEmacs error
+;; (("foobarbaz" 4 nil nil "\e$(0GnM$\e(B") . "\e$(0GnM$\e(B")
+;; (("foobarbaz" 5 nil nil "\e$(0GnM$\e(B") . "f\e$(0GnM$\e(B")
+;; (("foobarbaz" 6 nil nil "\e$(0GnM$\e(B") . "fo\e$(0GnM$\e(B")
+;; (("foobarbaz" 8 3 nil "\e$(0GnM$\e(B") . "b\e$(0GnM$\e(B")
+;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 14 4 ?x "\e$BF|K\8l\e(B") . "xe\e$B$KF|K\8l\e(B")
+;; (("\e$B$3\e(Bh\e$B$s\e(Be\e$B$K\e(Bl\e$B$A\e(Bl\e$B$O\e(Bo" 13 4 ?x "\e$BF|K\8l\e(B") . "xex\e$BF|K\8l\e(B")
+;; ))
+;; (let (ret)
+;; (condition-case e
+;; (setq ret (apply #'truncate-string-to-width (car test)))
+;; (error (setq ret e)))
+;; (unless (equal ret (cdr test))
+;; (error "%s: expected %s, got %s"
+;; (prin1-to-string (cons 'truncate-string-to-width (car test)))
+;; (prin1-to-string (cdr test))
+;; (if (consp ret)
+;; (format "error: %s: %s" (car ret)
+;; (prin1-to-string (cdr ret)))
+;; (prin1-to-string ret))))))
;;; For backward compatibility ...
;;;###autoload
(defalias 'truncate-string 'truncate-string-to-width)
-(make-obsolete 'truncate-string 'truncate-string-to-width)
+
+;;;###autoload
+(make-obsolete 'truncate-string 'truncate-string-to-width "20.1")
\f
;;; Nested alist handler. Nested alist is alist whose elements are
;;; also nested alist.
Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
any Lisp object, and BRANCHES is a list of cons cells of the form
-(KEY-ELEMENT . NESTED-ALIST).
+\(KEY-ELEMENT . NESTED-ALIST).
You can use a nested alist to store any Lisp object (ENTRY) for a key
sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
(setq i (1+ i)))
(setcar alist entry)
(if branches
- (if (cdr alist)
- (error "Can't set branches for keyseq %s" keyseq)
- (setcdr alist branches)))))
+ (setcdr (last alist) branches))))
;;;###autoload
(defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil
even if ALIST is not deep enough."
(or (nested-alist-p alist)
- (error "invalid argument %s" alist))
+ (error "Invalid argument %s" alist))
(or len
(setq len (length keyseq)))
(let ((i (or start 0)))
;;;###autoload
(defun coding-system-eol-type-mnemonic (coding-system)
- "Return mnemonic letter of eol-type of CODING-SYSTEM."
- (let ((eol-type (coding-system-eol-type coding-system)))
- (cond ((vectorp eol-type) eol-mnemonic-undecided)
- ((eq eol-type 0) eol-mnemonic-unix)
- ((eq eol-type 1) eol-mnemonic-dos)
- ((eq eol-type 2) eol-mnemonic-mac)
- (t ?-))))
+ "Return the string indicating end-of-line format of CODING-SYSTEM."
+ (let* ((eol-type (coding-system-eol-type coding-system))
+ (val (cond ((vectorp eol-type) eol-mnemonic-undecided)
+ ((eq eol-type 0) eol-mnemonic-unix)
+ ((eq eol-type 1) eol-mnemonic-dos)
+ ((eq eol-type 2) eol-mnemonic-mac)
+ (t "-"))))
+ (if (stringp val)
+ val
+ (char-to-string val))))
;;;###autoload
(defun coding-system-post-read-conversion (coding-system)
- "Return the value of CODING-SYSTEM's post-read-conversion property."
+ "Return the value of CODING-SYSTEM's `post-read-conversion' property."
(coding-system-get coding-system 'post-read-conversion))
;;;###autoload
(defun coding-system-pre-write-conversion (coding-system)
- "Return the value of CODING-SYSTEM's pre-write-conversion property."
+ "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
(coding-system-get coding-system 'pre-write-conversion))
;;;###autoload
(defun coding-system-translation-table-for-decode (coding-system)
- "Return the value of CODING-SYSTEM's translation-table-for-decode property."
+ "Return the value of CODING-SYSTEM's `translation-table-for-decode' property."
(coding-system-get coding-system 'translation-table-for-decode))
;;;###autoload
(defun coding-system-translation-table-for-encode (coding-system)
- "Return the value of CODING-SYSTEM's translation-table-for-encode property."
+ "Return the value of CODING-SYSTEM's `translation-table-for-encode' property."
(coding-system-get coding-system 'translation-table-for-encode))
-(defun coding-system-lessp (x y)
- (cond ((eq x 'no-conversion) t)
- ((eq y 'no-conversion) nil)
- ((eq x 'emacs-mule) t)
- ((eq y 'emacs-mule) nil)
- ((eq x 'undecided) t)
- ((eq y 'undecided) nil)
- (t (let ((c1 (coding-system-mnemonic x))
- (c2 (coding-system-mnemonic y)))
- (or (< (downcase c1) (downcase c2))
- (and (not (> (downcase c1) (downcase c2)))
- (< c1 c2)))))))
-
-;;;###autoload
-(defun coding-system-list (&optional base-only)
- "Return a list of all existing coding systems.
-If optional arg BASE-ONLY is non-nil, only base coding systems are listed."
- (let* ((codings (sort (copy-sequence coding-system-list)
- 'coding-system-lessp))
- (tail (cons nil codings)))
- ;; Remove subsidiary coding systems (eol variants) and alias
- ;; coding systems (if necessary).
- (while (cdr tail)
- (let* ((coding (car (cdr tail)))
- (aliases (coding-system-get coding 'alias-coding-systems)))
- (if (or
- ;; CODING is an eol variant if not in ALIASES.
- (not (memq coding aliases))
- ;; CODING is an alias if it is not car of ALIASES.
- (and base-only (not (eq coding (car aliases)))))
- (setcdr tail (cdr (cdr tail)))
- (setq tail (cdr tail)))))
- codings))
-
;;;###autoload
(defun coding-system-equal (coding-system-1 coding-system-2)
"Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
"Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
PRIORITY-LIST is an alist of coding categories vs the corresponding
coding systems ordered by priority."
- `(let* ((prio-list ,priority-list)
- (coding-category-list coding-category-list)
- ,@(mapcar (function (lambda (x) (list x x))) coding-category-list))
- (mapcar (function (lambda (x) (set (car x) (cdr x))))
- prio-list)
- (set-coding-priority (mapcar (function (lambda (x) (car x))) prio-list))
- (detect-coding-region ,from ,to)))
+ `(unwind-protect
+ (let* ((prio-list ,priority-list)
+ (coding-category-list coding-category-list)
+ ,@(mapcar (function (lambda (x) (list x x)))
+ coding-category-list))
+ (mapc (function (lambda (x) (set (car x) (cdr x))))
+ prio-list)
+ (set-coding-priority (mapcar #'car prio-list))
+ (detect-coding-region ,from ,to))
+ ;; We must restore the internal database.
+ (set-coding-priority coding-category-list)
+ (update-coding-systems-internal)))
;;;###autoload
(defun detect-coding-with-language-environment (from to lang-env)
(detect-coding-region from to))))
\f
-;;; Composite character manipulations.
-
-;;;###autoload
-(defun compose-region (start end)
- "Compose all characters in the current region into one composite character.
-When called from a program, expects two arguments,
-positions (integers or markers) specifying the region."
- (interactive "r")
- (save-excursion
- (let ((str (buffer-substring start end)))
- (goto-char start)
- (insert (compose-string str))
- (delete-char (- end start)))))
+(provide 'mule-util)
-;;;###autoload
-(defun decompose-region (start end)
- "Decompose all composite characters in the current region.
-Composite characters are broken up into individual components.
-When called from a program, expects two arguments,
-positions (integers or markers) specifying the region."
- (interactive "r")
- (let ((buf (current-buffer))
- (cmpchar-head (char-to-string leading-code-composition)))
- (with-temp-buffer
- (insert-buffer-substring buf start end)
- (set-buffer-multibyte nil)
- (goto-char (point-min))
- (while (search-forward cmpchar-head nil t)
- (if (looking-at "[\240-\377][\240-\377][\240-\377][\240-\377]+")
- (let* ((from (1- (point)))
- (to (match-end 0))
- (str (string-as-multibyte (buffer-substring from to))))
- (if (cmpcharp (string-to-char str))
- (progn
- (delete-region from to)
- (insert (string-as-unibyte (decompose-string str))))
- (goto-char to)))))
- (set-buffer-multibyte t)
- (let ((tempbuf (current-buffer)))
- (save-excursion
- (set-buffer buf)
- (goto-char start)
- (delete-region start end)
- (insert-buffer tempbuf))))))
-
-;;;###autoload
-(defun decompose-string (string)
- "Decompose all composite characters in STRING."
- (let ((len (length string))
- (idx 0)
- (i 0)
- (str-list nil)
- ch)
- (while (< idx len)
- (setq ch (aref string idx))
- (if (>= ch min-composite-char)
- (progn
- (if (> idx i)
- (setq str-list (cons (substring string i idx) str-list)))
- (setq str-list (cons (decompose-composite-char ch) str-list))
- (setq i (1+ idx))))
- (setq idx (1+ idx)))
- (if (not str-list)
- (copy-sequence string)
- (if (> idx i)
- (setq str-list (cons (substring string i idx) str-list)))
- (apply 'concat (nreverse str-list)))))
-
-;;;###autoload
-(defconst reference-point-alist
- '((tl . 0) (tc . 1) (tr . 2)
- (ml . 3) (mc . 4) (mr . 5)
- (bl . 6) (bc . 7) (br . 8)
- (top-left . 0) (top-center . 1) (top-right . 2)
- (mid-left . 3) (mid-center . 4) (mid-right . 5)
- (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
- (0 . 0) (1 . 1) (2 . 2)
- (3 . 3) (4 . 4) (5 . 5)
- (6 . 6) (7 . 7) (8 . 8))
- "Alist of reference point symbols vs reference point codes.
-A reference point symbol is to be used to specify a composition rule
-while making a composite character by the function `compose-chars'
-(which see).
-
-Meanings of reference point codes are as follows:
-
- 0----1----2 <-- ascent 0:tl or top-left
- | | 1:tc or top-center
- | | 2:tr or top-right
- | | 3:ml or mid-left
- | 4 <--+---- center 4:mc or mid-center
- | | 5:mr or mid-right
---- 3 5 <-- baseline 6:bl or bottom-left
- | | 7:bc or bottom-center
- 6----7----8 <-- descent 8:br or bottom-right
-
-Reference point symbols are to be used to specify composition rule of
-the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
-is a reference point in the overall glyphs already composed, and
-NEW-REF-POINT is a reference point in the new glyph to be added.
-
-For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
-overall glyph is updated as follows:
-
- +-------+--+ <--- new ascent
- | | |
- | global| |
- | glyph | |
---- | | | <--- baseline (doesn't change)
- +----+--+--+
- | | new |
- | |glyph|
- +----+-----+ <--- new descent
-")
-
-;; Return a string for char CH to be embedded in multibyte form of
-;; composite character.
-;;;###autoload
-(defun compose-chars-component (ch)
- (if (< ch 128)
- (format "\240%c" (+ ch 128))
- (let ((str (string-as-unibyte (char-to-string ch))))
- (if (cmpcharp ch)
- (if (= (aref str 1) ?\xFF)
- (error "Can't compose a rule-based composition character")
- (substring str (if (= (aref str 1) ?\xFF) 2 1)))
- (aset str 0 (+ (aref str 0) ?\x20))
- str))))
-
-;; Return a string for composition rule RULE to be embedded in
-;; multibyte form of composite character.
-(defsubst compose-chars-rule (rule)
- (char-to-string (+ ?\xA0
- (* (cdr (assq (car rule) reference-point-alist)) 9)
- (cdr (assq (cdr rule) reference-point-alist)))))
-
-;;;###autoload
-(defun compose-chars (first-component &rest args)
- "Return one char string composed from the arguments.
-Each argument is a character (including a composite character)
-or a composition rule.
-A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
-See the documentation of `reference-point-alist' for more detail."
- (if (= (length args) 0)
- (char-to-string first-component)
- (let* ((with-rule (consp (car args)))
- (str (if with-rule (concat (vector leading-code-composition ?\xFF))
- (char-to-string leading-code-composition))))
- (if (and with-rule
- (cmpcharp first-component))
- (error "Can't compose an already composed character"))
- (setq str (concat str (compose-chars-component first-component)))
- (while args
- (if with-rule
- (progn
- (if (not (consp (car args)))
- (error "Invalid composition rule: %s" (car args)))
- (if (cmpcharp (car (cdr args)))
- (error "Can't compose an already composed character"))
- (setq str (concat str (compose-chars-rule (car args))
- (compose-chars-component (car (cdr args))))
- args (cdr (cdr args))))
- (setq str (concat str (compose-chars-component (car args)))
- args (cdr args))))
- (string-as-multibyte str))))
-
-;;;###autoload
-(defun decompose-composite-char (char &optional type with-composition-rule)
- "Convert composite character CHAR to a sequence of the components.
-Optional 1st arg TYPE specifies the type of sequence returned.
-It should be `string' (default), `list', or `vector'.
-Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
-sequence contains embedded composition rules if any. In this case, the
-order of elements in the sequence is the same as arguments for
-`compose-chars' to create CHAR.
-If TYPE is omitted or is `string', composition rules are omitted
-even if WITH-COMPOSITION-RULE is t."
- (or type
- (setq type 'string))
- (let* ((len (composite-char-component-count char))
- (i (1- len))
- l)
- (setq with-composition-rule (and with-composition-rule
- (not (eq type 'string))
- (composite-char-composition-rule-p char)))
- (while (> i 0)
- (setq l (cons (composite-char-component char i) l))
- (if with-composition-rule
- (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
- (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
- (setq i (1- i)))
- (setq l (cons (composite-char-component char 0) l))
- (cond ((eq type 'string)
- (apply 'string l))
- ((eq type 'list)
- l)
- (t ; i.e. TYPE is vector
- (vconcat l)))))
+;; Local Variables:
+;; coding: iso-2022-7bit
+;; End:
;;; mule-util.el ends here