summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/ediff-url.el
blob: 12329bdc690dd48fb47a97644b9569fb6e822a36 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;;; ediff-url.el --- Diffing buffer against downloaded url
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: Sat Nov 24 2007
;; Version: 0.56
;; Last-Updated: 2010-03-18 Thu
;; URL: http://bazaar.launchpad.net/~nxhtml/nxhtml/main/annotate/head%3A/util/ediff-url.el
;;
;; Features that might be required by this library:
;;
  ;; `mail-prsvr', `mm-util', `timer', `url-parse', `url-util',
  ;; `url-vars'.
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; This file contains a simple function, `ediff-url', to help you
;; update a single file from the web.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 2, 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.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(require 'url-util)
(eval-when-compile (require 'cl))

(defvar ediff-url-read-url-history nil)

(defun ediff-url-redir-launchpad (url)
  "Check if bazaar list page on Launchpad.
If URL is a description page for a file uploaded to EmacsWiki
suggest to use the download URL instead."
  (let* ((bazaar-url "http://bazaar.launchpad.net/")
         (bazaar-len (length bazaar-url)))
    (if (and (< bazaar-len (length url))
             (string= bazaar-url (substring url 0 bazaar-len)))
        (let* ((url-show-status nil) ;; just annoying showing status here
               (buffer (url-retrieve-synchronously url))
               (handle nil)
               (http-status nil)
               ;; Fix-me: better more flexible pattern?
               (dl-patt "<a href=\"\\(.*?\\)\">download file</a>")
               dl-url)
          (unless buffer
            (message "Got empty buffer for %s" url)
            (throw 'command-level nil))
          (with-current-buffer buffer
            (if (= 0 (buffer-size))
                (progn
                  (message "Got empty page for %s" url)
                  (throw 'command-level nil))
              (require 'url-http)
              (setq http-status (url-http-parse-response))
              (if (memq http-status '(200 201))
                  (progn
                    (goto-char (point-min))
                    (unless (search-forward "\n\n" nil t)
                      (error "Could not find header end in buffer for %s" url))
                    (unless (re-search-forward dl-patt nil t)
                      (error "Could not find download link"))
                    (setq dl-url (match-string 1))
                    (set-buffer-modified-p nil)
                    (kill-buffer buffer)
                    dl-url)
                (kill-buffer buffer)
                (setq buffer nil)
                (setq http-status
                      (concat (number-to-string http-status)
                              (case http-status
                                (401 " (unauthorized)")
                                (403 " (forbidden)")
                                (404 " (not found)")
                                (408 " (request timeout)")
                                (410 " (gone)")
                                (500 " (internal server error)")
                                (503 " (service unavailable)")
                                (504 " (gateway timeout)")
                                (530 " (user access denied)")
                                )))
                (message "Got status %s for %s" http-status url)
                (throw 'command-level nil)))))
      url)))

(defun ediff-url-redir-emacswiki-description-page (url)
  "Check if description page on EmacsWiki.
If URL is a description page for a file uploaded to EmacsWiki
suggest to use the download URL instead."
  ;;(let* ((desc-url "http://www.emacswiki.org/emacs/")
  (let* ((emacswiki-url "http://www.emacswiki.org/")
         (emacswiki-len (length emacswiki-url)))
    (if (and (< emacswiki-len (length url))
             (string= emacswiki-url (substring url 0 emacswiki-len))
             (not (string-match-p "/download/" url)))
        (let ((prompt
               (concat "This seem to be the description page on EmacsWiki,"
                       "\n\tdo you want the download url instead? ")))
          (when (y-or-n-p prompt)
            ;;(let ((start (+ 6 (string-match "/wiki/" url))))
            (let ((start (+ 7 (string-match "/emacs/" url))))
              (concat (substring url 0 start)
                                "download/"
                                (substring url start)))))
      ;; Not on the wiki, just return the url:
      url)))

(defcustom ediff-url-redirects '(ediff-url-redir-emacswiki-description-page
                                 ediff-url-redir-launchpad
                                 )
  "List of functions checking url given to `ediff-url'.
Each function should take an URL as argument and return this URL
or a new URL."
  :type '(repeat function)
  :group 'ediff)

;;;###autoload
(defun ediff-url (url)
  "Compare current buffer to a web URL using `ediff-buffers'.
Check URL using `ediff-url-redirects' before fetching the file.

This is for checking downloaded file.  A the file may have a comment
telling the download URL of thise form in the header:

   ;; URL: http://the-server.net/the-path/the-file.el

If not the user is asked for the URL."
  (interactive (let ((url-init (url-get-url-at-point)))
                 (unless url-init
                   (when (eq major-mode 'emacs-lisp-mode)
                     (save-excursion
                       (goto-char (point-min))
                       (when (re-search-forward "URL:[ \t]*" nil t)
                         (setq url-init (url-get-url-at-point))))))
                 (list (read-from-minibuffer "Url for download file: "
                                             (cons (or url-init "") 1) ;nil
                                             nil nil
                                             'ediff-url-read-url-history
                                             ;;url-init
                                             ))))
  (catch 'command-level ;; Fix-me: remove and let go to top later
    (unless (> (length url) 0)
      (message "No URL given, aborted by user")
      (throw 'command-level nil))
    ;; Check if URL seems reasonable
    (dolist (fun ediff-url-redirects)
      (setq url (funcall fun url)))
    ;; Fetch URL and run ediff
    (let* ((url-buf-name (concat "URL=" url))
           (url-buf (get-buffer url-buf-name)))
      (when url-buf
        (unless (y-or-n-p "Use previously downloaded url? ")
          (kill-buffer url-buf)
          (setq url-buf nil)))
      (unless url-buf
        (setq url-buf (get-buffer-create url-buf-name))
        (let ((current-major major-mode))
          (with-current-buffer url-buf
            (url-insert-file-contents url)
            ;; Assume same modes:
            (funcall current-major))))
      (ediff-buffers url-buf (current-buffer)))))

(provide 'ediff-url)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ediff-url.el ends here