;;; outside.el ;;; ;;; Copyright (C) 2007, 2008 Thien-Thi Nguyen ;;; ;;; This file is part of ttn's personal elisp library, released under ;;; the terms of the GNU General Public License as published by the ;;; Free Software Foundation; either version 3, or (at your option) any ;;; later version. There is NO WARRANTY. See file COPYING for details. ;;; ;;; Description: Interact w/ the world outside Emacs. ;;;###autoload (defun rewrite-shell-command-w/cd (string) "If STRING begins w/ \"$foo\", return a new string. In the new string, the \"$foo\" is replaced with \"cd $foo\", and furthermore \"$foo\" is replaced with the value of `(getenv \"foo\")'. For example, if (getenv \"hack\") => \"~/build/hack\", then (rewrite-shell-command-w/cd \"$hack.b ; echo $hack\") => \"cd ~/build/hack.b ; echo $hack\" For other values of STRING, simply return it." (save-match-data (if (string-match "^[$]\\([a-zA-Z_]+\\)" string) (replace-match (concat "cd " (getenv (match-string 1 string))) t t string) string))) ;;;###autoload (defun bg-shell-command (cmd &optional watch) "Do shell CMD in background, renaming controlling buffer. CMD is passed through `rewrite-shell-command-w/cd'. Prefix arg WATCH non-nil means switch to that buffer, wait 0.1s for output and move to the beginning of the buffer." (interactive "sShell command (to be backgrounded): \nP") (let (buf) (save-window-excursion (shell-command (concat (rewrite-shell-command-w/cd cmd) "&")) (set-buffer "*Async Shell Command*") (setq buf (rename-buffer (concat "*bg job* " cmd) t))) (when watch (switch-to-buffer buf) (sit-for 0) (accept-process-output (get-buffer-process buf) 0 100 t) (goto-char (point-min))))) ;;;###autoload (defun saved-shell-command (cmd) "Does a `shell-command', renames buffer, optionally kills it. CMD is passed through `rewrite-shell-command-w/cd'." ;;; Thanks go to gsri for suggesting scrolling. (interactive "sShell command: ") (let ((n 0) (newname (concat "*shell* " cmd)) (oldw (selected-window)) (numlines 0) (window-min-height 1)) ; override Emacs (shell-command (rewrite-shell-command-w/cd cmd)) (switch-to-buffer-other-window "*Shell Command Output*") (while (get-buffer newname) (setq newname (format "*shell* %s <%d>" cmd (setq n (1+ n))))) (rename-buffer newname) (setq numlines (count-lines 1 (point-max))) (if (> numlines 0) (progn (if (< numlines (window-height)) (enlarge-window (- (1+ numlines) (window-height)))) (if (catch 'keep-p (while t (let ((c (read-char "Keep? (SPC keeps, TAB scrolls) "))) (if (eq (string-to-char "\C-i") c) (if (pos-visible-in-window-p (point-max)) (goto-char (point-min)) (scroll-up)) (throw 'keep-p (eq (string-to-char " ") c)))))) (setq buffer-read-only (message "")) (delete-window) (kill-buffer newname) (message ""))) (delete-window) (kill-buffer newname)) (select-window oldw))) (provide 'outside) ;;; outside.el ends here