; Auto-commit of ChangeLog files.
[emacs.git] / lisp / misc.el
Commit [+]AuthorDateLineData
5f4b1dfe Juanma Barranquero2011-05-09 17:52:57 +02001;;; misc.el --- some nonstandard editing and utility commands for Emacs
6594deb0 Eric S. Raymond1992-05-30 22:12:04 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1989, 2001-2015 Free Software Foundation, Inc.
0d20f9a0 Jim Blandy1989-10-31 15:59:53 +00004
34dc21db Glenn Morris2014-02-09 17:34:22 -08005;; Maintainer: emacs-devel@gnu.org
30764597 Pavel Janík2002-05-02 05:41:46 +00006;; Keywords: convenience
bd78fa1d Chong Yidong2010-08-29 12:17:13 -04007;; Package: emacs
9750e079 Eric S. Raymond1992-07-22 04:22:30 +00008
0d20f9a0
JB
Jim Blandy1989-10-31 15:59:53 +00009;; This file is part of GNU Emacs.
10
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000011;; GNU Emacs is free software: you can redistribute it and/or modify
0d20f9a0 Jim Blandy1989-10-31 15:59:53 +000012;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000013;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
0d20f9a0
JB
Jim Blandy1989-10-31 15:59:53 +000015
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000022;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0d20f9a0 Jim Blandy1989-10-31 15:59:53 +000023
55535639
PJ
Pavel Janík2001-07-15 16:15:35 +000024;;; Commentary:
25
e5167999 Eric S. Raymond1992-07-16 21:47:34 +000026;;; Code:
0d20f9a0 Jim Blandy1989-10-31 15:59:53 +000027
84f29e6b
JB
Juanma Barranquero2011-05-10 01:57:40 +020028(eval-when-compile
29 (require 'tabulated-list))
30
0d20f9a0
JB
Jim Blandy1989-10-31 15:59:53 +000031(defun copy-from-above-command (&optional arg)
32 "Copy characters from previous nonblank line, starting just above point.
33Copy ARG characters, but not past the end of that line.
34If no argument given, copy the entire rest of the line.
35The characters copied are inserted in the buffer before point."
36 (interactive "P")
37 (let ((cc (current-column))
38 n
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
ff26cdfe Juanma Barranquero2006-11-27 14:00:11 +000052 (setq string (make-string (min n (- (current-column) cc)) ?\s))
0d20f9a0
JB
Jim Blandy1989-10-31 15:59:53 +000053 (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)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -070059 (min (line-end-position)
0d20f9a0
JB
Jim Blandy1989-10-31 15:59:53 +000060 (+ n (point)))))))
61 (insert string)))
6594deb0 Eric S. Raymond1992-05-30 22:12:04 +000062
2fd8a18a
LK
Lute Kamstra2003-09-24 08:52:09 +000063;; Variation of `zap-to-char'.
64
65(defun zap-up-to-char (arg char)
821a311f Juanma Barranquero2006-11-27 17:02:43 +000066 "Kill up to, but not including ARGth occurrence of CHAR.
2fd8a18a
LK
Lute Kamstra2003-09-24 08:52:09 +000067Case is ignored if `case-fold-search' is non-nil in the current buffer.
68Goes backward if ARG is negative; error if CHAR not found.
69Ignores 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)))))
79
9bccd1e3
JB
Juanma Barranquero2003-05-30 23:15:40 +000080;; These were added with an eye to making possible a more CCA-compatible
81;; command set; but that turned out not to be interesting.
82
83(defun mark-beginning-of-buffer ()
84 "Set mark at the beginning of the buffer."
85 (interactive)
86 (push-mark (point-min)))
87
88(defun mark-end-of-buffer ()
89 "Set mark at the end of the buffer."
90 (interactive)
91 (push-mark (point-max)))
92
93(defun upcase-char (arg)
2fd8a18a Lute Kamstra2003-09-24 08:52:09 +000094 "Uppercasify ARG chars starting from point. Point doesn't move."
9bccd1e3
JB
Juanma Barranquero2003-05-30 23:15:40 +000095 (interactive "p")
96 (save-excursion
97 (upcase-region (point) (progn (forward-char arg) (point)))))
98
99(defun forward-to-word (arg)
100 "Move forward until encountering the beginning of a word.
101With argument, do this that many times."
add9ca2d Bastien Guerry2012-08-22 19:12:35 +0200102 (interactive "^p")
9bccd1e3
JB
Juanma Barranquero2003-05-30 23:15:40 +0000103 (or (re-search-forward (if (> arg 0) "\\W\\b" "\\b\\W") nil t arg)
104 (goto-char (if (> arg 0) (point-max) (point-min)))))
105
106(defun backward-to-word (arg)
107 "Move backward until encountering the end of a word.
108With argument, do this that many times."
add9ca2d Bastien Guerry2012-08-22 19:12:35 +0200109 (interactive "^p")
9bccd1e3
JB
Juanma Barranquero2003-05-30 23:15:40 +0000110 (forward-to-word (- arg)))
111
e8d24e5b
JL
Juri Linkov2008-12-28 23:48:21 +0000112;;;###autoload
113(defun butterfly ()
0f9568b7
JL
Juri Linkov2008-12-28 23:50:47 +0000114 "Use butterflies to flip the desired bit on the drive platter.
115Open hands and let the delicate wings flap once. The disturbance
116ripples outward, changing the flow of the eddy currents in the
117upper atmosphere. These cause momentary pockets of higher-pressure
118air to form, which act as lenses that deflect incoming cosmic rays,
119focusing them to strike the drive platter and flip the desired bit.
120You can type `M-x butterfly C-M-c' to run it. This is a permuted
121variation of `C-x M-c M-butterfly' from url `http://xkcd.com/378/'."
e8d24e5b
JL
Juri Linkov2008-12-28 23:48:21 +0000122 (interactive)
123 (if (yes-or-no-p "Do you really want to unleash the powers of the butterfly? ")
124 (progn
0f9568b7
JL
Juri Linkov2008-12-28 23:50:47 +0000125 (switch-to-buffer (get-buffer-create "*butterfly*"))
126 (erase-buffer)
127 (sit-for 0)
0f9568b7
JL
Juri Linkov2008-12-28 23:50:47 +0000128 (animate-string "Amazing physics going on..."
129 (/ (window-height) 2) (- (/ (window-width) 2) 12))
e8d24e5b
JL
Juri Linkov2008-12-28 23:48:21 +0000130 (sit-for (* 5 (/ (abs (random)) (float most-positive-fixnum))))
131 (message "Successfully flipped one bit!"))
0f9568b7
JL
Juri Linkov2008-12-28 23:50:47 +0000132 (message "Well, then go to xkcd.com!")
133 (browse-url "http://xkcd.com/378/")))
e8d24e5b Juri Linkov2008-12-28 23:48:21 +0000134
5f4b1dfe
JB
Juanma Barranquero2011-05-09 17:52:57 +0200135;; A command to list dynamically loaded libraries. This useful in
136;; environments where dynamic-library-alist is used, i.e., Windows
137
138(defvar list-dynamic-libraries--loaded-only-p)
139(make-variable-buffer-local 'list-dynamic-libraries--loaded-only-p)
140
2a8ce227
JB
Juanma Barranquero2012-04-10 16:16:05 +0200141(defun list-dynamic-libraries--loaded (from)
142 "Compute the \"Loaded from\" column.
143Internal use only."
144 (if from
145 (let ((name (car from))
146 (path (or (cdr from) "<unknown>")))
147 ;; This is a roundabout way to change the tooltip without
148 ;; having to replace the default printer function
149 (propertize name
150 'display (propertize name
151 'help-echo (concat "Loaded from: " path))))
152 ""))
153
5f4b1dfe
JB
Juanma Barranquero2011-05-09 17:52:57 +0200154(defun list-dynamic-libraries--refresh ()
155 "Recompute the list of dynamic libraries.
156Internal use only."
157 (setq tabulated-list-format ; recomputed because column widths can change
158 (let ((max-id-len 0) (max-name-len 0))
159 (dolist (lib dynamic-library-alist)
160 (let ((id-len (length (symbol-name (car lib))))
161 (name-len (apply 'max (mapcar 'length (cdr lib)))))
162 (when (> id-len max-id-len) (setq max-id-len id-len))
163 (when (> name-len max-name-len) (setq max-name-len name-len))))
164 (vector (list "Library" (1+ max-id-len) t)
165 (list "Loaded from" (1+ max-name-len) t)
166 (list "Candidate names" 0 t))))
ae9c0411 Juanma Barranquero2011-06-21 12:04:28 +0200167 (tabulated-list-init-header)
5f4b1dfe
JB
Juanma Barranquero2011-05-09 17:52:57 +0200168 (setq tabulated-list-entries nil)
169 (dolist (lib dynamic-library-alist)
170 (let* ((id (car lib))
171 (from (get id :loaded-from)))
172 (when (or from
173 (not list-dynamic-libraries--loaded-only-p))
174 (push (list id (vector (symbol-name id)
2a8ce227 Juanma Barranquero2012-04-10 16:16:05 +0200175 (list-dynamic-libraries--loaded from)
5f4b1dfe
JB
Juanma Barranquero2011-05-09 17:52:57 +0200176 (mapconcat 'identity (cdr lib) ", ")))
177 tabulated-list-entries)))))
178
179;;;###autoload
180(defun list-dynamic-libraries (&optional loaded-only-p buffer)
181 "Display a list of all dynamic libraries known to Emacs.
182\(These are the libraries listed in `dynamic-library-alist'.)
183If optional argument LOADED-ONLY-P (interactively, prefix arg)
184is non-nil, only libraries already loaded are listed.
185Optional argument BUFFER specifies a buffer to use, instead of
186\"*Dynamic Libraries*\".
187The return value is always nil."
188 (interactive "P")
189 (unless (bufferp buffer)
190 (setq buffer (get-buffer-create "*Dynamic Libraries*")))
191 (with-current-buffer buffer
192 (tabulated-list-mode)
193 (setq tabulated-list-sort-key (cons "Library" nil))
194 (add-hook 'tabulated-list-revert-hook 'list-dynamic-libraries--refresh nil t)
5f4b1dfe
JB
Juanma Barranquero2011-05-09 17:52:57 +0200195 (setq list-dynamic-libraries--loaded-only-p loaded-only-p)
196 (list-dynamic-libraries--refresh)
197 (tabulated-list-print))
198 (display-buffer buffer)
199 nil)
200
896546cd
RS
Richard M. Stallman1997-06-22 18:57:55 +0000201(provide 'misc)
202
6594deb0 Eric S. Raymond1992-05-30 22:12:04 +0000203;;; misc.el ends here