forked from elsatch/org-sync
-
Notifications
You must be signed in to change notification settings - Fork 6
/
os-util.el
89 lines (73 loc) · 3.31 KB
/
os-util.el
1
2
3
4
5
6
7
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;;; os-utils.el --- Utility functions for org-sync.
;; Copyright (C) 2013 Albert Krewinkel
;;
;; Author: Albert Krewinkel <tarleb+org-sync@moltkeplatz.de>
;; Keywords: org, synchronization, json
;; Homepage: http://orgmode.org/worg/org-contrib/gsoc2012/student-projects/org-sync
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; This file is not part of GNU Emacs.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides utility functions for org-sync.
;;; Code:
(eval-when-compile (require 'cl))
(require 'url)
(require 'json)
(defvar org-sync-util-default-encoding 'utf-8
"Default encoding expected in server responses.")
(defvar org-sync-util-debug nil
"If non-nil, show debug messages.")
(defun org-sync-util-get-coding-system (encoding)
"Find the coding system specified by `encoding' or nil if none is found.
This method accepts either a string or a symbol, the case of
which is ignored."
(let ((coding-system (cl-assoc encoding coding-system-alist :test 'equalp)))
(if coding-system
(intern (car coding-system))
nil)))
(defun org-sync-util-get-response-encoding (&optional bound)
"Get encoding charset from a http response header."
(save-excursion
(goto-char (point-min))
(let ((noerror t)
(content-type-regex
"^Content-Type: application/json;? *\\(charset=\\(.*\\)\\b\\)?$"))
(when (<= 0 (search-forward-regexp content-type-regex bound noerror))
(org-sync-util-get-coding-system (match-string 2))))))
(defun org-sync-util-ensure-correct-encoding (end-of-headers)
"Make sure that the data returned by a server is interpreted in
the right encoding."
(let* ((resp-encoding (org-sync-util-get-response-encoding end-of-headers))
(new-encoding (or resp-encoding
org-sync-util-default-encoding))
(old-encoding buffer-file-coding-system))
(unless (coding-system-equal new-encoding old-encoding)
(recode-region end-of-headers (point-max) new-encoding old-encoding))))
(defun org-sync-util-read-json-from-response-buffer (buffer &optional keep-buffer)
"Get JSON data from `buffer', then kill the buffer unless
`keep-buffer' is non-nil."
(with-current-buffer buffer
(toggle-enable-multibyte-characters 1)
(org-sync-util-ensure-correct-encoding url-http-end-of-headers)
(goto-char url-http-end-of-headers)
(when org-sync-util-debug
(message "%s" (buffer-substring (point) (point-max))))
(prog1
(cons url-http-response-status (ignore-errors (json-read)))
(unless keep-buffer
(kill-buffer)))))
(defun org-sync-util-read-json-from-url (url)
"Get JSON data from url"
(setq buffer (url-retrieve-synchronously url))
(org-sync-util-read-json-from-response-buffer buffer))
(provide 'os-util)
;; os-util.el ends here