emacs/lisp/international/rfc1843.el

130 lines
4 KiB
EmacsLisp
Raw Normal View History

;;; rfc1843.el --- HZ (rfc1843) decoding -*- lexical-binding:t -*-
2005-08-06 19:51:42 +00:00
;; Copyright (C) 1998-2020 Free Software Foundation, Inc.
2000-09-19 13:40:08 +00:00
;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
;; Keywords: news HZ HZ+ mail i18n
;; This file is part of GNU Emacs.
2000-09-19 13:40:08 +00:00
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
2000-09-19 13:40:08 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
2000-09-19 13:40:08 +00:00
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
2000-09-19 13:40:08 +00:00
;;; Commentary:
;; Test:
;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}")
;;; Code:
(eval-when-compile (require 'cl-lib))
2000-09-19 13:40:08 +00:00
(defvar rfc1843-word-regexp
"~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
(defvar rfc1843-word-regexp-strictly
"~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)")
(defvar rfc1843-hzp-word-regexp
"~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
2000-09-19 13:40:08 +00:00
(defvar rfc1843-hzp-word-regexp-strictly
"~\\({\\([\041-\167][\041-\176]\\)+\\|\
[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
2000-09-19 13:40:08 +00:00
(defcustom rfc1843-decode-loosely nil
"Loosely check HZ encoding if non-nil.
When it is set non-nil, only buffers or strings with strictly
HZ-encoded are decoded."
:type 'boolean
:group 'mime)
2000-09-19 13:40:08 +00:00
(defcustom rfc1843-decode-hzp t
"HZ+ decoding support if non-nil.
HZ+ specification (also known as HZP) is to provide a standardized
7-bit representation of mixed Big5, GB, and ASCII text for convenient
e-mail transmission, news posting, etc."
2000-09-19 13:40:08 +00:00
:type 'boolean
:group 'mime)
2000-09-19 13:40:08 +00:00
(defcustom rfc1843-newsgroups-regexp "chinese\\|hz"
"Regexp of newsgroups in which might be HZ encoded."
Use regexp type for regexps in defcustom declarations * lisp/calendar/diary-lib.el (diary-face-attrs): * lisp/cedet/semantic/db-ebrowse.el (semanticdb-ebrowse-file-match): * lisp/cedet/srecode/document.el (srecode-document-autocomment-common-nouns-abbrevs) (srecode-document-autocomment-function-alist) (srecode-document-autocomment-return-first-alist) (srecode-document-autocomment-return-last-alist) (srecode-document-autocomment-param-alist) (srecode-document-autocomment-param-type-alist): * lisp/desktop.el (desktop-clear-preserve-buffers): * lisp/elide-head.el (elide-head-headers-to-hide): * lisp/erc/erc-backend.el (erc-encoding-coding-alist): * lisp/erc/erc-ezbounce.el (erc-ezb-regexp): * lisp/files.el (auto-save-file-name-transforms): * lisp/gnus/deuglify.el (gnus-outlook-deuglify-attrib-cut-regexp) (gnus-outlook-deuglify-attrib-verb-regexp) (gnus-outlook-deuglify-attrib-end-regexp): * lisp/gnus/gnus-fun.el (gnus-x-face-omit-files, gnus-face-omit-files): * lisp/gnus/spam.el (spam-spamassassin-positive-spam-flag-header): * lisp/htmlfontify.el (hfy-src-doc-link-unstyle): * lisp/info-look.el (info-lookup-file-name-alist): * lisp/international/rfc1843.el (rfc1843-newsgroups-regexp): * lisp/mail/feedmail.el (feedmail-queue-slug-suspect-regexp): * lisp/mail/rmail-spam-filter.el (rsf-white-list, rsf-definitions-alist): * lisp/man.el (Man-name-local-regexp): * lisp/net/ange-ftp.el (ange-ftp-dumb-unix-host-regexp): * lisp/net/newst-backend.el (newsticker-auto-mark-filter-list): * lisp/net/rcirc.el (rcirc-authinfo, rcirc-coding-system-alist): * lisp/net/tramp-adb.el (tramp-adb-prompt): * lisp/org/org-agenda.el (org-agenda-hide-tags-regexp) (org-agenda-category-icon-alist): * lisp/org/org-protocol.el (org-protocol-data-separator): * lisp/org/org-table.el (org-table-number-regexp): * lisp/org/ox-latex.el (org-latex-known-warnings): * lisp/progmodes/bug-reference.el (bug-reference-bug-regexp): * lisp/progmodes/hideif.el (hide-ifdef-header-regexp): * lisp/progmodes/idlw-help.el (idlwave-help-doclib-name) (idlwave-help-doclib-keyword): * lisp/progmodes/idlwave.el (idlwave-no-change-comment): * lisp/progmodes/python.el (python-shell-prompt-input-regexps) (python-shell-prompt-output-regexps, python-shell-prompt-regexp) (python-shell-prompt-block-regexp, python-shell-prompt-output-regexp) (python-shell-prompt-pdb-regexp, python-shell-compilation-regexp-alist) (python-pdbtrack-stacktrace-info-regexp): * lisp/progmodes/sql.el (sql-send-terminator, sql-ansi-statement-starters): * lisp/speedbar.el (speedbar-directory-unshown-regexp) (speedbar-file-unshown-regexp): * lisp/textmodes/flyspell.el (flyspell-mark-duplications-exceptions) (flyspell-tex-command-regexp): * lisp/textmodes/paragraphs.el (sentence-end-base): * lisp/textmodes/tildify.el (tildify-pattern, tildify-space-pattern): * lisp/vc/ediff-init.el (ediff-metachars): * lisp/vc/vc-git.el (vc-git-root-log-format): * lisp/vc/vc-hg.el (vc-hg-root-log-format): * lisp/whitespace.el (whitespace-indentation-regexp) (whitespace-space-after-tab-regexp): * lisp/woman.el (woman-manpath-man-regexp) (woman-imenu-generic-expression): Use 'regexp' instead of 'string' as type for values that are regexps in defcustom declarations.
2019-12-21 18:52:06 +01:00
:type 'regexp
:group 'mime)
2000-09-19 13:40:08 +00:00
(defun rfc1843-decode-region (from to)
"Decode HZ in the region between FROM and TO."
(interactive "r")
(let (str firstc)
(save-excursion
(goto-char from)
(if (or rfc1843-decode-loosely
(re-search-forward (if rfc1843-decode-hzp
rfc1843-hzp-word-regexp-strictly
rfc1843-word-regexp-strictly) to t))
(save-restriction
(narrow-to-region from to)
(goto-char (point-min))
(while (re-search-forward (if rfc1843-decode-hzp
rfc1843-hzp-word-regexp
rfc1843-word-regexp) (point-max) t)
2003-02-04 13:24:35 +00:00
(setq str (buffer-substring-no-properties
2000-09-19 13:40:08 +00:00
(match-beginning 1)
(match-end 1)))
(setq firstc (aref str 0))
(insert (decode-coding-string
2000-09-19 13:40:08 +00:00
(rfc1843-decode
(prog1
(substring str 1)
(delete-region (match-beginning 0) (match-end 0)))
firstc)
(if (eq firstc ?{) 'cn-gb-2312 'cn-big5))))
(goto-char (point-min))
(while (search-forward "~" (point-max) t)
(cond ((eq (char-after) ?\n)
(delete-char -1)
(delete-char 1))
((eq (char-after) ?~)
(delete-char 1)))))))))
(defun rfc1843-decode-string (string)
"Decode HZ STRING and return the results."
(let ((m enable-multibyte-characters))
2000-09-19 13:40:08 +00:00
(with-temp-buffer
(when m
(set-buffer-multibyte 'to))
2000-09-19 13:40:08 +00:00
(insert string)
(inline
(rfc1843-decode-region (point-min) (point-max)))
(buffer-string))))
(defun rfc1843-decode (word &optional firstc)
"Decode HZ WORD and return it."
(let ((i -1) (s (substring word 0)) v)
(if (or (not firstc) (eq firstc ?{))
(while (< (cl-incf i) (length s))
2000-09-19 13:40:08 +00:00
(if (eq (setq v (aref s i)) ? ) nil
(aset s i (+ 128 v))))
(while (< (cl-incf i) (length s))
2000-09-19 13:40:08 +00:00
(if (eq (setq v (aref s i)) ? ) nil
(setq v (+ (* 94 v) (aref s (1+ i)) -3135))
(aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161)))
(setq v (% v 157))
(aset s (cl-incf i) (+ v (if (< v 63) 64 98))))))
2000-09-19 13:40:08 +00:00
s))
(provide 'rfc1843)
;;; rfc1843.el ends here