quickurl.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. ;;; quickurl.el --- insert a URL based on text at point in buffer
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Pearson <davep@davep.org>
  4. ;; Maintainer: Dave Pearson <davep@davep.org>
  5. ;; Created: 1999-05-28
  6. ;; Keywords: hypermedia
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; This package provides a simple method of inserting a URL based on the
  21. ;; text at point in the current buffer. This is part of an on-going effort
  22. ;; to increase the information I provide people while reducing the amount
  23. ;; of typing I need to do. No-doubt there are undiscovered Emacs packages
  24. ;; out there that do all of this and do it better, feel free to point me to
  25. ;; them, in the mean time I'm having fun playing with Emacs Lisp.
  26. ;;
  27. ;; The URLs are stored in an external file as a list of either cons cells,
  28. ;; or lists. A cons cell entry looks like this:
  29. ;;
  30. ;; (<Lookup> . <URL>)
  31. ;;
  32. ;; where <Lookup> is a string that acts as the keyword lookup and <URL> is
  33. ;; the URL associated with it. An example might be:
  34. ;;
  35. ;; ("GNU" . "http://www.gnu.org/")
  36. ;;
  37. ;; A list entry looks like:
  38. ;;
  39. ;; (<Lookup> <URL> <Comment>)
  40. ;;
  41. ;; where <Lookup> and <URL> are the same as with the cons cell and <Comment>
  42. ;; is any text you like that describes the URL. This description will be
  43. ;; used when presenting a list of URLS using `quickurl-list'. An example
  44. ;; might be:
  45. ;;
  46. ;; ("FSF" "http://www.fsf.org/" "The Free Software Foundation")
  47. ;;
  48. ;; Given the above, your quickurl file might look like:
  49. ;;
  50. ;; (("GNU" . "http://www.gnu.org/")
  51. ;; ("FSF" "http://www.fsf.org/" "The Free Software Foundation")
  52. ;; ("emacs" . "http://www.emacs.org/")
  53. ;; ("davep" "http://www.davep.org/" "Dave's homepage"))
  54. ;;
  55. ;; In case you're wondering about the mixture of cons cells and lists,
  56. ;; quickurl started life using just the cons cells, there were no comments.
  57. ;; URL comments are a later addition and so there is a mixture to keep
  58. ;; backward compatibility with existing URL lists.
  59. ;;
  60. ;; The name and location of the file is up to you, the default name used by
  61. ;; `quickurl' is stored in `quickurl-url-file'.
  62. ;;
  63. ;; quickurl is always available from:
  64. ;;
  65. ;; <URL:http://www.davep.org/emacs/quickurl.el>
  66. ;;; TODO:
  67. ;;
  68. ;; o The quickurl-browse-url* functions pretty much duplicate their non
  69. ;; browsing friends. It would feel better if a more generic solution could
  70. ;; be found.
  71. ;;; Code:
  72. ;; Things we need:
  73. (eval-when-compile
  74. (require 'cl))
  75. (require 'thingatpt)
  76. (require 'pp)
  77. (require 'browse-url)
  78. ;; Customize options.
  79. (defgroup quickurl nil
  80. "Insert a URL based on text at point in buffer."
  81. :version "21.1"
  82. :group 'abbrev
  83. :prefix "quickurl-")
  84. (defcustom quickurl-url-file (convert-standard-filename "~/.quickurls")
  85. "*File that contains the URL list."
  86. :type 'file
  87. :group 'quickurl)
  88. (defcustom quickurl-format-function (lambda (url) (format "<URL:%s>" (quickurl-url-url url)))
  89. "*Function to format the URL before insertion into the current buffer."
  90. :type 'function
  91. :group 'quickurl)
  92. (defcustom quickurl-sort-function (lambda (list)
  93. (sort list
  94. (lambda (x y)
  95. (string<
  96. (downcase (quickurl-url-description x))
  97. (downcase (quickurl-url-description y))))))
  98. "*Function to sort the URL list."
  99. :type 'function
  100. :group 'quickurl)
  101. (defcustom quickurl-grab-lookup-function #'current-word
  102. "*Function to grab the thing to lookup."
  103. :type 'function
  104. :group 'quickurl)
  105. (defcustom quickurl-assoc-function #'assoc-ignore-case
  106. "*Function to use for alist lookup into `quickurl-urls'."
  107. :type 'function
  108. :group 'quickurl)
  109. (defcustom quickurl-completion-ignore-case t
  110. "*Should `quickurl-ask' ignore case when doing the input lookup?"
  111. :type 'boolean
  112. :group 'quickurl)
  113. (defcustom quickurl-prefix ";; -*- lisp -*-\n\n"
  114. "*Text to write to `quickurl-url-file' before writing the URL list."
  115. :type 'string
  116. :group 'quickurl)
  117. (defcustom quickurl-postfix ""
  118. "*Text to write to `quickurl-url-file' after writing the URL list.
  119. See the constant `quickurl-reread-hook-postfix' for some example text that
  120. could be used here."
  121. :type 'string
  122. :group 'quickurl)
  123. (defcustom quickurl-list-mode-hook nil
  124. "*Hooks for `quickurl-list-mode'."
  125. :type 'hook
  126. :group 'quickurl)
  127. ;; Constants.
  128. ;;;###autoload
  129. (defconst quickurl-reread-hook-postfix
  130. "
  131. ;; Local Variables:
  132. ;; eval: (progn (require 'quickurl) (add-hook 'local-write-file-hooks (lambda () (quickurl-read) nil)))
  133. ;; End:
  134. "
  135. "Example `quickurl-postfix' text that adds a local variable to the
  136. `quickurl-url-file' so that if you edit it by hand it will ensure that
  137. `quickurl-urls' is updated with the new URL list.
  138. To make use of this do something like:
  139. (setq quickurl-postfix quickurl-reread-hook-postfix)
  140. in your ~/.emacs (after loading/requiring quickurl).")
  141. ;; Non-customize variables.
  142. (defvar quickurl-urls nil
  143. "URL alist for use with `quickurl' and `quickurl-ask'.")
  144. (defvar quickurl-list-mode-map
  145. (let ((map (make-sparse-keymap)))
  146. (suppress-keymap map t)
  147. (define-key map "a" #'quickurl-list-add-url)
  148. (define-key map [(control m)] #'quickurl-list-insert-url)
  149. (define-key map "u" #'quickurl-list-insert-naked-url)
  150. (define-key map " " #'quickurl-list-insert-with-lookup)
  151. (define-key map "l" #'quickurl-list-insert-lookup)
  152. (define-key map "d" #'quickurl-list-insert-with-desc)
  153. (define-key map [(control g)] #'quickurl-list-quit)
  154. (define-key map "q" #'quickurl-list-quit)
  155. (define-key map [mouse-2] #'quickurl-list-mouse-select)
  156. (define-key map "?" #'describe-mode)
  157. map)
  158. "Local keymap for a `quickurl-list-mode' buffer.")
  159. (defvar quickurl-list-buffer-name "*quickurl-list*"
  160. "Name for the URL listing buffer.")
  161. (defvar quickurl-list-last-buffer nil
  162. "`current-buffer' when `quickurl-list' was called.")
  163. ;; Functions for working with a URL entry.
  164. (defun quickurl-url-commented-p (url)
  165. "Does the URL have a comment?"
  166. (listp (cdr url)))
  167. (defun quickurl-make-url (keyword url &optional comment)
  168. "Create a URL from KEYWORD, URL and (optionally) COMMENT."
  169. (if (and comment (not (zerop (length comment))))
  170. (list keyword url comment)
  171. (cons keyword url)))
  172. (defun quickurl-url-keyword (url)
  173. "Return the keyword for the URL.
  174. Note that this function is a setfable place."
  175. (car url))
  176. (defsetf quickurl-url-keyword (url) (store)
  177. `(setf (car ,url) ,store))
  178. (defun quickurl-url-url (url)
  179. "Return the actual URL of the URL.
  180. Note that this function is a setfable place."
  181. (if (quickurl-url-commented-p url)
  182. (cadr url)
  183. (cdr url)))
  184. (defsetf quickurl-url-url (url) (store)
  185. `
  186. (if (quickurl-url-commented-p ,url)
  187. (setf (cadr ,url) ,store)
  188. (setf (cdr ,url) ,store)))
  189. (defun quickurl-url-comment (url)
  190. "Get the comment from a URL.
  191. If the URL has no comment an empty string is returned. Also note that this
  192. function is a setfable place."
  193. (if (quickurl-url-commented-p url)
  194. (nth 2 url)
  195. ""))
  196. (defsetf quickurl-url-comment (url) (store)
  197. `
  198. (if (quickurl-url-commented-p ,url)
  199. (if (zerop (length ,store))
  200. (setf (cdr ,url) (cadr ,url))
  201. (setf (nth 2 ,url) ,store))
  202. (unless (zerop (length ,store))
  203. (setf (cdr ,url) (list (cdr ,url) ,store)))))
  204. (defun quickurl-url-description (url)
  205. "Return a description for the URL.
  206. If the URL has a comment then this is returned, otherwise the keyword is
  207. returned."
  208. (let ((desc (quickurl-url-comment url)))
  209. (if (zerop (length desc))
  210. (quickurl-url-keyword url)
  211. desc)))
  212. ;; Main code:
  213. (defun* quickurl-read (&optional buffer)
  214. "`read' the URL list from BUFFER into `quickurl-urls'.
  215. BUFFER, if nil, defaults to current buffer.
  216. Note that this function moves point to `point-min' before doing the `read'
  217. It also restores point after the `read'."
  218. (save-excursion
  219. (setf (point) (point-min))
  220. (setq quickurl-urls (funcall quickurl-sort-function
  221. (read (or buffer (current-buffer)))))))
  222. (defun quickurl-load-urls ()
  223. "Load the contents of `quickurl-url-file' into `quickurl-urls'."
  224. (when (file-exists-p quickurl-url-file)
  225. (with-temp-buffer
  226. (insert-file-contents quickurl-url-file)
  227. (quickurl-read))))
  228. (defun quickurl-save-urls ()
  229. "Save the contents of `quickurl-urls' to `quickurl-url-file'."
  230. (with-temp-buffer
  231. (let ((standard-output (current-buffer)))
  232. (princ quickurl-prefix)
  233. (pp quickurl-urls)
  234. (princ quickurl-postfix)
  235. (write-region (point-min) (point-max) quickurl-url-file nil 0))))
  236. (defun quickurl-find-url (lookup)
  237. "Return URL associated with key LOOKUP.
  238. The lookup is done by looking in the alist `quickurl-urls' and the `cons'
  239. for the URL is returned. The actual method used to look into the alist
  240. depends on the setting of the variable `quickurl-assoc-function'."
  241. (funcall quickurl-assoc-function lookup quickurl-urls))
  242. (defun quickurl-insert (url &optional silent)
  243. "Insert URL, formatted using `quickurl-format-function'.
  244. Also display a `message' saying what the URL was unless SILENT is non-nil."
  245. (insert (funcall quickurl-format-function url))
  246. (unless silent
  247. (message "Found %s" (quickurl-url-url url))))
  248. ;;;###autoload
  249. (defun* quickurl (&optional lookup)
  250. "Insert a URL based on LOOKUP.
  251. If not supplied LOOKUP is taken to be the word at point in the current
  252. buffer, this default action can be modified via
  253. `quickurl-grab-lookup-function'."
  254. (interactive)
  255. (when (or lookup
  256. (setq lookup (funcall quickurl-grab-lookup-function)))
  257. (quickurl-load-urls)
  258. (let ((url (quickurl-find-url lookup)))
  259. (if (null url)
  260. (error "No URL associated with \"%s\"" lookup)
  261. (when (looking-at "\\w")
  262. (skip-syntax-forward "\\w"))
  263. (insert " ")
  264. (quickurl-insert url)))))
  265. ;;;###autoload
  266. (defun quickurl-ask (lookup)
  267. "Insert a URL, with `completing-read' prompt, based on LOOKUP."
  268. (interactive
  269. (list
  270. (progn
  271. (quickurl-load-urls)
  272. (let ((completion-ignore-case quickurl-completion-ignore-case))
  273. (completing-read "Lookup: " quickurl-urls nil t)))))
  274. (let ((url (quickurl-find-url lookup)))
  275. (when url
  276. (quickurl-insert url))))
  277. (defun quickurl-grab-url ()
  278. "Attempt to grab a word/URL pair from point in the current buffer.
  279. Point should be somewhere on the URL and the word is taken to be the thing
  280. that is returned from calling `quickurl-grab-lookup-function' once a
  281. `backward-word' has been issued at the start of the URL.
  282. It is assumed that the URL is either \"unguarded\" or is wrapped inside an
  283. <URL:...> wrapper."
  284. (let ((url (thing-at-point 'url)))
  285. (when url
  286. (save-excursion
  287. (beginning-of-thing 'url)
  288. ;; `beginning-of-thing' doesn't take you to the start of a marked-up
  289. ;; URL, only to the start of the URL within the "markup". So, we
  290. ;; need to do a little more work to get to where we want to be.
  291. (when (thing-at-point-looking-at thing-at-point-markedup-url-regexp)
  292. (search-backward "<URL:"))
  293. (backward-word 1)
  294. (let ((word (funcall quickurl-grab-lookup-function)))
  295. (when word
  296. (quickurl-make-url
  297. ;; The grab function may return the word with properties. I don't
  298. ;; want the properties. I couldn't find a method of stripping
  299. ;; them from a "string" so this will have to do. If you know of
  300. ;; a better method of doing this I'd love to know.
  301. (with-temp-buffer
  302. (insert word)
  303. (buffer-substring-no-properties (point-min) (point-max)))
  304. url)))))))
  305. ;;;###autoload
  306. (defun quickurl-add-url (word url comment)
  307. "Allow the user to interactively add a new URL associated with WORD.
  308. See `quickurl-grab-url' for details on how the default word/URL combination
  309. is decided."
  310. (interactive (let ((word-url (quickurl-grab-url)))
  311. (list (read-string "Word: " (quickurl-url-keyword word-url))
  312. (read-string "URL: " (quickurl-url-url word-url))
  313. (read-string "Comment: " (quickurl-url-comment word-url)))))
  314. (if (zerop (length word))
  315. (error "You must specify a WORD for lookup")
  316. (quickurl-load-urls)
  317. (let* ((current-url (quickurl-find-url word))
  318. (add-it (if current-url
  319. (if (called-interactively-p 'interactive)
  320. (y-or-n-p (format "\"%s\" exists, replace URL? " word))
  321. t)
  322. t)))
  323. (when add-it
  324. (if current-url
  325. (progn
  326. (setf (quickurl-url-url current-url) url)
  327. (setf (quickurl-url-comment current-url) comment))
  328. (push (quickurl-make-url word url comment) quickurl-urls))
  329. (setq quickurl-urls (funcall quickurl-sort-function quickurl-urls))
  330. (quickurl-save-urls)
  331. (when (get-buffer quickurl-list-buffer-name)
  332. (quickurl-list-populate-buffer))
  333. (when (called-interactively-p 'interactive)
  334. (message "Added %s" url))))))
  335. ;;;###autoload
  336. (defun quickurl-browse-url (&optional lookup)
  337. "Browse the URL associated with LOOKUP.
  338. If not supplied LOOKUP is taken to be the word at point in the
  339. current buffer, this default action can be modified via
  340. `quickurl-grab-lookup-function'."
  341. (interactive)
  342. (when (or lookup
  343. (setq lookup (funcall quickurl-grab-lookup-function)))
  344. (quickurl-load-urls)
  345. (let ((url (quickurl-find-url lookup)))
  346. (if url
  347. (browse-url (quickurl-url-url url))
  348. (error "No URL associated with \"%s\"" lookup)))))
  349. ;;;###autoload
  350. (defun quickurl-browse-url-ask (lookup)
  351. "Browse the URL, with `completing-read' prompt, associated with LOOKUP."
  352. (interactive (list
  353. (progn
  354. (quickurl-load-urls)
  355. (completing-read "Browse: " quickurl-urls nil t))))
  356. (let ((url (quickurl-find-url lookup)))
  357. (when url
  358. (browse-url (quickurl-url-url url)))))
  359. ;;;###autoload
  360. (defun quickurl-edit-urls ()
  361. "Pull `quickurl-url-file' into a buffer for hand editing."
  362. (interactive)
  363. (find-file quickurl-url-file))
  364. ;; quickurl-list mode.
  365. (put 'quickurl-list-mode 'mode-class 'special)
  366. ;;;###autoload
  367. (defun quickurl-list-mode ()
  368. "A mode for browsing the quickurl URL list.
  369. The key bindings for `quickurl-list-mode' are:
  370. \\{quickurl-list-mode-map}"
  371. (interactive)
  372. (kill-all-local-variables)
  373. (use-local-map quickurl-list-mode-map)
  374. (setq major-mode 'quickurl-list-mode
  375. mode-name "quickurl list")
  376. (run-mode-hooks 'quickurl-list-mode-hook)
  377. (setq buffer-read-only t
  378. truncate-lines t))
  379. ;;;###autoload
  380. (defun quickurl-list ()
  381. "Display `quickurl-list' as a formatted list using `quickurl-list-mode'."
  382. (interactive)
  383. (quickurl-load-urls)
  384. (unless (string= (buffer-name) quickurl-list-buffer-name)
  385. (setq quickurl-list-last-buffer (current-buffer)))
  386. (pop-to-buffer quickurl-list-buffer-name)
  387. (quickurl-list-populate-buffer)
  388. (quickurl-list-mode))
  389. (defun quickurl-list-populate-buffer ()
  390. "Populate the `quickurl-list' buffer."
  391. (with-current-buffer (get-buffer quickurl-list-buffer-name)
  392. (let ((buffer-read-only nil)
  393. (fmt (format "%%-%ds %%s\n"
  394. (apply #'max (or (loop for url in quickurl-urls
  395. collect (length (quickurl-url-description url)))
  396. (list 20))))))
  397. (setf (buffer-string) "")
  398. (loop for url in quickurl-urls
  399. do (let ((start (point)))
  400. (insert (format fmt (quickurl-url-description url)
  401. (quickurl-url-url url)))
  402. (add-text-properties start (1- (point))
  403. '(mouse-face highlight
  404. help-echo "mouse-2: insert this URL"))))
  405. (setf (point) (point-min)))))
  406. (defun quickurl-list-add-url (word url comment)
  407. "Wrapper for `quickurl-add-url' that doesn't guess the parameters."
  408. (interactive "sWord: \nsURL: \nsComment: ")
  409. (quickurl-add-url word url comment))
  410. (defun quickurl-list-quit ()
  411. "Kill the buffer named `quickurl-list-buffer-name'."
  412. (interactive)
  413. (kill-buffer quickurl-list-buffer-name)
  414. (switch-to-buffer quickurl-list-last-buffer)
  415. (delete-other-windows))
  416. (defun quickurl-list-mouse-select (event)
  417. "Select the URL under the mouse click."
  418. (interactive "e")
  419. (setf (point) (posn-point (event-end event)))
  420. (quickurl-list-insert-url))
  421. (defun quickurl-list-insert (type)
  422. "Insert the URL under cursor into `quickurl-list-last-buffer'.
  423. TYPE dictates what will be inserted, options are:
  424. `url' - Insert the URL as <URL:url>
  425. `naked-url' - Insert the URL with no formatting
  426. `with-lookup' - Insert \"lookup <URL:url>\"
  427. `with-desc' - Insert \"description <URL:url>\"
  428. `lookup' - Insert the lookup for that URL"
  429. (let ((url (nth (count-lines (point-min) (line-beginning-position))
  430. quickurl-urls)))
  431. (if url
  432. (with-current-buffer quickurl-list-last-buffer
  433. (insert
  434. (case type
  435. (url (funcall quickurl-format-function url))
  436. (naked-url (quickurl-url-url url))
  437. (with-lookup (format "%s <URL:%s>"
  438. (quickurl-url-keyword url)
  439. (quickurl-url-url url)))
  440. (with-desc (format "%S <URL:%s>"
  441. (quickurl-url-description url)
  442. (quickurl-url-url url)))
  443. (lookup (quickurl-url-keyword url)))))
  444. (error "No URL details on that line"))
  445. url))
  446. (defmacro quickurl-list-make-inserter (type)
  447. "Macro to make a key-response function for use in `quickurl-list-mode-map'."
  448. `(defun ,(intern (format "quickurl-list-insert-%S" type)) ()
  449. ,(format "Insert the result of calling `quickurl-list-insert' with `%s'." type)
  450. (interactive)
  451. (when (quickurl-list-insert ',type)
  452. (quickurl-list-quit))))
  453. (quickurl-list-make-inserter url)
  454. (quickurl-list-make-inserter naked-url)
  455. (quickurl-list-make-inserter with-lookup)
  456. (quickurl-list-make-inserter with-desc)
  457. (quickurl-list-make-inserter lookup)
  458. (provide 'quickurl)
  459. ;;; quickurl.el ends here