1 ;;; morse.el --- convert text to morse code and back
3 ;; Copyright (C) 1995, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Rick Farnbach <rick_farnbach@MENTORG.COM>
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 <https://www.gnu.org/licenses/>.
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.
33 (defvar morse-code
'(("a" .
".-")
86 ;; ligature character?? ("Ch" . "----")
93 ;; Recently standardized
95 "Morse code character set.")
97 (defvar nato-alphabet
'(("a" .
"Alfa")
134 ;; Punctuation is not part of standard
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 URL `http://www.nga.mil/'")
152 (defun morse-region (beg end
)
153 "Convert all text in a given region to morse code."
156 (setq end
(copy-marker end
)))
161 (while (< (point) end
)
162 (setq str
(downcase (buffer-substring (point) (1+ (point)))))
163 (cond ((looking-at "\\s-+")
164 (goto-char (match-end 0))
166 ((setq morse
(assoc str morse-code
))
168 (insert sep
(cdr morse
))
175 (defun unmorse-region (beg end
)
176 "Convert morse coded text in region to ordinary ASCII text."
179 (setq end
(copy-marker end
)))
181 (let (str paren morse
)
183 (while (< (point) end
)
184 (if (null (looking-at "[-.]+"))
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))
190 (if (string-equal "(" (car morse
))
191 (if (setq paren
(null paren
)) "(" ")")
194 (delete-char 1))))))))
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
202 (setq end
(copy-marker end
)))
207 (while (< (point) end
)
208 (setq str
(downcase (buffer-substring (point) (1+ (point)))))
209 (cond ((looking-at "\\s-+")
210 (goto-char (match-end 0))
212 ((setq nato
(assoc str nato-alphabet
))
214 (insert sep
(cdr nato
))
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
226 (setq end
(copy-marker end
)))
228 (let (str paren nato
)
230 (while (< (point) end
)
231 (if (null (looking-at "[a-z]+"))
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))
237 (if (string-equal "(" (car nato
))
238 (if (setq paren
(null paren
)) "(" ")")
241 (delete-char 1))))))))
245 ;;; morse.el ends here