vic-bib-functions.el 4.1 KB
Newer Older
Victor A. Stoichiţă's avatar
Victor A. Stoichiţă committed
1
;; Helper functions useful for bibliography management
2

Victor A. Stoichiţă's avatar
Victor A. Stoichiţă committed
3
;; From Xah Lee : remove ascii chars from bibtex keys
4
5
6
7
(defun xah-asciify-text (&optional @begin @end)
  "Remove accents in some letters and some
Change European language characters into equivalent ASCII ones, e.g. “café” ⇒ “cafe”.
When called interactively, work on current line or text selection.
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
URL `http://ergoemacs.org/emacs/emacs_zap_gremlins.html'
Version 2018-11-12"
  (interactive)
  (let (($charMap
         [
          ["ß" "ss"]
          ["á\\|à\\|â\\|ä\\|ā\\|ǎ\\|ã\\|å\\|ą\\|ă\\|ạ\\|ả\\|ả\\|ấ\\|ầ\\|ẩ\\|ẫ\\|ậ\\|ắ\\|ằ\\|ẳ\\|ặ" "a"]
          ["æ" "ae"]
          ["ç\\|č\\|ć" "c"]
          ["é\\|è\\|ê\\|ë\\|ē\\|ě\\|ę\\|ẹ\\|ẻ\\|ẽ\\|ế\\|ề\\|ể\\|ễ\\|ệ" "e"]
          ["í\\|ì\\|î\\|ï\\|ī\\|ǐ\\|ỉ\\|ị" "i"]
          ["ñ\\|ň\\|ń" "n"]
          ["ó\\|ò\\|ô\\|ö\\|õ\\|ǒ\\|ø\\|ō\\|ồ\\|ơ\\|ọ\\|ỏ\\|ố\\|ổ\\|ỗ\\|ộ\\|ớ\\|ờ\\|ở\\|ợ" "o"]
          ["ú\\|ù\\|û\\|ü\\|ū\\|ũ\\|ư\\|ụ\\|ủ\\|ứ\\|ừ\\|ử\\|ữ\\|ự"     "u"]
          ["ý\\|ÿ\\|ỳ\\|ỷ\\|ỹ"     "y"]
          ["þ" "th"]
          ["ď\\|ð\\|đ" "d"]
          ["ĩ" "i"]
          ["ľ\\|ĺ\\|ł" "l"]
          ["ř\\|ŕ" "r"]
          ["š\\|ş\\|ś" "s"]
          ["ť\\|ţ" "t"]
          ["ž\\|ź\\|ż" "z"]
          [" " " "]       ; thin space etc
          ["–" "-"]       ; dash
          ["—\\|一" "--"] ; em dash etc
          ])
        $begin $end
        )
    (if (null @begin)
        (if (use-region-p)
            (setq $begin (region-beginning) $end (region-end))
          (setq $begin (line-beginning-position) $end (line-end-position)))
      (setq $begin @begin $end @end))
    (let ((case-fold-search t))
      (save-restriction
        (narrow-to-region $begin $end)
        (mapc
         (lambda ($pair)
           (goto-char (point-min))
           (while (search-forward-regexp (elt $pair 0) (point-max) t)
             (replace-match (elt $pair 1))))
         $charMap)))))

(defun xah-asciify-string (@string)
  "Returns a new string. European language chars are changed ot ASCII ones e.g. “café” ⇒ “cafe”.
See `xah-asciify-text'
Version 2015-06-08"
  (with-temp-buffer
      (insert @string)
      (xah-asciify-text (point-min) (point-max))
      (buffer-string)))
Victor A. Stoichiţă's avatar
Victor A. Stoichiţă committed
61
62
63
64
65
66
67
68
69



;; Debug autokey generation in bibtex buffers 
;; (defun vic/bibtex-generate-autokey ()
;;   (interactive)
;;   (message (bibtex-generate-autokey)))

(defun vic/ebib-create-org-noter-id (key db)
70
71
  "Create an org-noter property linking to the first pdf file in entry KEY in DB. If an annotated file exists according to `vic/pdf-annotated-filename-regexp' it is preferred. If `file' is empty or has no pdf file, return the empty string."
  (let ((files-with-props (ebib-get-field-value "file" key db 'noerror 'unbraced 'xref))
72
	(noter-property org-noter-property-doc-file) 
Victor A. Stoichiţă's avatar
Victor A. Stoichiţă committed
73
74
75
76
77
78
79
80
81
82
	(pdf-ext "\.pdf$")
	(annotated vic/pdf-annotated-filename-regexp)
	(file))
    (when files-with-props
      (setq files (ebib--split-files (substring-no-properties files-with-props)))
      ;; Try to select 1st annotated file aomngst all pdf files
      (setq file (cl-find annotated (cl-member pdf-ext files :test #'string-match-p) :test #'string-match-p))
      (unless file
	;; Default to first pdf file if no annotated file found
	(setq file (cl-find pdf-ext files :test #'string-match-p))))
83
84
85
86
    (if file
	;; insert newline here instead of `ebib-notes-template'
	(format "\n:%s: %s" noter-property (ebib--expand-file-name file))
      "")))
Victor A. Stoichiţă's avatar
Victor A. Stoichiţă committed
87

88
89
90
91
92
93
94
(defun vic/ebib-create-note-tags (key db)
  "Return orgmode tags based on current entry's Keywords. If field empty return the empty string."
  (let ((keywords (ebib-get-field-value "keywords" key db 'noerror 'unbraced 'xref)))
    (if keywords
	;; space before!
	(format " :%s:" (replace-regexp-in-string " " "_" (replace-regexp-in-string ", " "\:" keywords)))
      "")))
95
96


Victor A. Stoichiţă's avatar
Victor A. Stoichiţă committed
97
98
99
100
101
102
103
104
;; (defun vic/test-ebib-create-org-noter-id ()
;;   (interactive)
;;   (let ((db ebib--cur-db)
;; 	(key (ebib--get-key-at-point)))
;;   (message (vic/ebib-create-org-noter-id key db))))


(provide 'vic-bib-functions)