This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
sourcetrail.el
executable file
·209 lines (165 loc) · 6.49 KB
/
sourcetrail.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;;; sourcetrail.el --- Communication with Sourcetrail -*- lexical-binding: t; -*-
;; Copyright (C) 2016
;; Author: Andreas Stallinger <astallinger@sourcetrail.com>
;; Keywords: external, tool
;; Version: 0.1
;; Package-Requires: ((emacs "24.4"))
;; License:
;; This file is not part of GNU Emacs
;; The MIT License (MIT)
;; Copyright (c) 2016 Coati Software OG
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the "Software"),
;; to deal in the Software without restriction, including without limitation
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
;; DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
;; OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; Commentary:
;;emacs-sourcetrail
;;===========
;;emacs-sourcetrail is a plugin for Emacs to communicate with Sourcetrail_.
;;.. _Sourcetrail: https://sourcetrail.com
;;Install
;;-------
;;Usage
;;-----
;;From Sourcetrail to Emacs
;;~~~~~~~~~~~~~~~~~~~
;;* enable sourcetrail-mode in Emacs
;;* Right click in sourcetrail -> **Set IDE Curor**
;;* In the Emacs should now open the file and put the cursor in the position form sourcetrail.
;;From Emacs to Sourcetrail
;;~~~~~~~~~~~~~~~~~~~
;;* Navigate your cursor to the location in the text.
;;* Sent location to sourcetrail
;; ;+ Press **M-x** and enter **sourcetrail-send-loation**
;; ;+ bind **sourcetrail-send-location** to a key sequence and use it.
;;Preferences
;;-----------
;;* **M-x** customize
;;* search for sourcetrail
;;* 3 Settins should be displayed now
;;Emacs Sourcetrail Ip
;;~~~~~~~~~~~~~~
;;Ip address for the Tcp communcation, default is ``localhost``
;;Emacs Sourcetrail Port Sourcetrail
;;~~~~~~~~~~~~~~~~~~~~~~
;;Port Sourcetrail listens to, default is ``6667``
;;Emacs Sourcetrail Port Emacs
;;~~~~~~~~~~~~~~~~~~~~~~
;;Port Sourcetrail listens to, default is ``6666``
;;; Code:
(require 'subr-x)
(defgroup sourcetrail nil
"Settings for the sourcetrail plugin."
:group 'external)
(defcustom sourcetrail-port-sourcetrail 6667
"Port Sourcetrail listens to."
:group 'sourcetrail
:type '(number))
(defcustom sourcetrail-port-emacs 6666
"Port for listening to Sourcetrail."
:group 'sourcetrail
:type '(number))
(defcustom sourcetrail-ip "localhost"
"Ip for communication with sourcetrail."
:group 'sourcetrail
:type '(string))
(defconst sourcetrail-server-name "sourcetrail-server")
(defconst sourcetrail-server-buffer "*-sourcetrail-server-buffer*")
(defvar sourcetrail-server nil)
(defvar sourcetrail-client nil)
(defvar sourcetrail-col nil)
(defvar sourcetrail-row nil)
(defvar sourcetrail-file nil)
(defvar sourcetrail-message nil)
(defun buildTokenLocationMessage nil
"Building a formated message for sending to sourcetrail."
(setq sourcetrail-col (number-to-string (current-column)))
(setq sourcetrail-row (number-to-string (line-number-at-pos)))
(setq sourcetrail-file (buffer-file-name))
(setq sourcetrail-message (mapconcat 'identity (list "setActiveToken" sourcetrail-file sourcetrail-row sourcetrail-col) ">>"))
(setq sourcetrail-message (mapconcat 'identity (list sourcetrail-message "<EOM>") "")))
(defun sourcetrail-send-ping nil
"Sending ping to sourcetrail."
(setq sourcetrail-client
(open-network-stream "sourcetrail-client"
"*sourcetrail-client*" sourcetrail-ip sourcetrail-port-sourcetrail))
(process-send-string sourcetrail-client "ping>>Emacs<EOM>"))
(defun sourcetrail-send-message(message)
"Sending message to sourcetrail."
(setq sourcetrail-client
(open-network-stream "sourcetrail-client"
"*sourcetrail-client*" sourcetrail-ip sourcetrail-port-sourcetrail))
(process-send-string sourcetrail-client message))
(defun sourcetrail-server-start nil
"Start tcp server."
(unless sourcetrail-server
(setq sourcetrail-server
(make-network-process :name (or sourcetrail-server-name "*sourcetrail-server")
:server t
:service (or sourcetrail-port-emacs 6666)
:family 'ipv4
:buffer sourcetrail-server-buffer
:filter 'sourcetrail-listen-filter))
(if sourcetrail-server
(set-process-query-on-exit-flag sourcetrail-server nil)
(error "Could not start server process"))
(sourcetrail-send-ping)))
(defun sourcetrail-listen-filter (proc string)
"Tcp listener filter. No need for PROC. STRING is the command send from sourcetrail."
(process-buffer proc)
(if (string-suffix-p "<EOM>" string)
(progn
;; split message
(setq sourcetrail-message (split-string (string-remove-suffix "<EOM>" string) ">>"))
(when (string= (car sourcetrail-message) "moveCursor")
;;moveCuror message
;; filepath
(setq sourcetrail-file (nth 1 sourcetrail-message))
;; row and col
(setq sourcetrail-row (string-to-number (nth 2 sourcetrail-message)))
(setq sourcetrail-col (string-to-number (nth 3 sourcetrail-message)))
;; open file
(find-file sourcetrail-file)
;; move cursor
(forward-line (- sourcetrail-row (line-number-at-pos)))
(move-to-column sourcetrail-col)
)
(when (string= (car sourcetrail-message) "ping")
(sourcetrail-send-ping)
)
)
(message "Could not process the message from sourcetrail: %s" string)
)
)
(defun sourcetrail-server-stop nil
"Stops TCP Listener for Sourcetrail."
(when sourcetrail-server
(delete-process sourcetrail-server-name)
(setq sourcetrail-server nil)))
;;;###autoload
(defun sourcetrail-send-location nil
"Sends current location to Sourcetrail."
(interactive)
(sourcetrail-send-message (buildTokenLocationMessage))
)
;;;###autoload
(define-minor-mode sourcetrail-mode
"Start/stop sourcetrail mode."
:global t
:lighter " sourcetrail"
; value of sourcetrail-mode is toggled before this implicitly
(if sourcetrail-mode (sourcetrail-server-start) (sourcetrail-server-stop)))
(provide 'sourcetrail)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; sourcetrail.el ends here