about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc/chrome.el
blob: 133c7af355fa9f2755e2a3a096fbc7c058b95124 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
;;; chrome.el --- Helpers for Google Chrome -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com>

;;; Commentary:
;; Some helper functions for working with Google Chrome.

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'macros)
(require 'alist)
(require 'list)
(require 'general)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar chrome/install-kbds? t
  "If t, install keybinding.")

;; TODO: Consider modelling this as a rose-tree that can nest itself
;; arbitrarily.
;; TODO: Consider exporting existing chrome bookmarks.
(defconst chrome/label->url
  '(("Google" . "www.google.com")
    ("Hacker News" . "news.ycombinator.com")
    ("Gmail" . "www.gmail.com")
    ("WhatsApp" . "web.whatsapp.com")
    ("Google Chat" . "chat/")
    ("Google Calendar" . "calendar/")
    ("Teknql" . "teknql.slack.com/messages")
    ("Twitter" . "twitter.com"))
  "Mapping labels to urls for my bookmarks.")

(defconst chrome/splash-pages
  '("Google Calendar"
    "Gmail"
    "Google Chat"
    "WhatsApp"
    "Teknql")
  "The pages that should open when I open Chrome.")

;; TODO: Add defensive check to start chrome if it isn't already open.

;; TODO: Support option to create new session even if one already exists.

(defun chrome/open-splash-pages ()
  "Opens Chrome with my preferred splash pages."
  (interactive)
  (->> chrome/splash-pages
       (-map (lambda (x) (alist/get x chrome/label->url)))
       chrome/open-urls))

;; TODO: Support optional kwargs.
(cl-defun chrome/open-url (url &key new-window?)
  "Opens `URL' in google-chrome.
Will open without toolbars if APP-MODE? is t."
  (shell-command (s-concat
                  "google-chrome "
                  (if new-window? "--new-window " "")
                  url)))

(defun chrome/open-urls (urls)
  "Open multiple `URLS' in chrome."
  (chrome/open-url
   (list/join " " urls)))

(defun chrome/browse ()
  "Display a counsel window for browsing URLs."
  (interactive)
  (ivy-read
   "URL: "
   chrome/label->url
   :action (lambda (entry)
             (chrome/open-url (cdr entry)))))

(provide 'chrome)
;;; chrome.el ends here