;;; -*- lexical-binding: t -*- ;;; ;;; this file: ;;; http://user.it.uu.se/~embe8573/emacs-init/my-bibtex.el ;;; https://dataswamp.org/~incal/emacs-init/my-bibtex.el (require 'bibtex) (require 'cl-lib) (require 'isbn-verify) (require 'issn-verify) (require 'map) (require 'mode-line) (require 'my-string) (require 'super) (setq bibtex-comment-start "%") (setq bibtex-help-message nil) ;; autokey (setq bibtex-autokey-edit-before-use nil) (setq bibtex-autokey-titleword-ignore '("A" "An" "The" "(.*)")) (setq bibtex-autokey-titleword-length nil) (setq bibtex-autokey-titleword-separator "-") (cl-loop for (r w) in `(("ä" "a") ("Ä" "A") ("å" "a") ("Å" "A") ("é" "e") ("ö" "o") ("Ö" "O") ("ń" "n") ("ś" "s") ) do (add-to-list 'bibtex-autokey-titleword-change-strings (cons r w)) ) (defun bibtex-insert-autokey-check-isbn () (interactive) (let*((isbn-str "isbn") (isbn-10-str "isbn-10") (isbn-13-str "isbn-13") (issn-str "issn") (vals (list (cons issn-str (bibtex-autokey-get-field issn-str)) (cons isbn-str (bibtex-autokey-get-field isbn-str)) (cons isbn-10-str (bibtex-autokey-get-field isbn-10-str)) (cons isbn-13-str (bibtex-autokey-get-field isbn-13-str)) )) (issn (alist-get issn-str vals)) (isbn (alist-get isbn-str vals)) (isbn-10 (alist-get isbn-10-str vals)) (isbn-13 (alist-get isbn-13-str vals)) ) (unless (empty-string-p issn) (issn-verify issn)) (unless (empty-string-p isbn) (isbn-verify isbn)) (unless (empty-string-p isbn-10) (isbn-verify isbn-10)) (unless (empty-string-p isbn-13) (isbn-verify isbn-13)) (bibtex-insert-autokey) )) (defalias 'bibtex-insert-autokey-check-issn #'bibtex-insert-autokey-check-isbn) (defun bibtex-insert-autokey () (re-search-forward "^}") (let ((title (bibtex-autokey-get-title))) (bibtex-beginning-of-entry) (search-forward "{") (insert title) (kill-line) (insert ",") )) ;; field (defun bibtex-prev-field () (interactive) (cl-labels ((search-prev () (when (re-search-backward "=\\|@" nil t) (let ((eol (line-end-position))) (unless (re-search-forward "{" eol t) (forward-char 2) ))) (point) )) (when (= (point) (search-prev)) (let ((start (point))) (beginning-of-line) (when (= (point) (search-prev)) (goto-char start) ))))) (defun re-search-forward-return (regexp) (save-excursion (re-search-forward regexp (point-max) t) )) (defun bibtex-beginning-of-next-field () (interactive) (cl-labels ((advance (to) (goto-char to) (if (looking-at "[[:space:]]*{") (goto-char (match-end 0)) (forward-char 1) ))) (let*((next-curly (re-search-forward-return "{")) (next-equal (re-search-forward-return "=")) ) (if (and next-curly next-equal) (if (< next-curly next-equal) (goto-char next-curly) (advance next-equal) ) (if next-equal (advance next-equal) (if next-curly (goto-char next-curly)) ))))) ;; book (defun create-book (&optional title author publisher year isbn) "Insert a Bibtex book at point. \nOptional insert TITLE, AUTHOR, PUBLISHER, YEAR, and ISBN. \nTo insert an entry with blank field, use \\[new-book]" (interactive "stitle: \nsauthor: \nspublisher: \nsyear: \nsISBN: ") (let*((bib-str-nils (format "@book{,\n author = {%s},\n isbn = {%s},\n publisher = {%s},\n title = {%s},\n year = {%s}\n}" author isbn publisher title year)) (bib-str (replace-regexp-in-string "{nil}" "{}" bib-str-nils)) ) (beginning-of-line) (let ((start (point))) (insert bib-str) (goto-char (+ start 24) )))) ; point at first field (defun new-book () "Insert a Bibtex book, with blank field, at point. \nsee `create-book'" (interactive) (create-book) ) (defun validate-compile-count () (interactive) (when (bibtex-validate t) (let ((num-books (+ (how-many "^@book" (point-min)) (how-many "^@periodical" (point-min)) ))) (message "%s" num-books) (sleep-for 2) (save-window-excursion (compile "make") )))) (defun set-biblatex-keys () (let ((the-map bibtex-mode-map)) (define-key the-map "\C-\M-n" #'bibtex-next-entry) (define-key the-map "\C-\M-p" #'bibtex-previous-entry) (define-key the-map [backtab] #'bibtex-prev-field) (define-key the-map "\t" #'bibtex-beginning-of-next-field) (define-key the-map "\M-n" #'bibtex-insert-autokey-check-isbn) (define-key the-map "\C-c\C-c" #'validate-compile-count) )) ;; (setq bibtex-mode-hook nil) (defun bibtex-mode-hook-f () (disable-super-global-keys bibtex-mode-map) (set-biblatex-keys) (lines) ) (add-hook 'bibtex-mode-hook #'bibtex-mode-hook-f)