Removed debug.
[ShellArchive.git] / bookmarker.el
blobac76abc252bc80e9acada84023b7efa0cb6f3998
1 (defvar bookmarker-filename "~/bookmarks.txt"
2 "File in which to store (and from which to read) bookmarks.")
4 (defvar bookmarker-history '()
5 "Visited urls.")
7 (defvar bookmarker-bookmark-cache '()
8 "Cache of the current bookmarks. Used internally and filled
9 with `bookmarker-get-bookmarks'")
11 (defvar bookmarker-complete-function 'completing-read
12 "Completion function used to complete bookmark list")
14 (defun bookmarker-get-bookmarks (&optional bookmark-file)
15 "Parse the bookmarks file and return an alist of name - url matches."
16 (let ((bookmark-file (or bookmark-file bookmarker-filename))
17 (bookmarks '()))
18 (save-window-excursion
19 (find-file bookmark-file)
20 (goto-char (point-min))
21 (while (re-search-forward "\"\\(.+?\\)\"[[:blank:]]+\\(.*\\)" nil t)
22 (let ((name (match-string 1))
23 (url (match-string 2)))
24 (push (cons name url) bookmarks))))
25 (reverse bookmarks)))
27 (defun bookmarker-expand-url (name &optional bookmark-file)
28 "Expand and prompt for placeholders in url referenced by
29 NAME. Returns the full url. If the first argument is left blank
30 then the url returned will only be the domain section of a url."
31 (interactive)
32 (catch 'url
33 (let* ((url (cdr (assoc name (bookmarker-get-bookmarks bookmark-file)))))
34 (when url
35 (let ((urlobj (url-generic-parse-url url))
36 (match-start 0)
37 (count 0))
38 (while (setq match-start
39 (string-match "%{\\(.+?\\)}" url match-start))
40 (let ((replacement (save-match-data
41 (url-hexify-string
42 (read-from-minibuffer
43 (concat "String " (match-string 1 url) ": "))))))
44 (when (and (eq count 0) (eq replacement ""))
45 (throw 'url (url-host urlobj)))
46 (setq count (1+ count))
47 (setq url (replace-match replacement nil t url))
48 ;; make sure the next replacement happens ahead of the last
49 (setq match-start (+ match-start (length replacement)))))
50 (throw 'url url))))))
52 (defun bookmarker-names (&optional bookmark-file)
53 "All of the friendly names for bookmarks."
54 (let ((bookmarks (bookmarker-get-bookmarks bookmark-file)))
55 (mapcar 'car bookmarks)))
57 (defun bookmarker-visit ()
58 "Ask for a url (or a name) and then visit it."
59 (interactive)
60 (let* ((name (funcall bookmarker-complete-function "Bookmark: "
61 (bookmarker-names) nil nil nil 'bookmarker-history))
62 (url (bookmarker-expand-url name)))
63 (browse-url (or url name))))
65 (provide 'bookmarker)