Add `remember-notes' function to store random notes across Emacs
[emacs.git] / lisp / play / morse.el
blob54dfd1c4ea34d4e32ebd103b5b6aed8cad25a477
1 ;;; morse.el --- convert text to morse code and back -*- coding: utf-8 -*-
3 ;; Copyright (C) 1995, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Rick Farnbach <rick_farnbach@MENTORG.COM>
6 ;; Keywords: games
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Converts text to Morse code and back with M-x morse-region and
26 ;; M-x unmorse-region (though Morse code is no longer official :-().
28 ;; Converts text to NATO phonetic alphabet and back with M-x
29 ;; nato-region and M-x denato-region.
31 ;;; Code:
33 (defvar morse-code '(("a" . ".-")
34 ("b" . "-...")
35 ("c" . "-.-.")
36 ("d" . "-..")
37 ("e" . ".")
38 ("f" . "..-.")
39 ("g" . "--.")
40 ("h" . "....")
41 ("i" . "..")
42 ("j" . ".---")
43 ("k" . "-.-")
44 ("l" . ".-..")
45 ("m" . "--")
46 ("n" . "-.")
47 ("o" . "---")
48 ("p" . ".--.")
49 ("q" . "--.-")
50 ("r" . ".-.")
51 ("s" . "...")
52 ("t" . "-")
53 ("u" . "..-")
54 ("v" . "...-")
55 ("w" . ".--")
56 ("x" . "-..-")
57 ("y" . "-.--")
58 ("z" . "--..")
59 ;; Punctuation
60 ("=" . "-...-")
61 ("?" . "..--..")
62 ("/" . "-..-.")
63 ("," . "--..--")
64 ("." . ".-.-.-")
65 (":" . "---...")
66 ("'" . ".----.")
67 ("-" . "-....-")
68 ("(" . "-.--.-")
69 (")" . "-.--.-")
70 ;; Numbers
71 ("0" . "-----")
72 ("1" . ".----")
73 ("2" . "..---")
74 ("3" . "...--")
75 ("4" . "....-")
76 ("5" . ".....")
77 ("6" . "-....")
78 ("7" . "--...")
79 ("8" . "---..")
80 ("9" . "----.")
81 ;; Non-ASCII
82 ("Ä" . ".-.-")
83 ("Æ" . ".-.-")
84 ("Á" . ".--.-")
85 ("Å" . ".--.-")
86 ;; ligature character?? ("Ch" . "----")
87 ("ß" . ".../...")
88 ("É" . "..-..")
89 ("Ñ" . "--.--")
90 ("Ö" . "---.")
91 ("Ø" . "---.")
92 ("Ü" . "..--")
93 ;; Recently standardized
94 ("@" . ".--.-."))
95 "Morse code character set.")
97 (defvar nato-alphabet '(("a" . "Alfa")
98 ("b" . "Bravo")
99 ("c" . "Charlie")
100 ("d" . "Delta")
101 ("e" . "Echo")
102 ("f" . "Foxtrot")
103 ("g" . "Golf")
104 ("h" . "Hotel")
105 ("i" . "India")
106 ("j" . "Juliett")
107 ("k" . "Kilo")
108 ("l" . "Lima")
109 ("m" . "Mike")
110 ("n" . "November")
111 ("o" . "Oscar")
112 ("p" . "Papa")
113 ("q" . "Quebec")
114 ("r" . "Romeo")
115 ("s" . "Sierra")
116 ("t" . "Tango")
117 ("u" . "Uniform")
118 ("v" . "Victor")
119 ("w" . "Whiskey")
120 ("x" . "Xray")
121 ("y" . "Yankee")
122 ("z" . "Zulu")
123 ;; Numbers
124 ("0" . "Zero")
125 ("1" . "One")
126 ("2" . "Two")
127 ("3" . "Three")
128 ("4" . "Four")
129 ("5" . "Five")
130 ("6" . "Six")
131 ("7" . "Seven")
132 ("8" . "Eight")
133 ("9" . "Niner")
134 ;; Punctuation is not part of standard
135 ("=" . "Equals")
136 ("?" . "Query")
137 ("/" . "Slash")
138 ("," . "Comma")
139 ("." . "Stop")
140 (":" . "Colon")
141 ("'" . "Apostrophe")
142 ("-" . "Dash")
143 ("(" . "Open")
144 (")" . "Close")
145 ("@" . "At"))
146 "NATO phonetic alphabet.
147 See ''International Code of Signals'' (INTERCO), United States
148 Edition, 1969 Edition (Revised 2003) available from National
149 Geospatial-Intelligence Agency at http://www.nga.mil/")
151 ;;;###autoload
152 (defun morse-region (beg end)
153 "Convert all text in a given region to morse code."
154 (interactive "*r")
155 (if (integerp end)
156 (setq end (copy-marker end)))
157 (save-excursion
158 (let ((sep "")
159 str morse)
160 (goto-char beg)
161 (while (< (point) end)
162 (setq str (downcase (buffer-substring (point) (1+ (point)))))
163 (cond ((looking-at "\\s-+")
164 (goto-char (match-end 0))
165 (setq sep ""))
166 ((setq morse (assoc str morse-code))
167 (delete-char 1)
168 (insert sep (cdr morse))
169 (setq sep "/"))
171 (forward-char 1)
172 (setq sep "")))))))
174 ;;;###autoload
175 (defun unmorse-region (beg end)
176 "Convert morse coded text in region to ordinary ASCII text."
177 (interactive "*r")
178 (if (integerp end)
179 (setq end (copy-marker end)))
180 (save-excursion
181 (let (str paren morse)
182 (goto-char beg)
183 (while (< (point) end)
184 (if (null (looking-at "[-.]+"))
185 (forward-char 1)
186 (setq str (buffer-substring (match-beginning 0) (match-end 0)))
187 (if (null (setq morse (rassoc str morse-code)))
188 (goto-char (match-end 0))
189 (replace-match
190 (if (string-equal "(" (car morse))
191 (if (setq paren (null paren)) "(" ")")
192 (car morse)) t)
193 (if (looking-at "/")
194 (delete-char 1))))))))
196 ;;;###autoload
197 (defun nato-region (beg end)
198 "Convert all text in a given region to NATO phonetic alphabet."
199 ;; Copied from morse-region. -- ashawley 2009-02-10
200 (interactive "*r")
201 (if (integerp end)
202 (setq end (copy-marker end)))
203 (save-excursion
204 (let ((sep "")
205 str nato)
206 (goto-char beg)
207 (while (< (point) end)
208 (setq str (downcase (buffer-substring (point) (1+ (point)))))
209 (cond ((looking-at "\\s-+")
210 (goto-char (match-end 0))
211 (setq sep ""))
212 ((setq nato (assoc str nato-alphabet))
213 (delete-char 1)
214 (insert sep (cdr nato))
215 (setq sep "-"))
217 (forward-char 1)
218 (setq sep "")))))))
220 ;;;###autoload
221 (defun denato-region (beg end)
222 "Convert NATO phonetic alphabet in region to ordinary ASCII text."
223 ;; Copied from unmorse-region. -- ashawley 2009-02-10
224 (interactive "*r")
225 (if (integerp end)
226 (setq end (copy-marker end)))
227 (save-excursion
228 (let (str paren nato)
229 (goto-char beg)
230 (while (< (point) end)
231 (if (null (looking-at "[a-z]+"))
232 (forward-char 1)
233 (setq str (buffer-substring (match-beginning 0) (match-end 0)))
234 (if (null (setq nato (rassoc (capitalize str) nato-alphabet)))
235 (goto-char (match-end 0))
236 (replace-match
237 (if (string-equal "(" (car nato))
238 (if (setq paren (null paren)) "(" ")")
239 (car nato)) t)
240 (if (looking-at "-")
241 (delete-char 1))))))))
243 (provide 'morse)
245 ;;; morse.el ends here