* admin/gitmerge.el (gitmerge-missing):
[emacs.git] / lisp / misc.el
blob8806ac838376ac4d2fe7df9ded24305564383555
1 ;;; misc.el --- some nonstandard editing and utility commands for Emacs
3 ;; Copyright (C) 1989, 2001-2017 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'tabulated-list))
31 ;;;###autoload
32 (defun copy-from-above-command (&optional arg)
33 "Copy characters from previous nonblank line, starting just above point.
34 Copy ARG characters, but not past the end of that line.
35 If no argument given, copy the entire rest of the line.
36 The characters copied are inserted in the buffer before point."
37 (interactive "P")
38 (let ((cc (current-column))
40 (string ""))
41 (save-excursion
42 (beginning-of-line)
43 (backward-char 1)
44 (skip-chars-backward "\ \t\n")
45 (move-to-column cc)
46 ;; Default is enough to copy the whole rest of the line.
47 (setq n (if arg (prefix-numeric-value arg) (point-max)))
48 ;; If current column winds up in middle of a tab,
49 ;; copy appropriate number of "virtual" space chars.
50 (if (< cc (current-column))
51 (if (= (preceding-char) ?\t)
52 (progn
53 (setq string (make-string (min n (- (current-column) cc)) ?\s))
54 (setq n (- n (min n (- (current-column) cc)))))
55 ;; In middle of ctl char => copy that whole char.
56 (backward-char 1)))
57 (setq string (concat string
58 (buffer-substring
59 (point)
60 (min (line-end-position)
61 (+ n (point)))))))
62 (insert string)))
64 ;; Variation of `zap-to-char'.
66 ;;;###autoload
67 (defun zap-up-to-char (arg char)
68 "Kill up to, but not including ARGth occurrence of CHAR.
69 Case is ignored if `case-fold-search' is non-nil in the current buffer.
70 Goes backward if ARG is negative; error if CHAR not found.
71 Ignores CHAR at point."
72 (interactive "p\ncZap up to char: ")
73 (let ((direction (if (>= arg 0) 1 -1)))
74 (kill-region (point)
75 (progn
76 (forward-char direction)
77 (unwind-protect
78 (search-forward (char-to-string char) nil nil arg)
79 (backward-char direction))
80 (point)))))
82 ;; These were added with an eye to making possible a more CCA-compatible
83 ;; command set; but that turned out not to be interesting.
85 ;;;###autoload
86 (defun mark-beginning-of-buffer ()
87 "Set mark at the beginning of the buffer."
88 (interactive)
89 (push-mark (point-min)))
91 ;;;###autoload
92 (defun mark-end-of-buffer ()
93 "Set mark at the end of the buffer."
94 (interactive)
95 (push-mark (point-max)))
97 ;;;###autoload
98 (defun upcase-char (arg)
99 "Uppercasify ARG chars starting from point. Point doesn't move."
100 (interactive "p")
101 (save-excursion
102 (upcase-region (point) (progn (forward-char arg) (point)))))
104 ;;;###autoload
105 (defun forward-to-word (arg)
106 "Move forward until encountering the beginning of a word.
107 With argument, do this that many times."
108 (interactive "^p")
109 (or (re-search-forward (if (> arg 0) "\\W\\b" "\\b\\W") nil t arg)
110 (goto-char (if (> arg 0) (point-max) (point-min)))))
112 ;;;###autoload
113 (defun backward-to-word (arg)
114 "Move backward until encountering the end of a word.
115 With argument, do this that many times."
116 (interactive "^p")
117 (forward-to-word (- arg)))
119 ;;;###autoload
120 (defun butterfly ()
121 "Use butterflies to flip the desired bit on the drive platter.
122 Open hands and let the delicate wings flap once. The disturbance
123 ripples outward, changing the flow of the eddy currents in the
124 upper atmosphere. These cause momentary pockets of higher-pressure
125 air to form, which act as lenses that deflect incoming cosmic rays,
126 focusing them to strike the drive platter and flip the desired bit.
127 You can type `M-x butterfly C-M-c' to run it. This is a permuted
128 variation of `C-x M-c M-butterfly' from url `http://xkcd.com/378/'."
129 (interactive)
130 (if (yes-or-no-p "Do you really want to unleash the powers of the butterfly? ")
131 (progn
132 (switch-to-buffer (get-buffer-create "*butterfly*"))
133 (erase-buffer)
134 (sit-for 0)
135 (animate-string "Amazing physics going on..."
136 (/ (window-height) 2) (- (/ (window-width) 2) 12))
137 (sit-for (* 5 (/ (abs (random)) (float most-positive-fixnum))))
138 (message "Successfully flipped one bit!"))
139 (message "Well, then go to xkcd.com!")
140 (browse-url "http://xkcd.com/378/")))
142 ;; A command to list dynamically loaded libraries. This useful in
143 ;; environments where dynamic-library-alist is used, i.e., Windows
145 (defvar list-dynamic-libraries--loaded-only-p)
146 (make-variable-buffer-local 'list-dynamic-libraries--loaded-only-p)
148 (defun list-dynamic-libraries--loaded (from)
149 "Compute the \"Loaded from\" column.
150 Internal use only."
151 (if from
152 (let ((name (car from))
153 (path (or (cdr from) "<unknown>")))
154 ;; This is a roundabout way to change the tooltip without
155 ;; having to replace the default printer function
156 (propertize name
157 'display (propertize name
158 'help-echo (concat "Loaded from: " path))))
159 ""))
161 (defun list-dynamic-libraries--refresh ()
162 "Recompute the list of dynamic libraries.
163 Internal use only."
164 (setq tabulated-list-format ; recomputed because column widths can change
165 (let ((max-id-len 0) (max-name-len 0))
166 (dolist (lib dynamic-library-alist)
167 (let ((id-len (length (symbol-name (car lib))))
168 (name-len (apply 'max (mapcar 'length (cdr lib)))))
169 (when (> id-len max-id-len) (setq max-id-len id-len))
170 (when (> name-len max-name-len) (setq max-name-len name-len))))
171 (vector (list "Library" (1+ max-id-len) t)
172 (list "Loaded from" (1+ max-name-len) t)
173 (list "Candidate names" 0 t))))
174 (tabulated-list-init-header)
175 (setq tabulated-list-entries nil)
176 (dolist (lib dynamic-library-alist)
177 (let* ((id (car lib))
178 (from (get id :loaded-from)))
179 (when (or from
180 (not list-dynamic-libraries--loaded-only-p))
181 (push (list id (vector (symbol-name id)
182 (list-dynamic-libraries--loaded from)
183 (mapconcat 'identity (cdr lib) ", ")))
184 tabulated-list-entries)))))
186 ;;;###autoload
187 (defun list-dynamic-libraries (&optional loaded-only-p buffer)
188 "Display a list of all dynamic libraries known to Emacs.
189 \(These are the libraries listed in `dynamic-library-alist'.)
190 If optional argument LOADED-ONLY-P (interactively, prefix arg)
191 is non-nil, only libraries already loaded are listed.
192 Optional argument BUFFER specifies a buffer to use, instead of
193 \"*Dynamic Libraries*\".
194 The return value is always nil."
195 (interactive "P")
196 (unless (bufferp buffer)
197 (setq buffer (get-buffer-create "*Dynamic Libraries*")))
198 (with-current-buffer buffer
199 (tabulated-list-mode)
200 (setq tabulated-list-sort-key (cons "Library" nil))
201 (add-hook 'tabulated-list-revert-hook 'list-dynamic-libraries--refresh nil t)
202 (setq list-dynamic-libraries--loaded-only-p loaded-only-p)
203 (list-dynamic-libraries--refresh)
204 (tabulated-list-print))
205 (display-buffer buffer)
206 nil)
208 (provide 'misc)
210 ;;; misc.el ends here