home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / GNU / emacs.inst / emacs19.idb / usr / gnu / lib / emacs / site-lisp / thing@pt.el.z / thing@pt.el
Encoding:
Text File  |  1994-08-02  |  6.8 KB  |  208 lines

  1. ;;; thing@pt.el --- Get the `thing' at point
  2.  
  3. ;; Copyright (C) Mike Williams 1991,1992,1993
  4.  
  5. ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
  6. ;; Keywords: extensions
  7. ;; Created: Thu Mar 28 13:48:23 1991
  8. ;; Version: $Revision: 1.16 $
  9.  
  10. ;; This file is not part of GNU Emacs, but is made available under the
  11. ;; same conditions.
  12. ;;
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  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. ;;; Commentary ============================================================
  24. ;;
  25. ;; This file provides routines for getting the `thing' at the location of
  26. ;; point, whatever that `thing' happens to be.  The `thing' is defined by
  27. ;; it's beginning and end positions in the buffer.
  28. ;;
  29. ;; The function bounds-of-thing-at-point finds the beginning and end
  30. ;; positions by moving first forward to the end of the `thing', and then
  31. ;; backwards to the beginning.  By default, it uses the corresponding
  32. ;; forward-`thing' operator (eg. forward-word, forward-line).
  33. ;;
  34. ;; Special cases are allowed for using properties associated with the named
  35. ;; `thing': 
  36. ;;
  37. ;;   forward-op        Function to call to skip forward over a `thing' (or
  38. ;;                      with a negative argument, backward).
  39. ;;                      
  40. ;;   beginning-op    Function to call to skip to the beginning of a `thing'.
  41. ;;   end-op        Function to call to skip to the end of a `thing'.
  42. ;;
  43. ;; Reliance on existing operators means that many `things' can be accessed
  44. ;; without further code:  eg.
  45. ;;     (thing-at-point 'line)
  46. ;;     (thing-at-point 'page)
  47.  
  48. ;;; Code ==================================================================
  49.  
  50. (provide 'thing@pt)
  51.  
  52. ;;=== Version =============================================================
  53.  
  54. (defconst thing@pt-version (substring "$Revision: 1.16 $" 11 -2)
  55.   "The revision number of thing@pt (as string).  The complete RCS id is:
  56.  
  57.   $Id: thing@pt.el,v 1.16 1993/09/30 23:54:56 mike Exp $")
  58.  
  59. ;;=== Basic movement ======================================================
  60.  
  61. ;;;###autoload
  62. (defun forward-thing (THING &optional N)
  63.   "Move forward to the end of the next THING."
  64.   (let ((forward-op (or (get THING 'forward-op)
  65.             (intern-soft (format "forward-%s" THING)))))
  66.     (if (fboundp forward-op)
  67.     (funcall forward-op (or N 1))
  68.       (error "Can't determine how to move over %ss" THING))))
  69.  
  70. ;;=== General routines ====================================================
  71.  
  72. ;;;###autoload
  73. (defun bounds-of-thing-at-point (THING)
  74.   "Determine the start and end buffer locations for the THING at point,
  75. where THING is an entity for which there is a either a corresponding
  76. forward-THING operation, or corresponding beginning-of-THING and
  77. end-of-THING operations, eg. 'word, 'sentence, 'defun.
  78.   Return a cons cell '(start . end) giving the start and end positions."
  79.   (let ((orig (point)))
  80.     (condition-case nil
  81.     (save-excursion
  82.       (let ((end (progn 
  83.                (funcall 
  84.             (or (get THING 'end-op) 
  85.                 (function (lambda () (forward-thing THING 1)))))
  86.                (point)))
  87.         (beg (progn 
  88.                (funcall 
  89.             (or (get THING 'beginning-op) 
  90.                 (function (lambda () (forward-thing THING -1)))))
  91.                (point))))
  92.         (if (and beg end (<= beg orig) (< orig end))
  93.         (cons beg end))))
  94.       (error nil))))
  95.  
  96. ;;;###autoload
  97. (defun thing-at-point (THING)
  98.   "Return the THING at point, where THING is an entity defined by
  99. bounds-of-thing-at-point."
  100.   (let ((bounds (bounds-of-thing-at-point THING)))
  101.     (if bounds 
  102.     (buffer-substring (car bounds) (cdr bounds)))))
  103.  
  104. ;;=== Go to beginning/end =================================================
  105.  
  106. (defun beginning-of-thing (THING)
  107.   (let ((bounds (bounds-of-thing-at-point THING)))
  108.     (or bounds (error "No %s here" THING))
  109.     (goto-char (car bounds))))
  110.  
  111. (defun end-of-thing (THING)
  112.   (let ((bounds (bounds-of-thing-at-point THING)))
  113.     (or bounds (error "No %s here" THING))
  114.     (goto-char (cdr bounds))))
  115.  
  116. ;;=== Special cases =======================================================
  117.  
  118. ;;--- Sexps ---
  119.  
  120. (defun in-string-p ()
  121.   (let ((orig (point)))
  122.     (save-excursion
  123.       (beginning-of-defun)
  124.       (nth 3 (parse-partial-sexp (point) orig)))))
  125.  
  126. (defun end-of-sexp ()
  127.   (let ((char-syntax (char-syntax (char-after (point)))))
  128.     (if (or (eq char-syntax ?\))
  129.         (and (eq char-syntax ?\") (in-string-p)))
  130.     (forward-char 1)
  131.       (forward-sexp 1))))
  132.  
  133. (put 'sexp 'end-op 'end-of-sexp)
  134.  
  135. ;;--- Lists ---
  136.  
  137. (put 'list 'end-op (function (lambda () (up-list 1))))
  138. (put 'list 'beginning-op 'backward-sexp)
  139.  
  140. ;;--- Filenames ---
  141.  
  142. (defvar file-name-chars "~/A-Za-z0-9---_.${}#%,"
  143.   "Characters allowable in filenames.")
  144.  
  145. (put 'filename 'end-op    
  146.      (function (lambda () (skip-chars-forward file-name-chars))))
  147. (put 'filename 'beginning-op
  148.      (function (lambda () (skip-chars-backward file-name-chars (point-min)))))
  149.  
  150. ;;--- Whitespace ---
  151.  
  152. (defun forward-whitespace (ARG)
  153.   (interactive "p")
  154.   (if (natnump ARG) 
  155.       (re-search-forward "[ \t]+\\|\n" nil nil ARG)
  156.     (while (< ARG 0)
  157.       (if (re-search-backward "[ \t]+\\|\n" nil nil)
  158.       (or (eq (char-after (match-beginning 0)) 10)
  159.           (skip-chars-backward " \t")))
  160.       (setq ARG (1+ ARG)))))
  161.  
  162. ;;--- Buffer ---
  163.  
  164. (put 'buffer 'end-op 'end-of-buffer)
  165. (put 'buffer 'beginning-op 'beginning-of-buffer)
  166.  
  167. ;;--- Symbols ---
  168.  
  169. (defun forward-symbol (ARG)
  170.   (interactive "p")
  171.   (if (natnump ARG) 
  172.       (re-search-forward "\\(\\sw\\|\\s_\\)+" nil nil ARG)
  173.     (while (< ARG 0)
  174.       (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil nil)
  175.       (skip-syntax-backward "w_"))
  176.       (setq ARG (1+ ARG)))))
  177.  
  178. ;;=== Aliases =============================================================
  179.  
  180. (defun word-at-point () (thing-at-point 'word))
  181. (defun sentence-at-point () (thing-at-point 'sentence))
  182.  
  183. (defun read-from-whole-string (STR)
  184.   "Read a lisp expression from STR, signalling an error if the entire string
  185. was not used."
  186.   (let* ((read-data (read-from-string STR))
  187.      (more-left 
  188.       (condition-case nil
  189.           (progn (read-from-string (substring STR (cdr read-data)))
  190.              t)
  191.         (end-of-file nil))))
  192.     (if more-left
  193.     (error "Can't read whole string")
  194.       (car read-data))))
  195.  
  196. (defun form-at-point (&optional THING PRED) 
  197.   (let ((sexp (condition-case nil 
  198.           (read-from-whole-string (thing-at-point (or THING 'sexp)))
  199.         (error nil))))
  200.     (if (or (not PRED) (funcall PRED sexp)) sexp)))
  201.  
  202. (defun sexp-at-point ()   (form-at-point 'sexp))
  203. (defun symbol-at-point () (form-at-point 'sexp 'symbolp))
  204. (defun number-at-point () (form-at-point 'sexp 'numberp))
  205. (defun list-at-point ()   (form-at-point 'list 'listp))
  206.  
  207. ;;=== END of thing@pt.el ==================================================
  208.