ad15348e1032f42b6435b67d9484b0eb8bdde92a
[riece] / lisp / riece-mcat.el
1 ;;; riece-mcat.el --- message catalog
2 ;; Copyright (C) 2007 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5
6 ;; This file is part of Riece.
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
22
23 ;;; Code:
24
25 (require 'pp)
26
27 (defun riece-mcat (string)
28   (let ((feature (get-language-info current-language-environment
29                                     'riece-mcat-feature)))
30     (if feature
31         (progn
32           (require feature)
33           (or (cdr (assoc string
34                           (symbol-value
35                            (intern (concat (symbol-name feature) "-alist")))))
36               string))
37       string)))
38
39 (defun riece-mcat-extract-from-form (form)
40   (if (and form (listp form) (listp (cdr form)))
41       (if (eq (car form) 'riece-mcat)
42           (cdr form)
43         (delq nil (apply #'nconc
44                          (mapcar #'riece-mcat-extract-from-form form))))))
45
46 (defun riece-mcat-extract (files alist)
47   (save-excursion
48     (let (message-list)
49       (while files
50         (with-temp-buffer
51           (insert-file-contents (car files))
52           (goto-char (point-min))
53           (while (progn
54                    (while (progn (skip-chars-forward " \t\n\f")
55                                  (looking-at ";"))
56                      (forward-line 1))
57                    (not (eobp)))
58             (setq message-list
59                   (nconc message-list
60                          (riece-mcat-extract-from-form
61                           (read (current-buffer)))))))
62         (setq files (cdr files)))
63       (setq message-list (sort message-list #'string-lessp))
64       (while message-list
65         (if (equal (car message-list)
66                    (nth 1 message-list))
67             (setq message-list (nthcdr 2 message-list))
68           (unless (assoc (car message-list) alist)
69             (setq alist (cons (list (car message-list)) alist)))
70           (setq message-list (cdr message-list))))
71       alist)))
72
73 (defun riece-mcat-update (files mcat-file mcat-alist)
74   (let ((pp-escape-newlines t)
75         alist)
76     (save-excursion
77       (set-buffer (find-file-noselect mcat-file))
78       (goto-char (point-min))
79       (if (re-search-forward (concat "^\\s-*(\\(defvar\\|defconst\\)\\s-+"
80                                      (regexp-quote (symbol-name mcat-alist)))
81                              nil t)
82           (progn
83             (goto-char (match-beginning 0))
84             (save-excursion
85               (eval (read (current-buffer))))
86             (delete-region (point) (progn (forward-sexp) (point))))
87         (set mcat-alist nil))
88       (setq alist (riece-mcat-extract files (symbol-value mcat-alist)))
89       (insert "(defconst " (symbol-name mcat-alist) "\n  '(")
90       (while alist
91         (insert "(" (pp-to-string (car (car alist))) " . "
92                 (pp-to-string (cdr (car alist))) ")")
93         (if (cdr alist)
94             (insert "\n    "))
95         (setq alist (cdr alist)))
96       (insert "))")
97       (save-buffer))))
98
99 (defconst riece-mcat-description "Translate messages")
100
101 (defun riece-mcat-insinuate ()
102   (set-language-info "Japanese" 'riece-mcat-feature 'riece-mcat-japanese))
103
104 (defun riece-mcat-uninstall ()
105   (set-language-info "Japanese" 'riece-mcat-feature nil))
106
107 (provide 'riece-mcat)
108
109 ;;; riece-mcat.el ends here