;;; issn-verify.el --- verify ISSN -*- lexical-binding: t -*- ;; ;; Author: Emanuel Berg ;; Created: 2020-09-06 ;; Keywords: bib, tex ;; License: GPL3+ ;; Package-Requires: ((cl-lib "1.0")) ;; URL: https://dataswamp.org/~incal/emacs-init/issn-verify.el ;; Version: 2.3.8 ;; ;;; Commentary: ;; ;; This Elisp package provides functions to verify ISSN by ;; computing their check digits. ;; ;; Also see: ;; ;; https://dataswamp.org/~incal/books/isbn.txt ;; https://dataswamp.org/~incal/emacs-init/bibtex/bibtex-autokey-insert.el ;; https://dataswamp.org/~incal/emacs-init/isbn-verify.el ;; ;; Install this package: ;; ;; M-x load-file RET RET ;; ;; Usage: ;; ;; - Put region over an ISSN and do M-x issn-verify RET ;; ;; - Or just do M-x issn-verify RET and input the ISSN ;; ;; - To use with Bibtex: see URL above ;; ;; ISSN: ;; ;; ISSN = International Standard Serial Number (ISO 3297, 1975) ;; ;; For example, The Ring Magazine has ISSN 0035-5410. ;; ;; The ISSN format: ;; ;; d_1 d_2 d_3 d_4 - d_5 d_6 d_7 c ;; ;; d_i (for i in 1-7) is a single integer. ;; ;; c, the check digit, is 0-9 or X, to denote 10. ;; ;; The algorithm to compute the check digit c: ;; ;; c' = (8 * d_1 * ;; 7 * d_2 * ;; 6 * d_3 * ;; 5 * d_4 * ;; 4 * d_5 * ;; 3 * d_6 * ;; 2 * d_7) % 11 ;; ;; { 0 (c' == 0) ;; c = { ;; { 11 - c' (c' != 0) ;; ;;; Code: (require 'cl-lib) (defun issn-get-digits (issn) "Get ISSN as numbers only." (let ((check-format (string-match-p "[[:digit:]]\\{4\\}-[[:digit:]]\\{3\\}[X[:digit:]]" issn) )) (if (and (= 9 (length issn)) check-format (zerop check-format) ) (let*((no-dash (replace-regexp-in-string "-" "" issn)) (issn-list (string-to-list no-dash)) (c-prop (car (last issn-list))) (c-prop-x (if (equal "X" c-prop) c-prop (- c-prop ?0))) (7-1st (butlast issn-list)) (nums (mapcar (lambda (c) (- c ?0)) 7-1st)) ) `(,@nums ,c-prop-x) ) (error "Malformed ISSN") ))) (defun issn-verify (issn) "Verify ISSN." (interactive (if (use-region-p) (list (buffer-substring-no-properties (region-beginning) (region-end))) (list (read-string "ISSN: ")) )) (let*((issn-lst (issn-get-digits issn)) (c-prop (car (last issn-lst))) (c-w (cl-loop with cwl = 0 for d in issn-lst for w downfrom 8 to 2 do (cl-incf cwl (* w d)) finally return cwl) ) (c-prime (mod c-w 11)) (c (if (zerop c-prime) 0 (- 11 c-prime) )) (c-x (if (= 10 c) "X" c)) ) (if (equal c-prop c-x) (message "[ISSN OK] %s" c-x) (error "Incorrect ISSN, correct check digit is %s" c-prop) ))) (defalias 'verify-issn #'issn-verify) ;; Bicycle Quarterly (issn-verify "1941-8809" ) ; [ISSN OK] 9 ;; Bicycling (issn-verify "1651-9744" ) ; [ISSN OK] 4 ;; Black Belt (issn-verify "0277-3066" ) ; [ISSN OK] 6 ;; Filter (issn-verify "1654-9813" ) ; [ISSN OK] 3 ;; Linux Pro (issn-verify "1471-5679" ) ; Incorrect ISSN, correct check digit is 9 ;; National Geographic (issn-verify "2535-41241") ; Malformed ISSN ;; Outside (issn-verify "0278-433" ) ; Malformed ISSN ;; The Economist (issn-verify "0013-06a4" ) ; Malformed ISSN ;; The Ring (issn-verify "0035x5410" ) ; Malformed ISSN (provide 'issn-verify) ;;; issn-verify.el ends here