]> code.delx.au - gnu-emacs/blobdiff - lisp/net/quickurl.el
Merge from trunk.
[gnu-emacs] / lisp / net / quickurl.el
index 0751aec4286946e0f83ed639e61f5b1b2fdc6e01..7a1e5fc6a80d047282dce47dac427258144343c4 100644 (file)
@@ -1,6 +1,6 @@
-;;; quickurl.el --- insert an URL based on text at point in buffer
+;;; quickurl.el --- insert a URL based on text at point in buffer
 
-;; Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2011  Free Software Foundation, Inc.
 
 ;; Author: Dave Pearson <davep@davep.org>
 ;; Maintainer: Dave Pearson <davep@davep.org>
@@ -9,10 +9,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 ;;
-;; This package provides a simple method of inserting an URL based on the
+;; This package provides a simple method of inserting a URL based on the
 ;; text at point in the current buffer. This is part of an on-going effort
-;; to increase the information I provide people while reducing the ammount
+;; to increase the information I provide people while reducing the amount
 ;; of typing I need to do. No-doubt there are undiscovered Emacs packages
 ;; out there that do all of this and do it better, feel free to point me to
 ;; them, in the mean time I'm having fun playing with Emacs Lisp.
 (require 'pp)
 (require 'browse-url)
 
-;; Attempt to handle older/other emacs.
-(eval-and-compile
-  ;; If customize isn't available just use defvar instead.
-  (unless (fboundp 'defgroup)
-    (defmacro defgroup  (&rest rest) nil)
-    (defmacro defcustom (symbol init docstring &rest rest)
-      `(defvar ,symbol ,init ,docstring))))
-
 ;; Customize options.
 
 (defgroup quickurl nil
-  "Insert an URL based on text at point in buffer."
+  "Insert a URL based on text at point in buffer."
   :version "21.1"
   :group  'abbrev
   :prefix "quickurl-")
@@ -182,23 +172,36 @@ in your ~/.emacs (after loading/requiring quickurl).")
 (defvar quickurl-urls nil
   "URL alist for use with `quickurl' and `quickurl-ask'.")
 
-(defvar quickurl-list-mode-map nil
+(defvar quickurl-list-mode-map
+  (let ((map (make-sparse-keymap)))
+    (suppress-keymap map t)
+    (define-key map "a"           #'quickurl-list-add-url)
+    (define-key map [(control m)] #'quickurl-list-insert-url)
+    (define-key map "u"           #'quickurl-list-insert-naked-url)
+    (define-key map " "           #'quickurl-list-insert-with-lookup)
+    (define-key map "l"           #'quickurl-list-insert-lookup)
+    (define-key map "d"           #'quickurl-list-insert-with-desc)
+    (define-key map [(control g)] #'quickurl-list-quit)
+    (define-key map "q"           #'quickurl-list-quit)
+    (define-key map [mouse-2]     #'quickurl-list-mouse-select)
+    (define-key map "?"           #'describe-mode)
+    map)
   "Local keymap for a `quickurl-list-mode' buffer.")
 
 (defvar quickurl-list-buffer-name "*quickurl-list*"
-  "Name for the URL listinig buffer.")
+  "Name for the URL listing buffer.")
 
 (defvar quickurl-list-last-buffer nil
   "`current-buffer' when `quickurl-list' was called.")
 
-;; Functions for working with an URL entry.
+;; Functions for working with a URL entry.
 
 (defun quickurl-url-commented-p (url)
   "Does the URL have a comment?"
   (listp (cdr url)))
 
 (defun quickurl-make-url (keyword url &optional comment)
-  "Create an URL from KEYWORD, URL and (optionaly) COMMENT."
+  "Create a URL from KEYWORD, URL and (optionaly) COMMENT."
   (if (and comment (not (zerop (length comment))))
       (list keyword url comment)
     (cons keyword url)))
@@ -227,7 +230,7 @@ Note that this function is a setfable place."
     (setf (cdr ,url) ,store)))
 
 (defun quickurl-url-comment (url)
-  "Get the comment from an URL.
+  "Get the comment from a URL.
 
 If the URL has no comment an empty string is returned. Also note that this
 function is a setfable place."
@@ -256,15 +259,17 @@ returned."
 
 ;; Main code:
 
-(defun* quickurl-read (&optional (buffer (current-buffer)))
+(defun* quickurl-read (&optional buffer)
   "`read' the URL list from BUFFER into `quickurl-urls'.
 
+BUFFER, if nil, defaults to current buffer.
 Note that this function moves point to `point-min' before doing the `read'
 It also restores point after the `read'."
   (save-excursion
     (setf (point) (point-min))
-    (setq quickurl-urls (funcall quickurl-sort-function (read buffer)))))
-                       
+    (setq quickurl-urls (funcall quickurl-sort-function
+                                 (read (or buffer (current-buffer)))))))
+
 (defun quickurl-load-urls ()
   "Load the contents of `quickurl-url-file' into `quickurl-urls'."
   (when (file-exists-p quickurl-url-file)
@@ -280,7 +285,7 @@ It also restores point after the `read'."
       (pp quickurl-urls)
       (princ quickurl-postfix)
       (write-region (point-min) (point-max) quickurl-url-file nil 0))))
-  
+
 (defun quickurl-find-url (lookup)
   "Return URL associated with key LOOKUP.
 
@@ -298,14 +303,15 @@ Also display a `message' saying what the URL was unless SILENT is non-nil."
     (message "Found %s" (quickurl-url-url url))))
 
 ;;;###autoload
-(defun* quickurl (&optional (lookup (funcall quickurl-grab-lookup-function)))
-  "Insert an URL based on LOOKUP.
+(defun* quickurl (&optional lookup)
+  "Insert a URL based on LOOKUP.
 
 If not supplied LOOKUP is taken to be the word at point in the current
-buffer, this default action can be modifed via
+buffer, this default action can be modified via
 `quickurl-grab-lookup-function'."
   (interactive)
-  (when lookup
+  (when (or lookup
+            (setq lookup (funcall quickurl-grab-lookup-function)))
     (quickurl-load-urls)
     (let ((url (quickurl-find-url lookup)))
       (if (null url)
@@ -317,7 +323,7 @@ buffer, this default action can be modifed via
 
 ;;;###autoload
 (defun quickurl-ask (lookup)
-  "Insert an URL, with `completing-read' prompt, based on LOOKUP."
+  "Insert a URL, with `completing-read' prompt, based on LOOKUP."
   (interactive
    (list
     (progn
@@ -327,9 +333,9 @@ buffer, this default action can be modifed via
   (let ((url (quickurl-find-url lookup)))
     (when url
       (quickurl-insert url))))
-   
+
 (defun quickurl-grab-url ()
-  "Attempt to grab a word/url pair from point in the current buffer.
+  "Attempt to grab a word/URL pair from point in the current buffer.
 
 Point should be somewhere on the URL and the word is taken to be the thing
 that is returned from calling `quickurl-grab-lookup-function' once a
@@ -363,18 +369,18 @@ It is assumed that the URL is either \"unguarded\" or is wrapped inside an
 (defun quickurl-add-url (word url comment)
   "Allow the user to interactively add a new URL associated with WORD.
 
-See `quickurl-grab-url' for details on how the default word/url combination
+See `quickurl-grab-url' for details on how the default word/URL combination
 is decided."
   (interactive (let ((word-url (quickurl-grab-url)))
                  (list (read-string "Word: "    (quickurl-url-keyword word-url))
                        (read-string "URL: "     (quickurl-url-url word-url))
                        (read-string "Comment: " (quickurl-url-comment word-url)))))
   (if (zerop (length word))
-      (error "You must specify a WORD for lookup")  
+      (error "You must specify a WORD for lookup")
     (quickurl-load-urls)
     (let* ((current-url (quickurl-find-url word))
            (add-it      (if current-url
-                            (if (interactive-p)
+                            (if (called-interactively-p 'interactive)
                                 (y-or-n-p (format "\"%s\" exists, replace URL? " word))
                               t)
                           t)))
@@ -388,18 +394,19 @@ is decided."
         (quickurl-save-urls)
         (when (get-buffer quickurl-list-buffer-name)
           (quickurl-list-populate-buffer))
-        (when (interactive-p)
+        (when (called-interactively-p 'interactive)
           (message "Added %s" url))))))
 
 ;;;###autoload
-(defun* quickurl-browse-url (&optional (lookup (funcall quickurl-grab-lookup-function)))
+(defun quickurl-browse-url (&optional lookup)
   "Browse the URL associated with LOOKUP.
 
 If not supplied LOOKUP is taken to be the word at point in the
-current buffer, this default action can be modifed via
+current buffer, this default action can be modified via
 `quickurl-grab-lookup-function'."
   (interactive)
-  (when lookup
+  (when (or lookup
+            (setq lookup (funcall quickurl-grab-lookup-function)))
     (quickurl-load-urls)
     (let ((url (quickurl-find-url lookup)))
       (if url
@@ -425,21 +432,6 @@ current buffer, this default action can be modifed via
 
 ;; quickurl-list mode.
 
-(unless quickurl-list-mode-map
-  (let ((map (make-sparse-keymap)))
-    (suppress-keymap map t)
-    (define-key map "a"           #'quickurl-list-add-url)
-    (define-key map [(control m)] #'quickurl-list-insert-url)
-    (define-key map "u"           #'quickurl-list-insert-naked-url)
-    (define-key map " "           #'quickurl-list-insert-with-lookup)
-    (define-key map "l"           #'quickurl-list-insert-lookup)
-    (define-key map "d"           #'quickurl-list-insert-with-desc)
-    (define-key map [(control g)] #'quickurl-list-quit)
-    (define-key map "q"           #'quickurl-list-quit)
-    (define-key map [mouse-2]     #'quickurl-list-mouse-select)
-    (define-key map "?"           #'describe-mode)
-    (setq quickurl-list-mode-map map)))
-
 (put 'quickurl-list-mode 'mode-class 'special)
 
 ;;;###autoload
@@ -454,7 +446,7 @@ The key bindings for `quickurl-list-mode' are:
   (use-local-map quickurl-list-mode-map)
   (setq major-mode 'quickurl-list-mode
         mode-name  "quickurl list")
-  (run-hooks 'quickurl-list-mode-hook)
+  (run-mode-hooks 'quickurl-list-mode-hook)
   (setq buffer-read-only t
         truncate-lines   t))
 
@@ -513,23 +505,21 @@ TYPE dictates what will be inserted, options are:
   `with-lookup' - Insert \"lookup <URL:url>\"
   `with-desc'   - Insert \"description <URL:url>\"
   `lookup'      - Insert the lookup for that URL"
-  (let ((url (nth (save-excursion
-                    (beginning-of-line)
-                    (count-lines (point-min) (point)))
+  (let ((url (nth (count-lines (point-min) (line-beginning-position))
                   quickurl-urls)))
     (if url
         (with-current-buffer quickurl-list-last-buffer
           (insert
            (case type
-             ('url         (funcall quickurl-format-function url))
-             ('naked-url   (quickurl-url-url url))
-             ('with-lookup (format "%s <URL:%s>"
+             (url         (funcall quickurl-format-function url))
+             (naked-url   (quickurl-url-url url))
+             (with-lookup (format "%s <URL:%s>"
                                    (quickurl-url-keyword url)
                                    (quickurl-url-url url)))
-             ('with-desc   (format "%S <URL:%s>"
+             (with-desc   (format "%S <URL:%s>"
                                    (quickurl-url-description url)
                                    (quickurl-url-url url)))
-             ('lookup      (quickurl-url-keyword url)))))
+             (lookup      (quickurl-url-keyword url)))))
       (error "No URL details on that line"))
     url))
 
@@ -546,7 +536,7 @@ TYPE dictates what will be inserted, options are:
 (quickurl-list-make-inserter with-lookup)
 (quickurl-list-make-inserter with-desc)
 (quickurl-list-make-inserter lookup)
-  
+
 (provide 'quickurl)
 
 ;;; quickurl.el ends here