]> git.donarmstrong.com Git - lib.git/blob - emacs_el/tiny-tools/tiny/tinynbr.el
add tiny-tools
[lib.git] / emacs_el / tiny-tools / tiny / tinynbr.el
1 ;;; tinynbr.el --- Number conversion minor mode oct/bin/hex
2
3 ;; This file is not part of Emacs
4
5 ;;{{{ Id
6
7 ;; Copyright (C)    1997-2007 Jari Aalto
8 ;; Keywords:        tools
9 ;; Author:          Jari Aalto
10 ;; Maintainer:      Jari Aalto
11 ;;
12 ;; To get information on this program, call M-x tinynbr-version.
13 ;; Look at the code with folding.el.
14
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)
18 ;; any later version.
19 ;;
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
23 ;; for more details.
24 ;;
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.
29 ;;
30 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
31
32 ;;}}}
33 ;;{{{ Install
34
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.
39 ;;
40 ;;      (require 'tinynbr)
41 ;;
42 ;;  You can also use the preferred way: autoload
43 ;;
44 ;;       (autoload 'tinynbr-mode "tinynbr t t)
45 ;;       ;;  Put all minor mode activations below C-c m map
46 ;;       ;;
47 ;;       (global-set-key "\C-cmN"  'tinynbr-mode)
48 ;;
49 ;;  If you have any questions, use this function to contact author
50 ;;
51 ;;       M-x tinynbr-submit-bug-report
52
53 ;;}}}
54 ;;{{{ Documentation
55
56 ;; ..................................................... &t-commentary ...
57 ;;; Commentary:
58 ;;
59 ;;  Preface, aug 1997
60 ;;
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.
67 ;;
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.
73 ;;
74 ;;  Overview of features
75 ;;
76 ;;      o   Int         --> hex,oct,bin conversion at current point
77 ;;      o   hex,oct,bin --> int         conversion at current point
78
79 ;;}}}
80
81 ;;; Change Log:
82
83 ;;; Code:
84
85 ;;{{{ setup: require
86
87 (require 'tinylibm)
88
89 (ti::package-defgroup-tiny TinyNbr tinynbr-: tools
90   "Number conversion minor mode oct/bin/hex.")
91
92 (defcustom tinynbr-:load-hook nil
93   "*Hook run when file has been loaded."
94   :type  'hook
95   :group 'TinyNbr)
96
97 ;;;###autoload (autoload 'tinynbr-version "tinynbr" "Display commentary." t)
98
99 (eval-and-compile
100   (ti::macrof-version-bug-report
101    "tinynbr.el"
102    "tinynbr"
103    tinynbr-:version-id
104    "$Id: tinynbr.el,v 2.40 2007/05/01 17:20:51 jaalto Exp $"
105    '(tinynbr-:version-id
106      tinynbr-:load-hook)))
107
108 (defun tinynbr-read-number-at-point (&optional reverse base)
109   "Read base 1= or 16 number at point."
110   (if reverse
111       (ti::buffer-read-word "[0-9]+" 'strict)
112     (ti::buffer-read-word
113      "[0-9xXa-fA-F]+" 'strict)))
114
115 (defun tinynbr-read-number (&optional reverse)
116   "Read word if point is at non-whitespace. Optional REVERSE."
117   (let* ((char (following-char))
118          (nbr  (when char
119                  (setq char (char-to-string char))
120                  (save-excursion
121                    (unless (string-match "[ \t\f\r\n]" char)
122                      (tinynbr-read-number-at-point))))))
123     nbr))
124
125 ;;}}}
126 ;;{{{ Minor Mode
127
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)
132
133 (eval-and-compile
134   (ti::macrof-minor-mode-wizard
135    "tinynbr-" " Tnbr" "z" "Nbr" 'Tnbr "tinynbr-:"
136    "Simple number conversion minor mode.
137
138 Mode description:
139
140 \\{tinynbr-:mode-prefix-map}"
141
142    "TinyNbr"
143    nil
144    "Number conversion mode"
145    (list                                ;arg 10
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]
150     "----"
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]
154     "----"
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])
159    (progn
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))))
171
172 ;;}}}
173 ;;{{{ Code
174
175 ;;; Create functions, and inform autoload generator.
176
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)
183
184 (mapcar
185  (function
186   (lambda (x)
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))))
190           (base  (nth 1 x))
191           def)
192       (setq def
193             (` (defun (, sym1) (&optional insert reverse)
194                  "If prefix arg INSERT is non-nil, insert result to buffer."
195                  (interactive "P")
196                  (let* ((nbr (tinynbr-read-number reverse))
197                         ret)
198                    (when nbr
199                      (if (string-match "^0[Xx]\\(.*\\)" nbr)
200                          (setq nbr (match-string 1 nbr)))
201                      (if (null reverse)
202                          (setq ret (radix nbr (, base)))
203                        (setq ret ((, sym3) (string-to-int nbr)))))
204                    (cond
205                     ((null nbr)
206                      (message "TinyNbr: Can't find number at current point."))
207                     (t
208                      (if (not insert)
209                          (message "%s => %s %s"
210                                   nbr
211                                   ret
212                                   (cond
213                                    ((equal (, base)  2)
214                                     (if reverse "bin - dec" "dec - bin"  ))
215                                    ((equal (, base)  8)
216                                     (if reverse "oct - dec" "dec - oct"  ))
217                                    ((equal (, base) 16)
218                                     (if reverse "hex - dec" "dec - hex"  ))
219                                    (t "")))
220                        (save-excursion
221                          (end-of-line)
222                          (insert " " (if (numberp ret)
223                                          (int-to-string ret)
224                                        ret))))))))))
225       (eval def)
226
227       (setq def
228             (` (defun (, sym2) (&optional insert)
229                  "If prefix arg INSERT is non-nil, insert result to buffer."
230                  (interactive "P")
231                  ((, sym1) insert 'reverse))))
232       (eval def))))
233  '(
234    (hex 16)
235    (oct 8)
236    (bin 2)))
237
238 ;;}}}
239
240 (add-hook  'tinynbr-:mode-hook 'tinynbr-mode-define-keys)
241 (provide   'tinynbr)
242 (run-hooks 'tinynbr-:load-hook)
243
244 ;;; tinynbr.el ends here