Created
February 12, 2013 02:12
-
-
Save handlename/4759570 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; np.el --- post region to no-paste | |
;; Copyright (C) 2012-2013 NAGATA Hiroaki (handlename) | |
;;; Commentary: | |
;; (defvar np:endpoint "API ENDPOINT") | |
;; (defvar np:username "USERNAME") | |
;; (defvar np:password "PASSWORD") | |
;; (global-set-key (kbd "C-c C-p") 'np:post-region) | |
(require 'url-http) | |
(defvar np:endpoint nil) | |
(defvar np:username nil) | |
(defvar np:password nil) | |
(defvar np:curl-command "curl") | |
(defvar np:curl-output-buffer-name "*np:curl-output*") | |
(defun np:curl-post (text &optional summary channel) | |
(call-process np:curl-command nil np:curl-output-buffer-name nil | |
"-v" | |
np:endpoint | |
"--basic" "--user" (concat np:username ":" np:password) | |
"-d" (concat "text=" (url-hexify-string text)) | |
(if summary | |
(concat "-d" "summary=" summary)) | |
(if channel | |
(concat "-d" "channel=" channel)))) | |
(defun np:extract-uid (string) | |
(if (string-match "< Location: /np/\\([0-9a-z]+\\)" string) | |
(match-string 1 string) | |
nil)) | |
(defun np:open-in-browser (url) | |
(call-process "open" nil nil nil url)) | |
(defun np:post (text &optional summary channel) | |
(switch-to-buffer-other-window | |
(get-buffer-create np:curl-output-buffer-name)) | |
(erase-buffer) | |
(np:curl-post text summary channel) | |
(let ((path (np:extract-uid (buffer-string)))) | |
(message path) | |
(np:open-in-browser (concat np:endpoint "/" path))) | |
(kill-buffer np:curl-output-buffer-name)) | |
(defun np:post-region (summary channel start end) | |
(interactive "ssummary: \nschannel: \nr") | |
(let ((text (buffer-substring start end))) | |
(np:post text summary channel))) | |
(provide 'np) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment