unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* the next volume of Berserk
@ 2021-09-04  4:00 Emanuel Berg via Users list for the GNU Emacs text editor
  2021-09-04  5:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 2+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-04  4:00 UTC (permalink / raw)
  To: help-gnu-emacs

You have to type a lot so that you don't have to type so
much!

The author/artist mentioned in the example, Kentaro Miura of
Japan, died in 2021, at only 54 BTW. So we should all take
a moment and think about that before we read and comment on
the following Elisp ...

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   https://dataswamp.org/~incal/emacs-init/bibtex/bibtex-next-volume.el
;;;
;;; uses:
;;;   https://dataswamp.org/~incal/emacs-init/bibtex-incal.el
;;;   https://dataswamp.org/~incal/emacs-init/isbn-verify.el
;;;
;;; To test, uncomment the below entry (named "berserk-23"),
;;; move point above it, and do
;;;
;;;   M-x next-book RET RET RET RET RET RET
;;;
;;; @book{berserk-23,
;;;   author     = {Kentaro Miura},
;;;   isbn       = {978-1-59307-864-5},
;;;   publisher  = {Dark Horse},
;;;   title      = {Berserk 23},
;;;   year       = {2008}
;;; }
;;;
;;; This will insert the following entry, which indeed is the
;;; next book in the series.
;;;
;;; @book{berserk-24,
;;;   author     = {Kentaro Miura},
;;;   isbn       = {978-1-59307-865-2},
;;;   publisher  = {Dark Horse},
;;;   title      = {Berserk 24},
;;;   year       = {2008}
;;; }

(require 'bibtex-incal)
(require 'isbn-verify)

(defun isbn-13-bibtex-get-isbn ()
  (let ((isbn-13 (bibtex-autokey-get-field "isbn-13")))
    (if (not (string= isbn-13 ""))
        isbn-13
      (let ((isbn (bibtex-autokey-get-field "isbn")))
        (unless (string= isbn "")
          isbn) ))))

(defun isbn-13-publisher (isbn)
  (when
    (string-match
     "97\\(8\\|9\\)-[[:digit:]]*-\\([[:digit:]]*\\)-[[:digit:]]*-[[:digit:]]*"
     isbn)
    (let ((publisher (string-to-number (match-string 2 isbn))))
      (read-number "Publisher: " publisher) )))
;; (isbn-13-publisher "978-1-59307-864-5") ; 59307

(defun isbn-13-publication-next (isbn)
  (when
    (string-match
     "97\\(8\\|9\\)-[[:digit:]]*-[[:digit:]]*-\\([[:digit:]]*\\)-[[:digit:]]*"
     isbn)
    (let*((publication      (string-to-number (match-string 2 isbn)))
          (publication-next (1+ publication)) )
      (read-number "Publication: " publication-next) )))
;; (isbn-13-publication-next "978-1-59307-864-5") ; 865

(defun isbn-13-next (isbn)
  (when (string-match "\\(97\\(8\\|9\\)-[[:digit:]]*\\).*" isbn)
    (let*((but-last    (format "%s-%s-%s" (match-string 1 isbn)
                               (isbn-13-publisher isbn)
                               (isbn-13-publication-next isbn) ))
          (check-digit (read-number "Check digit: " (isbn-verify but-last))) )
      (format "%s-%s" but-last check-digit) )))
;; (isbn-13-next "978-1-59307-864-5")

(defun title-next (title)
  (when (string-match "\\(.+\\) \\([[:digit:]]+\\)" title)
    (let*((name     (match-string 1 title))
          (vol      (match-string 2 title))
          (vol-next (read-number "Volume: " (1+ (string-to-number vol)))) )
      (format "%s %s" name vol-next) )))
;; (title-next "Berserk 23") ; Berserk 24

(defun new-book-next ()
  (interactive)
  (let ((beg (point)))
    (when (bibtex-next-entry)
      (let ((author    (bibtex-autokey-get-field "author"))
            (isbn      (isbn-13-bibtex-get-isbn))
            (publisher (bibtex-autokey-get-field "publisher"))
            (title     (bibtex-autokey-get-field "title"))
            (year      (string-to-number
                        (bibtex-autokey-get-field "year") )))
        (unless (and author isbn publisher title year)
          (error "Incomplete Bibtex entry") )
        (let*((isbn-new  (isbn-13-next isbn))
              (title-new (title-next title))
              (year-new  (read-number "Year: " year)) )
          (goto-char beg)
          (create-book author isbn-new publisher title-new year-new)
          (bibtex-insert-autokey-check-isbn)
          (goto-char beg) )))))
(defalias 'next-book #'new-book-next)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: the next volume of Berserk
  2021-09-04  4:00 the next volume of Berserk Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-09-04  5:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 2+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-04  5:44 UTC (permalink / raw)
  To: help-gnu-emacs

I wrote a better version - I think - by getting away with the
string match stuff (almost) and instead unified the retrieval
from the ISBN-13 string ... keep it real.

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   https://dataswamp.org/~incal/emacs-init/bibtex/bibtex-next-volume.el
;;;
;;; uses:
;;;   https://dataswamp.org/~incal/emacs-init/bibtex-incal.el
;;;   https://dataswamp.org/~incal/emacs-init/isbn-verify.el
;;;
;;; To test, uncomment the below entry (named "berserk-23"),
;;; move point above it, and do
;;;
;;;   M-x next-book RET RET RET RET RET RET
;;;
;;; @book{berserk-23,
;;;   author     = {Kentaro Miura},
;;;   isbn       = {978-1-59307-864-5},
;;;   publisher  = {Dark Horse},
;;;   title      = {Berserk 23},
;;;   year       = {2008}
;;; }
;;;
;;; This will insert the following entry, which indeed is the
;;; next book in the series:
;;;
;;; @book{berserk-24,
;;;   author     = {Kentaro Miura},
;;;   isbn       = {978-1-59307-865-2},
;;;   publisher  = {Dark Horse},
;;;   title      = {Berserk 24},
;;;   year       = {2008}
;;; }

(require 'cl-lib)
(require 'bibtex)
(require 'bibtex-incal)
(require 'isbn-verify)

(defun title-next (title)
  (when (string-match "\\(.+\\) \\([[:digit:]]+\\)" title)
    (let*((name     (match-string 1 title))
          (vol      (match-string 2 title))
          (vol-next (read-number "Volume: " (1+ (string-to-number vol)))) )
      (format "%s %s" name vol-next) )))
;; (title-next "Berserk 23") ; Berserk 24

(defun isbn-13-bibtex-entry-get-isbn ()
  (let ((isbn-13 (bibtex-autokey-get-field "isbn-13")))
    (if (not (string= isbn-13 ""))
        isbn-13
      (let ((isbn (bibtex-autokey-get-field "isbn")))
        (unless (string= isbn "")
          isbn) ))))

(defun isbn-13-field (isbn field &optional field-stop)
  (let*((beg  (1- field))
        (end  (or field-stop field))
        (sep  "-")
        (subl (cl-subseq (split-string isbn sep) beg end)) )
    (if (= 1 (length subl))
        (string-to-number (car subl))
      (string-join subl sep) )))
;; (isbn-13-field "978-1-59307-864-5" 1)   ; 978
;; (isbn-13-field "978-1-59307-864-5" 1 4) ; 978-1-59307-864

(defun isbn-13-publisher (isbn)
  (let ((publisher (isbn-13-field isbn 3)))
    (read-number "Publisher: " publisher) ))
;; (isbn-13-publisher "978-1-59307-864-5") ; 59307

(defun isbn-13-publication-next (isbn)
  (let*((publication      (isbn-13-field isbn 4))
        (publication-next (1+ publication)) )
    (read-number "Publication: " publication-next) ))
;; (isbn-13-publication-next "978-1-59307-864-5") ; 865

(defun isbn-13-next (isbn)
  (let*((but-last    (format "%s-%s-%s" (isbn-13-field isbn 1 2)
                                        (isbn-13-publisher isbn)
                                        (isbn-13-publication-next isbn) ))
        (check-digit (read-number "Check digit: " (isbn-verify but-last))) )
    (format "%s-%s" but-last check-digit) ))
;; (isbn-13-next "978-1-59307-864-5") ; 978-1-59307-865-2

(defun new-book-next ()
  (interactive)
  (let ((beg (point)))
    (when (bibtex-next-entry)
      (let ((author    (bibtex-autokey-get-field "author"))
            (isbn      (isbn-13-bibtex-entry-get-isbn))
            (publisher (bibtex-autokey-get-field "publisher"))
            (title     (bibtex-autokey-get-field "title"))
            (year      (string-to-number
                        (bibtex-autokey-get-field "year") )))
        (unless (and author isbn publisher title year)
          (error "Incomplete Bibtex entry") )
        (let*((isbn-new  (isbn-13-next isbn))
              (title-new (title-next title))
              (year-new  (read-number "Year: " year)) )
          (goto-char beg)
          (create-book author isbn-new publisher title-new year-new)
          (bibtex-insert-autokey-check-isbn)
          (goto-char beg) )))))
(defalias 'next-book #'new-book-next)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-04  5:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-04  4:00 the next volume of Berserk Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-04  5:44 ` Emanuel Berg via Users list for the GNU Emacs text editor

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).