Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Fixer le coding system fourni par une chaine

2 réponses
Avatar
Vincent Lefevre
Bonjour,

J'aimerais fixer le coding system que j'ai sous forme de chaîne (e.g.
"iso-8859-1", "UTF-8", etc., la chaîne venant de la valeur de charset
dans un mail). C'est pour utiliser avec modify-coding-system-alist où
le 2e argument est un symbole de fonction; il faut donc que je renvoie
un symbole correspondant au coding system.

J'ai vu que po-compat.el utilise une liste po-content-type-charset-alist
qui donne diverses correspondances chaîne -> symbole. Idem pour
latexenc.el (latex-inputenc-coding-alist). N'existe-t-il pas une
telle liste en standard?

Pour info, voilà ce que j'ai actuellement. Pour le moment, la chaîne est
simplement affichée avec "message". Si quelqu'un a des améliorations (je
ne connais pas bien toutes les possibilités d'Emacs), je suis preneur.

(defun mutt-find-file-coding-system (arg-list)
"\
Determine the coding system of a mail file. Use the current locale if the
file doesn't declare a charset (in practice, when composing a mail message
instead of editing one). This is a heuristic."
(if (eq (car arg-list) 'insert-file-contents)
(let ((case-fold-search t))
(save-excursion
(goto-char (point-min))
(cond
((looking-at "^From:") ;; Composed mail (new mail or reply).
locale-coding-system)
((and (search-forward-regexp
"^\\(.+\n\\)*Content-Transfer-Encoding: 8bit"
nil t)
(progn
(goto-char (point-min))
(search-forward-regexp
"^\\(.+\n\\)*Content-Type:.*charset=\"?\\([-0-9a-z]*\\)"
nil t)))
(message (match-string 2)))
(t 'undecided-unix))))
'undecided-unix)
)

(modify-coding-system-alist 'file "/mutt-.*-[0-9]+-[0-9]+-[0-9]+\\'"
'mutt-find-file-coding-system)

--
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)

2 réponses

Avatar
Matthieu Moy
Vincent Lefevre <vincent+ writes:

le 2e argument est un symbole de fonction; il faut donc que je renvoie
un symbole correspondant au coding system.



(eq (intern "foo") 'foo) renvoie 't.

Ça aide ?

--
Matthieu
Avatar
Vincent Lefevre
Dans l'article ,
Matthieu Moy écrit:

(eq (intern "foo") 'foo) renvoie 't.

Ça aide ?



Merci. En fait les deux fonctions que je citais l'utilisent dans
certains cas, mais je n'avais pas fait attention, comme je ne
connaissais pas... Maintenant ça fonctionne bien:

(defun mutt-find-file-coding-system (arg-list)
"
Determine the coding system of a mail file. Use the current locale if the
file doesn't declare a charset (in practice, when composing a mail message
instead of editing one). This is a heuristic."
(if (eq (car arg-list) 'insert-file-contents)
(let ((case-fold-search t))
(save-excursion
(goto-char (point-min))
(cond
((looking-at "^From:") ;; Composed mail (new mail or reply).
locale-coding-system)
((and (search-forward-regexp
"^(.+n)*Content-Transfer-Encoding: 8bit"
nil t)
(progn
(goto-char (point-min))
(search-forward-regexp
"^(.+n)*Content-Type:.*charset="?([-0-9a-z]*)"
nil t)))
(let ((charset (intern (downcase (match-string 2)))))
(if (memq charset (coding-system-list))
charset
'undecided-unix)))
(t 'undecided-unix))))
'undecided-unix)
)

(modify-coding-system-alist 'file "/mutt-.*-[0-9]+-[0-9]+-[0-9]+'"
'mutt-find-file-coding-system)

--
Vincent Lefèvre - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)