13651
|
1 ;;; morse.el --- Convert text to morse code and back.
|
|
2
|
|
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Rick Farnbach <rick_farnbach@MENTORG.COM>
|
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
10 ;; it under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 ;; GNU General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
|
23 ;;; Code:
|
|
24
|
|
25 (defvar morse-code '(("a" . ".-")
|
|
26 ("b" . "-...")
|
|
27 ("c" . "-.-.")
|
|
28 ("d" . "-..")
|
|
29 ("e" . ".")
|
|
30 ("f" . "..-.")
|
|
31 ("g" . "--.")
|
|
32 ("h" . "....")
|
|
33 ("i" . "..")
|
|
34 ("j" . ".---")
|
|
35 ("k" . "-.-")
|
|
36 ("l" . ".-..")
|
|
37 ("m" . "--")
|
|
38 ("n" . "-.")
|
|
39 ("o" . "---")
|
|
40 ("p" . ".--.")
|
|
41 ("q" . "--.-")
|
|
42 ("r" . ".-.")
|
|
43 ("s" . "...")
|
|
44 ("t" . "-")
|
|
45 ("u" . "..-")
|
|
46 ("v" . "...-")
|
|
47 ("w" . ".--")
|
|
48 ("x" . "-..-")
|
|
49 ("y" . "-.--")
|
|
50 ("z" . "--..")
|
|
51 ;; Punctuation
|
|
52 ("=" . "-...-")
|
|
53 ("?" . "..--..")
|
|
54 ("/" . "-..-.")
|
|
55 ("," . "--..--")
|
|
56 ("." . ".-.-.-")
|
|
57 (":" . "---...")
|
|
58 ("'" . ".----.")
|
|
59 ("-" . "-....-")
|
|
60 ("(" . "-.--.-")
|
|
61 (")" . "-.--.-")
|
|
62 ;; Numbers
|
|
63 ("0" . "-----")
|
|
64 ("1" . ".----")
|
|
65 ("2" . "..---")
|
|
66 ("3" . "...--")
|
|
67 ("4" . "....-")
|
|
68 ("5" . ".....")
|
|
69 ("6" . "-....")
|
|
70 ("7" . "--...")
|
|
71 ("8" . "---..")
|
|
72 ("9" . "----."))
|
|
73 "Morse code character set.")
|
|
74
|
|
75 (defun morse-region (beg end)
|
|
76 "Convert all text in a given region to morse code."
|
|
77 (interactive "r")
|
|
78 (if (integerp end)
|
|
79 (setq end (copy-marker end)))
|
|
80 (save-excursion
|
|
81 (let ((sep "")
|
|
82 str morse)
|
|
83 (goto-char beg)
|
|
84 (while (< (point) end)
|
|
85 (setq str (downcase (buffer-substring (point) (1+ (point)))))
|
|
86 (cond ((looking-at "\\s-+")
|
|
87 (goto-char (match-end 0))
|
|
88 (setq sep ""))
|
|
89 ((setq morse (assoc str morse-code))
|
|
90 (delete-char 1)
|
|
91 (insert sep (cdr morse))
|
|
92 (setq sep "/"))
|
|
93 (t
|
|
94 (forward-char 1)
|
|
95 (setq sep "")))))))
|
|
96
|
|
97 (defun unmorse-region (beg end)
|
|
98 "Convert morse coded text in region to ordinary ASCII text."
|
|
99 (interactive "r")
|
|
100 (if (integerp end)
|
|
101 (setq end (copy-marker end)))
|
|
102 (save-excursion
|
|
103 (let (str paren morse)
|
|
104 (goto-char beg)
|
|
105 (while (< (point) end)
|
|
106 (if (null (looking-at "[-.]+"))
|
|
107 (forward-char 1)
|
|
108 (setq str (buffer-substring (match-beginning 0) (match-end 0)))
|
|
109 (if (null (setq morse (rassoc str morse-code)))
|
|
110 (goto-char (match-end 0))
|
|
111 (replace-match
|
|
112 (if (string-equal "(" (car morse))
|
|
113 (if (setq paren (null paren)) "(" ")")
|
|
114 (car morse)) t)
|
|
115 (if (looking-at "/")
|
|
116 (delete-char 1))))))))
|
|
117
|
|
118 (provide 'morse)
|
|
119
|
|
120 ;;; morse.el ends here
|