From 94b7639bef15f591e1d57666a0c2d03b477a6eb0 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Thu, 22 Mar 2012 14:38:59 +0100 Subject: [PATCH] * helm-config.el: Move apt to *apt.el. * helm-apt.el: new file. --- helm-apt.el | 169 +++++++++++++++++++++++++++++++++++++++++++++++++ helm-config.el | 151 +------------------------------------------ 2 files changed, 171 insertions(+), 149 deletions(-) create mode 100644 helm-apt.el diff --git a/helm-apt.el b/helm-apt.el new file mode 100644 index 000000000..af7506d48 --- /dev/null +++ b/helm-apt.el @@ -0,0 +1,169 @@ +;;; helm-apt.el --- Helm interface for Debian/Ubuntu packages (apt-*) + +;; Copyright (C) 2012 Thierry Volpiatto + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: + +(eval-when-compile (require 'cl)) +(require 'helm) +(require 'helm-utils) + +(defvar helm-c-source-apt + '((name . "APT") + (init . helm-c-apt-init) + (candidates-in-buffer) + (candidate-transformer helm-c-apt-candidate-transformer) + (display-to-real . helm-c-apt-display-to-real) + (requires-pattern . 2) + (update . helm-c-apt-refresh) + (action + ("Show package description" . helm-c-apt-cache-show) + ("Install package" . helm-c-apt-install) + ("Reinstall package" . helm-c-apt-reinstall) + ("Remove package" . helm-c-apt-uninstall) + ("Purge package" . helm-c-apt-purge)) + (persistent-action . helm-c-apt-persistent-action) + (persistent-help . "Show package description"))) + +(defvar helm-c-apt-query "emacs") +(defvar helm-c-apt-search-command "apt-cache search '%s'") +(defvar helm-c-apt-show-command "apt-cache show '%s'") +(defvar helm-c-apt-installed-packages nil) +(defvar helm-c-apt-all-packages nil) +(defvar helm-c-apt-input-history nil) + +(defun helm-c-apt-refresh () + "Refresh installed candidates list." + (setq helm-c-apt-installed-packages nil) + (setq helm-c-apt-all-packages nil)) + +(defun helm-c-apt-persistent-action (candidate) + "Persistent action for APT source." + (helm-c-apt-cache-show candidate)) + +(defun helm-c-apt-candidate-transformer (candidates) + "Show installed CANDIDATES and the ones to deinstall in a different color." + (loop for cand in candidates + for name = (helm-c-apt-display-to-real cand) + collect (cond ((string= (assoc-default + name helm-c-apt-installed-packages) + "deinstall") + (propertize cand 'face 'helm-apt-deinstalled)) + ((string= (assoc-default + name helm-c-apt-installed-packages) + "install") + (propertize cand 'face 'helm-apt-installed)) + (t cand)))) + +(defun helm-c-apt-init () + "Initialize list of debian packages." + (let ((query "")) + (unless (and helm-c-apt-installed-packages + helm-c-apt-all-packages) + (message "Loading package list...") + (setq helm-c-apt-installed-packages + (with-temp-buffer + (call-process-shell-command "dpkg --get-selections" + nil (current-buffer)) + (loop for i in (split-string (buffer-string) "\n" t) + for p = (split-string i) + collect (cons (car p) (cadr p))))) + (setq helm-c-apt-all-packages + (with-current-buffer + (helm-candidate-buffer + (get-buffer-create (format "*helm-apt*"))) + (erase-buffer) + (call-process-shell-command + (format helm-c-apt-search-command query) + nil (current-buffer)))) + (message "Loading package list done") + (sit-for 0.5)))) + +(defun helm-c-apt-display-to-real (line) + "Return only name of a debian package. +LINE is displayed like: +package name - description." + (car (split-string line " - "))) + +(defun helm-c-shell-command-if-needed (command) + "Run shell command COMMAND to describe package. +If a buffer named COMMAND already exists, just switch to it." + (let ((buf (get-buffer command))) + (helm-c-switch-to-buffer (get-buffer-create command)) + (unless buf (insert (shell-command-to-string command))))) + +(defun helm-c-apt-cache-show (package) + "Show information on apt package PACKAGE." + (helm-c-shell-command-if-needed + (format helm-c-apt-show-command package))) + +(defun helm-c-apt-install (package) + "Run 'apt-get install' shell command on PACKAGE." + (helm-c-apt-generic-action :action 'install)) + +(defun helm-c-apt-reinstall (package) + "Run 'apt-get install --reinstall' shell command on PACKAGE." + (helm-c-apt-generic-action :action 'reinstall)) + +(defun helm-c-apt-uninstall (package) + "Run 'apt-get remove' shell command on PACKAGE." + (helm-c-apt-generic-action :action 'uninstall)) + +(defun helm-c-apt-purge (package) + "Run 'apt-get purge' shell command on PACKAGE." + (helm-c-apt-generic-action :action 'purge)) + +(defun* helm-c-apt-generic-action (&key action) + "Run 'apt-get ACTION'. +Support install, remove and purge actions." + (ansi-term (getenv "SHELL") "helm apt") + (term-line-mode) + (let ((command (case action + ('install "sudo apt-get install ") + ('reinstall "sudo apt-get install --reinstall ") + ('uninstall "sudo apt-get remove ") + ('purge "sudo apt-get purge ") + (t (error "Unknow action")))) + (beg (point)) + end + (cand-list (mapconcat #'(lambda (x) (format "'%s'" x)) + (helm-marked-candidates) " "))) + (goto-char (point-max)) + (insert (concat command cand-list)) + (setq end (point)) + (if (y-or-n-p (format "%s package" (symbol-name action))) + (progn + (setq helm-c-external-commands-list nil) + (setq helm-c-apt-installed-packages nil) + (term-char-mode) (term-send-input)) + (delete-region beg end) (term-send-eof) (kill-buffer)))) + +;;;###autoload +(defun helm-apt (arg) + "Preconfigured `helm' : frontend of APT package manager. +With a prefix arg reload cache." + (interactive "P") + (let ((query (read-string "Search Package: " nil 'helm-c-apt-input-history))) + (when arg (helm-c-apt-refresh)) + (helm :sources 'helm-c-source-apt + :prompt "Search Package: " + :input query + :history 'helm-c-apt-input-history))) + + +(provide 'helm-apt) + +;;; helm-apt.el ends here diff --git a/helm-config.el b/helm-config.el index b8838e760..0423c81a9 100644 --- a/helm-config.el +++ b/helm-config.el @@ -38,6 +38,8 @@ (require 'helm-eval) (require 'helm-tags) (require 'helm-adaptative) +(require 'helm-apt) +(require 'helm-gentoo) (eval-when-compile (require 'org)) ; Shut up byte compiler about org-directory. (eval-when-compile (require 'semantic nil t)) (require 'helm-match-plugin) @@ -2905,143 +2907,6 @@ Only math* symbols are collected." (display-time-world-display display-time-world-list))))) (candidates-in-buffer))) - - -;;; Helm interface for Debian/Ubuntu packages (apt-*) -;; -;; -(defvar helm-c-source-apt - '((name . "APT") - (init . helm-c-apt-init) - (candidates-in-buffer) - (candidate-transformer helm-c-apt-candidate-transformer) - (display-to-real . helm-c-apt-display-to-real) - (requires-pattern . 2) - (update . helm-c-apt-refresh) - (action - ("Show package description" . helm-c-apt-cache-show) - ("Install package" . helm-c-apt-install) - ("Reinstall package" . helm-c-apt-reinstall) - ("Remove package" . helm-c-apt-uninstall) - ("Purge package" . helm-c-apt-purge)) - (persistent-action . helm-c-apt-persistent-action) - (persistent-help . "Show package description"))) - -(defvar helm-c-apt-query "emacs") -(defvar helm-c-apt-search-command "apt-cache search '%s'") -(defvar helm-c-apt-show-command "apt-cache show '%s'") -(defvar helm-c-apt-installed-packages nil) -(defvar helm-c-apt-all-packages nil) -(defvar helm-c-apt-input-history nil) - -(defun helm-c-apt-refresh () - "Refresh installed candidates list." - (setq helm-c-apt-installed-packages nil) - (setq helm-c-apt-all-packages nil)) - -(defun helm-c-apt-persistent-action (candidate) - "Persistent action for APT source." - (helm-c-apt-cache-show candidate)) - -(defun helm-c-apt-candidate-transformer (candidates) - "Show installed CANDIDATES and the ones to deinstall in a different color." - (loop for cand in candidates - for name = (helm-c-apt-display-to-real cand) - collect (cond ((string= (assoc-default - name helm-c-apt-installed-packages) - "deinstall") - (propertize cand 'face 'helm-apt-deinstalled)) - ((string= (assoc-default - name helm-c-apt-installed-packages) - "install") - (propertize cand 'face 'helm-apt-installed)) - (t cand)))) - -(defun helm-c-apt-init () - "Initialize list of debian packages." - (let ((query "")) - (unless (and helm-c-apt-installed-packages - helm-c-apt-all-packages) - (message "Loading package list...") - (setq helm-c-apt-installed-packages - (with-temp-buffer - (call-process-shell-command "dpkg --get-selections" - nil (current-buffer)) - (loop for i in (split-string (buffer-string) "\n" t) - for p = (split-string i) - collect (cons (car p) (cadr p))))) - (setq helm-c-apt-all-packages - (with-current-buffer - (helm-candidate-buffer - (get-buffer-create (format "*helm-apt*"))) - (erase-buffer) - (call-process-shell-command - (format helm-c-apt-search-command query) - nil (current-buffer)))) - (message "Loading package list done") - (sit-for 0.5)))) - -(defun helm-c-apt-display-to-real (line) - "Return only name of a debian package. -LINE is displayed like: -package name - description." - (car (split-string line " - "))) - -(defun helm-c-shell-command-if-needed (command) - "Run shell command COMMAND to describe package. -If a buffer named COMMAND already exists, just switch to it." - (let ((buf (get-buffer command))) - (helm-c-switch-to-buffer (get-buffer-create command)) - (unless buf (insert (shell-command-to-string command))))) - -(defun helm-c-apt-cache-show (package) - "Show information on apt package PACKAGE." - (helm-c-shell-command-if-needed - (format helm-c-apt-show-command package))) - -(defun helm-c-apt-install (package) - "Run 'apt-get install' shell command on PACKAGE." - (helm-c-apt-generic-action :action 'install)) - -(defun helm-c-apt-reinstall (package) - "Run 'apt-get install --reinstall' shell command on PACKAGE." - (helm-c-apt-generic-action :action 'reinstall)) - -(defun helm-c-apt-uninstall (package) - "Run 'apt-get remove' shell command on PACKAGE." - (helm-c-apt-generic-action :action 'uninstall)) - -(defun helm-c-apt-purge (package) - "Run 'apt-get purge' shell command on PACKAGE." - (helm-c-apt-generic-action :action 'purge)) - -(defun* helm-c-apt-generic-action (&key action) - "Run 'apt-get ACTION'. -Support install, remove and purge actions." - (ansi-term (getenv "SHELL") "helm apt") - (term-line-mode) - (let ((command (case action - ('install "sudo apt-get install ") - ('reinstall "sudo apt-get install --reinstall ") - ('uninstall "sudo apt-get remove ") - ('purge "sudo apt-get purge ") - (t (error "Unknow action")))) - (beg (point)) - end - (cand-list (mapconcat #'(lambda (x) (format "'%s'" x)) - (helm-marked-candidates) " "))) - (goto-char (point-max)) - (insert (concat command cand-list)) - (setq end (point)) - (if (y-or-n-p (format "%s package" (symbol-name action))) - (progn - (setq helm-c-external-commands-list nil) - (setq helm-c-apt-installed-packages nil) - (term-char-mode) (term-send-input)) - (delete-region beg end) (term-send-eof) (kill-buffer)))) - -;; (helm-c-apt-install "jed") - ;;; Helm ratpoison UI ;; @@ -4000,18 +3865,6 @@ See also `helm-create--actions'." (interactive) (helm-other-buffer 'helm-c-source-time-world "*helm world time*")) -;;;###autoload -(defun helm-apt (arg) - "Preconfigured `helm' : frontend of APT package manager. -With a prefix arg reload cache." - (interactive "P") - (let ((query (read-string "Search Package: " nil 'helm-c-apt-input-history))) - (when arg (helm-c-apt-refresh)) - (helm :sources 'helm-c-source-apt - :prompt "Search Package: " - :input query - :history 'helm-c-apt-input-history))) - ;;;###autoload (defun helm-c-run-external-command (program) "Preconfigured `helm' to run External PROGRAM asyncronously from Emacs.