1 ;;; proced.el --- operate on system processes like dired
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Roland Winkler <winkler@gnu.org>
6 ;; Keywords: Processes, Unix
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Proced makes an Emacs buffer containing a listing of the current
26 ;; system processes. You can use the normal Emacs commands to move around
27 ;; in this buffer, and special Proced commands to operate on the processes
28 ;; listed. See `proced-mode' for getting started.
31 ;; - interactive temporary customizability of flags in `proced-grammar-alist'
32 ;; - allow "sudo kill PID", "renice PID"
35 ;; - Currently, `process-attributes' returns the list of
36 ;; command-line arguments of a process as one concatenated string.
37 ;; This format is compatible with `shell-command'. Also, under
38 ;; MS-Windows, the command-line arguments are actually stored as a
39 ;; single string, so that it is impossible to reverse-engineer it back
40 ;; into separate arguments. Alternatively, `process-attributes'
41 ;; could (try to) return a list of strings that correspond to individual
42 ;; command-line arguments. Then one could feed such a list of
43 ;; command-line arguments into `call-process' or `start-process'.
44 ;; Are there real-world applications when such a feature would be useful?
45 ;; What about something like `proced-restart-pid'?
49 (require 'time-date
) ; for `with-decoded-time-value'
57 (defcustom proced-signal-function
'signal-process
58 "Name of signal function.
59 It can be an elisp function (usually `signal-process') or a string specifying
60 the external command (usually \"kill\")."
62 :type
'(choice (function :tag
"function")
63 (string :tag
"command")))
65 (defcustom proced-signal-list
66 '( ;; signals supported on all POSIX compliant systems
67 ("HUP" .
" (1. Hangup)")
68 ("INT" .
" (2. Terminal interrupt)")
69 ("QUIT" .
" (3. Terminal quit)")
70 ("ABRT" .
" (6. Process abort)")
71 ("KILL" .
" (9. Kill - cannot be caught or ignored)")
72 ("ALRM" .
" (14. Alarm Clock)")
73 ("TERM" .
" (15. Termination)")
75 ;; Which systems do not support these signals so that we can
76 ;; exclude them from `proced-signal-list'?
77 ("CONT" .
" (Continue executing)")
78 ("STOP" .
" (Stop executing / pause - cannot be caught or ignored)")
79 ("TSTP" .
" (Terminal stop / pause)"))
80 "List of signals, used for minibuffer completion."
82 :type
'(repeat (cons (string :tag
"signal name")
83 (string :tag
"description"))))
85 ;; For which attributes can we use a fixed width of the output field?
86 ;; A fixed width speeds up formatting, yet it can make
87 ;; `proced-grammar-alist' system-dependent.
88 ;; (If proced runs like top(1) we want it to be fast.)
90 ;; If it is impossible / unlikely that an attribute has the same value
91 ;; for two processes, then sorting can be based on one ordinary (fast)
92 ;; predicate like `<'. Otherwise, a list of proced predicates can be used
93 ;; to refine the sort.
95 ;; It would be neat if one could temporarily override the following
97 (defcustom proced-grammar-alist
98 '( ;; attributes defined in `process-attributes'
99 (euid "EUID" "%d" right proced-
< nil
(euid pid
) (nil t nil
))
100 (user "User" nil left proced-string-lessp nil
(user pid
) (nil t nil
))
101 (egid "EGID" "%d" right proced-
< nil
(egid euid pid
) (nil t nil
))
102 (group "Group" nil left proced-string-lessp nil
(group user pid
) (nil t nil
))
103 (comm "Command" nil left proced-string-lessp nil
(comm pid
) (nil t nil
))
104 (state "Stat" nil left proced-string-lessp nil
(state pid
) (nil t nil
))
105 (ppid "PPID" "%d" right proced-
< nil
(ppid pid
)
106 ((lambda (ppid) (proced-filter-parents proced-process-alist ppid
))
107 "refine to process parents"))
108 (pgrp "PGrp" "%d" right proced-
< nil
(pgrp euid pid
) (nil t nil
))
109 (sess "Sess" "%d" right proced-
< nil
(sess pid
) (nil t nil
))
110 (ttname "TTY" proced-format-ttname left proced-string-lessp nil
(ttname pid
) (nil t nil
))
111 (tpgid "TPGID" "%d" right proced-
< nil
(tpgid pid
) (nil t nil
))
112 (minflt "MinFlt" "%d" right proced-
< nil
(minflt pid
) (nil t t
))
113 (majflt "MajFlt" "%d" right proced-
< nil
(majflt pid
) (nil t t
))
114 (cminflt "CMinFlt" "%d" right proced-
< nil
(cminflt pid
) (nil t t
))
115 (cmajflt "CMajFlt" "%d" right proced-
< nil
(cmajflt pid
) (nil t t
))
116 (utime "UTime" proced-format-time right proced-time-lessp t
(utime pid
) (nil t t
))
117 (stime "STime" proced-format-time right proced-time-lessp t
(stime pid
) (nil t t
))
118 (time "Time" proced-format-time right proced-time-lessp t
(time pid
) (nil t t
))
119 (cutime "CUTime" proced-format-time right proced-time-lessp t
(cutime pid
) (nil t t
))
120 (cstime "CSTime" proced-format-time right proced-time-lessp t
(cstime pid
) (nil t t
))
121 (ctime "CTime" proced-format-time right proced-time-lessp t
(ctime pid
) (nil t t
))
122 (pri "Pr" "%d" right proced-
< t
(pri pid
) (nil t t
))
123 (nice "Ni" "%3d" 3 proced-
< t
(nice pid
) (t t nil
))
124 (thcount "THCount" "%d" right proced-
< t
(thcount pid
) (nil t t
))
125 (start "Start" proced-format-start
6 proced-time-lessp nil
(start pid
) (t t nil
))
126 (vsize "VSize" "%d" right proced-
< t
(vsize pid
) (nil t t
))
127 (rss "RSS" "%d" right proced-
< t
(rss pid
) (nil t t
))
128 (etime "ETime" proced-format-time right proced-time-lessp t
(etime pid
) (nil t t
))
129 (pcpu "%CPU" "%.1f" right proced-
< t
(pcpu pid
) (nil t t
))
130 (pmem "%Mem" "%.1f" right proced-
< t
(pmem pid
) (nil t t
))
131 (args "Args" proced-format-args left proced-string-lessp nil
(args pid
) (nil t nil
))
133 ;; attributes defined by proced (see `proced-process-attributes')
134 (pid "PID" "%d" right proced-
< nil
(pid)
135 ((lambda (ppid) (proced-filter-children proced-process-alist ppid
))
136 "refine to process children"))
138 (tree "Tree" proced-format-tree left nil nil nil nil
))
139 "Alist of rules for handling Proced attributes.
141 Each element has the form
143 (KEY NAME FORMAT JUSTIFY PREDICATE REVERSE SORT-SCHEME REFINER).
145 Symbol KEY is the car of a process attribute.
147 String NAME appears in the header line.
149 FORMAT specifies the format for displaying the attribute values. It can
150 be a string passed to `format'. It can be a function called with one
151 argument, the value of the attribute. The value nil means take as is.
153 If JUSTIFY is an integer, its modulus gives the width of the attribute
154 values formatted with FORMAT. If JUSTIFY is positive, NAME appears
155 right-justified, otherwise it appears left-justified. If JUSTIFY is 'left
156 or 'right, the field width is calculated from all field values in the listing.
157 If JUSTIFY is 'left, the field values are formatted left-justified and
158 right-justified otherwise.
160 PREDICATE is the predicate for sorting and filtering the process listing
161 based on attribute KEY. PREDICATE takes two arguments P1 and P2,
162 the corresponding attribute values of two processes. PREDICATE should
163 return 'equal if P1 has same rank like P2. Any other non-nil value says
164 that P1 is \"less than\" P2, or nil if not.
165 If PREDICATE is nil the attribute cannot be sorted.
167 PREDICATE defines an ascending sort order. REVERSE is non-nil if the sort
170 SORT-SCHEME is a list (KEY1 KEY2 ...) defining a hierarchy of rules
171 for sorting the process listing. KEY1, KEY2, ... are KEYs appearing as cars
172 of `proced-grammar-alist'. First the PREDICATE of KEY1 is evaluated.
173 If it yields non-equal, it defines the sort order for the corresponding
174 processes. If it evaluates to 'equal the PREDICATE of KEY2 is evaluated, etc.
176 REFINER can be a list of flags (LESS-B EQUAL-B LARGER-B) used by the command
177 `proced-refine' (see there) to refine the listing based on attribute KEY.
178 This command compares the value of attribute KEY of every process with
179 the value of attribute KEY of the process at the position of point
181 If PREDICATE yields non-nil, the process is accepted if LESS-B is non-nil.
182 If PREDICATE yields 'equal, the process is accepted if EQUAL-B is non-nil.
183 If PREDICATE yields nil, the process is accepted if LARGER-B is non-nil.
185 REFINER can also be a list (FUNCTION HELP-ECHO).
186 FUNCTION is called with one argument, the PID of the process at the position
187 of point. The function must return a list of PIDs that is used for the refined
188 listing. HELP-ECHO is a string that is shown when mouse is over this field.
190 If REFINER is nil no refinement is done."
192 :type
'(repeat (list :tag
"Attribute"
194 (string :tag
"Header")
195 (choice :tag
"Format"
196 (const :tag
"None" nil
)
197 (string :tag
"Format String")
198 (function :tag
"Formatting Function"))
199 (choice :tag
"Justification"
200 (const :tag
"left" left
)
201 (const :tag
"right" right
)
202 (integer :tag
"width"))
203 (choice :tag
"Predicate"
204 (const :tag
"None" nil
)
205 (function :tag
"Function"))
206 (boolean :tag
"Descending Sort Order")
207 (repeat :tag
"Sort Scheme" (symbol :tag
"Key"))
208 (choice :tag
"Refiner"
209 (const :tag
"None" nil
)
210 (list (function :tag
"Refinement Function")
211 (string :tag
"Help echo"))
212 (list :tag
"Refine Flags"
213 (boolean :tag
"Less")
214 (boolean :tag
"Equal")
215 (boolean :tag
"Larger"))))))
217 (defcustom proced-custom-attributes nil
218 "List of functions defining custom attributes.
219 This variable extends the functionality of `proced-process-attributes'.
220 Each function is called with one argument, the list of attributes
221 of a system process. It returns a cons cell of the form (KEY . VALUE)
222 like `process-attributes'. This cons cell is appended to the list
223 returned by `proced-process-attributes'.
224 If the function returns nil, the value is ignored."
226 :type
'(repeat (function :tag
"Attribute")))
228 ;; Formatting and sorting rules are defined "per attribute". If formatting
229 ;; and / or sorting should use more than one attribute, it appears more
230 ;; transparent to define a new derived attribute, so that formatting and
231 ;; sorting can use them consistently. (Are there exceptions to this rule?
232 ;; Would it be advantageous to have yet more general methods available?)
233 ;; Sorting can also be based on attributes that are invisible in the listing.
235 (defcustom proced-format-alist
236 '((short user pid tree pcpu pmem start time
(args comm
))
237 (medium user pid tree pcpu pmem vsize rss ttname state start time
(args comm
))
238 (long user euid group pid tree pri nice pcpu pmem vsize rss ttname state
239 start time
(args comm
))
240 (verbose user euid group egid pid ppid tree pgrp sess pri nice pcpu pmem
241 state thcount vsize rss ttname tpgid minflt majflt cminflt cmajflt
242 start time utime stime ctime cutime cstime etime
(args comm
)))
243 "Alist of formats of listing.
244 The car of each element is a symbol, the name of the format.
245 The cdr is a list of attribute keys appearing in `proced-grammar-alist'.
246 An element of this list may also be a list of attribute keys that specifies
247 alternatives. If the first attribute is absent for a process, use the second
250 :type
'(alist :key-type
(symbol :tag
"Format Name")
251 :value-type
(repeat :tag
"Keys"
252 (choice (symbol :tag
"")
253 (repeat :tag
"Alternative Keys"
254 (symbol :tag
""))))))
256 (defcustom proced-format
'short
257 "Current format of Proced listing.
258 It can be the car of an element of `proced-format-alist'.
259 It can also be a list of keys appearing in `proced-grammar-alist'."
261 :type
'(choice (symbol :tag
"Format Name")
262 (repeat :tag
"Keys" (symbol :tag
""))))
263 (make-variable-buffer-local 'proced-format
)
265 ;; FIXME: is there a better name for filter `user' that does not coincide
266 ;; with an attribute key?
267 (defcustom proced-filter-alist
268 `((user (user .
,(concat "\\`" (regexp-quote (user-real-login-name)) "\\'")))
269 (user-running (user .
,(concat "\\`" (regexp-quote (user-real-login-name)) "\\'"))
270 (state .
"\\`[Rr]\\'"))
272 (all-running (state .
"\\`[Rr]\\'"))
273 (emacs (fun-all .
(lambda (list)
274 (proced-filter-children list
,(emacs-pid))))))
275 "Alist of process filters.
276 The car of each element is a symbol, the name of the filter.
277 The cdr is a list of elementary filters that are applied to every process.
278 A process is displayed if it passes all elementary filters of a selected
281 An elementary filter can be one of the following:
282 \(KEY . REGEXP) If value of attribute KEY matches REGEXP,
284 \(KEY . FUN) Apply function FUN to attribute KEY. Accept this process,
285 if FUN returns non-nil.
286 \(function . FUN) For each process, apply function FUN to list of attributes
287 of each. Accept the process if FUN returns non-nil.
288 \(fun-all . FUN) Apply function FUN to entire process list.
289 FUN must return the filtered list."
291 :type
'(repeat (cons :tag
"Filter"
292 (symbol :tag
"Filter Name")
293 (repeat :tag
"Filters"
294 (choice (cons :tag
"Key . Regexp" (symbol :tag
"Key") regexp
)
295 (cons :tag
"Key . Function" (symbol :tag
"Key") function
)
296 (cons :tag
"Function" (const :tag
"Key: function" function
) function
)
297 (cons :tag
"Fun-all" (const :tag
"Key: fun-all" fun-all
) function
))))))
299 (defcustom proced-filter
'user
300 "Current filter of proced listing.
301 It can be the car of an element of `proced-filter-alist'.
302 It can also be a list of elementary filters as in the cdrs of the elements
303 of `proced-filter-alist'."
305 :type
'(choice (symbol :tag
"Filter Name")
306 (repeat :tag
"Filters"
307 (choice (cons :tag
"Key . Regexp" (symbol :tag
"Key") regexp
)
308 (cons :tag
"Key . Function" (symbol :tag
"Key") function
)
309 (cons :tag
"Function" (const :tag
"Key: function" function
) function
)
310 (cons :tag
"Fun-all" (const :tag
"Key: fun-all" fun-all
) function
)))))
311 (make-variable-buffer-local 'proced-filter
)
313 (defcustom proced-sort
'pcpu
314 "Current sort scheme for proced listing.
315 It must be the KEY of an element of `proced-grammar-alist'.
316 It can also be a list of KEYs as in the SORT-SCHEMEs of the elements
317 of `proced-grammar-alist'."
319 :type
'(choice (symbol :tag
"Sort Scheme")
320 (repeat :tag
"Key List" (symbol :tag
"Key"))))
321 (make-variable-buffer-local 'proced-sort
)
323 (defcustom proced-descend t
324 "Non-nil if proced listing is sorted in descending order."
326 :type
'(boolean :tag
"Descending Sort Order"))
327 (make-variable-buffer-local 'proced-descend
)
329 (defcustom proced-goal-attribute
'args
330 "If non-nil, key of the attribute that defines the `goal-column'."
332 :type
'(choice (const :tag
"none" nil
)
333 (symbol :tag
"key")))
335 (defcustom proced-auto-update-interval
5
336 "Time interval in seconds for auto updating Proced buffers."
340 (defcustom proced-auto-update-flag nil
341 "Non-nil for auto update of a Proced buffer.
342 Can be changed interactively via `proced-toggle-auto-update'."
345 (make-variable-buffer-local 'proced-auto-update-flag
)
347 (defcustom proced-tree-flag nil
348 "Non-nil for display of Proced buffer as process tree."
351 (make-variable-buffer-local 'proced-tree-flag
)
353 (defcustom proced-post-display-hook nil
354 "Normal hook run after displaying or updating a Proced buffer.
355 May be used to adapt the window size via `fit-window-to-buffer'."
357 :options
'(fit-window-to-buffer)
360 (defcustom proced-after-send-signal-hook nil
361 "Normal hook run after sending a signal to processes by `proced-send-signal'.
362 May be used to revert the process listing."
364 :options
'(proced-revert)
367 ;; Internal variables
369 (defvar proced-available
(not (null (list-system-processes)))
370 "Non-nil means Proced is known to work on this system.")
372 (defvar proced-process-alist nil
373 "Alist of processes displayed by Proced.
374 The car of each element is the PID, and the cdr is a list of
375 cons pairs, see `proced-process-attributes'.")
376 (make-variable-buffer-local 'proced-process-alist
)
378 (defvar proced-sort-internal nil
379 "Sort scheme for listing (internal format).
380 It is a list of lists (KEY PREDICATE REVERSE).")
382 (defvar proced-marker-char ?
* ; the answer is 42
383 "In Proced, the current mark character.")
385 ;; Faces and font-lock code taken from dired,
386 ;; but face variables are deprecated for new code.
387 (defgroup proced-faces nil
388 "Faces used by Proced."
393 '((t (:inherit font-lock-constant-face
)))
394 "Face used for Proced marks."
395 :group
'proced-faces
)
397 (defface proced-marked
398 '((t (:inherit error
)))
399 "Face used for marked processes."
400 :group
'proced-faces
)
402 (defface proced-sort-header
403 '((t (:inherit font-lock-keyword-face
)))
404 "Face used for header of attribute used for sorting."
405 :group
'proced-faces
)
407 (defvar proced-re-mark
"^[^ \n]"
408 "Regexp matching a marked line.
409 Important: the match ends just after the marker.")
411 (defvar proced-header-line nil
412 "Headers in Proced buffer as a string.")
413 (make-variable-buffer-local 'proced-header-line
)
415 (defvar proced-temp-alist nil
416 "Temporary alist (internal variable).")
418 (defvar proced-process-tree nil
419 "Proced process tree (internal variable).")
421 (defvar proced-tree-depth nil
422 "Internal variable for depth of Proced process tree.")
424 (defvar proced-auto-update-timer nil
425 "Stores if Proced auto update timer is already installed.")
427 (defvar proced-log-buffer
"*Proced log*"
428 "Name of Proced Log buffer.")
430 (defconst proced-help-string
431 "(n)ext, (p)revious, (m)ark, (u)nmark, (k)ill, (q)uit (type ? for more help)"
432 "Help string for Proced.")
434 (defconst proced-header-help-echo
435 "mouse-1, mouse-2: sort by attribute %s%s (%s)"
436 "Help string shown when mouse is over a sortable header.")
438 (defconst proced-field-help-echo
439 "mouse-2, RET: refine by attribute %s %s"
440 "Help string shown when mouse is over a refinable field.")
442 (defvar proced-font-lock-keywords
443 `(;; (Any) proced marks.
444 (,proced-re-mark .
'proced-mark
)
445 ;; Processes marked with `proced-marker-char'
446 ;; Should we make sure that only certain attributes are font-locked?
447 (,(concat "^[" (char-to-string proced-marker-char
) "]")
448 ".+" (proced-move-to-goal-column) nil
(0 'proced-marked
))))
450 (defvar proced-mode-map
451 (let ((km (make-sparse-keymap)))
453 (define-key km
" " 'next-line
)
454 (define-key km
"n" 'next-line
)
455 (define-key km
"p" 'previous-line
)
456 (define-key km
"\C-n" 'next-line
)
457 (define-key km
"\C-p" 'previous-line
)
458 (define-key km
"\C-?" 'previous-line
)
459 (define-key km
[down] 'next-line)
460 (define-key km [up] 'previous-line)
462 (define-key km "d" 'proced-mark) ; Dired compatibility ("delete")
463 (define-key km "m" 'proced-mark)
464 (put 'proced-mark :advertised-binding "m")
465 (define-key km "u" 'proced-unmark)
466 (define-key km "\177" 'proced-unmark-backward)
467 (define-key km "M" 'proced-mark-all)
468 (define-key km "U" 'proced-unmark-all)
469 (define-key km "t" 'proced-toggle-marks)
470 (define-key km "C" 'proced-mark-children)
471 (define-key km "P" 'proced-mark-parents)
473 (define-key km "f" 'proced-filter-interactive)
474 (define-key km [mouse-2] 'proced-refine)
475 (define-key km "\C-m" 'proced-refine)
477 (define-key km "sc" 'proced-sort-pcpu)
478 (define-key km "sm" 'proced-sort-pmem)
479 (define-key km "sp" 'proced-sort-pid)
480 (define-key km "ss" 'proced-sort-start)
481 (define-key km "sS" 'proced-sort-interactive)
482 (define-key km "st" 'proced-sort-time)
483 (define-key km "su" 'proced-sort-user)
484 ;; similar to `Buffer-menu-sort-by-column'
485 (define-key km [header-line mouse-1] 'proced-sort-header)
486 (define-key km [header-line mouse-2] 'proced-sort-header)
487 (define-key km "T" 'proced-toggle-tree)
489 (define-key km "F" 'proced-format-interactive)
491 (define-key km "o" 'proced-omit-processes)
492 (define-key km "x" 'proced-send-signal) ; Dired compatibility
493 (define-key km "k" 'proced-send-signal) ; kill processes
495 (define-key km "h" 'describe-mode)
496 (define-key km "?" 'proced-help)
497 (define-key km [remap undo] 'proced-undo)
498 (define-key km [remap advertised-undo] 'proced-undo)
499 ;; Additional keybindings are inherited from `special-mode-map'
501 "Keymap for Proced commands.")
504 proced-menu proced-mode-map "Proced Menu"
507 :help "Mark Current Process"]
508 ["Unmark" proced-unmark
509 :help "Unmark Current Process"]
510 ["Mark All" proced-mark-all
511 :help "Mark All Processes"]
512 ["Unmark All" proced-unmark-all
513 :help "Unmark All Process"]
514 ["Toggle Marks" proced-toggle-marks
515 :help "Marked Processes Become Unmarked, and Vice Versa"]
516 ["Mark Children" proced-mark-children
517 :help "Mark Current Process and its Children"]
518 ["Mark Parents" proced-mark-parents
519 :help "Mark Current Process and its Parents"]
522 :help "Select Filter for Process Listing"
523 ,@(mapcar (lambda (el)
524 (let ((filter (car el)))
525 `[,(symbol-name filter)
526 (proced-filter-interactive ',filter)
528 :selected (eq proced-filter ',filter)]))
529 proced-filter-alist))
531 :help "Select Sort Scheme"
532 ["Sort..." proced-sort-interactive
533 :help "Sort Process List"]
535 ["Sort by %CPU" proced-sort-pcpu]
536 ["Sort by %MEM" proced-sort-pmem]
537 ["Sort by PID" proced-sort-pid]
538 ["Sort by START" proced-sort-start]
539 ["Sort by TIME" proced-sort-time]
540 ["Sort by USER" proced-sort-user])
542 :help "Select Format for Process Listing"
543 ,@(mapcar (lambda (el)
544 (let ((format (car el)))
545 `[,(symbol-name format)
546 (proced-format-interactive ',format)
548 :selected (eq proced-format ',format)]))
549 proced-format-alist))
550 ["Tree Display" proced-toggle-tree
552 :selected (eval proced-tree-flag)
553 :help "Display Proced Buffer as Process Tree"]
555 ["Omit Marked Processes" proced-omit-processes
556 :help "Omit Marked Processes in Process Listing."]
558 ["Revert" revert-buffer
559 :help "Revert Process Listing"]
560 ["Auto Update" proced-toggle-auto-update
562 :selected (eval proced-auto-update-flag)
563 :help "Auto Update of Proced Buffer"]
564 ["Send signal" proced-send-signal
565 :help "Send Signal to Marked Processes"]))
568 (defun proced-marker-regexp ()
569 "Return regexp matching `proced-marker-char'."
570 ;; `proced-marker-char' must appear in column zero
571 (concat "^" (regexp-quote (char-to-string proced-marker-char))))
573 (defun proced-success-message (action count)
574 "Display success message for ACTION performed for COUNT processes."
575 (message "%s %s process%s" action count (if (= 1 count) "" "es")))
577 ;; Unlike dired, we do not define our own commands for vertical motion.
578 ;; If `goal-column' is set, `next-line' and `previous-line' are fancy
579 ;; commands to satisfy our modest needs. If `proced-goal-attribute'
580 ;; and/or `goal-column' are not set, `next-line' and `previous-line'
581 ;; are really what we need to preserve the column of point.
582 ;; We use `proced-move-to-goal-column' for "non-interactive" cases only
583 ;; to get a well-defined position of point.
585 (defun proced-move-to-goal-column ()
586 "Move to `goal-column' if non-nil. Return position of point."
590 (forward-char goal-column)
594 (defun proced-header-line ()
595 "Return header line for Proced buffer."
596 (list (propertize " " 'display '(space :align-to 0))
597 (if (<= (window-hscroll) (length proced-header-line))
598 (replace-regexp-in-string ;; preserve text properties
600 (substring proced-header-line (window-hscroll))))))
602 (defun proced-pid-at-point ()
603 "Return pid of system process at point.
604 Return nil if point is not on a process line."
607 (if (looking-at "^. .")
608 (get-text-property (match-end 0) 'proced-pid))))
612 (define-derived-mode proced-mode special-mode "Proced"
613 "Mode for displaying system processes and sending signals to them.
614 Type \\[proced] to start a Proced session. In a Proced buffer
615 type \\<proced-mode-map>\\[proced-mark] to mark a process for later commands.
616 Type \\[proced-send-signal] to send signals to marked processes.
618 The initial content of a listing is defined by the variable `proced-filter'
619 and the variable `proced-format'.
620 The variable `proced-filter' specifies which system processes are displayed.
621 The variable `proced-format' specifies which attributes are displayed for
622 each process. Type \\[proced-filter-interactive] and \\[proced-format-interactive]
623 to change the values of `proced-filter' and `proced-format'.
624 The current value of the variable `proced-filter' is indicated in the
627 The sort order of Proced listings is defined by the variable `proced-sort'.
628 Type \\[proced-sort-interactive] or click on a header in the header line
629 to change the sort scheme. The current sort scheme is indicated in the
630 mode line, using \"+\" or \"-\" for ascending or descending sort order.
632 Type \\[proced-toggle-tree] to toggle whether the listing is
633 displayed as process tree.
635 An existing Proced listing can be refined by typing \\[proced-refine].
636 Refining an existing listing does not update the variable `proced-filter'.
638 The attribute-specific rules for formatting, filtering, sorting, and refining
639 are defined in `proced-grammar-alist'.
641 After displaying or updating a Proced buffer, Proced runs the normal hook
642 `proced-post-display-hook'.
647 (setq buffer-read-only t
649 header-line-format '(:eval (proced-header-line)))
650 (add-hook 'post-command-hook 'force-mode-line-update nil t)
651 (set (make-local-variable 'revert-buffer-function) 'proced-revert)
652 (set (make-local-variable 'font-lock-defaults)
653 '(proced-font-lock-keywords t nil nil beginning-of-line))
654 (if (and (not proced-auto-update-timer) proced-auto-update-interval)
655 (setq proced-auto-update-timer
656 (run-at-time t proced-auto-update-interval
657 'proced-auto-update-timer))))
660 (defun proced (&optional arg)
661 "Generate a listing of UNIX system processes.
663 If invoked with optional ARG, do not select the window displaying
664 the process information.
666 This function runs the normal hook `proced-post-display-hook'.
668 See `proced-mode' for a description of features available in
671 (unless proced-available
672 (error "Proced is not available on this system"))
673 (let ((buffer (get-buffer-create "*Proced*")) new)
675 (setq new (zerop (buffer-size)))
678 ;; `proced-update' runs `proced-post-display-hook' only if the
679 ;; Proced buffer has been selected. Yet the following call of
680 ;; `proced-update' is for an empty Proced buffer that has not
681 ;; yet been selected. Therefore we need to call
682 ;; `proced-post-display-hook' below.
686 (display-buffer buffer)
687 (with-current-buffer buffer
689 (pop-to-buffer buffer)
692 (substitute-command-keys
693 "Type \\<proced-mode-map>\\[quit-window] to quit, \\[proced-help] for help")))))
695 (defun proced-auto-update-timer ()
696 "Auto-update Proced buffers using `run-at-time'."
697 (dolist (buf (buffer-list))
698 (with-current-buffer buf
699 (if (and (eq major-mode 'proced-mode)
700 proced-auto-update-flag)
701 (proced-update t t)))))
703 (defun proced-toggle-auto-update (arg)
704 "Change whether this Proced buffer is updated automatically.
705 With prefix ARG, update this buffer automatically if ARG is positive,
706 otherwise do not update. Sets the variable `proced-auto-update-flag'.
707 The time interval for updates is specified via `proced-auto-update-interval'."
708 (interactive (list (or current-prefix-arg 'toggle)))
709 (setq proced-auto-update-flag
710 (cond ((eq arg 'toggle) (not proced-auto-update-flag))
711 (arg (> (prefix-numeric-value arg) 0))
712 (t (not proced-auto-update-flag))))
713 (message "Proced auto update %s"
714 (if proced-auto-update-flag "enabled" "disabled")))
718 (defun proced-mark (&optional count)
719 "Mark the current (or next COUNT) processes."
721 (proced-do-mark t count))
723 (defun proced-unmark (&optional count)
724 "Unmark the current (or next COUNT) processes."
726 (proced-do-mark nil count))
728 (defun proced-unmark-backward (&optional count)
729 "Unmark the previous (or COUNT previous) processes."
730 ;; Analogous to `dired-unmark-backward',
731 ;; but `ibuffer-unmark-backward' behaves different.
733 (proced-do-mark nil (- (or count 1))))
735 (defun proced-do-mark (mark &optional count)
736 "Mark the current (or next COUNT) processes using MARK."
737 (or count (setq count 1))
738 (let ((backward (< count 0))
740 (setq count (1+ (if (<= 0 count) count
741 (min (1- (line-number-at-pos)) (abs count)))))
743 (while (not (or (zerop (setq count (1- count))) (eobp)))
744 (proced-insert-mark mark backward))
745 (proced-move-to-goal-column)))
747 (defun proced-toggle-marks ()
748 "Toggle marks: marked processes become unmarked, and vice versa."
750 (let ((mark-re (proced-marker-regexp))
753 (goto-char (point-min))
755 (cond ((looking-at mark-re)
756 (proced-insert-mark nil))
758 (proced-insert-mark t))
760 (forward-line 1)))))))
762 (defun proced-insert-mark (mark &optional backward)
763 "If MARK is non-nil, insert `proced-marker-char'.
764 If BACKWARD is non-nil, move one line backwards before inserting the mark.
765 Otherwise move one line forward after inserting the mark."
766 (if backward (forward-line -1))
767 (insert (if mark proced-marker-char ?\s))
769 (unless backward (forward-line)))
771 (defun proced-mark-all ()
773 If `transient-mark-mode' is turned on and the region is active,
776 (proced-do-mark-all t))
778 (defun proced-unmark-all ()
779 "Unmark all processes.
780 If `transient-mark-mode' is turned on and the region is active,
783 (proced-do-mark-all nil))
785 (defun proced-do-mark-all (mark)
786 "Mark all processes using MARK.
787 If `transient-mark-mode' is turned on and the region is active,
790 (proced-marker-char (if mark proced-marker-char ?\s))
791 (marker-re (proced-marker-regexp))
792 end buffer-read-only)
795 ;; Operate even on those lines that are only partially a part
796 ;; of region. This appears most consistent with
797 ;; `proced-move-to-goal-column'.
798 (progn (setq end (save-excursion
799 (goto-char (region-end))
800 (unless (looking-at "^") (forward-line))
802 (goto-char (region-beginning))
803 (unless (looking-at "^") (beginning-of-line)))
804 (goto-char (point-min))
805 (setq end (point-max)))
806 (while (< (point) end)
807 (unless (looking-at marker-re)
808 (setq count (1+ count))
809 (insert proced-marker-char)
812 (proced-success-message (if mark "Marked" "Unmarked") count))))
814 (defun proced-mark-children (ppid &optional omit-ppid)
815 "Mark child processes of process PPID.
816 Also mark process PPID unless prefix OMIT-PPID is non-nil."
817 (interactive (list (proced-pid-at-point) current-prefix-arg))
818 (proced-mark-process-alist
819 (proced-filter-children proced-process-alist ppid omit-ppid)))
821 (defun proced-mark-parents (cpid &optional omit-cpid)
822 "Mark parent processes of process CPID.
823 Also mark CPID unless prefix OMIT-CPID is non-nil."
824 (interactive (list (proced-pid-at-point) current-prefix-arg))
825 (proced-mark-process-alist
826 (proced-filter-parents proced-process-alist cpid omit-cpid)))
828 (defun proced-mark-process-alist (process-alist &optional quiet)
829 "Mark processes in PROCESS-ALIST.
830 If QUIET is non-nil suppress status message."
833 (let (buffer-read-only)
835 (goto-char (point-min))
837 (when (assq (proced-pid-at-point) process-alist)
838 (insert proced-marker-char)
840 (setq count (1+ count)))
843 (proced-success-message "Marked" count))))
845 ;; Mostly analog of `dired-do-kill-lines'.
846 ;; However, for negative args the target lines of `dired-do-kill-lines'
847 ;; include the current line, whereas `dired-mark' for negative args operates
848 ;; on the preceding lines. Here we are consistent with `dired-mark'.
849 (defun proced-omit-processes (&optional arg quiet)
850 "Omit marked processes.
851 With prefix ARG, omit that many lines starting with the current line.
852 \(A negative argument omits backward.)
853 If `transient-mark-mode' is turned on and the region is active,
854 omit the processes in region.
855 If QUIET is non-nil suppress status message.
856 Returns count of omitted lines."
858 (let ((mark-re (proced-marker-regexp))
861 (cond ((use-region-p) ;; Omit active region
862 (let ((lines (count-lines (region-beginning) (region-end))))
864 (goto-char (region-beginning))
865 (while (< count lines)
866 (proced-omit-process)
867 (setq count (1+ count))))))
868 ((not arg) ;; Omit marked lines
870 (goto-char (point-min))
871 (while (and (not (eobp))
872 (re-search-forward mark-re nil t))
873 (proced-omit-process)
874 (setq count (1+ count)))))
875 ((< 0 arg) ;; Omit forward
876 (while (and (not (eobp)) (< count arg))
877 (proced-omit-process)
878 (setq count (1+ count))))
879 ((< arg 0) ;; Omit backward
880 (while (and (not (bobp)) (< count (- arg)))
882 (proced-omit-process)
883 (setq count (1+ count)))))
884 (unless (zerop count) (proced-move-to-goal-column))
885 (unless quiet (proced-success-message "Omitted" count))
888 (defun proced-omit-process ()
889 "Omit process from listing point is on.
890 Update `proced-process-alist' accordingly."
891 (setq proced-process-alist
892 (assq-delete-all (proced-pid-at-point) proced-process-alist))
893 (delete-region (line-beginning-position)
894 (save-excursion (forward-line) (point))))
898 (defun proced-filter (process-alist filter-list)
899 "Apply FILTER-LIST to PROCESS-ALIST.
900 Return the filtered process list."
901 (if (symbolp filter-list)
902 (setq filter-list (cdr (assq filter-list proced-filter-alist))))
903 (dolist (filter filter-list)
905 (cond ( ;; apply function to entire process list
906 (eq (car filter) 'fun-all)
907 (setq new-alist (funcall (cdr filter) process-alist)))
908 ( ;; apply predicate to each list of attributes
909 (eq (car filter) 'function)
910 (dolist (process process-alist)
911 (if (funcall (car filter) (cdr process))
912 (push process new-alist))))
913 (t ;; apply predicate to specified attribute
914 (let ((fun (if (stringp (cdr filter))
916 (string-match ,(cdr filter) val))
919 (dolist (process process-alist)
920 (setq value (cdr (assq (car filter) (cdr process))))
921 (if (and value (funcall fun value))
922 (push process new-alist))))))
923 (setq process-alist new-alist)))
926 (defun proced-filter-interactive (scheme)
927 "Filter Proced buffer using SCHEME.
928 When called interactively, an empty string means nil, i.e., no filtering.
929 Set variable `proced-filter' to SCHEME. Revert listing."
931 (let ((scheme (completing-read "Filter: "
932 proced-filter-alist nil t)))
933 (list (if (string= "" scheme) nil (intern scheme)))))
934 ;; only update if necessary
935 (unless (eq proced-filter scheme)
936 (setq proced-filter scheme)
939 (defun proced-filter-parents (process-alist pid &optional omit-pid)
940 "For PROCESS-ALIST return list of parent processes of PID.
941 This list includes PID unless OMIT-PID is non-nil."
942 (let ((parent-list (unless omit-pid (list (assq pid process-alist))))
943 (process (assq pid process-alist))
945 (while (and (setq ppid (cdr (assq 'ppid (cdr process))))
946 ;; Ignore a PPID that equals PID.
948 ;; Accept only PPIDs that correspond to members in PROCESS-ALIST.
949 (setq process (assq ppid process-alist)))
951 (push process parent-list))
954 (defun proced-filter-children (process-alist ppid &optional omit-ppid)
955 "For PROCESS-ALIST return list of child processes of PPID.
956 This list includes PPID unless OMIT-PPID is non-nil."
957 (let ((proced-temp-alist (proced-children-alist process-alist))
959 (dolist (pid (proced-children-pids ppid))
960 (push (assq pid process-alist) new-alist))
962 (assq-delete-all ppid new-alist)
967 (defun proced-children-alist (process-alist)
968 "Return children alist for PROCESS-ALIST.
969 The children alist has elements (PPID PID1 PID2 ...).
970 PPID is a parent PID. PID1, PID2, ... are the child processes of PPID.
971 The children alist inherits the sorting order of PROCESS-ALIST.
972 The list of children does not include grandchildren."
973 ;; The PPIDs inherit the sorting order of PROCESS-ALIST.
974 (let ((process-tree (mapcar (lambda (a) (list (car a))) process-alist))
976 (dolist (process process-alist)
977 (setq ppid (cdr (assq 'ppid (cdr process))))
979 ;; Ignore a PPID that equals PID.
980 (/= ppid (car process))
981 ;; Accept only PPIDs that correspond to members in PROCESS-ALIST.
982 (assq ppid process-alist))
983 (let ((temp-alist process-tree) elt)
984 (while (setq elt (pop temp-alist))
985 (when (eq ppid (car elt))
986 (setq temp-alist nil)
987 (setcdr elt (cons (car process) (cdr elt))))))))
988 ;; The child processes inherit the sorting order of PROCESS-ALIST.
990 (mapcar (lambda (a) (cons (car a) (nreverse (cdr a))))
993 (defun proced-children-pids (ppid)
994 "Return list of children PIDs of PPID (including PPID)."
995 (let ((cpids (cdr (assq ppid proced-temp-alist))))
997 (cons ppid (apply 'append (mapcar 'proced-children-pids cpids)))
1000 (defun proced-process-tree (process-alist)
1001 "Return process tree for PROCESS-ALIST.
1002 It is an alist of alists where the car of each alist is a parent process
1003 and the cdr is a list of child processes according to the ppid attribute
1005 The process tree inherits the sorting order of PROCESS-ALIST."
1006 (let ((proced-temp-alist (proced-children-alist process-alist))
1007 pid-alist proced-process-tree)
1008 (while (setq pid-alist (pop proced-temp-alist))
1009 (push (proced-process-tree-internal pid-alist) proced-process-tree))
1010 (nreverse proced-process-tree)))
1012 (defun proced-process-tree-internal (pid-alist)
1013 "Helper function for `proced-process-tree'."
1014 (let ((cpid-list (cdr pid-alist)) cpid-alist cpid)
1015 (while (setq cpid (car cpid-list))
1016 (if (setq cpid-alist (assq cpid proced-temp-alist))
1017 ;; Unprocessed part of process tree that needs to be
1018 ;; analyzed recursively.
1020 (setq proced-temp-alist
1021 (assq-delete-all cpid proced-temp-alist))
1022 (setcar cpid-list (proced-process-tree-internal cpid-alist)))
1023 ;; We already processed this subtree and take it "as is".
1024 (setcar cpid-list (assq cpid proced-process-tree))
1025 (setq proced-process-tree
1026 (assq-delete-all cpid proced-process-tree)))
1030 (defun proced-toggle-tree (arg)
1031 "Toggle the display of the process listing as process tree.
1032 With prefix ARG, display as process tree if ARG is positive, otherwise
1033 do not display as process tree. Sets the variable `proced-tree-flag'.
1035 The process tree is generated from the selected processes in the
1036 Proced buffer (that is, the processes in `proced-process-alist').
1037 All processes that do not have a parent process in this list
1038 according to their ppid attribute become the root of a process tree.
1039 Each parent process is followed by its child processes.
1040 The process tree inherits the chosen sorting order of the process listing,
1041 that is, child processes of the same parent process are sorted using
1042 the selected sorting order."
1043 (interactive (list (or current-prefix-arg 'toggle)))
1044 (setq proced-tree-flag
1045 (cond ((eq arg 'toggle) (not proced-tree-flag))
1046 (arg (> (prefix-numeric-value arg) 0))
1047 (t (not proced-tree-flag))))
1049 (message "Proced process tree display %s"
1050 (if proced-tree-flag "enabled" "disabled")))
1052 (defun proced-tree (process-alist)
1053 "Rearrange PROCESS-ALIST as process tree.
1054 If `proced-tree-flag' is non-nil, rearrange PROCESS-ALIST such that
1055 every processes is followed by its child processes. Each process
1056 gets a tree attribute that specifies the depth of the process in the tree.
1057 A root process is a process with no parent within PROCESS-ALIST according
1058 to its value of the ppid attribute. It has depth 0.
1060 If `proced-tree-flag' is nil, remove the tree attribute.
1061 Return the rearranged process list."
1062 (if proced-tree-flag
1063 ;; add tree attribute
1064 (let ((process-tree (proced-process-tree process-alist))
1065 (proced-tree-depth 0)
1066 (proced-temp-alist process-alist)
1067 proced-process-tree pt)
1068 (while (setq pt (pop process-tree))
1069 (proced-tree-insert pt))
1070 (nreverse proced-process-tree))
1071 ;; remove tree attribute
1072 (let ((process-alist process-alist))
1073 (while process-alist
1074 (setcar process-alist
1075 (assq-delete-all 'tree (car process-alist)))
1076 (pop process-alist)))
1079 (defun proced-tree-insert (process-tree)
1080 "Helper function for `proced-tree'."
1081 (let ((pprocess (assq (car process-tree) proced-temp-alist)))
1082 (push (append (list (car pprocess))
1083 (list (cons 'tree proced-tree-depth))
1085 proced-process-tree)
1086 (if (cdr process-tree)
1087 (let ((proced-tree-depth (1+ proced-tree-depth)))
1088 (mapc 'proced-tree-insert (cdr process-tree))))))
1092 ;; Filters are used to select the processes in a new listing.
1093 ;; Refiners are used to narrow down further (interactively) the processes
1094 ;; in an existing listing.
1096 (defun proced-refine (&optional event)
1097 "Refine Proced listing by comparing with the attribute value at point.
1098 Optional EVENT is the location of the Proced field.
1100 Refinement is controlled by the REFINER defined for each attribute ATTR
1101 in `proced-grammar-alist'.
1103 If REFINER is a list of flags and point is on a process's value of ATTR,
1104 this command compares the value of ATTR of every process with the value
1105 of ATTR of the process at the position of point.
1107 The predicate for the comparison of two ATTR values is defined
1108 in `proced-grammar-alist'. For each return value of the predicate
1109 a refine flag is defined in `proced-grammar-alist'. One can select
1110 processes for which the value of ATTR is \"less than\", \"equal\",
1111 and / or \"larger\" than ATTR of the process point is on. A process
1112 is included in the new listing if the refine flag for the corresponding
1113 return value of the predicate is non-nil.
1114 The help-echo string for `proced-refine' uses \"+\" or \"-\" to indicate
1115 the current values of these refine flags.
1117 If REFINER is a cons pair (FUNCTION . HELP-ECHO), FUNCTION is called
1118 with one argument, the PID of the process at the position of point.
1119 The function must return a list of PIDs that is used for the refined
1120 listing. HELP-ECHO is a string that is shown when mouse is over this field.
1122 This command refines an already existing process listing generated initially
1123 based on the value of the variable `proced-filter'. It does not change
1124 this variable. It does not revert the listing. If you frequently need
1125 a certain refinement, consider defining a new filter in `proced-filter-alist'."
1126 (interactive (list last-input-event))
1127 (if event (posn-set-point (event-end event)))
1128 (let ((key (get-text-property (point) 'proced-key))
1129 (pid (get-text-property (point) 'proced-pid)))
1131 (let* ((grammar (assq key proced-grammar-alist))
1132 (refiner (nth 7 grammar)))
1134 (cond ((functionp (car refiner))
1135 (setq proced-process-alist (funcall (car refiner) pid)))
1137 (let ((predicate (nth 4 grammar))
1138 (ref (cdr (assq key (cdr (assq pid proced-process-alist)))))
1140 (dolist (process proced-process-alist)
1141 (setq val (funcall predicate (cdr (assq key (cdr process))) ref))
1142 (if (cond ((not val) (nth 2 refiner))
1143 ((eq val 'equal) (nth 1 refiner))
1144 (val (car refiner)))
1145 (push process new-alist)))
1146 (setq proced-process-alist new-alist))))
1147 ;; Do not revert listing.
1149 (message "No refiner defined here."))))
1151 ;; Proced predicates for sorting and filtering are based on a three-valued
1153 ;; Predicates take two arguments P1 and P2, the corresponding attribute
1154 ;; values of two processes. Predicates should return 'equal if P1 has
1155 ;; same rank like P2. Any other non-nil value says that P1 is "less than" P2,
1158 (defun proced-< (num1 num2)
1159 "Return t if NUM1 less than NUM2.
1160 Return `equal' if NUM1 equals NUM2. Return nil if NUM1 greater than NUM2."
1165 (defun proced-string-lessp (s1 s2)
1166 "Return t if string S1 is less than S2 in lexicographic order.
1167 Return `equal' if S1 and S2 have identical contents.
1168 Return nil otherwise."
1171 (string-lessp s1 s2)))
1173 (defun proced-time-lessp (t1 t2)
1174 "Return t if time value T1 is less than time value T2.
1175 Return `equal' if T1 equals T2. Return nil otherwise."
1176 (with-decoded-time-value ((high1 low1 micro1 pico1 type1 t1)
1177 (high2 low2 micro2 pico2 type2 t2))
1178 (cond ((< high1 high2))
1179 ((< high2 high1) nil)
1183 ((< micro2 micro1) nil)
1185 ((< pico2 pico1) nil)
1190 (defsubst proced-xor (b1 b2)
1191 "Return the logical exclusive or of args B1 and B2."
1195 (defun proced-sort-p (p1 p2)
1196 "Predicate for sorting processes P1 and P2."
1197 (if (not (cdr proced-sort-internal))
1198 ;; only one predicate: fast scheme
1199 (let* ((sorter (car proced-sort-internal))
1200 (k1 (cdr (assq (car sorter) (cdr p1))))
1201 (k2 (cdr (assq (car sorter) (cdr p2)))))
1202 ;; if the attributes are undefined, we should really abort sorting
1204 (proced-xor (funcall (nth 1 sorter) k1 k2)
1206 (let ((sort-list proced-sort-internal) sorter predicate k1 k2)
1208 (while (setq sorter (pop sort-list))
1209 (setq k1 (cdr (assq (car sorter) (cdr p1)))
1210 k2 (cdr (assq (car sorter) (cdr p2)))
1213 (funcall (nth 1 sorter) k1 k2)))
1214 (if (not (eq predicate 'equal))
1215 (throw 'done (proced-xor predicate (nth 2 sorter)))))
1216 (eq t predicate)))))
1218 (defun proced-sort (process-alist sorter descend)
1219 "Sort PROCESS-ALIST using scheme SORTER.
1220 SORTER is a scheme like `proced-sort'.
1221 DESCEND is non-nil if the first element of SORTER is sorted
1222 in descending order.
1223 Return the sorted process list."
1224 ;; translate SORTER into a list of lists (KEY PREDICATE REVERSE)
1225 (setq proced-sort-internal
1226 (mapcar (lambda (arg)
1227 (let ((grammar (assq arg proced-grammar-alist)))
1228 (unless (nth 4 grammar)
1229 (error "Attribute %s not sortable" (car grammar)))
1230 (list arg (nth 4 grammar) (nth 5 grammar))))
1231 (cond ((listp sorter) sorter)
1232 ((and (symbolp sorter)
1233 (nth 6 (assq sorter proced-grammar-alist))))
1234 ((symbolp sorter) (list sorter))
1235 (t (error "Sorter undefined %s" sorter)))))
1236 (if proced-sort-internal
1238 ;; splice DESCEND into the list
1239 (setcar proced-sort-internal
1240 (list (caar proced-sort-internal)
1241 (nth 1 (car proced-sort-internal)) descend))
1242 (sort process-alist 'proced-sort-p))
1245 (defun proced-sort-interactive (scheme &optional arg)
1246 "Sort Proced buffer using SCHEME.
1247 When called interactively, an empty string means nil, i.e., no sorting.
1249 Prefix ARG controls sort order:
1250 - If prefix ARG is positive (negative), sort in ascending (descending) order.
1251 - If ARG is nil or 'no-arg and SCHEME is equal to the previous sorting scheme,
1252 reverse the sorting order.
1253 - If ARG is nil or 'no-arg and SCHEME differs from the previous sorting scheme,
1254 adopt the sorting order defined for SCHEME in `proced-grammar-alist'.
1256 Set variable `proced-sort' to SCHEME. The current sort scheme is displayed
1257 in the mode line, using \"+\" or \"-\" for ascending or descending order."
1260 (scheme (completing-read "Sort attribute: "
1261 (dolist (grammar proced-grammar-alist choices)
1263 (push (list (car grammar)) choices)))
1265 (list (if (string= "" scheme) nil (intern scheme))
1266 ;; like 'toggle in `define-derived-mode'
1267 (or current-prefix-arg 'no-arg))))
1269 (setq proced-descend
1270 ;; If `proced-sort-interactive' is called repeatedly for the same
1271 ;; sort key, the sort order is reversed.
1272 (cond ((and (eq arg 'no-arg) (equal proced-sort scheme))
1273 (not proced-descend))
1275 (nth 5 (assq (if (consp scheme) (car scheme) scheme)
1276 proced-grammar-alist)))
1277 (arg (< (prefix-numeric-value arg) 0))
1278 ((equal proced-sort scheme)
1279 (not proced-descend))
1280 (t (nth 5 (assq (if (consp scheme) (car scheme) scheme)
1281 proced-grammar-alist))))
1285 (defun proced-sort-pcpu (&optional arg)
1286 "Sort Proced buffer by percentage CPU time (%CPU).
1287 Prefix ARG controls sort order, see `proced-sort-interactive'."
1288 (interactive (list (or current-prefix-arg 'no-arg)))
1289 (proced-sort-interactive 'pcpu arg))
1291 (defun proced-sort-pmem (&optional arg)
1292 "Sort Proced buffer by percentage memory usage (%MEM).
1293 Prefix ARG controls sort order, see `proced-sort-interactive'."
1294 (interactive (list (or current-prefix-arg 'no-arg)))
1295 (proced-sort-interactive 'pmem arg))
1297 (defun proced-sort-pid (&optional arg)
1298 "Sort Proced buffer by PID.
1299 Prefix ARG controls sort order, see `proced-sort-interactive'."
1300 (interactive (list (or current-prefix-arg 'no-arg)))
1301 (proced-sort-interactive 'pid arg))
1303 (defun proced-sort-start (&optional arg)
1304 "Sort Proced buffer by time the command started (START).
1305 Prefix ARG controls sort order, see `proced-sort-interactive'."
1306 (interactive (list (or current-prefix-arg 'no-arg)))
1307 (proced-sort-interactive 'start arg))
1309 (defun proced-sort-time (&optional arg)
1310 "Sort Proced buffer by CPU time (TIME).
1311 Prefix ARG controls sort order, see `proced-sort-interactive'."
1312 (interactive (list (or current-prefix-arg 'no-arg)))
1313 (proced-sort-interactive 'time arg))
1315 (defun proced-sort-user (&optional arg)
1316 "Sort Proced buffer by USER.
1317 Prefix ARG controls sort order, see `proced-sort-interactive'."
1318 (interactive (list (or current-prefix-arg 'no-arg)))
1319 (proced-sort-interactive 'user arg))
1321 (defun proced-sort-header (event &optional arg)
1322 "Sort Proced listing based on an attribute.
1323 EVENT is a mouse event with starting position in the header line.
1324 It is converted to the corresponding attribute key.
1325 This command updates the variable `proced-sort'.
1326 Prefix ARG controls sort order, see `proced-sort-interactive'."
1327 (interactive (list last-input-event (or last-prefix-arg 'no-arg)))
1328 (let ((start (event-start event))
1330 (save-selected-window
1331 (select-window (posn-window start))
1332 (setq col (+ (1- (car (posn-actual-col-row start)))
1334 (when (and (<= 0 col) (< col (length proced-header-line)))
1335 (setq key (get-text-property col 'proced-key proced-header-line))
1337 (proced-sort-interactive key arg)
1338 (message "No sorter defined here."))))))
1342 (defun proced-format-time (time)
1343 "Format time interval TIME."
1344 (let* ((ftime (float-time time))
1345 (days (truncate ftime 86400))
1346 (ftime (mod ftime 86400))
1347 (hours (truncate ftime 3600))
1348 (ftime (mod ftime 3600))
1349 (minutes (truncate ftime 60))
1350 (seconds (mod ftime 60)))
1352 (format "%d-%02d:%02d:%02d" days hours minutes seconds))
1354 (format "%02d:%02d:%02d" hours minutes seconds))
1356 (format "%02d:%02d" minutes seconds)))))
1358 (defun proced-format-start (start)
1360 The return string is always 6 characters wide."
1361 (let ((d-start (decode-time start))
1362 (d-current (decode-time)))
1363 (cond ( ;; process started in previous years
1364 (< (nth 5 d-start) (nth 5 d-current))
1365 (format-time-string " %Y" start))
1366 ;; process started today
1367 ((and (= (nth 3 d-start) (nth 3 d-current))
1368 (= (nth 4 d-start) (nth 4 d-current)))
1369 (format-time-string " %H:%M" start))
1370 (t ;; process started this year
1371 (format-time-string "%b %e" start)))))
1373 (defun proced-format-ttname (ttname)
1374 "Format attribute TTNAME, omitting path \"/dev/\"."
1375 ;; Does this work for all systems?
1376 (substring ttname (if (string-match "\\`/dev/" ttname)
1379 (defun proced-format-tree (tree)
1380 "Format attribute TREE."
1381 (concat (make-string tree ?\s) (number-to-string tree)))
1383 ;; Proced assumes that every process occupies only one line in the listing.
1384 (defun proced-format-args (args)
1385 "Format attribute ARGS.
1386 Replace newline characters by \"^J\" (two characters)."
1387 (replace-regexp-in-string "\n" "^J" args))
1389 (defun proced-format (process-alist format)
1390 "Display PROCESS-ALIST using FORMAT."
1391 (if (symbolp format)
1392 (setq format (cdr (assq format proced-format-alist))))
1394 ;; Not all systems give us all attributes. We take `emacs-pid' as a
1395 ;; representative process PID. If FORMAT contains a list of alternative
1396 ;; attributes, we take the first attribute that is non-nil for `emacs-pid'.
1397 ;; If none of the alternatives is non-nil, the attribute is ignored
1399 (let ((standard-attributes
1400 (car (proced-process-attributes (list (emacs-pid)))))
1402 (if (and proced-tree-flag
1403 (assq 'ppid standard-attributes))
1404 (push (cons 'tree 0) standard-attributes))
1405 (dolist (fmt format)
1407 (if (assq fmt standard-attributes)
1408 (push fmt new-format))
1409 (while (setq fmi (pop fmt))
1410 (when (assq fmi standard-attributes)
1411 (push fmi new-format)
1413 (setq format (nreverse new-format)))
1415 (insert (make-string (length process-alist) ?\n))
1416 (let ((whitespace " ") (unknown "?")
1417 (sort-key (if (consp proced-sort) (car proced-sort) proced-sort))
1418 header-list grammar)
1419 ;; Loop over all attributes
1420 (while (setq grammar (assq (pop format) proced-grammar-alist))
1421 (let* ((key (car grammar))
1422 (fun (cond ((stringp (nth 2 grammar))
1423 `(lambda (arg) (format ,(nth 2 grammar) arg)))
1424 ((not (nth 2 grammar)) 'identity)
1425 ( t (nth 2 grammar))))
1426 (whitespace (if format whitespace ""))
1428 ;; We use the text property `proced-key' to store in each
1429 ;; field the corresponding key.
1430 ;; Of course, the sort predicate appearing in help-echo
1431 ;; is only part of the story. But it gives the main idea.
1434 (let ((descend (if (eq key sort-key) proced-descend (nth 5 grammar))))
1435 `(proced-key ,key mouse-face highlight
1436 help-echo ,(format proced-header-help-echo
1437 (if descend "-" "+")
1439 (if descend "descending" "ascending"))))))
1440 (refiner (nth 7 grammar))
1442 (cond ((functionp (car refiner))
1443 `(proced-key ,key mouse-face highlight
1444 help-echo ,(format "mouse-2, RET: %s"
1447 `(proced-key ,key mouse-face highlight
1448 help-echo ,(format "mouse-2, RET: refine by attribute %s %s"
1450 (mapconcat (lambda (s)
1455 ;; highlight the header of the sort column
1456 (if (eq key sort-key)
1457 (setq hprops (append '(face proced-sort-header) hprops)))
1458 (goto-char (point-min))
1459 (cond ( ;; fixed width of output field
1460 (numberp (nth 3 grammar))
1461 (dolist (process process-alist)
1463 (setq value (cdr (assq key (cdr process))))
1465 (apply 'propertize (funcall fun value) fprops)
1466 (format (concat "%" (number-to-string (nth 3 grammar)) "s")
1470 (push (format (concat "%" (number-to-string (nth 3 grammar)) "s")
1471 (apply 'propertize (nth 1 grammar) hprops))
1474 ( ;; last field left-justified
1475 (and (not format) (eq 'left (nth 3 grammar)))
1476 (dolist (process process-alist)
1478 (setq value (cdr (assq key (cdr process))))
1479 (insert (if value (apply 'propertize (funcall fun value) fprops)
1482 (push (apply 'propertize (nth 1 grammar) hprops) header-list))
1484 (t ;; calculated field width
1485 (let ((width (length (nth 1 grammar)))
1487 (dolist (process process-alist)
1488 (setq value (cdr (assq key (cdr process))))
1490 (setq value (apply 'propertize (funcall fun value) fprops)
1491 width (max width (length value))
1492 field-list (cons value field-list))
1493 (push unknown field-list)
1494 (setq width (max width (length unknown)))))
1495 (let ((afmt (concat "%" (if (eq 'left (nth 3 grammar)) "-" "")
1496 (number-to-string width) "s")))
1497 (push (format afmt (apply 'propertize (nth 1 grammar) hprops))
1499 (dolist (value (nreverse field-list))
1501 (insert (format afmt value) whitespace)
1502 (forward-line))))))))
1505 (goto-char (point-min))
1506 (dolist (process process-alist)
1507 ;; We use the text property `proced-pid' to store in each line
1508 ;; the corresponding pid
1509 (put-text-property (point) (line-end-position) 'proced-pid (car process))
1512 (setq proced-header-line
1513 (mapconcat 'identity (nreverse header-list) whitespace))
1514 (if (string-match "[ \t]+$" proced-header-line)
1515 (setq proced-header-line (substring proced-header-line 0
1516 (match-beginning 0))))
1517 ;; (delete-trailing-whitespace)
1518 (goto-char (point-min))
1519 (while (re-search-forward "[ \t\r]+$" nil t)
1520 (delete-region (match-beginning 0) (match-end 0)))))
1522 (defun proced-format-interactive (scheme &optional revert)
1523 "Format Proced buffer using SCHEME.
1524 When called interactively, an empty string means nil, i.e., no formatting.
1525 Set variable `proced-format' to SCHEME.
1526 With prefix REVERT non-nil revert listing."
1528 (let ((scheme (completing-read "Format: "
1529 proced-format-alist nil t)))
1530 (list (if (string= "" scheme) nil (intern scheme))
1531 current-prefix-arg)))
1532 ;; only update if necessary
1533 (when (or (not (eq proced-format scheme)) revert)
1534 (setq proced-format scheme)
1535 (proced-update revert)))
1539 (defun proced-process-attributes (&optional pid-list)
1540 "Return alist of attributes for each system process.
1541 This alist can be customized via `proced-custom-attributes'.
1542 Optional arg PID-LIST is a list of PIDs of system process that are analyzed.
1543 If no attributes are known for a process (possibly because it already died)
1544 the process is ignored."
1545 ;; Should we make it customizable whether processes with empty attribute
1546 ;; lists are ignored? When would such processes be of interest?
1547 (let (process-alist attributes attr)
1548 (dolist (pid (or pid-list (list-system-processes)) process-alist)
1549 (when (setq attributes (process-attributes pid))
1550 (setq attributes (cons (cons 'pid pid) attributes))
1551 (dolist (fun proced-custom-attributes)
1552 (if (setq attr (funcall fun attributes))
1553 (push attr attributes)))
1554 (push (cons pid attributes) process-alist)))))
1556 (defun proced-update (&optional revert quiet)
1557 "Update the Proced process information. Preserves point and marks.
1558 With prefix REVERT non-nil, revert listing.
1559 Suppress status information if QUIET is nil.
1560 After updating a displayed Proced buffer run the normal hook
1561 `proced-post-display-hook'."
1562 ;; This is the main function that generates and updates the process listing.
1564 (setq revert (or revert (not proced-process-alist)))
1565 (or quiet (message (if revert "Updating process information..."
1566 "Updating process display...")))
1567 (if revert ;; evaluate all processes
1568 (setq proced-process-alist (proced-process-attributes)))
1569 ;; filtering and sorting
1570 (setq proced-process-alist
1571 (proced-sort (proced-filter proced-process-alist proced-filter)
1572 proced-sort proced-descend))
1574 ;; display as process tree?
1575 (setq proced-process-alist
1576 (proced-tree proced-process-alist))
1578 ;; It is useless to keep undo information if we revert, filter, or
1579 ;; refine the listing so that `proced-process-alist' has changed.
1580 ;; We could keep the undo information if we only re-sort the buffer.
1581 ;; Would that be useful? Re-re-sorting is easy, too.
1582 (if (consp buffer-undo-list)
1583 (setq buffer-undo-list nil))
1584 (let ((buffer-undo-list t)
1585 ;; If point is on a field, we try to return point to that field.
1586 ;; Otherwise we try to return to the same column
1587 (old-pos (let ((pid (proced-pid-at-point))
1588 (key (get-text-property (point) 'proced-key)))
1589 (list pid key ; can both be nil
1591 (if (get-text-property (1- (point)) 'proced-key)
1592 (- (point) (previous-single-property-change
1593 (point) 'proced-key))
1595 (current-column)))))
1596 buffer-read-only mp-list)
1597 ;; remember marked processes (whatever the mark was)
1598 (goto-char (point-min))
1599 (while (re-search-forward "^\\(\\S-\\)" nil t)
1600 (push (cons (save-match-data (proced-pid-at-point))
1601 (match-string-no-properties 1)) mp-list))
1605 (proced-format proced-process-alist proced-format)
1606 (goto-char (point-min))
1610 (setq proced-header-line (concat " " proced-header-line))
1611 (if revert (set-buffer-modified-p nil))
1613 ;; set `goal-column'
1614 (let ((grammar (assq proced-goal-attribute proced-grammar-alist)))
1615 (setq goal-column ;; set to nil if no match
1617 (not (zerop (buffer-size)))
1618 (string-match (regexp-quote (nth 1 grammar))
1619 proced-header-line))
1624 ;; Restore process marks and buffer position (if possible).
1625 ;; Sometimes this puts point in the middle of the proced buffer
1626 ;; where it is not interesting. Is there a better / more flexible solution?
1627 (goto-char (point-min))
1628 (let (pid mark new-pos)
1629 (if (or mp-list (car old-pos))
1631 (setq pid (proced-pid-at-point))
1632 (when (setq mark (assq pid mp-list))
1635 (beginning-of-line))
1636 (when (eq (car old-pos) pid)
1638 (let ((limit (line-end-position)) pos)
1639 (while (and (not new-pos)
1640 (setq pos (next-property-change (point) nil limit)))
1642 (when (eq (nth 1 old-pos)
1643 (get-text-property (point) 'proced-key))
1644 (forward-char (min (nth 2 old-pos)
1645 (- (next-property-change (point))
1647 (setq new-pos (point))))
1649 ;; we found the process, but the field of point
1650 ;; is not listed anymore
1651 (setq new-pos (proced-move-to-goal-column))))
1652 (setq new-pos (min (+ (line-beginning-position) (nth 2 old-pos))
1653 (line-end-position)))))
1657 (goto-char (point-min))
1658 (proced-move-to-goal-column)))
1660 ;; Does the long `mode-name' clutter the mode line? It would be nice
1661 ;; to have some other location for displaying the values of the various
1662 ;; flags that affect the behavior of proced (flags one might want
1663 ;; to change on the fly). Where??
1667 (concat ": " (symbol-name proced-filter))
1670 (let* ((key (if (consp proced-sort) (car proced-sort)
1672 (grammar (assq key proced-grammar-alist)))
1673 (concat " by " (if proced-descend "-" "+")
1676 (force-mode-line-update)
1677 ;; run `proced-post-display-hook' only for a displayed buffer.
1678 (if (get-buffer-window) (run-hooks 'proced-post-display-hook))
1680 (or quiet (input-pending-p)
1681 (message (if revert "Updating process information...done."
1682 "Updating process display...done.")))))
1684 (defun proced-revert (&rest _args)
1685 "Reevaluate the process listing based on the currently running processes.
1686 Preserves point and marks."
1689 (defun proced-send-signal (&optional signal)
1690 "Send a SIGNAL to the marked processes.
1691 If no process is marked, operate on current process.
1692 SIGNAL may be a string (HUP, INT, TERM, etc.) or a number.
1693 If SIGNAL is nil display marked processes and query interactively for SIGNAL.
1694 After sending the signal, this command runs the normal hook
1695 `proced-after-send-signal-hook'."
1697 (let ((regexp (proced-marker-regexp))
1699 ;; collect marked processes
1701 (goto-char (point-min))
1702 (while (re-search-forward regexp nil t)
1703 (push (cons (proced-pid-at-point)
1704 ;; How much info should we collect here?
1705 (buffer-substring-no-properties
1706 (+ 2 (line-beginning-position))
1707 (line-end-position)))
1711 (nreverse process-alist)
1712 ;; take current process
1713 (list (cons (proced-pid-at-point)
1714 (buffer-substring-no-properties
1715 (+ 2 (line-beginning-position))
1716 (line-end-position))))))
1718 ;; Display marked processes (code taken from `dired-mark-pop-up').
1719 (let ((bufname " *Marked Processes*") ; use leading space in buffer name
1720 ; to make this buffer ephemeral
1721 (header-line (substring-no-properties proced-header-line)))
1722 (with-current-buffer (get-buffer-create bufname)
1723 (setq truncate-lines t
1724 proced-header-line header-line ; inherit header line
1725 header-line-format '(:eval (proced-header-line)))
1726 (add-hook 'post-command-hook 'force-mode-line-update nil t)
1727 (let ((inhibit-read-only t))
1729 (buffer-disable-undo)
1730 (setq buffer-read-only t)
1731 (dolist (process process-alist)
1732 (insert " " (cdr process) "\n"))
1734 (goto-char (point-min)))
1735 (save-window-excursion
1736 ;; Analogous to `dired-pop-to-buffer'
1737 ;; Don't split window horizontally. (Bug#1806)
1738 (let (split-width-threshold)
1739 (pop-to-buffer (current-buffer)))
1740 (fit-window-to-buffer (get-buffer-window) nil 1)
1741 (let* ((completion-ignore-case t)
1742 (pnum (if (= 1 (length process-alist))
1744 (format "%d processes" (length process-alist))))
1745 (completion-extra-properties
1746 '(:annotation-function
1747 (lambda (s) (cdr (assoc s proced-signal-list))))))
1749 (completing-read (concat "Send signal [" pnum
1750 "] (default TERM): ")
1752 nil nil nil nil "TERM")))))))
1756 ;; Why not always use `signal-process'? See
1757 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-03/msg02955.html
1758 (if (functionp proced-signal-function)
1759 ;; use built-in `signal-process'
1760 (let ((signal (if (stringp signal)
1761 (if (string-match "\\`[0-9]+\\'" signal)
1762 (string-to-number signal)
1763 (make-symbol signal))
1765 (dolist (process process-alist)
1768 proced-signal-function (car process) signal))
1769 (setq count (1+ count))
1770 (proced-log "%s\n" (cdr process))
1771 (push (cdr process) failures))
1772 (error ; catch errors from failed signals
1773 (proced-log "%s\n" err)
1774 (proced-log "%s\n" (cdr process))
1775 (push (cdr process) failures)))))
1776 ;; use external system call
1777 (let ((signal (concat "-" (if (numberp signal)
1778 (number-to-string signal) signal))))
1779 (dolist (process process-alist)
1782 (if (zerop (call-process
1783 proced-signal-function nil t nil
1784 signal (number-to-string (car process))))
1785 (setq count (1+ count))
1786 (proced-log (current-buffer))
1787 (proced-log "%s\n" (cdr process))
1788 (push (cdr process) failures))
1789 (error ; catch errors from failed signals
1790 (proced-log (current-buffer))
1791 (proced-log "%s\n" (cdr process))
1792 (push (cdr process) failures)))))))
1794 ;; Proced error message are not always very precise.
1795 ;; Can we issue a useful one-line summary in the
1796 ;; message area (using FAILURES) if only one signal failed?
1799 (format "%d of %d signal%s failed"
1800 (length failures) (length process-alist)
1801 (if (= 1 (length process-alist)) "" "s")))
1802 (proced-success-message "Sent signal to" count)))
1804 (run-hooks 'proced-after-send-signal-hook)))
1806 ;; similar to `dired-why'
1807 (defun proced-why ()
1808 "Pop up a buffer with error log output from Proced.
1809 A group of errors from a single command ends with a formfeed.
1810 Thus, use \\[backward-page] to find the beginning of a group of errors."
1812 (if (get-buffer proced-log-buffer)
1813 (save-selected-window
1814 ;; move `proced-log-buffer' to the front of the buffer list
1815 (select-window (display-buffer (get-buffer proced-log-buffer)))
1816 (setq truncate-lines t)
1817 (set-buffer-modified-p nil)
1818 (setq buffer-read-only t)
1819 (goto-char (point-max))
1824 ;; similar to `dired-log'
1825 (defun proced-log (log &rest args)
1826 "Log a message or the contents of a buffer.
1827 If LOG is a string and there are more args, it is formatted with
1828 those ARGS. Usually the LOG string ends with a \\n.
1829 End each bunch of errors with (proced-log t signal):
1830 this inserts the current time, buffer and signal at the start of the page,
1831 and \f (formfeed) at the end."
1832 (let ((obuf (current-buffer)))
1833 (with-current-buffer (get-buffer-create proced-log-buffer)
1834 (goto-char (point-max))
1835 (let (buffer-read-only)
1836 (cond ((stringp log)
1838 (apply 'format log args)
1841 (insert-buffer-substring log))
1846 (insert (current-time-string)
1847 "\tBuffer `" (buffer-name obuf) "', "
1848 (format "signal `%s'\n" (car args)))
1849 (goto-char (point-max))
1850 (insert "\f\n")))))))
1852 ;; similar to `dired-log-summary'
1853 (defun proced-log-summary (signal string)
1854 "State a summary of SIGNAL's failures, in echo area and log buffer.
1855 STRING is an overall summary of the failures."
1856 (message "Signal %s: %s--type ? for details" signal string)
1857 ;; Log a summary describing a bunch of errors.
1858 (proced-log (concat "\n" string "\n"))
1859 (proced-log t signal))
1861 (defun proced-help ()
1862 "Provide help for the Proced user."
1865 (if (eq last-command 'proced-help)
1867 (message proced-help-string)))
1869 (defun proced-undo ()
1870 "Undo in a Proced buffer.
1871 This doesn't recover killed processes, it just undoes changes in the Proced
1872 buffer. You can use it to recover marks."
1874 (let (buffer-read-only)
1876 (message "Change in Proced buffer undone.
1877 Killed processes cannot be recovered by Emacs."))
1881 ;;; proced.el ends here