| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
(defun string-to-sequence (string type) |
|---|
| 38 |
"Convert STRING to a sequence of TYPE which contains characters in STRING. |
|---|
| 39 |
TYPE should be `list' or `vector'." |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
(cond ((eq type 'list) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
(append string nil)) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
((eq type 'vector) |
|---|
| 53 |
|
|---|
| 54 |
(vconcat string)) |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
(t |
|---|
| 60 |
(error "Invalid type: %s" type))) |
|---|
| 61 |
|
|---|
| 62 |
) |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
(make-obsolete 'string-to-sequence |
|---|
| 66 |
"use `string-to-list' or `string-to-vector'." |
|---|
| 67 |
"22.1") |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
(defsubst string-to-list (string) |
|---|
| 71 |
"Return a list of characters in STRING." |
|---|
| 72 |
(append string nil)) |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
(defsubst string-to-vector (string) |
|---|
| 76 |
"Return a vector of characters in STRING." |
|---|
| 77 |
(vconcat string)) |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
(defun store-substring (string idx obj) |
|---|
| 81 |
"Embed OBJ (string or character) at index IDX of STRING." |
|---|
| 82 |
(if (integerp obj) |
|---|
| 83 |
(aset string idx obj) |
|---|
| 84 |
(let ((len1 (length obj)) |
|---|
| 85 |
(len2 (length string)) |
|---|
| 86 |
(i 0)) |
|---|
| 87 |
(while (< i len1) |
|---|
| 88 |
(aset string (+ idx i) (aref obj i)) |
|---|
| 89 |
(setq i (1+ i))))) |
|---|
| 90 |
string) |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
(defun truncate-string-to-width (str end-column |
|---|
| 94 |
&optional start-column padding ellipsis) |
|---|
| 95 |
"Truncate string STR to end at column END-COLUMN. |
|---|
| 96 |
The optional 3rd arg START-COLUMN, if non-nil, specifies the starting |
|---|
| 97 |
column; that means to return the characters occupying columns |
|---|
| 98 |
START-COLUMN ... END-COLUMN of STR. Both END-COLUMN and START-COLUMN |
|---|
| 99 |
are specified in terms of character display width in the current |
|---|
| 100 |
buffer; see also `char-width'. |
|---|
| 101 |
|
|---|
| 102 |
The optional 4th arg PADDING, if non-nil, specifies a padding |
|---|
| 103 |
character (which should have a display width of 1) to add at the end |
|---|
| 104 |
of the result if STR doesn't reach column END-COLUMN, or if END-COLUMN |
|---|
| 105 |
comes in the middle of a character in STR. PADDING is also added at |
|---|
| 106 |
the beginning of the result if column START-COLUMN appears in the |
|---|
| 107 |
middle of a character in STR. |
|---|
| 108 |
|
|---|
| 109 |
If PADDING is nil, no padding is added in these cases, so |
|---|
| 110 |
the resulting string may be narrower than END-COLUMN. |
|---|
| 111 |
|
|---|
| 112 |
If ELLIPSIS is non-nil, it should be a string which will replace the |
|---|
| 113 |
end of STR (including any padding) if it extends beyond END-COLUMN, |
|---|
| 114 |
unless the display width of STR is equal to or less than the display |
|---|
| 115 |
width of ELLIPSIS. If it is non-nil and not a string, then ELLIPSIS |
|---|
| 116 |
defaults to \"...\"." |
|---|
| 117 |
(or start-column |
|---|
| 118 |
(setq start-column 0)) |
|---|
| 119 |
(when (and ellipsis (not (stringp ellipsis))) |
|---|
| 120 |
(setq ellipsis "...")) |
|---|
| 121 |
(let ((str-len (length str)) |
|---|
| 122 |
(str-width (string-width str)) |
|---|
| 123 |
(ellipsis-len (if ellipsis (length ellipsis) 0)) |
|---|
| 124 |
(ellipsis-width (if ellipsis (string-width ellipsis) 0)) |
|---|
| 125 |
(idx 0) |
|---|
| 126 |
(column 0) |
|---|
| 127 |
(head-padding "") (tail-padding "") |
|---|
| 128 |
ch last-column last-idx from-idx) |
|---|
| 129 |
(condition-case nil |
|---|
| 130 |
(while (< column start-column) |
|---|
| 131 |
(setq ch (aref str idx) |
|---|
| 132 |
column (+ column (char-width ch)) |
|---|
| 133 |
idx (1+ idx))) |
|---|
| 134 |
(args-out-of-range (setq idx str-len))) |
|---|
| 135 |
(if (< column start-column) |
|---|
| 136 |
(if padding (make-string end-column padding) "") |
|---|
| 137 |
(when (and padding (> column start-column)) |
|---|
| 138 |
(setq head-padding (make-string (- column start-column) padding))) |
|---|
| 139 |
(setq from-idx idx) |
|---|
| 140 |
(when (>= end-column column) |
|---|
| 141 |
(if (and (< end-column str-width) |
|---|
| 142 |
(> str-width ellipsis-width)) |
|---|
| 143 |
(setq end-column (- end-column ellipsis-width)) |
|---|
| 144 |
(setq ellipsis "")) |
|---|
| 145 |
(condition-case nil |
|---|
| 146 |
(while (< column end-column) |
|---|
| 147 |
(setq last-column column |
|---|
| 148 |
last-idx idx |
|---|
| 149 |
ch (aref str idx) |
|---|
| 150 |
column (+ column (char-width ch)) |
|---|
| 151 |
idx (1+ idx))) |
|---|
| 152 |
(args-out-of-range (setq idx str-len))) |
|---|
| 153 |
(when (> column end-column) |
|---|
| 154 |
(setq column last-column |
|---|
| 155 |
idx last-idx)) |
|---|
| 156 |
(when (and padding (< column end-column)) |
|---|
| 157 |
(setq tail-padding (make-string (- end-column column) padding)))) |
|---|
| 158 |
(concat head-padding (substring str from-idx idx) |
|---|
| 159 |
tail-padding ellipsis)))) |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
(defsubst nested-alist-p (obj) |
|---|
| 220 |
"Return t if OBJ is a nested alist. |
|---|
| 221 |
|
|---|
| 222 |
Nested alist is a list of the form (ENTRY . BRANCHES), where ENTRY is |
|---|
| 223 |
any Lisp object, and BRANCHES is a list of cons cells of the form |
|---|
| 224 |
\(KEY-ELEMENT . NESTED-ALIST). |
|---|
| 225 |
|
|---|
| 226 |
You can use a nested alist to store any Lisp object (ENTRY) for a key |
|---|
| 227 |
sequence KEYSEQ, where KEYSEQ is a sequence of KEY-ELEMENT. KEYSEQ |
|---|
| 228 |
can be a string, a vector, or a list." |
|---|
| 229 |
(and obj (listp obj) (listp (cdr obj)))) |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
(defun set-nested-alist (keyseq entry alist &optional len branches) |
|---|
| 233 |
"Set ENTRY for KEYSEQ in a nested alist ALIST. |
|---|
| 234 |
Optional 4th arg LEN non-nil means the first LEN elements in KEYSEQ |
|---|
| 235 |
is considered. |
|---|
| 236 |
Optional argument BRANCHES if non-nil is branches for a keyseq |
|---|
| 237 |
longer than KEYSEQ. |
|---|
| 238 |
See the documentation of `nested-alist-p' for more detail." |
|---|
| 239 |
(or (nested-alist-p alist) |
|---|
| 240 |
(error "Invalid argument %s" alist)) |
|---|
| 241 |
(let ((islist (listp keyseq)) |
|---|
| 242 |
(len (or len (length keyseq))) |
|---|
| 243 |
(i 0) |
|---|
| 244 |
key-elt slot) |
|---|
| 245 |
(while (< i len) |
|---|
| 246 |
(if (null (nested-alist-p alist)) |
|---|
| 247 |
(error "Keyseq %s is too long for this nested alist" keyseq)) |
|---|
| 248 |
(setq key-elt (if islist (nth i keyseq) (aref keyseq i))) |
|---|
| 249 |
(setq slot (assoc key-elt (cdr alist))) |
|---|
| 250 |
(if (null slot) |
|---|
| 251 |
(progn |
|---|
| 252 |
(setq slot (cons key-elt (list t))) |
|---|
| 253 |
(setcdr alist (cons slot (cdr alist))))) |
|---|
| 254 |
(setq alist (cdr slot)) |
|---|
| 255 |
(setq i (1+ i))) |
|---|
| 256 |
(setcar alist entry) |
|---|
| 257 |
(if branches |
|---|
| 258 |
(setcdr (last alist) branches)))) |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
(defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long) |
|---|
| 262 |
"Look up key sequence KEYSEQ in nested alist ALIST. Return the definition. |
|---|
| 263 |
Optional 1st argument LEN specifies the length of KEYSEQ. |
|---|
| 264 |
Optional 2nd argument START specifies index of the starting key. |
|---|
| 265 |
The returned value is normally a nested alist of which |
|---|
| 266 |
car part is the entry for KEYSEQ. |
|---|
| 267 |
If ALIST is not deep enough for KEYSEQ, return number which is |
|---|
| 268 |
how many key elements at the front of KEYSEQ it takes |
|---|
| 269 |
to reach a leaf in ALIST. |
|---|
| 270 |
Optional 3rd argument NIL-FOR-TOO-LONG non-nil means return nil |
|---|
| 271 |
even if ALIST is not deep enough." |
|---|
| 272 |
(or (nested-alist-p alist) |
|---|
| 273 |
(error "Invalid argument %s" alist)) |
|---|
| 274 |
(or len |
|---|
| 275 |
(setq len (length keyseq))) |
|---|
| 276 |
(let ((i (or start 0))) |
|---|
| 277 |
(if (catch 'lookup-nested-alist-tag |
|---|
| 278 |
(if (listp keyseq) |
|---|
| 279 |
(while (< i len) |
|---|
| 280 |
(if (setq alist (cdr (assoc (nth i keyseq) (cdr alist)))) |
|---|
| 281 |
(setq i (1+ i)) |
|---|
| 282 |
(throw 'lookup-nested-alist-tag t)))) |
|---|
| 283 |
(while (< i len) |
|---|
| 284 |
(if (setq alist (cdr (assoc (aref keyseq i) (cdr alist)))) |
|---|
| 285 |
(setq i (1+ i)) |
|---|
| 286 |
(throw 'lookup-nested-alist-tag t)))) |
|---|
| 287 |
|
|---|
| 288 |
(if nil-for-too-long nil i) |
|---|
| 289 |
alist))) |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
(defun coding-system-post-read-conversion (coding-system) |
|---|
| 296 |
"Return the value of CODING-SYSTEM's `post-read-conversion' property." |
|---|
| 297 |
(coding-system-get coding-system 'post-read-conversion)) |
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
(defun coding-system-pre-write-conversion (coding-system) |
|---|
| 301 |
"Return the value of CODING-SYSTEM's `pre-write-conversion' property." |
|---|
| 302 |
(coding-system-get coding-system 'pre-write-conversion)) |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
(defun coding-system-translation-table-for-decode (coding-system) |
|---|
| 306 |
"Return the value of CODING-SYSTEM's `translation-table-for-decode' property." |
|---|
| 307 |
(coding-system-get coding-system 'translation-table-for-decode)) |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
(defun coding-system-translation-table-for-encode (coding-system) |
|---|
| 311 |
"Return the value of CODING-SYSTEM's `translation-table-for-encode' property." |
|---|
| 312 |
(coding-system-get coding-system 'translation-table-for-encode)) |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
(defmacro detect-coding-with-priority (from to priority-list) |
|---|
| 316 |
"Detect a coding system of the text between FROM and TO with PRIORITY-LIST. |
|---|
| 317 |
PRIORITY-LIST is an alist of coding categories vs the corresponding |
|---|
| 318 |
coding systems ordered by priority." |
|---|
| 319 |
`(unwind-protect |
|---|
| 320 |
(let* ((prio-list ,priority-list) |
|---|
| 321 |
(coding-category-list coding-category-list) |
|---|
| 322 |
,@(mapcar (function (lambda (x) (list x x))) |
|---|
| 323 |
coding-category-list)) |
|---|
| 324 |
(mapc (function (lambda (x) (set (car x) (cdr x)))) |
|---|
| 325 |
prio-list) |
|---|
| 326 |
(set-coding-priority (mapcar #'car prio-list)) |
|---|
| 327 |
|
|---|
| 328 |
(update-coding-systems-internal) |
|---|
| 329 |
(detect-coding-region ,from ,to)) |
|---|
| 330 |
|
|---|
| 331 |
(set-coding-priority coding-category-list) |
|---|
| 332 |
(update-coding-systems-internal))) |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
(defun detect-coding-with-language-environment (from to lang-env) |
|---|
| 336 |
"Detect a coding system of the text between FROM and TO with LANG-ENV. |
|---|
| 337 |
The detection takes into account the coding system priorities for the |
|---|
| 338 |
language environment LANG-ENV." |
|---|
| 339 |
(let ((coding-priority (get-language-info lang-env 'coding-priority))) |
|---|
| 340 |
(if coding-priority |
|---|
| 341 |
(detect-coding-with-priority |
|---|
| 342 |
from to |
|---|
| 343 |
(mapcar (function (lambda (x) |
|---|
| 344 |
(cons (coding-system-get x 'coding-category) x))) |
|---|
| 345 |
coding-priority)) |
|---|
| 346 |
(detect-coding-region from to)))) |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
(defun char-displayable-p (char) |
|---|
| 350 |
"Return non-nil if we should be able to display CHAR. |
|---|
| 351 |
On a multi-font display, the test is only whether there is an |
|---|
| 352 |
appropriate font from the selected frame's fontset to display CHAR's |
|---|
| 353 |
charset in general. Since fonts may be specified on a per-character |
|---|
| 354 |
basis, this may not be accurate." |
|---|
| 355 |
(cond ((< char 256) |
|---|
| 356 |
|
|---|
| 357 |
t) |
|---|
| 358 |
((not enable-multibyte-characters) |
|---|
| 359 |
|
|---|
| 360 |
nil) |
|---|
| 361 |
((display-multi-font-p) |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
(car (internal-char-font nil char))) |
|---|
| 366 |
(t |
|---|
| 367 |
(let ((coding (terminal-coding-system))) |
|---|
| 368 |
(if coding |
|---|
| 369 |
(let ((safe-chars (coding-system-get coding 'safe-chars)) |
|---|
| 370 |
(safe-charsets (coding-system-get coding 'safe-charsets))) |
|---|
| 371 |
(or (and safe-chars |
|---|
| 372 |
(aref safe-chars char)) |
|---|
| 373 |
(and safe-charsets |
|---|
| 374 |
(memq (char-charset char) safe-charsets))))))))) |
|---|
| 375 |
|
|---|
| 376 |
(provide 'mule-util) |
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|