mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathproced.el
2326 lines (2100 loc) · 96.6 KB
/
proced.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
;;; proced.el --- operate on system processes like dired -*- lexical-binding:t -*-
;; Copyright (C) 2008-2025 Free Software Foundation, Inc.
;; Author: Roland Winkler <winkler@gnu.org>
;; Keywords: Processes, Unix
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Proced makes an Emacs buffer containing a listing of the current
;; system processes. You can use the normal Emacs commands to move around
;; in this buffer, and special Proced commands to operate on the processes
;; listed. See `proced-mode' for getting started.
;;
;; To do:
;; - Interactive temporary customizability of flags in `proced-grammar-alist'
;;
;; Thoughts and Ideas
;; - Currently, `process-attributes' returns the list of
;; command-line arguments of a process as one concatenated string.
;; This format is compatible with `shell-command'. Also, under
;; MS-Windows, the command-line arguments are actually stored as a
;; single string, so that it is impossible to reverse-engineer it back
;; into separate arguments. Alternatively, `process-attributes'
;; could (try to) return a list of strings that correspond to individual
;; command-line arguments. Then one could feed such a list of
;; command-line arguments into `call-process' or `start-process'.
;; Are there real-world applications when such a feature would be useful?
;; What about something like `proced-restart-pid'?
;;; Code:
(defgroup proced nil
"Proced mode."
:group 'processes
:group 'unix
:prefix "proced-")
(defcustom proced-show-remote-processes nil
"Whether processes of the remote host shall be shown.
This happens only when `default-directory' is remote."
:version "29.1"
:type 'boolean)
(defcustom proced-signal-function #'signal-process
"Name of signal function.
It can be an elisp function (usually `signal-process') or a string specifying
the external command (usually \"kill\")."
:type '(choice (function :tag "function")
(string :tag "command")))
(make-obsolete-variable 'proced-signal-function "no longer used." "29.1")
(defcustom proced-renice-command "renice"
"Name of renice command."
:version "24.3"
:type '(string :tag "command"))
(defcustom proced-signal-list
'( ;; signals supported on all POSIX compliant systems
("HUP" . " (1. Hangup)")
("INT" . " (2. Terminal interrupt)")
("QUIT" . " (3. Terminal quit)")
("ABRT" . " (6. Process abort)")
("KILL" . " (9. Kill - cannot be caught or ignored)")
("ALRM" . " (14. Alarm Clock)")
("TERM" . " (15. Termination)")
;; signals supported on systems conforming to POSIX 1003.1-2001
;; according to (info "(coreutils) Signal specifications")
("BUS" . " (Access to an undefined portion of a memory object)")
("CHLD" . " (Child process terminated, stopped, or continued)")
("CONT" . " (Continue executing, if stopped)")
("FPE" . " (Erroneous arithmetic operation)")
("ILL" . " (Illegal Instruction)")
("PIPE" . " (Write on a pipe with no one to read it)")
("SEGV" . " (Invalid memory reference)")
("STOP" . " (Stop executing / pause - cannot be caught or ignored)")
("TSTP" . " (Terminal stop / pause)")
("TTIN" . " (Background process attempting read)")
("TTOU" . " (Background process attempting write)")
("URG" . " (High bandwidth data is available at a socket)")
("USR1" . " (User-defined signal 1)")
("USR2" . " (User-defined signal 2)"))
"List of signals, used for minibuffer completion."
:type '(repeat (cons (string :tag "signal name")
(string :tag "description"))))
;; For which attributes can we use a fixed width of the output field?
;; A fixed width speeds up formatting, yet it can make
;; `proced-grammar-alist' system-dependent.
;; (If proced runs like top(1) we want it to be fast.)
;;
;; If it is impossible / unlikely that an attribute has the same value
;; for two processes, then sorting can be based on one ordinary (fast)
;; predicate like `<'. Otherwise, a list of proced predicates can be used
;; to refine the sort.
;;
;; It would be neat if one could temporarily override the following
;; predefined rules.
(defcustom proced-grammar-alist
'( ;; attributes defined in `process-attributes'
(euid "EUID" "%d" right proced-< nil (euid pid) (nil t nil))
(user "User" proced-format-user left proced-string-lessp nil
(user pid) (nil t nil))
(egid "EGID" "%d" right proced-< nil (egid euid pid) (nil t nil))
(group "Group" nil left proced-string-lessp nil (group user pid)
(nil t nil))
(comm "Command" nil left proced-string-lessp nil (comm pid) (nil t nil))
(state "Stat" proced-format-state left proced-string-lessp nil
(state pid) (nil t nil))
(ppid "PPID" proced-format-ppid right proced-< nil (ppid pid)
((lambda (ppid)
(proced-filter-parents proced-process-alist ppid))
"refine to process parents"))
(pgrp "PGrp" proced-format-pgrp right proced-< nil (pgrp euid pid)
(nil t nil))
(sess "Sess" proced-format-sess right proced-< nil (sess pid)
(nil t nil))
(ttname "TTY" proced-format-ttname left proced-string-lessp nil
(ttname pid) (nil t nil))
(tpgid "TPGID" "%d" right proced-< nil (tpgid pid) (nil t nil))
(minflt "MinFlt" "%d" right proced-< nil (minflt pid) (nil t t))
(majflt "MajFlt" "%d" right proced-< nil (majflt pid) (nil t t))
(cminflt "CMinFlt" "%d" right proced-< nil (cminflt pid) (nil t t))
(cmajflt "CMajFlt" "%d" right proced-< nil (cmajflt pid) (nil t t))
(utime "UTime" proced-format-time right proced-time-lessp t (utime pid)
(nil t t))
(stime "STime" proced-format-time right proced-time-lessp t (stime pid)
(nil t t))
(time "Time" proced-format-time right proced-time-lessp t (time pid)
(nil t t))
(cutime "CUTime" proced-format-time right proced-time-lessp t (cutime pid)
(nil t t))
(cstime "CSTime" proced-format-time right proced-time-lessp t (cstime pid)
(nil t t))
(ctime "CTime" proced-format-time right proced-time-lessp t (ctime pid)
(nil t t))
(pri "Pr" "%d" right proced-< t (pri pid) (nil t t))
(nice "Ni" "%3d" 3 proced-< t (nice pid) (t t nil))
(thcount "THCount" "%d" right proced-< t (thcount pid) (nil t t))
(start "Start" proced-format-start left proced-time-lessp nil (start pid)
(t t nil))
(vsize "VSize" proced-format-memory right proced-< t (vsize pid)
(nil t t))
(rss "RSS" proced-format-rss right proced-< t (rss pid) (nil t t))
(etime "ETime" proced-format-time right proced-time-lessp t (etime pid)
(nil t t))
(pcpu "%CPU" proced-format-cpu right proced-< t (pcpu pid) (nil t t))
(pmem "%Mem" proced-format-mem right proced-< t (pmem pid) (nil t t))
(args "Args" proced-format-args left proced-string-lessp nil
(args pid) (nil t nil))
;;
;; attributes defined by proced (see `proced-process-attributes')
(pid "PID" proced-format-pid right proced-< nil (pid)
((lambda (ppid) (proced-filter-children proced-process-alist ppid))
"refine to process children"))
;; process tree
(tree "Tree" proced-format-tree left nil nil nil nil))
"Alist of rules for handling Proced attributes.
Each element has the form
(KEY NAME FORMAT JUSTIFY PREDICATE REVERSE SORT-SCHEME REFINER).
Symbol KEY is the car of a process attribute.
String NAME appears in the header line.
FORMAT specifies the format for displaying the attribute values. It can
be a string passed to `format'. It can be a function called with one
argument, the value of the attribute. The value nil means take as is.
If JUSTIFY is an integer, its modulus gives the width of the attribute
values formatted with FORMAT. If JUSTIFY is positive, NAME appears
right-justified, otherwise it appears left-justified. If JUSTIFY is `left'
or `right', the field width is calculated from all field values in the listing.
If JUSTIFY is `left', the field values are formatted left-justified and
right-justified otherwise.
PREDICATE is the predicate for sorting and filtering the process listing
based on attribute KEY. PREDICATE takes two arguments P1 and P2,
the corresponding attribute values of two processes. PREDICATE should
return `equal' if P1 has same rank like P2. Any other non-nil value says
that P1 is \"less than\" P2, or nil if not.
If PREDICATE is nil the attribute cannot be sorted.
PREDICATE defines an ascending sort order. REVERSE is non-nil if the sort
order is descending.
SORT-SCHEME is a list (KEY1 KEY2 ...) defining a hierarchy of rules
for sorting the process listing. KEY1, KEY2, ... are KEYs appearing as cars
of `proced-grammar-alist'. First the PREDICATE of KEY1 is evaluated.
If it yields non-equal, it defines the sort order for the corresponding
processes. If it evaluates to `equal' the PREDICATE of KEY2 is evaluated, etc.
REFINER can be a list of flags (LESS-B EQUAL-B LARGER-B) used by the command
`proced-refine' (see there) to refine the listing based on attribute KEY.
This command compares the value of attribute KEY of every process with
the value of attribute KEY of the process at the position of point
using PREDICATE.
If PREDICATE yields non-nil, the process is accepted if LESS-B is non-nil.
If PREDICATE yields `equal', the process is accepted if EQUAL-B is non-nil.
If PREDICATE yields nil, the process is accepted if LARGER-B is non-nil.
REFINER can also be a list (FUNCTION HELP-ECHO).
FUNCTION is called with one argument, the PID of the process at the position
of point. The function must return a list of PIDs that is used for the refined
listing. HELP-ECHO is a string that is shown when mouse is over this field.
If REFINER is nil no refinement is done."
:type '(repeat (list :tag "Attribute"
(symbol :tag "Key")
(string :tag "Header")
(choice :tag "Format"
(const :tag "None" nil)
(string :tag "Format String")
(function :tag "Formatting Function"))
(choice :tag "Justification"
(const :tag "left" left)
(const :tag "right" right)
(integer :tag "width"))
(choice :tag "Predicate"
(const :tag "None" nil)
(function :tag "Function"))
(boolean :tag "Descending Sort Order")
(repeat :tag "Sort Scheme" (symbol :tag "Key"))
(choice :tag "Refiner"
(const :tag "None" nil)
(list (function :tag "Refinement Function")
(string :tag "Help echo"))
(list :tag "Refine Flags"
(boolean :tag "Less")
(boolean :tag "Equal")
(boolean :tag "Larger"))))))
(defcustom proced-custom-attributes nil
"List of functions defining custom attributes.
This variable extends the functionality of `proced-process-attributes'.
Each function is called with one argument, the list of attributes
of a system process. It returns a cons cell of the form (KEY . VALUE)
like `process-attributes'. This cons cell is appended to the list
returned by `proced-process-attributes'.
If the function returns nil, the value is ignored."
:type '(repeat (function :tag "Attribute")))
;; Formatting and sorting rules are defined "per attribute". If formatting
;; and / or sorting should use more than one attribute, it appears more
;; transparent to define a new derived attribute, so that formatting and
;; sorting can use them consistently. (Are there exceptions to this rule?
;; Would it be advantageous to have yet more general methods available?)
;; Sorting can also be based on attributes that are invisible in the listing.
(defcustom proced-format-alist
'((short user pid tree pcpu pmem start time (args comm))
(medium user pid tree pcpu pmem vsize rss ttname state start time (args comm))
(long user euid group pid tree pri nice pcpu pmem vsize rss ttname state
start time (args comm))
(verbose user euid group egid pid ppid tree pgrp sess pri nice pcpu pmem
state thcount vsize rss ttname tpgid minflt majflt cminflt cmajflt
start time utime stime ctime cutime cstime etime (args comm)))
"Alist of formats of listing.
The car of each element is a symbol, the name of the format.
The cdr is a list of attribute keys appearing in `proced-grammar-alist'.
An element of this list may also be a list of attribute keys that specifies
alternatives. If the first attribute is absent for a process, use the second
one, etc."
:type '(alist :key-type (symbol :tag "Format Name")
:value-type (repeat :tag "Keys"
(choice (symbol :tag "")
(repeat :tag "Alternative Keys"
(symbol :tag ""))))))
(defcustom proced-format 'short
"Current format of Proced listing.
It can be the car of an element of `proced-format-alist'.
It can also be a list of keys appearing in `proced-grammar-alist'."
:type '(choice (symbol :tag "Format Name")
(repeat :tag "Keys" (symbol :tag "")))
:local t)
;; FIXME: is there a better name for filter `user' that does not coincide
;; with an attribute key?
(defcustom proced-filter-alist
`((user (user . proced-user-name))
(user-running (user . proced-user-name)
(state . "\\`[Rr]\\'"))
(all)
(all-running (state . "\\`[Rr]\\'"))
(emacs (fun-all . (lambda (list)
(proced-filter-children list ,(emacs-pid))))))
"Alist of process filters.
The car of each element is a symbol, the name of the filter.
The cdr is a list of elementary filters that are applied to every process.
A process is displayed if it passes all elementary filters of a selected
filter.
An elementary filter can be one of the following:
\(KEY . REGEXP) If value of attribute KEY matches REGEXP,
accept this process.
\(KEY . FUN) Apply function FUN to attribute KEY. Accept this process,
if FUN returns non-nil.
\(function . FUN) For each process, apply function FUN to list of attributes
of each. Accept the process if FUN returns non-nil.
\(fun-all . FUN) Apply function FUN to entire process list.
FUN must return the filtered list."
:type '(repeat (cons :tag "Filter"
(symbol :tag "Filter Name")
(repeat :tag "Filters"
(choice (cons :tag "Key . Regexp" (symbol :tag "Key") regexp)
(cons :tag "Key . Function" (symbol :tag "Key") function)
(cons :tag "Function" (const :tag "Key: function" function) function)
(cons :tag "Fun-all" (const :tag "Key: fun-all" fun-all) function))))))
(defcustom proced-filter 'user
"Current filter of proced listing.
It can be the car of an element of `proced-filter-alist'.
It can also be a list of elementary filters as in the cdrs of the elements
of `proced-filter-alist'."
:type '(choice (symbol :tag "Filter Name")
(repeat :tag "Filters"
(choice (cons :tag "Key . Regexp" (symbol :tag "Key") regexp)
(cons :tag "Key . Function" (symbol :tag "Key") function)
(cons :tag "Function" (const :tag "Key: function" function) function)
(cons :tag "Fun-all" (const :tag "Key: fun-all" fun-all) function))))
:local t)
(defcustom proced-sort 'pcpu
"Current sort scheme for proced listing.
It must be the KEY of an element of `proced-grammar-alist'.
It can also be a list of KEYs as in the SORT-SCHEMEs of the elements
of `proced-grammar-alist'."
:type '(choice (symbol :tag "Sort Scheme")
(repeat :tag "Key List" (symbol :tag "Key")))
:local t)
(defcustom proced-descend t
"Non-nil if proced listing is sorted in descending order."
:type '(boolean :tag "Descending Sort Order")
:local t)
(defcustom proced-goal-attribute 'args
"If non-nil, key of the attribute that defines the `goal-column'."
:type '(choice (const :tag "none" nil)
(symbol :tag "key")))
(defcustom proced-auto-update-interval 5
"Time interval in seconds for auto updating Proced buffers."
:type 'integer)
(defcustom proced-auto-update-flag nil
"Non-nil means auto update proced buffers.
Special value `visible' means only update proced buffers that are currently
displayed in a window. Can be changed interactively via
`proced-toggle-auto-update'."
:type '(radio (const :tag "Don't auto update" nil)
(const :tag "Only update visible proced buffers" visible)
(const :tag "Update all proced buffers" t))
:local t)
(defcustom proced-tree-flag nil
"Non-nil for display of Proced buffer as process tree."
:type 'boolean
:local t)
(defcustom proced-post-display-hook nil
"Normal hook run after displaying or updating a Proced buffer.
May be used to adapt the window size via `fit-window-to-buffer'."
:type 'hook
:options '(fit-window-to-buffer))
(defcustom proced-after-send-signal-hook nil
"Normal hook run after sending a signal to processes by `proced-send-signal'.
May be used to revert the process listing."
:type 'hook
:options '(proced-revert))
(defcustom proced-enable-color-flag nil
"Non-nil means Proced should display some process attributes with color."
:type 'boolean
:version "29.1")
(defcustom proced-low-memory-usage-threshold 0.1
"The upper bound for low relative memory usage display in Proced.
When `proced-enable-color-flag' is non-nil, RSS values denoting a
proportion of memory, relative to total memory, that is lower
than this value will be displayed using the `proced-memory-low-usage' face."
:type 'float
:version "29.1")
(defcustom proced-medium-memory-usage-threshold 0.5
"The upper bound for medium relative memory usage display in Proced.
When `proced-enable-color-flag' is non-nil, RSS values denoting a
proportion of memory, relative to total memory, that is less than
this value, but greater than `proced-low-memory-usage-threshold',
will be displayed using the `proced-memory-medium-usage' face.
RSS values denoting a greater proportion than this value will be
displayed using the `proced-memory-high-usage' face."
:type 'float
:version "29.1")
;; Internal variables
(defvar proced-available t;(not (null (list-system-processes)))
"Non-nil means Proced is known to work on this system.")
(defvar-local proced-process-alist nil
"Alist of processes displayed by Proced.
The car of each element is the PID, and the cdr is a list of
cons pairs, see `proced-process-attributes'.")
(defvar proced-sort-internal nil
"Sort scheme for listing (internal format).
It is a list of lists (KEY PREDICATE REVERSE).")
(defvar proced-marker-char ?* ; the answer is 42
"In Proced, the current mark character.")
;; Faces and font-lock code taken from dired,
;; but face variables are deprecated for new code.
(defgroup proced-faces nil
"Faces used by Proced."
:group 'proced
:group 'faces)
(defface proced-mark
'((t (:inherit font-lock-constant-face)))
"Face used for Proced marks.")
(defface proced-marked
'((t (:inherit error)))
"Face used for marked processes.")
(defface proced-sort-header
'((t (:inherit font-lock-keyword-face)))
"Face used for header of attribute used for sorting.")
(defface proced-run-status-code
'((t (:foreground "green")))
"Face used in Proced buffers for running or runnable status code character \"R\"."
:version "29.1")
(defface proced-interruptible-sleep-status-code
'((((class color) (min-colors 88)) (:foreground "DimGrey"))
(t (:slant italic)))
"Face used in Proced buffers for interruptible sleep status code character \"S\"."
:version "29.1")
(defface proced-uninterruptible-sleep-status-code
'((((class color)) (:foreground "red"))
(t (:weight bold)))
"Face used in Proced buffers for uninterruptible sleep status code character \"D\"."
:version "29.1")
(defface proced-executable
'((((class color) (min-colors 88) (background dark)) (:foreground "DeepSkyBlue"))
(((class color) (background dark)) (:foreground "cyan"))
(((class color) (background light)) (:foreground "blue"))
(t (:weight bold)))
"Face used in Proced buffers for executable names.
The first word in the process arguments attribute is assumed to
be the executable that runs in the process."
:version "29.1")
(defface proced-memory-high-usage
'((((class color) (min-colors 88) (background dark)) (:foreground "orange"))
(((class color) (min-colors 88) (background light)) (:foreground "OrangeRed"))
(((class color)) (:foreground "red"))
(t (:underline t)))
"Face used in Proced buffers for high memory usage."
:version "29.1")
(defface proced-memory-medium-usage
'((((class color) (min-colors 88) (background dark)) (:foreground "yellow3"))
(((class color) (min-colors 88) (background light)) (:foreground "orange"))
(((class color)) (:foreground "yellow")))
"Face used in Proced buffers for medium memory usage."
:version "29.1")
(defface proced-memory-low-usage
'((((class color) (min-colors 88) (background dark)) (:foreground "#8bcd50"))
(((class color)) (:foreground "green")))
"Face used in Proced buffers for low memory usage."
:version "29.1")
(defface proced-emacs-pid
'((((class color) (min-colors 88)) (:foreground "purple"))
(((class color)) (:foreground "magenta")))
"Face used in Proced buffers for the process ID of the current Emacs process."
:version "29.1")
(defface proced-pid
'((((class color) (min-colors 88)) (:foreground "#5085ef"))
(((class color)) (:foreground "blue")))
"Face used in Proced buffers for process IDs."
:version "29.1")
(defface proced-session-leader-pid
'((((class color) (min-colors 88)) (:foreground "#5085ef" :underline t))
(((class color)) (:foreground "blue" :underline t))
(t (:underline t)))
"Face used in Proced buffers for process IDs which are session leaders."
:version "29.1")
(defface proced-ppid
'((((class color) (min-colors 88)) (:foreground "#5085bf"))
(((class color)) (:foreground "blue")))
"Face used in Proced buffers for parent process IDs."
:version "29.1")
(defface proced-pgrp
'((((class color) (min-colors 88)) (:foreground "#4785bf"))
(((class color)) (:foreground "blue")))
"Face used in Proced buffers for process group IDs."
:version "29.1")
(defface proced-sess
'((((class color) (min-colors 88)) (:foreground "#41729f"))
(((class color)) (:foreground "MidnightBlue")))
"Face used in Proced buffers for process session IDs."
:version "29.1")
(defface proced-cpu
'((((class color) (min-colors 88)) (:foreground "#6d5cc3" :weight bold))
(t (:weight bold)))
"Face used in Proced buffers for process CPU utilization."
:version "29.1")
(defface proced-mem
'((((class color) (min-colors 88))
(:foreground "#6d5cc3")))
"Face used in Proced buffers for process memory utilization."
:version "29.1")
(defface proced-user
'((t (:weight bold)))
"Face used in Proced buffers for the user owning the process."
:version "29.1")
(defface proced-time-colon
'((((class color) (min-colors 88)) (:foreground "DarkMagenta"))
(t (:weight bold)))
"Face used in Proced buffers for the colon in time strings."
:version "29.1")
(defvar proced-re-mark "^[^ \n]"
"Regexp matching a marked line.
Important: the match ends just after the marker.")
(defvar-local proced-header-line nil
"Headers in Proced buffer as a string.")
(defvar proced-temp-alist nil
"Temporary alist (internal variable).")
(defvar proced-process-tree nil
"Proced process tree (internal variable).")
(defvar proced-tree-depth nil
"Internal variable for depth of Proced process tree.")
(defvar proced-auto-update-timer nil
"Stores if Proced auto update timer is already installed.")
(defvar proced-log-buffer "*Proced log*"
"Name of Proced Log buffer.")
(defconst proced-help-string
(concat "\\<proced-mode-map> "
"\\[next-line] next, "
"\\[previous-line] previous, "
"\\[proced-mark] mark, "
"\\[proced-unmark] unmark, "
"\\[proced-send-signal] kill, "
"\\[quit-window] quit "
"(type \\[proced-help] for more help)")
"Help string for Proced.")
(defconst proced-header-help-echo
"mouse-1, mouse-2: sort by attribute %s%s (%s)"
"Help string shown when mouse is over a sortable header.")
(defconst proced-field-help-echo
"mouse-2, RET: refine by attribute %s %s"
"Help string shown when mouse is over a refinable field.")
(defvar proced-font-lock-keywords
`(;; (Any) proced marks.
(,proced-re-mark . 'proced-mark)
;; Processes marked with `proced-marker-char'
;; Should we make sure that only certain attributes are font-locked?
(,(concat "^[" (char-to-string proced-marker-char) "]")
".+" (proced-move-to-goal-column) nil (0 'proced-marked))))
(defvar-keymap proced-mode-map
:doc "Keymap for Proced commands."
;; moving
"SPC" #'next-line
"n" #'next-line
"p" #'previous-line
"C-n" #'next-line
"C-p" #'previous-line
"S-SPC" #'previous-line
"<down>" #'next-line
"<up>" #'previous-line
;; marking
"d" #'proced-mark ; Dired compatibility ("delete")
"m" #'proced-mark
"u" #'proced-unmark
"DEL" #'proced-unmark-backward
"M" #'proced-mark-all
"U" #'proced-unmark-all
"t" #'proced-toggle-marks
"C" #'proced-mark-children
"P" #'proced-mark-parents
;; filtering
"f" #'proced-filter-interactive
"<mouse-2>" #'proced-refine
"RET" #'proced-refine
;; sorting
"s c" #'proced-sort-pcpu
"s m" #'proced-sort-pmem
"s p" #'proced-sort-pid
"s s" #'proced-sort-start
"s S" #'proced-sort-interactive
"s t" #'proced-sort-time
"s u" #'proced-sort-user
;; similar to `Buffer-menu-sort-by-column'
"<header-line> <mouse-1>" #'proced-sort-header
"<header-line> <mouse-2>" #'proced-sort-header
"T" #'proced-toggle-tree
;; formatting
"F" #'proced-format-interactive
;; operate
"o" #'proced-omit-processes
"x" #'proced-send-signal ; Dired compatibility
"k" #'proced-send-signal ; kill processes
"r" #'proced-renice ; renice processes
;; misc
"h" #'describe-mode
"?" #'proced-help
"<remap> <undo>" #'proced-undo
;; Additional keybindings are inherited from `special-mode-map'
)
(put 'proced-mark :advertised-binding "m")
(defvar-local proced-refinements nil
"Information about the current buffer refinements.
It should be a list of elements of the form (REFINER PID KEY GRAMMAR), where
REFINER and GRAMMAR are as described in `proced-grammar-alist', PID is the
process ID of the process used to create the refinement, and KEY the attribute
of the process. A value of nil indicates that there are no active refinements.")
(easy-menu-define proced-menu proced-mode-map
"Proced Menu."
`("Proced"
["Mark" proced-mark
:help "Mark Current Process"]
["Unmark" proced-unmark
:help "Unmark Current Process"]
["Mark All" proced-mark-all
:help "Mark All Processes"]
["Unmark All" proced-unmark-all
:help "Unmark All Process"]
["Toggle Marks" proced-toggle-marks
:help "Marked Processes Become Unmarked, and Vice Versa"]
["Mark Children" proced-mark-children
:help "Mark Current Process and its Children"]
["Mark Parents" proced-mark-parents
:help "Mark Current Process and its Parents"]
"--"
("Filters"
:help "Select Filter for Process Listing"
,@(mapcar (lambda (el)
(let ((filter (car el)))
`[,(symbol-name filter)
(proced-filter-interactive ',filter)
:style radio
:selected (eq proced-filter ',filter)]))
proced-filter-alist))
("Sorting"
:help "Select Sort Scheme"
["Sort..." proced-sort-interactive
:help "Sort Process List"]
"--"
["Sort by %CPU" proced-sort-pcpu]
["Sort by %MEM" proced-sort-pmem]
["Sort by PID" proced-sort-pid]
["Sort by START" proced-sort-start]
["Sort by TIME" proced-sort-time]
["Sort by USER" proced-sort-user])
("Formats"
:help "Select Format for Process Listing"
,@(mapcar (lambda (el)
(let ((format (car el)))
`[,(symbol-name format)
(proced-format-interactive ',format)
:style radio
:selected (eq proced-format ',format)]))
proced-format-alist))
["Tree Display" proced-toggle-tree
:style toggle
:selected (eval proced-tree-flag)
:help "Display Proced Buffer as Process Tree"]
"--"
["Omit Marked Processes" proced-omit-processes
:help "Omit Marked Processes in Process Listing."]
"--"
["Revert" revert-buffer
:help "Revert Process Listing"]
["Auto Update" proced-toggle-auto-update
:style toggle
:selected (eval proced-auto-update-flag)
:help "Auto Update of Proced Buffer"]
"--"
["Send signal" proced-send-signal
:help "Send Signal to Marked Processes"]
["Renice" proced-renice
:help "Renice Marked Processes"]))
;; helper functions
(defun proced-user-name (user)
"Check the `user' attribute with user name `proced' is running for."
(string-equal user (if (file-remote-p default-directory)
(file-remote-p default-directory 'user)
(user-real-login-name))))
(defun proced-marker-regexp ()
"Return regexp matching `proced-marker-char'."
;; `proced-marker-char' must appear in column zero
(concat "^" (regexp-quote (char-to-string proced-marker-char))))
(defun proced-success-message (action count)
"Display success message for ACTION performed for COUNT processes."
(message "%s %s process%s" action count (if (= 1 count) "" "es")))
;; Unlike dired, we do not define our own commands for vertical motion.
;; If `goal-column' is set, `next-line' and `previous-line' are fancy
;; commands to satisfy our modest needs. If `proced-goal-attribute'
;; and/or `goal-column' are not set, `next-line' and `previous-line'
;; are really what we need to preserve the column of point.
;; We use `proced-move-to-goal-column' for "non-interactive" cases only
;; to get a well-defined position of point.
(defun proced-move-to-goal-column ()
"Move to `goal-column' if non-nil. Return position of point."
(beginning-of-line)
(unless (eobp)
(if goal-column
(forward-char goal-column)
(forward-char 2)))
(point))
(defun proced-header-line ()
"Return header line for Proced buffer."
(let ((base (line-number-display-width 'columns))
(hl (if (<= (window-hscroll) (length proced-header-line))
(substring proced-header-line (window-hscroll)))))
(when hl
;; From buff-menu.el: Turn whitespace chars in the header into
;; stretch specs so they work regardless of the header-line face.
(let ((pos 0))
(while (string-match "[ \t\n]+" hl pos)
(setq pos (match-end 0))
(put-text-property (match-beginning 0) pos 'display
`(space :align-to (,(+ pos base) . width))
hl)))
(setq hl (replace-regexp-in-string ;; preserve text properties
"\\(%\\)" "\\1\\1"
hl)))
(list (propertize " " 'display `(space :align-to (,base . width)))
hl)))
(defun proced-pid-at-point ()
"Return pid of system process at point.
Return nil if point is not on a process line."
(save-excursion
(beginning-of-line)
(if (looking-at "^. .")
(get-text-property (match-end 0) 'proced-pid))))
(defun proced--position-info (pos)
"Return information of the process at POS.
The returned information will have the form `(PID KEY COLUMN)' where
PID is the process ID of the process at point, KEY is the value of the
proced-key text property at point, and COLUMN is the column for which the
current value of the proced-key text property starts, or 0 if KEY is nil."
;; If point is on a field, we try to return point to that field.
;; Otherwise we try to return to the same column
(save-excursion
(goto-char pos)
(let ((pid (proced-pid-at-point))
(key (get-text-property (point) 'proced-key)))
(list pid key ; can both be nil
(if key
(if (get-text-property (1- (point)) 'proced-key)
(- (point) (previous-single-property-change
(point) 'proced-key))
0)
(current-column))))))
(defun proced--determine-pos (key column)
"Return position of point in the current line using KEY and COLUMN.
Attempt to find the first position on the current line where the
text property proced-key is equal to KEY. If this is not possible, return
the position of point of column COLUMN on the current line."
(save-excursion
(let (new-pos)
(if key
(let ((limit (line-end-position)) pos)
(while (and (not new-pos)
(setq pos (next-property-change (point) nil limit)))
(goto-char pos)
(when (eq key (get-text-property (point) 'proced-key))
(forward-char (min column (- (next-property-change (point))
(point))))
(setq new-pos (point))))
(unless new-pos
;; we found the process, but the field of point
;; is not listed anymore
(setq new-pos (proced-move-to-goal-column))))
(setq new-pos (min (+ (line-beginning-position) column)
(line-end-position))))
new-pos)))
;; proced mode
(define-derived-mode proced-mode special-mode "Proced"
"Mode for displaying system processes and sending signals to them.
Type \\[proced] to start a Proced session. In a Proced buffer
type \\<proced-mode-map>\\[proced-mark] to mark a process for later commands.
Type \\[proced-send-signal] to send signals to marked processes.
Type \\[proced-renice] to renice marked processes.
The initial content of a listing is defined by the variable
`proced-filter' and the variable `proced-format'.
The variable `proced-filter' specifies which system processes are
displayed.
The variable `proced-format' specifies which attributes are
displayed for each process.
Type \\[proced-filter-interactive] and \\[proced-format-interactive] to \
change the values of `proced-filter' and
`proced-format'. The current value of the variable
`proced-filter' is indicated in the mode line.
The sort order of Proced listings is defined by the variable `proced-sort'.
Type \\[proced-sort-interactive] or click on a header in the header \
line to change the sort scheme.
The current sort scheme is indicated in the mode line, using
\"+\" or \"-\" for ascending or descending sort order.
Type \\[proced-toggle-tree] to toggle whether the listing is displayed as process tree.
Type \\[proced-toggle-auto-update] to automatically update the
process list. The time interval for updates can be configured
via `proced-auto-update-interval'.
An existing Proced listing can be refined by typing \\[proced-refine].
Refining an existing listing does not update the variable `proced-filter'.
The attribute-specific rules for formatting, filtering, sorting,
and refining are defined in `proced-grammar-alist'.
After displaying or updating a Proced buffer, Proced runs the
normal hook `proced-post-display-hook'.
\\{proced-mode-map}"
:interactive nil
(abbrev-mode 0)
(auto-fill-mode 0)
(setq buffer-read-only t
truncate-lines t
header-line-format '(:eval (proced-header-line)))
(add-hook 'post-command-hook #'force-mode-line-update nil t) ;; FIXME: Why?
(setq-local revert-buffer-function #'proced-revert)
(setq-local font-lock-defaults
'(proced-font-lock-keywords t nil nil beginning-of-line))
(setq-local switch-to-buffer-preserve-window-point nil)
;; So that the heading scales together with the body of the table.
(setq-local text-scale-remap-header-line t)
(if (and (not proced-auto-update-timer) proced-auto-update-interval)
(setq proced-auto-update-timer
(run-at-time t proced-auto-update-interval
'proced-auto-update-timer))))
;;;###autoload
(defun proced (&optional arg)
"Generate a listing of UNIX system processes.
\\<proced-mode-map>
If invoked with optional non-negative ARG, do not select the
window displaying the process information.
If `proced-show-remote-processes' is non-nil or the command is
invoked with a negative ARG `\\[universal-argument] \\[negative-argument]', \
and `default-directory'
points to a remote host, the system processes of that host are shown.
This function runs the normal hook `proced-post-display-hook'.
See `proced-mode' for a description of features available in
Proced buffers."
(interactive "P")
(unless proced-available
(error "Proced is not available on this system"))
(let ((buffer (get-buffer-create "*Proced*")) new)
(set-buffer buffer)
(when (and (file-remote-p default-directory)
(not
(or proced-show-remote-processes
(eq arg '-))))
(setq default-directory temporary-file-directory))
(setq new (zerop (buffer-size)))
(when new
(proced-mode)
;; `proced-update' runs `proced-post-display-hook' only if the
;; Proced buffer has been selected. Yet the following call of
;; `proced-update' is for an empty Proced buffer that has not
;; yet been selected. Therefore we need to call
;; `proced-post-display-hook' below.
(proced-update t))
(if arg
(progn
(display-buffer buffer)
(with-current-buffer buffer
(proced-update t)))
(pop-to-buffer buffer)
(proced-update t)
(message
(substitute-command-keys
"Type \\<proced-mode-map>\\[quit-window] to quit, \\[proced-help] for help")))))
(defun proced-auto-update-timer ()
"Auto-update Proced buffers using `run-at-time'.
If there are no proced buffers, cancel the timer."
(if-let* ((buffers (match-buffers '(derived-mode . proced-mode))))
(dolist (buf buffers)
(when-let* ((flag (buffer-local-value 'proced-auto-update-flag buf))
((or (not (eq flag 'visible))
(get-buffer-window buf 'visible))))
(with-current-buffer buf
(proced-update t t))))
(cancel-timer proced-auto-update-timer)
(setq proced-auto-update-timer nil)))
(defun proced-toggle-auto-update (arg)
"Change whether this Proced buffer is updated automatically.
With prefix ARG, update this buffer automatically if ARG is positive,
update the buffer only when the buffer is displayed in a window if ARG is 0,
otherwise do not update. Sets the variable `proced-auto-update-flag' by
cycling between nil, `visible' and t. The time interval for updates is
specified via `proced-auto-update-interval'."
(interactive (list (or current-prefix-arg 'toggle)) proced-mode)
(setq proced-auto-update-flag
(cond ((eq arg 'toggle)
(cond ((not proced-auto-update-flag) 'visible)
((eq proced-auto-update-flag 'visible) t)
(t nil)))
(arg
(setq arg (prefix-numeric-value arg))
(message "%s" arg)
(cond ((> arg 0) t)
((eq arg 0) 'visible)
(t nil)))
(t (not proced-auto-update-flag))))
(message "Proced auto update %s"
(cond ((eq proced-auto-update-flag 'visible) "enabled (only when buffer is visible)")
(proced-auto-update-flag "enabled (unconditionally)")
(t "disabled"))))
;;; Mark
(defun proced-mark (&optional count)
"Mark the current (or next COUNT) processes."
(interactive "p" proced-mode)
(proced-do-mark t count))
(defun proced-unmark (&optional count)
"Unmark the current (or next COUNT) processes."