Initial Commit
[packages] / xemacs-packages / auctex / style / letter.el
1 ;;; letter.el - Special code for letter style.
2
3 ;; Copyright (C) 1993, 2012, 2013  Free Software Foundation, Inc.
4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Keywords: tex
8
9 ;; This file is part of AUCTeX.
10
11 ;; AUCTeX is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; AUCTeX is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 ;; 02110-1301, USA.
25
26 ;;; Code:
27
28 (defvar LaTeX-letter-class-options
29   '("a4paper" "a5paper" "b5paper" "letterpaper" "legalpaper" "executivepaper"
30     "landscape" "10pt" "11pt" "12pt" "oneside" "twoside" "draft" "final"
31     "leqno" "fleqn")
32   "Package options for the letter class.")
33
34 ;; You may want to define this in tex-site.el to contain your
35 ;; organizations address.
36 (defvar LaTeX-letter-sender-address ""
37   "Initial value when prompting for a sender address in the letter style.")
38
39 (TeX-add-style-hook
40  "letter"
41  (lambda ()
42    (LaTeX-add-environments
43     '("letter" LaTeX-env-recipient))
44    (LaTeX-add-pagestyles "headings" "firstpage")
45    (setq LaTeX-default-document-environment "letter")
46    (TeX-add-symbols
47     '("name" "Sender: ")
48     '("address" "Sender address: ")
49     '("signature" "Signature: ")
50     '("opening" "Opening: ")
51     '("closing" "Closing: ")
52     "location"
53     "telephone"
54     "makelabels"
55     "stopbreaks"
56     "startbreaks"
57     "cc"
58     "encl"
59     "ps"
60     "stopletter"
61     "returnaddress"
62     "startlabels"
63     "mlabel"
64     "descriptionlabel"
65     "ccname"
66     "enclname"
67     "pagename"
68     "headtoname")
69
70    ;; Fontification
71    (when (and (featurep 'font-latex)
72               (eq TeX-install-font-lock 'font-latex-setup))
73      (font-latex-add-keywords '(("name" "{")
74                                 ("address" "{")
75                                 ("signature" "{")
76                                 ("opening" "{")
77                                 ("closing" "{")
78                                 ("location" "{")
79                                 ("telephone" "{")
80                                 ("cc" "{")
81                                 ("encl" "{")
82                                 ("ps" "{"))
83                               'function)))
84  LaTeX-dialect)
85
86 (defun LaTeX-env-recipient (environment)
87   "Insert ENVIRONMENT and prompt for recipient and address."
88   (let ((sender (read-string "Sender: " (user-full-name)))
89         (sender-address (read-string "Sender address: "
90                                      LaTeX-letter-sender-address))
91         (recipient (read-string "Recipient: "))
92         (address (read-string "Recipient address: "))
93         (signature (read-string "Signature: "))
94         (opening (read-string "Opening: "))
95         (closing (read-string "Closing: "))
96         (date (read-string "Date: " (LaTeX-today))))
97
98     (insert TeX-esc "name" TeX-grop sender TeX-grcl)
99     (newline-and-indent)
100     (if (not (zerop (length sender-address)))
101         (progn
102           (setq LaTeX-letter-sender-address sender-address)
103           (insert TeX-esc "address" TeX-grop sender-address TeX-grcl)
104           (newline-and-indent)))
105     (if (not (zerop (length signature)))
106         (progn
107           (insert TeX-esc "signature" TeX-grop signature TeX-grcl)
108           (newline-and-indent)))
109     (if (not (zerop (length date)))
110         (progn
111           (insert TeX-esc "renewcommand" TeX-grop TeX-esc "today" TeX-grcl
112                   TeX-grop date TeX-grcl)
113           (newline-and-indent)))
114     (newline-and-indent)
115
116     (let ((indentation (current-column)))
117       (LaTeX-insert-environment
118        environment
119        (concat TeX-grop recipient
120                (if (not (zerop (length address)))
121                    (concat
122                     (if (not (zerop (length recipient)))
123                         (concat " " TeX-esc TeX-esc " "))
124                     address))
125                TeX-grcl))
126       (save-excursion                   ; Fix indentation of address
127         (if (search-backward TeX-grcl nil 'move)
128             (let ((addr-end (point-marker)))
129               (if (search-backward TeX-grop nil 'move)
130                   (let ((addr-column (current-column)))
131                     (while (search-forward
132                             (concat TeX-esc TeX-esc)
133                             (marker-position addr-end) 'move)
134                       (progn
135                         (newline)
136                         (indent-to addr-column))))))))
137       (insert "\n")
138       (indent-to indentation))
139     (insert TeX-esc "opening"
140             TeX-grop
141             (if (zerop (length opening))
142                 (concat TeX-esc " ")
143               opening)
144             TeX-grcl "\n")
145
146     (indent-relative-maybe)
147     (save-excursion
148       (insert "\n" TeX-esc "closing"
149               TeX-grop
150               (if (zerop (length closing))
151                   (concat TeX-esc " ")
152                 closing)
153               TeX-grcl "\n")
154       (indent-relative-maybe))))
155
156 (defun LaTeX-today nil
157   "Return a string representing todays date according to flavor."
158   (interactive)
159   (let ((ctime-string (current-time-string))
160         (month-alist '(("Jan". "01")
161                        ("Feb" . "02")
162                        ("Mar" . "03")
163                        ("Apr" . "04")
164                        ("May" . "05")
165                        ("Jun" . "06")
166                        ("Jul" . "07")
167                        ("Aug" . "08")
168                        ("Sep" . "09")
169                        ("Oct" . "10")
170                        ("Nov" . "11")
171                        ("Dec" . "12"))))
172     (string-match
173      "^\\S-+\\s-+\\(\\S-+\\)\\s-+\\(\\S-+\\)\\s-+\\S-+\\s-+\\(\\S-+\\)"
174      ctime-string)
175     (let ((year (substring ctime-string (match-beginning 3) (match-end 3)))
176           (month (substring ctime-string (match-beginning 1) (match-end 1)))
177           (day (substring ctime-string (match-beginning 2) (match-end 2))))
178       (if (assoc month month-alist)
179           (progn
180             (setq month (cdr (assoc month month-alist)))
181             (if (> 2 (length day))
182                 (setq day (concat "0" day)))))
183       (format "%s-%s-%s" year month day))))
184
185 ;;; letter.el ends here