1 ;;; quickurl.el --- insert a URL based on text at point in buffer
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: Dave Pearson <davep@davep.org>
6 ;; Maintainer: Dave Pearson <davep@davep.org>
8 ;; Keywords: hypermedia
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This package provides a simple method of inserting a URL based on the
28 ;; text at point in the current buffer. This is part of an on-going effort
29 ;; to increase the information I provide people while reducing the amount
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.
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:
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:
42 ;; ("GNU" . "http://www.gnu.org/")
44 ;; A list entry looks like:
46 ;; (<Lookup> <URL> <Comment>)
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
53 ;; ("FSF" "http://www.fsf.org/" "The Free Software Foundation")
55 ;; Given the above, your quickurl file might look like:
57 ;; (("GNU" . "http://www.gnu.org/")
58 ;; ("FSF" "http://www.fsf.org/" "The Free Software Foundation")
59 ;; ("emacs" . "http://www.emacs.org/")
60 ;; ("davep" "http://www.davep.org/" "Dave's homepage"))
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.
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'.
70 ;; quickurl is always available from:
72 ;; <URL:http://www.davep.org/emacs/quickurl.el>
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
84 (eval-when-compile (require 'cl-lib
))
91 (defgroup quickurl nil
92 "Insert a URL based on text at point in buffer."
97 (defcustom quickurl-url-file
98 (locate-user-emacs-file "quickurls" ".quickurls")
99 "File that contains the URL list."
103 (defcustom quickurl-format-function
(lambda (url) (format "<URL:%s>" (quickurl-url-url url
)))
104 "Function to format the URL before insertion into the current buffer."
108 (defcustom quickurl-sort-function
(lambda (list)
112 (downcase (quickurl-url-description x
))
113 (downcase (quickurl-url-description y
))))))
114 "Function to sort the URL list."
118 (defcustom quickurl-grab-lookup-function
#'current-word
119 "Function to grab the thing to lookup."
123 (defcustom quickurl-assoc-function
#'assoc-ignore-case
124 "Function to use for alist lookup into `quickurl-urls'."
128 (defcustom quickurl-completion-ignore-case t
129 "Should `quickurl-ask' ignore case when doing the input lookup?"
133 (defcustom quickurl-prefix
";; -*- lisp -*-\n\n"
134 "Text to write to `quickurl-url-file' before writing the URL list."
138 (defcustom quickurl-postfix
""
139 "Text to write to `quickurl-url-file' after writing the URL list.
141 See the constant `quickurl-reread-hook-postfix' for some example text that
146 (defcustom quickurl-list-mode-hook nil
147 "Hooks for `quickurl-list-mode'."
154 (defconst quickurl-reread-hook-postfix
157 ;; eval: (progn (require 'quickurl) (add-hook 'local-write-file-hooks (lambda () (quickurl-read) nil)))
160 "Example `quickurl-postfix' text that adds a local variable to the
161 `quickurl-url-file' so that if you edit it by hand it will ensure that
162 `quickurl-urls' is updated with the new URL list.
164 To make use of this do something like:
166 (setq quickurl-postfix quickurl-reread-hook-postfix)
168 in your init file (after loading/requiring quickurl).")
170 ;; Non-customize variables.
172 (defvar quickurl-urls nil
173 "URL alist for use with `quickurl' and `quickurl-ask'.")
175 (defvar quickurl-list-mode-map
176 (let ((map (make-sparse-keymap)))
177 (suppress-keymap map t
)
178 (define-key map
"a" #'quickurl-list-add-url
)
179 (define-key map
[(control m
)] #'quickurl-list-insert-url
)
180 (define-key map
"u" #'quickurl-list-insert-naked-url
)
181 (define-key map
" " #'quickurl-list-insert-with-lookup
)
182 (define-key map
"l" #'quickurl-list-insert-lookup
)
183 (define-key map
"d" #'quickurl-list-insert-with-desc
)
184 (define-key map
[(control g
)] #'quickurl-list-quit
)
185 (define-key map
"q" #'quickurl-list-quit
)
186 (define-key map
[mouse-2
] #'quickurl-list-mouse-select
)
187 (define-key map
"?" #'describe-mode
)
189 "Local keymap for a `quickurl-list-mode' buffer.")
191 (defvar quickurl-list-buffer-name
"*quickurl-list*"
192 "Name for the URL listing buffer.")
194 (defvar quickurl-list-last-buffer nil
195 "`current-buffer' when `quickurl-list' was called.")
197 ;; Functions for working with a URL entry.
199 (defun quickurl-url-commented-p (url)
200 "Does the URL have a comment?"
203 (defun quickurl-make-url (keyword url
&optional comment
)
204 "Create a URL from KEYWORD, URL and (optionally) COMMENT."
205 (if (and comment
(not (zerop (length comment
))))
206 (list keyword url comment
)
209 (defalias 'quickurl-url-keyword
#'car
210 "Return the keyword for the URL.
213 (defun quickurl-url-url (url)
214 "Return the actual URL of the URL.
216 Note that this function is a setfable place."
217 (declare (gv-setter (lambda (store)
218 `(setf (if (quickurl-url-commented-p ,url
)
222 (if (quickurl-url-commented-p url
)
226 (defun quickurl-url-comment (url)
227 "Get the comment from a URL.
229 If the URL has no comment an empty string is returned. Also note that this
230 function is a setfable place."
232 (gv-setter (lambda (store)
233 `(if (quickurl-url-commented-p ,url
)
234 (if (zerop (length ,store
))
235 (setf (cdr ,url
) (cadr ,url
))
236 (setf (nth 2 ,url
) ,store
))
237 (unless (zerop (length ,store
))
238 (setf (cdr ,url
) (list (cdr ,url
) ,store
)))))))
239 (if (quickurl-url-commented-p url
)
243 (defun quickurl-url-description (url)
244 "Return a description for the URL.
246 If the URL has a comment then this is returned, otherwise the keyword is
248 (let ((desc (quickurl-url-comment url
)))
249 (if (zerop (length desc
))
250 (quickurl-url-keyword url
)
255 (cl-defun quickurl-read (&optional buffer
)
256 "`read' the URL list from BUFFER into `quickurl-urls'.
258 BUFFER, if nil, defaults to current buffer.
259 Note that this function moves point to `point-min' before doing the `read'
260 It also restores point after the `read'."
262 (goto-char (point-min))
263 (setq quickurl-urls
(funcall quickurl-sort-function
264 (read (or buffer
(current-buffer)))))))
266 (defun quickurl-load-urls ()
267 "Load the contents of `quickurl-url-file' into `quickurl-urls'."
268 (when (file-exists-p quickurl-url-file
)
270 (insert-file-contents quickurl-url-file
)
273 (defun quickurl-save-urls ()
274 "Save the contents of `quickurl-urls' to `quickurl-url-file'."
276 (let ((standard-output (current-buffer))
278 (princ quickurl-prefix
)
280 (princ quickurl-postfix
)
281 (write-region (point-min) (point-max) quickurl-url-file nil
0))))
283 (defun quickurl-find-url (lookup)
284 "Return URL associated with key LOOKUP.
286 The lookup is done by looking in the alist `quickurl-urls' and the `cons'
287 for the URL is returned. The actual method used to look into the alist
288 depends on the setting of the variable `quickurl-assoc-function'."
289 (funcall quickurl-assoc-function lookup quickurl-urls
))
291 (defun quickurl-insert (url &optional silent
)
292 "Insert URL, formatted using `quickurl-format-function'.
294 Also display a `message' saying what the URL was unless SILENT is non-nil."
295 (insert (funcall quickurl-format-function url
))
297 (message "Found %s" (quickurl-url-url url
))))
300 (cl-defun quickurl (&optional lookup
)
301 "Insert a URL based on LOOKUP.
303 If not supplied LOOKUP is taken to be the word at point in the current
304 buffer, this default action can be modified via
305 `quickurl-grab-lookup-function'."
308 (setq lookup
(funcall quickurl-grab-lookup-function
)))
310 (let ((url (quickurl-find-url lookup
)))
312 (error "No URL associated with \"%s\"" lookup
)
313 (when (looking-at "\\w")
314 (skip-syntax-forward "\\w"))
316 (quickurl-insert url
)))))
319 (defun quickurl-ask (lookup)
320 "Insert a URL, with `completing-read' prompt, based on LOOKUP."
325 (let ((completion-ignore-case quickurl-completion-ignore-case
))
326 (completing-read "Lookup: " quickurl-urls nil t
)))))
327 (let ((url (quickurl-find-url lookup
)))
329 (quickurl-insert url
))))
331 (defun quickurl-grab-url ()
332 "Attempt to grab a word/URL pair from point in the current buffer.
334 Point should be somewhere on the URL and the word is taken to be the thing
335 that is returned from calling `quickurl-grab-lookup-function' once a
336 `backward-word' has been issued at the start of the URL.
338 It is assumed that the URL is either \"unguarded\" or is wrapped inside an
340 (let ((url (thing-at-point 'url
)))
343 (beginning-of-thing 'url
)
344 ;; `beginning-of-thing' doesn't take you to the start of a marked-up
345 ;; URL, only to the start of the URL within the "markup". So, we
346 ;; need to do a little more work to get to where we want to be.
347 (when (thing-at-point-looking-at thing-at-point-markedup-url-regexp
)
348 (search-backward "<URL:"))
350 (let ((word (funcall quickurl-grab-lookup-function
)))
353 ;; The grab function may return the word with properties. I don't
354 ;; want the properties. I couldn't find a method of stripping
355 ;; them from a "string" so this will have to do. If you know of
356 ;; a better method of doing this I'd love to know.
359 (buffer-substring-no-properties (point-min) (point-max)))
363 (defun quickurl-add-url (word url comment
)
364 "Allow the user to interactively add a new URL associated with WORD.
366 See `quickurl-grab-url' for details on how the default word/URL combination
368 (interactive (let ((word-url (quickurl-grab-url)))
369 (list (read-string "Word: " (quickurl-url-keyword word-url
))
370 (read-string "URL: " (quickurl-url-url word-url
))
371 (read-string "Comment: " (quickurl-url-comment word-url
)))))
372 (if (zerop (length word
))
373 (error "You must specify a WORD for lookup")
375 (let* ((current-url (quickurl-find-url word
))
376 (add-it (if current-url
377 (if (called-interactively-p 'interactive
)
378 (y-or-n-p (format "\"%s\" exists, replace URL? " word
))
384 (setf (quickurl-url-url current-url
) url
)
385 (setf (quickurl-url-comment current-url
) comment
))
386 (push (quickurl-make-url word url comment
) quickurl-urls
))
387 (setq quickurl-urls
(funcall quickurl-sort-function quickurl-urls
))
389 (when (get-buffer quickurl-list-buffer-name
)
390 (quickurl-list-populate-buffer))
391 (when (called-interactively-p 'interactive
)
392 (message "Added %s" url
))))))
395 (defun quickurl-browse-url (&optional lookup
)
396 "Browse the URL associated with LOOKUP.
398 If not supplied LOOKUP is taken to be the word at point in the
399 current buffer, this default action can be modified via
400 `quickurl-grab-lookup-function'."
403 (setq lookup
(funcall quickurl-grab-lookup-function
)))
405 (let ((url (quickurl-find-url lookup
)))
407 (browse-url (quickurl-url-url url
))
408 (error "No URL associated with \"%s\"" lookup
)))))
411 (defun quickurl-browse-url-ask (lookup)
412 "Browse the URL, with `completing-read' prompt, associated with LOOKUP."
416 (completing-read "Browse: " quickurl-urls nil t
))))
417 (let ((url (quickurl-find-url lookup
)))
419 (browse-url (quickurl-url-url url
)))))
422 (defun quickurl-edit-urls ()
423 "Pull `quickurl-url-file' into a buffer for hand editing."
425 (find-file quickurl-url-file
))
427 ;; quickurl-list mode.
429 (put 'quickurl-list-mode
'mode-class
'special
)
432 (defun quickurl-list-mode ()
433 "A mode for browsing the quickurl URL list.
435 The key bindings for `quickurl-list-mode' are:
437 \\{quickurl-list-mode-map}"
439 (kill-all-local-variables)
440 (use-local-map quickurl-list-mode-map
)
441 (setq major-mode
'quickurl-list-mode
442 mode-name
"quickurl list")
443 (run-mode-hooks 'quickurl-list-mode-hook
)
444 (setq buffer-read-only t
448 (defun quickurl-list ()
449 "Display `quickurl-list' as a formatted list using `quickurl-list-mode'."
452 (unless (string= (buffer-name) quickurl-list-buffer-name
)
453 (setq quickurl-list-last-buffer
(current-buffer)))
454 (pop-to-buffer quickurl-list-buffer-name
)
455 (quickurl-list-populate-buffer)
456 (quickurl-list-mode))
458 (defun quickurl-list-populate-buffer ()
459 "Populate the `quickurl-list' buffer."
460 (with-current-buffer (get-buffer quickurl-list-buffer-name
)
461 (let* ((sizes (or (cl-loop for url in quickurl-urls
462 collect
(length (quickurl-url-description url
)))
464 (fmt (format "%%-%ds %%s\n" (apply #'max sizes
)))
465 (inhibit-read-only t
))
467 (cl-loop for url in quickurl-urls
468 do
(let ((start (point)))
469 (insert (format fmt
(quickurl-url-description url
)
470 (quickurl-url-url url
)))
473 '(mouse-face highlight
474 help-echo
"mouse-2: insert this URL"))))
475 (goto-char (point-min)))))
477 (defun quickurl-list-add-url (word url comment
)
478 "Wrapper for `quickurl-add-url' that doesn't guess the parameters."
479 (interactive "sWord: \nsURL: \nsComment: ")
480 (quickurl-add-url word url comment
))
482 (defun quickurl-list-quit ()
483 "Kill the buffer named `quickurl-list-buffer-name'."
485 (kill-buffer quickurl-list-buffer-name
)
486 (switch-to-buffer quickurl-list-last-buffer
)
487 (delete-other-windows))
489 (defun quickurl-list-mouse-select (event)
490 "Select the URL under the mouse click."
492 (goto-char (posn-point (event-end event
)))
493 (quickurl-list-insert-url))
495 (defun quickurl-list-insert (type)
496 "Insert the URL under cursor into `quickurl-list-last-buffer'.
497 TYPE dictates what will be inserted, options are:
498 `url' - Insert the URL as <URL:url>
499 `naked-url' - Insert the URL with no formatting
500 `with-lookup' - Insert \"lookup <URL:url>\"
501 `with-desc' - Insert \"description <URL:url>\"
502 `lookup' - Insert the lookup for that URL"
503 (let ((url (nth (count-lines (point-min) (line-beginning-position))
506 (with-current-buffer quickurl-list-last-buffer
509 (`url
(funcall quickurl-format-function url
))
510 (`naked-url
(quickurl-url-url url
))
511 (`with-lookup
(format "%s <URL:%s>"
512 (quickurl-url-keyword url
)
513 (quickurl-url-url url
)))
514 (`with-desc
(format "%S <URL:%s>"
515 (quickurl-url-description url
)
516 (quickurl-url-url url
)))
517 (`lookup
(quickurl-url-keyword url
)))))
518 (error "No URL details on that line"))
521 (defmacro quickurl-list-make-inserter
(type)
522 "Macro to make a key-response function for use in `quickurl-list-mode-map'."
523 `(defun ,(intern (format "quickurl-list-insert-%S" type
)) ()
524 ,(format "Insert the result of calling `quickurl-list-insert' with `%s'." type
)
526 (when (quickurl-list-insert ',type
)
527 (quickurl-list-quit))))
529 (quickurl-list-make-inserter url
)
530 (quickurl-list-make-inserter naked-url
)
531 (quickurl-list-make-inserter with-lookup
)
532 (quickurl-list-make-inserter with-desc
)
533 (quickurl-list-make-inserter lookup
)
537 ;;; quickurl.el ends here