misc.el 7.0 KB

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