Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-196
[bpt/emacs.git] / lisp / url / url-history.el
CommitLineData
8c8b8430 1;;; url-history.el --- Global history tracking for URL package
8c8b8430
SM
2;; Keywords: comm, data, processes, hypermedia
3
4;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5;;; Copyright (c) 1993 - 1996 by William M. Perry <wmperry@cs.indiana.edu>
6;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
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 2, or (at your option)
13;;; 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; see the file COPYING. If not, write to the
22;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;;; Boston, MA 02111-1307, USA.
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25
26;; This can get a recursive require.
27;;(require 'url)
28(eval-when-compile (require 'cl))
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.
39If non-nil, url will keep track of all the URLS visited.
40If eq to `t', then the list is saved to disk at the end of each emacs
41session."
42 :type 'boolean
43 :group 'url-history)
44
45(defcustom url-history-file nil
46 "*The global history file for the URL package.
47This file contains a list of all the URLs you have visited. This file
48is parsed at startup and used to provide URL completion."
49 :type '(choice (const :tag "Default" :value nil) file)
50 :group 'url-history)
51
52(defcustom url-history-save-interval 3600
53 "*The number of seconds between automatic saves of the history list.
54Default is 1 hour. Note that if you change this variable outside of
55the `customize' interface after `url-do-setup' has been run, you need
56to run the `url-history-setup-save-timer' function manually."
57 :set (function (lambda (var val)
58 (set-default var val)
59 (and (featurep 'url)
60 (fboundp 'url-history-setup-save-timer)
61 (let ((def (symbol-function
62 'url-history-setup-save-timer)))
63 (not (and (listp def) (eq 'autoload (car def)))))
64 (url-history-setup-save-timer))))
65 :type 'integer
66 :group 'url-history)
67
68(defvar url-history-timer nil)
69
70(defvar url-history-list nil
71 "List of urls visited this session.")
72
73(defvar url-history-changed-since-last-save nil
74 "Whether the history list has changed since the last save operation.")
75
76(defvar url-history-hash-table nil
77 "Hash table for global history completion.")
78
79;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80;;;###autoload
81(defun url-history-setup-save-timer ()
82 "Reset the history list timer."
83 (interactive)
84 (cond
85 ((featurep 'itimer)
86 (ignore-errors (delete-itimer url-history-timer))
87 (setq url-history-timer nil)
88 (if url-history-save-interval
89 (setq url-history-timer
90 (start-itimer "url-history-saver" 'url-history-save-history
91 url-history-save-interval
92 url-history-save-interval))))
93 ((fboundp 'run-at-time)
94 (ignore-errors (cancel-timer url-history-timer))
95 (setq url-history-timer nil)
96 (if url-history-save-interval
97 (setq url-history-timer
98 (run-at-time url-history-save-interval
99 url-history-save-interval
100 'url-history-save-history))))
101 (t nil)))
102
103;;;###autoload
104(defun url-history-parse-history (&optional fname)
105 "Parse a history file stored in FNAME."
106 ;; Parse out the mosaic global history file for completions, etc.
107 (or fname (setq fname (expand-file-name url-history-file)))
108 (cond
109 ((not (file-exists-p fname))
110 (message "%s does not exist." fname))
111 ((not (file-readable-p fname))
112 (message "%s is unreadable." fname))
113 (t
114 (condition-case nil
115 (load fname nil t)
116 (error (message "Could not load %s" fname)))))
117 (if (not url-history-hash-table)
118 (setq url-history-hash-table (make-hash-table :size 31 :test 'equal))))
119
120(defun url-history-update-url (url time)
121 (setq url-history-changed-since-last-save t)
122 (puthash (if (vectorp url) (url-recreate-url url) url) time url-history-hash-table))
123
124;;;###autoload
125(defun url-history-save-history (&optional fname)
126 "Write the global history file into `url-history-file'.
127The type of data written is determined by what is in the file to begin
128with. If the type of storage cannot be determined, then prompt the
129user for what type to save as."
130 (interactive)
131 (or fname (setq fname (expand-file-name url-history-file)))
132 (cond
133 ((not url-history-changed-since-last-save) nil)
134 ((not (file-writable-p fname))
135 (message "%s is unwritable." fname))
136 (t
137 (let ((make-backup-files nil)
138 (version-control nil)
139 (require-final-newline t))
140 (save-excursion
141 (set-buffer (get-buffer-create " *url-tmp*"))
142 (erase-buffer)
143 (let ((count 0))
144 (maphash (function
145 (lambda (key value)
146 (while (string-match "[\r\n]+" key)
147 (setq key (concat (substring key 0 (match-beginning 0))
148 (substring key (match-end 0) nil))))
149 (setq count (1+ count))
150 (insert "(puthash \"" key "\""
151 (if (not (stringp value)) " '" "")
152 (prin1-to-string value)
153 " url-history-hash-table)\n")))
154 url-history-hash-table)
155 (goto-char (point-min))
156 (insert (format
157 "(setq url-history-hash-table (make-hash-table :size %d :test 'equal))\n"
158 (/ count 4)))
159 (goto-char (point-max))
160 (insert "\n")
161 (write-file fname))
162 (kill-buffer (current-buffer))))))
163 (setq url-history-changed-since-last-save nil))
164
165(defun url-have-visited-url (url)
166 (url-do-setup)
167 (gethash url url-history-hash-table nil))
168
169(defun url-completion-function (string predicate function)
170 (url-do-setup)
171 (cond
172 ((eq function nil)
173 (let ((list nil))
174 (maphash (function (lambda (key val)
175 (setq list (cons (cons key val)
176 list))))
177 url-history-hash-table)
178 (try-completion string (nreverse list) predicate)))
179 ((eq function t)
180 (let ((stub (concat "^" (regexp-quote string)))
181 (retval nil))
182 (maphash
183 (function
184 (lambda (url time)
185 (if (string-match stub url)
186 (setq retval (cons url retval)))))
187 url-history-hash-table)
188 retval))
189 ((eq function 'lambda)
190 (and url-history-hash-table
191 (gethash string url-history-hash-table)
192 t))
193 (t
194 (error "url-completion-function very confused."))))
195
196(provide 'url-history)
e5566bd5
MB
197
198;;; arch-tag: fbbbaf63-db36-4e88-bc9f-2939aa93afb2