unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: good enough for MELPA work?
Date: Wed, 26 Feb 2020 08:27:03 +0100	[thread overview]
Message-ID: <87imjtg660.fsf@ebih.ebihd> (raw)

OK, I got it to work, the code looks OK.

I used (checkdoc-current-buffer t) to help with
the conventions.

It (checkdoc) still complains about the first
line, but that is OK, right? So that is a bug,
if indeed alright.

It also complains about every function not
being documented but I think not everything has
to be, only interactive functions for sure.
Maybe I'll still do it later, just to shut it
up and prove who has the better fighter and the
better gym.

The one thing I wonder tho is, does Emacs have
one global namespace? If so, should you adhere
to some convention with respect to names?
How do you avoid collisions?

Personally I only have a couple of packages
from M/ELPA but I know some people are big
consumers, so I wonder how that works
out, for them?

;;; isbn-verify - Verify ISBN-10 and ISBN-13 -*- lexical-binding: t -*-
;;
;;; Commentary:
;;
;; This Elisp package provides functions to
;; verify ISBNs by computing their check
;; digits. The package can be found here:
;;   <https://dataswamp.org/~incal/emacs-init/isbn-verify.el>
;;
;; For more info on ISBNs and how the check
;; digits are computed, see:
;;   <https://dataswamp.org/~incal/books/isbn.txt>
;;
;; To see how it works, try
;; `check-isbn-at-point' on these two Biblatex
;; entries, the first one with ISBN-10, the
;; second with ISBN-13. Place point at the
;; beginning of the ISBN, then do M-x ciap RET
;;
;; @book{russia-and-the-arms-trade,
;;   author    = {Ian Anthony},
;;   isbn      = {0-19-829278-3},
;;   publisher = {Oxford},
;;   title     = {Russia and the Arms Trade},
;;   year      = 1998
;; }
;;
;; @book{baa-lo-4,
;;   author     = {Yukito Kishiro},
;;   isbn       = {978-1-61262-294-1},
;;   publisher  = {Kodansha},
;;   title      = {Battle Angel Alita: The Last Order 4},
;;   year       = {2014 (2011)}
;; }
;;
;; Or, with Lisp:
;;
;;   (checksum-isbn-10 "0-19-829576-6")     ; 6
;;   (checksum-isbn-13 "978-1-61262-294-1") ; 1
;;
;;; Code:

(require 'cl-lib)

(defun digits-only-string (str)
  (replace-regexp-in-string "[^0-9]" "" str) )

(defun check-isbn-at-point ()
  "Compute and echo the check digit from the ISBN at point."
  (interactive)
  (let*((isbn-string (thing-at-point 'symbol t))
        (digits-only (digits-only-string isbn-string))
        (num-digits  (length digits-only))
        (check-digit (if (> num-digits 10)
                         (checksum-isbn-13 digits-only)
                       (checksum-isbn-10 digits-only) )))
    (message "check digit: %s" check-digit)
    ))
(defalias 'ciap #'check-isbn-at-point)

(defun char-to-int (c)
  (- c ?0))

(defun isbn-integer-list (isbn)
  (let*((digits-only       (digits-only-string isbn))
        (isbn-list         (string-to-list digits-only))
        (isbn-ints         (cl-map 'list
                                   (lambda (c) (char-to-int c))
                                   isbn-list) ))
    isbn-ints) )

(defun checksum-isbn-13 (isbn)
  (let*((isbn-ints (isbn-integer-list isbn))
        (sum       0))
    (cl-loop for e in isbn-ints
             for i from 0 to 11
             do (progn (message "%d" i)
                       (cl-incf sum (* e (or (and (zerop (mod i 2)) 1) 3)))
                       )
             )
    (let ((checksum (- 10 (mod sum 10))))
      (if (= 10 checksum) 0 checksum) )))

;; 13 ISBN-13 tests:
;;
;; (checksum-isbn-13 "91-518-4657-8")     ; 8
;; (checksum-isbn-13 "978-1-63236-616-0") ; 0
;; (checksum-isbn-13 "978-91-0-012814-2") ; 2
;; (checksum-isbn-13 "978-91-29-59023-4") ; 4
;; (checksum-isbn-13 "978-91-7037-681-8") ; 8
;; (checksum-isbn-13 "978-91-7515-205-9") ; 9
;; (checksum-isbn-13 "978-91-7515-317-9") ; 9
;; (checksum-isbn-13 "978-91-86936-31-0") ; 0
;; (checksum-isbn-13 "978-91-87861-54-3") ; 3
;; (checksum-isbn-13 "978-91-87861-67-3") ; 3
;; (checksum-isbn-13 "978-91-87861-99-4") ; 4
;; (checksum-isbn-13 "9780062802187")     ; 7
;; (checksum-isbn-13 "9789188805034")     ; 4

(defun checksum-isbn-10 (isbn)
  (let*((isbn-ints (isbn-integer-list isbn))
        (sum       0) )
    (cl-loop for e in isbn-ints
             for i downfrom 10 to 2
             do (cl-incf sum (* e i)) )
    (let ((checksum (mod (- 11 (mod sum 11)) 11)))
      (if (= 10 checksum) "X" checksum) )))

;; 10 ISBN-10 tests:
;;
;; (checksum-isbn-10 "0-201-53992-6") ; 6
;; (checksum-isbn-10 "0312168144")    ; 4
;; (checksum-isbn-10 "1-4012-0622-0") ; 0
;; (checksum-isbn-10 "1616558717")    ; 7
;; (checksum-isbn-10 "91-510-6483-9") ; 9
;; (checksum-isbn-10 "91-7054-940-0") ; 0
;; (checksum-isbn-10 "91-7089-710-7") ; 7
;; (checksum-isbn-10 "91-85668-01-X") ; X
;; (checksum-isbn-10 "91-88930-23-8") ; 8
;; (checksum-isbn-10 "9177988515")    ; 5

(provide 'isbn-verify)
;;; isbn-verify.el ends here

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




             reply	other threads:[~2020-02-26  7:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-26  7:27 Emanuel Berg via Users list for the GNU Emacs text editor [this message]
2020-02-26  9:16 ` good enough for MELPA work? Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87imjtg660.fsf@ebih.ebihd \
    --to=help-gnu-emacs@gnu.org \
    --cc=moasenwood@zoho.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).