This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
elisp-depmap-secondhelp.el
100 lines (84 loc) · 3.78 KB
/
elisp-depmap-secondhelp.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
;;; elisp-depmap-secondhelp.el --- Helper functions for parse library -*- lexical-binding: t; -*-
;; Copright (C) 2020 Mehmet Tekman <[email protected]>
;; Author: Mehmet Tekman
;; URL: https://github.com/mtekman/elisp-depmap.el
;; Keywords: outlines
;; Package-Requires: ((emacs "26.1") (dash "2.17.0"))
;; Version: 0.1
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;; Commentary:
;; See elisp-depmap.el
;;; Code:
(require 'cl-lib)
(require 'dash)
(defgroup elisp-depmap nil
"Main group for elisp-depmap package."
:group 'coding)
(defcustom elisp-depmap-secondhelp-regexreferences
"\\( \\|(\\|\\b\\|'\\)%s\\( \\|)\\|\\b\\)"
"Regex to find references to a definition."
:type 'string
:group 'elisp-depmap)
(defsubst elisp-depmap-secondhelp--generateregexfromalist (alist)
"From ALIST, get the car variables and put them in a regex.
This will be used to scan all files for top level definitions."
(concat "^(\\(cl-\\)?\\(" (mapconcat (lambda (x) (format "%s" (car x)))
alist "\\|") "\\)"))
(defun elisp-depmap-secondhelp--callingfuncatline (lnum file list-asc)
"Retrieve the function name in LIST-ASC that LNUM bisects in FILE."
(let ((func nil))
(dolist (elm list-asc)
(let ((func-lbeg (1+ (nth 0 elm)))
(func-lend (nth 1 elm))
(func-name (nth 2 elm))
(func-file (nth 3 elm)))
;; If current line number in file matches the
;; flanking beg and end in the list (and the file)
(if (and (string= file func-file)
(<= func-lbeg lnum func-lend))
(cl-pushnew func-name func))))
(if func
(if (> 1 (length func))
(error "Multiple functions at line... %d: %s" lnum func)
(car func)))))
(defun elisp-depmap-secondhelp--makesortedlinelist (hashtable)
"Make an ascending list of the start and end positions of all functions from HASHTABLE."
(let ((funcsbylinenum nil))
(maphash
(lambda (nam vals)
(let ((lbeg (plist-get vals :line-beg))
(lend (plist-get vals :line-end))
(file (plist-get vals :file)))
;; We only want functions (those with a lend)
(if lend
(cl-pushnew `(,lbeg ,lend ,nam ,file) funcsbylinenum))))
hashtable)
(--sort (< (car it) (car other)) funcsbylinenum)))
(defun elisp-depmap-secondhelp--updatementionslist (vname file annotations funcs-by-line-asc)
"Update mentions list from ANNOTATIONS for variable VNAME by checking in ASCLIST of line numbers for function bounds in FILE."
(let ((vnam-regex (format elisp-depmap-secondhelp-regexreferences vname))
(mentionlst (plist-get annotations :mentions))
(vnam-line (plist-get annotations :line-beg)))
(save-excursion
(goto-char 0)
(while (search-forward-regexp vnam-regex nil t)
(let ((lnum (line-number-at-pos)))
(unless (eq lnum vnam-line)
;; skip the top level definition
(let ((called-func (elisp-depmap-secondhelp--callingfuncatline
lnum
file
funcs-by-line-asc)))
(if called-func
(push called-func mentionlst))))))
(plist-put annotations :mentions mentionlst))))
(provide 'elisp-depmap-secondhelp)
;;; elisp-depmap-secondhelp.el ends here