1 ;;; org-panel.el --- Simple routines for us with bad memory
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Thu Nov 15 15:35:03 2007
6 ;; Lxast-Updated: Wed Nov 21 03:06:03 2007 (3600 +0100)
11 ;; Features that might be required by this library:
13 ;; `easymenu', `font-lock', `noutline', `org', `outline', `syntax',
16 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20 ;; This defines a kind of control panel for `org-mode'. This control
21 ;; panel should make it fast to move around and edit structure etc.
23 ;; To bring up the control panel type
27 ;; Type ? there for help.
29 ;; I suggest you add the following to your .emacs for quick access of
32 ;; (eval-after-load 'org-mode
33 ;; (define-key org-mode-map [(control ?c) ?p] 'orgpan-panel))
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;; This program is free software; you can redistribute it and/or
43 ;; modify it under the terms of the GNU General Public License as
44 ;; published by the Free Software Foundation; either version 2, or
45 ;; (at your option) any later version.
47 ;; This program is distributed in the hope that it will be useful,
48 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
49 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50 ;; General Public License for more details.
52 ;; You should have received a copy of the GNU General Public License
53 ;; along with this program; see the file COPYING. If not, write to
54 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
55 ;; Floor, Boston, MA 02110-1301, USA.
57 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64 ;; Fix-me: this is for testing. A minor mode version interferes badly
65 ;; with emulation minor modes. On the other hand, the other version
66 ;; interferes badly with (interactive ...).
67 (defvar orgpan-minor-mode-version t
)
70 '((t (:inherit
'widget-field
)))
73 (defvar orgpan-field-face
'orgpan-field
)
75 (defface orgpan-active-field
76 '((t (:inherit
'highlight
)))
79 (defvar orgpan-active-field-face
'orgpan-active-field
)
81 (defface orgpan-spaceline
83 "Face for spacing lines."
86 (defcustom orgpan-panel-buttons nil
87 "Panel style, if non-nil use buttons.
88 If there are buttons in the panel they are used to change the way
89 the arrow keys work. The panel looks something like this, with
90 the first button chosen:
92 [Navigate] [Restructure] [TODO/Priority]
94 up/down, left: Go to, right: Visibility
96 The line below the buttons try to give a short hint about what
97 the arrow keys does. \(Personally I prefer the version without
98 buttons since I then do not have to remember which button is
103 ;; Fix-me: add org-mode-map
104 (defconst orgpan-org-mode-commands nil
)
105 (defconst orgpan-org-commands
117 outline-next-visible-heading
118 outline-previous-visible-heading
119 outline-forward-same-level
120 outline-backward-same-level
126 org-move-subtree-down
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;; Hook functions etc
136 ;;(defvar orgpan-this-panel-window nil)
138 (defun orgpan-delete-panel ()
141 (when (buffer-live-p orgpan-panel-buffer
)
142 (delete-windows-on orgpan-panel-buffer
)
143 (kill-buffer orgpan-panel-buffer
))
144 (setq orgpan-panel-buffer nil
)
145 (setq orgpan-panel-window nil
)
146 (orgpan-panel-minor-mode 0)
147 (remove-hook 'post-command-hook
'orgpan-minor-post-command
)
148 (remove-hook 'post-command-hook
'orgpan-mode-post-command
)
149 ;;(remove-hook 'window-configuration-change-hook 'orgpan-window-config-change)
152 (defvar orgpan-last-command-was-from-panel nil
)
153 (defun orgpan-mode-pre-command ()
154 (setq orgpan-last-command-was-from-panel nil
)
156 (if (not (and (windowp orgpan-org-window
)
157 (window-live-p orgpan-org-window
)))
159 (setq this-command
'ignore
)
160 (orgpan-delete-panel)
161 (message "The window belonging to the panel had disappeared, removed panel."))
162 (let ((buf (window-buffer orgpan-org-window
)))
163 (when (with-current-buffer buf
164 (derived-mode-p 'org-mode
))
165 (setq orgpan-last-org-buffer buf
))
166 ;; Fix me: add a list of those commands that are not
167 ;; meaningful from the panel (for example org-time-stamp)
168 (when (or (memq this-command orgpan-org-commands
)
169 (memq this-command orgpan-org-mode-commands
)
170 ;; For some reason not all org commands are found above:
171 (string= "org-" (substring (format "%s" this-command
) 0 4)))
172 (if (not (with-current-buffer buf
173 (derived-mode-p 'org-mode
)))
175 (if (buffer-live-p orgpan-org-buffer
)
176 (set-window-buffer orgpan-org-window orgpan-org-buffer
)
177 (message "Please use `l' or `b' to choose an org-mode buffer"))
178 (setq this-command
'ignore
))
179 (setq orgpan-org-buffer
(window-buffer orgpan-org-window
))
180 (setq orgpan-last-command-was-from-panel t
)
181 (select-window orgpan-org-window
)
182 ;;(when (active-minibuffer-window
183 ;;(set-buffer orgpan-org-buffer)
185 (error (lwarn 't
:warning
"orgpan-pre: %S" err
))))
187 (defun orgpan-mode-post-command ()
190 (unless (and (windowp orgpan-panel-window
)
191 (window-live-p orgpan-panel-window
)
192 (bufferp orgpan-panel-buffer
)
193 (buffer-live-p orgpan-panel-buffer
))
194 ;;(orgpan-delete-panel)
196 (when (and orgpan-last-command-was-from-panel
197 (windowp orgpan-panel-window
)
198 (window-live-p orgpan-panel-window
))
199 (select-window orgpan-panel-window
)
200 (when (derived-mode-p 'orgpan-mode
)
201 (setq deactivate-mark t
)
202 (when orgpan-panel-buttons
203 (unless (and orgpan-point
204 (= (point) orgpan-point
))
205 ;; Go backward so it is possible to click on a "button":
206 (orgpan-backward-field)))))
207 (setq orgpan-this-panel-window nil
))
208 (error (lwarn 't
:warning
"orgpan-post: %S" err
))))
210 ;; (defun orgpan-window-config-change ()
211 ;; "Check if any frame is displaying an orgpan panel.
212 ;; If not remove `orgpan-mode-post-command' and this function from
214 ;; (condition-case err
216 ;; (let ((found-pan nil))
217 ;; (dolist (f (frame-list))
218 ;; (dolist (w (window-list f 'nomini))
219 ;; (with-current-buffer (window-buffer w)
220 ;; (when (derived-mode-p 'orgpan-mode)
221 ;; (setq found-pan t)))))
223 ;; (remove-hook 'post-command-hook 'orgpan-mode-post-command)
224 ;; (remove-hook 'window-configuration-change-hook 'orgpan-window-config-change)))
225 ;; (error (lwarn 't :warning "Error in orgpan-config-change: %S" err))))
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 (defun orgpan-last-buffer ()
232 "Open last org-mode buffer in panels org window."
234 (let ((buf (window-buffer orgpan-org-window
))
235 (last-buf orgpan-last-org-buffer
))
236 (when (with-current-buffer buf
237 (derived-mode-p 'org-mode
))
238 (setq orgpan-last-org-buffer buf
))
239 (when (eq last-buf buf
)
242 (orgpan-switch-buffer)
243 (set-window-buffer orgpan-org-window last-buf
))))
245 (defun orgpan-switch-buffer ()
246 "Switch to next org-mode buffer in panels org window."
248 (let ((buf (window-buffer orgpan-org-window
))
250 (with-current-buffer buf
251 (when (derived-mode-p 'org-mode
)
253 (setq orgpan-last-org-buffer buf
)))
254 (setq org-buffers
(delq nil
(mapcar (lambda (buf)
255 (when (with-current-buffer buf
256 (derived-mode-p 'org-mode
))
259 (setq org-buffers
(delq buf org-buffers
))
260 (set-window-buffer orgpan-org-window
(car org-buffers
))
261 (setq orgpan-org-buffer
(car org-buffers
))))
263 (defun orgpan-paste-subtree ()
265 (if (y-or-n-p "Paste subtree here? ")
267 (message "Nothing was pasted")))
269 (defun orgpan-cut-subtree ()
271 (let ((heading (progn
272 (org-back-to-heading)
273 (buffer-substring (point) (line-end-position))
275 (if (y-or-n-p (format "Do you want to cut the subtree\n%s\n? " heading
))
277 (message "Nothing was cut"))))
279 (defun orgpan-copy-subtree ()
281 (let ((heading (progn
282 (org-back-to-heading)
283 (buffer-substring (point) (line-end-position))
285 (if (y-or-n-p (format "Do you want to copy the subtree\n%s\n? " heading
))
287 (message "Nothing was copied"))))
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 (defvar orgpan-ovl-help nil
)
295 (defun orgpan-check-panel-mode ()
296 (unless (derived-mode-p 'orgpan-mode
)
297 (error "Not orgpan-mode in buffer: " major-mode
)))
299 (defun orgpan-display-bindings-help ()
300 (orgpan-check-panel-mode)
301 (setq orgpan-point
(point))
302 (let* ((ovls (overlays-at (point)))
304 (help (when ovl
(overlay-get ovl
'orgpan-explain
))))
305 (dolist (o (overlays-in (point-min) (point-max)))
306 (overlay-put o
'face orgpan-field-face
))
307 (overlay-put ovl
'face orgpan-active-field-face
)
308 (overlay-put orgpan-ovl-help
'before-string help
)))
310 (defun orgpan-forward-field ()
312 (orgpan-check-panel-mode)
313 (let ((pos (next-overlay-change (point))))
314 (unless (overlays-at pos
)
315 (setq pos
(next-overlay-change pos
)))
316 (when (= pos
(point-max))
317 (setq pos
(point-min))
318 (unless (overlays-at pos
)
319 (setq pos
(next-overlay-change pos
))))
321 (orgpan-display-bindings-help))
323 (defun orgpan-backward-field ()
325 (orgpan-check-panel-mode)
326 (when (= (point) (point-min))
327 (goto-char (point-max)))
328 (let ((pos (previous-overlay-change (point))))
329 (unless (overlays-at pos
)
330 (setq pos
(previous-overlay-change pos
)))
332 (orgpan-display-bindings-help))
335 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 (defconst orgpan-mode-map
339 ;; Fix-me: clean up here!
340 ;; Fix-me: viper support
341 (let ((map (make-sparse-keymap)))
342 (define-key map
[?q
] 'orgpan-delete-panel
)
343 (define-key map
[??
] 'orgpan-help
)
345 (define-key map
[?c
] 'orgpan-copy-subtree
)
346 (define-key map
[?x
] 'orgpan-cut-subtree
)
347 (define-key map
[?p
] 'orgpan-paste-subtree
)
348 (define-key map
[?z
] 'undo
)
350 (define-key map
[?b
] 'orgpan-switch-buffer
)
351 (define-key map
[?l
] 'orgpan-last-buffer
)
352 ;; Some keys for moving between headings. Emacs keys for next/prev
354 (define-key map
[(control ?p
)] 'outline-previous-visible-heading
)
355 (define-key map
[(control ?n
)] 'outline-next-visible-heading
)
356 (define-key map
[(shift control ?p
)] 'outline-backward-same-level
)
357 (define-key map
[(shift control ?n
)] 'outline-forward-same-level
)
358 ;; A mnemunic for up:
359 (define-key map
[(control ?u
)] 'outline-up-heading
)
360 ;; Search sparse tree:
362 ;; Fix-me: Search does not work, some problem with
363 ;; interactive. Probably have to turn the whole thing around and
364 ;; always be in the org buffer, but with a minor mode running
367 ;;(define-key map [?s] 'org-sparse-tree)
368 (define-key map
[?s
] 'orgpan-occur
)
369 ;; Same as in org-mode:
370 ;;(define-key map [(control ?c)(control ?v)] 'org-show-todo-tree)
371 ;; Fix-me: This leads to strange problems:
372 ;;(define-key map [t] 'ignore)
375 (defun orgpan-occur ()
376 "Replacement for `org-occur'.
379 (let ((rgx (read-from-minibuffer "my mini Regexp: ")))
380 (setq orgpan-last-command-was-from-panel t
)
381 (select-window orgpan-org-window
)
384 (defvar orgpan-panel-window nil
385 "The window showing `orgpan-panel-buffer'.")
387 (defvar orgpan-panel-buffer nil
389 There can be only one such buffer at any time.")
391 (defvar orgpan-org-window nil
)
392 ;;(make-variable-buffer-local 'orgpan-org-window)
395 (defvar orgpan-org-buffer nil
)
396 ;;(make-variable-buffer-local 'orgpan-org-buffer)
398 (defvar orgpan-last-org-buffer nil
)
399 ;;(make-variable-buffer-local 'orgpan-last-org-buffer)
401 (defvar orgpan-point nil
)
402 ;;(make-variable-buffer-local 'orgpan-point)
404 (defun orgpan-avoid-viper-in-buffer ()
405 ;; Fix-me: This is ugly. However see `this-major-mode-requires-vi-state':
406 (set (make-local-variable 'viper-emacs-state-mode-list
) '(orgpan-mode))
407 (set (make-local-variable 'viper-new-major-mode-buffer-list
) nil
)
408 (local-set-key [?\
] 'ignore
))
410 (define-derived-mode orgpan-mode nil
"Org-Panel"
411 "Mode for org-simple.el control panel."
412 (setq buffer-read-only t
)
413 (unless orgpan-minor-mode-version
414 (add-hook 'pre-command-hook
'orgpan-mode-pre-command nil t
)
415 (add-hook 'post-command-hook
'orgpan-mode-post-command t
))
416 (orgpan-avoid-viper-in-buffer)
417 (setq cursor-type nil
))
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
423 (defun orgpan-insert-field (text keymap explain
)
428 (ovl (make-overlay beg end
)))
429 (overlay-put ovl
'face orgpan-field-face
)
430 (overlay-put ovl
'keymap keymap
)
431 (overlay-put ovl
'orgpan-explain explain
)))
433 (defconst orgpan-with-keymap
434 (let ((map (make-sparse-keymap)))
435 (set-keymap-parent map org-mode-map
)
436 ;; Users are used to tabbing between fields:
437 (define-key map
[(tab)] 'orgpan-forward-field
)
438 (define-key map
[(shift tab
)] 'orgpan-backward-field
)
439 ;; Now we must use something else for visibility (first does not work if Viper):
440 (define-key map
[(meta tab
)] 'org-cycle
)
441 (define-key map
[(control meta tab
)] 'org-global-cycle
)
444 (defconst orgpan-without-keymap
445 (let ((map (make-sparse-keymap)))
446 (set-keymap-parent map org-mode-map
)
447 ;; Visibility (those are in org-mode-map):
448 ;;(define-key map [tab] 'org-cycle)
449 ;;(define-key map [(shift tab)] 'org-global-cycle)
451 (define-key map
[left] 'outline-up-heading)
452 (define-key map [right] 'org-cycle)
453 (define-key map [up] 'outline-previous-visible-heading)
454 (define-key map [down] 'outline-next-visible-heading)
455 (define-key map [(shift down)] 'outline-forward-same-level)
456 (define-key map [(shift up)] 'outline-backward-same-level)
458 (define-key map [(control up)] 'org-move-subtree-up)
459 (define-key map [(control down)] 'org-move-subtree-down)
460 (define-key map [(control left)] 'org-do-promote)
461 (define-key map [(control right)] 'org-do-demote)
462 (define-key map [(control shift left)] 'org-promote-subtree)
463 (define-key map [(control shift right)] 'org-demote-subtree)
465 (define-key map [?+] 'org-priority-up)
466 (define-key map [?-] 'org-priority-down)
467 (define-key map [?t] 'org-todo)
470 (defun orgpan-make-panel-without-buttons (buf)
471 (with-current-buffer buf
472 (insert (propertize "Org Panel" 'face 'orgpan-active-field))
473 (insert " ? for help, q quit\n")
474 (insert (propertize "arrows" 'face 'font-lock-keyword-face)
476 (propertize "C-arrows" 'face 'font-lock-keyword-face)
478 (propertize "cxpz" 'face 'font-lock-keyword-face)
479 ": copy cut paste undo, "
480 (propertize "tT+-" 'face 'font-lock-keyword-face)
482 (propertize "s" 'face 'font-lock-keyword-face)
485 (set-keymap-parent orgpan-mode-map orgpan-without-keymap)
488 (defun orgpan-make-panel-with-buttons (buf)
489 (with-current-buffer buf
490 (let* ((base-map (make-sparse-keymap))
491 (space-line (propertize "\n\n" 'face 'orgpan-spaceline))
492 (arrow-face 'font-lock-keyword-face)
493 (L (propertize "left" 'face arrow-face))
494 (R (propertize "right" 'face arrow-face))
495 (U (propertize "up" 'face arrow-face))
496 (D (propertize "down" 'face arrow-face)))
497 ;;(message D)(sit-for 2)
498 (define-key base-map [left] 'ignore
)
499 (define-key base-map
[right] 'ignore)
500 (define-key base-map [up] 'ignore)
501 (define-key base-map [down] 'ignore)
502 (define-key base-map [?q] 'delete-window)
503 (define-key base-map [??] 'orgpan-help)
505 (let ((map (copy-keymap base-map)))
506 (define-key map [left] 'outline-up-heading)
507 (define-key map [right] 'org-cycle
)
508 (define-key map
[up] 'outline-previous-visible-heading)
509 (define-key map [down] 'outline-next-visible-heading)
510 (define-key map [(shift down)] 'outline-forward-same-level)
511 (define-key map [(shift up)] 'outline-backward-same-level)
512 (orgpan-insert-field "Navigate" map (concat U "/" D ", " L ": Go to, " R ": Visibility")))
514 (let ((map (copy-keymap base-map)))
515 (define-key map [up] 'org-move-subtree-up
)
516 (define-key map
[down] 'org-move-subtree-down)
517 (define-key map [left] 'org-do-promote)
518 (define-key map [right] 'org-do-demote)
519 (define-key map [(shift left)] 'org-promote-subtree)
520 (define-key map [(shift right)] 'org-demote-subtree)
524 (propertize "Move" 'face 'font-lock-warning-face)
526 (propertize "Level (w S: Subtree Level)" 'face 'font-lock-warning-face))))
528 (let ((map (copy-keymap base-map)))
529 (define-key map [up] 'org-priority-up)
530 (define-key map [down] 'org-priority-down
)
531 (define-key map
[right] 'org-todo)
532 (orgpan-insert-field "TODO/priority" map
533 (concat R ": TODO, " U "/" D ": Priority")))
535 (insert " ? for help, q quit\n")
536 (orgpan-display-bindings-help)
537 (setq orgpan-ovl-help (make-overlay (point) (point)))
540 (defun orgpan-make-panel-buffer ()
541 "Make the panel buffer."
542 (let* ((buf-name "*Org Panel*"))
543 (when orgpan-panel-buffer (kill-buffer orgpan-panel-buffer))
544 (setq orgpan-panel-buffer (get-buffer-create buf-name))
545 (if orgpan-panel-buttons
546 (orgpan-make-panel-with-buttons orgpan-panel-buffer)
547 (orgpan-make-panel-without-buttons orgpan-panel-buffer))
548 (with-current-buffer orgpan-panel-buffer
550 (goto-char (point-min)))
551 orgpan-panel-buffer))
553 (defun orgpan-help ()
555 (set-keymap-parent orgpan-with-keymap nil)
556 (set-keymap-parent orgpan-without-keymap nil)
557 (describe-function 'orgpan-panel)
558 (set-keymap-parent orgpan-with-keymap org-mode-map)
559 (set-keymap-parent orgpan-without-keymap org-mode-map)
560 (message "Use 'l' to remove help window")
563 (defun orgpan-panel ()
564 "Create a control panel for current `org-mode' buffer.
565 The control panel may be used to quickly move around and change
566 the headings. The idea is that when you want to to a lot of this
567 kind of editing you should be able to do that with few
568 keystrokes (and without having to remember the complicated
569 keystrokes). A typical situation when this perhaps can be useful
570 is when you are looking at your notes file \(usually ~/.notes,
571 see `remember-data-file') where you have saved quick notes with
574 The keys below are defined in the panel. Note that the commands
575 are carried out in the `org-mode' buffer that belongs to the
580 In addition to the keys above most of the keys in `org-mode' can
581 also be used from the panel.
583 Note: There are two forms of the control panel, one with buttons
584 and one without. The default is without, see
585 `orgpan-panel-buttons'. If buttons are used choosing a different
586 button changes the binding of the arrow keys."
588 (unless (derived-mode-p 'org-mode)
589 (error "Buffer is not in org-mode"))
590 (orgpan-delete-panel)
591 (unless orgpan-org-mode-commands
592 (map-keymap (lambda (ev def)
596 (setq orgpan-org-mode-commands
597 (cons def orgpan-org-mode-commands))))
599 ;;(org-back-to-heading)
600 ;;(remove-hook 'window-configuration-change-hook 'orgpan-window-config-change)
602 (set-window-buffer (selected-window) (orgpan-make-panel-buffer))
603 (setq orgpan-panel-window (selected-window))
604 ;;(set-window-dedicated-p (selected-window) t)
605 (fit-window-to-buffer nil nil 3)
606 (setq orgpan-org-window (next-window))
607 ;; The minor mode version starts here:
608 (when orgpan-minor-mode-version
609 (select-window orgpan-org-window)
610 (orgpan-panel-minor-mode 1)
611 (add-hook 'post-command-hook 'orgpan-minor-post-command t)))
613 (defun orgpan-minor-post-command ()
615 ;; Check org window and buffer
616 (windowp orgpan-org-window)
617 (window-live-p orgpan-org-window)
618 (eq orgpan-org-window (selected-window))
619 (derived-mode-p 'org-mode)
620 ;; Check panel window and buffer
621 (windowp orgpan-panel-window)
622 (window-live-p orgpan-panel-window)
623 (bufferp orgpan-panel-buffer)
624 (buffer-live-p orgpan-panel-buffer)
625 (eq (window-buffer orgpan-panel-window) orgpan-panel-buffer)
627 orgpan-panel-minor-mode)
628 (orgpan-delete-panel)))
630 (define-minor-mode orgpan-panel-minor-mode
631 "Minor mode used in `org-mode' buffer when showing panel."
632 :keymap orgpan-mode-map
639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640 ;;; org-panel.el ends here