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