-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrosemacs.el
1916 lines (1679 loc) · 79 KB
/
rosemacs.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; rosemacs.el --- Tools for ROS users
;; Copyright (c) 2008, Willow Garage, Inc.
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; * Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; * Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; * Neither the name of the Willow Garage, Inc. nor the names of its
;; contributors may be used to endorse or promote products derived from
;; this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.
;;
;; Author: Bhaskara Marthi
;; Keywords: tools, convenience
;;; Commentary:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Installation instructions
;; 1. Put this file somewhere (if it's not already in
;; tools/rosemacs in your ros tree)
;;
;; 2. (Optional) From emacs do M-x byte-compile followed by
;; this file's full path
;;
;; 3. Add the following lines to your .emacs
;; (add-to-list 'load-path "/path/to/rosemacs")
;; (require 'rosemacs)
;; (invoke-rosemacs)
;;
;; 4. Add the following line or equivalent to
;; .emacs to activate keyboard shortcuts for the added
;; commands (\C-x\C-r means control-x control-r):
;; (global-set-key "\C-x\C-r" ros-keymap)
;;
;; 5. Make sure the standard ROS variables are set in the
;; emacs process environment. If you follow the standard
;; ROS installation instructions about sourcing setup.bash
;; in your .bashrc, then this will automatically happen
;; if you launch emacs from a bash shell.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Usage
;;
;; 0. If you did 4 above, you can type the prefix followed
;; by \C-h to see the list of added commands.
;;
;; 1. Directory tracking and tab completion for rosbash
;; commands, including rostopic and rosnode, should now
;; work correctly in shell mode
;;
;; 2. The commands {find|view}-ros-{file|message|service},
;; and view-most-recent-ros-log for navigating the ros
;; libraries are available. Tab completion should work
;; for all of them.
;;
;; 3. The customization option ros-topic-update-interval governs
;; how frequently rosemacs polls the list of ros topics
;; and nodes. Assuming this is positive, it will enable tab
;; completion of ros topics in the shell and for commands
;; such as echo-ros-topic. Additionally, you can use
;; add-hz-update to define a list of topics for which the
;; hz rate is tracked in the background, viewable using
;; display-ros-topic-info.
;;
;; 4. Similarly, set ros-node-update-interval to set up
;; tracking and completion of nodes.
;;
;; 5. ros-core starts a core. ros-run runs a node. In
;; either case, an appropriately named buffer is created
;; for the new process.
;;
;; 6. ros-launch to start a launch file in a new buffer.
;; Within that buffer, k to kill, r to relaunch.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code:
(require 'shell)
(require 'warnings)
(require 'time-stamp)
(require 'ansi-color)
(require 'rosbag-view-mode)
(require 'cl-lib)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Parameters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Moved to end of file
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; State
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar ros-stacks nil "Vector of ros stacks")
(defvar ros-stack-locations nil
"Vector of directories containing the items in ros-stacks")
(defvar ros-packages nil "Vector of ros packages")
(defvar ros-package-locations nil
"Vector of directories containing the items in ros-packages")
(defvar ros-messages nil "Vector of ros messages")
(defvar ros-message-packages nil
"Vector of packages corresponding to each ros message")
(defvar ros-services nil "Vector of ros services")
(defvar ros-service-packages nil
"Vector of packages corresponding to each service")
(defvar ros-actions nil "Vector of ros messages")
(defvar ros-action-packages nil
"Vector of packages corresponding to each ros message")
(defvar ros-root (getenv "ROS_ROOT"))
(defvar ros-topics nil "Vector of current published ros topics")
(defvar ros-subscribed-topics nil "Subscribed topics")
(defvar ros-all-topics nil
"All topics (generated from published and subscribed)")
(defvar ros-topic-hertz-processes nil
"Alist from topic name to process running rostopic hz on that topic")
(defvar ros-topic-publication-rates nil
"Hash table from topic name to hertz rate of that topic")
(defvar ros-topic-last-hz-rate nil
"Alist from topic name to last time we saw output from rostopic hz")
(defvar ros-topic-buffer (get-buffer-create "*ros-topics*")
"Holds the buffer *ros-topics* if it exists")
(defvar ros-events-buffer (get-buffer-create "*ros-events*"))
(defvar ros-hz-topic-regexps nil
"If a topic name matches one of these, it is hz tracked")
(defvar ros-topic-timer nil
"If non-nil, equals timer object used to schedule calls to rostopic list")
(defvar ros-num-publishers (make-hash-table :test 'equal)
"num publishers of a topic")
(defvar ros-num-subscribers (make-hash-table :test 'equal)
"num subscribers of a topic")
(defvar ros-find-args nil)
(defvar ros-find-args-history nil)
(defvar rosemacs/pathname nil "Will hold the path containing this file")
;; (defvar rosemacs/invoked t)
(defvar rosemacs/invoked nil)
(defvar rosemacs/nodes nil "List of nodes")
(defvar rosemacs/nodes-vec (vector) "Vector of nodes")
(defvar roslaunch/history-list nil)
(defvar roslaunch/package-history-list nil
"History of packages used in roslaunch")
(defvar roslaunch/launchfile-history-lists (make-hash-table :test 'equal)
"Map each ros package name to the corresponding history list of launch files")
(defvar roslaunch/launchfile-history-list nil)
(defvar rosrun/history-list nil)
(defvar ros-buffer-package nil
"A buffer-local variable for caching the current buffer's ros package.")
(make-variable-buffer-local 'ros-buffer-package)
(with-current-buffer (get-buffer-create "*ros-topics*")
(insert "Uninitialized (use the display-ros-topic-info command rather than just switching to this buffer)"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Preloading
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ros-load-stack-locations ()
"Reload locations of ros stacks by calling out to rosstack list"
(interactive)
(with-temp-buffer
(let ((l nil))
(message "Calling rosstack")
(call-process "rosstack" nil t nil "list")
(goto-char (point-min))
(message "Parsing rosstack output")
(let ((done nil))
;; Loop over lines; each line contains a stack and directory
(while (not done)
(let ((p (point)))
;; Search for string terminated by space
(setq done (not (re-search-forward "[[:space:]]" (point-max) t)))
(unless done
(let ((stack (buffer-substring p (1- (point)))))
(setq p (point))
;; Search for following string terminated by newline
(re-search-forward "\n")
(let ((dir (buffer-substring p (1- (point)))))
(push (cons stack dir) l)))))))
(let ((stack-alist
(cl-sort
(vconcat l)
(lambda (pair1 pair2) (string< (car pair1) (car pair2))))))
(setq ros-stacks (cl-map 'vector #'car stack-alist)
ros-stack-locations (cl-map 'vector #'cdr stack-alist)))
(message "Done loading ROS stack info"))))
(defun ros-load-package-locations ()
"Reload locations of ros packages by calling out to rospack list"
(interactive)
(with-temp-buffer
(let ((l nil))
(message "Calling rospack")
(call-process "rospack" nil t nil "list")
(goto-char (point-min))
(message "Parsing rospack output")
(let ((done nil))
;; Loop over lines; each line contains a package and directory
(while (not done)
(let ((p (point)))
;; Search for string terminated by space
(setq done (not (re-search-forward "[[:space:]]" (point-max) t)))
(unless done
(let ((package (buffer-substring p (1- (point)))))
(setq p (point))
;; Search for following string terminated by newline
(re-search-forward "\n")
(let ((dir (buffer-substring p (1- (point)))))
(push (cons package dir) l)))))))
(let ((package-alist
(cl-sort
(vconcat l)
(lambda (pair1 pair2) (string< (car pair1) (car pair2))))))
(setq ros-packages (cl-map 'vector #'car package-alist)
ros-package-locations (cl-map 'vector #'cdr package-alist)
ros-messages nil
ros-message-packages nil
ros-services nil
ros-service-packages nil))
(message "Done loading ROS package info"))))
(defun ros-files-in-package (dir ext &optional subdir)
"Return list of files in subdirectory ext/ of dir whose extension is .ext"
(with-temp-buffer
(let ((l nil)
(done nil)
(p nil))
(call-process "ls" nil t nil (concat dir "/" (or subdir ext) "/"))
(goto-char (point-min))
(while (not done)
(setq p (point))
(setq done (not (re-search-forward "\\([^[:space:]]+\\)[[:space:]]+" (point-max) t)))
(unless done
(let ((str (buffer-substring (match-beginning 1) (match-end 1))))
(let ((m (string-match (concat "\." ext "$") str)))
(when m
(push (substring str 0 m) l))))))
l)))
(defun all-files-in-packages (ext &optional subdir)
"Look in each package for files with a extension .ext in subdirectory ext/"
(unless ros-package-locations
(ros-load-package-locations))
(let ((l nil))
(dotimes-with-progress-reporter (i (length ros-package-locations))
(concat "Caching locations of ." ext " files: ")
(let ((package (aref ros-packages i))
(dir (aref ros-package-locations i)))
(dolist (m (ros-files-in-package dir ext subdir))
(push (cons m package) l))))
(cl-sort (vconcat l) (lambda (pair1 pair2) (string< (car pair1) (car pair2))))))
(defun cache-ros-message-locations ()
"Look in each package directory for .msg files"
(let ((v (all-files-in-packages "msg")))
(setq ros-messages (cl-map 'vector #'car v)
ros-message-packages (cl-map 'vector #'cdr v))))
(defun cache-ros-service-locations ()
"Look in each package directory for .srv files"
(let ((v (all-files-in-packages "srv")))
(setq ros-services (cl-map 'vector #'car v)
ros-service-packages (cl-map 'vector #'cdr v))))
(defun cache-ros-action-locations ()
"Look in each package directory for .action files"
(let ((v (all-files-in-packages "action" "action")))
(setq ros-actions (cl-map 'vector #'car v)
ros-action-packages (cl-map 'vector #'cdr v))))
(defun get-rosemacs-path ()
(message "[rosemacs] path: %s" load-file-name)
(let* ((a-path (or load-file-name ""))
(ind (string-match "\\(.*\\)rosemacs.el\\(\\|c\\)$" a-path)))
(if ind
(match-string 1 load-file-name)
(display-warning 'rosemacs "Could not determine rosemacs path"))))
(setq rosemacs/pathname (get-rosemacs-path))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lookup
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ros-stack-dir (stack)
(unless ros-stack-locations
(ros-load-stack-locations))
(rosemacs-lookup-vectors stack ros-stacks ros-stack-locations))
(defun ros-package-dir (package)
(unless ros-package-locations
(ros-load-package-locations))
(rosemacs-lookup-vectors package ros-packages ros-package-locations))
(defun ros-message-package (m)
(unless ros-message-packages
(cache-ros-message-locations))
(rosemacs-lookup-vectors m ros-messages ros-message-packages))
(defun ros-service-package (m)
(unless ros-service-packages
(cache-ros-service-locations))
(rosemacs-lookup-vectors m ros-services ros-service-packages))
(defun ros-action-package (m)
(unless ros-action-packages
(cache-ros-action-locations))
(rosemacs-lookup-vectors m ros-actions ros-action-packages))
(defun ros-package-for-path (path &optional allow-nonexistent)
(let ((path (cond ((file-directory-p path)
(directory-file-name path))
((or (file-exists-p path) allow-nonexistent)
(directory-file-name (file-name-directory path)))
(t nil))))
(catch 'done
(while (and path (not (equal path "/")))
(let ((files (directory-files path)))
(if (or (member "manifest.xml" files) (member "package.xml" files))
(throw 'done (file-name-nondirectory path))
(setf path (directory-file-name (file-name-directory path)))))))))
(defun ros-package-for-buffer (buffer &optional allow-nonexistent)
(let ((fn (buffer-file-name buffer)))
(when fn
(ros-package-for-path fn allow-nonexistent))))
(defun get-buffer-ros-package ()
(or ros-buffer-package
(setq ros-buffer-package (ros-package-for-buffer (current-buffer)))))
(defun ros-current-pkg-modeline-entry ()
(interactive)
(let ((pkg (or ros-buffer-package (ros-package-for-buffer (current-buffer)))))
(unless ros-buffer-package
(if pkg
(setf ros-buffer-package pkg)
(setf ros-buffer-package :none)))
(if (and pkg (not (eq pkg :none)))
(format "(ROS Pkg: %s)" pkg)
"")))
(defun parse-ros-file-prefix (str)
"Divide something of the form PACKAGE/DIRS/FILE-PREFIX into its three pieces. Or, if it's just a package prefix, return just that."
(if (string-match "\\([^/]+\\)\\(/.*\\)" str)
(let ((package (match-string 1 str))
(path (match-string 2 str)))
(if (string-match "\\(/.*/\\)\\([^/]*\\)" path)
(cl-values package (match-string 1 path) (match-string 2 path))
(cl-values package "/" (substring path 1))))
(cl-values str nil nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Completion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun current-ros-word (&optional package)
"wraps (current-word) from simple.el to pick what is most likely a package, topic, message or action name"
(let* ((word (current-word nil nil)) ;; neither strict nor just the symbol
(index (cl-search "/" word :from-end t)))
;; E.G. navp_action/nav_actionFeedback -> nav_actionFeedback
(if index
(if package
(cl-subseq word 0 index)
(cl-subseq word (1+ index)))
word)))
(setq topic-completor (completion-table-dynamic
(lambda (str) (rosemacs-bsearch str ros-all-topics))))
(setq node-completor (completion-table-dynamic
(lambda (str) (rosemacs-bsearch str rosemacs/nodes-vec))))
(setq ros-package-completor
;; Longer because it has to deal with the case of PACKAGE/PATH-PREFIX in addition to PACKAGE-PREFIX
(completion-table-dynamic
(lambda (str)
(unless ros-packages (ros-load-package-locations))
(cl-multiple-value-bind (package dir-prefix dir-suffix) (parse-ros-file-prefix str)
(if dir-prefix
(let ((dir (concat (ros-package-dir package) dir-prefix)))
(let* ((files (directory-files dir nil (concat "^" dir-suffix)))
(comps (all-completions dir-suffix files)))
(mapcar (lambda (comp) (concat package dir-prefix comp)) comps)))
(rosemacs-bsearch package ros-packages))))))
(defun comint-dynamic-complete-ros-package ()
;; Like the above, except in the shell
(unless ros-packages (ros-load-package-locations))
(let ((prefix (comint-get-ros-package-prefix)))
(when prefix
(cl-multiple-value-bind (package dir-prefix dir-suffix) (parse-ros-file-prefix prefix)
(if dir-prefix
(let ((dir (concat (ros-package-dir package) dir-prefix)))
(let ((completions (all-completions dir-suffix (directory-files dir nil (concat "^" dir-suffix)))))
(comint-dynamic-simple-complete dir-suffix completions)
(skip-syntax-backward " ")))
(progn
(comint-dynamic-simple-complete prefix (all-completions prefix ros-package-completor))
(skip-syntax-backward " ")))))))
(defun comint-dynamic-complete-ros-topic ()
(let ((prefix (comint-get-ros-topic-prefix)))
(when prefix
(comint-dynamic-simple-complete prefix (all-completions prefix topic-completor))
(skip-syntax-backward " "))))
(defun comint-dynamic-complete-ros-node ()
(let ((prefix (comint-get-ros-node-prefix)))
(when prefix
(comint-dynamic-simple-complete prefix (all-completions prefix node-completor))
(skip-syntax-backward " "))))
(defun ros-completing-read-package (&optional prompt default completion-function
history-list)
(unless ros-packages
(ros-load-package-locations))
(let ((completion-function (or completion-function ros-completion-function))
(prompt (concat (or prompt "Enter package")
(if default
(format " (default `%s'): " default)
": "))))
(funcall completion-function
prompt (cl-map 'list #'identity ros-packages)
nil nil nil history-list default)))
(defun ros-completing-read-pkg-file (prompt &optional default-pkg)
(if (eq ros-completion-function 'ido-completing-read)
(ros-ido-completing-read-pkg-file prompt default-pkg)
(funcall ros-completion-function prompt ros-package-completor nil nil
default-pkg)))
(defun packs-and-stacks ()
"sorted list of stack and package names"
(unless ros-packages
(ros-load-package-locations))
(unless ros-stacks
(ros-load-stack-locations))
(sort (cl-remove-duplicates
(append (cl-map 'list #'identity ros-packages)
(cl-map 'list #'identity ros-stacks))
:test 'equal)
'string<))
;; Ido completion
(defun ros-ido-completing-read-pkg-file (prompt &optional default-pkg)
(unless ros-packages
(ros-load-package-locations))
(unless ros-stacks
(ros-load-stack-locations))
(let ((old-ido-make-file-list (symbol-function 'ido-make-file-list-1))
(ros-packages-list (cl-map 'list #'identity ros-packages))
(ros-stacks-list (cl-map 'list #'identity ros-stacks)))
(cl-flet ((pkg-expr->path (str)
(let ((pkg-name (cl-second (split-string str "/"))))
(unless (= (length pkg-name) 0)
(cond ((member pkg-name ros-packages-list)
(concat (ros-package-dir pkg-name)
(substring str (string-match "/" str 1))))
((member pkg-name ros-stacks-list)
(concat (ros-stack-dir pkg-name)
(substring str (string-match "/" str 1))))
(t nil))))))
(cl-letf (((symbol-function 'ido-make-file-list-1)
#'(lambda (dir &optional merged)
(let ((path (pkg-expr->path dir)))
(if path
(funcall old-ido-make-file-list path merged)
(mapcar (lambda (pkg) (if merged
(cons (concat pkg "/") "/")
(concat pkg "/")))
(packs-and-stacks)))))))
(substring (ido-read-file-name
prompt "/"
(when (member default-pkg (packs-and-stacks))
default-pkg))
1)))))
;; Unit tests for function, part of docu
;; ELISP> (ros-uniquify-for-completion "test" '("hello" "test" "world" "test") '("p1" "p2" "p3" "p4"))
;; ("test (p2)" ("test (p2)" "test (p4)" "hello" "world"))
;; ELISP> (ros-uniquify-for-completion nil '("hello" "test" "world" "test") '("p1" "p2" "p3" "p4") )
;; (nil ("hello" "test (p2)" "world" "test (p4)"))
;; ELISP> (ros-uniquify-for-completion "test2" '("hello" "test" "world" "test") '("p1" "p2" "p3" "p4") )
;; (nil ("hello" "test (p2)" "world" "test (p4)"))
;; ELISP> (ros-uniquify-for-completion "world" '("hello" "test" "world" "test") '("p1" "p2" "p3" "p4") )
;; ("world" ("hello" "test (p2)" "world" "test (p4)"))
;; last test case for package, need not sort result list to make it first, ido does that anyway
;; ELISP> (ros-uniquify-for-completion "test" '("hello" "test" "world" "test") '("p1" "p2" "p3" "p4") "p4")
;; ("test (p4)" ("test (p2)" "test (p4)" "hello" "world"))
(defun ros-uniquify-for-completion (default itemlist packagelist &optional package )
"default is e.g. a message, service or action, itemlist is a
list of candidate names (possible duplicates). packagelist is a
list that for each item in itemlist names the package. Package
is optionally a guess of name of package of default. Returns a
list of uniquified default, uniquified itemlist, and
resorteditemlist to contain first all items matching
default. Prefers item with matching package when possible."
(let* ((uniquified-item-list (cl-map 'list (lambda (m pkg)
(if (> (cl-count m itemlist :test 'equal) 1)
(format "%s (%s)" m pkg)
m))
itemlist packagelist))
(hits (cl-loop for item in itemlist
for uniqueitem in uniquified-item-list
when (string= item default)
collect uniqueitem)))
(cond
((null hits) (list nil uniquified-item-list))
((= 1 (length hits))
(list default uniquified-item-list))
(t ;; more than one hit, try to match package if any
(let ((resultitem (car (member (format "%s (%s)" default package) hits))))
;; move all matching to front
(cl-loop for item in (reverse hits) do
(setf uniquified-item-list
(cons item (remove item uniquified-item-list))))
(list
(if resultitem resultitem (car hits))
uniquified-item-list))))))
(defun ros-completing-read-message (prompt &optional default)
(unless ros-messages
(cache-ros-message-locations))
(let* ((ros-messages-list (cl-map 'list 'identity ros-messages))
(unique-pair (ros-uniquify-for-completion default ros-messages-list ros-message-packages))
(uniquified-default (car unique-pair))
(uniquified-messages-list (cadr unique-pair))
(result (funcall ros-completion-function
(cl-concatenate 'string prompt
(if uniquified-default
(format " (default %s): " uniquified-default)
": "))
uniquified-messages-list
nil nil nil nil
uniquified-default))
(ws-pos (cl-position ?\s result))
(message (substring result 0 ws-pos))
(package (when ws-pos
(let ((package-str (substring result ws-pos)))
(substring package-str 2 (- (length package-str) 1))))))
(if package
(cl-concatenate 'string package "/" message)
message)))
(defun ros-completing-read-service (prompt &optional default)
(unless ros-services
(cache-ros-service-locations))
(let* ((ros-services-list (cl-map 'list 'identity ros-services))
(unique-pair (ros-uniquify-for-completion default ros-services-list ros-service-packages))
(uniquified-default (car unique-pair))
(uniquified-services-list (cadr unique-pair))
(result (funcall ros-completion-function
(cl-concatenate 'string prompt
(if uniquified-default
(format " (default %s): " uniquified-default)
": "))
uniquified-services-list
nil nil nil nil
uniquified-default))
(ws-pos (cl-position ?\s result))
(service (substring result 0 ws-pos))
(package (when ws-pos
(let ((package-str (substring result ws-pos)))
(substring package-str 2 (- (length package-str) 1))))))
(if package
(cl-concatenate 'string package "/" service)
service)))
(defun ros-completing-read-action (prompt &optional defaults)
"asks user for ROS action, allows multiple defaults e.g. MyGoal, MyGoalGoal"
(unless ros-actions
(cache-ros-action-locations))
(let* ((ros-actions-list (cl-map 'list 'identity ros-actions))
(default (if (atom defaults)
defaults
(cl-loop for x in defaults when (member x ros-actions-list) return x)))
(unique-pair (ros-uniquify-for-completion default ros-actions-list ros-action-packages))
(uniquified-default (car unique-pair))
(uniquified-action-list (cadr unique-pair))
(result (funcall ros-completion-function
(cl-concatenate 'string prompt
(if uniquified-default
(format " (default %s): " uniquified-default)
": "))
uniquified-action-list
nil nil nil nil
uniquified-default))
(ws-pos (cl-position ?\s result))
(action (substring result 0 ws-pos))
(package (when ws-pos
(let ((package-str (substring result ws-pos)))
(substring package-str 2 (- (length package-str) 1))))))
(if package
(cl-concatenate 'string package "/" action)
action)))
(defun ros-completing-read-topic (prompt &optional default)
(funcall ros-completion-function
(cl-concatenate 'string prompt
(if (member default (cl-map 'list 'identity ros-all-topics))
(format " (default %s): " default)
": "))
(cl-map 'list #'identity ros-all-topics)
nil nil nil nil
(when (member default (cl-map 'list 'identity ros-all-topics))
default)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Navigation commands
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun find-ros-file (package-name &optional dont-reload)
"Open up the directory corresponding to PACKAGE-NAME in dired mode. If used interactively, tab completion will work."
(interactive (list (ros-completing-read-pkg-file "Enter ros path: ") nil))
(cl-multiple-value-bind (package dir-prefix dir-suffix) (parse-ros-file-prefix package-name)
(let* ((package-dir (ros-package-dir package))
(stack-dir (ros-stack-dir package))
(path (if dir-prefix
(cond
(package-dir (concat package-dir dir-prefix dir-suffix))
(stack-dir (concat stack-dir dir-prefix dir-suffix)))
package-dir)))
(if path
(find-file path)
(if dont-reload
(error "Did not find %s in the ros package list." package-name)
(progn
(lwarn '(rosemacs) :debug "Did not find %s. Reloading ros package list and trying again..." package-name)
(ros-load-package-locations)
(find-ros-file package-name t)))))))
(defun view-ros-file (ros-file-name &optional dont-reload)
"View (open in read-only mode with simpler editing commands — see emacs help) the file corresponding to ROS-FILE-NAME (in form packagename/filename). If used interactively, tab completion will work."
(interactive (list (ros-completing-read-pkg-file "Enter ros path: ") nil))
(cl-multiple-value-bind (package dir-prefix dir-suffix) (parse-ros-file-prefix ros-file-name)
(let* ((package-dir (ros-package-dir package))
(path (if dir-prefix (concat package-dir dir-prefix dir-suffix) package-dir)))
(if path
(view-file-other-window path)
(if dont-reload
(error "Did not find %s in the ros package list." ros-file-name)
(progn
(lwarn '(rosemacs) :debug "Did not find %s. Reloading ros package list and trying again..." ros-file-name)
(ros-load-package-locations)
(view-ros-file ros-file-name t)))))))
(defun find-ros-message (message)
"Open definition of a ros message. If used interactively, tab completion will work."
(interactive (list (ros-completing-read-message
"Enter message name"
(current-ros-word))))
(let* ((p+m (split-string message "/"))
(p (if (cdr p+m)
(car p+m)
(ros-message-package message)))
(m (car (last p+m))))
(unless p
(error "Could not find package for message %s" message))
(let ((dir (ros-package-dir p)))
(unless dir
(error "Could not find directory corresponding to package %s" p))
(find-file (concat dir "/msg/" m ".msg")))))
(defun find-ros-service (service)
"Open definition of a ros service. If used interactively, tab completion will work."
(interactive (list (ros-completing-read-service
"Enter service name"
(current-ros-word))))
(let* ((p+m (split-string service "/"))
(p (if (cdr p+m)
(car p+m)
(ros-service-package service)))
(m (car (last p+m))))
(unless p
(error "Could not find package for service %s" service))
(let ((dir (ros-package-dir p)))
(unless dir
(error "Could not find directory corresponding to package %s" p))
(find-file (concat dir "/srv/" m ".srv")))))
(defun action-message-prefix (message-name)
"if message-name has an action suffix, returns prefix, else nil"
(cl-loop for suffix in '("ActionGoal" "ActionFeedback" "ActionResult"
"Goal" "Feedback" "Result" "Action")
for x = (let ((index (cl-search suffix message-name :from-end t)))
(when (and index
(= index (- (length message-name) (length suffix))))
index))
when (integerp x) return (cl-subseq message-name 0 x)))
;; (defun action-message-to-action (message-name)
;; "removes a Goal, Result or Feedback Suffix"
;; (or
;; (has-action-message-suffix message-name)
;; message-name))
(defun find-ros-action (action)
"Open definition of a ros action. If used interactively, tab completion will work."
(interactive (let* ((word (current-ros-word))
(prefix (action-message-prefix word))
;; fallback if someone named message ...Goal, also add original word
(words (if prefix
(list prefix word)
word)))
(list (ros-completing-read-action
"Enter action name"
words))))
(let* ((p+m (split-string action "/"))
(p (if (cdr p+m)
(car p+m)
(ros-action-package action)))
(m (car (last p+m))))
(unless p
(error "Could not find package for action %s" action))
(let ((dir (ros-package-dir p)))
(unless dir
(error "Could not find directory corresponding to package %s" p))
(find-file (concat dir "/action/" m ".action")))))
;; (defun view-ros-message (message)
;; "Open definition of a ros message in view mode. If used interactively, tab completion will work."
;; (interactive (list (completing-read
;; (if (current-word t t)
;; (format "Enter message name (default %s): " (current-word t t))
;; "Enter message name: ")
;; message-completor nil nil nil nil (current-word t t))))
;; (let ((p (ros-message-package message)))
;; (if p
;; (let ((dir (ros-package-dir p)))
;; (if dir
;; (view-file-other-window (concat dir "/msg/" message ".msg"))
;; (error "Could not find directory corresponding to package %s" p)))
;; (error "Could not find package for message %s" message))))
(defun view-ros-message (message)
"Open definition of a ros message in view mode.
If used interactively, tab completion will work."
(interactive (list (ros-completing-read-message
"Enter message name"
(current-ros-word))))
(let ((max-mini-window-height 0))
(shell-command (format "rosmsg show %s" message))))
(defun view-ros-action (message)
"Open definition of a ros action in view mode.
If used interactively, tab completion will work."
(interactive (list (ros-completing-read-action
"Enter action name"
(current-ros-word))))
(let ((max-mini-window-height 0))
(shell-command (format "rosmsg show %sGoal;echo '---';rosmsg show %sResult;echo '---';rosmsg show %sFeedback" message message message))))
(defun view-ros-service (service)
"Open definition of a ros service in view mode.
If used interactively, tab completion will work."
(interactive (list (ros-completing-read-service
"Enter service name"
(current-ros-word))))
(let ((max-mini-window-height))
(shell-command (format "rossrv show %s" service))))
(defun ros-rgrep-package (ros-pkg regexp files)
"Run a recursive grep in `ros-pkg', with `regexp' as search
pattern and `files' as file pattern. This function is similar to
RGREP but with a ros package instead of a directory as
parameter."
(interactive (progn (grep-compute-defaults)
(let ((package (ros-completing-read-package
nil (get-buffer-ros-package)))
(regexp (grep-read-regexp)))
(list
package
regexp
(grep-read-files regexp)))))
(rgrep regexp files (ros-package-path ros-pkg)))
(defun ros-find-dired (ros-pkg args)
"Run find in ros package `ros-pkg' with arguments `args' and
load the result in a dired buffer. This function is similar to
FIND-DIRED but with a ros package instead of a directory as
parameter."
(interactive (list (ros-completing-read-package
nil (get-buffer-ros-package))
(read-string "Run find (within args): " ros-find-args
'(ros-find-args-history . 1))))
(find-dired (ros-package-path ros-pkg) args))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Core
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ros-core ()
(interactive)
(if (get-buffer "*roscore*")
(switch-to-buffer (get-buffer "*roscore*"))
(progn (start-process "roscore" (get-buffer-create "*roscore*") "roscore")
(message "roscore started"))))
(defun ros-set-master-uri (host port)
"Set the master uri used by other commands (e.g. rostopic)"
(interactive "sEnter master uri host: \nnEnter master uri port: ")
(let ((uri (format "http://%s:%d" host port)))
(setenv "ROS_MASTER_URI" uri)
(message "Set ROS_MASTER_URI to %s" uri)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rosnode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun rosemacs/parse-node-list (start finish)
(lwarn '(rosemacs) :debug "Parsing node list")
(goto-char start)
(let ((current-nodes nil))
(while (re-search-forward "^\\/\\(.*\\)$" nil t)
(when (> (match-end 0) finish)
(cl-return))
(push (match-string 1) current-nodes))
(let ((sorted-nodes (cl-sort current-nodes 'string<)))
(cl-destructuring-bind (added deleted)
(rosemacs-list-diffs rosemacs/nodes sorted-nodes)
(setq rosemacs/nodes sorted-nodes
rosemacs/nodes-vec (vconcat rosemacs/nodes))
(update-ros-node-buffer)
(when added
(lwarn '(rosemacs) :debug "New nodes: %s" added)
(rosemacs/add-event (format "New nodes: %s" added)))
(let ((l (length deleted)))
(when (> l 0)
(if (= l 1)
(rosemacs/add-event (format "Ros node %s exited" (car deleted)) t)
(rosemacs/add-event (format "%s ros nodes exited: %s" l deleted) t))))))))
(defun rosemacs/rosnode-filter (proc str)
(with-current-buffer (process-buffer proc)
(goto-char (point-max))
(insert str)
(let ((found-start (re-search-backward "BEGIN ROSNODE LIST$" nil t)))
(if found-start
(let* ((start-pt (match-end 0))
(found-finish (re-search-forward "END ROSNODE LIST$" nil t))
(end-pt (match-end 0)))
(when found-finish
(rosemacs/parse-node-list start-pt (match-beginning 0))
(delete-region (point-min) end-pt)))))))
(defun rosemacs/track-nodes (interval)
(interactive "nEnter rosnode update interval in seconds (0 to stop tracking): ")
(let ((name "*rosnode-tracker*"))
(let ((old-proc (get-process name)))
(when old-proc
(message "Cancelling existing rosnode tracker")
;; doesn't seem to respond to sigint reliably
(delete-process old-proc))
(when (> interval 0)
(let ((proc
(start-process name name
(concat rosemacs/pathname "poll-rosnode")
(format "%s" interval))))
(set-process-query-on-exit-flag proc nil)
(set-process-filter proc 'rosemacs/rosnode-filter))))))
(defun rosemacs/get-stamp-string ()
(goto-char (point-min))
(let ((pos (re-search-forward "^\\(Last updated.*\\)$" nil t)))
(if pos
(match-string 1)
"Last updated: <>")))
(defun update-ros-node-buffer ()
(let ((ros-node-buffer (get-buffer-create "*ros-nodes*")))
(save-excursion
(set-buffer ros-node-buffer)
(let ((old-stamp (rosemacs/get-stamp-string)))
(erase-buffer)
(princ (format "Master uri: %s\n" (getenv "ROS_MASTER_URI")) ros-node-buffer)
(princ old-stamp ros-node-buffer)
(let ((time-stamp-pattern "5/^Last updated: <%02H:%02M:%02S")) (time-stamp))
(princ "\n\n" ros-node-buffer)
(dolist (n rosemacs/nodes)
(princ (format "%s\n" n) ros-node-buffer))))))
(defun rosemacs/display-nodes (&optional other-window)
(interactive)
(let ((buf (get-buffer-create "*ros-nodes*")))
(if other-window
(display-buffer buf)
(switch-to-buffer buf))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rostopic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun rosemacs-topic-filter (proc str)
(with-current-buffer (process-buffer proc)
(goto-char (point-max))
(insert str)
(let ((found-start (re-search-backward "BEGIN ROSTOPIC LIST$" nil t)))
(if found-start
(let* ((start-point (match-end 0))
(found-finish (re-search-forward "END ROSTOPIC LIST" nil t)))
(if found-finish
(let ((finish-pt (match-beginning 0)))
(rosemacs/parse-topic-list start-point finish-pt)
(delete-region (point-min) finish-pt))))))))
(defun rosemacs/track-topics (interval)
(interactive "Enter rostopic update interval in seconds (0 to stop tracking).")
(let ((name "*rostopic-tracker*"))
(let ((old-proc (get-process name)))
(when old-proc
(message "Cancelling existing rostopic tracker")
(delete-process old-proc)))
(when (> interval 0)
(let ((proc
(start-process name name
(concat rosemacs/pathname "poll-rostopic")
(format "%s" interval))))
(set-process-query-on-exit-flag proc nil)
(set-process-filter proc 'rosemacs-topic-filter)))))
(defun display-ros-topic-info ()
"Display current ros topic info in *ros-topics* buffer"
(interactive)
(let ((buf (get-buffer "*ros-topics*")))
(if buf
(switch-to-buffer buf)
(progn
(setq ros-topic-buffer (get-buffer-create "*ros-topics*"))
(switch-to-buffer ros-topic-buffer)))
(ros-topic-list-mode 1)
(update-ros-topic-buffer)))
(defun add-hz-update (topic-regexp)
(interactive (list (ros-completing-read-topic "Enter topic name or regexp to track: ")))
(push topic-regexp ros-hz-topic-regexps)
(dolist (topic ros-topics)
(when (string-match topic-regexp topic)
(unless (assoc topic ros-topic-last-hz-rate)
(start-hz-tracker topic)))))
(defun remove-hz-update (topic-regexp)
(interactive (list (funcall ros-completion-function "Enter regexp to stop tracking: "
ros-hz-topic-regexps)))
(setq ros-hz-topic-regexps (delete topic-regexp ros-hz-topic-regexps))
(dolist (pair ros-topic-last-hz-rate)
(let ((topic (car pair)))
(when (string-match topic-regexp topic)
(stop-hz-tracker topic)))))
(defun echo-ros-topic (topic)
"Create a new buffer in which rostopic echo is done on the given topic
(read interactively, with tab-completion)"
(interactive (list (let ((word (current-word)))
(ros-completing-read-topic
"Enter topic name"
word))))
(let* ((topic-full-name (if (string-match "^/" topic) topic (concat "/" topic)))
(buffer-name (concat "*rostopic:" topic-full-name "*"))
(process (start-process buffer-name buffer-name "rostopic" "echo" topic-full-name)))
(view-buffer-other-window (process-buffer process))
(ros-topic-echo-mode 1)))