-;; Brain-mode: the Extend-o-Brain Emacs UI
+;; Brain-mode: the Extend-o-Brain Emacs library
 ;;
 ;; Required global variables:
 ;;
-;;     extendo-rexster-host: IP of the rexster server
-;;     extendo-rexster-port: port of the Rexster server
-;;     extendo-rexster-graph: name of Extend-o-Brain graph served by Rexster
+;;     exo-rexster-host: IP of the rexster server
+;;     exo-rexster-port: port of the Rexster server
+;;     exo-rexster-graph: name of Extend-o-Brain graph served by Rexster
 ;;
 ;; For example:
 ;;
 ;;     (defun brain-mode ()
-;;         (defvar extendo-rexster-host "localhost")
-;;         (defvar extendo-rexster-port "8182")
-;;         (defvar extendo-rexster-graph "joshkb"))
-
-;; Uncomment only when debugging
-(add-hook 'after-init-hook '(lambda () (setq debug-on-error t)))
-
-(eval-when-compile (require 'cl))
-
-;; for JSON-formatted messages to and from Rexster
+;;         (defvar exo-rexster-host "localhost")
+;;         (defvar exo-rexster-port "8182")
+;;         (defvar exo-rexster-graph "joshkb"))
+
+
+;; DEPENDENCIES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; for JSON-formatted messages to and from Extendo services
 (require 'json)
 
 ;; for line number annotations in buffers

;; for visiting URLs in a browser
(require 'goto-addr)

;;(require 'ring)


;; for encryption of sensitive values
(require 'aes)

;; LOCAL IMPORTS ;;;;;;;;;;;;;;;;;;;;;;;
;; not required by this library ;;;;;;;;

(require 'latex-math-preview)


;; HELPER CODE ;;;;;;;;;;;;;;;;;;;;;;;;;

;; from Emacs-w3m
(defun w3m-url-encode-string (str &optional coding)

            (let ((c (read-char p)))
                (if c (funcall f c))))))

;; Note: when updating this list of mappings, also update the PKB
(global-set-key (kbd "C-c C-a C-p")     (char-arg 'exo-insert-attr-priority "priority = ?"))
(global-set-key (kbd "C-c C-a C-s")     (char-arg 'exo-insert-attr-sharability "sharability = ?"))
(global-set-key (kbd "C-c C-a C-w")     (char-arg 'exo-insert-attr-weight "weight = ?"))
(global-set-key (kbd "C-c C-a d")       'exo-insert-current-date)
(global-set-key (kbd "C-c C-a s")       'exo-insert-current-time-with-seconds)
(global-set-key (kbd "C-c C-a t")       'exo-insert-current-time)
(global-set-key (kbd "C-c C-d")         (char-arg 'exo-choose-depth "depth = ?"))
(global-set-key (kbd "C-c C-l")         (minibuffer-arg 'exo-goto-line "line: "))
(global-set-key (kbd "C-c C-r C-b a")   (exo-visit-in-amazon 'current-root-value))
(global-set-key (kbd "C-c C-r C-b e")   (exo-visit-in-ebay 'current-root-value))
(global-set-key (kbd "C-c C-r C-b d")   (exo-visit-in-delicious 'current-root-value))
(global-set-key (kbd "C-c C-r C-b g")   (exo-visit-in-google 'current-root-value))
(global-set-key (kbd "C-c C-r C-b m")   (exo-visit-in-google-maps 'current-root-value))
(global-set-key (kbd "C-c C-r C-b s")   (exo-visit-in-google-scholar 'current-root-value))
(global-set-key (kbd "C-c C-r C-b t")   (exo-visit-in-twitter 'current-root-value))
(global-set-key (kbd "C-c C-r C-b w")   (exo-visit-in-wikipedia 'current-root-value))
(global-set-key (kbd "C-c C-r C-b y")   (exo-visit-in-youtube 'current-root-value))
(global-set-key (kbd "C-c C-s C-d")     (char-arg 'exo-set-default-sharability "default sharability = ?"))
(global-set-key (kbd "C-c C-s C-m")     (char-arg 'exo-set-min-sharability "minimum sharability = ?"))
(global-set-key (kbd "C-c C-t C-a b")   'exo-visit-target-alias)
(global-set-key (kbd "C-c C-t C-b a")   (exo-visit-in-amazon 'current-target-value))
(global-set-key (kbd "C-c C-t C-b e")   (exo-visit-in-ebay 'current-target-value))
(global-set-key (kbd "C-c C-t C-b d")   (exo-visit-in-delicious 'current-target-value))
(global-set-key (kbd "C-c C-t C-b g")   (exo-visit-in-google 'current-target-value))
(global-set-key (kbd "C-c C-t C-b m")   (exo-visit-in-google-maps 'current-target-value))
(global-set-key (kbd "C-c C-t C-b s")   (exo-visit-in-google-scholar 'current-target-value))
(global-set-key (kbd "C-c C-t C-b t")   (exo-visit-in-twitter 'current-target-value))
(global-set-key (kbd "C-c C-t C-b w")   (exo-visit-in-wikipedia 'current-target-value))
(global-set-key (kbd "C-c C-t C-b y")   (exo-visit-in-youtube 'current-target-value))
(global-set-key (kbd "C-c C-t C-p")     (char-arg 'exo-set-target-priority "new priority = ?"))
(global-set-key (kbd "C-c C-t C-s")     (char-arg 'exo-set-target-sharability "new sharability = ?"))
(global-set-key (kbd "C-c C-t C-w")     (char-arg 'exo-set-target-weight "new weight = ?"))
;; TODO: finish generalizing these "C-c C-t x" functions to root vs. target
(global-set-key (kbd "C-c C-t a")       (exo-visit-as-url 'current-target-value))
(global-set-key (kbd "C-c C-t c")       'exo-copy-target-value-to-clipboard)
(global-set-key (kbd "C-c C-t i")       (exo-atom-info 'current-target))
(global-set-key (kbd "C-c C-t l")       'exo-preview-target-latex-math)
(global-set-key (kbd "C-c C-t r")       'exo-copy-target-reference-to-clipboard)
;; Note: this should perhaps be a local setting
(global-set-key (kbd "C-c C-v ;")       'exo-toggle-truncate-lines)
(global-set-key (kbd "C-c C-v b")       'exo-update-to-backward-view)
(global-set-key (kbd "C-c C-v e")       'exo-enter-edit-view)
(global-set-key (kbd "C-c C-v f")       'exo-update-to-forward-view)
(global-set-key (kbd "C-c C-v i")       'exo-toggle-inference-viewstyle)
(global-set-key (kbd "C-c C-v r")       'exo-enter-readonly-view)
(global-set-key (kbd "C-c C-v s")       'exo-toggle-emacspeak)
(global-set-key (kbd "C-c C-v t")       (minibuffer-arg 'exo-set-value-truncation-length "value truncation length: "))
(global-set-key (kbd "C-c C-w C-d")     (char-arg 'exo-set-default-weight "default weight = ?"))
(global-set-key (kbd "C-c C-w C-m")     (char-arg 'exo-set-min-weight "minimun weight = ?"))
(global-set-key (kbd "C-c a")           'exo-visit-url-at-point)
(global-set-key (kbd "C-c d")           'exo-duplicates)
(global-set-key (kbd "C-c e")           'exo-export)
(global-set-key (kbd "C-c f")           'exo-find-roots)
(global-set-key (kbd "C-c h")           'exo-history)
(global-set-key (kbd "C-c i")           'exo-infer-types)
(global-set-key (kbd "C-c P")           'exo-priorities)
(global-set-key (kbd "C-c p")           'exo-push-view)
(global-set-key (kbd "C-c r")           (minibuffer-arg 'exo-ripple-query "ripple query: "))
(global-set-key (kbd "C-c s")           (minibuffer-arg 'exo-search "search for: "))
(global-set-key (kbd "C-c t")           'exo-visit-target)
(global-set-key (kbd "C-c u")           'exo-update-view)
(global-set-key (kbd "C-c v")           'exo-events)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq-default truncate-lines t)
(if full-colors-supported
    (let ()
        (global-hl-line-mode 1)
        (set-face-background 'hl-line "ivory")))
;; These may or may not be necessary
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
