url-history.el 6.6 KB

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