| 1 |
;;; def-use-sym.el --- def-use-sym.el -*- lexical-binding: t; -*- |
| 2 |
;; Copyright (C) 2007 Vesa Karvonen |
| 3 |
;; |
| 4 |
;; MLton is released under a HPND-style license. |
| 5 |
;; See the file MLton-LICENSE for details. |
| 6 |
|
| 7 |
(require 'def-use-util) |
| 8 |
|
| 9 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 10 |
;; Points and Positions |
| 11 |
|
| 12 |
(defun def-use-pos-to-point (pos) |
| 13 |
"Returns the value of point in the current buffer at the position." |
| 14 |
(save-excursion |
| 15 |
(def-use-goto-line (def-use-pos-line pos)) |
| 16 |
(+ (point) (def-use-pos-col pos)))) |
| 17 |
|
| 18 |
(defun def-use-point-to-pos (point) |
| 19 |
"Returns the position corresponding to the specified point in the |
| 20 |
current buffer." |
| 21 |
(save-excursion |
| 22 |
(goto-char point) |
| 23 |
(beginning-of-line) |
| 24 |
(let ((line (+ (count-lines 1 (point)) 1)) |
| 25 |
(col (- point (point)))) |
| 26 |
(def-use-pos line col)))) |
| 27 |
|
| 28 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 29 |
;; Basic symbol lookup support |
| 30 |
|
| 31 |
(defvar def-use-mode-to-move-to-symbol-start-alist nil |
| 32 |
"Association list mapping modes to functions that move the point |
| 33 |
backwards to the start of the symbol at the point.") |
| 34 |
|
| 35 |
(defvar def-use-mode-to-move-to-symbol-end-alist nil |
| 36 |
"Association list mapping modes to functions that move the point to the |
| 37 |
end of the symbol at the point.") |
| 38 |
|
| 39 |
(defun def-use-move-to-symbol-start () |
| 40 |
(let ((mode-move |
| 41 |
(assoc major-mode def-use-mode-to-move-to-symbol-start-alist))) |
| 42 |
(if mode-move |
| 43 |
(funcall (cdr mode-move)) |
| 44 |
(skip-syntax-backward "w_" (def-use-point-at-current-line))))) |
| 45 |
|
| 46 |
(defun def-use-move-to-symbol-end () |
| 47 |
(let ((mode-move |
| 48 |
(assoc major-mode def-use-mode-to-move-to-symbol-end-alist))) |
| 49 |
(if mode-move |
| 50 |
(funcall (cdr mode-move)) |
| 51 |
(skip-syntax-forward "w_" (def-use-point-at-next-line))))) |
| 52 |
|
| 53 |
(defun def-use-ref-at-point (point) |
| 54 |
"Returns a reference for the symbol at the specified point in the |
| 55 |
current buffer." |
| 56 |
(let ((src (def-use-buffer-file-truename))) |
| 57 |
(when src |
| 58 |
(def-use-ref src |
| 59 |
(def-use-point-to-pos |
| 60 |
(save-excursion |
| 61 |
(goto-char point) |
| 62 |
(def-use-move-to-symbol-start) |
| 63 |
(point))))))) |
| 64 |
|
| 65 |
(defun def-use-extract-sym-name-at-point (point) |
| 66 |
"Tries to extracts what looks like the name of the symbol at point. |
| 67 |
This doesn't really understand the syntax of the language, so the result |
| 68 |
is only valid when there really is a symbol at the point." |
| 69 |
(save-excursion |
| 70 |
(goto-char point) |
| 71 |
(let* ((start (progn (def-use-move-to-symbol-start) (point))) |
| 72 |
(end (progn (def-use-move-to-symbol-end) (point)))) |
| 73 |
(when (and (<= start point) |
| 74 |
(<= point end) |
| 75 |
(< start end)) |
| 76 |
(buffer-substring start end))))) |
| 77 |
|
| 78 |
(defun def-use-extract-sym-name-at-ref (ref) |
| 79 |
"Tries to extract what looks like the name of the symbol at ref." |
| 80 |
(save-window-excursion |
| 81 |
(def-use-find-file (def-use-ref-src ref)) |
| 82 |
(def-use-extract-sym-name-at-point |
| 83 |
(def-use-pos-to-point (def-use-ref-pos ref))))) |
| 84 |
|
| 85 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 86 |
|
| 87 |
(provide 'def-use-sym) |