Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 152 lines (127 sloc) 4.944 kB
e6f32cf @thierryvolpiatto * helm-sys.el: new
thierryvolpiatto authored
1 ;;; helm-sys.el --- System related functions for helm.
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
20 (eval-when-compile (require 'cl))
21 (require 'helm)
22
23
24 ;;; Top (process)
25 (defvar helm-c-top-command "COLUMNS=%s top -b -n 1"
26 "Top command (batch mode). %s is replaced with `frame-width'.")
27 (defvar helm-c-source-top
28 '((name . "Top (Press C-c C-u to refresh)")
29 (init . helm-c-top-init)
30 (candidates-in-buffer)
31 (display-to-real . helm-c-top-display-to-real)
32 (persistent-action . helm-c-top-sh-persistent-action)
33 (persistent-help . "SIGTERM")
34 (action
35 ("kill (TERM)" . (lambda (pid)
36 (helm-c-top-sh (format "kill -TERM %s" pid))))
37 ("kill (KILL)" . (lambda (pid)
38 (helm-c-top-sh (format "kill -KILL %s" pid))))
39 ("Copy PID" . (lambda (pid) (kill-new pid))))))
40
41 (defun helm-c-top-sh (cmd)
42 (message "Executed %s\n%s" cmd (shell-command-to-string cmd)))
43
44 (defun helm-c-top-sh-persistent-action (pid)
45 (delete-other-windows)
46 (helm-c-top-sh (format "kill -TERM %s" pid))
47 (helm-force-update))
48
49 (defun helm-c-top-init ()
50 (with-current-buffer (helm-candidate-buffer 'global)
51 (call-process-shell-command
2adcd43 @thierryvolpiatto * helm.el: Remove all digit shortcut code.
thierryvolpiatto authored
52 (format helm-c-top-command (frame-width))
e6f32cf @thierryvolpiatto * helm-sys.el: new
thierryvolpiatto authored
53 nil (current-buffer))))
54
55 (defun helm-c-top-display-to-real (line)
56 (car (split-string line)))
57
58 ;;; X RandR resolution change
59 ;;
60 ;;
61 ;;; FIXME I do not care multi-display.
62
63 (defun helm-c-xrandr-info ()
64 "Return a pair with current X screen number and current X display name."
65 (with-temp-buffer
66 (call-process "xrandr" nil (current-buffer) nil
67 "--current")
68 (let (screen output)
69 (goto-char (point-min))
70 (save-excursion
71 (when (re-search-forward "\\(^Screen \\)\\([0-9]\\):" nil t)
72 (setq screen (match-string 2))))
73 (when (re-search-forward "^\\(.*\\) connected" nil t)
74 (setq output (match-string 1)))
75 (list screen output))))
76
77 (defun helm-c-xrandr-screen ()
78 "Return current X screen number."
79 (car (helm-c-xrandr-info)))
80
81 (defun helm-c-xrandr-output ()
82 "Return current X display name."
83 (cadr (helm-c-xrandr-info)))
84
85 (defvar helm-c-source-xrandr-change-resolution
86 '((name . "Change Resolution")
87 (candidates
88 . (lambda ()
89 (with-temp-buffer
90 (call-process "xrandr" nil (current-buffer) nil
91 "--screen" (helm-c-xrandr-screen) "-q")
92 (goto-char 1)
93 (loop with modes = nil
94 while (re-search-forward " \\([0-9]+x[0-9]+\\)" nil t)
95 for mode = (match-string 1)
96 unless (member mode modes)
97 collect mode into modes
98 finally return modes))))
99 (action
100 ("Change Resolution"
101 . (lambda (mode)
102 (call-process "xrandr" nil nil nil
103 "--screen" (helm-c-xrandr-screen)
104 "--output" (helm-c-xrandr-output)
105 "--mode" mode))))))
106
107 ;;; Emacs process
108 ;;
109 ;;
110 (defvar helm-c-source-emacs-process
111 '((name . "Emacs Process")
112 (candidates . (lambda () (mapcar #'process-name (process-list))))
113 (persistent-action . (lambda (elm)
114 (delete-process (get-process elm))
115 (helm-delete-current-selection)))
116 (persistent-help . "Kill Process")
117 (action ("Kill Process" . (lambda (elm)
118 (delete-process (get-process elm)))))))
119
120 ;;;###autoload
121 (defun helm-top ()
122 "Preconfigured `helm' for top command."
123 (interactive)
124 (let ((helm-samewindow t)
125 (helm-display-function 'helm-default-display-buffer)
126 (helm-candidate-number-limit 9999))
127 (save-window-excursion
128 (delete-other-windows)
129 (helm-other-buffer 'helm-c-source-top "*helm top*"))))
130
131 ;;;###autoload
132 (defun helm-list-emacs-process ()
133 "Preconfigured `helm' for emacs process."
134 (interactive)
135 (helm-other-buffer 'helm-c-source-emacs-process "*helm process*"))
136
137 ;;;###autoload
138 (defun helm-xrandr-set ()
139 (interactive)
140 (helm :sources 'helm-c-source-xrandr-change-resolution
141 :buffer "*helm xrandr*"))
142
143 (provide 'helm-sys)
144
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
145 ;; Local Variables:
146 ;; coding: utf-8
147 ;; indent-tabs-mode: nil
148 ;; byte-compile-dynamic: t
149 ;; End:
150
e6f32cf @thierryvolpiatto * helm-sys.el: new
thierryvolpiatto authored
151 ;;; helm-sys.el ends here
Something went wrong with that request. Please try again.