Mercurial > emacs
annotate lisp/net/quickurl.el @ 112332:28ca83ef1128
Merge from mainline.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 17 Jan 2011 11:24:36 -0800 |
parents | 61f7601898b1 |
children |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38086
diff
changeset
|
1 ;;; quickurl.el --- insert an URL based on text at point in buffer |
28210 | 2 |
112284
61f7601898b1
Refill some copyright headers.
Glenn Morris <rgm@gnu.org>
parents:
112228
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
61f7601898b1
Refill some copyright headers.
Glenn Morris <rgm@gnu.org>
parents:
112228
diff
changeset
|
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
28210 | 5 |
28285
c54d62415e91
Changed the type of parameter passed to the
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
6 ;; Author: Dave Pearson <davep@davep.org> |
c54d62415e91
Changed the type of parameter passed to the
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
7 ;; Maintainer: Dave Pearson <davep@davep.org> |
28210 | 8 ;; Created: 1999-05-28 |
9 ;; Keywords: hypermedia | |
10 | |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38086
diff
changeset
|
11 ;; This file is part of GNU Emacs. |
28210 | 12 |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; GNU Emacs is free software: you can redistribute it and/or modify |
28210 | 14 ;; it under the terms of the GNU General Public License as published by |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; the Free Software Foundation, either version 3 of the License, or |
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
16 ;; (at your option) any later version. |
28210 | 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 | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
28210 | 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/") | |
38086
50acec1e0d30
(quickurl-url-file): Run through convert-standard-filename.
Eli Zaretskii <eliz@gnu.org>
parents:
34809
diff
changeset
|
61 ;; ("davep" "http://www.davep.org/" "Dave's homepage")) |
28210 | 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 ;; | |
34809
5131c8bca419
Commentry change, I've moved my web site.
Gerd Moellmann <gerd@gnu.org>
parents:
28285
diff
changeset
|
73 ;; <URL:http://www.davep.org/emacs/quickurl.el> |
28210 | 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 | |
38086
50acec1e0d30
(quickurl-url-file): Run through convert-standard-filename.
Eli Zaretskii <eliz@gnu.org>
parents:
34809
diff
changeset
|
99 (defcustom quickurl-url-file (convert-standard-filename "~/.quickurls") |
28210 | 100 "*File that contains the URL list." |
101 :type 'file | |
102 :group 'quickurl) | |
103 | |
28285
c54d62415e91
Changed the type of parameter passed to the
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
104 (defcustom quickurl-format-function (lambda (url) (format "<URL:%s>" (quickurl-url-url url))) |
28210 | 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 | |
110909
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
176 (defvar quickurl-list-mode-map |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
177 (let ((map (make-sparse-keymap))) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
178 (suppress-keymap map t) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
179 (define-key map "a" #'quickurl-list-add-url) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
180 (define-key map [(control m)] #'quickurl-list-insert-url) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
181 (define-key map "u" #'quickurl-list-insert-naked-url) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
182 (define-key map " " #'quickurl-list-insert-with-lookup) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
183 (define-key map "l" #'quickurl-list-insert-lookup) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
184 (define-key map "d" #'quickurl-list-insert-with-desc) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
185 (define-key map [(control g)] #'quickurl-list-quit) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
186 (define-key map "q" #'quickurl-list-quit) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
187 (define-key map [mouse-2] #'quickurl-list-mouse-select) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
188 (define-key map "?" #'describe-mode) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
189 map) |
28210 | 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 | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
263 (defun* quickurl-read (&optional buffer) |
28210 | 264 "`read' the URL list from BUFFER into `quickurl-urls'. |
265 | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
266 BUFFER, if nil, defaults to current buffer. |
28210 | 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)) | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
271 (setq quickurl-urls (funcall quickurl-sort-function |
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
272 (read (or buffer (current-buffer))))))) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38436
diff
changeset
|
273 |
28210 | 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)))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38436
diff
changeset
|
289 |
28210 | 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." | |
28285
c54d62415e91
Changed the type of parameter passed to the
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
302 (insert (funcall quickurl-format-function url)) |
28210 | 303 (unless silent |
304 (message "Found %s" (quickurl-url-url url)))) | |
305 | |
306 ;;;###autoload | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
307 (defun* quickurl (&optional lookup) |
28210 | 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) | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
314 (when (or lookup |
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
315 (setq lookup (funcall quickurl-grab-lookup-function))) |
28210 | 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)))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38436
diff
changeset
|
337 |
28210 | 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)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38436
diff
changeset
|
380 (error "You must specify a WORD for lookup") |
28210 | 381 (quickurl-load-urls) |
382 (let* ((current-url (quickurl-find-url word)) | |
383 (add-it (if current-url | |
105372
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
100908
diff
changeset
|
384 (if (called-interactively-p 'interactive) |
28210 | 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)) | |
105372
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
100908
diff
changeset
|
398 (when (called-interactively-p 'interactive) |
28210 | 399 (message "Added %s" url)))))) |
400 | |
401 ;;;###autoload | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
402 (defun quickurl-browse-url (&optional lookup) |
28210 | 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) | |
55459
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
409 (when (or lookup |
4a7badfefc94
(quickurl, quickurl-browse-url, quickurl-read):
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
410 (setq lookup (funcall quickurl-grab-lookup-function))) |
28210 | 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") | |
62768 | 450 (run-mode-hooks 'quickurl-list-mode-hook) |
28210 | 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))) | |
38086
50acec1e0d30
(quickurl-url-file): Run through convert-standard-filename.
Eli Zaretskii <eliz@gnu.org>
parents:
34809
diff
changeset
|
478 (add-text-properties start (1- (point)) |
50acec1e0d30
(quickurl-url-file): Run through convert-standard-filename.
Eli Zaretskii <eliz@gnu.org>
parents:
34809
diff
changeset
|
479 '(mouse-face highlight |
50acec1e0d30
(quickurl-url-file): Run through convert-standard-filename.
Eli Zaretskii <eliz@gnu.org>
parents:
34809
diff
changeset
|
480 help-echo "mouse-2: insert this URL")))) |
28210 | 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" | |
111445
56b71cddc9c5
Replace still more end-of-line etc with line-end-position, etc.
Glenn Morris <rgm@gnu.org>
parents:
110909
diff
changeset
|
509 (let ((url (nth (count-lines (point-min) (line-beginning-position)) |
28210 | 510 quickurl-urls))) |
511 (if url | |
512 (with-current-buffer quickurl-list-last-buffer | |
513 (insert | |
514 (case type | |
28285
c54d62415e91
Changed the type of parameter passed to the
Gerd Moellmann <gerd@gnu.org>
parents:
28210
diff
changeset
|
515 ('url (funcall quickurl-format-function url)) |
28210 | 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) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38436
diff
changeset
|
540 |
28210 | 541 (provide 'quickurl) |
542 | |
543 ;;; quickurl.el ends here |