morse.el 6.4 KB

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