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