From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: Using Org for browsing and managing buffers Date: Thu, 15 Apr 2010 00:20:31 -0400 Message-ID: <87mxx5cpo0.fsf@stats.ox.ac.uk> References: <87aatda0gv.fsf@stats.ox.ac.uk> <87d3y1pyuz.wl%ucecesf@ucl.ac.uk> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O2GZG-0000Cn-CL for emacs-orgmode@gnu.org; Thu, 15 Apr 2010 00:20:42 -0400 Received: from [140.186.70.92] (port=34432 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O2GZD-0000C2-Uz for emacs-orgmode@gnu.org; Thu, 15 Apr 2010 00:20:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O2GZA-0005wO-DO for emacs-orgmode@gnu.org; Thu, 15 Apr 2010 00:20:39 -0400 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:54650) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O2GZA-0005w9-2Y for emacs-orgmode@gnu.org; Thu, 15 Apr 2010 00:20:36 -0400 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: e.fraga@ucl.ac.uk Cc: emacs org-mode mailing list --=-=-= Eric S Fraga writes: > On Thu, 08 Apr 2010 21:17:20 -0400, Dan Davison wrote: > >> I've been working on an Org tool to browse Emacs buffers. Emacs has the >> function list-buffers (C-x C-b), where you can view a list of buffers, >> delete buffers, etc. This is intended to be a replacement for >> list-buffers, implemented in Org-mode. > > Excellent! I really like this. I currently use both bs-show and ido > for buffer selection but I could easily be convinced to use this > instead of bs-show. Hi Eric, > Some comments: > >> The buffer has some special key-bindings: > > I have a problem with the current bindings: you are (I guess) trying > to emulate dired to some degree (cf. bindings for RET, 'd', 'o' [1] and > 'x'). It would be nice therefore to have movement bindings as well > ('n' and 'p'). I can obviously bind these myself but 'p' is already > taken and it would be nice to be as consistent with the defaults as > possible. Yes, I think I'm trying to emulate dired while still staying true to Org. But I hadn't thought carefully about the keybindings yet and your comments are very helpful. As an experiment at least, I've now changed things so that Org speed commands are turned on everywhere in the buffer. That means that we gain all the commands listed in `org-speed-commands-default', including n, p, f, b, u for movement. Another one I notice is useful is '.' for outline-mark-subtree, so that '. d x' deletes a group of buffers This does also mean that various inappropriate speed commands become exposed (but then pressing random keys is a strategy that should be confined to gnus). > Maybe 'P' for properties on/off? Done. > When I tried column view, the default widths are much too wide for my > netbook. Can these be changed? I've made them narrower by default, and introduced a customizable variable org-buffers-columns-format. > More importantly, when I left column > view, the buffer view was now different than the original view. Thanks. I've fixed that. > Originally, I had two levels (mode + buffer) and now it was a single > level (buffer). How do I get back to mode+buffer two level view? org-buffers-list:by, which is now bound to B (used to be b). It brings up minibuffer completion for the property you want to group by, so B maj RET would be enough to regroup by major-mode (should you need to). > > Thanks again! > > eric > > Footnotes: > [1] I found 'o' initially annoying as knowing that I was in org, I > expected it to behave like 'o' in agenda (hide other window)... Noted. On the other hand o in Org speed commands is bound to org-open-at-point. I've got rid of my binding and am allowing it to fall through to the speed command default but I'd welcome any further suggestions. Note that the variable org-buffers-follow-link-method can be used so specify the behaviour of RET on a line with a buffer link. Thanks a lot for the comments. The columns view thing is a bit experimental still; I'm not sure what to do about the key bindings, or indeed quite what the purpose of it is in this context, but it did seem like a natural buffer to allow it in. I haven't used bs-show. It might be interesting to try to get some of the power of ibuffer in this mode. But Org is better than the alternatives at hierarchical grouping and tree navigation. And also, the alternatives are very ugly. Current code below and at http://github.com/dandavison/org-buffers. Dan --=-=-= Content-Disposition: inline; filename=org-buffers.el ;;; org-buffers.el --- An Org-mode tool for buffer management ;; Copyright (C) 2010 Dan Davison ;; Author: Dan Davison ;; Keywords: outlines, hypermedia, calendar, wp ;; Homepage: http://orgmode.org ;;; License: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;;; Code: (require 'org) (require 'cl) ;;; Variables (defvar org-buffers-buffer-name "*Buffers*" "Name of buffer in which buffer list is displayed") (defvar org-buffers-state '((:by . "major-mode") (:atom . heading) (:properties . nil)) "Association list specifying the current state of org-buffers.") (defvar org-buffers-follow-link-method 'org-open-at-point "Method used to follow link with RET. Must be one of 'org-open-at-point :: use `org-open-at-point' to follow link. 'current-window :: use switch-to-buffer 'other-window :: use switch-to-buffer-other-window Setting this variable to 'current-window makes the behaviour more consistent with that of `Buffer-menu-mode' and `dired-mode'") (defvar org-buffers-buffer-properties '(("buffer-name" . (buffer-name)) ("major-mode" . (let ((mode (symbol-name major-mode))) (if (string-match "-mode$" mode) (replace-match "" nil t mode) mode))) ("buffer-file-name" . (buffer-file-name)) ("default-directory" . default-directory) ("buffer-modified-p" . (format "%s" (buffer-modified-p)))) "Association list specifying properties to be stored for each buffer. The car of each element is the name of the property, and the cdr is an expression which, when evaluated in the buffer, yields the property value.") (defgroup org-buffers nil "Options for customising `org-buffers-mode'" :tag "Org-buffers Mode" :group 'org) (defcustom org-buffers-columns-format (mapconcat 'identity '("%15buffer-name(Buffer)" "%15major-mode(Mode)" "%20default-directory(Dir)" "%5buffer-modified-p(Modified)") " ") "Format for columns when viewing buffers listing in columns view. See `org-columns-default-format'." :group 'org-buffers) (defcustom org-buffers-excluded-buffers `("*Completions*" ,org-buffers-buffer-name) "List of names of buffers that should not be listed by org-buffers-list." :group 'org-buffers) (defcustom org-buffers-excluded-modes nil "List of names of major-modes (strings) that should not be listed by org-buffers-list." :group 'org-buffers) ;;; Mode (defvar org-buffers-mode-map (make-sparse-keymap)) (defvar org-buffers-mode-hook nil "Hook for functions to be called after buffer listing is created. Note that the buffer is read-only, so if the hook function is to modify the buffer it should use a let binding to temporarily bind inhibit-read-only to a non-nil value.") (define-minor-mode org-buffers-mode "An Org-mode tool for buffer management. In addition to the following org-buffers commands, all the commands listed in `org-speed-commands-default' are available. \\{org-buffers-mode-map}" nil " buffers" nil (org-set-local 'org-tag-alist '(("delete" . ?d))) (org-set-local'org-tags-column -50) (org-set-local 'org-columns-default-format org-buffers-columns-format) (org-set-local 'org-use-speed-commands (lambda () t)) (add-hook 'kill-buffer-hook 'org-buffers-reset-state nil 'local)) (defun org-buffers-help () (interactive) (describe-function 'org-buffers-mode)) ;;; Keys (define-key org-buffers-mode-map "B" 'org-buffers-list:by) (define-key org-buffers-mode-map "c" 'org-buffers-columns-view) (define-key org-buffers-mode-map "d" 'org-buffers-tag-for-deletion) (define-key org-buffers-mode-map "g" 'org-buffers-list:refresh) (define-key org-buffers-mode-map "h" 'org-buffers-toggle-headings) (define-key org-buffers-mode-map "P" 'org-buffers-toggle-properties) (define-key org-buffers-mode-map "q" 'bury-buffer) (define-key org-buffers-mode-map "u" 'org-buffers-remove-tags) (define-key org-buffers-mode-map "x" 'org-buffers-execute-pending-operations) (define-key org-buffers-mode-map [(return)] 'org-buffers-follow-link) (define-key org-buffers-mode-map "?" 'org-buffers-help) ;;; Listing and view cycling (defun org-buffers-list (&optional refresh frame) "Create an Org-mode listing of Emacs buffers. By default, buffers are grouped by major mode. Optional argument FRAME specifies the frame whose buffers should be listed." (interactive) (pop-to-buffer (or (and (not refresh) (get-buffer org-buffers-buffer-name)) (let ((org-buffers-p (equal (buffer-name) org-buffers-buffer-name)) (by (or (org-buffers-state-get :by) "major-mode")) (atom (org-buffers-state-get :atom)) target) (when org-buffers-p (if (and (org-before-first-heading-p) (not (org-on-heading-p))) (outline-next-heading)) (setq target (condition-case nil (org-make-org-heading-search-string) (error nil)))) (with-current-buffer (get-buffer-create org-buffers-buffer-name) (setq buffer-read-only nil) (erase-buffer) (org-mode) (dolist (buffer (sort (remove-if 'org-buffers-exclude-p (mapcar 'buffer-name (buffer-list frame))) 'string<)) (org-insert-heading t) (insert (org-make-link-string (concat "buffer:" buffer) buffer) "\n") (dolist (pair (org-buffers-get-buffer-props buffer)) (org-set-property (car pair) (cdr pair)))) (org-buffers-set-state '((:atom . heading))) (goto-char (point-min)) (unless (equal by "NONE") (org-buffers-group-by by)) (if target (condition-case nil (org-link-search target) (error nil))) (beginning-of-line) (if (equal by "NONE") (org-overview) (case atom ('heading (progn (org-overview) (org-content))) ('line (progn (show-all) (org-buffers-toggle-headings))))) (save-excursion (mark-whole-buffer) (indent-region (point-min) (point-max))) (org-buffers-mode) (setq buffer-read-only t) (current-buffer)))))) (defun org-buffers-list:refresh (&optional arg) "Refresh org-buffers listing." (interactive "P") (if arg (org-buffers-reset-state)) (org-buffers-list 'refresh)) (defun org-buffers-list:by (&optional prop) "Group buffers according to value of property PROP." (interactive) (let ((inhibit-read-only t) (headings-p (org-buffers-state-eq :atom 'heading))) (unless (org-buffers-state-get :properties) (org-buffers-toggle-properties)) (org-buffers-set-state `((:by . ,(or prop (org-completing-read "Property to group by: " (cons "NONE" (mapcar 'car org-buffers-buffer-properties))))))) (org-buffers-list 'refresh) (unless headings-p (org-buffers-toggle-headings)))) (defun org-buffers-toggle-properties () "Toggle entry properties in org-buffers listing buffer. Removing properties may provide a less cluttered appearance for browsing. However, in-buffer properties will be restored during certain operations, such as `org-buffers-list:by'." (interactive) (if (org-buffers-state-get :properties) (progn (org-buffers-delete-properties) (show-all) (org-buffers-set-state '((:properties . nil)))) (org-buffers-set-state '((:atom . heading) (:properties . t))) (org-buffers-list 'refresh))) (defun org-buffers-toggle-headings () "Toggle viewing of buffers as org headings. Headings will be automatically restored during certain operations, such as setting deletion tags." (interactive) (let ((inhibit-read-only t) (headings-p (org-buffers-state-eq :atom 'heading)) (flat-p (org-buffers-state-eq :by "NONE"))) (if (and headings-p (org-buffers-state-get :properties)) (org-buffers-toggle-properties)) (save-excursion (goto-char (point-min)) (if (and (or headings-p (not flat-p)) (not (outline-on-heading-p))) (outline-next-heading)) (if flat-p (progn (push-mark (point) 'nomsg 'activate) (goto-char (point-max)) (org-ctrl-c-star) (pop-mark)) (while (not (eobp)) (push-mark (save-excursion (forward-line 1) (point)) 'nomsg 'activate) (org-forward-same-level 1) (org-ctrl-c-star) (pop-mark))) (mark-whole-buffer) (indent-region (point-min) (point-max))) (org-buffers-set-state `((:atom . ,(if headings-p 'line 'heading)))))) (defun org-buffers-delete-properties () (let ((inhibit-read-only t)) (save-excursion (goto-char (point-min)) (org-buffers-delete-regions (nreverse (org-buffers-map-entries 'org-buffers-get-property-block)))))) (defun org-buffers-get-property-block () "Return the (beg . end) range of the property drawer. Unlike the org version the limits include the keywords delimiting the drawer." (let ((beg (point)) (end (progn (outline-next-heading) (point)))) (goto-char beg) (if (re-search-forward org-property-drawer-re end t) (cons (match-beginning 1) (match-end 0))))) (defun org-buffers-group-by (property) "Group top level headings according to the value of PROPERTY." (let ((atom (org-buffers-state-get :atom))) (save-excursion (goto-char (point-min)) (mapc (lambda (subtree) ;; Create subtree for each value of `property' (org-insert-heading t) (if (> (org-buffers-outline-level) 1) (org-promote)) (insert (car subtree) "\n") (org-insert-subheading t) (mapc 'org-buffers-insert-parsed-entry (cdr subtree))) (prog1 (mapcar (lambda (val) ;; Form list of parsed entries for each unique value of `property' (cons val (org-buffers-parse-selected-entries property val))) (sort (delete-dups (org-buffers-map-entries (lambda () (org-entry-get nil property nil)))) 'string<)) (erase-buffer)))))) (defun org-buffers-exclude-p (buffer) "Return non-nil if BUFFER should not be listed." (or (member (with-current-buffer buffer major-mode) org-buffers-excluded-modes) (member buffer org-buffers-excluded-buffers) (string= (substring buffer 0 1) " "))) (defun org-buffers-reset-state () (org-buffers-set-state '((:by . "major-mode") (:atom . heading) (:properties . nil)))) (defun org-buffers-columns-view () "View buffers in Org-mode columns view. This is currently experimental. RET can be used to follow links in the first column, but certain other org-buffers keys conflict with column-view or otherwise do not work correctly." (interactive) (let ((inhibit-read-only t)) (unless (org-buffers-state-get :properties) (org-buffers-toggle-properties)) (save-excursion (goto-char (point-min)) (org-columns)))) ;;; Parsing and inserting entries (defun org-buffers-parse-selected-entries (prop val) "Parse all entries with property PROP value VAL." (delq nil (org-buffers-map-entries (lambda () (when (equal (org-entry-get nil prop) val) (cons (org-get-heading) (org-get-entry))))))) (defun org-buffers-insert-parsed-entry (entry) "Insert a parsed entry" (unless (org-at-heading-p) (org-insert-heading)) (insert (car entry) "\n") (if (org-buffers-state-get :properties) (insert (cdr entry)))) (defun org-buffers-get-buffer-props (buffer) "Create alist of properties of BUFFER, as strings." (with-current-buffer buffer (mapcar (lambda (pair) (cons (car pair) (eval (cdr pair)))) org-buffers-buffer-properties))) ;;; Follow-link behaviour (defun org-buffers-follow-link () "Follow link to buffer on this line. The buffer-switching behaviour of this function is determined by the variable `org-buffers-follow-link-method'. See also `org-buffers-switch-to-buffer' and `org-buffers-switch-to-buffer-other-window', whose behaviour is hard-wired." (interactive) (org-buffers-switch-to-buffer-generic org-buffers-follow-link-method)) (defun org-buffers-switch-to-buffer () "Switch to this entry's buffer in current window." (interactive) (org-buffers-switch-to-buffer-generic 'current-window)) (defun org-buffers-switch-to-buffer-other-window () "Switch to this entry's buffer in other window." (interactive) (org-buffers-switch-to-buffer-generic 'other-window)) (defun org-buffers-switch-to-buffer-generic (method) (save-excursion (let ((atom (org-buffers-state-get :atom)) buffer) (cond ((eq atom 'heading) (org-back-to-heading)) (t (beginning-of-line))) (setq buffer (org-buffers-get-buffer-name)) (if (get-buffer buffer) (case method ('org-open-at-point (org-open-at-point)) ('current-window (switch-to-buffer buffer)) ('other-window (switch-to-buffer-other-window buffer))) (error "No such buffer: %s" buffer))))) (defun org-buffers-get-buffer-name () "Get buffer-name for current entry." (let ((headings-p (org-buffers-state-eq :atom 'heading))) (or (and headings-p (org-entry-get nil "buffer-name")) (and (save-excursion (if headings-p (org-back-to-heading)) (re-search-forward "\\[\\[buffer:\\([^\]]*\\)" (point-at-eol) t)) (org-link-unescape (match-string 1)))))) ;;; Setting tags and executing operations (defun org-buffers-tag-for-deletion () "Mark buffer for deletion. If a region is selected, all buffers in the region are marked for deletion. Buffers marked for deletion can be deleted using `org-buffers-execute-pending-operations'." (interactive) (org-buffers-set-tags '("delete"))) (defun org-buffers-remove-tags () "Remove deletion marks from buffers. If a region is selected, marks are removed from all buffers in the region." (interactive) (org-buffers-set-tags nil)) (defun org-buffers-set-tags (data) "Set tags to DATA at all non top-level headings in region. DATA should be a list of strings. If DATA is nil, remove all tags at such headings." (let* ((inhibit-read-only t) (region-p (org-region-active-p)) (beg (if region-p (region-beginning) (point))) (end (if region-p (region-end) (point))) (headings-p (org-buffers-state-eq :atom 'heading)) beg-line end-line end-of-last-heading) (save-excursion (setq beg-line (progn (goto-char beg) (org-current-line)) end-line (progn (goto-char end) (org-current-line)) end-of-last-heading (progn (outline-end-of-heading) (point))) (if headings-p (setq end (if (and region-p (not (eq end-line beg-line)) (not (eq end end-of-last-heading))) (progn (goto-char end) (org-back-to-heading) (point)) end-of-last-heading) beg (progn (goto-char beg) (point-at-bol))) (org-buffers-toggle-headings) ;; doesn't alter line numbers (setq beg (progn (org-goto-line beg-line) (point-at-bol)) end (if (eq end-line beg-line) (point-at-eol) (progn (org-goto-line end-line) (point-at-bol))))) (narrow-to-region beg end) (goto-char (point-min)) (org-buffers-map-entries (lambda () (when (or (org-buffers-state-eq :by "NONE") (> (org-outline-level) 1)) (org-set-tags-to (if data (delete-duplicates (append data (org-get-tags)) :test 'string-equal)))))) (widen) (org-content)) (unless region-p (outline-next-heading) (unless (or (> (org-outline-level) 1) (org-buffers-state-eq :by "NONE")) (outline-next-heading))) (unless headings-p (org-buffers-toggle-headings)))) (defun org-buffers-execute-pending-operations () "Execute all pending operations. Currently the only type of operation supported is deletion. Buffers are tagged for deletion using `org-buffers-tag-for-deletion'. Remove such tags from buffers using `org-buffers-remove-tags'." (interactive) (let ((inhibit-read-only t) (headings-p (org-buffers-state-eq :atom 'heading)) buffer) (unless headings-p (org-buffers-toggle-headings)) (org-buffers-delete-regions (nreverse (org-buffers-map-entries (lambda () (if (setq buffer (org-buffers-get-buffer-name)) (if (not (kill-buffer buffer)) (error "Failed to kill buffer %s" buffer) (cons (point) (1+ (org-end-of-subtree)))))) "+delete"))) (org-buffers-delete-regions (nreverse (org-buffers-map-entries (lambda () (if (and (eq (org-outline-level) 1) (eq (point-at-eol) (org-end-of-subtree))) (cons (point-at-bol) (1+ (point)))))))) (unless headings-p (org-buffers-toggle-headings)))) ;;; Utilities (defun org-buffers-map-entries (func &optional match) (org-scan-tags func (if match (cdr (org-make-tags-matcher match)) t))) (defun org-buffers-set-state (state) "Add STATE to global state list. New settings have precedence over existing ones." (mapc (lambda (pair) (unless (assoc (car pair) state) (add-to-list 'state pair))) org-buffers-state) (setq org-buffers-state state)) (defmacro org-buffers-delete-regions (regions) "Delete regions in list. REGIONS is a list of (beg . end) cons cells specifying buffer regions." `(mapc (lambda (pair) (if pair (delete-region (car pair) (cdr pair)))) ,regions)) (defmacro org-buffers-state-get (key) `(cdr (assoc ,key org-buffers-state))) (defmacro org-buffers-state-eq (key val) `(equal (org-buffers-state-get ,key) ,val)) (defmacro org-buffers-outline-level () '(save-excursion (beginning-of-line) (org-outline-level))) ;;; Links to buffers (org-add-link-type "buffer" 'display-buffer) (add-hook 'org-store-link-functions 'org-buffers-store-link) (defun org-buffers-store-link (&optional force) "Store a link to an Emacs buffer. Returns nil by default, to avoid hijacking other link types." (if force (let* ((target (buffer-name)) (desc target) link) (org-store-link-props :type "buffer") (setq link (org-make-link "buffer:" target)) (org-add-link-props :link link :description desc) link))) (provide 'org-buffers) ;;; org-buffers.el ends here --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode --=-=-=--