frame-fns.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; frame-fns.el --- Non-interactive frame and window functions.
  2. ;;
  3. ;; Filename: frame-fns.el
  4. ;; Description: Non-interactive frame and window functions.
  5. ;; Author: Drew Adams
  6. ;; Maintainer: Drew Adams
  7. ;; Copyright (C) 1996-2011, Drew Adams, all rights reserved.
  8. ;; Created: Tue Mar 5 16:15:50 1996
  9. ;; Version: 21.1
  10. ;; Last-Updated: Tue Jan 4 09:43:55 2011 (-0800)
  11. ;; By: dradams
  12. ;; Update #: 197
  13. ;; URL: http://www.emacswiki.org/cgi-bin/wiki/frame-fns.el
  14. ;; Keywords: internal, extensions, local, frames
  15. ;; Compatibility: GNU Emacs: 20.x, 21.x, 22.x, 23.x
  16. ;;
  17. ;; Features that might be required by this library:
  18. ;;
  19. ;; `avoid'.
  20. ;;
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Non-interactive frame and window functions.
  26. ;;
  27. ;; Main new functions defined here:
  28. ;;
  29. ;; `1-window-frames-on', `distance', `flash-ding',
  30. ;; `frame-geom-spec-cons', `frame-geom-value-cons',
  31. ;; `frame-geom-spec-numeric', `frame-geom-value-numeric',
  32. ;; `frames-on', `get-a-frame', `get-frame-name',
  33. ;; `multi-window-frames-on', `read-frame', `window-coords'.
  34. ;;
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;
  37. ;;; Change log:
  38. ;;
  39. ;; 2011/01/04 dadams
  40. ;; Removed autoload cookies from non-interactive functions.
  41. ;; 2010/01/12 dadams
  42. ;; 1-window-frames-on, multi-window-frames-on:
  43. ;; save-excursion + set-buffer -> with-current-buffer.
  44. ;; 2008/04/05 dadams
  45. ;; get-a-frame: Define without using member-if.
  46. ;; 2005/10/31 dadams
  47. ;; read-frame: Swapped default and init values in call to completing-read.
  48. ;; 2004/11/26 dadams
  49. ;; Added frame-geom-spec-numeric and frame-geom-value-numeric.
  50. ;; 2004/03/19 dadams
  51. ;; read-frame: 1) if default is a frame, use its name,
  52. ;; 2) use frame-name-history, not minibuffer-history,
  53. ;; and use make-frame-names-alist, not frame-alist,
  54. ;; in completing-read
  55. ;; 1996/02/14 dadams
  56. ;; Added: window-coords, distance.
  57. ;;
  58. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  59. ;;
  60. ;; This program is free software; you can redistribute it and/or modify
  61. ;; it under the terms of the GNU General Public License as published by
  62. ;; the Free Software Foundation; either version 2, or (at your option)
  63. ;; any later version.
  64. ;; This program is distributed in the hope that it will be useful,
  65. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  66. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  67. ;; GNU General Public License for more details.
  68. ;; You should have received a copy of the GNU General Public License
  69. ;; along with this program; see the file COPYING. If not, write to
  70. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  71. ;; Floor, Boston, MA 02110-1301, USA.
  72. ;;
  73. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74. ;;
  75. ;;; Code:
  76. (eval-when-compile (require 'cl)) ;; (plus, for Emacs 20: dolist, push
  77. ;; and, for Emacs <20: cadr, when, unless)
  78. (require 'avoid nil t) ;; mouse-avoidance-point-position
  79. ;;;;;;;;;;;;;;;;;;;;;;;
  80. (defun window-coords (&optional position)
  81. "Return window coordinates of buffer POSITION (default: point).
  82. If POSITION is nil, (point) is used."
  83. (unless (fboundp 'mouse-avoidance-point-position) (require 'avoid))
  84. (cdr (mouse-avoidance-point-position)))
  85. (defun distance (pt1 pt2)
  86. "Distance as the crow flies between PT1 and PT2.
  87. PT1 and PT2 are each a cons of the form (X . Y)."
  88. (let ((xdiff (abs (- (car pt1) (car pt2))))
  89. (ydiff (abs (- (cdr pt1) (cdr pt2)))))
  90. (sqrt (+ (* xdiff xdiff) (* ydiff ydiff)))))
  91. (defun frame-geom-value-numeric (type value &optional frame)
  92. "Return equivalent geometry value for FRAME in numeric terms.
  93. A geometry value equivalent to VALUE for FRAME is returned,
  94. where the value is numeric, not a consp.
  95. TYPE is the car of the original geometry spec (TYPE . VALUE).
  96. It is `top' or `left', depending on which edge VALUE is related to.
  97. VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
  98. If VALUE is a consp, then it is converted to a numeric value, perhaps
  99. relative to the opposite frame edge from that in the original spec.
  100. FRAME defaults to the selected frame.
  101. Examples (measures in pixels) -
  102. Assuming display height/width=1024, frame height/width=600:
  103. 300 inside display edge: 300 => 300
  104. (+ 300) => 300
  105. 300 inside opposite display edge: (- 300) => -300
  106. -300 => -300
  107. 300 beyond display edge
  108. (= 724 inside opposite display edge): (+ -300) => -724
  109. 300 beyond display edge
  110. (= 724 inside opposite display edge): (- -300) => 724
  111. In the last two examples, the returned value is relative to the
  112. opposite frame edge from the edge indicated in the input spec."
  113. (if (consp value)
  114. (if (natnump (cadr value))
  115. ;; e.g. (+ 300) or (- 300) => 300 or -300
  116. (funcall (car value) (cadr value))
  117. ;; e.g. (+ -300) or (- -300)
  118. (let ((oppval (- (if (eq 'left type)
  119. (x-display-pixel-width)
  120. (x-display-pixel-height))
  121. (cadr value)
  122. (if (eq 'left type)
  123. (frame-pixel-width frame)
  124. (frame-pixel-height frame)))))
  125. (if (eq '+ (car value))
  126. (- oppval) ; e.g. (+ -300) => -724
  127. oppval))) ; e.g. (- -300) => 724
  128. ;; e.g. 300 or -300
  129. value))
  130. (defun frame-geom-spec-numeric (spec &optional frame)
  131. "Return equivalent geometry specification for FRAME in numeric terms.
  132. A geometry specification equivalent to SPEC for FRAME is returned,
  133. where the value is numeric, not a consp.
  134. SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
  135. If VALUE is a consp, then it is converted to a numeric value, perhaps
  136. relative to the opposite frame edge from that in the original SPEC.
  137. FRAME defaults to the selected frame.
  138. Examples (measures in pixels) -
  139. Assuming display height=1024, frame height=600:
  140. top 300 below display top: (top . 300) => (top . 300)
  141. (top + 300) => (top . 300)
  142. bottom 300 above display bottom: (top - 300) => (top . -300)
  143. (top . -300) => (top . -300)
  144. top 300 above display top
  145. (= bottom 724 above display bottom): (top + -300) => (top . -724)
  146. bottom 300 below display bottom
  147. (= top 724 below display top): (top - -300) => (top . 724)
  148. In the last two examples, the returned value is relative to the
  149. opposite frame edge from the edge indicated in the input SPEC."
  150. (cons (car spec) (frame-geom-value-numeric (car spec) (cdr spec))))
  151. (defun frame-geom-value-cons (type value &optional frame)
  152. "Return equivalent geometry value for FRAME as a cons with car `+'.
  153. A geometry value equivalent to VALUE for FRAME is returned,
  154. where the value is a cons with car `+', not numeric.
  155. TYPE is the car of the original geometry spec (TYPE . VALUE).
  156. It is `top' or `left', depending on which edge VALUE is related to.
  157. VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
  158. If VALUE is a number, then it is converted to a cons value, perhaps
  159. relative to the opposite frame edge from that in the original spec.
  160. FRAME defaults to the selected frame.
  161. Examples (measures in pixels) -
  162. Assuming display height/width=1024, frame height/width=600:
  163. 300 inside display edge: 300 => (+ 300)
  164. (+ 300) => (+ 300)
  165. 300 inside opposite display edge: (- 300) => (+ 124)
  166. -300 => (+ 124)
  167. 300 beyond display edge
  168. (= 724 inside opposite display edge): (+ -300) => (+ -300)
  169. 300 beyond display edge
  170. (= 724 inside opposite display edge): (- -300) => (+ 724)
  171. In the 3rd, 4th, and 6th examples, the returned value is relative to
  172. the opposite frame edge from the edge indicated in the input spec."
  173. (cond ((and (consp value) (eq '+ (car value))) ; e.g. (+ 300), (+ -300)
  174. value)
  175. ((natnump value) (list '+ value)) ; e.g. 300 => (+ 300)
  176. (t ; e.g. -300, (- 300), (- -300)
  177. (list '+ (- (if (eq 'left type) ; => (+ 124), (+ 124), (+ 724)
  178. (x-display-pixel-width)
  179. (x-display-pixel-height))
  180. (if (integerp value) (- value) (cadr value))
  181. (if (eq 'left type)
  182. (frame-pixel-width frame)
  183. (frame-pixel-height frame)))))))
  184. (defun frame-geom-spec-cons (spec &optional frame)
  185. "Return equivalent geometry spec for FRAME as a cons with car `+'.
  186. A geometry specification equivalent to SPEC for FRAME is returned,
  187. where the value is a cons with car `+', not numeric.
  188. SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
  189. If VALUE is a number, then it is converted to a cons value, perhaps
  190. relative to the opposite frame edge from that in the original spec.
  191. FRAME defaults to the selected frame.
  192. Examples (measures in pixels) -
  193. Assuming display height=1024, frame height=600:
  194. top 300 below display top: (top . 300) => (top + 300)
  195. (top + 300) => (top + 300)
  196. bottom 300 above display bottom: (top - 300) => (top + 124)
  197. (top . -300) => (top + 124)
  198. top 300 above display top
  199. (= bottom 724 above display bottom): (top + -300) => (top + -300)
  200. bottom 300 below display bottom
  201. (= top 724 below display top): (top - -300) => (top + 724)
  202. In the 3rd, 4th, and 6th examples, the returned value is relative to
  203. the opposite frame edge from the edge indicated in the input spec."
  204. (cons (car spec) (frame-geom-value-cons (car spec) (cdr spec))))
  205. (defun get-frame-name (&optional frame)
  206. "Return the string that names FRAME (a frame). Default is selected frame."
  207. (unless frame (setq frame (selected-frame)))
  208. (if (framep frame)
  209. (cdr (assq 'name (frame-parameters frame)))
  210. (error "Function `get-frame-name': Argument not a frame: `%s'" frame)))
  211. (defun get-a-frame (frame)
  212. "Return a frame, if any, named FRAME (a frame or a string).
  213. If none, return nil.
  214. If FRAME is a frame, it is returned."
  215. (cond ((framep frame) frame)
  216. ((stringp frame)
  217. (catch 'get-a-frame-found
  218. (dolist (fr (frame-list))
  219. (when (string= frame (get-frame-name fr))
  220. (throw 'get-a-frame-found fr)))
  221. nil))
  222. (t
  223. (error
  224. "Function `get-frame-name': Arg neither a string nor a frame: `%s'"
  225. frame))))
  226. (defun read-frame (prompt &optional default existing)
  227. "Read the name of a frame, and return it as a string.
  228. Prompts with 1st arg, PROMPT (a string).
  229. The default frame is named by the optional 2nd arg, DEFAULT, if a
  230. string or a frame, or by the `selected-frame', if nil.
  231. Non-nil optional 3rd arg, EXISTING, means to allow only names of
  232. existing frames."
  233. (setq default (if (framep default) (get-frame-name default)
  234. (or default (get-frame-name))))
  235. (unless (stringp default)
  236. (error
  237. "Function `read-frame': DEFAULT arg is neither a frame nor a string"))
  238. (completing-read prompt (make-frame-names-alist)
  239. ;; To limit to live frames:
  240. ;; (function (lambda (fn+f)(frame-live-p (cdr fn+f))))
  241. ;; `frame-name-history' is defined in `frame.el'.
  242. nil existing nil '(frame-name-history . 2) default))
  243. (defun frames-on (buffer &optional frame)
  244. "List of all live frames showing BUFFER (a buffer or its name).
  245. The optional FRAME argument is as for function `get-buffer-window'."
  246. (filtered-frame-list (function (lambda (fr) (get-buffer-window buffer fr)))))
  247. (defun 1-window-frames-on (buffer)
  248. "List of all visible 1-window frames showing BUFFER."
  249. (setq buffer (get-buffer buffer))
  250. (when buffer ; Do nothing if BUFFER is not a buffer.
  251. (let ((frs nil))
  252. (with-current-buffer buffer
  253. (when (buffer-live-p buffer) ; Do nothing if dead buffer.
  254. (dolist (fr (frames-on buffer)) ; Is it better to search through
  255. (save-window-excursion ; frames-on or windows-on?
  256. (select-frame fr)
  257. (when (one-window-p t fr) (push fr frs))))))
  258. frs)))
  259. (defun multi-window-frames-on (buffer)
  260. "List of all visible multi-window frames showing BUFFER."
  261. (setq buffer (get-buffer buffer))
  262. (when buffer ; Do nothing if BUFFER is not a buffer.
  263. (let ((frs nil))
  264. (with-current-buffer buffer
  265. (when (buffer-live-p buffer) ; Do nothing if dead buffer.
  266. (dolist (fr (frames-on buffer)) ; Is it better to search through
  267. (save-window-excursion ; frames-on or windows-on?
  268. (select-frame fr)
  269. (when (not (one-window-p t fr)) (push fr frs))))))
  270. frs)))
  271. (defun flash-ding (&optional do-not-terminate frame)
  272. "Ring bell (`ding'), after flashing FRAME (default: current), if relevant.
  273. Terminates any keyboard macro executing, unless arg DO-NOT-TERMINATE non-nil."
  274. (save-window-excursion
  275. (when frame (select-frame frame))
  276. (let ((visible-bell t)) ; Flash.
  277. (ding do-not-terminate)))
  278. (let ((visible-bell nil))
  279. (ding do-not-terminate))) ; Bell.
  280. ;;;;;;;;;;;;;;;;;;;;;;;
  281. (provide 'frame-fns)
  282. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  283. ;;; frame-fns.el ends here