date-at-point.el 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ;;; date-at-point.el --- Add `date' to `thing-at-point' function
  2. ;; Copyright © 2014-2015 Alex Kost
  3. ;; Author: Alex Kost <alezost@gmail.com>
  4. ;; Created: 31 Dec 2014
  5. ;; Version: 0.1
  6. ;; URL: https://github.com/alezost/date-at-point.el
  7. ;; Keywords: convenience
  8. ;; This program 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. ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file provides an additional `date' thing for `thing-at-point'
  20. ;; function.
  21. ;; Using:
  22. ;;
  23. ;; 1. Add (require 'date-at-point) to your elisp code.
  24. ;; 2. Use (thing-at-point 'date).
  25. ;; A default regexp (`date-at-point-regexp') is trying to match any
  26. ;; possible date style, e.g.: "2014-12-31", "31.12.2014", "12/31/14",
  27. ;; etc. If you find problems with the current regexp, please contact
  28. ;; the maintainer.
  29. ;;; Code:
  30. (require 'thingatpt)
  31. (defvar date-at-point-regexp
  32. (let ((separator (rx (any "-./")))
  33. (2-digits (rx (repeat 2 digit)))
  34. (2-4-digits (rx (repeat 2 4 digit))))
  35. (concat 2-4-digits separator 2-digits separator 2-4-digits))
  36. "Regular expression matching a date.")
  37. (defun date-at-point-bounds ()
  38. "Return the bounds of the date at point."
  39. (save-excursion
  40. (when (thing-at-point-looking-at date-at-point-regexp)
  41. (cons (match-beginning 0) (match-end 0)))))
  42. (put 'date 'bounds-of-thing-at-point 'date-at-point-bounds)
  43. ;;;###autoload
  44. (defun date-at-point ()
  45. "Return the date at point, or nil if none is found."
  46. (thing-at-point 'date))
  47. (provide 'date-at-point)
  48. ;;; date-at-point.el ends here