Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / net / quickurl.el
CommitLineData
a2ebe600 1;;; quickurl.el --- insert a URL based on text at point in buffer
8749abea 2
ab422c4d 3;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
8749abea 4
67ba52a6
GM
5;; Author: Dave Pearson <davep@davep.org>
6;; Maintainer: Dave Pearson <davep@davep.org>
8749abea
GM
7;; Created: 1999-05-28
8;; Keywords: hypermedia
9
e8af40ee 10;; This file is part of GNU Emacs.
8749abea 11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
8749abea 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
8749abea
GM
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8749abea
GM
24
25;;; Commentary:
26;;
a2ebe600 27;; This package provides a simple method of inserting a URL based on the
8749abea 28;; text at point in the current buffer. This is part of an on-going effort
91af3942 29;; to increase the information I provide people while reducing the amount
8749abea
GM
30;; of typing I need to do. No-doubt there are undiscovered Emacs packages
31;; out there that do all of this and do it better, feel free to point me to
32;; them, in the mean time I'm having fun playing with Emacs Lisp.
33;;
34;; The URLs are stored in an external file as a list of either cons cells,
35;; or lists. A cons cell entry looks like this:
36;;
37;; (<Lookup> . <URL>)
38;;
39;; where <Lookup> is a string that acts as the keyword lookup and <URL> is
40;; the URL associated with it. An example might be:
41;;
42;; ("GNU" . "http://www.gnu.org/")
43;;
44;; A list entry looks like:
45;;
46;; (<Lookup> <URL> <Comment>)
47;;
48;; where <Lookup> and <URL> are the same as with the cons cell and <Comment>
49;; is any text you like that describes the URL. This description will be
50;; used when presenting a list of URLS using `quickurl-list'. An example
51;; might be:
52;;
53;; ("FSF" "http://www.fsf.org/" "The Free Software Foundation")
54;;
55;; Given the above, your quickurl file might look like:
56;;
57;; (("GNU" . "http://www.gnu.org/")
58;; ("FSF" "http://www.fsf.org/" "The Free Software Foundation")
59;; ("emacs" . "http://www.emacs.org/")
d36ca65d 60;; ("davep" "http://www.davep.org/" "Dave's homepage"))
8749abea
GM
61;;
62;; In case you're wondering about the mixture of cons cells and lists,
63;; quickurl started life using just the cons cells, there were no comments.
64;; URL comments are a later addition and so there is a mixture to keep
65;; backward compatibility with existing URL lists.
66;;
67;; The name and location of the file is up to you, the default name used by
68;; `quickurl' is stored in `quickurl-url-file'.
69;;
70;; quickurl is always available from:
71;;
4b9e6d88 72;; <URL:http://www.davep.org/emacs/quickurl.el>
8749abea
GM
73
74;;; TODO:
75;;
76;; o The quickurl-browse-url* functions pretty much duplicate their non
77;; browsing friends. It would feel better if a more generic solution could
78;; be found.
79
80;;; Code:
81
82;; Things we need:
83
a464a6c7 84(eval-when-compile (require 'cl-lib))
8749abea
GM
85(require 'thingatpt)
86(require 'pp)
87(require 'browse-url)
88
8749abea
GM
89;; Customize options.
90
91(defgroup quickurl nil
a2ebe600 92 "Insert a URL based on text at point in buffer."
8749abea
GM
93 :version "21.1"
94 :group 'abbrev
95 :prefix "quickurl-")
96
d36ca65d 97(defcustom quickurl-url-file (convert-standard-filename "~/.quickurls")
fb7ada5f 98 "File that contains the URL list."
8749abea
GM
99 :type 'file
100 :group 'quickurl)
101
67ba52a6 102(defcustom quickurl-format-function (lambda (url) (format "<URL:%s>" (quickurl-url-url url)))
fb7ada5f 103 "Function to format the URL before insertion into the current buffer."
8749abea
GM
104 :type 'function
105 :group 'quickurl)
106
107(defcustom quickurl-sort-function (lambda (list)
108 (sort list
109 (lambda (x y)
110 (string<
111 (downcase (quickurl-url-description x))
112 (downcase (quickurl-url-description y))))))
fb7ada5f 113 "Function to sort the URL list."
8749abea
GM
114 :type 'function
115 :group 'quickurl)
116
117(defcustom quickurl-grab-lookup-function #'current-word
fb7ada5f 118 "Function to grab the thing to lookup."
8749abea
GM
119 :type 'function
120 :group 'quickurl)
121
122(defcustom quickurl-assoc-function #'assoc-ignore-case
fb7ada5f 123 "Function to use for alist lookup into `quickurl-urls'."
8749abea
GM
124 :type 'function
125 :group 'quickurl)
126
127(defcustom quickurl-completion-ignore-case t
fb7ada5f 128 "Should `quickurl-ask' ignore case when doing the input lookup?"
8749abea
GM
129 :type 'boolean
130 :group 'quickurl)
131
132(defcustom quickurl-prefix ";; -*- lisp -*-\n\n"
fb7ada5f 133 "Text to write to `quickurl-url-file' before writing the URL list."
8749abea
GM
134 :type 'string
135 :group 'quickurl)
136
137(defcustom quickurl-postfix ""
fb7ada5f 138 "Text to write to `quickurl-url-file' after writing the URL list.
8749abea
GM
139
140See the constant `quickurl-reread-hook-postfix' for some example text that
141could be used here."
142 :type 'string
143 :group 'quickurl)
144
145(defcustom quickurl-list-mode-hook nil
fb7ada5f 146 "Hooks for `quickurl-list-mode'."
8749abea
GM
147 :type 'hook
148 :group 'quickurl)
149
150;; Constants.
151
152;;;###autoload
153(defconst quickurl-reread-hook-postfix
154 "
155;; Local Variables:
156;; eval: (progn (require 'quickurl) (add-hook 'local-write-file-hooks (lambda () (quickurl-read) nil)))
157;; End:
158"
159 "Example `quickurl-postfix' text that adds a local variable to the
160`quickurl-url-file' so that if you edit it by hand it will ensure that
161`quickurl-urls' is updated with the new URL list.
162
163To make use of this do something like:
164
165 (setq quickurl-postfix quickurl-reread-hook-postfix)
166
865fe16f 167in your init file (after loading/requiring quickurl).")
8749abea
GM
168
169;; Non-customize variables.
170
171(defvar quickurl-urls nil
172 "URL alist for use with `quickurl' and `quickurl-ask'.")
173
a0310a6c
DN
174(defvar quickurl-list-mode-map
175 (let ((map (make-sparse-keymap)))
176 (suppress-keymap map t)
177 (define-key map "a" #'quickurl-list-add-url)
178 (define-key map [(control m)] #'quickurl-list-insert-url)
179 (define-key map "u" #'quickurl-list-insert-naked-url)
180 (define-key map " " #'quickurl-list-insert-with-lookup)
181 (define-key map "l" #'quickurl-list-insert-lookup)
182 (define-key map "d" #'quickurl-list-insert-with-desc)
183 (define-key map [(control g)] #'quickurl-list-quit)
184 (define-key map "q" #'quickurl-list-quit)
185 (define-key map [mouse-2] #'quickurl-list-mouse-select)
186 (define-key map "?" #'describe-mode)
187 map)
8749abea
GM
188 "Local keymap for a `quickurl-list-mode' buffer.")
189
190(defvar quickurl-list-buffer-name "*quickurl-list*"
c7015153 191 "Name for the URL listing buffer.")
8749abea
GM
192
193(defvar quickurl-list-last-buffer nil
194 "`current-buffer' when `quickurl-list' was called.")
195
a2ebe600 196;; Functions for working with a URL entry.
8749abea
GM
197
198(defun quickurl-url-commented-p (url)
199 "Does the URL have a comment?"
200 (listp (cdr url)))
201
202(defun quickurl-make-url (keyword url &optional comment)
e1dbe924 203 "Create a URL from KEYWORD, URL and (optionally) COMMENT."
8749abea
GM
204 (if (and comment (not (zerop (length comment))))
205 (list keyword url comment)
206 (cons keyword url)))
207
a464a6c7 208(defalias 'quickurl-url-keyword #'car
8749abea 209 "Return the keyword for the URL.
a464a6c7 210\n\(fn URL)")
8749abea
GM
211
212(defun quickurl-url-url (url)
213 "Return the actual URL of the URL.
214
215Note that this function is a setfable place."
a464a6c7
SM
216 (declare (gv-setter (lambda (store)
217 `(setf (if (quickurl-url-commented-p ,url)
218 (cadr ,url)
219 (cdr ,url))
220 ,store))))
8749abea
GM
221 (if (quickurl-url-commented-p url)
222 (cadr url)
223 (cdr url)))
224
8749abea 225(defun quickurl-url-comment (url)
a2ebe600 226 "Get the comment from a URL.
8749abea
GM
227
228If the URL has no comment an empty string is returned. Also note that this
229function is a setfable place."
a464a6c7
SM
230 (declare
231 (gv-setter (lambda (store)
232 `(if (quickurl-url-commented-p ,url)
233 (if (zerop (length ,store))
234 (setf (cdr ,url) (cadr ,url))
235 (setf (nth 2 ,url) ,store))
236 (unless (zerop (length ,store))
237 (setf (cdr ,url) (list (cdr ,url) ,store)))))))
8749abea
GM
238 (if (quickurl-url-commented-p url)
239 (nth 2 url)
240 ""))
241
8749abea
GM
242(defun quickurl-url-description (url)
243 "Return a description for the URL.
244
245If the URL has a comment then this is returned, otherwise the keyword is
246returned."
247 (let ((desc (quickurl-url-comment url)))
248 (if (zerop (length desc))
249 (quickurl-url-keyword url)
250 desc)))
251
252;; Main code:
253
a464a6c7 254(cl-defun quickurl-read (&optional buffer)
8749abea
GM
255 "`read' the URL list from BUFFER into `quickurl-urls'.
256
d0aa1aab 257BUFFER, if nil, defaults to current buffer.
8749abea
GM
258Note that this function moves point to `point-min' before doing the `read'
259It also restores point after the `read'."
260 (save-excursion
a464a6c7 261 (goto-char (point-min))
d0aa1aab
JB
262 (setq quickurl-urls (funcall quickurl-sort-function
263 (read (or buffer (current-buffer)))))))
a1506d29 264
8749abea
GM
265(defun quickurl-load-urls ()
266 "Load the contents of `quickurl-url-file' into `quickurl-urls'."
267 (when (file-exists-p quickurl-url-file)
268 (with-temp-buffer
269 (insert-file-contents quickurl-url-file)
270 (quickurl-read))))
271
272(defun quickurl-save-urls ()
273 "Save the contents of `quickurl-urls' to `quickurl-url-file'."
274 (with-temp-buffer
275 (let ((standard-output (current-buffer)))
276 (princ quickurl-prefix)
277 (pp quickurl-urls)
278 (princ quickurl-postfix)
279 (write-region (point-min) (point-max) quickurl-url-file nil 0))))
a1506d29 280
8749abea
GM
281(defun quickurl-find-url (lookup)
282 "Return URL associated with key LOOKUP.
283
284The lookup is done by looking in the alist `quickurl-urls' and the `cons'
285for the URL is returned. The actual method used to look into the alist
286depends on the setting of the variable `quickurl-assoc-function'."
287 (funcall quickurl-assoc-function lookup quickurl-urls))
288
289(defun quickurl-insert (url &optional silent)
290 "Insert URL, formatted using `quickurl-format-function'.
291
292Also display a `message' saying what the URL was unless SILENT is non-nil."
67ba52a6 293 (insert (funcall quickurl-format-function url))
8749abea
GM
294 (unless silent
295 (message "Found %s" (quickurl-url-url url))))
296
297;;;###autoload
a464a6c7 298(cl-defun quickurl (&optional lookup)
a2ebe600 299 "Insert a URL based on LOOKUP.
8749abea
GM
300
301If not supplied LOOKUP is taken to be the word at point in the current
a239d4e9 302buffer, this default action can be modified via
8749abea
GM
303`quickurl-grab-lookup-function'."
304 (interactive)
d0aa1aab
JB
305 (when (or lookup
306 (setq lookup (funcall quickurl-grab-lookup-function)))
8749abea
GM
307 (quickurl-load-urls)
308 (let ((url (quickurl-find-url lookup)))
309 (if (null url)
310 (error "No URL associated with \"%s\"" lookup)
311 (when (looking-at "\\w")
312 (skip-syntax-forward "\\w"))
313 (insert " ")
314 (quickurl-insert url)))))
315
316;;;###autoload
317(defun quickurl-ask (lookup)
a2ebe600 318 "Insert a URL, with `completing-read' prompt, based on LOOKUP."
8749abea
GM
319 (interactive
320 (list
321 (progn
322 (quickurl-load-urls)
323 (let ((completion-ignore-case quickurl-completion-ignore-case))
324 (completing-read "Lookup: " quickurl-urls nil t)))))
325 (let ((url (quickurl-find-url lookup)))
326 (when url
327 (quickurl-insert url))))
a1506d29 328
8749abea 329(defun quickurl-grab-url ()
a2ebe600 330 "Attempt to grab a word/URL pair from point in the current buffer.
8749abea
GM
331
332Point should be somewhere on the URL and the word is taken to be the thing
333that is returned from calling `quickurl-grab-lookup-function' once a
334`backward-word' has been issued at the start of the URL.
335
336It is assumed that the URL is either \"unguarded\" or is wrapped inside an
337<URL:...> wrapper."
338 (let ((url (thing-at-point 'url)))
339 (when url
340 (save-excursion
341 (beginning-of-thing 'url)
342 ;; `beginning-of-thing' doesn't take you to the start of a marked-up
343 ;; URL, only to the start of the URL within the "markup". So, we
344 ;; need to do a little more work to get to where we want to be.
345 (when (thing-at-point-looking-at thing-at-point-markedup-url-regexp)
346 (search-backward "<URL:"))
347 (backward-word 1)
348 (let ((word (funcall quickurl-grab-lookup-function)))
349 (when word
350 (quickurl-make-url
351 ;; The grab function may return the word with properties. I don't
352 ;; want the properties. I couldn't find a method of stripping
353 ;; them from a "string" so this will have to do. If you know of
354 ;; a better method of doing this I'd love to know.
355 (with-temp-buffer
356 (insert word)
357 (buffer-substring-no-properties (point-min) (point-max)))
358 url)))))))
359
360;;;###autoload
361(defun quickurl-add-url (word url comment)
362 "Allow the user to interactively add a new URL associated with WORD.
363
a2ebe600 364See `quickurl-grab-url' for details on how the default word/URL combination
8749abea
GM
365is decided."
366 (interactive (let ((word-url (quickurl-grab-url)))
367 (list (read-string "Word: " (quickurl-url-keyword word-url))
368 (read-string "URL: " (quickurl-url-url word-url))
369 (read-string "Comment: " (quickurl-url-comment word-url)))))
370 (if (zerop (length word))
a1506d29 371 (error "You must specify a WORD for lookup")
8749abea
GM
372 (quickurl-load-urls)
373 (let* ((current-url (quickurl-find-url word))
374 (add-it (if current-url
32226619 375 (if (called-interactively-p 'interactive)
8749abea
GM
376 (y-or-n-p (format "\"%s\" exists, replace URL? " word))
377 t)
378 t)))
379 (when add-it
380 (if current-url
381 (progn
382 (setf (quickurl-url-url current-url) url)
383 (setf (quickurl-url-comment current-url) comment))
384 (push (quickurl-make-url word url comment) quickurl-urls))
385 (setq quickurl-urls (funcall quickurl-sort-function quickurl-urls))
386 (quickurl-save-urls)
387 (when (get-buffer quickurl-list-buffer-name)
388 (quickurl-list-populate-buffer))
32226619 389 (when (called-interactively-p 'interactive)
8749abea
GM
390 (message "Added %s" url))))))
391
392;;;###autoload
d0aa1aab 393(defun quickurl-browse-url (&optional lookup)
8749abea
GM
394 "Browse the URL associated with LOOKUP.
395
396If not supplied LOOKUP is taken to be the word at point in the
a239d4e9 397current buffer, this default action can be modified via
8749abea
GM
398`quickurl-grab-lookup-function'."
399 (interactive)
d0aa1aab
JB
400 (when (or lookup
401 (setq lookup (funcall quickurl-grab-lookup-function)))
8749abea
GM
402 (quickurl-load-urls)
403 (let ((url (quickurl-find-url lookup)))
404 (if url
405 (browse-url (quickurl-url-url url))
406 (error "No URL associated with \"%s\"" lookup)))))
407
408;;;###autoload
409(defun quickurl-browse-url-ask (lookup)
410 "Browse the URL, with `completing-read' prompt, associated with LOOKUP."
411 (interactive (list
412 (progn
413 (quickurl-load-urls)
414 (completing-read "Browse: " quickurl-urls nil t))))
415 (let ((url (quickurl-find-url lookup)))
416 (when url
417 (browse-url (quickurl-url-url url)))))
418
419;;;###autoload
420(defun quickurl-edit-urls ()
421 "Pull `quickurl-url-file' into a buffer for hand editing."
422 (interactive)
423 (find-file quickurl-url-file))
424
425;; quickurl-list mode.
426
8749abea
GM
427(put 'quickurl-list-mode 'mode-class 'special)
428
429;;;###autoload
430(defun quickurl-list-mode ()
431 "A mode for browsing the quickurl URL list.
432
433The key bindings for `quickurl-list-mode' are:
434
435\\{quickurl-list-mode-map}"
436 (interactive)
437 (kill-all-local-variables)
438 (use-local-map quickurl-list-mode-map)
439 (setq major-mode 'quickurl-list-mode
440 mode-name "quickurl list")
57f9923e 441 (run-mode-hooks 'quickurl-list-mode-hook)
8749abea
GM
442 (setq buffer-read-only t
443 truncate-lines t))
444
445;;;###autoload
446(defun quickurl-list ()
447 "Display `quickurl-list' as a formatted list using `quickurl-list-mode'."
448 (interactive)
449 (quickurl-load-urls)
450 (unless (string= (buffer-name) quickurl-list-buffer-name)
451 (setq quickurl-list-last-buffer (current-buffer)))
452 (pop-to-buffer quickurl-list-buffer-name)
453 (quickurl-list-populate-buffer)
454 (quickurl-list-mode))
455
456(defun quickurl-list-populate-buffer ()
457 "Populate the `quickurl-list' buffer."
458 (with-current-buffer (get-buffer quickurl-list-buffer-name)
a464a6c7
SM
459 (let* ((sizes (or (cl-loop for url in quickurl-urls
460 collect (length (quickurl-url-description url)))
461 (list 20)))
462 (fmt (format "%%-%ds %%s\n" (apply #'max sizes)))
463 (inhibit-read-only t))
464 (erase-buffer)
465 (cl-loop for url in quickurl-urls
466 do (let ((start (point)))
467 (insert (format fmt (quickurl-url-description url)
468 (quickurl-url-url url)))
469 (add-text-properties
470 start (1- (point))
471 '(mouse-face highlight
472 help-echo "mouse-2: insert this URL"))))
473 (goto-char (point-min)))))
8749abea
GM
474
475(defun quickurl-list-add-url (word url comment)
476 "Wrapper for `quickurl-add-url' that doesn't guess the parameters."
477 (interactive "sWord: \nsURL: \nsComment: ")
478 (quickurl-add-url word url comment))
479
480(defun quickurl-list-quit ()
481 "Kill the buffer named `quickurl-list-buffer-name'."
482 (interactive)
483 (kill-buffer quickurl-list-buffer-name)
484 (switch-to-buffer quickurl-list-last-buffer)
485 (delete-other-windows))
486
487(defun quickurl-list-mouse-select (event)
488 "Select the URL under the mouse click."
489 (interactive "e")
a464a6c7 490 (goto-char (posn-point (event-end event)))
8749abea
GM
491 (quickurl-list-insert-url))
492
493(defun quickurl-list-insert (type)
494 "Insert the URL under cursor into `quickurl-list-last-buffer'.
495TYPE dictates what will be inserted, options are:
496 `url' - Insert the URL as <URL:url>
497 `naked-url' - Insert the URL with no formatting
498 `with-lookup' - Insert \"lookup <URL:url>\"
499 `with-desc' - Insert \"description <URL:url>\"
500 `lookup' - Insert the lookup for that URL"
9b026d9f 501 (let ((url (nth (count-lines (point-min) (line-beginning-position))
8749abea
GM
502 quickurl-urls)))
503 (if url
504 (with-current-buffer quickurl-list-last-buffer
505 (insert
a464a6c7
SM
506 (pcase type
507 (`url (funcall quickurl-format-function url))
508 (`naked-url (quickurl-url-url url))
509 (`with-lookup (format "%s <URL:%s>"
8749abea
GM
510 (quickurl-url-keyword url)
511 (quickurl-url-url url)))
a464a6c7 512 (`with-desc (format "%S <URL:%s>"
8749abea
GM
513 (quickurl-url-description url)
514 (quickurl-url-url url)))
a464a6c7 515 (`lookup (quickurl-url-keyword url)))))
8749abea
GM
516 (error "No URL details on that line"))
517 url))
518
519(defmacro quickurl-list-make-inserter (type)
520 "Macro to make a key-response function for use in `quickurl-list-mode-map'."
521 `(defun ,(intern (format "quickurl-list-insert-%S" type)) ()
522 ,(format "Insert the result of calling `quickurl-list-insert' with `%s'." type)
523 (interactive)
524 (when (quickurl-list-insert ',type)
525 (quickurl-list-quit))))
526
527(quickurl-list-make-inserter url)
528(quickurl-list-make-inserter naked-url)
529(quickurl-list-make-inserter with-lookup)
530(quickurl-list-make-inserter with-desc)
531(quickurl-list-make-inserter lookup)
a1506d29 532
8749abea
GM
533(provide 'quickurl)
534
535;;; quickurl.el ends here