-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mentor.el
1924 lines (1640 loc) · 69.5 KB
/
mentor.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
;;; mentor.el --- Frontend for the rTorrent bittorrent client -*- lexical-binding: t -*-
;; Copyright (C) 2010-2023 Stefan Kangas
;; Author: Stefan Kangas <[email protected]>
;; Maintainer: Stefan Kangas <[email protected]>
;; Version: 0.5
;; Keywords: comm, processes, bittorrent
;; Package-Requires: ((emacs "25.1") (xml-rpc "1.6.15") (seq "1.11") (async "1.9.3") (url-scgi "0.8"))
;; URL: https://github.com/skangas/mentor
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is NOT part of GNU Emacs.
;; Mentor 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.
;; Mentor 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.
;; You should have received a copy of the GNU General Public License
;; along with Mentor. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; mentor is an Emacs frontend for the `rTorrent' bittorrent client.
;; By default, it will start and run rTorrent from within Emacs but can also be
;; configured to use an external rTorrent instance over XML-RPC.
;; This project aims to provide a feature complete and highly customizable
;; interface, which will feel familiar to Emacs users. Key bindings are chosen
;; to be as close to the vanilla rTorrent curses interface as possible.
;; You can find the latest development version of mentor here:
;;
;; https://www.github.com/skangas/mentor
;; Bug reports, comments, and suggestions are welcome! Send them to
;; Stefan Kangas <[email protected]> or report them on GitHub.
;;; Code:
(eval-when-compile (require 'sort))
(eval-when-compile (require 'subr-x)) ; for `string-join' (on Emacs 26)
(require 'async)
(require 'cl-lib)
(require 'dired)
(require 'seq)
(require 'term)
(require 'xml-rpc)
(require 'mentor-data)
(require 'mentor-files)
(require 'mentor-rpc)
(require 'url-scgi)
(defconst mentor-version "0.5"
"The version of Mentor that you're using.")
;;;; Customizable variables
(defgroup mentor nil
"Emacs frontend for the rTorrent bittorrent client, using XML-RPC."
:prefix "mentor-"
:group 'external)
(defcustom mentor-custom-views
'((1 . "main")
(2 . "main") ; by default "name" is the same as main except for sorting
(3 . "started")
(4 . "stopped")
(5 . "complete")
(6 . "incomplete")
(7 . "hashing")
(8 . "seeding")
(9 . "leeching")
(0 . "active"))
"A list of mappings to bind keys to views.
This takes the form of a list of \"(BINDING . NAME)\" where
BINDING is the key to which the specified view NAME will be bound
to."
:group 'mentor
:type '(alist :key-type integer :value-type string))
(defcustom mentor-default-view "main"
"The default view to use when browsing torrents."
:group 'mentor
:type 'string)
(defcustom mentor-directory-prefix ""
"Prefix to use before all directories.
If rTorrent is running on a remote host, you could set this to
something like `/ssh:[email protected]:'."
:group 'mentor
:type 'string)
(defcustom mentor-highlight-enable nil
"If non-nil, highlight the line of the current torrent."
:group 'mentor
:type 'boolean)
(defcustom mentor-rtorrent-download-directory nil
"Download directory for background rTorrent."
:package-version '(mentor . "0.2")
:group 'mentor
:type 'string)
(defcustom mentor-rtorrent-keep-session nil
"If non-nil, save session in background rTorrent."
:package-version '(mentor . "0.2")
:group 'mentor
:type 'boolean)
(defcustom mentor-rtorrent-extra-conf nil
"Extra configuration to add to background rTorrent."
:package-version '(mentor . "0.2")
:group 'mentor
:type 'string)
(defcustom mentor-rtorrent-external-rpc nil
"URL to an external rTorrent XML-RPC socket.
The default nil value indicates that Mentor should spawn a new
rTorrent instance in the background.
To connect using a local socket file, use
`~/.rtorrent-rpc.socket' or `/any/full/path'. Note that you must
start the path with `/' or '~' for it to be recognized as a file
socket.
To connect using HTTPS, use `https://HOST[:PORT]/PATH'. This would
be the case when using a web server in front of rTorrent.
For security reasons, we strongly suggest to use one of the
methods above. However, it is also possibly to connect using a
scgi_port by specifying `scgi://HOST:PORT'.
Example values:
(1) ~/.rtorrent-rpc.socket
(2) https://localhost:8080/RPC2
(3) scgi://localhost:5000 [not recommended]"
:package-version '(mentor . "0.2")
:group 'mentor
:type 'string)
(defcustom mentor-rtorrent-use-system-daemon nil
"Set \"system.daemon = true\" when we generate rtorrent.rc.
This will only work with rTorrent 0.9.7 or later."
:package-version '(mentor . "0.3.5")
:group 'mentor
:type 'string)
(defface mentor-download-name '((t :foreground "#94BFF3"))
"Face for mentor download name."
:group 'mentor-faces)
(defface mentor-download-state '((t :foreground "#DCA3A3"))
"Face for mentor download name."
:group 'mentor-faces)
(defface mentor-download-progress '((t :foreground "#DCDCCC"))
"Face for mentor download name."
:group 'mentor-faces)
(defface mentor-download-speed-up '((t :foreground "#F0DFAF"))
"Face for mentor download name."
:group 'mentor-faces)
(defface mentor-download-speed-down '((t :foreground "#8FB28F"))
"Face for mentor download name."
:group 'mentor-faces)
(defface mentor-download-size '((t :foreground "#4C7073"))
"Face for mentor download name."
:group 'mentor-faces)
(defface mentor-tracker-name '((t :foreground "#4C7073"))
"Face for mentor tracker name."
:group 'mentor-faces)
(defface mentor-download-message '((t :foreground "#AC7373"))
"Face for mentor download name."
:group 'mentor-faces)
(defcustom mentor-view-columns
'(((mentor-download-state-column) -2 "State" mentor-download-state)
((mentor-download-speed-up-column) -5 "Up" mentor-download-speed-up)
((mentor-download-speed-down-column) -5 "Down" mentor-download-speed-down)
((mentor-download-progress-column) -3 "Cmp" mentor-download-progress)
((mentor-download-size-column) -4 "Size" mentor-download-size)
(name -50 "Name" mentor-download-name)
((mentor-download-tracker-name-column) -20 "Tracker" mentor-tracker-name)
(message -40 "Message" mentor-download-message)
(directory -100 "Directory"))
"A list of all columns to show in mentor view."
:group 'mentor
:type '(repeat (list symbol integer string)))
;;;; Internal variables
(defvar mentor-mode-hook)
(defvar mentor-current-view)
(defvar mentor-home-dir (expand-file-name (locate-user-emacs-file "mentor/"))
"Where Mentor should put its files.")
(defvar-local mentor--header-line "")
(defvar mentor-rtorrent-client-version)
(defvar mentor-rtorrent-library-version)
(defvar mentor-rtorrent-buffer-name "*mentor-term*"
"Name of the buffer that will run rTorrent process.")
(defvar mentor--rtorrent-session-directory nil)
(defvar mentor-rtorrent-name)
(defvar-local mentor-sort-list '(name))
(defvar-local mentor-last-used-view nil)
(defvar-local mentor-last-move-target "~")
(defvar mentor-view-torrent-list nil
"A list of torrents in given views.")
(defvar mentor-marker-char ?*)
(defface mentor-highlight-face
'((((class color) (background light))
:background "gray13")
(((class color) (background dark))
:background "dark goldenrod"))
"Face for highlighting the current torrent."
:group 'mentor)
(defvar mentor-default-item-faces
'((torrent . nil) (file . nil) (dir . mentor-directory-face))
"An alist with the default face for item types.")
;; Variables that should be changed by sub-modes
(defvar-local mentor-item-update-this-fun nil)
(defvar-local mentor-set-priority-fun nil)
(defvar-local mentor--columns-var 'mentor-view-columns)
;;;; Mentor major-mode
(defvar mentor-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map t)
;; navigation
(define-key map (kbd "<up>") #'mentor-previous-item)
(define-key map (kbd "<down>") #'mentor-next-item)
(define-key map (kbd "p") #'mentor-previous-item)
(define-key map (kbd "n") #'mentor-next-item)
;; download list actions
(define-key map (kbd "DEL") #'mentor-download-load-torrent)
(define-key map (kbd "l") #'mentor-download-load-magnet-link-or-url)
(define-key map (kbd "g") #'mentor-update)
(define-key map (kbd "G") #'mentor-reload)
(define-key map (kbd "M-g") #'mentor-update-item)
;; item actions
(define-key map (kbd "+") #'mentor-increase-priority)
(define-key map (kbd "-") #'mentor-decrease-priority)
;; single download actions
(define-key map (kbd "C") #'mentor-download-copy-data)
(define-key map (kbd "R") #'mentor-download-move)
(define-key map (kbd "b") #'mentor-download-set-inital-seeding)
(define-key map (kbd "e") #'mentor-download-set-create-resized-queued-flags)
(define-key map (kbd "o") #'mentor-download-change-target-directory)
(define-key map (kbd "d") #'mentor-download-stop)
(define-key map (kbd "D") #'mentor-download-remove)
(define-key map (kbd "k") #'mentor-download-close)
(define-key map (kbd "K") #'mentor-download-remove-including-files)
(define-key map (kbd "r") #'mentor-download-hash-check)
(define-key map (kbd "s") #'mentor-download-start)
(define-key map (kbd "x") #'mentor-call-command)
;; misc actions
(define-key map (kbd "RET") #'mentor-show-download-files)
(define-key map (kbd "TAB") #'mentor-toggle-item)
(define-key map (kbd "m") #'mentor-mark)
(define-key map (kbd "u") #'mentor-unmark)
(define-key map (kbd "M") #'mentor-mark-all)
(define-key map (kbd "U") #'mentor-unmark-all)
(define-key map (kbd "v") #'mentor-dired-jump)
;; sort functions
(define-key map (kbd "t c") #'mentor-sort-by-state)
(define-key map (kbd "t D") #'mentor-sort-by-directory)
(define-key map (kbd "t d") #'mentor-sort-by-download-speed)
(define-key map (kbd "t n") #'mentor-sort-by-name)
;; (define-key map (kbd "t p") #'mentor-sort-by-property-prompt)
(define-key map (kbd "t s") #'mentor-sort-by-size)
(define-key map (kbd "t t") #'mentor-sort-by-tied-file-name)
(define-key map (kbd "t u") #'mentor-sort-by-upload-speed)
(define-key map (kbd "q") #'bury-buffer)
(define-key map (kbd "Q") #'mentor-shutdown)
;; view bindings
(define-key map (kbd "a") #'mentor-add-torrent-to-view)
(define-key map (kbd "w") #'mentor-switch-to-view)
(define-key map (kbd "1") #'mentor-switch-to-view-1)
(define-key map (kbd "2") #'mentor-switch-to-view-2)
(define-key map (kbd "3") #'mentor-switch-to-view-3)
(define-key map (kbd "4") #'mentor-switch-to-view-4)
(define-key map (kbd "5") #'mentor-switch-to-view-5)
(define-key map (kbd "6") #'mentor-switch-to-view-6)
(define-key map (kbd "7") #'mentor-switch-to-view-7)
(define-key map (kbd "8") #'mentor-switch-to-view-8)
(define-key map (kbd "9") #'mentor-switch-to-view-9)
(define-key map (kbd "0") #'mentor-switch-to-view-0)
(define-key map [remap dired-jump] #'mentor-dired-jump)
map))
(easy-menu-define mentor-mode-menu mentor-mode-map
"Mentor menu."
'("Mentor"
["Load torrent" mentor-download-load-torrent t]
["Load Magnet Link or URL" mentor-download-load-magnet-link-or-url t]
"---"
["Update data" mentor-update t]
["Re-initialize data" mentor-reload t]
["Update item at point" mentor-update-item t]
("Sort"
["Sort by name" mentor-sort-by-name t]
["Sort by directory" mentor-sort-by-directory t]
["Sort by state" mentor-sort-by-state t]
["Sort by size" mentor-sort-by-size t]
["Sort by tied file name" mentor-sort-by-tied-file-name t]
["Sort by download speed" mentor-sort-by-download-speed t]
["Sort by upload speed" mentor-sort-by-upload-speed t]
;; ["Sort by any property..." mentor-sort-by-property-prompt t]
)
"---"
["Mark" mentor-mark t]
["Unmark" mentor-unmark t]
["Mark all" mentor-mark-all t]
["Unmark all" mentor-unmark-all t]
"---"
["Start download" mentor-download-start t]
["Stop download" mentor-download-stop t]
["Close download" mentor-download-close t]
["Change directory" mentor-download-change-target-directory t]
["Move download" mentor-download-move t]
["Copy download data" mentor-download-copy-data t]
["Remove download" mentor-download-remove t]
["Remove including data " mentor-download-remove-including-files t]
"---"
["Open file view" mentor-show-download-files t]
["Open in dired" mentor-dired-jump t]
["Hash check" mentor-download-hash-check t]
["Set resized/queued" mentor-download-set-create-resized-queued-flags t]
["Set initial seeding" mentor-download-set-inital-seeding t]
["Increase priority" mentor-increase-priority t]
["Decrease priority" mentor-decrease-priority t]
"---"
("Switch view"
["View 1" mentor-switch-to-view-1 t]
["View 2" mentor-switch-to-view-2 t]
["View 3" mentor-switch-to-view-3 t]
["View 4" mentor-switch-to-view-4 t]
["View 5" mentor-switch-to-view-5 t]
["View 6" mentor-switch-to-view-6 t]
["View 7" mentor-switch-to-view-7 t]
["View 8" mentor-switch-to-view-8 t]
["View 9" mentor-switch-to-view-9 t]
["View 0" mentor-switch-to-view-0 t]
["Named view..." mentor-switch-to-view t])
["Add torrent to view" mentor-add-torrent-to-view t]
"---"
["Run XML-RPC Command" mentor-call-command t]
["Customize Mentor" mentor-customize t]
"---"
["Bury Mentor Buffer" bury-buffer t]
["Quit Mentor" mentor-shutdown]))
(define-derived-mode mentor-mode special-mode "mentor"
"Major mode for controlling rTorrent.
Type \\[mentor] to start Mentor.
\\<mentor-mode-map>
rTorrent operations:
\\[mentor-download-load-torrent] Add torrent
\\[mentor-download-load-magnet-link-or-url] Add Magnet link, URL or torrent file path
\\[mentor-update] Reload data from rTorrent
\\[mentor-reload] Re-initialize all data from rTorrent
Operations on download at point (or marked downloads):
\\[mentor-download-start] Start download
\\[mentor-download-stop] Stop download
\\[mentor-download-close] Close download
\\[mentor-download-hash-check] Initiate hash check
\\[mentor-download-move] Move download
\\[mentor-download-change-target-directory] Change target directory
\\[mentor-download-remove] Remove download
\\[mentor-download-remove-including-files] Remove download including data
\\[mentor-download-copy-data] Copy downloaded data to location
\\[mentor-increase-priority] Increase priority of download
\\[mentor-decrease-priority] Decrease priority of download
\\[mentor-download-set-create-resized-queued-flags] Set the \"create/resize queued\" flags on all files in a torrent.
This is necessary if the underlying files in a torrent have been
deleted or truncated, and thus rtorrent must recreate them.
Operations on download at point:
\\[mentor-show-download-files] Enter files view
\\[mentor-update-item] Reload data from rTorrent
\\[mentor-dired-jump] Show download in Dired
Marking commands:
\\[mentor-mark] Mark download
\\[mentor-unmark] Unmark download
\\[mentor-mark-all] Mark all downloads
\\[mentor-unmark-all] Unmark all downloads
Sorting downloads:
\\[mentor-sort-by-directory] Sort downloads by directory
\\[mentor-sort-by-name] Sort by name
\\[mentor-sort-by-tied-file-name] Sort by tied file name
\\[mentor-sort-by-size] Sort by size
\\[mentor-sort-by-state] Sort by state
\\[mentor-sort-by-download-speed] Sort by download speed
\\[mentor-sort-by-upload-speed] Sort by upload speed
Misc commands:
\\[mentor-switch-to-view-0] .. \\[mentor-switch-to-view-9] Change currently active view
\\[mentor-switch-to-view] Switch to view (prompt)
\\[mentor-add-torrent-to-view] Add download to view
\\[mentor-call-command] Send XML-RPC command to rTorrent
\\[mentor-shutdown] Shutdown Mentor
\\[bury-buffer] Bury Mentor buffer"
:group 'mentor
:interactive nil
(setq truncate-lines t)
(setq buffer-read-only t)
(setq show-trailing-whitespace nil)
(setq mentor-current-view mentor-default-view
mentor-items (make-hash-table :test 'equal))
(add-hook 'post-command-hook #'mentor-post-command-hook t t)
;; No line numbers.
(when (and (fboundp 'display-line-numbers-mode)
(bound-and-true-p global-display-line-numbers-mode))
(display-line-numbers-mode -1))
(when (and (fboundp 'linum-mode)
(bound-and-true-p global-linum-mode))
(linum-mode -1))
(when (and (fboundp 'nlinum-mode)
(bound-and-true-p global-nlinum-mode))
(nlinum-mode -1))
(when (and (fboundp 'display-line-numbers-mode)
(bound-and-true-p global-display-line-numbers-mode))
(display-line-numbers-mode -1))
(run-mode-hooks 'mentor-mode-hook))
(defun mentor-rtorrent-create-conf (filename rpc)
"Generate rTorrent configuration and write to FILENAME.
It will use the RPC argument as value for scgi_local."
(let ((output
(concat
(format "## Autogenerated by mentor.el at %s\n"
(format-time-string "%Y-%m-%dT%H:%M:%S"))
(format "scgi_local = %s\n" rpc)
(when mentor-rtorrent-use-system-daemon
"system.daemon = true\n")
(if mentor-rtorrent-download-directory
(format "directory = %s\n" mentor-rtorrent-download-directory) "")
(if mentor-rtorrent-keep-session
(format "session = %s\n" mentor--rtorrent-session-directory) "")
"encoding.add = utf8\n"
(if mentor-rtorrent-extra-conf
(concat "\n## User additions from `mentor-rtorrent-extra-conf':\n"
mentor-rtorrent-extra-conf) "") "\n")))
(with-temp-file filename
(insert output))))
(defun mentor-rtorrent-already-running (buf)
(if (and buf (get-buffer buf))
(condition-case _error
;; Already running
(progn
(mentor-rpc-command "system.pid")
t)
(error
;; Running with problems -- needs restart
nil))
nil))
(defun mentor-rtorrent-run-in-background ()
"Start rTorrent in a new buffer."
(make-directory mentor-home-dir t)
(when mentor-rtorrent-keep-session
(make-directory
(setq mentor--rtorrent-session-directory
(expand-file-name "session" mentor-home-dir)) t))
(let ((bufname mentor-rtorrent-buffer-name)
(buf nil)
(rpc (expand-file-name "rtorrent.rpc" mentor-home-dir))
(conf (expand-file-name "rtorrent.rc" mentor-home-dir)))
(setq mentor-rpc--rtorrent-url (concat "scgi://" rpc))
(when (not (mentor-rtorrent-already-running bufname))
(when (bufferp bufname)
(kill-buffer bufname))
(mentor-rtorrent-create-conf conf rpc)
(let ((rtorrent (executable-find "rtorrent")))
(if (not rtorrent)
(error "Unable to find rtorrent executable"))
(get-buffer-create bufname)
(setq buf (term-ansi-make-term
bufname "rtorrent" nil "-n" "-o" (concat "import=" conf))))
(with-current-buffer buf
(term-mode)
(term-char-mode)
(let (term-escape-char)
(term-set-escape-char ?\C-x))
(bury-buffer)))))
(defun mentor-normalize-rpc-url (url)
(if (string-match "^[~/]" url)
(concat "scgi://" url)
url))
(defun mentor-setup-rtorrent ()
(if mentor-rtorrent-external-rpc
(setq mentor-rpc--rtorrent-url (mentor-normalize-rpc-url mentor-rtorrent-external-rpc))
(let ((rtorrent-started nil)
(since (float-time)))
(mentor-rtorrent-run-in-background)
(message "Waiting for rtorrent to start...")
(while (not rtorrent-started)
(condition-case err
(progn (mentor-rpc-command "system.pid")
(setq rtorrent-started t))
(error
(if (string-match "make client process failed: connection refused"
(error-message-string err))
(if (< (float-time) (+ since 10))
(sleep-for 0.1)
(error "XML-RPC not up after 10 seconds: %s" err))
(signal (car err) (cdr err)))))))))
;;;###autoload
(defun mentor ()
"Control rTorrent from Emacs using XML-RPC.
If mentor is already running, switch to its buffer. Otherwise,
start a new session.
Full documentation is available under `mentor-mode'."
(interactive)
(if (get-buffer "*mentor*")
;; Assume that it's set up correctly if it exists
(switch-to-buffer (get-buffer-create "*mentor*"))
;; Otherwise create and set it up
(switch-to-buffer (get-buffer-create "*mentor*"))
(mentor-mode)
(mentor-setup-rtorrent)
(setq mentor-item-update-this-fun 'mentor-download-update-and-reinsert-at-point)
(setq mentor-set-priority-fun 'mentor-download-set-priority-fun)
(setq mentor--columns-var 'mentor-view-columns)
(setq mentor-sort-list '((up.rate . t) name))
(mentor-init-header-line)
(setq mentor-rtorrent-client-version (mentor-rpc-command "system.client_version")
mentor-rtorrent-library-version (mentor-rpc-command "system.library_version")
mentor-rtorrent-name (mentor-rpc-command "session.name"))
(let* ((pwd-with-trailing-newline (mentor-rpc-command "execute.capture" "" "pwd"))
(pwd (substring pwd-with-trailing-newline 0 -1)))
;; The directory doesn't exist when running on a remote host.
(ignore-errors
(cd (file-name-as-directory pwd))))
(mentor-set-view mentor-default-view)
(when (equal mentor-current-view mentor-last-used-view)
(setq mentor-last-used-view (mentor-get-custom-view-name 2)))
(mentor-download-data-init)
(mentor-views-init)
(mentor-redisplay)
(goto-char (point-min))))
;;;###autoload
(defun mentor-customize ()
"Call the customize function with mentor as argument."
(interactive)
(customize-browse 'mentor))
(defun mentor-post-command-hook ()
(when mentor-highlight-enable
(mentor-highlight-torrent)))
(defun mentor-init-header-line ()
(setq header-line-format
'(:eval (concat
(propertize " " 'display '((space :align-to 1)))
(substring mentor--header-line
(min (length mentor--header-line)
(window-hscroll)))))))
;;;; Mentor items
(defun mentor-between-items ()
(not (mentor-item-id-at-point)))
(defun mentor-marker-regexp ()
(concat "^" (regexp-quote (char-to-string mentor-marker-char))))
(defun mentor-repeat-over-lines (arg function)
"Repeat FUNCTION over ARG lines.
This version skips non-file lines."
(let ((pos (make-marker)))
(beginning-of-line)
(while (and (> arg 0) (not (eobp)))
(setq arg (1- arg))
(beginning-of-line)
(while (and (not (eobp)) (mentor-between-items)) (forward-line 1))
(save-excursion
(forward-line 1)
(move-marker pos (1+ (point))))
(save-excursion (funcall function))
;; Advance to the next line--actually, to the line that *was* next.
;; (If FUNCTION inserted some new lines in between, skip them.)
(goto-char pos))
(while (and (< arg 0) (not (bobp)))
(setq arg (1+ arg))
(forward-line -1)
(while (and (not (bobp)) (mentor-between-items)) (forward-line -1))
(beginning-of-line)
(save-excursion (funcall function)))
(move-marker pos nil)
;; (dired-move-to-filename)
))
(defmacro mentor-map-over-marks (body arg &optional _show-progress)
"Eval BODY with point on each marked line. Return a list of BODY's results.
If no marked item could be found, execute BODY on the current line.
ARG, if non-nil, specifies the items to use instead of the marked items.
If ARG is an integer, use the next ARG (or previous -ARG, if
ARG<0) items. In that case, point is dragged along. This is
so that commands on the next ARG (instead of the marked) items
can be chained easily.
For any other non-nil value of ARG, use the current item.
If optional third arg SHOW-PROGRESS evaluates to non-nil,
redisplay item after it has been processed.
No guarantee is made about the position on the marked line.
BODY must ensure this itself if it depends on this.
Search starts at the beginning of the buffer, thus the car of the list
corresponds to the line nearest to the buffer's bottom. This
is also true for (positive and negative) integer values of ARG.
BODY should not be too long as it is expanded four times."
(declare (indent defun))
;;
;;Warning: BODY must not add new lines before point - this may cause an
;;endless loop.
;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
`(prog1
(let ((inhibit-read-only t) case-fold-search found results)
(if ,arg
(if (integerp ,arg)
(progn ;; no save-excursion, want to move point.
(mentor-repeat-over-lines
,arg
(function (lambda ()
;; (if ,show-progress (sit-for 0))
(setq results (cons ,body results)))))
(if (< ,arg 0)
(nreverse results)
results))
;; non-nil, non-integer ARG means use current file:
(list ,body))
(let ((regexp (mentor-marker-regexp)) next-position)
(save-excursion
(goto-char (point-min))
;; remember position of next marked file before BODY
;; can insert lines before the just found file,
;; confusing us by finding the same marked file again
;; and again and...
(setq next-position (and (re-search-forward regexp nil t)
(point-marker))
found (not (null next-position)))
(while next-position
(goto-char next-position)
;; (if ,show-progress (sit-for 0))
(setq results (cons ,body results))
;; move after last match
(goto-char next-position)
(forward-line 1)
(set-marker next-position nil)
(setq next-position (and (re-search-forward regexp nil t)
(point-marker)))))
(if found
results
(list ,body)))))))
;; ;; save-excursion loses, again
;; (dired-move-to-filename)))
(defun mentor-get-marked-items (&optional arg)
"Return the marked items' names as list of strings.
The list is in the same order as the buffer, that is, the car is the
first marked file.
Optional argument ARG, if non-nil, specifies items near
point instead of marked items. It usually comes from the prefix
argument.
If ARG is an integer, use the next ARG items.
Any other non-nil value means to use the current file instead."
(save-excursion
(nreverse
(mentor-map-over-marks
(mentor-item-property 'name (mentor-get-item-at-point))
arg))))
;; Based on `dired-mark-pop-up'
(defun mentor-mark-pop-up (bufname items function &rest args)
"Return FUNCTION's result on ARGS after showing which items are marked.
Displays the file names in a window showing a buffer named
BUFNAME; the default name being \" *Marked Items*\". The window
is not shown if there is just one item.
ITEMS is the list of marked items.
FUNCTION should not manipulate items, just read input
\(an argument or confirmation)."
(if (= (length items) 1)
(apply function args)
(let ((buffer (get-buffer-create (or bufname " *Marked Items*"))))
(with-current-buffer buffer
(with-current-buffer-window
buffer
(cons 'display-buffer-below-selected
'((window-height . fit-window-to-buffer)))
#'(lambda (window _value)
(with-selected-window window
(unwind-protect
(apply function args)
(when (window-live-p window)
(quit-restore-window window 'kill)))))
(erase-buffer)
(completion--insert-strings items))))))
(defun mentor-mark-prompt (arg items)
"Return a string suitable for use in a mentor prompt.
ARG is normally the prefix argument for the calling command.
ITEMS should be a list of item names."
(let ((count (length items)))
(if (= count 1)
(car items)
;; more than 1 item:
(if (integerp arg)
(format "[%s %d items]" (if (> arg 0) "next" "previous") count)
(format "[%d items]" count)))))
(defun mentor-mark-confirm (desc arg)
(let ((items (mentor-get-marked-items arg)))
(mentor-mark-pop-up nil items (function y-or-n-p)
(concat desc " "
(mentor-mark-prompt arg items) "? "))))
;;;; Getting torrent data
(defun mentor-download-data-init ()
"Initialize torrent data from rTorrent.
All torrent information will be re-fetched, making this an
expensive operation."
(message "Initializing torrent data...")
(mentor-rpc-d.multicall mentor-rpc-d-methods mentor-rpc-t-methods t)
(mentor-views-update-views)
(message "Initializing torrent data... DONE"))
(defun mentor-download-data-update-all ()
(message "Updating torrent data...")
(condition-case _err
(progn
(mentor-rpc-d.multicall mentor-rpc-volatile-d-methods mentor-rpc-t-methods)
(message "Updating torrent data...DONE"))
(mentor-need-init
(mentor-download-data-init))))
(defun mentor-download-update-this (download)
"Update specified DOWNLOAD."
(let* ((hash (mentor-item-property 'hash download))
(methods (append mentor-rpc-volatile-d-methods))
(values (cl-mapcar
(lambda (method)
(mentor-rpc-command method hash))
methods)))
(let ((d-properties (mentor-rpc-methods-to-properties methods)))
(mentor-data-download-update-from d-properties nil values))))
(defun mentor-download-update-and-reinsert-at-point ()
"Update download at point and reinsert in buffer."
(let ((download (mentor-get-item-at-point)))
(mentor-download-update-this download)
(mentor-download-reinsert-at-point)))
;;;; Main torrent view
(defmacro mentor-keep-position (&rest body)
"Run BODY form but keep the current position."
`(let ((kept-torrent-id (mentor-item-id-at-point))
(kept-torrent-pos-in-line (- (point) (line-beginning-position)))
(kept-point (point)))
,@body
(if kept-torrent-id
(condition-case _err
(progn (mentor-goto-download kept-torrent-id)
(forward-char kept-torrent-pos-in-line))
(mentor-missing-torrent
(goto-char kept-point)))
(goto-char kept-point))))
(defun mentor-insert-torrent (id)
"Insert download ID at point."
(let* ((item (mentor-get-item id))
(text (mentor-process-view-columns item mentor-view-columns))
(marked (mentor-item-marked item)))
(insert (propertize text
'marked marked
'field id
'collapsed t
'type 'torrent) "\n")
(when marked
(save-excursion
(mentor-previous-item)
(mentor-mark)))))
(defun mentor-insert-torrents ()
"Insert downloads in current view at point."
(let ((tor-ids (cdr (assoc (intern mentor-current-view)
mentor-view-torrent-list))))
(dolist (id tor-ids)
(mentor-insert-torrent id))))
(defun mentor-download-reinsert-at-point ()
"Reinsert download at point."
(let ((inhibit-read-only t)
(id (mentor-item-id-at-point)))
(mentor-delete-item-from-buffer (point))
(mentor-insert-torrent id)
(mentor-previous-item))
(mentor-goto-item-name-column))
(defun mentor-process-columns-helper (cols lenfun strfun)
(replace-regexp-in-string
" *$" "" ; Remove trailing whitespace
(apply #'concat
(cl-mapcar (lambda (column)
(let* ((len (funcall lenfun column))
(str (funcall strfun column)))
(concat (mentor-enforce-length str len) " ")))
cols))))
(defun mentor-process-view-header-columns (cols)
(mentor-process-columns-helper
cols
(lambda (col) ;lenfun
(cadr col))
(lambda (col) ; strfun
(cl-caddr col))))
(defun mentor-process-view-columns (item cols)
(concat
" "
(mentor-process-columns-helper
cols
(lambda (col) ; lenfun
(cadr col))
(lambda (col) ; strfun
(let* ((col-name (car col))
(text
(cond ((not col-name) "")
((listp col-name)
(apply (car col-name) item (cdr col-name)))
(t
(let ((text (mentor-item-property col-name item)))
(if text text "")))))
(col-face (cl-cadddr col)))
(if col-face
(propertize text
'face col-face)
text))))))
(defun mentor--find-name-column (cols)
(1+ (apply '+ (cl-mapcar
(lambda (col) (1+ (abs (cadr col))))
(seq-take-while
(lambda (fmt)
(not (eq (car fmt) 'name)))
cols)))))
(defun mentor-reload-header-line ()
(setq mentor--header-line
(mentor-process-view-header-columns (eval mentor--columns-var))))
(defvar-local mentor-highlight-overlay nil)
(defvar-local mentor-highlighted-torrent nil)
(defun mentor-highlight-torrent ()
(let ((cur (mentor-item-id-at-point)))
(when (not mentor-highlight-overlay)
(setq mentor-highlight-overlay (make-overlay 1 10))
(overlay-put mentor-highlight-overlay
'face 'mentor-highlight-face))
(if cur
(when (not (equal cur mentor-highlighted-torrent))
(setq mentor-highlighted-torrent cur)
(move-overlay mentor-highlight-overlay
(mentor-get-item-beginning)
(mentor-get-item-end)
(current-buffer)))
(delete-overlay mentor-highlight-overlay)
(setq mentor-highlighted-torrent nil))))
;;;; Sorting
(defun mentor-do-sort ()
(mentor-keep-position
(goto-char (point-min))
(save-excursion
(let ((sort-fold-case t)
(inhibit-read-only t))
(sort-subr nil
(lambda () (ignore-errors (mentor-forward-item 1)))
(lambda () (ignore-errors (mentor-end-of-item)))
(lambda ()
(let ((item (mentor-get-item-at-point)))
(cl-mapcar (lambda (p)
(let ((prop (or (and (listp p) (car p)) p)))
(mentor-item-property prop item)))
mentor-sort-list)))
nil
(lambda (a b)
(mentor-cmp-properties a b mentor-sort-list)))))))
(defun mentor-cmp-properties (x y &optional props)
(let* ((a (car x))
(b (car y))
(reverse (cdr-safe (car props)))
(cmp (if (stringp a)
(if reverse (not (string< a b)) (string< a b))
(if reverse (> a b) (< a b)))))
(when (and (not cmp) (equal a b) (> (length props) 1))
(setq cmp (mentor-cmp-properties (cdr x) (cdr y) (cdr props))))
cmp))
(defun mentor-sort (&optional property reverse append)
"Sort the mentor torrent buffer.
Defaults to sorting according to `mentor-sort-list'.
PROPERTY gives according to which property the torrents should be
sorted.
If REVERSE is non-nil, the result of the sort is reversed.
When APPEND is non-nil, instead of sorting directly, add the
result to the end of `mentor-sort-list'. This makes it possible
to sort according to several properties."
(when property
(let ((elem (cons property reverse)))
(if append
(add-to-list 'mentor-sort-list elem t)
(setq mentor-sort-list (list elem)))))
(mentor-do-sort))
(defun mentor-sort-by-directory (append)
"Sort downloads by directory.
With prefix argument APPEND, append this to the list of existing
sort criteria."
(interactive "P")