Skip to content

Instantly share code, notes, and snippets.

@kiwanami
Last active December 17, 2016 04:28
Show Gist options
  • Save kiwanami/5820978 to your computer and use it in GitHub Desktop.
Save kiwanami/5820978 to your computer and use it in GitHub Desktop.
ctable-0.1.2b.el and demo.
;;; An asynchronous data model sample for ctable.el
(require 'ctable)
(require 'deferred)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; synchronous version
(defun ctbl:sync-demo1 ()
(interactive)
(ctbl:open-table-buffer-easy
(loop with lim = 4000
for i from 0 upto lim
for d = (/ (random 1000) 1000.0)
collect
(list i d (exp (- (/ i 1.0 lim))) (exp (* (- (/ i 1.0 lim)) d))))))
;; (ctbl:sync-demo1) ; 5 seconds to display!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; asynchronous version
(defun ctbl:async-demo ()
"Sample code for implementation for async data model table."
(interactive)
(let ((param (copy-ctbl:param ctbl:default-rendering-param)))
(setf (ctbl:param-fixed-header param) t)
(let* ((async-model ; define async-data-model
(make-ctbl:async-model
:request 'ctbl:async-demo-request
:cancel 'ctbl:async-demo-cancel
:reset 'ctbl:async-demo-reset
:init-num 40 :more-num 20))
(cp
(ctbl:create-table-component-buffer
:model
(make-ctbl:model
:column-model
(list (make-ctbl:cmodel :title "row")
(make-ctbl:cmodel :title "delta")
(make-ctbl:cmodel :title "exp")
(make-ctbl:cmodel :title "exp-delta"))
:data async-model) ; here!
:param param)))
(ctbl:cp-add-click-hook
cp (lambda () (message "CTable : Click Hook [%S]"
(ctbl:cp-get-selected-data-row cp))))
(pop-to-buffer (ctbl:cp-get-buffer cp)))))
(defvar ctbl:async-demo-timer nil)
(defun ctbl:async-demo-request (row-num len responsef errorf)
(lexical-let
((row-num row-num) (len len)
(responsef responsef) (errorf errorf))
(setq ctbl:async-demo-timer
(deferred:$
(deferred:wait 500)
(deferred:nextc it
(lambda (x)
(setq ctbl:async-demo-timer nil)
(funcall responsef
(if (< 500 row-num) nil
(loop with lim = 100
for i from row-num below (+ row-num len)
for d = (/ (random 1000) 1000.0)
collect
(list i d (exp (- (/ i 1.0 lim)))
(exp (* (- (/ i 1.0 lim)) d))))))))))))
(defun ctbl:async-demo-reset ()
(message "RESET async data!!"))
(defun ctbl:async-demo-cancel ()
(when ctbl:async-demo-timer
(deferred:cancel ctbl:async-demo-timer)))
;; (progn (eval-current-buffer) (ctbl:async-demo))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; async wrapper version
(defun ctbl:sync-demo2 ()
(interactive)
(let* ((async-model ; wrapping a huge data in async-data-model
(ctbl:async-model-wrapper
(loop with lim = 4000
for i from 0 upto lim
for d = (/ (random 1000) 1000.0)
collect
(list i d (exp (- (/ i 1.0 lim))) (exp (* (- (/ i 1.0 lim)) d))))))
(cp
(ctbl:create-table-component-buffer
:model
(make-ctbl:model
:column-model
(list (make-ctbl:cmodel :title "row")
(make-ctbl:cmodel :title "delta")
(make-ctbl:cmodel :title "exp")
(make-ctbl:cmodel :title "exp-delta"))
:data async-model))))
(pop-to-buffer (ctbl:cp-get-buffer cp))))
;; (progn (eval-current-buffer) (ctbl:sync-demo2))
;; see the source https://github.com/kiwanami/emacs-ctable/blob/master/ctable.el
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment