mule-util.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ;;; mule-util.el --- utility functions for multilingual environment (mule)
  2. ;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  6. ;; Registration Number H14PRO021
  7. ;; Copyright (C) 2003
  8. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  9. ;; Registration Number H13PRO009
  10. ;; Keywords: mule, multilingual
  11. ;; This file is part of GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;;; Code:
  24. ;;; String manipulations while paying attention to multibyte
  25. ;;; characters.
  26. ;;;###autoload
  27. (defun string-to-sequence (string type)
  28. "Convert STRING to a sequence of TYPE which contains characters in STRING.
  29. TYPE should be `list' or `vector'."
  30. ;;; (let ((len (length string))
  31. ;;; (i 0)
  32. ;;; val)
  33. (cond ((eq type 'list)
  34. ;; Applicable post-Emacs 20.2 and asymptotically ~10 times
  35. ;; faster than the code below:
  36. (append string nil))
  37. ;;; (setq val (make-list len 0))
  38. ;;; (let ((l val))
  39. ;;; (while (< i len)
  40. ;;; (setcar l (aref string i))
  41. ;;; (setq l (cdr l) i (1+ i))))))
  42. ((eq type 'vector)
  43. ;; As above.
  44. (vconcat string))
  45. ;;; (setq val (make-vector len 0))
  46. ;;; (while (< i len)
  47. ;;; (aset val i (aref string i))
  48. ;;; (setq i (1+ i))))
  49. (t
  50. (error "Invalid type: %s" type)))
  51. ;;; val)
  52. )
  53. ;;;###autoload
  54. (make-obsolete 'string-to-sequence
  55. "use `string-to-list' or `string-to-vector'."
  56. "22.1")
  57. ;;;###autoload
  58. (defsubst string-to-list (string)
  59. "Return a list of characters in STRING."
  60. (append string nil))
  61. ;;;###autoload
  62. (defsubst string-to-vector (string)
  63. "Return a vector of characters in STRING."
  64. (vconcat string))
  65. ;;;###autoload
  66. (defun store-substring (string idx obj)
  67. "Embed OBJ (string or character) at index IDX of STRING."
  68. (if (integerp obj)
  69. (aset string idx obj)
  70. (let ((len1 (length obj))
  71. (len2 (length string))
  72. (i 0))
  73. (while (< i len1)
  74. (aset string (+ idx i) (aref obj i))
  75. (setq i (1+ i)))))
  76. string)
  77. ;;;###autoload
  78. (defun truncate-string-to-width (str end-column
  79. &optional start-column padding ellipsis)
  80. "Truncate string STR to end at column END-COLUMN.
  81. The optional 3rd arg START-COLUMN, if non-nil, specifies the starting
  82. column; that means to return the characters occupying columns
  83. START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN
  84. are specified in terms of character display width in the current
  85. buffer; see also `char-width'.
  86. The optional 4th arg PADDING, if non-nil, specifies a padding
  87. character (which should have a display width of 1) to add at the end
  88. of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN
  89. comes in the middle of a character in STR. PADDING is also added at
  90. the beginning of the result if column START-COLUMN appears in the
  91. middle of a character in STR.
  92. If PADDING is nil, no padding is added in these cases, so
  93. the resulting string may be narrower than END-COLUMN.
  94. If ELLIPSIS is non-nil, it should be a string which will replace the
  95. end of STR (including any padding) if it extends beyond END-COLUMN,
  96. unless the display width of STR is equal to or less than the display
  97. width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS
  98. defaults to \"...\"."
  99. (or start-column
  100. (setq start-column 0))
  101. (when (and ellipsis (not (stringp ellipsis)))
  102. (setq ellipsis "..."))
  103. (let ((str-len (length str))
  104. (str-width (string-width str))
  105. (ellipsis-len (if ellipsis (length ellipsis) 0))
  106. (ellipsis-width (if ellipsis (string-width ellipsis) 0))
  107. (idx 0)
  108. (column 0)
  109. (head-padding "") (tail-padding "")
  110. ch last-column last-idx from-idx)
  111. (condition-case nil
  112. (while (< column start-column)
  113. (setq ch (aref str idx)
  114. column (+ column (char-width ch))
  115. idx (1+ idx)))
  116. (args-out-of-range (setq idx str-len)))
  117. (if (< column start-column)
  118. (if padding (make-string end-column padding) "")
  119. (when (and padding (> column start-column))
  120. (setq head-padding (make-string (- column start-column) padding)))
  121. (setq from-idx idx)
  122. (when (>= end-column column)
  123. (if (and (< end-column str-width)
  124. (> str-width ellipsis-width))
  125. (setq end-column (- end-column ellipsis-width))
  126. (setq ellipsis ""))
  127. (condition-case nil
  128. (while (< column end-column)
  129. (setq last-column column
  130. last-idx idx
  131. ch (aref str idx)
  132. column (+ column (char-width ch))
  133. idx (1+ idx)))
  134. (args-out-of-range (setq idx str-len)))
  135. (when (> column end-column)
  136. (setq column last-column
  137. idx last-idx))
  138. (when (and padding (< column end-column))
  139. (setq tail-padding (make-string (- end-column column) padding))))
  140. (concat head-padding (substring str from-idx idx)
  141. tail-padding ellipsis))))
  142. ;;; Test suite for truncate-string-to-width
  143. ;; (dolist (test '((("" 0) . "")
  144. ;; (("x" 1) . "x")
  145. ;; (("xy" 1) . "x")
  146. ;; (("xy" 2 1) . "y")
  147. ;; (("xy" 0) . "")
  148. ;; (("xy" 3) . "xy")
  149. ;; (("$AVP(B" 0) . "")
  150. ;; (("$AVP(B" 1) . "")
  151. ;; (("$AVP(B" 2) . "$AVP(B")
  152. ;; (("$AVP(B" 1 nil ? ) . " ")
  153. ;; (("$AVPND(B" 3 1 ? ) . " ")
  154. ;; (("x$AVP(Bx" 2) . "x")
  155. ;; (("x$AVP(Bx" 3) . "x$AVP(B")
  156. ;; (("x$AVP(Bx" 3) . "x$AVP(B")
  157. ;; (("x$AVP(Bx" 4 1) . "$AVP(Bx")
  158. ;; (("kor$(CGQ(Be$(C1[(Ban" 8 1 ? ) . "or$(CGQ(Be$(C1[(B")
  159. ;; (("kor$(CGQ(Be$(C1[(Ban" 7 2 ? ) . "r$(CGQ(Be ")
  160. ;; (("" 0 nil nil "...") . "")
  161. ;; (("x" 3 nil nil "...") . "x")
  162. ;; (("$AVP(B" 3 nil nil "...") . "$AVP(B")
  163. ;; (("foo" 3 nil nil "...") . "foo")
  164. ;; (("foo" 2 nil nil "...") . "fo") ;; XEmacs failure?
  165. ;; (("foobar" 6 0 nil "...") . "foobar")
  166. ;; (("foobarbaz" 6 nil nil "...") . "foo...")
  167. ;; (("foobarbaz" 7 2 nil "...") . "ob...")
  168. ;; (("foobarbaz" 9 3 nil "...") . "barbaz")
  169. ;; (("$A$3(Bh$A$s(Be$A$K(Bl$A$A(Bl$A$O(Bo" 15 1 ? t) . " h$A$s(Be$A$K(Bl$A$A(Bl$A$O(Bo")
  170. ;; (("$A$3(Bh$A$s(Be$A$K(Bl$A$A(Bl$A$O(Bo" 14 1 ? t) . " h$A$s(Be$A$K(Bl$A$A(B...")
  171. ;; (("x" 3 nil nil "$(Gemk#(B") . "x")
  172. ;; (("$AVP(B" 2 nil nil "$(Gemk#(B") . "$AVP(B")
  173. ;; (("$AVP(B" 1 nil ?x "$(Gemk#(B") . "x") ;; XEmacs error
  174. ;; (("$AVPND(B" 3 nil ? "$(Gemk#(B") . "$AVP(B ") ;; XEmacs error
  175. ;; (("foobarbaz" 4 nil nil "$(Gemk#(B") . "$(Gemk#(B")
  176. ;; (("foobarbaz" 5 nil nil "$(Gemk#(B") . "f$(Gemk#(B")
  177. ;; (("foobarbaz" 6 nil nil "$(Gemk#(B") . "fo$(Gemk#(B")
  178. ;; (("foobarbaz" 8 3 nil "$(Gemk#(B") . "b$(Gemk#(B")
  179. ;; (("$A$3(Bh$A$s(Be$A$K(Bl$A$A(Bl$A$O(Bo" 14 4 ?x "$AHU1>$(Gk#(B") . "xe$A$KHU1>$(Gk#(B")
  180. ;; (("$A$3(Bh$A$s(Be$A$K(Bl$A$A(Bl$A$O(Bo" 13 4 ?x "$AHU1>$(Gk#(B") . "xex$AHU1>$(Gk#(B")
  181. ;; ))
  182. ;; (let (ret)
  183. ;; (condition-case e
  184. ;; (setq ret (apply #'truncate-string-to-width (car test)))
  185. ;; (error (setq ret e)))
  186. ;; (unless (equal ret (cdr test))
  187. ;; (error "%s: expected %s, got %s"
  188. ;; (prin1-to-string (cons 'truncate-string-to-width (car test)))
  189. ;; (prin1-to-string (cdr test))
  190. ;; (if (consp ret)
  191. ;; (format "error: %s: %s" (car ret)
  192. ;; (prin1-to-string (cdr ret)))
  193. ;; (prin1-to-string ret))))))
  194. ;;; Nested alist handler. Nested alist is alist whose elements are
  195. ;;; also nested alist.
  196. ;;;###autoload
  197. (defsubst nested-alist-p (obj)
  198. "Return t if OBJ is a nested alist.
  199. Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is
  200. any Lisp object, and BRANCHES is a list of cons cells of the form
  201. \(KEY-ELEMENT . NESTED-ALIST).
  202. You can use a nested alist to store any Lisp object (ENTRY) for a key
  203. sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ
  204. can be a string, a vector, or a list."
  205. (and obj (listp obj) (listp (cdr obj))))
  206. ;;;###autoload
  207. (defun set-nested-alist (keyseq entry alist &optional len branches)
  208. "Set ENTRY for KEYSEQ in a nested alist ALIST.
  209. Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ
  210. are considered.
  211. Optional 5th argument BRANCHES if non-nil is branches for a keyseq
  212. longer than KEYSEQ.
  213. See the documentation of `nested-alist-p' for more detail."
  214. (or (nested-alist-p alist)
  215. (error "Invalid argument %s" alist))
  216. (let ((islist (listp keyseq))
  217. (len (or len (length keyseq)))
  218. (i 0)
  219. key-elt slot)
  220. (while (< i len)
  221. (if (null (nested-alist-p alist))
  222. (error "Keyseq %s is too long for this nested alist" keyseq))
  223. (setq key-elt (if islist (nth i keyseq) (aref keyseq i)))
  224. (setq slot (assoc key-elt (cdr alist)))
  225. (unless slot
  226. (setq slot (cons key-elt (list t)))
  227. (setcdr alist (cons slot (cdr alist))))
  228. (setq alist (cdr slot))
  229. (setq i (1+ i)))
  230. (setcar alist entry)
  231. (if branches
  232. (setcdr (last alist) branches))))
  233. ;;;###autoload
  234. (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
  235. "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
  236. Optional 3rd argument LEN specifies the length of KEYSEQ.
  237. Optional 4th argument START specifies index of the starting key.
  238. The returned value is normally a nested alist of which
  239. car part is the entry for KEYSEQ.
  240. If ALIST is not deep enough for KEYSEQ, return number which is
  241. how many key elements at the front of KEYSEQ it takes
  242. to reach a leaf in ALIST.
  243. Optional 5th argument NIL-FOR-TOO-LONG non-nil means return nil
  244. even if ALIST is not deep enough."
  245. (or (nested-alist-p alist)
  246. (error "Invalid argument %s" alist))
  247. (or len
  248. (setq len (length keyseq)))
  249. (let ((i (or start 0)))
  250. (if (catch 'lookup-nested-alist-tag
  251. (if (listp keyseq)
  252. (while (< i len)
  253. (if (setq alist (cdr (assoc (nth i keyseq) (cdr alist))))
  254. (setq i (1+ i))
  255. (throw 'lookup-nested-alist-tag t))))
  256. (while (< i len)
  257. (if (setq alist (cdr (assoc (aref keyseq i) (cdr alist))))
  258. (setq i (1+ i))
  259. (throw 'lookup-nested-alist-tag t))))
  260. ;; KEYSEQ is too long.
  261. (if nil-for-too-long nil i)
  262. alist)))
  263. ;; Coding system related functions.
  264. ;;;###autoload
  265. (defun coding-system-post-read-conversion (coding-system)
  266. "Return the value of CODING-SYSTEM's `post-read-conversion' property."
  267. (coding-system-get coding-system :post-read-conversion))
  268. ;;;###autoload
  269. (defun coding-system-pre-write-conversion (coding-system)
  270. "Return the value of CODING-SYSTEM's `pre-write-conversion' property."
  271. (coding-system-get coding-system :pre-write-conversion))
  272. ;;;###autoload
  273. (defun coding-system-translation-table-for-decode (coding-system)
  274. "Return the value of CODING-SYSTEM's `decode-translation-table' property."
  275. (coding-system-get coding-system :decode-translation-table))
  276. ;;;###autoload
  277. (defun coding-system-translation-table-for-encode (coding-system)
  278. "Return the value of CODING-SYSTEM's `encode-translation-table' property."
  279. (coding-system-get coding-system :encode-translation-table))
  280. ;;;###autoload
  281. (defmacro with-coding-priority (coding-systems &rest body)
  282. "Execute BODY like `progn' with CODING-SYSTEMS at the front of priority list.
  283. CODING-SYSTEMS is a list of coding systems. See `set-coding-system-priority'.
  284. This affects the implicit sorting of lists of coding systems returned by
  285. operations such as `find-coding-systems-region'."
  286. (let ((current (make-symbol "current")))
  287. `(let ((,current (coding-system-priority-list)))
  288. (apply #'set-coding-system-priority ,coding-systems)
  289. (unwind-protect
  290. (progn ,@body)
  291. (apply #'set-coding-system-priority ,current)))))
  292. ;;;###autoload(put 'with-coding-priority 'lisp-indent-function 1)
  293. (put 'with-coding-priority 'edebug-form-spec t)
  294. ;;;###autoload
  295. (defmacro detect-coding-with-priority (from to priority-list)
  296. "Detect a coding system of the text between FROM and TO with PRIORITY-LIST.
  297. PRIORITY-LIST is an alist of coding categories vs the corresponding
  298. coding systems ordered by priority."
  299. `(with-coding-priority (mapcar #'cdr ,priority-list)
  300. (detect-coding-region ,from ,to)))
  301. (make-obsolete 'detect-coding-with-priority
  302. "use `with-coding-priority' and `detect-coding-region'." "23.1")
  303. ;;;###autoload
  304. (defun detect-coding-with-language-environment (from to lang-env)
  305. "Detect a coding system for the text between FROM and TO with LANG-ENV.
  306. The detection takes into account the coding system priorities for the
  307. language environment LANG-ENV."
  308. (let ((coding-priority (get-language-info lang-env 'coding-priority)))
  309. (if coding-priority
  310. (with-coding-priority coding-priority
  311. (detect-coding-region from to)))))
  312. (declare-function internal-char-font "fontset.c" (position &optional ch))
  313. ;;;###autoload
  314. (defun char-displayable-p (char)
  315. "Return non-nil if we should be able to display CHAR.
  316. On a multi-font display, the test is only whether there is an
  317. appropriate font from the selected frame's fontset to display
  318. CHAR's charset in general. Since fonts may be specified on a
  319. per-character basis, this may not be accurate."
  320. (cond ((< char 128)
  321. ;; ASCII characters are always displayable.
  322. t)
  323. ((not enable-multibyte-characters)
  324. ;; Maybe there's a font for it, but we can't put it in the buffer.
  325. nil)
  326. ((display-multi-font-p)
  327. ;; On a window system, a character is displayable if we have
  328. ;; a font for that character in the default face of the
  329. ;; currently selected frame.
  330. (car (internal-char-font nil char)))
  331. (t
  332. ;; On a terminal, a character is displayable if the coding
  333. ;; system for the terminal can encode it.
  334. (let ((coding (terminal-coding-system)))
  335. (when coding
  336. (let ((cs-list (coding-system-get coding :charset-list)))
  337. (cond
  338. ((listp cs-list)
  339. (catch 'tag
  340. (mapc #'(lambda (charset)
  341. (if (encode-char char charset)
  342. (throw 'tag charset)))
  343. cs-list)
  344. nil))
  345. ((eq cs-list 'iso-2022)
  346. (catch 'tag2
  347. (mapc #'(lambda (charset)
  348. (if (and (plist-get (charset-plist charset)
  349. :iso-final-char)
  350. (encode-char char charset))
  351. (throw 'tag2 charset)))
  352. charset-list)
  353. nil))
  354. ((eq cs-list 'emacs-mule)
  355. (catch 'tag3
  356. (mapc #'(lambda (charset)
  357. (if (and (plist-get (charset-plist charset)
  358. :emacs-mule-id)
  359. (encode-char char charset))
  360. (throw 'tag3 charset)))
  361. charset-list)
  362. nil)))))))))
  363. (provide 'mule-util)
  364. ;; Local Variables:
  365. ;; coding: iso-2022-7bit
  366. ;; End:
  367. ;;; mule-util.el ends here