Fix commit 2011-07-01T01:34:38Z!ueno@unixuser.org.
[emacs.git] / lisp / misc.el
blob8087c7f525903c5bab9d2bab8345029dd23d7c42
1 ;;; misc.el --- some nonstandard editing and utility commands for Emacs
3 ;; Copyright (C) 1989, 2001-2011 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: convenience
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'tabulated-list))
31 (defun copy-from-above-command (&optional arg)
32 "Copy characters from previous nonblank line, starting just above point.
33 Copy ARG characters, but not past the end of that line.
34 If no argument given, copy the entire rest of the line.
35 The characters copied are inserted in the buffer before point."
36 (interactive "P")
37 (let ((cc (current-column))
39 (string ""))
40 (save-excursion
41 (beginning-of-line)
42 (backward-char 1)
43 (skip-chars-backward "\ \t\n")
44 (move-to-column cc)
45 ;; Default is enough to copy the whole rest of the line.
46 (setq n (if arg (prefix-numeric-value arg) (point-max)))
47 ;; If current column winds up in middle of a tab,
48 ;; copy appropriate number of "virtual" space chars.
49 (if (< cc (current-column))
50 (if (= (preceding-char) ?\t)
51 (progn
52 (setq string (make-string (min n (- (current-column) cc)) ?\s))
53 (setq n (- n (min n (- (current-column) cc)))))
54 ;; In middle of ctl char => copy that whole char.
55 (backward-char 1)))
56 (setq string (concat string
57 (buffer-substring
58 (point)
59 (min (line-end-position)
60 (+ n (point)))))))
61 (insert string)))
63 ;; Variation of `zap-to-char'.
65 (defun zap-up-to-char (arg char)
66 "Kill up to, but not including ARGth occurrence of CHAR.
67 Case is ignored if `case-fold-search' is non-nil in the current buffer.
68 Goes backward if ARG is negative; error if CHAR not found.
69 Ignores CHAR at point."
70 (interactive "p\ncZap up to char: ")
71 (let ((direction (if (>= arg 0) 1 -1)))
72 (kill-region (point)
73 (progn
74 (forward-char direction)
75 (unwind-protect
76 (search-forward (char-to-string char) nil nil arg)
77 (backward-char direction))
78 (point)))))
80 ;; These were added with an eye to making possible a more CCA-compatible
81 ;; command set; but that turned out not to be interesting.
83 (defun mark-beginning-of-buffer ()
84 "Set mark at the beginning of the buffer."
85 (interactive)
86 (push-mark (point-min)))
88 (defun mark-end-of-buffer ()
89 "Set mark at the end of the buffer."
90 (interactive)
91 (push-mark (point-max)))
93 (defun upcase-char (arg)
94 "Uppercasify ARG chars starting from point. Point doesn't move."
95 (interactive "p")
96 (save-excursion
97 (upcase-region (point) (progn (forward-char arg) (point)))))
99 (defun forward-to-word (arg)
100 "Move forward until encountering the beginning of a word.
101 With argument, do this that many times."
102 (interactive "p")
103 (or (re-search-forward (if (> arg 0) "\\W\\b" "\\b\\W") nil t arg)
104 (goto-char (if (> arg 0) (point-max) (point-min)))))
106 (defun backward-to-word (arg)
107 "Move backward until encountering the end of a word.
108 With argument, do this that many times."
109 (interactive "p")
110 (forward-to-word (- arg)))
112 ;;;###autoload
113 (defun butterfly ()
114 "Use butterflies to flip the desired bit on the drive platter.
115 Open hands and let the delicate wings flap once. The disturbance
116 ripples outward, changing the flow of the eddy currents in the
117 upper atmosphere. These cause momentary pockets of higher-pressure
118 air to form, which act as lenses that deflect incoming cosmic rays,
119 focusing them to strike the drive platter and flip the desired bit.
120 You can type `M-x butterfly C-M-c' to run it. This is a permuted
121 variation of `C-x M-c M-butterfly' from url `http://xkcd.com/378/'."
122 (interactive)
123 (if (yes-or-no-p "Do you really want to unleash the powers of the butterfly? ")
124 (progn
125 (switch-to-buffer (get-buffer-create "*butterfly*"))
126 (erase-buffer)
127 (sit-for 0)
128 (animate-string "Amazing physics going on..."
129 (/ (window-height) 2) (- (/ (window-width) 2) 12))
130 (sit-for (* 5 (/ (abs (random)) (float most-positive-fixnum))))
131 (message "Successfully flipped one bit!"))
132 (message "Well, then go to xkcd.com!")
133 (browse-url "http://xkcd.com/378/")))
135 ;; A command to list dynamically loaded libraries. This useful in
136 ;; environments where dynamic-library-alist is used, i.e., Windows
138 (defvar list-dynamic-libraries--loaded-only-p)
139 (make-variable-buffer-local 'list-dynamic-libraries--loaded-only-p)
141 (defun list-dynamic-libraries--refresh ()
142 "Recompute the list of dynamic libraries.
143 Internal use only."
144 (setq tabulated-list-format ; recomputed because column widths can change
145 (let ((max-id-len 0) (max-name-len 0))
146 (dolist (lib dynamic-library-alist)
147 (let ((id-len (length (symbol-name (car lib))))
148 (name-len (apply 'max (mapcar 'length (cdr lib)))))
149 (when (> id-len max-id-len) (setq max-id-len id-len))
150 (when (> name-len max-name-len) (setq max-name-len name-len))))
151 (vector (list "Library" (1+ max-id-len) t)
152 (list "Loaded from" (1+ max-name-len) t)
153 (list "Candidate names" 0 t))))
154 (tabulated-list-init-header)
155 (setq tabulated-list-entries nil)
156 (dolist (lib dynamic-library-alist)
157 (let* ((id (car lib))
158 (from (get id :loaded-from)))
159 (when (or from
160 (not list-dynamic-libraries--loaded-only-p))
161 (push (list id (vector (symbol-name id)
162 (or from "")
163 (mapconcat 'identity (cdr lib) ", ")))
164 tabulated-list-entries)))))
166 ;;;###autoload
167 (defun list-dynamic-libraries (&optional loaded-only-p buffer)
168 "Display a list of all dynamic libraries known to Emacs.
169 \(These are the libraries listed in `dynamic-library-alist'.)
170 If optional argument LOADED-ONLY-P (interactively, prefix arg)
171 is non-nil, only libraries already loaded are listed.
172 Optional argument BUFFER specifies a buffer to use, instead of
173 \"*Dynamic Libraries*\".
174 The return value is always nil."
175 (interactive "P")
176 (unless (bufferp buffer)
177 (setq buffer (get-buffer-create "*Dynamic Libraries*")))
178 (with-current-buffer buffer
179 (tabulated-list-mode)
180 (setq tabulated-list-sort-key (cons "Library" nil))
181 (add-hook 'tabulated-list-revert-hook 'list-dynamic-libraries--refresh nil t)
182 (setq list-dynamic-libraries--loaded-only-p loaded-only-p)
183 (list-dynamic-libraries--refresh)
184 (tabulated-list-print))
185 (display-buffer buffer)
186 nil)
188 (provide 'misc)
190 ;;; misc.el ends here