Skip to content

Commit 0b68549

Browse files
author
George Moutsopoulos
committed
org link pydef separate
1 parent c294c77 commit 0b68549

File tree

2 files changed

+99
-44
lines changed

2 files changed

+99
-44
lines changed

org-annotate-python.el

+5-44
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
;;; Code:
1515
(require 'org-annotate-code)
1616
(require 'ol)
17-
(org-link-set-parameters "pydef"
18-
:follow 'org-annotate-python-pydef-search
19-
:export #'org-annotate-python-pydef-export
20-
:store #'org-annotate-python-pydef-store-link)
17+
(require 'org-link-pydef)
2118

2219
(defconst org-annotate-code-level-regex
2320
"^\\(?: {%d}\\)"
@@ -77,13 +74,7 @@ possibly under a heading0."
7774
(if (search-forward-regexp org-annotate-code-variable-regex end t)
7875
(match-string-no-properties 1)))))
7976

80-
(defun org-annotate-python-get-pydef-name ()
81-
"Return function+variable dotted name in list."
82-
(let ((funname (org-annotate-python-get-defun-name))
83-
(varname (org-annotate-python-get-variable-name)))
84-
(cond ((not funname) varname)
85-
((not varname) funname)
86-
((concat funname "." varname)))))
77+
8778

8879
(defun org-annotate-python-squash-list-to-level (listnames level)
8980
"Squash candidates in LISTNAMES up to LEVEL.
@@ -162,31 +153,8 @@ Optional squash for final annotation, if nil keep all, if zero keeps only filena
162153
(annotation (org-annotate-python-add-filename-node filename dottedannotation)))
163154
(org-annotate-python-squash-list-keep-and-last annotation (when squash (1+ squash))))) ; here squash=0 means keeping only filename.
164155

165-
(defun org-annotate-python-pydef-store-link (&optional nofile ask)
166-
"Store a link to a man page."
167-
(when (memq major-mode '(python-mode))
168-
;; This is a man page, we do make this link.
169-
(let* ((filename (buffer-file-name))
170-
(dotted (org-annotate-python-get-pydef-name))
171-
(name (if ask (org-annotate-python-pydef-select-candidate dotted) dotted))
172-
(description nil))
173-
(unless nofile
174-
(setq name (concat filename "::" name)))
175-
(org-link-store-props
176-
:type "pydef"
177-
:link name
178-
:description description))))
179-
180-
(defun org-annotate-python-pydef-export (link description format)
181-
"Export LINK with DESCRIPTION into FORMAT."
182-
(let ((path link)
183-
(desc (or description link)))
184-
(pcase format
185-
;; (`html (format "%s" path))
186-
;; (`latex (format "\\href{%s}{%s}" path desc))
187-
;; (`texinfo (format "@uref{%s,%s}" path desc))
188-
;; (`ascii (format "%s (%s)" desc path))
189-
(_ (if description (format "%s (%s)" desc path) path)))))
156+
157+
190158

191159
(defun org-annotate-python-make-search-string (name)
192160
(format org-annotate-code-def-regex-name-format name name))
@@ -211,18 +179,11 @@ Optional squash for final annotation, if nil keep all, if zero keeps only filena
211179
(goto-char beg))
212180
(goto-char last)))
213181

214-
(defun org-annotate-python-pydef-search (link)
215-
(let* ((splitlink (org-annotate-python-pydef-split-filename-searchname link))
216-
(filename (car splitlink))
217-
(dotted (cdr splitlink)))
218-
(if filename (find-file filename))
219-
(org-annotate-python-goto-dotted dotted)))
220-
221182
(defun org-annotate-python-info-at-point (&optional ask)
222183
"Return a plist with python info at point."
223184
(interactive "P")
224185
(let* ((filename (buffer-file-name))
225-
(dotted (org-annotate-python-get-pydef-name))
186+
(dotted (org-link-pydef-get-pydef t t))
226187
(selection (if ask
227188
(org-annotate-python-pydef-select-candidate
228189
dotted

org-link-pydef.el

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
;;; org-link-pydef.el --- Support for links to python defintions in Org mode
2+
;; Copyright (C) 2020
3+
4+
;; Author: George Moutsopoulos <[email protected]>
5+
;; Version: 1.0
6+
;; Package-Requires: ((org-mode) (org-annotate-code))
7+
;; Keywords: annotate, capture, code, comments, python
8+
9+
;;; Commentary:
10+
11+
;; This package provides links that point to python definitions.
12+
;; It integrates with org-annotate-code to provide annotations to them.
13+
14+
;;; Code:
15+
16+
(require 'ol)
17+
(org-link-set-parameters "pydef"
18+
:follow 'org-link-pydef-follow
19+
:export #'org-link-pydef-export
20+
:store #'org-link-pydef-store)
21+
22+
(defun org-link-pydef-get-pydef (&optional with-variable no-module)
23+
(let* ((filename (gm/get-relative-pyroot-filename))
24+
(dotted-filename (string-replace "/" "." filename))
25+
(module (replace-regexp-in-string "\\.py$" "" dotted-filename))
26+
(module (replace-regexp-in-string "^.*python\\.ev\\." "ev." module))
27+
(funname (python-info-current-defun))
28+
(varname (save-excursion (python-nav-beginning-of-statement)
29+
(python-info-current-symbol)))
30+
(pydef-no-module (if with-variable
31+
(cond ((not funname) varname)
32+
((not varname) funname)
33+
((concat funname "." varname)))
34+
(or funname varname)
35+
))
36+
(pydef-with-module (concat module "::" pydef-no-module))
37+
(pydef (if no-module pydef-no-module pydef-with-module)))
38+
pydef
39+
))
40+
41+
(defun org-link-pydef-store (&optional with-variable without-file)
42+
"Store a link to a man page."
43+
(when (memq major-mode '(python-mode))
44+
(let* ((link (org-link-pydef-get-pydef with-variable without-file))
45+
(description nil))
46+
(org-link-store-props
47+
:type "pydef"
48+
:link link
49+
:description description))))
50+
51+
(defun org-link-pydef-export (link)
52+
"Export LINK with DESCRIPTION into FORMAT."
53+
(let ((path link)
54+
(desc (or description link)))
55+
(pcase format
56+
(`html (format "%s" path))
57+
(`latex (format "\\href{%s}{%s}" path desc))
58+
(`texinfo (format "@uref{%s,%s}" path desc))
59+
(`ascii (format "%s (%s)" desc path))
60+
(_ (if description (format "%s (%s)" desc path) path)))))
61+
62+
(defun org-link-pydef-split (link)
63+
"Return cons of file and dotted pydef from pydef LINK."
64+
(string-match "^\\(?1:.*\\)::\\(?2:.*\\)$" link)
65+
(cons (match-string-no-properties 1 link) (match-string-no-properties 2 link)))
66+
67+
(defun org-link-pydef-get-dotted-point (dotted)
68+
"Goto DOTTED definition."
69+
(save-excursion
70+
(let* ((names (split-string dotted "\\."))
71+
(first-names (butlast names 1))
72+
(last-name (car (last names)))
73+
(beg (point-min))
74+
(end (point-max)))
75+
(dolist (name first-names)
76+
(goto beg)
77+
(search-forward-regexp
78+
(format "\\(?:def\\|class\\) *%s\\)(" name) end)
79+
(save-excursion (setq beg (python-nav-beginning-of-defun)))
80+
(save-excursion (setq end (python-nav-end-of-defun))))
81+
(goto beg)
82+
(search-forward-regexp
83+
(format "\\(?:\\(?:def\\|class\\) *%s\\)(\\|\\(?:%s *=\\)" last-name) end)
84+
(python-nav-beginning-of-statement)
85+
(point))))
86+
87+
(defun org-link-pydef-follow (link)
88+
(let* ((splitlink (org-link-pydef-split link))
89+
(filename (car splitlink))
90+
(dotted (cdr splitlink)))
91+
(if filename (find-file filename))
92+
(goto (org-link-pydef-get-dotted-point dotted))))
93+
94+
(provide 'org-link-pydef)

0 commit comments

Comments
 (0)