Here's a few small emacsisms ive been working on, they're all small things that I've written on a whim to help with work
grep -r to search files and will open each file that returned a match
(defun recursive-grep-open (where pattern)
"searches where for pattern using a recursive grep and will open all files that match"
(interactive "DWhere: \nsPattern: ")
(with-temp-buffer
(call-process "grep"
nil
'(t nil)
nil
"-rl"
pattern
where)
(goto-char (point-min))
(while (< (point) (point-max)) (let ((cp (point))) (end-of-line) (find-file (buffer-substring cp (point))) (beginning-of-line 2)))))
audit-set-destination) with a heading for the filename, the line number and message which is prompted for and the code snippet itself.
(defvar audit-destination-buffer nil)
(defun audit-set-destination (buffer)
(interactive "bDestination Buffer: ")
(setq tm-audit-destination-buffer buffer))
(defun audit-this (beg end message)
(interactive "r\nsMessage: ")
(save-excursion
(let* ((buffer-name (buffer-name))
(source-buf (current-buffer))
(where (or tm-audit-destination-buffer
(read-buffer "Audit buffer: ")))
(file-heading (format "\n* File: %s\n" buffer-name))
(text (buffer-substring beg end))
(start-line (progn (goto-char beg)
(line-number-at-pos)))
(chunk (format "\n** Line: %d\n - %s\n%s\n"
start-line
message
text)))
(switch-to-buffer where)
(save-excursion
(goto-char (point-min))
(if (not (re-search-forward (format "^\\* File: %s$" buffer-name) nil t))
(progn (goto-char (point-max))
(insert file-heading)))
(insert chunk))
(switch-to-buffer source-buf))))
post-command-hook, is there a better place for this?
;; slightly nasty
;; this could probably be optomised, but
(defun add-function-name-to-header-line ()
"adds the name of the current function into the header line"
(if (eq major-mode 'cperl-mode)
(progn
(save-excursion
(if (re-search-backward "^sub\s+\\([a-zA-Z0-9_]+\\)" nil t)
(setq header-line-format (format "function: %s" (match-string 1)))
(setq header-line-format nil))))))
(add-to-list 'post-command-hook 'add-function-name-to-header-line)
(defun helpful-buffer-name ()
(interactive)
(save-excursion
(goto-char (point-min))
(if (re-search-forward "^package \\(.+\\);" nil t)
(let ((pkg (match-string 1)))
(if (get-buffer pkg)
(rename-buffer (format "%s (%s)"
pkg
(buffer-file-name))
t)
(rename-buffer pkg))))))
(add-hook 'cperl-mode-hook (lambda () (helpful-buffer-name)))
I hope some of you out there find this stuff useful!
edit: Changed the headings so they look better on planet lisp.. sorry guys.
Yet another code monkey from London, living in LA.
Links