gnus-win.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. ;;; gnus-win.el --- window configuration functions for Gnus
  2. ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'gnus)
  20. (require 'gnus-util)
  21. (defgroup gnus-windows nil
  22. "Window configuration."
  23. :group 'gnus)
  24. (defcustom gnus-use-full-window t
  25. "*If non-nil, use the entire Emacs screen."
  26. :group 'gnus-windows
  27. :type 'boolean)
  28. (defcustom gnus-window-min-width 2
  29. "*Minimum width of Gnus buffers."
  30. :group 'gnus-windows
  31. :type 'integer)
  32. (defcustom gnus-window-min-height 1
  33. "*Minimum height of Gnus buffers."
  34. :group 'gnus-windows
  35. :type 'integer)
  36. (defcustom gnus-always-force-window-configuration nil
  37. "*If non-nil, always force the Gnus window configurations."
  38. :group 'gnus-windows
  39. :type 'boolean)
  40. (defcustom gnus-use-frames-on-any-display nil
  41. "*If non-nil, frames on all displays will be considered usable by Gnus.
  42. When nil, only frames on the same display as the selected frame will be
  43. used to display Gnus windows."
  44. :version "22.1"
  45. :group 'gnus-windows
  46. :type 'boolean)
  47. (defvar gnus-buffer-configuration
  48. '((group
  49. (vertical 1.0
  50. (group 1.0 point)))
  51. (summary
  52. (vertical 1.0
  53. (summary 1.0 point)))
  54. (article
  55. (cond
  56. (gnus-use-trees
  57. '(vertical 1.0
  58. (summary 0.25 point)
  59. (tree 0.25)
  60. (article 1.0)))
  61. (t
  62. '(vertical 1.0
  63. (summary 0.25 point)
  64. (article 1.0)))))
  65. (server
  66. (vertical 1.0
  67. (server 1.0 point)))
  68. (browse
  69. (vertical 1.0
  70. (browse 1.0 point)))
  71. (message
  72. (vertical 1.0
  73. (message 1.0 point)))
  74. (pick
  75. (vertical 1.0
  76. (article 1.0 point)))
  77. (info
  78. (vertical 1.0
  79. (info 1.0 point)))
  80. (summary-faq
  81. (vertical 1.0
  82. (summary 0.25)
  83. (faq 1.0 point)))
  84. (only-article
  85. (vertical 1.0
  86. (article 1.0 point)))
  87. (edit-article
  88. (vertical 1.0
  89. (article 1.0 point)))
  90. (edit-form
  91. (vertical 1.0
  92. (group 0.5)
  93. (edit-form 1.0 point)))
  94. (edit-score
  95. (vertical 1.0
  96. (summary 0.25)
  97. (edit-score 1.0 point)))
  98. (edit-server
  99. (vertical 1.0
  100. (server 0.5)
  101. (edit-form 1.0 point)))
  102. (post
  103. (vertical 1.0
  104. (post 1.0 point)))
  105. (reply
  106. (vertical 1.0
  107. (article 0.5)
  108. (message 1.0 point)))
  109. (forward
  110. (vertical 1.0
  111. (message 1.0 point)))
  112. (reply-yank
  113. (vertical 1.0
  114. (message 1.0 point)))
  115. (mail-bounce
  116. (vertical 1.0
  117. (article 0.5)
  118. (message 1.0 point)))
  119. (pipe
  120. (vertical 1.0
  121. (summary 0.25 point)
  122. ("*Shell Command Output*" 1.0)))
  123. (bug
  124. (vertical 1.0
  125. (if gnus-bug-create-help-buffer '("*Gnus Help Bug*" 0.5))
  126. ("*Gnus Bug*" 1.0 point)))
  127. (score-trace
  128. (vertical 1.0
  129. (summary 0.5 point)
  130. ("*Score Trace*" 1.0)))
  131. (score-words
  132. (vertical 1.0
  133. (summary 0.5 point)
  134. ("*Score Words*" 1.0)))
  135. (split-trace
  136. (vertical 1.0
  137. (summary 0.5 point)
  138. ("*Split Trace*" 1.0)))
  139. (category
  140. (vertical 1.0
  141. (category 1.0)))
  142. (compose-bounce
  143. (vertical 1.0
  144. (article 0.5)
  145. (message 1.0 point)))
  146. (display-term
  147. (vertical 1.0
  148. ("*display*" 1.0)))
  149. (mml-preview
  150. (vertical 1.0
  151. (message 0.5)
  152. (mml-preview 1.0 point))))
  153. "Window configuration for all possible Gnus buffers.
  154. See the Gnus manual for an explanation of the syntax used.")
  155. (defvar gnus-window-to-buffer
  156. '((group . gnus-group-buffer)
  157. (summary . gnus-summary-buffer)
  158. (article . gnus-article-buffer)
  159. (server . gnus-server-buffer)
  160. (browse . "*Gnus Browse Server*")
  161. (edit-group . gnus-group-edit-buffer)
  162. (edit-form . gnus-edit-form-buffer)
  163. (edit-server . gnus-server-edit-buffer)
  164. (edit-score . gnus-score-edit-buffer)
  165. (message . gnus-message-buffer)
  166. (mail . gnus-message-buffer)
  167. (post-news . gnus-message-buffer)
  168. (faq . gnus-faq-buffer)
  169. (tree . gnus-tree-buffer)
  170. (score-trace . "*Score Trace*")
  171. (split-trace . "*Split Trace*")
  172. (info . gnus-info-buffer)
  173. (category . gnus-category-buffer)
  174. (article-copy . gnus-article-copy)
  175. (draft . gnus-draft-buffer)
  176. (mml-preview . mml-preview-buffer))
  177. "Mapping from short symbols to buffer names or buffer variables.")
  178. (defcustom gnus-configure-windows-hook nil
  179. "*A hook called when configuring windows."
  180. :version "22.1"
  181. :group 'gnus-windows
  182. :type 'hook)
  183. ;;; Internal variables.
  184. (defvar gnus-current-window-configuration nil
  185. "The most recently set window configuration.")
  186. (defvar gnus-created-frames nil)
  187. (defvar gnus-window-frame-focus nil)
  188. (defun gnus-kill-gnus-frames ()
  189. "Kill all frames Gnus has created."
  190. (while gnus-created-frames
  191. (when (frame-live-p (car gnus-created-frames))
  192. ;; We slap a condition-case around this `delete-frame' to ensure
  193. ;; against errors if we try do delete the single frame that's left.
  194. (ignore-errors
  195. (delete-frame (car gnus-created-frames))))
  196. (pop gnus-created-frames)))
  197. ;;;###autoload
  198. (defun gnus-add-configuration (conf)
  199. "Add the window configuration CONF to `gnus-buffer-configuration'."
  200. (setq gnus-buffer-configuration
  201. (cons conf (delq (assq (car conf) gnus-buffer-configuration)
  202. gnus-buffer-configuration))))
  203. (defvar gnus-frame-list nil)
  204. (defun gnus-window-to-buffer-helper (obj)
  205. (cond ((not (symbolp obj))
  206. obj)
  207. ((boundp obj)
  208. (symbol-value obj))
  209. ((fboundp obj)
  210. (funcall obj))
  211. (t
  212. nil)))
  213. (defun gnus-configure-frame (split &optional window)
  214. "Split WINDOW according to SPLIT."
  215. (let* ((current-window (or (get-buffer-window (current-buffer)) (selected-window)))
  216. (window (or window current-window)))
  217. (select-window window)
  218. ;; The SPLIT might be something that is to be evalled to
  219. ;; return a new SPLIT.
  220. (while (and (not (assq (car split) gnus-window-to-buffer))
  221. (symbolp (car split)) (fboundp (car split)))
  222. (setq split (eval split)))
  223. (let* ((type (car split))
  224. (subs (cddr split))
  225. (len (if (eq type 'horizontal) (window-width) (window-height)))
  226. (total 0)
  227. (window-min-width (or gnus-window-min-width window-min-width))
  228. (window-min-height (or gnus-window-min-height window-min-height))
  229. s result new-win rest comp-subs size sub)
  230. (cond
  231. ;; Nothing to do here.
  232. ((null split))
  233. ;; Don't switch buffers.
  234. ((null type)
  235. (and (memq 'point split) window))
  236. ;; This is a buffer to be selected.
  237. ((not (memq type '(frame horizontal vertical)))
  238. (let ((buffer (cond ((stringp type) type)
  239. (t (cdr (assq type gnus-window-to-buffer))))))
  240. (unless buffer
  241. (error "Invalid buffer type: %s" type))
  242. (let ((buf (gnus-get-buffer-create
  243. (gnus-window-to-buffer-helper buffer))))
  244. (when (buffer-name buf)
  245. (if (eq buf (window-buffer (selected-window)))
  246. (set-buffer buf)
  247. (switch-to-buffer buf))))
  248. (when (memq 'frame-focus split)
  249. (setq gnus-window-frame-focus window))
  250. ;; We return the window if it has the `point' spec.
  251. (and (memq 'point split) window)))
  252. ;; This is a frame split.
  253. ((eq type 'frame)
  254. (unless gnus-frame-list
  255. (setq gnus-frame-list (list (window-frame current-window))))
  256. (let ((i 0)
  257. params frame fresult)
  258. (while (< i (length subs))
  259. ;; Frame parameter is gotten from the sub-split.
  260. (setq params (cadr (elt subs i)))
  261. ;; It should be a list.
  262. (unless (listp params)
  263. (setq params nil))
  264. ;; Create a new frame?
  265. (unless (setq frame (elt gnus-frame-list i))
  266. (nconc gnus-frame-list (list (setq frame (make-frame params))))
  267. (push frame gnus-created-frames))
  268. ;; Is the old frame still alive?
  269. (unless (frame-live-p frame)
  270. (setcar (nthcdr i gnus-frame-list)
  271. (setq frame (make-frame params))))
  272. ;; Select the frame in question and do more splits there.
  273. (select-frame frame)
  274. (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
  275. (incf i))
  276. ;; Select the frame that has the selected buffer.
  277. (when fresult
  278. (select-frame (window-frame fresult)))))
  279. ;; This is a normal split.
  280. (t
  281. (when (> (length subs) 0)
  282. ;; First we have to compute the sizes of all new windows.
  283. (while subs
  284. (setq sub (append (pop subs) nil))
  285. (while (and (not (assq (car sub) gnus-window-to-buffer))
  286. (symbolp (car sub)) (fboundp (car sub)))
  287. (setq sub (eval sub)))
  288. (when sub
  289. (push sub comp-subs)
  290. (setq size (cadar comp-subs))
  291. (cond ((equal size 1.0)
  292. (setq rest (car comp-subs))
  293. (setq s 0))
  294. ((floatp size)
  295. (setq s (floor (* size len))))
  296. ((integerp size)
  297. (setq s size))
  298. (t
  299. (error "Invalid size: %s" size)))
  300. ;; Try to make sure that we are inside the safe limits.
  301. (cond ((zerop s))
  302. ((eq type 'horizontal)
  303. (setq s (max s window-min-width)))
  304. ((eq type 'vertical)
  305. (setq s (max s window-min-height))))
  306. (setcar (cdar comp-subs) s)
  307. (incf total s)))
  308. ;; Take care of the "1.0" spec.
  309. (if rest
  310. (setcar (cdr rest) (- len total))
  311. (error "No 1.0 specs in %s" split))
  312. ;; The we do the actual splitting in a nice recursive
  313. ;; fashion.
  314. (setq comp-subs (nreverse comp-subs))
  315. (while comp-subs
  316. (if (null (cdr comp-subs))
  317. (setq new-win window)
  318. (setq new-win
  319. (split-window window (cadar comp-subs)
  320. (eq type 'horizontal))))
  321. (setq result (or (gnus-configure-frame
  322. (car comp-subs) window)
  323. result))
  324. (select-window new-win)
  325. (setq window new-win)
  326. (setq comp-subs (cdr comp-subs))))
  327. ;; Return the proper window, if any.
  328. (when result
  329. (select-window result)))))))
  330. (defvar gnus-frame-split-p nil)
  331. (defun gnus-configure-windows (setting &optional force)
  332. (cond
  333. ((null setting)
  334. ;; Do nothing.
  335. )
  336. ((window-configuration-p setting)
  337. (set-window-configuration setting))
  338. (t
  339. (setq gnus-current-window-configuration setting)
  340. (setq force (or force gnus-always-force-window-configuration))
  341. (let ((split (if (symbolp setting)
  342. (cadr (assq setting gnus-buffer-configuration))
  343. setting))
  344. all-visible)
  345. (setq gnus-frame-split-p nil)
  346. (unless split
  347. (error "No such setting in `gnus-buffer-configuration': %s" setting))
  348. (if (and (setq all-visible (gnus-all-windows-visible-p split))
  349. (not force))
  350. ;; All the windows mentioned are already visible, so we just
  351. ;; put point in the assigned buffer, and do not touch the
  352. ;; winconf.
  353. (select-window all-visible)
  354. ;; Make sure "the other" buffer, nntp-server-buffer, is live.
  355. (unless (gnus-buffer-live-p nntp-server-buffer)
  356. (nnheader-init-server-buffer))
  357. ;; Either remove all windows or just remove all Gnus windows.
  358. (let ((frame (selected-frame)))
  359. (unwind-protect
  360. (if gnus-use-full-window
  361. ;; We want to remove all other windows.
  362. (if (not gnus-frame-split-p)
  363. ;; This is not a `frame' split, so we ignore the
  364. ;; other frames.
  365. (delete-other-windows)
  366. ;; This is a `frame' split, so we delete all windows
  367. ;; on all frames.
  368. (gnus-delete-windows-in-gnusey-frames))
  369. ;; Just remove some windows.
  370. (gnus-remove-some-windows)
  371. (if (featurep 'xemacs)
  372. (switch-to-buffer nntp-server-buffer)
  373. (set-buffer nntp-server-buffer)))
  374. (select-frame frame)))
  375. (let (gnus-window-frame-focus)
  376. (if (featurep 'xemacs)
  377. (switch-to-buffer nntp-server-buffer)
  378. (set-buffer nntp-server-buffer))
  379. (gnus-configure-frame split)
  380. (run-hooks 'gnus-configure-windows-hook)
  381. (when gnus-window-frame-focus
  382. (gnus-select-frame-set-input-focus
  383. (window-frame gnus-window-frame-focus)))))))))
  384. (defun gnus-delete-windows-in-gnusey-frames ()
  385. "Do a `delete-other-windows' in all frames that have Gnus windows."
  386. (let ((buffers (gnus-buffers)))
  387. (mapcar
  388. (lambda (frame)
  389. (unless (eq (cdr (assq 'minibuffer
  390. (frame-parameters frame)))
  391. 'only)
  392. (select-frame frame)
  393. (let (do-delete)
  394. (walk-windows
  395. (lambda (window)
  396. (when (memq (window-buffer window) buffers)
  397. (setq do-delete t))))
  398. (when do-delete
  399. (delete-other-windows)))))
  400. (frame-list))))
  401. (defun gnus-all-windows-visible-p (split)
  402. "Say whether all buffers in SPLIT are currently visible.
  403. In particular, the value returned will be the window that
  404. should have point."
  405. (let ((stack (list split))
  406. (all-visible t)
  407. type buffer win buf)
  408. (while (and (setq split (pop stack))
  409. all-visible)
  410. (when (consp (car split))
  411. (push 1.0 split)
  412. (push 'vertical split))
  413. ;; The SPLIT might be something that is to be evalled to
  414. ;; return a new SPLIT.
  415. (while (and (not (assq (car split) gnus-window-to-buffer))
  416. (symbolp (car split)) (fboundp (car split)))
  417. (setq split (eval split)))
  418. (setq type (elt split 0))
  419. (cond
  420. ;; Nothing here.
  421. ((null split) t)
  422. ;; A buffer.
  423. ((not (memq type '(horizontal vertical frame)))
  424. (setq buffer (cond ((stringp type) type)
  425. (t (cdr (assq type gnus-window-to-buffer)))))
  426. (unless buffer
  427. (error "Invalid buffer type: %s" type))
  428. (if (and (setq buf (get-buffer (gnus-window-to-buffer-helper buffer)))
  429. (buffer-live-p buf)
  430. (setq win (gnus-get-buffer-window buf t)))
  431. (if (memq 'point split)
  432. (setq all-visible win))
  433. (setq all-visible nil)))
  434. (t
  435. (when (eq type 'frame)
  436. (setq gnus-frame-split-p t))
  437. (setq stack (append (cddr split) stack)))))
  438. (unless (eq all-visible t)
  439. all-visible)))
  440. (defun gnus-window-top-edge (&optional window)
  441. "Return the top coordinate of WINDOW."
  442. (nth 1 (window-edges window)))
  443. (defun gnus-remove-some-windows ()
  444. (let ((buffers (gnus-buffers))
  445. buf bufs lowest-buf lowest)
  446. (save-excursion
  447. ;; Remove windows on all known Gnus buffers.
  448. (while (setq buf (pop buffers))
  449. (when (get-buffer-window buf)
  450. (push buf bufs)
  451. (pop-to-buffer buf)
  452. (when (or (not lowest)
  453. (< (gnus-window-top-edge) lowest))
  454. (setq lowest (gnus-window-top-edge)
  455. lowest-buf buf))))
  456. (when lowest-buf
  457. (pop-to-buffer lowest-buf)
  458. (if (featurep 'xemacs)
  459. (switch-to-buffer nntp-server-buffer)
  460. (set-buffer nntp-server-buffer)))
  461. (mapcar (lambda (b) (delete-windows-on b t))
  462. (delq lowest-buf bufs)))))
  463. (eval-and-compile
  464. (cond
  465. ((fboundp 'frames-on-display-list)
  466. (defalias 'gnus-frames-on-display-list 'frames-on-display-list))
  467. ((and (featurep 'xemacs) (fboundp 'frame-device))
  468. (defun gnus-frames-on-display-list ()
  469. (apply 'filtered-frame-list 'identity (list (frame-device nil)))))
  470. (t
  471. (defalias 'gnus-frames-on-display-list 'frame-list))))
  472. (defun gnus-get-buffer-window (buffer &optional frame)
  473. (cond ((and (null gnus-use-frames-on-any-display)
  474. (memq frame '(t 0 visible)))
  475. (car
  476. (let ((frames (gnus-frames-on-display-list)))
  477. (gnus-remove-if (lambda (win) (not (memq (window-frame win)
  478. frames)))
  479. (get-buffer-window-list buffer nil frame)))))
  480. (t
  481. (get-buffer-window buffer frame))))
  482. (provide 'gnus-win)
  483. ;;; gnus-win.el ends here