-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelm-firefox.el
174 lines (144 loc) · 6.29 KB
/
helm-firefox.el
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;;; helm-firefox.el --- Firefox bookmarks -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2015 Thierry Volpiatto <[email protected]>
;; Version: 1.1
;; Package-Requires: ((helm "1.5") (cl-lib "0.5") (emacs "24.1"))
;; URL: https://github.com/emacs-helm/helm-firefox
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-lib)
(require 'helm)
(require 'helm-utils)
(require 'helm-adaptive)
(require 'helm-net)
(defgroup helm-firefox nil
"Helm libraries and applications for Firefox navigator."
:group 'helm)
(defvar helm-firefox-bookmark-url-regexp "\\(https\\|http\\|ftp\\|about\\|file\\)://[^ \"]*")
(defvar helm-firefox-bookmarks-regexp ">\\([^><]+.\\)</[aA]>")
(defvar helm-firefox-history nil)
(defface helm-firefox-title
'((t (:inherit 'font-lock-type-face)))
"Face used for firefox bookmark titles."
:group 'helm-firefox)
(defun helm-get-firefox-user-init-dir (directory)
"Guess the default Firefox user directory name."
(with-temp-buffer
(insert-file-contents
(expand-file-name "profiles.ini" directory))
(goto-char (point-min))
(prog1
(cl-loop with dir
with atime = 0
while (re-search-forward "Path=" nil t)
for tmpdir = (expand-file-name
(buffer-substring-no-properties
(point) (point-at-eol))
directory)
for bmkfile = (expand-file-name "bookmarks.html" tmpdir)
for at = (if (file-exists-p bmkfile)
(float-time (nth 5 (file-attributes bmkfile)))
(max atime 0))
when (> at atime)
do (setq dir tmpdir
atime at)
finally return (file-name-as-directory
(expand-file-name
dir directory)))
(kill-buffer))))
(defvar helm-firefox-bookmark-user-directory nil
"The Firefox user directory.
This variable is set automatically by helm-firefox, you should not
have to modify it yourself.
Should be located in `helm-firefox-default-directory', you may have
several different directories there if you use different firefox
versions, if the default found by helm-firefox is not the one you want
to use, look at your \"profiles.ini\" file which profile you are
currently using, Firefox use by default the one of the recentest
Firefox installation, it is adviced to use Firefox sync instead of
changing this default value.")
(defcustom helm-firefox-default-directory "~/.mozilla/firefox/"
"The root directory containing firefox config.
On Mac OS X, probably set to \"~/Library/Application Support/Firefox/\".
DO NOT use `setq' to configure this variable."
:group 'helm-firefox
:type 'string
:set (lambda (var val)
(set var val)
(setq helm-firefox-bookmark-user-directory
(helm-get-firefox-user-init-dir val))))
(defun helm-guess-firefox-bookmark-file ()
"Return the path of the Firefox bookmarks file."
(expand-file-name "bookmarks.html" helm-firefox-bookmark-user-directory))
(defvar helm-firefox-bookmarks-alist nil)
(defvar helm-source-firefox-bookmarks
(helm-build-sync-source "Firefox Bookmarks"
:init (lambda ()
(setq helm-firefox-bookmarks-alist
(helm-html-bookmarks-to-alist
(helm-guess-firefox-bookmark-file)
helm-firefox-bookmark-url-regexp
helm-firefox-bookmarks-regexp)))
:candidates (lambda ()
(cl-loop for (bmk . url) in helm-firefox-bookmarks-alist
collect (concat bmk "\n" url)))
:multiline t
:filtered-candidate-transformer
'(helm-adaptive-sort
helm-firefox-highlight-bookmarks)
:action (helm-make-actions
"Browse Url"
(lambda (_candidate)
(let ((urls (helm-marked-candidates)))
(cl-loop for cand in urls
do (helm-browse-url cand))))
"Copy Url"
(lambda (url)
(kill-new url)
(message "`%s' copied to kill-ring" url)))))
(defun helm-firefox-bookmarks-get-value (elm)
(assoc-default elm helm-firefox-bookmarks-alist))
(defun helm-firefox-highlight-bookmarks (bookmarks _source)
(cl-loop for bmk in bookmarks
for split = (split-string bmk "\n")
collect (cons (concat (propertize
(car split)
'face 'helm-firefox-title)
"\n"
(cadr split))
(cadr split))))
;;;###autoload
(defun helm-firefox-bookmarks ()
"Preconfigured `helm' for firefox bookmark.
You will have to enable html bookmarks in firefox:
open \"about:config\" in firefox and double click on this line to enable value
to true:
user_pref(\"browser.bookmarks.autoExportHTML\", false);
You should have now:
user_pref(\"browser.bookmarks.autoExportHTML\", true);
After closing firefox, you will be able to browse your bookmarks."
(interactive)
(helm :sources `(helm-source-firefox-bookmarks
,(helm-build-dummy-source "DuckDuckgo"
:action (lambda (candidate)
(helm-browse-url
(format helm-surfraw-duckduckgo-url
(url-hexify-string candidate))))))
:truncate-lines t
:buffer "*Helm Firefox*"
:history 'helm-firefox-history))
(provide 'helm-firefox)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:
;;; helm-firefox.el ends here