Re-regenerate.
[emacs.git] / lisp / proced.el
blob9f7b4206a47e69d2aefb73575392569ac8150a78
1 ;;; proced.el --- operate on system processes like dired
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: Roland Winkler <Roland.Winkler@physik.uni-erlangen.de>
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/>.
23 ;;; Commentary:
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.
30 ;; To do:
31 ;; - interactive temporary customizability of flags in `proced-grammar-alist'
32 ;; - allow "sudo kill PID", "renice PID"
34 ;; Wishlist
35 ;; - tree view like pstree(1)
37 ;; Thoughts and Ideas
38 ;; - Currently, `system-process-attributes' returns the list of
39 ;; command-line arguments of a process as one concatenated string.
40 ;; This format is compatible with `shell-command'. Also, under
41 ;; MS-Windows, the command-line arguments are actually stored as a
42 ;; single string, so that it is impossible to reverse-engineer it back
43 ;; into separate arguments. Alternatively, `system-process-attributes'
44 ;; could (try to) return a list of strings that correspond to individual
45 ;; command-line arguments. Then one could feed such a list of
46 ;; command-line arguments into `call-process' or `start-process'.
47 ;; Are there real-world applications when such a feature would be useful?
48 ;; What about something like `proced-restart-pid'?
50 ;;; Code:
52 (require 'time-date) ; for `with-decoded-time-value'
54 (defgroup proced nil
55 "Proced mode."
56 :group 'processes
57 :group 'unix
58 :prefix "proced-")
60 (defcustom proced-signal-function 'signal-process
61 "Name of signal function.
62 It can be an elisp function (usually `signal-process') or a string specifying
63 the external command (usually \"kill\")."
64 :group 'proced
65 :type '(choice (function :tag "function")
66 (string :tag "command")))
68 (defcustom proced-signal-list
69 '( ;; signals supported on all POSIX compliant systems
70 ("HUP (1. Hangup)")
71 ("INT (2. Terminal interrupt)")
72 ("QUIT (3. Terminal quit)")
73 ("ABRT (6. Process abort)")
74 ("KILL (9. Kill - cannot be caught or ignored)")
75 ("ALRM (14. Alarm Clock)")
76 ("TERM (15. Termination)")
77 ;; POSIX 1003.1-2001
78 ;; Which systems do not support these signals so that we can
79 ;; exclude them from `proced-signal-list'?
80 ("CONT (Continue executing)")
81 ("STOP (Stop executing / pause - cannot be caught or ignored)")
82 ("TSTP (Terminal stop / pause)"))
83 "List of signals, used for minibuffer completion."
84 :group 'proced
85 :type '(repeat (string :tag "signal")))
87 ;; For which attributes can we use a fixed width of the output field?
88 ;; A fixed width speeds up formatting, yet it can make
89 ;; `proced-grammar-alist' system-dependent.
90 ;; (If proced runs like top(1) we want it to be fast.)
92 ;; If it is impossible / unlikely that an attribute has the same value
93 ;; for two processes, then sorting can be based on one ordinary (fast)
94 ;; predicate like `<'. Otherwise, a list of proced predicates can be used
95 ;; to refine the sort.
97 ;; It would be neat if one could temporarily override the following
98 ;; predefined rules.
99 (defcustom proced-grammar-alist
100 '( ;; attributes defined in `system-process-attributes'
101 (euid "EUID" "%d" right proced-< nil (euid pid) (nil t nil))
102 (user "USER" nil left proced-string-lessp nil (user pid) (nil t nil))
103 (egid "EGID" "%d" right proced-< nil (egid euid pid) (nil t nil))
104 (group "GROUP" nil left proced-string-lessp nil (group user pid) (nil t nil))
105 (comm "COMMAND" nil left proced-string-lessp nil (comm pid) (nil t nil))
106 (state "STAT" nil left proced-string-lessp nil (state pid) (nil t nil))
107 (ppid "PPID" "%d" right proced-< nil (ppid pid)
108 ((lambda (ppid) (proced-filter-parents proced-process-alist ppid)) .
109 "refine to process parents"))
110 (pgrp "PGRP" "%d" right proced-< nil (pgrp euid pid) (nil t nil))
111 (sess "SESS" "%d" right proced-< nil (sess pid) (nil t nil))
112 (ttname "TTY" proced-format-ttname left proced-string-lessp nil (ttname pid) (nil t nil))
113 (tpgid "TPGID" "%d" right proced-< nil (tpgid pid) (nil t nil))
114 (minflt "MINFLT" "%d" right proced-< nil (minflt pid) (nil t t))
115 (majflt "MAJFLT" "%d" right proced-< nil (majflt pid) (nil t t))
116 (cminflt "CMINFLT" "%d" right proced-< nil (cminflt pid) (nil t t))
117 (cmajflt "CMAJFLT" "%d" right proced-< nil (cmajflt pid) (nil t t))
118 (utime "UTIME" proced-format-time right proced-time-lessp t (utime pid) (nil t t))
119 (stime "STIME" proced-format-time right proced-time-lessp t (stime pid) (nil t t))
120 (cutime "CUTIME" proced-format-time right proced-time-lessp t (cutime pid) (nil t t))
121 (cstime "CSTIME" proced-format-time right proced-time-lessp t (cstime 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"))
137 ;; time: sum of utime and stime
138 (time "TIME" proced-format-time right proced-time-lessp t (time pid) (nil t t))
139 ;; ctime: sum of cutime and cstime
140 (ctime "CTIME" proced-format-time right proced-time-lessp t (ctime pid) (nil t t)))
141 "Alist of rules for handling Proced attributes.
143 Each element has the form
145 (KEY NAME FORMAT JUSTIFY PREDICATE REVERSE SORT-SCHEME REFINER).
147 Symbol KEY is the car of a process attribute.
149 String NAME appears in the header line.
151 FORMAT specifies the format for displaying the attribute values.
152 It can be a string passed to `format'. It can be a function called
153 with one argument, the value of the attribute. Nil means take as is.
155 If JUSTIFY is an integer, its modulus gives the width of the attribute
156 values formatted with FORMAT. If JUSTIFY is positive, NAME appears
157 right-justified, otherwise it appears left-justified. If JUSTIFY is 'left
158 or 'right, the field width is calculated from all field values in the listing.
159 If JUSTIFY is 'left, the field values are formatted left-justified and
160 right-justified otherwise.
162 PREDICATE is the predicate for sorting and filtering the process listing
163 based on attribute KEY. PREDICATE takes two arguments P1 and P2,
164 the corresponding attribute values of two processes. PREDICATE should
165 return 'equal if P1 has same rank like P2. Any other non-nil value says
166 that P1 is \"less than\" P2, or nil if not.
168 PREDICATE defines an ascending sort order. REVERSE is non-nil if the sort
169 order is descending.
171 SORT-SCHEME is a list (KEY1 KEY2 ...) defining a hierarchy of rules
172 for sorting the process listing. KEY1, KEY2, ... are KEYs appearing as cars
173 of `proced-grammar-alist'. First the PREDICATE of KEY1 is evaluated.
174 If it yields non-equal, it defines the sort order for the corresponding
175 processes. If it evaluates to 'equal the PREDICATE of KEY2 is evaluated, etc.
177 REFINER can be a list of flags (LESS-B EQUAL-B LARGER-B) used by the command
178 `proced-refine' (see there) to refine the listing based on attribute KEY.
179 This command compares the value of attribute KEY of every process with
180 the value of attribute KEY of the process at the position of point
181 using PREDICATE.
182 If PREDICATE yields non-nil, the process is accepted if LESS-B is non-nil.
183 If PREDICATE yields 'equal, the process is accepted if EQUAL-B is non-nil.
184 If PREDICATE yields nil, the process is accepted if LARGER-B is non-nil.
186 REFINER can also be a cons pair (FUNCTION . HELP-ECHO).
187 FUNCTION is called with one argument, the PID of the process at the position
188 of point. The function must return a list of PIDs that is used for the refined
189 listing. HELP-ECHO is a string that is shown when mouse is over this field.
191 If REFINER is nil no refinement is done."
192 :group 'proced
193 :type '(repeat (list :tag "Attribute"
194 (symbol :tag "Key")
195 (string :tag "Header")
196 (choice :tag "Format"
197 (const :tag "None" nil)
198 (string :tag "Format String")
199 (function :tag "Formatting Function"))
200 (choice :tag "Justification"
201 (const :tag "left" left)
202 (const :tag "right" right)
203 (integer :tag "width"))
204 (function :tag "Predicate")
205 (boolean :tag "Descending Sort Order")
206 (repeat :tag "Sort Scheme" (symbol :tag "Key"))
207 (choice :tag "Refiner"
208 (list :tag "Refine Flags"
209 (boolean :tag "Less")
210 (boolean :tag "Equal")
211 (boolean :tag "Larger"))
212 (cons (function :tag "Refinement Function")
213 (string :tag "Help echo"))
214 (const :tag "None" nil)))))
216 (defcustom proced-custom-attributes nil
217 "List of functions defining custom attributes.
218 This variable extends the functionality of `proced-process-attributes'.
219 Each function is called with one argument, the list of attributes
220 of a system process. It returns a cons cell of the form (KEY . VALUE)
221 like `system-process-attributes'. This cons cell is appended to the list
222 returned by `proced-process-attributes'.
223 If the function returns nil, the value is ignored."
224 :group 'proced
225 :type '(repeat (function :tag "Attribute")))
227 ;; Formatting and sorting rules are defined "per attribute". If formatting
228 ;; and / or sorting should use more than one attribute, it appears more
229 ;; transparent to define a new derived attribute, so that formatting and
230 ;; sorting can use them consistently. (Are there exceptions to this rule?
231 ;; Would it be advantageous to have yet more general methods available?)
232 ;; Sorting can also be based on attributes that are invisible in the listing.
234 (defcustom proced-format-alist
235 '((short user pid pcpu pmem start time (args comm))
236 (medium user pid pcpu pmem vsize rss ttname state start time (args comm))
237 (long user euid group pid pri nice pcpu pmem vsize rss ttname state
238 start time (args comm))
239 (verbose user euid group egid pid ppid pgrp sess pri nice pcpu pmem
240 state thcount vsize rss ttname tpgid minflt majflt cminflt cmajflt
241 start time utime stime ctime cutime cstime etime (args comm)))
242 "Alist of formats of listing.
243 The car of each element is a symbol, the name of the format.
244 The cdr is a list of attribute keys appearing in `proced-grammar-alist'.
245 An element of this list may also be a list of attribute keys that specifies
246 alternatives. If the first attribute is absent for a process, use the second
247 one, etc."
248 :group 'proced
249 :type '(alist :key-type (symbol :tag "Format Name")
250 :value-type (repeat :tag "Keys"
251 (choice (symbol :tag "")
252 (repeat :tag "Alternative Keys"
253 (symbol :tag ""))))))
255 (defcustom proced-format 'short
256 "Current format of Proced listing.
257 It can be the car of an element of `proced-format-alist'.
258 It can also be a list of keys appearing in `proced-grammar-alist'."
259 :group 'proced
260 :type '(choice (symbol :tag "Format Name")
261 (repeat :tag "Keys" (symbol :tag ""))))
262 (make-variable-buffer-local 'proced-format)
264 ;; FIXME: is there a better name for filter `user' that does not coincide
265 ;; with an attribute key?
266 (defcustom proced-filter-alist
267 `((user (user . ,(concat "\\`" (user-real-login-name) "\\'")))
268 (user-running (user . ,(concat "\\`" (user-real-login-name) "\\'"))
269 (state . "\\`[Rr]\\'"))
270 (all)
271 (all-running (state . "\\`[Rr]\\'"))
272 (emacs (fun-all . (lambda (list)
273 (proced-filter-children list ,(emacs-pid))))))
274 "Alist of process filters.
275 The car of each element is a symbol, the name of the filter.
276 The cdr is a list of elementary filters that are applied to every process.
277 A process is displayed if it passes all elementary filters of a selected
278 filter.
280 An elementary filter can be one of the following:
281 \(KEY . REGEXP) If value of attribute KEY matches REGEXP,
282 accept this process.
283 \(KEY . FUN) Apply function FUN to attribute KEY. Accept this process,
284 if FUN returns non-nil.
285 \(function . FUN) For each process, apply function FUN to list of attributes
286 of each. Accept the process if FUN returns non-nil.
287 \(fun-all . FUN) Apply function FUN to entire process list.
288 FUN must return the filtered list."
289 :group 'proced
290 :type '(repeat (cons :tag "Filter"
291 (symbol :tag "Filter Name")
292 (repeat :tag "Filters"
293 (choice (cons :tag "Key . Regexp" (symbol :tag "Key") regexp)
294 (cons :tag "Key . Function" (symbol :tag "Key") function)
295 (cons :tag "Function" (const :tag "Key: function" function) function)
296 (cons :tag "Fun-all" (const :tag "Key: fun-all" fun-all) function))))))
298 (defcustom proced-filter 'user
299 "Current filter of proced listing.
300 It can be the car of an element of `proced-filter-alist'.
301 It can also be a list of elementary filters as in the cdrs of the elements
302 of `proced-filter-alist'."
303 :group 'proced
304 :type '(choice (symbol :tag "Filter Name")
305 (repeat :tag "Filters"
306 (choice (cons :tag "Key . Regexp" (symbol :tag "Key") regexp)
307 (cons :tag "Key . Function" (symbol :tag "Key") function)
308 (cons :tag "Function" (const :tag "Key: function" function) function)
309 (cons :tag "Fun-all" (const :tag "Key: fun-all" fun-all) function)))))
310 (make-variable-buffer-local 'proced-filter)
312 (defcustom proced-sort 'pcpu
313 "Current sort scheme for proced listing.
314 It must be the KEY of an element of `proced-grammar-alist'.
315 It can also be a list of KEYs as in the SORT-SCHEMEs of the elements
316 of `proced-grammar-alist'."
317 :group 'proced
318 :type '(choice (symbol :tag "Sort Scheme")
319 (repeat :tag "Key List" (symbol :tag "Key"))))
320 (make-variable-buffer-local 'proced-format)
322 (defcustom proced-descend t
323 "Non-nil if proced listing is sorted in descending order."
324 :group 'proced
325 :type '(boolean :tag "Descending Sort Order"))
326 (make-variable-buffer-local 'proced-descend)
328 (defcustom proced-goal-attribute 'args
329 "If non-nil, key of the attribute that defines the `goal-column'."
330 :group 'proced
331 :type '(choice (const :tag "none" nil)
332 (symbol :tag "key")))
334 (defcustom proced-auto-update-interval 5
335 "Time interval in seconds for auto updating Proced buffers."
336 :group 'proced
337 :type 'integer)
339 (defcustom proced-auto-update-flag nil
340 "Non-nil for auto update of a Proced buffer.
341 Can be changed interactively via `proced-toggle-auto-update'."
342 :group 'proced
343 :type 'boolean)
344 (make-variable-buffer-local 'proced-auto-update-flag)
346 ;; Internal variables
348 (defvar proced-available (not (null (list-system-processes)))
349 "Non-nil means Proced is known to work on this system.")
351 (defvar proced-process-alist nil
352 "Alist of processes displayed by Proced.
353 The car of each element is the PID, and the cdr is a list of
354 cons pairs, see `proced-process-attributes'.")
355 (make-variable-buffer-local 'proced-process-alist)
357 (defvar proced-sort-internal nil
358 "Sort scheme for listing (internal format).
359 It is a list of lists (KEY PREDICATE REVERSE).")
361 (defvar proced-marker-char ?* ; the answer is 42
362 "In proced, the current mark character.")
364 ;; Faces and font-lock code taken from dired,
365 ;; but face variables are deprecated for new code.
366 (defgroup proced-faces nil
367 "Faces used by Proced."
368 :group 'proced
369 :group 'faces)
371 (defface proced-mark
372 '((t (:inherit font-lock-constant-face)))
373 "Face used for proced marks."
374 :group 'proced-faces)
376 (defface proced-marked
377 '((t (:inherit font-lock-warning-face)))
378 "Face used for marked processes."
379 :group 'proced-faces)
381 (defface proced-sort-header
382 '((t (:inherit font-lock-keyword-face)))
383 "Face used for header of attribute used for sorting."
384 :group 'proced-faces)
386 (defvar proced-re-mark "^[^ \n]"
387 "Regexp matching a marked line.
388 Important: the match ends just after the marker.")
390 (defvar proced-header-line nil
391 "Headers in Proced buffer as a string.")
392 (make-variable-buffer-local 'proced-header-line)
394 (defvar proced-process-tree nil
395 "Process tree of listing (internal variable).")
397 (defvar proced-auto-update-timer nil
398 "Stores if Proced auto update timer is already installed.")
400 (defvar proced-log-buffer "*Proced log*"
401 "Name of Proced Log buffer.")
403 (defconst proced-help-string
404 "(n)ext, (p)revious, (m)ark, (u)nmark, (k)ill, (q)uit (type ? for more help)"
405 "Help string for proced.")
407 (defconst proced-header-help-echo
408 "mouse-1, mouse-2: sort by attribute %s%s (%s)"
409 "Help string shown when mouse is over a sortable header.")
411 (defconst proced-field-help-echo
412 "mouse-2, RET: refine by attribute %s %s"
413 "Help string shown when mouse is over a refinable field.")
415 (defvar proced-font-lock-keywords
416 `(;; (Any) proced marks.
417 (,proced-re-mark . 'proced-mark)
418 ;; Processes marked with `proced-marker-char'
419 ;; Should we make sure that only certain attributes are font-locked?
420 (,(concat "^[" (char-to-string proced-marker-char) "]")
421 ".+" (proced-move-to-goal-column) nil (0 'proced-marked))))
423 (defvar proced-mode-map
424 (let ((km (make-sparse-keymap)))
425 ;; moving
426 (define-key km " " 'proced-next-line)
427 (define-key km "n" 'next-line)
428 (define-key km "p" 'previous-line)
429 (define-key km "\C-n" 'next-line)
430 (define-key km "\C-p" 'previous-line)
431 (define-key km "\C-?" 'previous-line)
432 (define-key km [down] 'next-line)
433 (define-key km [up] 'previous-line)
434 ;; marking
435 (define-key km "d" 'proced-mark) ; Dired compatibility ("delete")
436 (define-key km "m" 'proced-mark)
437 (define-key km "u" 'proced-unmark)
438 (define-key km "\177" 'proced-unmark-backward)
439 (define-key km "M" 'proced-mark-all)
440 (define-key km "U" 'proced-unmark-all)
441 (define-key km "t" 'proced-toggle-marks)
442 (define-key km "C" 'proced-mark-children)
443 (define-key km "P" 'proced-mark-parents)
444 ;; filtering
445 (define-key km "f" 'proced-filter-interactive)
446 (define-key km [mouse-2] 'proced-refine)
447 (define-key km "\C-m" 'proced-refine)
448 ;; sorting
449 (define-key km "sc" 'proced-sort-pcpu)
450 (define-key km "sm" 'proced-sort-pmem)
451 (define-key km "sp" 'proced-sort-pid)
452 (define-key km "ss" 'proced-sort-start)
453 (define-key km "sS" 'proced-sort-interactive)
454 (define-key km "st" 'proced-sort-time)
455 (define-key km "su" 'proced-sort-user)
456 ;; similar to `Buffer-menu-sort-by-column'
457 (define-key km [header-line mouse-1] 'proced-sort-header)
458 (define-key km [header-line mouse-2] 'proced-sort-header)
459 ;; formatting
460 (define-key km "F" 'proced-format-interactive)
461 ;; operate
462 (define-key km "o" 'proced-omit-processes)
463 (define-key km "x" 'proced-send-signal) ; Dired compatibility
464 (define-key km "k" 'proced-send-signal) ; kill processes
465 ;; misc
466 (define-key km "g" 'revert-buffer) ; Dired compatibility
467 (define-key km "h" 'describe-mode)
468 (define-key km "?" 'proced-help)
469 (define-key km "q" 'quit-window)
470 (define-key km [remap undo] 'proced-undo)
471 (define-key km [remap advertised-undo] 'proced-undo)
473 "Keymap for proced commands.")
475 (easy-menu-define
476 proced-menu proced-mode-map "Proced Menu"
477 `("Proced"
478 ["Mark" proced-mark
479 :help "Mark Current Process"]
480 ["Unmark" proced-unmark
481 :help "Unmark Current Process"]
482 ["Mark All" proced-mark-all
483 :help "Mark All Processes"]
484 ["Unmark All" proced-unmark-all
485 :help "Unmark All Process"]
486 ["Toggle Marks" proced-toggle-marks
487 :help "Marked Processes Become Unmarked, and Vice Versa"]
488 ["Mark Children" proced-mark-children
489 :help "Mark Current Process and its Children"]
490 ["Mark Parents" proced-mark-parents
491 :help "Mark Current Process and its Parents"]
492 "--"
493 ("Filters"
494 :help "Select Filter for Process Listing"
495 ,@(mapcar (lambda (el)
496 (let ((filter (car el)))
497 `[,(symbol-name filter)
498 (proced-filter-interactive ',filter)
499 :style radio
500 :selected (eq proced-filter ',filter)]))
501 proced-filter-alist))
502 ("Sorting"
503 :help "Select Sort Scheme"
504 ["Sort..." proced-sort-interactive
505 :help "Sort Process List"]
506 "--"
507 ["Sort by %CPU" proced-sort-pcpu]
508 ["Sort by %MEM" proced-sort-pmem]
509 ["Sort by PID" proced-sort-pid]
510 ["Sort by START" proced-sort-start]
511 ["Sort by TIME" proced-sort-time]
512 ["Sort by USER" proced-sort-user])
513 ("Formats"
514 :help "Select Format for Process Listing"
515 ,@(mapcar (lambda (el)
516 (let ((format (car el)))
517 `[,(symbol-name format)
518 (proced-format-interactive ',format)
519 :style radio
520 :selected (eq proced-format ',format)]))
521 proced-format-alist))
522 "--"
523 ["Omit Marked Processes" proced-omit-processes
524 :help "Omit Marked Processes in Process Listing."]
525 "--"
526 ["Revert" revert-buffer
527 :help "Revert Process Listing"]
528 ["Auto Update" proced-toggle-auto-update
529 :style toggle
530 :selected (eval proced-auto-update-flag)
531 :help "Auto Update of Proced Buffer"]
532 ["Send signal" proced-send-signal
533 :help "Send Signal to Marked Processes"]))
535 ;; helper functions
536 (defun proced-marker-regexp ()
537 "Return regexp matching `proced-marker-char'."
538 ;; `proced-marker-char' must appear in column zero
539 (concat "^" (regexp-quote (char-to-string proced-marker-char))))
541 (defun proced-success-message (action count)
542 "Display success message for ACTION performed for COUNT processes."
543 (message "%s %s process%s" action count (if (= 1 count) "" "es")))
545 ;; Unlike dired, we do not define our own commands for vertical motion.
546 ;; If `goal-column' is set, `next-line' and `previous-line' are fancy
547 ;; commands to satisfy our modest needs. If `proced-goal-attribute'
548 ;; and/or `goal-column' are not set, `next-line' and `previous-line'
549 ;; are really what we need to preserve the column of point.
550 ;; We use `proced-move-to-goal-column' for "non-interactive" cases only
551 ;; to get a well-defined position of point.
553 (defun proced-move-to-goal-column ()
554 "Move to `goal-column' if non-nil. Return position of point."
555 (beginning-of-line)
556 (unless (eobp)
557 (if goal-column
558 (forward-char goal-column)
559 (forward-char 2)))
560 (point))
562 (defun proced-header-line ()
563 "Return header line for Proced buffer."
564 (list (propertize " " 'display '(space :align-to 0))
565 (replace-regexp-in-string ;; preserve text properties
566 "\\(%\\)" "\\1\\1" (substring proced-header-line (window-hscroll)))))
568 (defun proced-pid-at-point ()
569 "Return pid of system process at point.
570 Return nil if point is not on a process line."
571 (save-excursion
572 (beginning-of-line)
573 (if (looking-at "^. .")
574 (get-text-property (match-end 0) 'proced-pid))))
576 ;; proced mode
578 (define-derived-mode proced-mode nil "Proced"
579 "Mode for displaying UNIX system processes and sending signals to them.
580 Type \\[proced] to start a Proced session. In a Proced buffer
581 type \\<proced-mode-map>\\[proced-mark] to mark a process for later commands.
582 Type \\[proced-send-signal] to send signals to marked processes.
584 The initial content of a listing is defined by the variable `proced-filter'
585 and the variable `proced-format'.
586 The variable `proced-filter' specifies which system processes are displayed.
587 The variable `proced-format' specifies which attributes are displayed for
588 each process. Type \\[proced-filter-interactive] and \\[proced-format-interactive]
589 to change the values of `proced-filter' and `proced-format'.
590 The current value of the variable `proced-filter' is indicated in the
591 mode line.
593 The sort order of Proced listings is defined by the variable `proced-sort'.
594 Type \\[proced-sort-interactive] or click on a header in the header line
595 to change the sort scheme. The current sort scheme is indicated in the
596 mode line, using \"+\" or \"-\" for ascending or descending sort order.
598 An existing Proced listing can be refined by typing \\[proced-refine].
599 Refining an existing listing does not update the variable `proced-filter'.
601 The attribute-specific rules for formatting, filtering, sorting, and refining
602 are defined in `proced-grammar-alist'.
604 \\{proced-mode-map}"
605 (abbrev-mode 0)
606 (auto-fill-mode 0)
607 (setq buffer-read-only t
608 truncate-lines t
609 header-line-format '(:eval (proced-header-line)))
610 (add-hook 'post-command-hook 'force-mode-line-update nil t)
611 (set (make-local-variable 'revert-buffer-function) 'proced-revert)
612 (set (make-local-variable 'font-lock-defaults)
613 '(proced-font-lock-keywords t nil nil beginning-of-line))
614 (if (and (not proced-auto-update-timer) proced-auto-update-interval)
615 (setq proced-auto-update-timer
616 (run-at-time t proced-auto-update-interval
617 'proced-auto-update-timer))))
619 ;; Proced mode is suitable only for specially formatted data.
620 (put 'proced-mode 'mode-class 'special)
622 ;;;###autoload
623 (defun proced (&optional arg)
624 "Generate a listing of UNIX system processes.
625 If invoked with optional ARG the window displaying the process
626 information will be displayed but not selected.
628 See `proced-mode' for a description of features available in Proced buffers."
629 (interactive "P")
630 (unless proced-available
631 (error "Proced is not available on this system"))
632 (let ((buffer (get-buffer-create "*Proced*")) new)
633 (set-buffer buffer)
634 (setq new (zerop (buffer-size)))
635 (if new (proced-mode))
636 (if (or new arg)
637 (proced-update t))
638 (if arg
639 (display-buffer buffer)
640 (pop-to-buffer buffer)
641 (message
642 (substitute-command-keys
643 "Type \\<proced-mode-map>\\[quit-window] to quit, \\[proced-help] for help")))))
645 (defun proced-auto-update-timer ()
646 "Auto-update Proced buffers using `run-at-time'."
647 (dolist (buf (buffer-list))
648 (with-current-buffer buf
649 (if (and (eq major-mode 'proced-mode)
650 proced-auto-update-flag)
651 (proced-update t t)))))
653 (defun proced-toggle-auto-update (arg)
654 "Change whether this Proced buffer is updated automatically.
655 With prefix ARG, update this buffer automatically if ARG is positive,
656 otherwise do not update. Sets the variable `proced-auto-update-flag'.
657 The time interval for updates is specified via `proced-auto-update-interval'."
658 (interactive (list (or current-prefix-arg 'toggle)))
659 (setq proced-auto-update-flag
660 (cond ((eq arg 'toggle) (not proced-auto-update-flag))
661 (arg (> (prefix-numeric-value arg) 0))
662 (t (not proced-auto-update-flag))))
663 (message "Proced auto update %s"
664 (if proced-auto-update-flag "enabled" "disabled")))
666 (defun proced-mark (&optional count)
667 "Mark the current (or next COUNT) processes."
668 (interactive "p")
669 (proced-do-mark t count))
671 (defun proced-unmark (&optional count)
672 "Unmark the current (or next COUNT) processes."
673 (interactive "p")
674 (proced-do-mark nil count))
676 (defun proced-unmark-backward (&optional count)
677 "Unmark the previous (or COUNT previous) processes."
678 ;; Analogous to `dired-unmark-backward',
679 ;; but `ibuffer-unmark-backward' behaves different.
680 (interactive "p")
681 (proced-do-mark nil (- (or count 1))))
683 (defun proced-do-mark (mark &optional count)
684 "Mark the current (or next COUNT) processes using MARK."
685 (or count (setq count 1))
686 (let ((backward (< count 0))
687 buffer-read-only)
688 (setq count (1+ (if (<= 0 count) count
689 (min (1- (line-number-at-pos)) (abs count)))))
690 (beginning-of-line)
691 (while (not (or (zerop (setq count (1- count))) (eobp)))
692 (proced-insert-mark mark backward))
693 (proced-move-to-goal-column)))
695 (defun proced-mark-all ()
696 "Mark all processes.
697 If `transient-mark-mode' is turned on and the region is active,
698 mark the region."
699 (interactive)
700 (proced-do-mark-all t))
702 (defun proced-unmark-all ()
703 "Unmark all processes.
704 If `transient-mark-mode' is turned on and the region is active,
705 unmark the region."
706 (interactive)
707 (proced-do-mark-all nil))
709 (defun proced-do-mark-all (mark)
710 "Mark all processes using MARK.
711 If `transient-mark-mode' is turned on and the region is active,
712 mark the region."
713 (let ((count 0) end buffer-read-only)
714 (save-excursion
715 (if (use-region-p)
716 ;; Operate even on those lines that are only partially a part
717 ;; of region. This appears most consistent with
718 ;; `proced-move-to-goal-column'.
719 (progn (setq end (save-excursion
720 (goto-char (region-end))
721 (unless (looking-at "^") (forward-line))
722 (point)))
723 (goto-char (region-beginning))
724 (unless (looking-at "^") (beginning-of-line)))
725 (goto-char (point-min))
726 (setq end (point-max)))
727 (while (< (point) end)
728 (setq count (1+ count))
729 (proced-insert-mark mark))
730 (proced-success-message "Marked" count))))
732 (defun proced-toggle-marks ()
733 "Toggle marks: marked processes become unmarked, and vice versa."
734 (interactive)
735 (let ((mark-re (proced-marker-regexp))
736 buffer-read-only)
737 (save-excursion
738 (goto-char (point-min))
739 (while (not (eobp))
740 (cond ((looking-at mark-re)
741 (proced-insert-mark nil))
742 ((looking-at " ")
743 (proced-insert-mark t))
745 (forward-line 1)))))))
747 (defun proced-insert-mark (mark &optional backward)
748 "If MARK is non-nil, insert `proced-marker-char'.
749 If BACKWARD is non-nil, move one line backwards before inserting the mark.
750 Otherwise move one line forward after inserting the mark."
751 (if backward (forward-line -1))
752 (insert (if mark proced-marker-char ?\s))
753 (delete-char 1)
754 (unless backward (forward-line)))
756 (defun proced-mark-children (ppid &optional omit-ppid)
757 "Mark child processes of process PPID.
758 Also mark process PPID unless prefix OMIT-PPID is non-nil."
759 (interactive (list (proced-pid-at-point) current-prefix-arg))
760 (proced-mark-process-alist
761 (proced-filter-children proced-process-alist ppid omit-ppid)))
763 (defun proced-mark-parents (cpid &optional omit-cpid)
764 "Mark parent processes of process CPID.
765 Also mark CPID unless prefix OMIT-CPID is non-nil."
766 (interactive (list (proced-pid-at-point) current-prefix-arg))
767 (proced-mark-process-alist
768 (proced-filter-parents proced-process-alist cpid omit-cpid)))
770 (defun proced-mark-process-alist (process-alist &optional quiet)
771 (let ((count 0))
772 (if process-alist
773 (let (buffer-read-only)
774 (save-excursion
775 (goto-char (point-min))
776 (while (not (eobp))
777 (when (assq (proced-pid-at-point) process-alist)
778 (insert proced-marker-char)
779 (delete-char 1)
780 (setq count (1+ count)))
781 (forward-line)))))
782 (unless quiet
783 (proced-success-message "Marked" count))))
785 ;; Mostly analog of `dired-do-kill-lines'.
786 ;; However, for negative args the target lines of `dired-do-kill-lines'
787 ;; include the current line, whereas `dired-mark' for negative args operates
788 ;; on the preceding lines. Here we are consistent with `dired-mark'.
789 (defun proced-omit-processes (&optional arg quiet)
790 "Omit marked processes.
791 With prefix ARG, omit that many lines starting with the current line.
792 \(A negative argument omits backward.)
793 If `transient-mark-mode' is turned on and the region is active,
794 omit the processes in region.
795 If QUIET is non-nil suppress status message.
796 Returns count of omitted lines."
797 (interactive "P")
798 (let ((mark-re (proced-marker-regexp))
799 (count 0)
800 buffer-read-only)
801 (cond ((use-region-p) ;; Omit active region
802 (let ((lines (count-lines (region-beginning) (region-end))))
803 (save-excursion
804 (goto-char (region-beginning))
805 (while (< count lines)
806 (proced-omit-process)
807 (setq count (1+ count))))))
808 ((not arg) ;; Omit marked lines
809 (save-excursion
810 (goto-char (point-min))
811 (while (and (not (eobp))
812 (re-search-forward mark-re nil t))
813 (proced-omit-process)
814 (setq count (1+ count)))))
815 ((< 0 arg) ;; Omit forward
816 (while (and (not (eobp)) (< count arg))
817 (proced-omit-process)
818 (setq count (1+ count))))
819 ((< arg 0) ;; Omit backward
820 (while (and (not (bobp)) (< count (- arg)))
821 (forward-line -1)
822 (proced-omit-process)
823 (setq count (1+ count)))))
824 (unless (zerop count) (proced-move-to-goal-column))
825 (unless quiet (proced-success-message "Omitted" count))
826 count))
828 (defun proced-omit-process ()
829 "Omit process from listing point is on.
830 Update `proced-process-alist' accordingly."
831 (setq proced-process-alist
832 (assq-delete-all (proced-pid-at-point) proced-process-alist))
833 (delete-region (line-beginning-position)
834 (save-excursion (forward-line) (point))))
836 ;;; Filtering
838 (defun proced-filter (process-alist filter-list)
839 "Apply FILTER-LIST to PROCESS-ALIST.
840 Return the filtered process list."
841 (if (symbolp filter-list)
842 (setq filter-list (cdr (assq filter-list proced-filter-alist))))
843 (dolist (filter filter-list)
844 (let (new-alist)
845 (cond ( ;; apply function to entire process list
846 (eq (car filter) 'fun-all)
847 (setq new-alist (funcall (cdr filter) process-alist)))
848 ( ;; apply predicate to each list of attributes
849 (eq (car filter) 'function)
850 (dolist (process process-alist)
851 (if (funcall (car filter) (cdr process))
852 (push process new-alist))))
853 (t ;; apply predicate to specified attribute
854 (let ((fun (if (stringp (cdr filter))
855 `(lambda (val)
856 (string-match ,(cdr filter) val))
857 (cdr filter)))
858 value)
859 (dolist (process process-alist)
860 (setq value (cdr (assq (car filter) (cdr process))))
861 (if (and value (funcall fun value))
862 (push process new-alist))))))
863 (setq process-alist new-alist)))
864 process-alist)
866 (defun proced-filter-interactive (scheme)
867 "Filter Proced buffer using SCHEME.
868 When called interactively, an empty string means nil, i.e., no filtering.
869 Set variable `proced-filter' to SCHEME. Revert listing."
870 (interactive
871 (let ((scheme (completing-read "Filter: "
872 proced-filter-alist nil t)))
873 (list (if (string= "" scheme) nil (intern scheme)))))
874 ;; only update if necessary
875 (unless (eq proced-filter scheme)
876 (setq proced-filter scheme)
877 (proced-update t)))
879 (defun proced-process-tree (process-alist)
880 "Return process tree for PROCESS-ALIST.
881 The process tree is an alist with elements (PPID PID1 PID2 ...).
882 PPID is a parent PID. PID1, PID2, ... are the child processes of PPID.
883 The list of children does not include grandchildren."
884 (let (children-list ppid cpids)
885 (dolist (process process-alist children-list)
886 (setq ppid (cdr (assq 'ppid (cdr process))))
887 (if ppid
888 (setq children-list
889 (if (setq cpids (assq ppid children-list))
890 (cons (cons ppid (cons (car process) (cdr cpids)))
891 (assq-delete-all ppid children-list))
892 (cons (list ppid (car process))
893 children-list)))))))
895 (defun proced-filter-children (process-alist ppid &optional omit-ppid)
896 "For PROCESS-ALIST return list of child processes of PPID.
897 This list includes PPID unless OMIT-PPID is non-nil."
898 (let ((proced-process-tree (proced-process-tree process-alist))
899 new-alist)
900 (dolist (pid (proced-children-pids ppid))
901 (push (assq pid process-alist) new-alist))
902 (if omit-ppid
903 (assq-delete-all ppid new-alist)
904 new-alist)))
906 ;; helper function
907 (defun proced-children-pids (ppid)
908 "Return list of children PIDs of PPID (including PPID)."
909 (let ((cpids (cdr (assq ppid proced-process-tree))))
910 (if cpids
911 (cons ppid (apply 'append (mapcar 'proced-children-pids cpids)))
912 (list ppid))))
914 (defun proced-filter-parents (process-alist pid &optional omit-pid)
915 "For PROCESS-ALIST return list of parent processes of PID.
916 This list includes CPID unless OMIT-CPID is non-nil."
917 (let ((parent-list (unless omit-pid (list (assq pid process-alist)))))
918 (while (setq pid (cdr (assq 'ppid (cdr (assq pid process-alist)))))
919 (push (assq pid process-alist) parent-list))
920 parent-list))
922 ;; Refining
924 ;; Filters are used to select the processes in a new listing.
925 ;; Refiners are used to narrow down further (interactively) the processes
926 ;; in an existing listing.
928 (defun proced-refine (&optional event)
929 "Refine Proced listing by comparing with the attribute value at point.
930 Optional EVENT is the location of the Proced field.
932 Refinement is controlled by the REFINER defined for each attribute ATTR
933 in `proced-grammar-alist'.
935 If REFINER is a list of flags and point is on a process's value of ATTR,
936 this command compares the value of ATTR of every process with the value
937 of ATTR of the process at the position of point.
939 The predicate for the comparison of two ATTR values is defined
940 in `proced-grammar-alist'. For each return value of the predicate
941 a refine flag is defined in `proced-grammar-alist'. One can select
942 processes for which the value of ATTR is \"less than\", \"equal\",
943 and / or \"larger\" than ATTR of the process point is on. A process
944 is included in the new listing if the refine flag for the corresponding
945 return value of the predicate is non-nil.
946 The help-echo string for `proced-refine' uses \"+\" or \"-\" to indicate
947 the current values of these refine flags.
949 If REFINER is a cons pair (FUNCTION . HELP-ECHO), FUNCTION is called
950 with one argument, the PID of the process at the position of point.
951 The function must return a list of PIDs that is used for the refined
952 listing. HELP-ECHO is a string that is shown when mouse is over this field.
954 This command refines an already existing process listing generated initially
955 based on the value of the variable `proced-filter'. It does not change
956 this variable. It does not revert the listing. If you frequently need
957 a certain refinement, consider defining a new filter in `proced-filter-alist'."
958 (interactive (list last-input-event))
959 (if event (posn-set-point (event-end event)))
960 (let ((key (get-text-property (point) 'proced-key))
961 (pid (get-text-property (point) 'proced-pid)))
962 (if (and key pid)
963 (let* ((grammar (assq key proced-grammar-alist))
964 (refiner (nth 7 grammar)))
965 (when refiner
966 (cond ((functionp (car refiner))
967 (setq proced-process-alist (funcall (car refiner) pid)))
968 ((consp refiner)
969 (let ((predicate (nth 4 grammar))
970 (ref (cdr (assq key (cdr (assq pid proced-process-alist)))))
971 val new-alist)
972 (dolist (process proced-process-alist)
973 (setq val (funcall predicate (cdr (assq key (cdr process))) ref))
974 (if (cond ((not val) (nth 2 refiner))
975 ((eq val 'equal) (nth 1 refiner))
976 (val (car refiner)))
977 (push process new-alist)))
978 (setq proced-process-alist new-alist))))
979 ;; Do not revert listing.
980 (proced-update)))
981 (message "No refiner defined here."))))
983 ;; Proced predicates for sorting and filtering are based on a three-valued
984 ;; logic:
985 ;; Predicates take two arguments P1 and P2, the corresponding attribute
986 ;; values of two processes. Predicates should return 'equal if P1 has
987 ;; same rank like P2. Any other non-nil value says that P1 is "less than" P2,
988 ;; or nil if not.
990 (defun proced-< (num1 num2)
991 "Return t if NUM1 less than NUM2.
992 Return `equal' if NUM1 equals NUM2. Return nil if NUM1 greater than NUM2."
993 (if (= num1 num2)
994 'equal
995 (< num1 num2)))
997 (defun proced-string-lessp (s1 s2)
998 "Return t if string S1 is less than S2 in lexicographic order.
999 Return `equal' if S1 and S2 have identical contents.
1000 Return nil otherwise."
1001 (if (string= s1 s2)
1002 'equal
1003 (string-lessp s1 s2)))
1005 (defun proced-time-lessp (t1 t2)
1006 "Return t if time value T1 is less than time value T2.
1007 Return `equal' if T1 equals T2. Return nil otherwise."
1008 (with-decoded-time-value ((high1 low1 micro1 t1)
1009 (high2 low2 micro2 t2))
1010 (cond ((< high1 high2))
1011 ((< high2 high1) nil)
1012 ((< low1 low2))
1013 ((< low2 low1) nil)
1014 ((< micro1 micro2))
1015 ((< micro2 micro1) nil)
1016 (t 'equal))))
1018 ;;; Sorting
1020 (defsubst proced-xor (b1 b2)
1021 "Return the logical exclusive or of args B1 and B2."
1022 (and (or b1 b2)
1023 (not (and b1 b2))))
1025 (defun proced-sort-p (p1 p2)
1026 "Predicate for sorting processes P1 and P2."
1027 (if (not (cdr proced-sort-internal))
1028 ;; only one predicate: fast scheme
1029 (let* ((sorter (car proced-sort-internal))
1030 (k1 (cdr (assq (car sorter) (cdr p1))))
1031 (k2 (cdr (assq (car sorter) (cdr p2)))))
1032 ;; if the attributes are undefined, we should really abort sorting
1033 (if (and k1 k2)
1034 (proced-xor (funcall (nth 1 sorter) k1 k2)
1035 (nth 2 sorter))))
1036 (let ((sort-list proced-sort-internal) sorter predicate k1 k2)
1037 (catch 'done
1038 (while (setq sorter (pop sort-list))
1039 (setq k1 (cdr (assq (car sorter) (cdr p1)))
1040 k2 (cdr (assq (car sorter) (cdr p2)))
1041 predicate
1042 (if (and k1 k2)
1043 (funcall (nth 1 sorter) k1 k2)))
1044 (if (not (eq predicate 'equal))
1045 (throw 'done (proced-xor predicate (nth 2 sorter)))))
1046 (eq t predicate)))))
1048 (defun proced-sort (process-alist sorter descend)
1049 "Sort PROCESS-ALIST using scheme SORTER.
1050 SORTER is a scheme like `proced-sort'.
1051 DESCEND is non-nil if the first element of SORTER is sorted
1052 in descending order.
1053 Return the sorted process list."
1054 ;; translate SORTER into a list of lists (KEY PREDICATE REVERSE)
1055 (setq proced-sort-internal
1056 (mapcar (lambda (arg)
1057 (let ((grammar (assq arg proced-grammar-alist)))
1058 (list arg (nth 4 grammar) (nth 5 grammar))))
1059 (cond ((listp sorter) sorter)
1060 ((and (symbolp sorter)
1061 (nth 6 (assq sorter proced-grammar-alist))))
1062 ((symbolp sorter) (list sorter))
1063 (t (error "Sorter undefined %s" sorter)))))
1064 (if proced-sort-internal
1065 (progn
1066 ;; splice DESCEND into the list
1067 (setcar proced-sort-internal
1068 (list (caar proced-sort-internal)
1069 (nth 1 (car proced-sort-internal)) descend))
1070 (sort process-alist 'proced-sort-p))
1071 process-alist))
1073 (defun proced-sort-interactive (scheme &optional arg)
1074 "Sort Proced buffer using SCHEME.
1075 When called interactively, an empty string means nil, i.e., no sorting.
1077 Prefix ARG controls sort order:
1078 - If prefix ARG is positive (negative), sort in ascending (descending) order.
1079 - If ARG is nil or 'no-arg and SCHEME is equal to the previous sorting scheme,
1080 reverse the sorting order.
1081 - If ARG is nil or 'no-arg and SCHEME differs from the previous sorting scheme,
1082 adopt the sorting order defined for SCHEME in `proced-grammar-alist'.
1084 Set variable `proced-sort' to SCHEME. The current sort scheme is displayed
1085 in the mode line, using \"+\" or \"-\" for ascending or descending order."
1086 (interactive
1087 (let ((scheme (completing-read "Sort attribute: "
1088 proced-grammar-alist nil t)))
1089 (list (if (string= "" scheme) nil (intern scheme))
1090 ;; like 'toggle in `define-derived-mode'
1091 (or current-prefix-arg 'no-arg))))
1093 (setq proced-descend
1094 ;; If `proced-sort-interactive' is called repeatedly for the same
1095 ;; sort key, the sort order is reversed.
1096 (cond ((and (eq arg 'no-arg) (equal proced-sort scheme))
1097 (not proced-descend))
1098 ((eq arg 'no-arg)
1099 (nth 5 (assq (if (consp scheme) (car scheme) scheme)
1100 proced-grammar-alist)))
1101 (arg (< (prefix-numeric-value arg) 0))
1102 ((equal proced-sort scheme)
1103 (not proced-descend))
1104 (t (nth 5 (assq (if (consp scheme) (car scheme) scheme)
1105 proced-grammar-alist))))
1106 proced-sort scheme)
1107 (proced-update))
1109 (defun proced-sort-pcpu (&optional arg)
1110 "Sort Proced buffer by percentage CPU time (%CPU).
1111 Prefix ARG controls sort order, see `proced-sort-interactive'."
1112 (interactive (list (or current-prefix-arg 'no-arg)))
1113 (proced-sort-interactive 'pcpu arg))
1115 (defun proced-sort-pmem (&optional arg)
1116 "Sort Proced buffer by percentage memory usage (%MEM).
1117 Prefix ARG controls sort order, see `proced-sort-interactive'."
1118 (interactive (list (or current-prefix-arg 'no-arg)))
1119 (proced-sort-interactive 'pmem arg))
1121 (defun proced-sort-pid (&optional arg)
1122 "Sort Proced buffer by PID.
1123 Prefix ARG controls sort order, see `proced-sort-interactive'."
1124 (interactive (list (or current-prefix-arg 'no-arg)))
1125 (proced-sort-interactive 'pid arg))
1127 (defun proced-sort-start (&optional arg)
1128 "Sort Proced buffer by time the command started (START).
1129 Prefix ARG controls sort order, see `proced-sort-interactive'."
1130 (interactive (list (or current-prefix-arg 'no-arg)))
1131 (proced-sort-interactive 'start arg))
1133 (defun proced-sort-time (&optional arg)
1134 "Sort Proced buffer by CPU time (TIME).
1135 Prefix ARG controls sort order, see `proced-sort-interactive'."
1136 (interactive (list (or current-prefix-arg 'no-arg)))
1137 (proced-sort-interactive 'time arg))
1139 (defun proced-sort-user (&optional arg)
1140 "Sort Proced buffer by USER.
1141 Prefix ARG controls sort order, see `proced-sort-interactive'."
1142 (interactive (list (or current-prefix-arg 'no-arg)))
1143 (proced-sort-interactive 'user arg))
1145 (defun proced-sort-header (event &optional arg)
1146 "Sort Proced listing based on an attribute.
1147 EVENT is a mouse event with starting position in the header line.
1148 It is converted in the corresponding attribute key.
1149 This command updates the variable `proced-sort'.
1150 Prefix ARG controls sort order, see `proced-sort-interactive'."
1151 (interactive (list last-input-event (or last-prefix-arg 'no-arg)))
1152 (let ((start (event-start event))
1153 col key)
1154 (save-selected-window
1155 (select-window (posn-window start))
1156 (setq col (+ (1- (car (posn-actual-col-row start)))
1157 (window-hscroll)))
1158 (when (and (<= 0 col) (< col (length proced-header-line)))
1159 (setq key (get-text-property col 'proced-key proced-header-line))
1160 (if key
1161 (proced-sort-interactive key arg)
1162 (message "No sorter defined here."))))))
1164 ;;; Formating
1166 (defun proced-format-time (time)
1167 "Format time interval TIME."
1168 (let* ((ftime (float-time time))
1169 (days (truncate ftime 86400))
1170 (ftime (mod ftime 86400))
1171 (hours (truncate ftime 3600))
1172 (ftime (mod ftime 3600))
1173 (minutes (truncate ftime 60))
1174 (seconds (mod ftime 60)))
1175 (cond ((< 0 days)
1176 (format "%d-%02d:%02d:%02d" days hours minutes seconds))
1177 ((< 0 hours)
1178 (format "%02d:%02d:%02d" hours minutes seconds))
1180 (format "%02d:%02d" minutes seconds)))))
1182 (defun proced-format-start (start)
1183 "Format time START.
1184 The return string is always 6 characters wide."
1185 (let ((d-start (decode-time start))
1186 (d-current (decode-time)))
1187 (cond ( ;; process started in previous years
1188 (< (nth 5 d-start) (nth 5 d-current))
1189 (format-time-string " %Y" start))
1190 ;; process started today
1191 ((and (= (nth 3 d-start) (nth 3 d-current))
1192 (= (nth 4 d-start) (nth 4 d-current)))
1193 (format-time-string " %H:%M" start))
1194 (t ;; process started this year
1195 (format-time-string "%b %e" start)))))
1197 (defun proced-format-ttname (ttname)
1198 "Format attribute TTNAME, omitting path \"/dev/\"."
1199 ;; Does this work for all systems?
1200 (substring ttname (if (string-match "\\`/dev/" ttname)
1201 (match-end 0) 0)))
1203 ;; Proced assumes that every process occupies only one line in the listing.
1204 (defun proced-format-args (args)
1205 "Format attribute ARGS.
1206 Replace newline characters by \"^J\" (two characters)."
1207 (replace-regexp-in-string "\n" "^J" args))
1209 (defun proced-format (process-alist format)
1210 "Display PROCESS-ALIST using FORMAT."
1211 (if (symbolp format)
1212 (setq format (cdr (assq format proced-format-alist))))
1214 ;; Not all systems give us all attributes. We take `emacs-pid' as a
1215 ;; representative process PID. If FORMAT contains a list of alternative
1216 ;; attributes, we take the first attribute that is non-nil for `emacs-pid'.
1217 ;; If none of the alternatives is non-nil, the attribute is ignored
1218 ;; in the listing.
1219 (let ((standard-attributes
1220 (car (proced-process-attributes (list (emacs-pid)))))
1221 new-format fmi)
1222 (dolist (fmt format)
1223 (if (symbolp fmt)
1224 (if (assq fmt standard-attributes)
1225 (push fmt new-format))
1226 (while (setq fmi (pop fmt))
1227 (when (assq fmi standard-attributes)
1228 (push fmi new-format)
1229 (setq fmt nil)))))
1230 (setq format (nreverse new-format)))
1232 (insert (make-string (length process-alist) ?\n))
1233 (let ((whitespace " ") (unknown "?")
1234 (sort-key (if (consp proced-sort) (car proced-sort) proced-sort))
1235 header-list grammar)
1236 ;; Loop over all attributes
1237 (while (setq grammar (assq (pop format) proced-grammar-alist))
1238 (let* ((key (car grammar))
1239 (fun (cond ((stringp (nth 2 grammar))
1240 `(lambda (arg) (format ,(nth 2 grammar) arg)))
1241 ((not (nth 2 grammar)) 'identity)
1242 ( t (nth 2 grammar))))
1243 (whitespace (if format whitespace ""))
1244 ;; Text properties:
1245 ;; We use the text property `proced-key' to store in each
1246 ;; field the corresponding key.
1247 ;; Of course, the sort predicate appearing in help-echo
1248 ;; is only part of the story. But it gives the main idea.
1249 (hprops (let ((descend (if (eq key sort-key) proced-descend (nth 5 grammar))))
1250 `(proced-key ,key mouse-face highlight
1251 help-echo ,(format proced-header-help-echo
1252 (if descend "-" "+")
1253 (nth 1 grammar)
1254 (if descend "descending" "ascending")))))
1255 (refiner (nth 7 grammar))
1256 (fprops
1257 (cond ((functionp (car refiner))
1258 `(proced-key ,key mouse-face highlight
1259 help-echo ,(format "mouse-2, RET: %s"
1260 (cdr refiner))))
1261 ((consp refiner)
1262 `(proced-key ,key mouse-face highlight
1263 help-echo ,(format "mouse-2, RET: refine by attribute %s %s"
1264 (nth 1 grammar)
1265 (mapconcat (lambda (s)
1266 (if s "+" "-"))
1267 refiner ""))))))
1268 value)
1270 ;; highlight the header of the sort column
1271 (if (eq key sort-key)
1272 (setq hprops (append '(face proced-sort-header) hprops)))
1273 (goto-char (point-min))
1274 (cond ( ;; fixed width of output field
1275 (numberp (nth 3 grammar))
1276 (dolist (process process-alist)
1277 (end-of-line)
1278 (setq value (cdr (assq key (cdr process))))
1279 (insert (if value
1280 (apply 'propertize (funcall fun value) fprops)
1281 (format (concat "%" (number-to-string (nth 3 grammar)) "s")
1282 unknown))
1283 whitespace)
1284 (forward-line))
1285 (push (format (concat "%" (number-to-string (nth 3 grammar)) "s")
1286 (apply 'propertize (nth 1 grammar) hprops))
1287 header-list))
1289 ( ;; last field left-justified
1290 (and (not format) (eq 'left (nth 3 grammar)))
1291 (dolist (process process-alist)
1292 (end-of-line)
1293 (setq value (cdr (assq key (cdr process))))
1294 (insert (if value (apply 'propertize (funcall fun value) fprops)
1295 unknown))
1296 (forward-line))
1297 (push (apply 'propertize (nth 1 grammar) hprops) header-list))
1299 (t ;; calculated field width
1300 (let ((width (length (nth 1 grammar)))
1301 field-list value)
1302 (dolist (process process-alist)
1303 (setq value (cdr (assq key (cdr process))))
1304 (if value
1305 (setq value (apply 'propertize (funcall fun value) fprops)
1306 width (max width (length value))
1307 field-list (cons value field-list))
1308 (push unknown field-list)
1309 (setq width (max width (length unknown)))))
1310 (let ((afmt (concat "%" (if (eq 'left (nth 3 grammar)) "-" "")
1311 (number-to-string width) "s")))
1312 (push (format afmt (apply 'propertize (nth 1 grammar) hprops))
1313 header-list)
1314 (dolist (value (nreverse field-list))
1315 (end-of-line)
1316 (insert (format afmt value) whitespace)
1317 (forward-line))))))))
1319 ;; final cleanup
1320 (goto-char (point-min))
1321 (dolist (process process-alist)
1322 ;; We use the text property `proced-pid' to store in each line
1323 ;; the corresponding pid
1324 (put-text-property (point) (line-end-position) 'proced-pid (car process))
1325 (forward-line))
1326 ;; Set header line
1327 (setq proced-header-line
1328 (mapconcat 'identity (nreverse header-list) whitespace))
1329 (if (string-match "[ \t]+$" proced-header-line)
1330 (setq proced-header-line (substring proced-header-line 0
1331 (match-beginning 0))))
1332 ;; (delete-trailing-whitespace)
1333 (goto-char (point-min))
1334 (while (re-search-forward "[ \t\r]+$" nil t)
1335 (delete-region (match-beginning 0) (match-end 0)))))
1337 (defun proced-format-interactive (scheme &optional revert)
1338 "Format Proced buffer using SCHEME.
1339 When called interactively, an empty string means nil, i.e., no formatting.
1340 Set variable `proced-format' to SCHEME.
1341 With prefix REVERT non-nil revert listing."
1342 (interactive
1343 (let ((scheme (completing-read "Format: "
1344 proced-format-alist nil t)))
1345 (list (if (string= "" scheme) nil (intern scheme))
1346 current-prefix-arg)))
1347 ;; only update if necessary
1348 (when (or (not (eq proced-format scheme)) revert)
1349 (setq proced-format scheme)
1350 (proced-update revert)))
1352 ;; generate listing
1354 (defun proced-process-attributes (&optional pid-list)
1355 "Return alist of attributes for each system process.
1356 This alist can be customized via `proced-custom-attributes'.
1357 Optional arg PID-LIST is a list of PIDs of system process that are analyzed.
1358 If no attributes are known for a process (possibly because it already died)
1359 the process is ignored."
1360 ;; Should we make it customizable whether processes with empty attribute
1361 ;; lists are ignored? When would such processes be of interest?
1362 (let (process-alist attributes)
1363 (dolist (pid (or pid-list (list-system-processes)) process-alist)
1364 (when (setq attributes (system-process-attributes pid))
1365 (let ((utime (cdr (assq 'utime attributes)))
1366 (stime (cdr (assq 'stime attributes)))
1367 (cutime (cdr (assq 'cutime attributes)))
1368 (cstime (cdr (assq 'cstime attributes)))
1369 attr)
1370 (setq attributes
1371 (append (list (cons 'pid pid))
1372 (if (and utime stime)
1373 (list (cons 'time (time-add utime stime))))
1374 (if (and cutime cstime)
1375 (list (cons 'ctime (time-add cutime cstime))))
1376 attributes))
1377 (dolist (fun proced-custom-attributes)
1378 (if (setq attr (funcall fun attributes))
1379 (push attr attributes)))
1380 (push (cons pid attributes) process-alist))))))
1382 (defun proced-update (&optional revert quiet)
1383 "Update the `proced' process information. Preserves point and marks.
1384 With prefix REVERT non-nil, revert listing.
1385 Suppress status information if QUIET is nil."
1386 ;; This is the main function that generates and updates the process listing.
1387 (interactive "P")
1388 (setq revert (or revert (not proced-process-alist)))
1389 (or quiet (message (if revert "Updating process information..."
1390 "Updating process display...")))
1391 (if revert ;; evaluate all processes
1392 (setq proced-process-alist (proced-process-attributes)))
1393 ;; filtering and sorting
1394 (setq proced-process-alist
1395 (proced-sort (proced-filter proced-process-alist proced-filter)
1396 proced-sort proced-descend))
1398 ;; It is useless to keep undo information if we revert, filter, or
1399 ;; refine the listing so that `proced-process-alist' has changed.
1400 ;; We could keep the undo information if we only re-sort the buffer.
1401 ;; Would that be useful? Re-re-sorting is easy, too.
1402 (if (consp buffer-undo-list)
1403 (setq buffer-undo-list nil))
1404 (let ((buffer-undo-list t)
1405 ;; If point is on a field, we try to return point to that field.
1406 ;; Otherwise we try to return to the same column
1407 (old-pos (let ((pid (proced-pid-at-point))
1408 (key (get-text-property (point) 'proced-key)))
1409 (list pid key ; can both be nil
1410 (if key
1411 (if (get-text-property (1- (point)) 'proced-key)
1412 (- (point) (previous-single-property-change
1413 (point) 'proced-key))
1415 (current-column)))))
1416 buffer-read-only mp-list)
1417 ;; remember marked processes (whatever the mark was)
1418 (goto-char (point-min))
1419 (while (re-search-forward "^\\(\\S-\\)" nil t)
1420 (push (cons (save-match-data (proced-pid-at-point))
1421 (match-string-no-properties 1)) mp-list))
1423 ;; generate listing
1424 (erase-buffer)
1425 (proced-format proced-process-alist proced-format)
1426 (goto-char (point-min))
1427 (while (not (eobp))
1428 (insert " ")
1429 (forward-line))
1430 (setq proced-header-line (concat " " proced-header-line))
1431 (if revert (set-buffer-modified-p nil))
1433 ;; set `goal-column'
1434 (let ((grammar (assq proced-goal-attribute proced-grammar-alist)))
1435 (setq goal-column ;; set to nil if no match
1436 (if (and grammar
1437 (not (zerop (buffer-size)))
1438 (string-match (regexp-quote (nth 1 grammar))
1439 proced-header-line))
1440 (if (nth 3 grammar)
1441 (match-beginning 0)
1442 (match-end 0)))))
1444 ;; Restore process marks and buffer position (if possible).
1445 ;; Sometimes this puts point in the middle of the proced buffer
1446 ;; where it is not interesting. Is there a better / more flexible solution?
1447 (goto-char (point-min))
1448 (let (pid mark new-pos)
1449 (if (or mp-list (car old-pos))
1450 (while (not (eobp))
1451 (setq pid (proced-pid-at-point))
1452 (when (setq mark (assq pid mp-list))
1453 (insert (cdr mark))
1454 (delete-char 1)
1455 (beginning-of-line))
1456 (when (eq (car old-pos) pid)
1457 (if (nth 1 old-pos)
1458 (let ((limit (line-end-position)) pos)
1459 (while (and (not new-pos)
1460 (setq pos (next-property-change (point) nil limit)))
1461 (goto-char pos)
1462 (when (eq (nth 1 old-pos)
1463 (get-text-property (point) 'proced-key))
1464 (forward-char (min (nth 2 old-pos)
1465 (- (next-property-change (point))
1466 (point))))
1467 (setq new-pos (point))))
1468 (unless new-pos
1469 ;; we found the process, but the field of point
1470 ;; is not listed anymore
1471 (setq new-pos (proced-move-to-goal-column))))
1472 (setq new-pos (min (+ (line-beginning-position) (nth 2 old-pos))
1473 (line-end-position)))))
1474 (forward-line)))
1475 (if new-pos
1476 (goto-char new-pos)
1477 (goto-char (point-min))
1478 (proced-move-to-goal-column)))
1479 ;; update modeline
1480 ;; Does the long `mode-name' clutter the modeline? It would be nice
1481 ;; to have some other location for displaying the values of the various
1482 ;; flags that affect the behavior of proced (flags one might want
1483 ;; to change on the fly). Where??
1484 (setq mode-name
1485 (concat "Proced"
1486 (if proced-filter
1487 (concat ": " (symbol-name proced-filter))
1489 (if proced-sort
1490 (let* ((key (if (consp proced-sort) (car proced-sort)
1491 proced-sort))
1492 (grammar (assq key proced-grammar-alist)))
1493 (concat " by " (if proced-descend "-" "+")
1494 (nth 1 grammar)))
1495 "")))
1496 (force-mode-line-update)
1497 ;; done
1498 (or quiet (input-pending-p)
1499 (message (if revert "Updating process information...done."
1500 "Updating process display...done.")))))
1502 (defun proced-revert (&rest args)
1503 "Reevaluate the process listing based on the currently running processes.
1504 Preserves point and marks."
1505 (proced-update t))
1507 ;; I do not want to reinvent the wheel. Should we rename `dired-pop-to-buffer'
1508 ;; and move it to window.el so that proced and ibuffer can easily use it, too?
1509 ;; What about functions like `appt-disp-window' that use
1510 ;; `shrink-window-if-larger-than-buffer'?
1511 (autoload 'dired-pop-to-buffer "dired")
1513 (defun proced-send-signal (&optional signal)
1514 "Send a SIGNAL to the marked processes.
1515 If no process is marked, operate on current process.
1516 SIGNAL may be a string (HUP, INT, TERM, etc.) or a number.
1517 If SIGNAL is nil display marked processes and query interactively for SIGNAL."
1518 (interactive)
1519 (let ((regexp (proced-marker-regexp))
1520 process-alist)
1521 ;; collect marked processes
1522 (save-excursion
1523 (goto-char (point-min))
1524 (while (re-search-forward regexp nil t)
1525 (push (cons (proced-pid-at-point)
1526 ;; How much info should we collect here?
1527 (substring (match-string-no-properties 0) 2))
1528 process-alist)))
1529 (setq process-alist
1530 (if process-alist
1531 (nreverse process-alist)
1532 ;; take current process
1533 (list (cons (proced-pid-at-point)
1534 (buffer-substring-no-properties
1535 (+ 2 (line-beginning-position))
1536 (line-end-position))))))
1537 (unless signal
1538 ;; Display marked processes (code taken from `dired-mark-pop-up').
1539 (let ((bufname " *Marked Processes*")
1540 (header-line (substring-no-properties proced-header-line)))
1541 (with-current-buffer (get-buffer-create bufname)
1542 (setq truncate-lines t
1543 proced-header-line header-line ; inherit header line
1544 header-line-format '(:eval (proced-header-line)))
1545 (add-hook 'post-command-hook 'force-mode-line-update nil t)
1546 (erase-buffer)
1547 (dolist (process process-alist)
1548 (insert " " (cdr process) "\n"))
1549 (save-window-excursion
1550 (dired-pop-to-buffer bufname) ; all we need
1551 (let* ((completion-ignore-case t)
1552 (pnum (if (= 1 (length process-alist))
1553 "1 process"
1554 (format "%d processes" (length process-alist))))
1555 ;; The following is an ugly hack. Is there a better way
1556 ;; to help people like me to remember the signals and
1557 ;; their meanings?
1558 (tmp (completing-read (concat "Send signal [" pnum
1559 "] (default TERM): ")
1560 proced-signal-list
1561 nil nil nil nil "TERM")))
1562 (setq signal (if (string-match "^\\(\\S-+\\)\\s-" tmp)
1563 (match-string 1 tmp) tmp))))))
1564 ;; send signal
1565 (let ((count 0)
1566 failures)
1567 ;; Why not always use `signal-process'? See
1568 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-03/msg02955.html
1569 (if (functionp proced-signal-function)
1570 ;; use built-in `signal-process'
1571 (let ((signal (if (stringp signal)
1572 (if (string-match "\\`[0-9]+\\'" signal)
1573 (string-to-number signal)
1574 (make-symbol signal))
1575 signal))) ; number
1576 (dolist (process process-alist)
1577 (condition-case err
1578 (if (zerop (funcall
1579 proced-signal-function (car process) signal))
1580 (setq count (1+ count))
1581 (proced-log "%s\n" (cdr process))
1582 (push (cdr process) failures))
1583 (error ;; catch errors from failed signals
1584 (proced-log "%s\n" err)
1585 (proced-log "%s\n" (cdr process))
1586 (push (cdr process) failures)))))
1587 ;; use external system call
1588 (let ((signal (concat "-" (if (numberp signal)
1589 (number-to-string signal) signal))))
1590 (dolist (process process-alist)
1591 (with-temp-buffer
1592 (condition-case err
1593 (if (zerop (call-process
1594 proced-signal-function nil t nil
1595 signal (number-to-string (car process))))
1596 (setq count (1+ count))
1597 (proced-log (current-buffer))
1598 (proced-log "%s\n" (cdr process))
1599 (push (cdr process) failures))
1600 (error ;; catch errors from failed signals
1601 (proced-log (current-buffer))
1602 (proced-log "%s\n" (cdr process))
1603 (push (cdr process) failures)))))))
1604 (if failures
1605 ;; Proced error message are not always very precise.
1606 ;; Can we issue a useful one-line summary in the
1607 ;; message area (using FAILURES) if only one signal failed?
1608 (proced-log-summary
1609 signal
1610 (format "%d of %d signal%s failed"
1611 (length failures) (length process-alist)
1612 (if (= 1 (length process-alist)) "" "s")))
1613 (proced-success-message "Sent signal to" count)))
1614 ;; final clean-up
1615 (run-hooks 'proced-after-send-signal-hook))))
1617 ;; similar to `dired-why'
1618 (defun proced-why ()
1619 "Pop up a buffer with error log output from Proced.
1620 A group of errors from a single command ends with a formfeed.
1621 Thus, use \\[backward-page] to find the beginning of a group of errors."
1622 (interactive)
1623 (if (get-buffer proced-log-buffer)
1624 (save-selected-window
1625 ;; move `proced-log-buffer' to the front of the buffer list
1626 (select-window (display-buffer (get-buffer proced-log-buffer)))
1627 (setq truncate-lines t)
1628 (set-buffer-modified-p nil)
1629 (setq buffer-read-only t)
1630 (goto-char (point-max))
1631 (forward-line -1)
1632 (backward-page 1)
1633 (recenter 0))))
1635 ;; similar to `dired-log'
1636 (defun proced-log (log &rest args)
1637 "Log a message or the contents of a buffer.
1638 If LOG is a string and there are more args, it is formatted with
1639 those ARGS. Usually the LOG string ends with a \\n.
1640 End each bunch of errors with (proced-log t signal):
1641 this inserts the current time, buffer and signal at the start of the page,
1642 and \f (formfeed) at the end."
1643 (let ((obuf (current-buffer)))
1644 (with-current-buffer (get-buffer-create proced-log-buffer)
1645 (goto-char (point-max))
1646 (let (buffer-read-only)
1647 (cond ((stringp log)
1648 (insert (if args
1649 (apply 'format log args)
1650 log)))
1651 ((bufferp log)
1652 (insert-buffer-substring log))
1653 ((eq t log)
1654 (backward-page 1)
1655 (unless (bolp)
1656 (insert "\n"))
1657 (insert (current-time-string)
1658 "\tBuffer `" (buffer-name obuf) "', "
1659 (format "signal `%s'\n" (car args)))
1660 (goto-char (point-max))
1661 (insert "\f\n")))))))
1663 ;; similar to `dired-log-summary'
1664 (defun proced-log-summary (signal string)
1665 "State a summary of SIGNAL's failures, in echo area and log buffer.
1666 STRING is an overall summary of the failures."
1667 (message "Signal %s: %s--type ? for details" signal string)
1668 ;; Log a summary describing a bunch of errors.
1669 (proced-log (concat "\n" string "\n"))
1670 (proced-log t signal))
1672 (defun proced-help ()
1673 "Provide help for the `proced' user."
1674 (interactive)
1675 (proced-why)
1676 (if (eq last-command 'proced-help)
1677 (describe-mode)
1678 (message proced-help-string)))
1680 (defun proced-undo ()
1681 "Undo in a proced buffer.
1682 This doesn't recover killed processes, it just undoes changes in the proced
1683 buffer. You can use it to recover marks."
1684 (interactive)
1685 (let (buffer-read-only)
1686 (undo))
1687 (message "Change in Proced buffer undone.
1688 Killed processes cannot be recovered by Emacs."))
1690 (provide 'proced)
1692 ;; arch-tag: a6e312ad-9032-45aa-972d-31a8cfc545af
1693 ;;; proced.el ends here