* lisp/image-mode.el (image-mode-winprops): Add winprops to
[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
940e5099
SM
97(defcustom quickurl-url-file
98 (locate-user-emacs-file "quickurls" ".quickurls")
fb7ada5f 99 "File that contains the URL list."
8749abea
GM
100 :type 'file
101 :group 'quickurl)
102
67ba52a6 103(defcustom quickurl-format-function (lambda (url) (format "<URL:%s>" (quickurl-url-url url)))
fb7ada5f 104 "Function to format the URL before insertion into the current buffer."
8749abea
GM
105 :type 'function
106 :group 'quickurl)
107
108(defcustom quickurl-sort-function (lambda (list)
109 (sort list
110 (lambda (x y)
111 (string<
112 (downcase (quickurl-url-description x))
113 (downcase (quickurl-url-description y))))))
fb7ada5f 114 "Function to sort the URL list."
8749abea
GM
115 :type 'function
116 :group 'quickurl)
117
118(defcustom quickurl-grab-lookup-function #'current-word
fb7ada5f 119 "Function to grab the thing to lookup."
8749abea
GM
120 :type 'function
121 :group 'quickurl)
122
123(defcustom quickurl-assoc-function #'assoc-ignore-case
fb7ada5f 124 "Function to use for alist lookup into `quickurl-urls'."
8749abea
GM
125 :type 'function
126 :group 'quickurl)
127
128(defcustom quickurl-completion-ignore-case t
fb7ada5f 129 "Should `quickurl-ask' ignore case when doing the input lookup?"
8749abea
GM
130 :type 'boolean
131 :group 'quickurl)
132
133(defcustom quickurl-prefix ";; -*- lisp -*-\n\n"
fb7ada5f 134 "Text to write to `quickurl-url-file' before writing the URL list."
8749abea
GM
135 :type 'string
136 :group 'quickurl)
137
138(defcustom quickurl-postfix ""
fb7ada5f 139 "Text to write to `quickurl-url-file' after writing the URL list.
8749abea
GM
140
141See the constant `quickurl-reread-hook-postfix' for some example text that
142could be used here."
143 :type 'string
144 :group 'quickurl)
145
146(defcustom quickurl-list-mode-hook nil
fb7ada5f 147 "Hooks for `quickurl-list-mode'."
8749abea
GM
148 :type 'hook
149 :group 'quickurl)
150
151;; Constants.
152
153;;;###autoload
154(defconst quickurl-reread-hook-postfix
155 "
156;; Local Variables:
157;; eval: (progn (require 'quickurl) (add-hook 'local-write-file-hooks (lambda () (quickurl-read) nil)))
158;; End:
159"
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.
163
164To make use of this do something like:
165
166 (setq quickurl-postfix quickurl-reread-hook-postfix)
167
865fe16f 168in your init file (after loading/requiring quickurl).")
8749abea
GM
169
170;; Non-customize variables.
171
172(defvar quickurl-urls nil
173 "URL alist for use with `quickurl' and `quickurl-ask'.")
174
a0310a6c
DN
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)
188 map)
8749abea
GM
189 "Local keymap for a `quickurl-list-mode' buffer.")
190
191(defvar quickurl-list-buffer-name "*quickurl-list*"
c7015153 192 "Name for the URL listing buffer.")
8749abea
GM
193
194(defvar quickurl-list-last-buffer nil
195 "`current-buffer' when `quickurl-list' was called.")
196
a2ebe600 197;; Functions for working with a URL entry.
8749abea
GM
198
199(defun quickurl-url-commented-p (url)
200 "Does the URL have a comment?"
201 (listp (cdr url)))
202
203(defun quickurl-make-url (keyword url &optional comment)
e1dbe924 204 "Create a URL from KEYWORD, URL and (optionally) COMMENT."
8749abea
GM
205 (if (and comment (not (zerop (length comment))))
206 (list keyword url comment)
207 (cons keyword url)))
208
a464a6c7 209(defalias 'quickurl-url-keyword #'car
8749abea 210 "Return the keyword for the URL.
a464a6c7 211\n\(fn URL)")
8749abea
GM
212
213(defun quickurl-url-url (url)
214 "Return the actual URL of the URL.
215
216Note that this function is a setfable place."
a464a6c7
SM
217 (declare (gv-setter (lambda (store)
218 `(setf (if (quickurl-url-commented-p ,url)
219 (cadr ,url)
220 (cdr ,url))
221 ,store))))
8749abea
GM
222 (if (quickurl-url-commented-p url)
223 (cadr url)
224 (cdr url)))
225
8749abea 226(defun quickurl-url-comment (url)
a2ebe600 227 "Get the comment from a URL.
8749abea
GM
228
229If the URL has no comment an empty string is returned. Also note that this
230function is a setfable place."
a464a6c7
SM
231 (declare
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)))))))
8749abea
GM
239 (if (quickurl-url-commented-p url)
240 (nth 2 url)
241 ""))
242
8749abea
GM
243(defun quickurl-url-description (url)
244 "Return a description for the URL.
245
246If the URL has a comment then this is returned, otherwise the keyword is
247returned."
248 (let ((desc (quickurl-url-comment url)))
249 (if (zerop (length desc))
250 (quickurl-url-keyword url)
251 desc)))
252
253;; Main code:
254
a464a6c7 255(cl-defun quickurl-read (&optional buffer)
8749abea
GM
256 "`read' the URL list from BUFFER into `quickurl-urls'.
257
d0aa1aab 258BUFFER, if nil, defaults to current buffer.
8749abea
GM
259Note that this function moves point to `point-min' before doing the `read'
260It also restores point after the `read'."
261 (save-excursion
a464a6c7 262 (goto-char (point-min))
d0aa1aab
JB
263 (setq quickurl-urls (funcall quickurl-sort-function
264 (read (or buffer (current-buffer)))))))
a1506d29 265
8749abea
GM
266(defun quickurl-load-urls ()
267 "Load the contents of `quickurl-url-file' into `quickurl-urls'."
268 (when (file-exists-p quickurl-url-file)
269 (with-temp-buffer
270 (insert-file-contents quickurl-url-file)
271 (quickurl-read))))
272
273(defun quickurl-save-urls ()
274 "Save the contents of `quickurl-urls' to `quickurl-url-file'."
275 (with-temp-buffer
3ca2c015
DB
276 (let ((standard-output (current-buffer))
277 (print-length nil))
8749abea
GM
278 (princ quickurl-prefix)
279 (pp quickurl-urls)
280 (princ quickurl-postfix)
281 (write-region (point-min) (point-max) quickurl-url-file nil 0))))
a1506d29 282
8749abea
GM
283(defun quickurl-find-url (lookup)
284 "Return URL associated with key LOOKUP.
285
286The lookup is done by looking in the alist `quickurl-urls' and the `cons'
287for the URL is returned. The actual method used to look into the alist
288depends on the setting of the variable `quickurl-assoc-function'."
289 (funcall quickurl-assoc-function lookup quickurl-urls))
290
291(defun quickurl-insert (url &optional silent)
292 "Insert URL, formatted using `quickurl-format-function'.
293
294Also display a `message' saying what the URL was unless SILENT is non-nil."
67ba52a6 295 (insert (funcall quickurl-format-function url))
8749abea
GM
296 (unless silent
297 (message "Found %s" (quickurl-url-url url))))
298
299;;;###autoload
a464a6c7 300(cl-defun quickurl (&optional lookup)
a2ebe600 301 "Insert a URL based on LOOKUP.
8749abea
GM
302
303If not supplied LOOKUP is taken to be the word at point in the current
a239d4e9 304buffer, this default action can be modified via
8749abea
GM
305`quickurl-grab-lookup-function'."
306 (interactive)
d0aa1aab
JB
307 (when (or lookup
308 (setq lookup (funcall quickurl-grab-lookup-function)))
8749abea
GM
309 (quickurl-load-urls)
310 (let ((url (quickurl-find-url lookup)))
311 (if (null url)
312 (error "No URL associated with \"%s\"" lookup)
313 (when (looking-at "\\w")
314 (skip-syntax-forward "\\w"))
315 (insert " ")
316 (quickurl-insert url)))))
317
318;;;###autoload
319(defun quickurl-ask (lookup)
a2ebe600 320 "Insert a URL, with `completing-read' prompt, based on LOOKUP."
8749abea
GM
321 (interactive
322 (list
323 (progn
324 (quickurl-load-urls)
325 (let ((completion-ignore-case quickurl-completion-ignore-case))
326 (completing-read "Lookup: " quickurl-urls nil t)))))
327 (let ((url (quickurl-find-url lookup)))
328 (when url
329 (quickurl-insert url))))
a1506d29 330
8749abea 331(defun quickurl-grab-url ()
a2ebe600 332 "Attempt to grab a word/URL pair from point in the current buffer.
8749abea
GM
333
334Point should be somewhere on the URL and the word is taken to be the thing
335that is returned from calling `quickurl-grab-lookup-function' once a
336`backward-word' has been issued at the start of the URL.
337
338It is assumed that the URL is either \"unguarded\" or is wrapped inside an
339<URL:...> wrapper."
340 (let ((url (thing-at-point 'url)))
341 (when url
342 (save-excursion
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:"))
349 (backward-word 1)
350 (let ((word (funcall quickurl-grab-lookup-function)))
351 (when word
352 (quickurl-make-url
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.
357 (with-temp-buffer
358 (insert word)
359 (buffer-substring-no-properties (point-min) (point-max)))
360 url)))))))
361
362;;;###autoload
363(defun quickurl-add-url (word url comment)
364 "Allow the user to interactively add a new URL associated with WORD.
365
a2ebe600 366See `quickurl-grab-url' for details on how the default word/URL combination
8749abea
GM
367is decided."
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))
a1506d29 373 (error "You must specify a WORD for lookup")
8749abea
GM
374 (quickurl-load-urls)
375 (let* ((current-url (quickurl-find-url word))
376 (add-it (if current-url
32226619 377 (if (called-interactively-p 'interactive)
8749abea
GM
378 (y-or-n-p (format "\"%s\" exists, replace URL? " word))
379 t)
380 t)))
381 (when add-it
382 (if current-url
383 (progn
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))
388 (quickurl-save-urls)
389 (when (get-buffer quickurl-list-buffer-name)
390 (quickurl-list-populate-buffer))
32226619 391 (when (called-interactively-p 'interactive)
8749abea
GM
392 (message "Added %s" url))))))
393
394;;;###autoload
d0aa1aab 395(defun quickurl-browse-url (&optional lookup)
8749abea
GM
396 "Browse the URL associated with LOOKUP.
397
398If not supplied LOOKUP is taken to be the word at point in the
a239d4e9 399current buffer, this default action can be modified via
8749abea
GM
400`quickurl-grab-lookup-function'."
401 (interactive)
d0aa1aab
JB
402 (when (or lookup
403 (setq lookup (funcall quickurl-grab-lookup-function)))
8749abea
GM
404 (quickurl-load-urls)
405 (let ((url (quickurl-find-url lookup)))
406 (if url
407 (browse-url (quickurl-url-url url))
408 (error "No URL associated with \"%s\"" lookup)))))
409
410;;;###autoload
411(defun quickurl-browse-url-ask (lookup)
412 "Browse the URL, with `completing-read' prompt, associated with LOOKUP."
413 (interactive (list
414 (progn
415 (quickurl-load-urls)
416 (completing-read "Browse: " quickurl-urls nil t))))
417 (let ((url (quickurl-find-url lookup)))
418 (when url
419 (browse-url (quickurl-url-url url)))))
420
421;;;###autoload
422(defun quickurl-edit-urls ()
423 "Pull `quickurl-url-file' into a buffer for hand editing."
424 (interactive)
425 (find-file quickurl-url-file))
426
427;; quickurl-list mode.
428
8749abea
GM
429(put 'quickurl-list-mode 'mode-class 'special)
430
431;;;###autoload
432(defun quickurl-list-mode ()
433 "A mode for browsing the quickurl URL list.
434
435The key bindings for `quickurl-list-mode' are:
436
437\\{quickurl-list-mode-map}"
438 (interactive)
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")
57f9923e 443 (run-mode-hooks 'quickurl-list-mode-hook)
8749abea
GM
444 (setq buffer-read-only t
445 truncate-lines t))
446
447;;;###autoload
448(defun quickurl-list ()
449 "Display `quickurl-list' as a formatted list using `quickurl-list-mode'."
450 (interactive)
451 (quickurl-load-urls)
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))
457
458(defun quickurl-list-populate-buffer ()
459 "Populate the `quickurl-list' buffer."
460 (with-current-buffer (get-buffer quickurl-list-buffer-name)
a464a6c7
SM
461 (let* ((sizes (or (cl-loop for url in quickurl-urls
462 collect (length (quickurl-url-description url)))
463 (list 20)))
464 (fmt (format "%%-%ds %%s\n" (apply #'max sizes)))
465 (inhibit-read-only t))
466 (erase-buffer)
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)))
471 (add-text-properties
472 start (1- (point))
473 '(mouse-face highlight
474 help-echo "mouse-2: insert this URL"))))
475 (goto-char (point-min)))))
8749abea
GM
476
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))
481
482(defun quickurl-list-quit ()
483 "Kill the buffer named `quickurl-list-buffer-name'."
484 (interactive)
485 (kill-buffer quickurl-list-buffer-name)
486 (switch-to-buffer quickurl-list-last-buffer)
487 (delete-other-windows))
488
489(defun quickurl-list-mouse-select (event)
490 "Select the URL under the mouse click."
491 (interactive "e")
a464a6c7 492 (goto-char (posn-point (event-end event)))
8749abea
GM
493 (quickurl-list-insert-url))
494
495(defun quickurl-list-insert (type)
496 "Insert the URL under cursor into `quickurl-list-last-buffer'.
497TYPE 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"
9b026d9f 503 (let ((url (nth (count-lines (point-min) (line-beginning-position))
8749abea
GM
504 quickurl-urls)))
505 (if url
506 (with-current-buffer quickurl-list-last-buffer
507 (insert
a464a6c7
SM
508 (pcase type
509 (`url (funcall quickurl-format-function url))
510 (`naked-url (quickurl-url-url url))
511 (`with-lookup (format "%s <URL:%s>"
8749abea
GM
512 (quickurl-url-keyword url)
513 (quickurl-url-url url)))
a464a6c7 514 (`with-desc (format "%S <URL:%s>"
8749abea
GM
515 (quickurl-url-description url)
516 (quickurl-url-url url)))
a464a6c7 517 (`lookup (quickurl-url-keyword url)))))
8749abea
GM
518 (error "No URL details on that line"))
519 url))
520
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)
525 (interactive)
526 (when (quickurl-list-insert ',type)
527 (quickurl-list-quit))))
528
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)
a1506d29 534
8749abea
GM
535(provide 'quickurl)
536
537;;; quickurl.el ends here