1 ;;; tinynbr.el --- Number conversion minor mode oct/bin/hex
3 ;; This file is not part of Emacs
7 ;; Copyright (C) 1997-2007 Jari Aalto
10 ;; Maintainer: Jari Aalto
12 ;; To get information on this program, call M-x tinynbr-version.
13 ;; Look at the code with folding.el.
15 ;; This program is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by the Free
17 ;; Software Foundation; either version 2 of the License, or (at your option)
20 ;; This program is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with program; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
30 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
35 ;; ....................................................... &t-install ...
36 ;; Put this file on your Emacs-Lisp load path, add following into your
37 ;; ~/.emacs startup file. This must be the very first entry before
38 ;; any keybindings take in effect.
42 ;; You can also use the preferred way: autoload
44 ;; (autoload 'tinynbr-mode "tinynbr t t)
45 ;; ;; Put all minor mode activations below C-c m map
47 ;; (global-set-key "\C-cmN" 'tinynbr-mode)
49 ;; If you have any questions, use this function to contact author
51 ;; M-x tinynbr-submit-bug-report
56 ;; ..................................................... &t-commentary ...
61 ;; One day in a laboratory the developer once forgot his desk
62 ;; calculator in another building. He was examining binary (hex)
63 ;; files and other electronic documents that used hex and base10
64 ;; numbers. He shroudly remembered that Unix included some basic
65 ;; calculator, but he dind't remember what was the name and how
66 ;; to use it. Whoops. Grin.
68 ;; Instead of returning to get the missing calculator, he started
69 ;; pouring some lisp to make a simple minor mode to help
70 ;; to get along with the current task at hand. It didn't take
71 ;; long to make it, and the laboratory day was success.
72 ;; Ahem. Maybe should look at package calc.el someday.
74 ;; Overview of features
76 ;; o Int --> hex,oct,bin conversion at current point
77 ;; o hex,oct,bin --> int conversion at current point
89 (ti::package-defgroup-tiny TinyNbr tinynbr-: tools
90 "Number conversion minor mode oct/bin/hex.")
92 (defcustom tinynbr-:load-hook nil
93 "*Hook run when file has been loaded."
97 ;;;###autoload (autoload 'tinynbr-version "tinynbr" "Display commentary." t)
100 (ti::macrof-version-bug-report
104 "$Id: tinynbr.el,v 2.40 2007/05/01 17:20:51 jaalto Exp $"
105 '(tinynbr-:version-id
106 tinynbr-:load-hook)))
108 (defun tinynbr-read-number-at-point (&optional reverse base)
109 "Read base 1= or 16 number at point."
111 (ti::buffer-read-word "[0-9]+" 'strict)
112 (ti::buffer-read-word
113 "[0-9xXa-fA-F]+" 'strict)))
115 (defun tinynbr-read-number (&optional reverse)
116 "Read word if point is at non-whitespace. Optional REVERSE."
117 (let* ((char (following-char))
119 (setq char (char-to-string char))
121 (unless (string-match "[ \t\f\r\n]" char)
122 (tinynbr-read-number-at-point))))))
128 ;;;###autoload (autoload 'tinynbr-mode "tinynbr" "" t)
129 ;;;###autoload (autoload 'turn-on-tinynbr-mode "tinynbr" "" t)
130 ;;;###autoload (autoload 'tun-off-tinynbr-mode "tinynbr" "" t)
131 ;;;###autoload (autoload 'tinynbr-commentary "tinynbr" "" t)
134 (ti::macrof-minor-mode-wizard
135 "tinynbr-" " Tnbr" "z" "Nbr" 'Tnbr "tinynbr-:"
136 "Simple number conversion minor mode.
140 \\{tinynbr-:mode-prefix-map}"
144 "Number conversion mode"
146 tinynbr-:mode-easymenu-name
147 ["int to hex" tinynbr-int-to-hex t]
148 ["int to oct" tinynbr-int-to-bin t]
149 ["int to bin" tinynbr-int-to-oct t]
151 ["hex to int" tinynbr-hex-to-int t]
152 ["oct to int" tinynbr-oct-to-int t]
153 ["bin to int" tinynbr-bin-to-int t]
155 ["Package version" tinynbr-version t]
156 ["Package commentary" tinynbr-commentary t]
157 ["Mode help" tinynbr-mode-help t]
158 ["Mode off" tinynbr-mode t])
160 (define-key map "X" 'tinynbr-hex-to-int)
161 (define-key map "B" 'tinynbr-bin-to-int)
162 (define-key map "O" 'tinynbr-oct-to-int)
163 (define-key map "x" 'tinynbr-int-to-hex)
164 (define-key map "b" 'tinynbr-int-to-bin)
165 (define-key map "o" 'tinynbr-int-to-oct)
166 (define-key map "v" 'tinynbr-version)
167 (define-key map "?" 'tinynbr-mode-help)
168 (define-key map "Hm" 'tinynbr-mode-help)
169 (define-key map "Hc" 'tinynbr-commentary)
170 (define-key map "Hv" 'tinynbr-version))))
175 ;;; Create functions, and inform autoload generator.
177 ;;;###autoload (autoload 'tinynbr-int-to-hex "tinynbr" "" t)
178 ;;;###autoload (autoload 'tinynbr-int-to-oct "tinynbr" "" t)
179 ;;;###autoload (autoload 'tinynbr-int-to-bin "tinynbr" "" t)
180 ;;;###autoload (autoload 'tinynbr-hex-to-int "tinynbr" "" t)
181 ;;;###autoload (autoload 'tinynbr-oct-to-int "tinynbr" "" t)
182 ;;;###autoload (autoload 'tinynbr-bin-to-int "tinynbr" "" t)
187 (let ((sym1 (intern (format "tinynbr-%s-to-int" (car x))))
188 (sym2 (intern (format "tinynbr-int-to-%s" (car x))))
189 (sym3 (intern (format "int-to-%s-string" (car x))))
193 (` (defun (, sym1) (&optional insert reverse)
194 "If prefix arg INSERT is non-nil, insert result to buffer."
196 (let* ((nbr (tinynbr-read-number reverse))
199 (if (string-match "^0[Xx]\\(.*\\)" nbr)
200 (setq nbr (match-string 1 nbr)))
202 (setq ret (radix nbr (, base)))
203 (setq ret ((, sym3) (string-to-int nbr)))))
206 (message "TinyNbr: Can't find number at current point."))
209 (message "%s => %s %s"
214 (if reverse "bin - dec" "dec - bin" ))
216 (if reverse "oct - dec" "dec - oct" ))
218 (if reverse "hex - dec" "dec - hex" ))
222 (insert " " (if (numberp ret)
228 (` (defun (, sym2) (&optional insert)
229 "If prefix arg INSERT is non-nil, insert result to buffer."
231 ((, sym1) insert 'reverse))))
240 (add-hook 'tinynbr-:mode-hook 'tinynbr-mode-define-keys)
242 (run-hooks 'tinynbr-:load-hook)
244 ;;; tinynbr.el ends here