Instantly share code, notes, and snippets.

Embed
What would you like to do?
;;; coders-little-helper -- all kind of helper functions
;;
;;; Commentary:
;; Helper functions for all kind of daily Emacs stuff.
;;
;; Run tests on the command line:
;;
;; emacs -batch -l coders-little-helper.el
;;
;; From inside Emacs
;; (load "coders-little-helper.el")
;;; Code:
(require 'subr-x)
(eval-when-compile
(require 'cl))
(defvar military-alphabet '(("a" . "alpha")
("b" . "bravo")
("c" . "charlie")
("d" . "delta")
("e" . "echo")
("f" . "foxtrot")
("g" . "golf")
("h" . "hotel")
("i" . "india")
("j" . "juliet")
("k" . "kilo")
("l" . "lima")
("m" . "mike")
("n" . "november")
("o" . "oscar")
("p" . "papa")
("q" . "quebec")
("r" . "romeo")
("s" . "sierra")
("t" . "tango")
("u" . "uniform")
("v" . "victor")
("w" . "whiskey")
("x" . "x-Ray")
("y" . "yankee")
("z" . "zulu"))
"Military alphabet alist (CHARACTER . WORD).
All characters are lowercase for lookup.")
(defun string-to-military (s)
"Convert input string S to military alphabet."
(interactive "sEnter string to transfor to military: ")
(cl-flet ((military-get-string (s)
(if (string-blank-p s) " "
(alist-get s military-alphabet "" nil #'equal))))
(let* ((chars (mapcar #'string s))
(normalized-chars (mapcar #'downcase chars))
(militarized-string (string-join (mapcar #'military-get-string normalized-chars) " ")))
(if (called-interactively-p 'any)
(message militarized-string)
militarized-string))))
(defun string-to-military-region (start end)
"Replace region from START to END with military alphabet converted strings."
(interactive "r")
(let ((content (buffer-substring-no-properties start end)))
(delete-region start end)
(insert (string-to-military content))))
;;; TEST
(defvar coders-little-helper-run-tests t
"Run test test when eval the file.")
(when coders-little-helper-run-tests
(defun test-string-to-military ()
"Smoke test."
(assert (string-equal (string-to-military "emacs") "echo mike alpha charlie sierra")))
;;; TEST SUITE
(defvar coders-little-helper-test-suite nil
"Test suite for coders-little-helper.")
(add-hook 'coders-little-helper-test-suite 'test-string-to-military)
(run-hooks 'coders-little-helper-test-suite))
(provide 'coders-little-helper)
;;; coders-little-helper.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment