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