Skip to content

Instantly share code, notes, and snippets.

@jidaikobo-shibata
Last active July 19, 2016 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jidaikobo-shibata/a20cd2d106edba225115 to your computer and use it in GitHub Desktop.
Save jidaikobo-shibata/a20cd2d106edba225115 to your computer and use it in GitHub Desktop.
Emacs(Elisp): create or update gist by using yagist. yagistでregionのgistをupdateする。
;;; ------------------------------------------------------------
;; gist-description: Emacs(Elisp): create or update gist by using yagist. yagistでregionのgistをupdateする。
;; gist-id: a20cd2d106edba225115
;; gist-name: yagist-region-create-or-update.el
;; gist-private: nil
(require 'yagist)
(defun yagist-region-create-or-update (beg end)
"Post the current region as a create or update at gist.github.com.
If gist-id exists update gist. BEG END."
(interactive "r")
(let* ((raw (buffer-substring-no-properties beg end))
(lines (split-string raw "\n"))
(description nil)
(id nil)
(name nil)
(private nil))
;; attributes
;; use concat to avoide synonym :-(
(while lines
(cond ((and (string-match (concat "gist" "-description: ") (car lines))
(not description))
(setq description (substring (car lines) (match-end 0))))
((and (string-match (concat "gist" "-id: ") (car lines))
(not id))
(setq id (substring (car lines) (match-end 0))))
((and (string-match (concat "gist" "-name: ") (car lines))
(not name))
(setq name (substring (car lines) (match-end 0))))
((and (string-match (concat "gist" "-private: ") (car lines))
(not private))
(setq private (substring (car lines) (match-end 0)))))
(setq lines (cdr lines)))
;; tab to space
(when raw
(setq raw (replace-regexp-in-string "\t" " " raw)))
;; update
(when (and id name raw description)
(yagist-request
"PATCH"
(format "https://api.github.com/gists/%s" id)
(yagist-simple-receiver "Update")
`(("description" . ,description)
("files" . ((,name . (("content" . ,raw))))))))
;; create
(if (and name raw description (not id))
(progn (yagist-request
"POST"
"https://api.github.com/gists"
'yagist-created-callback
`(("description" . ,description)
("public" . ,(if private :json-false 't))
("files" . ((,name . (("content" . ,raw))))))))
(error "Lack of parameters"))))
(global-set-key (kbd "C-M-g") 'yagist-region-create-or-update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment