2017-03-01 16:59:33 +00:00
|
|
|
(require 'json)
|
2017-03-03 01:10:16 +00:00
|
|
|
(require 'cl)
|
2017-03-01 16:59:33 +00:00
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defvar bbj-host "localhost")
|
|
|
|
(defvar bbj-port "7066")
|
|
|
|
(defvar bbj-width 80)
|
2017-03-03 01:10:16 +00:00
|
|
|
|
2017-03-03 02:28:13 +00:00
|
|
|
;; blah blah user servicable parts blah blaheiu hre ;;;;;;;;;;r;r;r;r;;;q;q;;;
|
2017-03-03 03:01:01 +00:00
|
|
|
(defvar bbj-old-p (eq emacs-major-version 24))
|
|
|
|
(defvar bbj-logged-in nil)
|
|
|
|
(defvar bbj-user nil)
|
|
|
|
(defvar bbj-hash nil)
|
2017-03-03 01:10:16 +00:00
|
|
|
(make-variable-buffer-local
|
2017-03-03 03:01:01 +00:00
|
|
|
(defvar bbj-*usermap* nil))
|
2017-03-03 01:10:16 +00:00
|
|
|
(make-variable-buffer-local
|
2017-03-03 03:01:01 +00:00
|
|
|
(defvar bbj-buffer-type nil))
|
2017-03-01 21:54:56 +00:00
|
|
|
(make-variable-buffer-local
|
2017-03-03 03:01:01 +00:00
|
|
|
(defvar bbj-aux-callback #'ignore))
|
2017-03-01 21:54:56 +00:00
|
|
|
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
(define-derived-mode bbj-mode fundamental-mode "BBJ"
|
2017-03-01 21:54:56 +00:00
|
|
|
"Mode for browsing and posting to BBJ."
|
|
|
|
:group 'bbj-mode
|
2017-03-03 03:01:01 +00:00
|
|
|
(local-set-key (kbd "SPC") 'bbj-next-post)
|
|
|
|
(local-set-key (kbd "j") 'bbj-next-post)
|
|
|
|
(local-set-key (kbd "n") 'bbj-next-post)
|
|
|
|
(local-set-key (kbd "<down>") 'bbj-next-post)
|
2017-03-03 01:10:16 +00:00
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(local-set-key (kbd "DEL") 'bbj-prev-post)
|
|
|
|
(local-set-key (kbd "k") 'bbj-prev-post)
|
|
|
|
(local-set-key (kbd "p") 'bbj-prev-post)
|
|
|
|
(local-set-key (kbd "<up>") 'bbj-prev-post)
|
2017-03-03 01:10:16 +00:00
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(local-set-key (kbd "RET") 'bbj-enter)
|
|
|
|
(local-set-key (kbd "l") 'bbj-enter)
|
|
|
|
(local-set-key (kbd "o") 'bbj-enter)
|
|
|
|
(local-set-key (kbd "<right>") 'bbj-enter)
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
(local-set-key (kbd "q") 'quit-window)
|
|
|
|
(local-set-key (kbd "<left>") 'quit-window)
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(local-set-key (kbd "+") 'bbj-compose)
|
|
|
|
(local-set-key (kbd "c") 'bbj-compose)
|
2017-03-03 01:10:16 +00:00
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(local-set-key (kbd "C-c C-c") 'bbj-aux)
|
|
|
|
(local-set-key (kbd "r") 'bbj-quote-current-post))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
(ignore-errors
|
|
|
|
(evil-set-initial-state 'bbj-mode 'emacs))
|
|
|
|
|
2017-03-03 01:20:11 +00:00
|
|
|
;;;; shit to put up with outdated emacs on the tilde ;;;;
|
|
|
|
|
|
|
|
(when (eq emacs-major-version 24)
|
|
|
|
|
|
|
|
(defun alist-get (key alist &optional default remove)
|
|
|
|
(ignore remove) ;;Silence byte-compiler.
|
|
|
|
(let ((x (assq key alist)))
|
|
|
|
(if x (cdr x) default)))
|
|
|
|
|
|
|
|
(defsubst string-trim-left (string)
|
|
|
|
"Remove leading whitespace from STRING."
|
|
|
|
(if (string-match "\\`[ \t\n\r]+" string)
|
|
|
|
(replace-match "" t t string)
|
|
|
|
string))
|
|
|
|
|
|
|
|
(defsubst string-trim-right (string)
|
|
|
|
"Remove trailing whitespace from STRING."
|
|
|
|
(if (string-match "[ \t\n\r]+\\'" string)
|
|
|
|
(replace-match "" t t string)
|
|
|
|
string))
|
|
|
|
|
|
|
|
(defsubst string-trim (string)
|
|
|
|
"Remove leading and trailing whitespace from STRING."
|
|
|
|
(string-trim-left (string-trim-right string))))
|
|
|
|
|
2017-03-03 01:10:16 +00:00
|
|
|
;;;; network shit ;;;;
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-descend (alist &rest keys)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Recursively retrieve symbols from a nested alist. A required beverage
|
|
|
|
for all JSON tourists."
|
|
|
|
(while keys
|
|
|
|
(setq alist (alist-get (pop keys) alist)))
|
|
|
|
alist)
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-request (method &rest pairs)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Poke netcat to poke the server who will hopefully poke us back"
|
|
|
|
;; json-false/json-nil are bound as nil here to stop them from being silly keywords
|
|
|
|
(let (json message json-false json-null
|
|
|
|
(data (list
|
2017-03-03 03:01:01 +00:00
|
|
|
(cons 'user bbj-user)
|
|
|
|
(cons 'auth_hash bbj-hash)
|
2017-03-03 01:10:16 +00:00
|
|
|
(cons 'method method))))
|
|
|
|
;; populate a query with our hash and username, then the func arguments
|
|
|
|
(while pairs
|
|
|
|
(push (cons (pop pairs) (pop pairs)) data))
|
|
|
|
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert (json-encode data))
|
|
|
|
(call-process-region
|
|
|
|
(point-min) (point-max)
|
|
|
|
shell-file-name t t nil ;; meow meow
|
2017-03-03 03:01:01 +00:00
|
|
|
"-c" (format "nc %s %s" bbj-host bbj-port))
|
2017-03-03 01:10:16 +00:00
|
|
|
(setq json (progn (goto-char (point-min)) (json-read))))
|
|
|
|
|
|
|
|
(if (eq json t) t ;; a few enpoints just return true/false
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq message (bbj-descend json 'error 'description))
|
|
|
|
(case (bbj-descend json 'error 'code)
|
2017-03-03 01:10:16 +00:00
|
|
|
;; haha epic handling
|
2017-03-03 03:30:54 +00:00
|
|
|
(4 (user-error
|
|
|
|
(if bbj-logged-in message
|
|
|
|
"Not logged in. Call M-x bbj-login")))
|
|
|
|
((5 6 7) (user-error message))
|
2017-03-03 01:10:16 +00:00
|
|
|
(otherwise json)))))
|
2017-03-01 21:54:56 +00:00
|
|
|
|
2017-03-01 16:59:33 +00:00
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-sethash (&optional password)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Either prompt for or take the arg `PASSWORD', and then sha256-hash it.
|
|
|
|
Sets it globally and also returns it."
|
|
|
|
(interactive)
|
2017-03-01 21:54:56 +00:00
|
|
|
(unless password (setq password
|
|
|
|
(read-from-minibuffer "(Password)> ")))
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq bbj-hash (secure-hash 'sha256 password)))
|
2017-03-01 16:59:33 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-login ()
|
2017-03-03 01:10:16 +00:00
|
|
|
"Prompts the user for a name and password. If it isn't registered, we'll take
|
|
|
|
care of that. Jumps to the index afterward. This function only needs to be used
|
|
|
|
once per emacs session."
|
2017-03-01 16:59:33 +00:00
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq bbj-user (read-from-minibuffer "(BBJ Username)> "))
|
2017-03-01 21:54:56 +00:00
|
|
|
(cond
|
2017-03-03 03:01:01 +00:00
|
|
|
((bbj-request "is_registered" 'target_user bbj-user)
|
|
|
|
(bbj-sethash)
|
|
|
|
(if (bbj-request "check_auth")
|
2017-03-03 01:10:16 +00:00
|
|
|
(progn
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq bbj-logged-in t)
|
|
|
|
(bbj-browse-index)
|
|
|
|
(message "Logged in as %s!" bbj-user))
|
2017-03-03 01:10:16 +00:00
|
|
|
(message "(Invalid Password!)")
|
2017-03-03 03:01:01 +00:00
|
|
|
(run-at-time 1 nil #'bbj-login)))
|
|
|
|
((y-or-n-p (format "Register for BBJ as %s? " bbj-user))
|
|
|
|
(bbj-sethash)
|
2017-03-03 01:10:16 +00:00
|
|
|
(let ((response
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-request "user_register"
|
2017-03-03 01:10:16 +00:00
|
|
|
;; need to add some cute prompts for these
|
|
|
|
'quip "" 'bio "")))
|
2017-03-01 21:54:56 +00:00
|
|
|
(if (alist-get 'error response)
|
|
|
|
(message "%s" (alist-get 'error response))
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq bbj-logged-in t)
|
|
|
|
(bbj-browse-index)
|
|
|
|
(message "Logged in as %s!" bbj-user))))))
|
2017-03-01 21:54:56 +00:00
|
|
|
|
|
|
|
|
2017-03-03 01:10:16 +00:00
|
|
|
;;;; user navigation shit. a LOT of user navigation shit. ;;;;
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-next-pos (string &optional regex prop backward group bound)
|
2017-03-03 01:10:16 +00:00
|
|
|
;; haha yes i ripped this from one of my other projects
|
|
|
|
"Takes a STRING and returns the char position of the beginning of its
|
|
|
|
next occurence from point in `current-buffer'. Returns nil if not found.
|
2017-03-03 03:01:01 +00:00
|
|
|
A simpler way to call this is to use `bbj-next-prop'.
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
When REGEX is non-nil, STRING is interpreted as a regular expression.
|
|
|
|
|
|
|
|
PROP, when non-nil, will only return matches if they have the corresponding
|
|
|
|
value for a property. This can either be a symbol or a cons cell. If it's
|
|
|
|
a symbol, the property key used is 'type. As a cons, The key and expected
|
|
|
|
value are given, eg '(type . end)
|
|
|
|
|
|
|
|
BACKWARD, when non-nil, does what it says on the tin.
|
|
|
|
|
|
|
|
When GROUP is non-nil and an integer, returns start pos of that match
|
|
|
|
group. When PROP is in effect, it checks property at this position instead
|
|
|
|
of 0.
|
|
|
|
|
|
|
|
BOUND can be a buffer position (integer) that the search will not exceed."
|
|
|
|
(save-excursion
|
|
|
|
(let ((search (if backward (if regex 're-search-backward 'search-backward)
|
|
|
|
(if regex 're-search-forward 'search-forward)))
|
|
|
|
(group (or group 0))
|
|
|
|
(propkey (if (consp prop) (car prop) 'type))
|
|
|
|
(propval (if (consp prop) (cdr prop) prop))
|
|
|
|
found)
|
|
|
|
(while (and (not found) (funcall search string bound t))
|
|
|
|
(if prop (setq found
|
|
|
|
(eql propval (get-char-property
|
|
|
|
(match-beginning group) propkey)))
|
|
|
|
(setq found t)))
|
|
|
|
(when found
|
|
|
|
(match-beginning group)))))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-next-prop (prop &optional backward bound)
|
|
|
|
"Like the `bbj-next-pos', but doesnt care about strings and
|
2017-03-03 01:10:16 +00:00
|
|
|
just hunts for a specific text property."
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-next-pos "." t prop backward nil bound))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-post-prop (prop &optional id)
|
2017-03-03 01:10:16 +00:00
|
|
|
"retrieve PROP from the current post. needs ID-seeking support"
|
|
|
|
(save-excursion
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-assert-post-start)
|
2017-03-03 01:10:16 +00:00
|
|
|
(get-char-property (point) prop)))
|
|
|
|
|
|
|
|
|
|
|
|
;; returns positions of the next head and ending seps, respectively
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-head-pos (&optional backward)
|
|
|
|
(bbj-next-prop 'head backward))
|
|
|
|
(defun bbj-sep-pos (&optional backward)
|
|
|
|
(bbj-next-prop 'end backward))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-assert-post-start ()
|
2017-03-03 01:10:16 +00:00
|
|
|
(unless (eql 'head (get-char-property (point) 'type))
|
2017-03-03 03:01:01 +00:00
|
|
|
(goto-char (bbj-head-pos t))))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-point-to-post (dir &optional nocenter)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Move the cursor from the head of one post to another, in (symbol) DIR"
|
|
|
|
(let ((check (case dir
|
2017-03-03 03:01:01 +00:00
|
|
|
(prev (bbj-head-pos t))
|
2017-03-03 01:10:16 +00:00
|
|
|
(next (save-excursion ;; or else point will stick
|
|
|
|
(while (eq 'head (get-char-property (point) 'type))
|
|
|
|
(goto-char (next-property-change (point))))
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-head-pos))))))
|
2017-03-03 01:10:16 +00:00
|
|
|
(when check
|
|
|
|
(goto-char check)
|
|
|
|
(back-to-indentation)
|
|
|
|
(unless nocenter (recenter 1)))))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-next-post ()
|
2017-03-03 01:10:16 +00:00
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-point-to-post 'next))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-prev-post ()
|
2017-03-03 01:10:16 +00:00
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-point-to-post 'prev))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-first-post ()
|
2017-03-03 01:10:16 +00:00
|
|
|
;; does interactive work like this? i never checked tbh
|
|
|
|
(interactive (push-mark))
|
2017-03-03 03:01:01 +00:00
|
|
|
(goto-char (+ 1 bbj-width (point-min))))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-aux ()
|
2017-03-03 01:10:16 +00:00
|
|
|
"just some random lazy callback shitty thing for C-c C-c"
|
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(funcall bbj-aux-callback))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-enter ()
|
2017-03-03 01:10:16 +00:00
|
|
|
"Handles the RETURN key (and other similar binds) depending on
|
|
|
|
content type. Currently only opens threads."
|
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(case bbj-buffer-type
|
2017-03-03 01:10:16 +00:00
|
|
|
(index
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-enter-thread
|
|
|
|
(alist-get 'thread_id (bbj-post-prop 'data))))))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-quote-current-post ()
|
2017-03-03 01:10:16 +00:00
|
|
|
"Pop a composer, and insert the post number at point as a quote."
|
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(case bbj-buffer-type
|
2017-03-03 01:10:16 +00:00
|
|
|
(thread
|
2017-03-03 03:01:01 +00:00
|
|
|
(let ((id (alist-get 'post_id (bbj-post-prop 'data))))
|
|
|
|
(bbj-compose)
|
2017-03-03 01:10:16 +00:00
|
|
|
(insert (format ">>%s\n\n" id))))
|
|
|
|
(index
|
|
|
|
;; recursion haha yes
|
|
|
|
(let ((buffer (current-buffer)))
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-enter)
|
2017-03-03 01:10:16 +00:00
|
|
|
(unless (equal buffer (current-buffer))
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-quote-current-post))))))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-compose ()
|
2017-03-03 01:10:16 +00:00
|
|
|
"Construct an appropriate callback to either create a thread or
|
|
|
|
reply to one. Pops a new window; window is killed and the message
|
|
|
|
is sent using C-c C-c."
|
|
|
|
(interactive)
|
2017-03-03 03:01:01 +00:00
|
|
|
(let ((params (case bbj-buffer-type
|
2017-03-03 01:10:16 +00:00
|
|
|
(index
|
|
|
|
`("Composing a new thread (C-c C-c to send)"
|
|
|
|
(lambda ()
|
2017-03-03 03:01:01 +00:00
|
|
|
(let* ((message (bbj-consume-window (current-buffer)))
|
|
|
|
(request (bbj-request "thread_create"
|
2017-03-03 01:10:16 +00:00
|
|
|
'body message
|
|
|
|
'title ,(read-from-minibuffer "(Thread Title)> ")
|
|
|
|
'tags ,(read-from-minibuffer "(Comma-seperated tags, if any)> "))))
|
2017-03-03 03:01:01 +00:00
|
|
|
(if (numberp (bbj-descend request 'error 'code))
|
2017-03-03 01:10:16 +00:00
|
|
|
(message "%s" request)
|
|
|
|
(message "thread submitted")
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-browse-index))))))
|
2017-03-03 01:10:16 +00:00
|
|
|
(thread
|
|
|
|
`("Replying to thread (C-c C-c to send)"
|
|
|
|
(lambda ()
|
2017-03-03 03:01:01 +00:00
|
|
|
(let* ((message (bbj-consume-window (current-buffer)))
|
|
|
|
(request (bbj-request "thread_reply"
|
2017-03-03 01:10:16 +00:00
|
|
|
'body message 'thread_id ,thread-id)))
|
2017-03-03 03:01:01 +00:00
|
|
|
(if (numberp (bbj-descend request 'error 'code))
|
2017-03-03 01:10:16 +00:00
|
|
|
(message "%s" request)
|
|
|
|
(message "reply submitted")
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-enter-thread ,thread-id)
|
2017-03-03 01:10:16 +00:00
|
|
|
(goto-char (point-max))
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-point-to-post 'prev)
|
2017-03-03 01:10:16 +00:00
|
|
|
(recenter nil)))))))))
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(apply #'bbj-compose-in-window params)))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-compose-in-window (title callback &rest cbargs)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Create a new buffer, pop it, set TITLE as the header line, and
|
|
|
|
assign CALLBACK to C-c C-c."
|
2017-03-01 21:54:56 +00:00
|
|
|
(let ((buffer (get-buffer-create "*BBJ: Compose*")))
|
|
|
|
(pop-to-buffer buffer)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(erase-buffer)
|
2017-03-03 01:10:16 +00:00
|
|
|
(text-mode)
|
|
|
|
(use-local-map (copy-keymap text-mode-map))
|
2017-03-03 03:01:01 +00:00
|
|
|
(local-set-key (kbd "C-c C-c") 'bbj-aux)
|
2017-03-01 21:54:56 +00:00
|
|
|
(setq header-line-format title
|
2017-03-03 03:01:01 +00:00
|
|
|
bbj-aux-callback callback))))
|
2017-03-01 21:54:56 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-consume-window (buffer)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Consume all text in the current buffer, delete the window if
|
|
|
|
it is one, and kill the buffer. Returns property-free string."
|
2017-03-01 21:54:56 +00:00
|
|
|
(interactive)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(let ((content (buffer-substring-no-properties
|
|
|
|
(point-min) (point-max))))
|
|
|
|
(quit-window t)
|
|
|
|
content)))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-postprocess ()
|
2017-03-03 01:10:16 +00:00
|
|
|
"Makes all the whitespace in and between posts consistent."
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-first-post)
|
2017-03-03 01:10:16 +00:00
|
|
|
(save-excursion
|
|
|
|
(while (re-search-forward "\n\n\n+" nil t)
|
|
|
|
(replace-match "\n\n"))))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-render-body (string &optional return-string notrim)
|
2017-03-03 01:10:16 +00:00
|
|
|
"takes an html STRING. If RETURN-STRING is non nil, it renders
|
|
|
|
it in a temp buffer and returns the string. Otherwise, inserts
|
|
|
|
and renders the content in the current buffer."
|
2017-03-03 03:01:01 +00:00
|
|
|
(let* ((shr-width bbj-width)
|
2017-03-03 01:10:16 +00:00
|
|
|
(shr-external-rendering-functions
|
2017-03-03 03:01:01 +00:00
|
|
|
'((span . bbj-render-tag-span)))
|
2017-03-03 01:10:16 +00:00
|
|
|
result)
|
|
|
|
(if (not return-string)
|
|
|
|
(let ((start (point)))
|
|
|
|
(insert string)
|
|
|
|
(shr-render-region start (point-max))
|
|
|
|
(insert "\n\n"))
|
|
|
|
(setq result
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert string)
|
|
|
|
(shr-render-region (point-min) (point-max))
|
|
|
|
(buffer-substring (point-min) (point-max))))
|
|
|
|
(if notrim result (string-trim result)))))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-timestring (epoch)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Make a cute timestring out of the epoch (for post heads)"
|
2017-03-03 02:28:13 +00:00
|
|
|
(format-time-string "%H:%M %a %m/%d/%y" (seconds-to-time epoch)))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-render-post (object)
|
2017-03-03 01:10:16 +00:00
|
|
|
"Render an API object into the current buffer. Can be either the parent object
|
|
|
|
or any of its children."
|
2017-03-03 03:01:01 +00:00
|
|
|
(let* ((userdata (cdr (assoc-string (alist-get 'author object) bbj-*usermap*)))
|
2017-03-03 01:10:16 +00:00
|
|
|
(title (alist-get 'title object))
|
|
|
|
(indicator (format ">>%s " (or title (alist-get 'post_id object)))))
|
|
|
|
(insert (propertize indicator
|
|
|
|
'face 'font-lock-function-name-face
|
|
|
|
'type 'head 'data object))
|
|
|
|
(when title (insert "\n"))
|
|
|
|
(insert (propertize
|
|
|
|
(concat "~" (alist-get 'name userdata) " ")
|
|
|
|
'face 'font-lock-keyword-face))
|
2017-03-03 03:01:01 +00:00
|
|
|
(insert (if (eq bbj-buffer-type 'index)
|
2017-03-03 01:10:16 +00:00
|
|
|
(propertize (format "@ %s\n%s replies; last active %s\n"
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-timestring (alist-get 'created object))
|
2017-03-03 01:10:16 +00:00
|
|
|
(alist-get 'reply_count object)
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-timestring (alist-get 'lastmod object)))
|
2017-03-03 01:10:16 +00:00
|
|
|
'face 'font-lock-comment-face)
|
2017-03-03 03:01:01 +00:00
|
|
|
(propertize (format "@ %s\n\n" (bbj-timestring (alist-get 'created object)))
|
2017-03-03 01:10:16 +00:00
|
|
|
'face 'font-lock-comment-face)))
|
2017-03-03 03:01:01 +00:00
|
|
|
(when (eq bbj-buffer-type 'thread)
|
|
|
|
(bbj-render-body (alist-get 'body object)))
|
|
|
|
(bbj-insert-sep)))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
;; (defun bbj-render-tag-span (dom)
|
2017-03-03 02:55:46 +00:00
|
|
|
;; "A slightly modified version of `shr-tag-span' which handles quotes and stuff."
|
2017-03-03 03:01:01 +00:00
|
|
|
;; (let ((class (if bbj-old-p
|
2017-03-03 02:55:46 +00:00
|
|
|
;; (assq :class (cdr dom))
|
|
|
|
;; (dom-attr dom 'class))))
|
|
|
|
;; (dolist (sub (if (consp (car dom)) (cddr (car dom)) (cddr dom)))
|
|
|
|
;; (if (stringp sub)
|
|
|
|
;; (cond
|
|
|
|
;; ((equal class "quote")
|
|
|
|
;; (insert (propertize sub
|
|
|
|
;; 'face 'font-lock-constant-face
|
|
|
|
;; 'type 'quote)))
|
|
|
|
;; ((equal class "linequote")
|
|
|
|
;; (insert (propertize sub
|
|
|
|
;; 'face 'font-lock-string-face
|
|
|
|
;; 'type 'linequote)))
|
|
|
|
;; (t (shr-insert sub)))
|
|
|
|
;; (shr-descend sub)))))
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-render-tag-span (dom)
|
|
|
|
"A highly bastardized version of `shr-tag-span', beaten and maimed until
|
|
|
|
it worked on emacs 24."
|
|
|
|
(let ((class (if bbj-old-p
|
2017-03-03 02:55:46 +00:00
|
|
|
(alist-get :class dom)
|
|
|
|
(dom-attr dom 'class)))
|
2017-03-03 03:01:01 +00:00
|
|
|
(text (if bbj-old-p
|
2017-03-03 02:55:46 +00:00
|
|
|
(alist-get 'text dom)
|
|
|
|
(car (last dom)))))
|
|
|
|
(cond
|
|
|
|
((equal class "quote")
|
|
|
|
(insert (propertize text
|
|
|
|
'face 'font-lock-constant-face
|
|
|
|
'type 'quote)))
|
|
|
|
((equal class "linequote")
|
|
|
|
(unless (bolp) (insert "\n"))
|
|
|
|
(insert (propertize text
|
|
|
|
'face 'font-lock-string-face
|
|
|
|
'type 'linequote)))
|
|
|
|
(text (shr-insert text)))))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-mksep ()
|
|
|
|
(format "\n%s\n" (make-string bbj-width ?\-)))
|
2017-03-03 01:10:16 +00:00
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-insert-sep (&optional drop-newline)
|
|
|
|
(let ((sep (bbj-mksep)))
|
2017-03-03 01:10:16 +00:00
|
|
|
(insert (propertize
|
|
|
|
(if drop-newline (subseq sep 1) sep)
|
|
|
|
'face 'font-lock-comment-face
|
|
|
|
'type 'end))))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-browse-index ()
|
2017-03-01 21:54:56 +00:00
|
|
|
(interactive)
|
2017-03-03 01:10:16 +00:00
|
|
|
(let* ((inhibit-read-only t)
|
|
|
|
(buffer (get-buffer-create "*BBJ Index*"))
|
2017-03-03 03:01:01 +00:00
|
|
|
(response (bbj-request "thread_index"))
|
|
|
|
(bbj-*usermap* (alist-get 'usermap response)))
|
2017-03-03 01:10:16 +00:00
|
|
|
(with-current-buffer buffer
|
|
|
|
(erase-buffer)
|
|
|
|
(bbj-mode)
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq bbj-buffer-type 'index
|
|
|
|
bbj-*usermap* (alist-get 'usermap response))
|
|
|
|
(bbj-insert-sep t)
|
2017-03-03 01:10:16 +00:00
|
|
|
(loop for thread across (alist-get 'threads response) do
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-render-post thread))
|
|
|
|
(bbj-postprocess))
|
2017-03-03 01:10:16 +00:00
|
|
|
(switch-to-buffer buffer)
|
|
|
|
(setq buffer-read-only t)))
|
|
|
|
|
|
|
|
|
2017-03-03 03:01:01 +00:00
|
|
|
(defun bbj-enter-thread (id)
|
2017-03-03 01:10:16 +00:00
|
|
|
(interactive)
|
|
|
|
(let* ((inhibit-read-only t)
|
2017-03-03 03:01:01 +00:00
|
|
|
(response (bbj-request "thread_load" 'thread_id id))
|
2017-03-03 01:10:16 +00:00
|
|
|
(buffer (get-buffer-create (format "BBJ: %s" (alist-get 'title response)))))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(erase-buffer)
|
|
|
|
(bbj-mode)
|
2017-03-03 03:01:01 +00:00
|
|
|
(setq bbj-buffer-type 'thread
|
|
|
|
bbj-*usermap* (alist-get 'usermap response))
|
2017-03-03 01:10:16 +00:00
|
|
|
(setq-local thread-id id)
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-insert-sep t)
|
|
|
|
(bbj-render-post response)
|
2017-03-03 01:10:16 +00:00
|
|
|
(loop for reply across (alist-get 'replies response) do
|
2017-03-03 03:01:01 +00:00
|
|
|
(bbj-render-post reply))
|
|
|
|
(bbj-postprocess))
|
2017-03-03 01:10:16 +00:00
|
|
|
(switch-to-buffer buffer)
|
|
|
|
(setq buffer-read-only t)))
|