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