]> code.delx.au - gnu-emacs/blob - lisp/url/url-history.el
c0649db57ce2eaa69c823779339f39a603177923
[gnu-emacs] / lisp / url / url-history.el
1 ;;; url-history.el --- Global history tracking for URL package
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Keywords: comm, data, processes, hypermedia
7
8 ;; This file is part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; This can get a recursive require.
28 ;;(require 'url)
29 (require 'url-parse)
30 (autoload 'url-do-setup "url")
31
32 (defgroup url-history nil
33 "History variables in the URL package."
34 :prefix "url-history"
35 :group 'url)
36
37 (defcustom url-history-track nil
38 "Controls whether to keep a list of all the URLs being visited.
39 If non-nil, the URL package will keep track of all the URLs visited.
40 If set to t, then the list is saved to disk at the end of each Emacs
41 session."
42 :set #'(lambda (var val)
43 (set-default var val)
44 (and (bound-and-true-p url-setup-done)
45 (url-history-setup-save-timer)))
46 :type '(choice (const :tag "off" nil)
47 (const :tag "on" t)
48 (const :tag "within session" 'session))
49 :group 'url-history)
50
51 (defcustom url-history-file nil
52 "The global history file for the URL package.
53 This file contains a list of all the URLs you have visited. This file
54 is parsed at startup and used to provide URL completion."
55 :type '(choice (const :tag "Default" :value nil) file)
56 :group 'url-history)
57
58 (defcustom url-history-save-interval 3600
59 "The number of seconds between automatic saves of the history list.
60 Default is 1 hour. Note that if you change this variable outside of
61 the `customize' interface after `url-do-setup' has been run, you need
62 to run the `url-history-setup-save-timer' function manually."
63 :set #'(lambda (var val)
64 (set-default var val)
65 (if (bound-and-true-p url-setup-done)
66 (url-history-setup-save-timer)))
67 :type 'integer
68 :group 'url-history)
69
70 (defvar url-history-timer nil)
71
72 (defvar url-history-changed-since-last-save nil
73 "Whether the history list has changed since the last save operation.")
74
75 (defvar url-history-hash-table (make-hash-table :size 31 :test 'equal)
76 "Hash table for global history completion.")
77
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79
80 (defun url-history-setup-save-timer ()
81 "Reset the history list timer."
82 (interactive)
83 (condition-case nil
84 (cancel-timer url-history-timer)
85 (error nil))
86 (setq url-history-timer nil)
87 (if (and (eq url-history-track t) url-history-save-interval)
88 (setq url-history-timer (run-at-time url-history-save-interval
89 url-history-save-interval
90 'url-history-save-history))))
91
92 (defun url-history-parse-history (&optional fname)
93 "Parse a history file stored in FNAME."
94 ;; Parse out the mosaic global history file for completions, etc.
95 (or fname (setq fname (expand-file-name url-history-file)))
96 (cond
97 ((not (file-exists-p fname))
98 ;; It's completely normal for this file not to exist, so don't complain.
99 ;; (message "%s does not exist." fname)
100 )
101 ((not (file-readable-p fname))
102 (message "%s is unreadable." fname))
103 (t
104 (condition-case nil
105 (load fname nil t)
106 (error (message "Could not load %s" fname))))))
107
108 (defun url-history-update-url (url time)
109 (setq url-history-changed-since-last-save t)
110 (puthash (if (vectorp url) (url-recreate-url url) url) time
111 url-history-hash-table))
112
113 (autoload 'url-make-private-file "url-util")
114
115 (defun url-history-save-history (&optional fname)
116 "Write the global history file into `url-history-file'.
117 The type of data written is determined by what is in the file to begin
118 with. If the type of storage cannot be determined, then prompt the
119 user for what type to save as."
120 (interactive)
121 (when url-history-changed-since-last-save
122 (or fname (setq fname (expand-file-name url-history-file)))
123 (if (condition-case nil
124 (progn
125 (url-make-private-file fname)
126 nil)
127 (error t))
128 (message "Error accessing history file `%s'" fname)
129 (let ((make-backup-files nil)
130 (version-control nil)
131 (require-final-newline t)
132 (count 0))
133 (with-temp-buffer
134 (maphash (lambda (key value)
135 (while (string-match "[\r\n]+" key)
136 (setq key (concat (substring key 0 (match-beginning 0))
137 (substring key (match-end 0) nil))))
138 (setq count (1+ count))
139 (insert "(puthash \"" key "\""
140 (if (not (stringp value)) " '" "")
141 (prin1-to-string value)
142 " url-history-hash-table)\n"))
143 url-history-hash-table)
144 ;; We used to add this in the file, but it just makes the code
145 ;; more complex with no benefit. Worse: it makes it harder to
146 ;; preserve preexisting history when loading the history file.
147 ;; (goto-char (point-min))
148 ;; (insert (format
149 ;; "(setq url-history-hash-table (make-hash-table :size %d :test 'equal))\n"
150 ;; (/ count 4)))
151 ;; (goto-char (point-max))
152 (insert "\n")
153 (write-file fname)))
154 (setq url-history-changed-since-last-save nil))))
155
156 (defun url-have-visited-url (url)
157 (url-do-setup)
158 (gethash url url-history-hash-table nil))
159
160 (defun url-completion-function (string predicate function)
161 ;; Completion function to complete urls from the history.
162 ;; This is obsolete since we can now pass the hash-table directly as a
163 ;; completion table.
164 (url-do-setup)
165 (cond
166 ((eq function nil)
167 (let ((list nil))
168 (maphash (lambda (key val) (push key list))
169 url-history-hash-table)
170 ;; Not sure why we bother reversing the list. --Stef
171 (try-completion string (nreverse list) predicate)))
172 ((eq function t)
173 (let ((stub (concat "\\`" (regexp-quote string)))
174 (retval nil))
175 (maphash
176 (lambda (url time)
177 (if (string-match stub url) (push url retval)))
178 url-history-hash-table)
179 retval))
180 ((eq function 'lambda)
181 (and (gethash string url-history-hash-table) t))
182 (t
183 (error "url-completion-function very confused"))))
184
185 (provide 'url-history)
186
187 ;;; url-history.el ends here