aboutsummaryrefslogtreecommitdiffstats
path: root/hypo.el
blob: 5f179b9413a287f9fdc026c9abd800fea58c5c03 (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
;;; hypo.el --- Client functions for hypo            -*- lexical-binding: t; -*-

;; Copyright (C) 2013  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: tools
;; Version: 0.2.0

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

;; 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:

;; Some functions to interact with hypo
;; (http://projects.ryuslash.org/hypo/).

;;; Code:

(require 'url)

(autoload 'beginning-of-sexp "thingatpt")
(autoload 'end-of-sexp "thingatpt")

(defgroup hypo nil
  "Customization options for `hypo'."
  :group 'convenience)

(defcustom hypo-instance-url "https://ryuslash.org/hypo"
  "URL of the hypo instance to communicate with."
  :group 'hypo
  :risky t
  :type 'string)

(defvar hypo--last-post nil
  "The hash of the last snippet sent to hypo.

Will be used for certain commands that operate on the last thing
sent to hypo.")

(defun hypo--collect-url ()
  "Collect the returned url."
  (let* ((start (search-forward "\n\n"))
         (end (1- (search-forward "\n"))))
    (setq hypo--last-post (buffer-substring-no-properties (- end 7) end))
    (copy-region-as-kill start end)
    (message "Copied %s to kill-ring"
             (buffer-substring-no-properties start end))))

(defun hypo--collect-and-kill (status)
  "Collect the returned url and kill the current buffer.

STATUS is ignored."
  (ignore status)
  (hypo--collect-url)
  (kill-buffer))

;;;###autoload
(defun hypo-region (start end filename)
  "Send the region START up to END to hypo as FILENAME."
  (interactive
   (list (region-beginning) (region-end)
         (let ((fname (buffer-file-name)))
           (if fname
               (concat
                "region-" (file-name-nondirectory (buffer-file-name)))
             (read-string "Filename: " (buffer-name))))))
  (let ((url-request-data (buffer-substring-no-properties start end))
        (url-request-method "PUT"))
    (url-retrieve (format "%s/%s" hypo-instance-url filename)
                  #'hypo--collect-and-kill)))

;;;###autoload
(defun hypo-buffer (buffer filename)
  "Send BUFFER to hypo as FILENAME."
  (interactive
   (list (current-buffer)
         (let ((fname (buffer-file-name)))
           (if fname
               (concat (file-name-nondirectory (buffer-file-name)))
             (read-string "Filename: " (buffer-name))))))
  (with-current-buffer buffer
    (hypo-region (point-min) (point-max) filename)))

;;;###autoload
(defun hypo-defun (filename)
  "Send the function at point to hypo as FILENAME."
  (interactive (list (or (concat (add-log-current-defun)
                                 (file-name-extension
                                  (or (buffer-file-name) "") t))
                         (read-string "Filename: " (buffer-name)))))
  (save-excursion
   (hypo-region (progn (beginning-of-defun) (point))
                (progn (end-of-defun) (point))
                filename)))

;;;###autoload
(defun hypo-last-sexp (filename)
  "Send the sexp at point to hypo as FILENAME."
  (interactive (list (read-string
                      "Filename: "
                      (cons (file-name-extension
                             (or (buffer-file-name) "") t) 0))))
  (save-excursion
    (hypo-region (progn (beginning-of-sexp) (point))
                 (progn (end-of-sexp) (point))
                 filename)))

(defun hypo-delete-last ()
  "Delete the last thing sent to hypo."
  (interactive)
  (unless hypo--last-post (error "Nothing posted this session"))
  (let ((url-request-method "DELETE")
        (url (format "%s/%s" hypo-instance-url hypo--last-post)))
    (url-retrieve url (lambda (status) (ignore status) (kill-buffer)))
    (setq hypo--last-post nil)
    (message "Deleted %s" url)))

(defun hypo--file-shower (filename)
  "Make a function to show file as FILENAME."
  (lambda (status)
    (ignore status)
    (let* ((start (1+ (marker-position url-http-end-of-headers)))
           (end (point-max))
           (contents (buffer-substring-no-properties start end))
           (buffer (generate-new-buffer filename)))
      (with-current-buffer buffer
        (insert contents)
        (write-file (concat "/tmp/" filename))
        (goto-char (point-min)))
      (kill-buffer)
      (set-window-buffer nil buffer))))

;;;###autoload
(defun hypo-get-file (hash filename)
  "Get the file HASH/FILENAME from your hypo instance."
  (interactive "MHash: \nMFile: ")
  (let ((url (format "%s/raw/%s/%s" hypo-instance-url hash filename)))
    (url-retrieve url (hypo--file-shower filename))))

(provide 'hypo)
;;; hypo.el ends here