From d30da08314e85c8698e39d6f3c1e695e75921556 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Sat, 3 Sep 2016 16:43:45 -0700 Subject: [PATCH] Move muni stuff into its own org file --- dotfiles/emacs.d/README.org | 110 ------------------------------------ dotfiles/emacs.d/muni.org | 106 ++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 110 deletions(-) create mode 100644 dotfiles/emacs.d/muni.org diff --git a/dotfiles/emacs.d/README.org b/dotfiles/emacs.d/README.org index 2b7b8a0b..436a4177 100644 --- a/dotfiles/emacs.d/README.org +++ b/dotfiles/emacs.d/README.org @@ -556,116 +556,6 @@ the ~:around~ keyword of advice-add. (+ (imalison:sin2 (/ (- f2 f1) 2)) (* (cos f2) (cos f1) (imalison:sin2 (/ (- l2 l1) 2))) )))))))) #+END_SRC -** Muni -Create cache structure containing stop information -#+BEGIN_SRC emacs-lisp -(defclass imalison:muni-stop () - ((id :initarg :id) - (name :initarg :name) - (lat :initarg :lat) - (long :initarg :long))) - -(defmethod imalison:muni-stop-get-lat-long ((stop imalison:muni-stop)) - (list (oref stop :lat) (oref stop :long))) - -(defvar imalison:muni-stop-cache (pcache-repository :object-name "imalison-muni-stop")) - -(defun imalison:muni-get-gtfs-stops (&optional filepath) - (unless filepath - (setq filepath - (concat (file-name-directory load-file-name) "stops.txt"))) - (let ((data (imalison:get-string-from-file filepath))) - (parse-csv-string-rows data ?\, ?\" "\n"))) - -(defun imalison:build-muni-stop-from-gtfs-stop (gtfs-stop) - (when (stringp (nth 3 gtfs-stop)) - (make-instance imalison:muni-stop - :id (nth 0 gtfs-stop) - :name (nth 1 gtfs-stop) - :lat (string-to-number (nth 3 gtfs-stop)) - :long (string-to-number (nth 4 gtfs-stop))))) - -(defun imalison:muni-populate-stop-cache (&optional filepath) - (cl-loop for gtfs-stop in (imalison:muni-get-gtfs-stops filepath) - do (let ((muni-stop - (imalison:build-muni-stop-from-gtfs-stop gtfs-stop))) - (when muni-stop - (pcache-put imalison:muni-stop-cache - (intern (oref muni-stop :id)) muni-stop))))) - -(defun imalison:muni-stop-from-id (stop-or-id) - (if (imalison:muni-stop-p stop) stop-or-id - (pcache-get imalison:muni-stop-cache stop-or-id))) -#+END_SRC -Get route information -#+BEGIN_SRC emacs-lisp -(defun imalison:muni-get-route-ids (route-name &optional direction) - (delete-dups - (mapcar (imalison:compose-fn intern car) - (s-match-strings-all - "^\\([[:digit:]]\\{1,10\\}\\)" - (shell-command-to-string - (format "muni show %s %s -v" route-name (or direction ""))))))) -#+END_SRC - -Stop selection functions -#+BEGIN_SRC emacs-lisp -(cl-defun imalison:closest-stop (stops &key location) - (unless location (setq location (imalison:get-lat-long))) - (let (minimizing (minimum 1.0e+INF)) - (cl-loop for stop in stops - do (let* ((actual-stop (imalison:muni-stop-from-id stop)) - (stop-lat-long (imalison:muni-stop-get-lat-long actual-stop)) - (this-dist (imalison:haversine-distance location stop-lat-long))) - (when (< this-dist minimum) - (setq minimizing actual-stop - minimum this-dist)))) - minimizing)) - -(cl-defun imalison:muni-stops-within (stops &key (radius .25) current-location) - (unless current-location (setq current-location (imalison:get-lat-long))) - (cl-loop for stop in stops - with actual-stop = nil - when (let ((stop-lat-long (imalison:muni-stop-get-lat-long actual-stop))) - (setq actual-stop (imalison:muni-stop-from-id stop)) - (< (imalison:haversine-distance current-location stop-lat-long) radius)) - collect actual-stop)) -#+END_SRC - -A function that shells out to get upcoming trains -#+BEGIN_SRC emacs-lisp -(defun imalison:muni-predict (route direction stop) - (s-trim - (shell-command-to-string - (format "muni predict %s %s %s" route direction stop)))) -#+END_SRC - -#+BEGIN_SRC emacs-lisp -(defun imalison:parse-muni-info (info-string) - (when (string-match "\\([[:digit:]]\\{1,3\\}\\) *minutes" info-string) - (match-string-no-properties 1 info-string))) -#+END_SRC - -A cached version of the muni functions for use in spaceline and elsewhere. -#+BEGIN_SRC emacs-lisp -(defvar imalison:muni-cache (pcache-repository :object-name "imalison-muni")) -(defvar imalison:current-location-ttl 10) -(defvar imalison:muni-arrival-ttl 25) - -(defun imalison:get-cached-muni-time () - (let ((current-location (pcache-get imalison:muni-cache 'current-location)) - (arrival-time (pcache-get imalison:muni-cache 'arrival-time))) - (unless arrival-time - (unless current-location - (setq current-location (imalison:get-lat-long)) - (pcache-put imalison:muni-cache 'current-location current-location - imalison:current-location-ttl)) - (setq arrival-time (imalison:parse-muni-info - (imalison:get-closest-muni-info current-location))) - (pcache-put imalison:muni-cache 'arrival-time arrival-time - imalison:muni-arrival-ttl)) - arrival-time)) -#+END_SRC ** Font Size This was taken from [[http://emacs.stackexchange.com/questions/7583/transiently-adjust-text-size-in-mode-line-and-minibuffer][here]]. It is primarily invoked from a hydra defined below. It would be cool if it got the default font size from whatever the default font was but it does not currently do that. #+BEGIN_SRC emacs-lisp diff --git a/dotfiles/emacs.d/muni.org b/dotfiles/emacs.d/muni.org new file mode 100644 index 00000000..16be30a7 --- /dev/null +++ b/dotfiles/emacs.d/muni.org @@ -0,0 +1,106 @@ +* Create cache structure containing stop information +#+BEGIN_SRC emacs-lisp +(defclass imalison:muni-stop () + ((id :initarg :id) + (name :initarg :name) + (lat :initarg :lat) + (long :initarg :long))) + +(defmethod imalison:muni-stop-get-lat-long ((stop imalison:muni-stop)) + (list (oref stop :lat) (oref stop :long))) + +(defvar imalison:muni-stop-cache (pcache-repository :object-name "imalison-muni-stop")) + +(defun imalison:muni-get-gtfs-stops (&optional filepath) + (unless filepath + (setq filepath + (concat (file-name-directory load-file-name) "stops.txt"))) + (let ((data (imalison:get-string-from-file filepath))) + (parse-csv-string-rows data ?\, ?\" "\n"))) + +(defun imalison:build-muni-stop-from-gtfs-stop (gtfs-stop) + (when (stringp (nth 3 gtfs-stop)) + (make-instance imalison:muni-stop + :id (nth 0 gtfs-stop) + :name (nth 1 gtfs-stop) + :lat (string-to-number (nth 3 gtfs-stop)) + :long (string-to-number (nth 4 gtfs-stop))))) + +(defun imalison:muni-populate-stop-cache (&optional filepath) + (cl-loop for gtfs-stop in (imalison:muni-get-gtfs-stops filepath) + do (let ((muni-stop + (imalison:build-muni-stop-from-gtfs-stop gtfs-stop))) + (when muni-stop + (pcache-put imalison:muni-stop-cache + (intern (oref muni-stop :id)) muni-stop))))) + +(defun imalison:muni-stop-from-id (stop-or-id) + (if (imalison:muni-stop-p stop) stop-or-id + (pcache-get imalison:muni-stop-cache stop-or-id))) +#+END_SRC +* Get route information +#+BEGIN_SRC emacs-lisp +(defun imalison:muni-get-route-ids (route-name &optional direction) + (delete-dups + (mapcar (imalison:compose-fn intern car) + (s-match-strings-all + "^\\([[:digit:]]\\{1,10\\}\\)" + (shell-command-to-string + (format "muni show %s %s -v" route-name (or direction ""))))))) +#+END_SRC +* Stop selection functions +#+BEGIN_SRC emacs-lisp +(cl-defun imalison:closest-stop (stops &key location) + (unless location (setq location (imalison:get-lat-long))) + (let (minimizing (minimum 1.0e+INF)) + (cl-loop for stop in stops + do (let* ((actual-stop (imalison:muni-stop-from-id stop)) + (stop-lat-long (imalison:muni-stop-get-lat-long actual-stop)) + (this-dist (imalison:haversine-distance location stop-lat-long))) + (when (< this-dist minimum) + (setq minimizing actual-stop + minimum this-dist)))) + minimizing)) + +(cl-defun imalison:muni-stops-within (stops &key (radius .25) current-location) + (unless current-location (setq current-location (imalison:get-lat-long))) + (cl-loop for stop in stops + with actual-stop = nil + when (let ((stop-lat-long (imalison:muni-stop-get-lat-long actual-stop))) + (setq actual-stop (imalison:muni-stop-from-id stop)) + (< (imalison:haversine-distance current-location stop-lat-long) radius)) + collect actual-stop)) +#+END_SRC +* A function that shells out to get upcoming trains +#+BEGIN_SRC emacs-lisp +(defun imalison:muni-predict (route direction stop) + (s-trim + (shell-command-to-string + (format "muni predict %s %s %s" route direction stop)))) +#+END_SRC + +#+BEGIN_SRC emacs-lisp +(defun imalison:parse-muni-info (info-string) + (when (string-match "\\([[:digit:]]\\{1,3\\}\\) *minutes" info-string) + (match-string-no-properties 1 info-string))) +#+END_SRC +* A cached version of the muni functions for use in spaceline and elsewhere. +#+BEGIN_SRC emacs-lisp +(defvar imalison:muni-cache (pcache-repository :object-name "imalison-muni")) +(defvar imalison:current-location-ttl 10) +(defvar imalison:muni-arrival-ttl 25) + +(defun imalison:get-cached-muni-time () + (let ((current-location (pcache-get imalison:muni-cache 'current-location)) + (arrival-time (pcache-get imalison:muni-cache 'arrival-time))) + (unless arrival-time + (unless current-location + (setq current-location (imalison:get-lat-long)) + (pcache-put imalison:muni-cache 'current-location current-location + imalison:current-location-ttl)) + (setq arrival-time (imalison:parse-muni-info + (imalison:get-closest-muni-info current-location))) + (pcache-put imalison:muni-cache 'arrival-time arrival-time + imalison:muni-arrival-ttl)) + arrival-time)) +#+END_SRC