Version number set to 6.01a, release 6.01a.
[org-mode.git] / lisp / org-mouse.el
blob40325a11b3cda57ddc5df08e0e65a0efcb0a8731
1 ;;; org-mouse.el --- Better mouse support for org-mode
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation
4 ;;
5 ;; Author: Piotr Zielinski <piotr dot zielinski at gmail dot com>
6 ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
7 ;; Version: 6.01a
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; Org-mouse provides mouse support for org-mode.
31 ;; http://orgmode.org
33 ;; Org-mouse implements the following features:
34 ;; * following links with the left mouse button (in Emacs 22)
35 ;; * subtree expansion/collapse (org-cycle) with the left mouse button
36 ;; * several context menus on the right mouse button:
37 ;; + general text
38 ;; + headlines
39 ;; + timestamps
40 ;; + priorities
41 ;; + links
42 ;; + tags
43 ;; * promoting/demoting/moving subtrees with mouse-3
44 ;; + if the drag starts and ends in the same line then promote/demote
45 ;; + otherwise move the subtree
47 ;; Use
48 ;; ---
50 ;; To use this package, put the following line in your .emacs:
52 ;; (require 'org-mouse)
55 ;; Fixme:
56 ;; + deal with folding / unfolding issues
58 ;; TODO (This list is only theoretical, if you'd like to have some
59 ;; feature implemented or a bug fix please send me an email, even if
60 ;; something similar appears in the list below. This will help me get
61 ;; the priorities right.):
63 ;; + org-store-link, insert link
64 ;; + org tables
65 ;; + occur with the current word/tag (same menu item)
66 ;; + ctrl-c ctrl-c, for example, renumber the current list
67 ;; + internal links
69 ;; Please email the maintainer with new feature suggestions / bugs
71 ;; History:
73 ;; SInce version 5.10: Changes are listed in the general org-mode docs.
75 ;; Version 5.09
76 ;; + Version number synchronization with Org-mode.
78 ;; Version 0.25
79 ;; + made compatible with org-mode 4.70 (thanks to Carsten for the patch)
81 ;; Version 0.24
82 ;; + minor changes to the table menu
84 ;; Version 0.23
85 ;; + preliminary support for tables and calculation marks
86 ;; + context menu support for org-agenda-undo & org-sort-entries
88 ;; Version 0.22
89 ;; + handles undo support for the agenda buffer (requires org-mode >=4.58)
91 ;; Version 0.21
92 ;; + selected text activates its context menu
93 ;; + shift-middleclick or right-drag inserts the text from the clipboard in the form of a link
95 ;; Version 0.20
96 ;; + the new "TODO Status" submenu replaces the "Cycle TODO" menu item
97 ;; + the TODO menu can now list occurrences of a specific TODO keyword
98 ;; + #+STARTUP line is now recognized
100 ;; Version 0.19
101 ;; + added support for dragging URLs to the org-buffer
103 ;; Version 0.18
104 ;; + added support for agenda blocks
106 ;; Version 0.17
107 ;; + toggle checkboxes with a single click
109 ;; Version 0.16
110 ;; + added support for checkboxes
112 ;; Version 0.15
113 ;; + org-mode now works with the Agenda buffer as well
115 ;; Version 0.14
116 ;; + added a menu option that converts plain list items to outline items
118 ;; Version 0.13
119 ;; + "Insert Heading" now inserts a sibling heading if the point is
120 ;; on "***" and a child heading otherwise
122 ;; Version 0.12
123 ;; + compatible with Emacs 21
124 ;; + custom agenda commands added to the main menu
125 ;; + moving trees should now work between windows in the same frame
127 ;; Version 0.11
128 ;; + fixed org-mouse-at-link (thanks to Carsten)
129 ;; + removed [follow-link] bindings
131 ;; Version 0.10
132 ;; + added a menu option to remove highlights
133 ;; + compatible with org-mode 4.21 now
135 ;; Version 0.08:
136 ;; + trees can be moved/promoted/demoted by dragging with the right
137 ;; mouse button (mouse-3)
138 ;; + small changes in the above function
140 ;; Versions 0.01 -- 0.07: (I don't remember)
142 (eval-when-compile (require 'cl))
143 (require 'org)
145 (defvar org-agenda-allow-remote-undo)
146 (defvar org-agenda-undo-list)
147 (defvar org-agenda-custom-commands)
148 (declare-function org-agenda-change-all-lines "org-agenda"
149 (newhead hdmarker &optional fixface))
150 (declare-function org-verify-change-for-undo "org-agenda" (l1 l2))
152 (defvar org-mouse-plain-list-regexp "\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
153 "Regular expression that matches a plain list.")
154 (defvar org-mouse-direct t
155 "Internal variable indicating whether the current action is direct.
157 If t, then the current action has been invoked directly through the buffer
158 it is intended to operate on. If nil, then the action has been invoked
159 indirectly, for example, through the agenda buffer.")
161 (defgroup org-mouse nil
162 "Mouse support for org-mode."
163 :tag "Org Mouse"
164 :group 'org)
166 (defcustom org-mouse-punctuation ":"
167 "Punctuation used when inserting text by drag and drop."
168 :group 'org-mouse
169 :type 'string)
172 (defun org-mouse-re-search-line (regexp)
173 "Search the current line for a given regular expression."
174 (beginning-of-line)
175 (re-search-forward regexp (point-at-eol) t))
177 (defun org-mouse-end-headline ()
178 "Go to the end of current headline (ignoring tags)."
179 (interactive)
180 (end-of-line)
181 (skip-chars-backward "\t ")
182 (when (looking-back ":[A-Za-z]+:")
183 (skip-chars-backward ":A-Za-z")
184 (skip-chars-backward "\t ")))
186 (defvar org-mouse-context-menu-function nil
187 "Function to create the context menu.
188 The value of this variable is the function invoked by
189 `org-mouse-context-menu' as the context menu.")
190 (make-variable-buffer-local 'org-mouse-context-menu-function)
192 (defun org-mouse-show-context-menu (event prefix)
193 "Invoke the context menu.
195 If the value of `org-mouse-context-menu-function' is a function, then
196 this function is called. Otherwise, the current major mode menu is used."
197 (interactive "@e \nP")
198 (if (and (= (event-click-count event) 1)
199 (or (not mark-active)
200 (sit-for (/ double-click-time 1000.0))))
201 (progn
202 (select-window (posn-window (event-start event)))
203 (when (not (org-mouse-mark-active))
204 (goto-char (posn-point (event-start event)))
205 (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook)))
206 (let ((redisplay-dont-pause t))
207 (sit-for 0)))
208 (if (functionp org-mouse-context-menu-function)
209 (funcall org-mouse-context-menu-function event)
210 (mouse-major-mode-menu event prefix)))
211 (setq this-command 'mouse-save-then-kill)
212 (mouse-save-then-kill event)))
215 (defun org-mouse-line-position ()
216 "Returns `:beginning' or `:middle' or `:end', depending on the point position.
218 If the point is at the end of the line, return `:end'.
219 If the point is separated from the beginning of the line only by white
220 space and *'s (`org-mouse-bolp'), return `:beginning'. Otherwise,
221 return `:middle'."
222 (cond
223 ((eolp) :end)
224 ((org-mouse-bolp) :beginning)
225 (t :middle)))
227 (defun org-mouse-empty-line ()
228 "Return non-nil iff the line contains only white space."
229 (save-excursion (beginning-of-line) (looking-at "[ \t]*$")))
231 (defun org-mouse-next-heading ()
232 "Go to the next heading.
233 If there is none, ensure that the point is at the beginning of an empty line."
234 (unless (outline-next-heading)
235 (beginning-of-line)
236 (unless (org-mouse-empty-line)
237 (end-of-line)
238 (newline))))
240 (defun org-mouse-insert-heading ()
241 "Insert a new heading, as `org-insert-heading'.
243 If the point is at the :beginning (`org-mouse-line-position') of the line,
244 insert the new heading before the current line. Otherwise, insert it
245 after the current heading."
246 (interactive)
247 (case (org-mouse-line-position)
248 (:beginning (beginning-of-line)
249 (org-insert-heading))
250 (t (org-mouse-next-heading)
251 (org-insert-heading))))
253 (defun org-mouse-timestamp-today (&optional shift units)
254 "Change the timestamp into SHIFT UNITS in the future.
256 For the acceptable UNITS, see `org-timestamp-change'."
257 (interactive)
258 (flet ((org-read-date (&rest rest) (current-time)))
259 (org-time-stamp nil))
260 (when shift
261 (org-timestamp-change shift units)))
263 (defun org-mouse-keyword-menu (keywords function &optional selected itemformat)
264 "A helper function.
266 Returns a menu fragment consisting of KEYWORDS. When a keyword
267 is selected by the user, FUNCTION is called with the selected
268 keyword as the only argument.
270 If SELECTED is nil, then all items are normal menu items. If
271 SELECTED is a function, then each item is a checkbox, which is
272 enabled for a given keyword iff (funcall SELECTED keyword) return
273 non-nil. If SELECTED is neither nil nor a function, then the
274 items are radio buttons. A radio button is enabled for the
275 keyword `equal' to SELECTED.
277 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
278 is a function, it is invoked with the keyword as the only
279 argument. If it is a string, it is interpreted as the format
280 string to (format ITEMFORMAT keyword). If it is neither a string
281 nor a function, elements of KEYWORDS are used directly. "
282 (mapcar
283 `(lambda (keyword)
284 (vector (cond
285 ((functionp ,itemformat) (funcall ,itemformat keyword))
286 ((stringp ,itemformat) (format ,itemformat keyword))
287 (t keyword))
288 (list 'funcall ,function keyword)
289 :style (cond
290 ((null ,selected) t)
291 ((functionp ,selected) 'toggle)
292 (t 'radio))
293 :selected (if (functionp ,selected)
294 (and (funcall ,selected keyword) t)
295 (equal ,selected keyword))))
296 keywords))
298 (defun org-mouse-remove-match-and-spaces ()
299 "Remove the match, make just one space around the point."
300 (interactive)
301 (replace-match "")
302 (just-one-space))
304 (defvar rest)
305 (defun org-mouse-replace-match-and-surround (newtext &optional fixedcase
306 literal string subexp)
307 "The same as `replace-match', but surrounds the replacement with spaces."
308 (apply 'replace-match rest)
309 (save-excursion
310 (goto-char (match-beginning (or subexp 0)))
311 (just-one-space)
312 (goto-char (match-end (or subexp 0)))
313 (just-one-space)))
316 (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat
317 nosurround)
318 "A helper function.
320 Returns a menu fragment consisting of KEYWORDS. When a keyword
321 is selected, group GROUP of the current match is replaced by the
322 keyword. The method ensures that both ends of the replacement
323 are separated from the rest of the text in the buffer by
324 individual spaces (unless NOSURROND is non-nil).
326 The final entry of the menu is always \"None\", which removes the
327 match.
329 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
330 is a function, it is invoked with the keyword as the only
331 argument. If it is a string, it is interpreted as the format
332 string to (format ITEMFORMAT keyword). If it is neither a string
333 nor a function, elements of KEYWORDS are used directly.
335 (setq group (or group 0))
336 (let ((replace (org-mouse-match-closure
337 (if nosurround 'replace-match
338 'org-mouse-replace-match-and-surround))))
339 (append
340 (org-mouse-keyword-menu
341 keywords
342 `(lambda (keyword) (funcall ,replace keyword t t nil ,group))
343 (match-string group)
344 itemformat)
345 `(["None" org-mouse-remove-match-and-spaces
346 :style radio
347 :selected ,(not (member (match-string group) keywords))]))))
349 (defun org-mouse-show-headlines ()
350 "Change the visibility of the current org buffer to only show headlines."
351 (interactive)
352 (let ((this-command 'org-cycle)
353 (last-command 'org-cycle)
354 (org-cycle-global-status nil))
355 (org-cycle '(4))
356 (org-cycle '(4))))
358 (defun org-mouse-show-overview ()
359 "Change visibility of current org buffer to first-level headlines only."
360 (interactive)
361 (let ((org-cycle-global-status nil))
362 (org-cycle '(4))))
364 (defun org-mouse-set-priority (priority)
365 "Set the priority of the current headline to PRIORITY."
366 (flet ((read-char-exclusive () priority))
367 (org-priority)))
369 (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]"
370 "Regular expression matching the priority indicator.
371 Differs from `org-priority-regexp' in that it doesn't contain the
372 leading '.*?'.")
374 (defun org-mouse-get-priority (&optional default)
375 "Return the priority of the current headline.
376 DEFAULT is returned if no priority is given in the headline."
377 (save-excursion
378 (if (org-mouse-re-search-line org-mouse-priority-regexp)
379 (match-string 1)
380 (when default (char-to-string org-default-priority)))))
382 ;; (defun org-mouse-at-link ()
383 ;; (and (eq (get-text-property (point) 'face) 'org-link)
384 ;; (save-excursion
385 ;; (goto-char (previous-single-property-change (point) 'face))
386 ;; (or (looking-at org-bracket-link-regexp)
387 ;; (looking-at org-angle-link-re)
388 ;; (looking-at org-plain-link-re)))))
391 (defun org-mouse-delete-timestamp ()
392 "Deletes the current timestamp as well as the preceding keyword.
393 SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:"
394 (when (or (org-at-date-range-p) (org-at-timestamp-p))
395 (replace-match "") ; delete the timestamp
396 (skip-chars-backward " :A-Z")
397 (when (looking-at " *[A-Z][A-Z]+:")
398 (replace-match ""))))
400 (defun org-mouse-looking-at (regexp skipchars &optional movechars)
401 (save-excursion
402 (let ((point (point)))
403 (if (looking-at regexp) t
404 (skip-chars-backward skipchars)
405 (forward-char (or movechars 0))
406 (when (looking-at regexp)
407 (> (match-end 0) point))))))
409 (defun org-mouse-priority-list ()
410 (loop for priority from ?A to org-lowest-priority
411 collect (char-to-string priority)))
413 (defun org-mouse-tag-menu () ;todo
414 (append
415 (let ((tags (org-split-string (org-get-tags) ":")))
416 (org-mouse-keyword-menu
417 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
418 `(lambda (tag)
419 (org-mouse-set-tags
420 (sort (if (member tag (quote ,tags))
421 (delete tag (quote ,tags))
422 (cons tag (quote ,tags)))
423 'string-lessp)))
424 `(lambda (tag) (member tag (quote ,tags)))
426 '("--"
427 ["Align Tags Here" (org-set-tags nil t) t]
428 ["Align Tags in Buffer" (org-set-tags t t) t]
429 ["Set Tags ..." (org-set-tags) t])))
433 (defun org-mouse-set-tags (tags)
434 (save-excursion
435 ;; remove existing tags first
436 (beginning-of-line)
437 (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)")
438 (replace-match ""))
440 ;; set new tags if any
441 (when tags
442 (end-of-line)
443 (insert " :" (mapconcat 'identity tags ":") ":")
444 (org-set-tags nil t))))
446 (defun org-mouse-insert-checkbox ()
447 (interactive)
448 (and (org-at-item-p)
449 (goto-char (match-end 0))
450 (unless (org-at-item-checkbox-p)
451 (delete-horizontal-space)
452 (insert " [ ] "))))
454 (defun org-mouse-agenda-type (type)
455 (case type
456 ('tags "Tags: ")
457 ('todo "TODO: ")
458 ('tags-tree "Tags tree: ")
459 ('todo-tree "TODO tree: ")
460 ('occur-tree "Occur tree: ")
461 (t "Agenda command ???")))
464 (defun org-mouse-list-options-menu (alloptions &optional function)
465 (let ((options (save-match-data
466 (split-string (match-string-no-properties 1)))))
467 (print options)
468 (loop for name in alloptions
469 collect
470 (vector name
471 `(progn
472 (replace-match
473 (mapconcat 'identity
474 (sort (if (member ',name ',options)
475 (delete ',name ',options)
476 (cons ',name ',options))
477 'string-lessp)
478 " ")
479 nil nil nil 1)
480 (when (functionp ',function) (funcall ',function)))
481 :style 'toggle
482 :selected (and (member name options) t)))))
484 (defun org-mouse-clip-text (text maxlength)
485 (if (> (length text) maxlength)
486 (concat (substring text 0 (- maxlength 3)) "...")
487 text))
489 (defun org-mouse-popup-global-menu ()
490 (popup-menu
491 `("Main Menu"
492 ["Show Overview" org-mouse-show-overview t]
493 ["Show Headlines" org-mouse-show-headlines t]
494 ["Show All" show-all t]
495 ["Remove Highlights" org-remove-occur-highlights
496 :visible org-occur-highlights]
497 "--"
498 ["Check Deadlines"
499 (if (functionp 'org-check-deadlines-and-todos)
500 (org-check-deadlines-and-todos org-deadline-warning-days)
501 (org-check-deadlines org-deadline-warning-days)) t]
502 ["Check TODOs" org-show-todo-tree t]
503 ("Check Tags"
504 ,@(org-mouse-keyword-menu
505 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
506 '(lambda (tag) (org-tags-sparse-tree nil tag)))
507 "--"
508 ["Custom Tag ..." org-tags-sparse-tree t])
509 ["Check Phrase ..." org-occur]
510 "--"
511 ["Display Agenda" org-agenda-list t]
512 ["Display Timeline" org-timeline t]
513 ["Display TODO List" org-todo-list t]
514 ("Display Tags"
515 ,@(org-mouse-keyword-menu
516 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
517 '(lambda (tag) (org-tags-view nil tag)))
518 "--"
519 ["Custom Tag ..." org-tags-view t])
520 ["Display Calendar" org-goto-calendar t]
521 "--"
522 ,@(org-mouse-keyword-menu
523 (mapcar 'car org-agenda-custom-commands)
524 '(lambda (key)
525 (eval `(flet ((read-char-exclusive () (string-to-char ,key)))
526 (org-agenda nil))))
527 nil
528 '(lambda (key)
529 (let ((entry (assoc key org-agenda-custom-commands)))
530 (org-mouse-clip-text
531 (cond
532 ((stringp (nth 1 entry)) (nth 1 entry))
533 ((stringp (nth 2 entry))
534 (concat (org-mouse-agenda-type (nth 1 entry))
535 (nth 2 entry)))
536 (t "Agenda Command '%s'"))
537 30))))
538 "--"
539 ["Delete Blank Lines" delete-blank-lines
540 :visible (org-mouse-empty-line)]
541 ["Insert Checkbox" org-mouse-insert-checkbox
542 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
543 ["Insert Checkboxes"
544 (org-mouse-for-each-item 'org-mouse-insert-checkbox)
545 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
546 ["Plain List to Outline" org-mouse-transform-to-outline
547 :visible (org-at-item-p)])))
550 (defun org-mouse-get-context (contextlist context)
551 (let ((contextdata (assq context contextlist)))
552 (when contextdata
553 (save-excursion
554 (goto-char (second contextdata))
555 (re-search-forward ".*" (third contextdata))))))
557 (defun org-mouse-for-each-item (function)
558 (save-excursion
559 (ignore-errors
560 (while t (org-previous-item)))
561 (ignore-errors
562 (while t
563 (funcall function)
564 (org-next-item)))))
566 (defun org-mouse-bolp ()
567 "Returns true if there only spaces, tabs, and '*', between the beginning of line and the point"
568 (save-excursion
569 (skip-chars-backward " \t*") (bolp)))
571 (defun org-mouse-insert-item (text)
572 (case (org-mouse-line-position)
573 (:beginning ; insert before
574 (beginning-of-line)
575 (looking-at "[ \t]*")
576 (open-line 1)
577 (indent-to (- (match-end 0) (match-beginning 0)))
578 (insert "+ "))
580 (:middle ; insert after
581 (end-of-line)
582 (newline t)
583 (indent-relative)
584 (insert "+ "))
586 (:end ; insert text here
587 (skip-chars-backward " \t")
588 (kill-region (point) (point-at-eol))
589 (unless (looking-back org-mouse-punctuation)
590 (insert (concat org-mouse-punctuation " ")))))
592 (insert text)
593 (beginning-of-line))
595 (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate)
596 (if (eq major-mode 'org-mode)
597 (org-mouse-insert-item text)
598 ad-do-it))
600 (defadvice dnd-open-file (around org-mouse-dnd-open-file activate)
601 (if (eq major-mode 'org-mode)
602 (org-mouse-insert-item uri)
603 ad-do-it))
605 (defun org-mouse-match-closure (function)
606 (let ((match (match-data t)))
607 `(lambda (&rest rest)
608 (save-match-data
609 (set-match-data ',match)
610 (apply ',function rest)))))
612 (defun org-mouse-todo-keywords ()
613 (if (boundp 'org-todo-keywords-1) org-todo-keywords-1 org-todo-keywords))
615 (defun org-mouse-match-todo-keyword ()
616 (save-excursion
617 (org-back-to-heading)
618 (if (looking-at outline-regexp) (goto-char (match-end 0)))
619 (or (looking-at (concat " +" org-todo-regexp " *"))
620 (looking-at " \\( *\\)"))))
622 (defun org-mouse-yank-link (click)
623 (interactive "e")
624 ;; Give temporary modes such as isearch a chance to turn off.
625 (run-hooks 'mouse-leave-buffer-hook)
626 (mouse-set-point click)
627 (setq mouse-selection-click-count 0)
628 (delete-horizontal-space)
629 (insert-for-yank (concat " [[" (current-kill 0) "]] ")))
631 (defun org-mouse-context-menu (&optional event)
632 (let ((stamp-prefixes (list org-deadline-string org-scheduled-string))
633 (contextlist (org-context)))
634 (flet ((get-context (context) (org-mouse-get-context contextlist context)))
635 (cond
636 ((org-mouse-mark-active)
637 (let ((region-string (buffer-substring (region-beginning) (region-end))))
638 (popup-menu
639 `(nil
640 ["Sparse Tree" (org-occur ',region-string)]
641 ["Find in Buffer" (occur ',region-string)]
642 ["Grep in Current Dir"
643 (grep (format "grep -rnH -e '%s' *" ',region-string))]
644 ["Grep in Parent Dir"
645 (grep (format "grep -rnH -e '%s' ../*" ',region-string))]
646 "--"
647 ["Convert to Link"
648 (progn (save-excursion (goto-char (region-beginning)) (insert "[["))
649 (save-excursion (goto-char (region-end)) (insert "]]")))]
650 ["Insert Link Here" (org-mouse-yank-link ',event)]))))
652 ((save-excursion (beginning-of-line) (looking-at "#\\+STARTUP: \\(.*\\)"))
653 (popup-menu
654 `(nil
655 ,@(org-mouse-list-options-menu (mapcar 'car org-startup-options)
656 'org-mode-restart))))
657 ((or (eolp)
658 (and (looking-at "\\( \\|\t\\)\\(+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$")
659 (looking-back " \\|\t")))
660 (org-mouse-popup-global-menu))
661 ((get-context :checkbox)
662 (popup-menu
663 '(nil
664 ["Toggle" org-toggle-checkbox t]
665 ["Remove" org-mouse-remove-match-and-spaces t]
667 ["All Clear" (org-mouse-for-each-item
668 (lambda ()
669 (when (save-excursion (org-at-item-checkbox-p))
670 (replace-match "[ ]"))))]
671 ["All Set" (org-mouse-for-each-item
672 (lambda ()
673 (when (save-excursion (org-at-item-checkbox-p))
674 (replace-match "[X]"))))]
675 ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t]
676 ["All Remove" (org-mouse-for-each-item
677 (lambda ()
678 (when (save-excursion (org-at-item-checkbox-p))
679 (org-mouse-remove-match-and-spaces))))]
681 ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_")
682 (member (match-string 0) (org-mouse-todo-keywords)))
683 (popup-menu
684 `(nil
685 ,@(org-mouse-keyword-replace-menu (org-mouse-todo-keywords))
686 "--"
687 ["Check TODOs" org-show-todo-tree t]
688 ["List all TODO keywords" org-todo-list t]
689 [,(format "List only %s" (match-string 0))
690 (org-todo-list (match-string 0)) t]
692 ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z")
693 (member (match-string 0) stamp-prefixes))
694 (popup-menu
695 `(nil
696 ,@(org-mouse-keyword-replace-menu stamp-prefixes)
697 "--"
698 ["Check Deadlines" org-check-deadlines t]
700 ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority
701 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
702 (org-mouse-priority-list) 1 "Priority %s" t))))
703 ((get-context :link)
704 (popup-menu
705 '(nil
706 ["Open" org-open-at-point t]
707 ["Open in Emacs" (org-open-at-point t) t]
708 "--"
709 ["Copy link" (kill-new (match-string 0))]
710 ["Cut link"
711 (progn
712 (kill-region (match-beginning 0) (match-end 0))
713 (just-one-space))]
714 "--"
715 ["Grep for TODOs"
716 (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))]
717 ; ["Paste file link" ((insert "file:") (yank))]
719 ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags
720 (popup-menu
721 `(nil
722 [,(format "Display '%s'" (match-string 1))
723 (org-tags-view nil ,(match-string 1))]
724 [,(format "Sparse Tree '%s'" (match-string 1))
725 (org-tags-sparse-tree nil ,(match-string 1))]
726 "--"
727 ,@(org-mouse-tag-menu))))
728 ((org-at-timestamp-p)
729 (popup-menu
730 '(nil
731 ["Show Day" org-open-at-point t]
732 ["Change Timestamp" org-time-stamp t]
733 ["Delete Timestamp" (org-mouse-delete-timestamp) t]
734 ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)]
735 "--"
736 ["Set for Today" org-mouse-timestamp-today]
737 ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)]
738 ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)]
739 ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)]
740 ["Set in a Month" (org-mouse-timestamp-today 1 'month)]
741 "--"
742 ["+ 1 Day" (org-timestamp-change 1 'day)]
743 ["+ 1 Week" (org-timestamp-change 7 'day)]
744 ["+ 1 Month" (org-timestamp-change 1 'month)]
745 "--"
746 ["- 1 Day" (org-timestamp-change -1 'day)]
747 ["- 1 Week" (org-timestamp-change -7 'day)]
748 ["- 1 Month" (org-timestamp-change -1 'month)])))
749 ((get-context :table-special)
750 (let ((mdata (match-data)))
751 (incf (car mdata) 2)
752 (store-match-data mdata))
753 (message "match: %S" (match-string 0))
754 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
755 '(" " "!" "^" "_" "$" "#" "*" "'") 0
756 (lambda (mark)
757 (case (string-to-char mark)
758 (? "( ) Nothing Special")
759 (?! "(!) Column Names")
760 (?^ "(^) Field Names Above")
761 (?_ "(^) Field Names Below")
762 (?$ "($) Formula Parameters")
763 (?# "(#) Recalculation: Auto")
764 (?* "(*) Recalculation: Manual")
765 (?' "(') Recalculation: None"))) t))))
766 ((assq :table contextlist)
767 (popup-menu
768 '(nil
769 ["Align Table" org-ctrl-c-ctrl-c]
770 ["Blank Field" org-table-blank-field]
771 ["Edit Field" org-table-edit-field]
772 "--"
773 ("Column"
774 ["Move Column Left" org-metaleft]
775 ["Move Column Right" org-metaright]
776 ["Delete Column" org-shiftmetaleft]
777 ["Insert Column" org-shiftmetaright]
778 "--"
779 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle])
780 ("Row"
781 ["Move Row Up" org-metaup]
782 ["Move Row Down" org-metadown]
783 ["Delete Row" org-shiftmetaup]
784 ["Insert Row" org-shiftmetadown]
785 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
786 "--"
787 ["Insert Hline" org-table-insert-hline])
788 ("Rectangle"
789 ["Copy Rectangle" org-copy-special]
790 ["Cut Rectangle" org-cut-special]
791 ["Paste Rectangle" org-paste-special]
792 ["Fill Rectangle" org-table-wrap-region])
793 "--"
794 ["Set Column Formula" org-table-eval-formula]
795 ["Set Field Formula" (org-table-eval-formula '(4))]
796 ["Edit Formulas" org-table-edit-formulas]
797 "--"
798 ["Recalculate Line" org-table-recalculate]
799 ["Recalculate All" (org-table-recalculate '(4))]
800 ["Iterate All" (org-table-recalculate '(16))]
801 "--"
802 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks]
803 ["Sum Column/Rectangle" org-table-sum
804 :active (or (org-at-table-p) (org-region-active-p))]
805 ["Field Info" org-table-field-info]
806 ["Debug Formulas"
807 (setq org-table-formula-debug (not org-table-formula-debug))
808 :style toggle :selected org-table-formula-debug]
810 ((and (assq :headline contextlist) (not (eolp)))
811 (let ((priority (org-mouse-get-priority t)))
812 (popup-menu
813 `("Headline Menu"
814 ("Tags and Priorities"
815 ,@(org-mouse-keyword-menu
816 (org-mouse-priority-list)
817 '(lambda (keyword)
818 (org-mouse-set-priority (string-to-char keyword)))
819 priority "Priority %s")
820 "--"
821 ,@(org-mouse-tag-menu))
822 ("TODO Status"
823 ,@(progn (org-mouse-match-todo-keyword)
824 (org-mouse-keyword-replace-menu (org-mouse-todo-keywords)
825 1)))
826 ["Show Tags"
827 (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags))
828 :visible (not org-mouse-direct)]
829 ["Show Priority"
830 (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority))
831 :visible (not org-mouse-direct)]
832 ,@(if org-mouse-direct '("--") nil)
833 ["New Heading" org-mouse-insert-heading :visible org-mouse-direct]
834 ["Set Deadline"
835 (progn (org-mouse-end-headline) (insert " ") (org-deadline))
836 :active (not (save-excursion
837 (org-mouse-re-search-line org-deadline-regexp)))]
838 ["Schedule Task"
839 (progn (org-mouse-end-headline) (insert " ") (org-schedule))
840 :active (not (save-excursion
841 (org-mouse-re-search-line org-scheduled-regexp)))]
842 ["Insert Timestamp"
843 (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t]
844 ; ["Timestamp (inactive)" org-time-stamp-inactive t]
845 "--"
846 ["Archive Subtree" org-archive-subtree]
847 ["Cut Subtree" org-cut-special]
848 ["Copy Subtree" org-copy-special]
849 ["Paste Subtree" org-paste-special :visible org-mouse-direct]
850 ("Sort Children"
851 ["Alphabetically" (org-sort-entries nil ?a)]
852 ["Numerically" (org-sort-entries nil ?n)]
853 ["By Time/Date" (org-sort-entries nil ?t)]
854 "--"
855 ["Reverse Alphabetically" (org-sort-entries nil ?A)]
856 ["Reverse Numerically" (org-sort-entries nil ?N)]
857 ["Reverse By Time/Date" (org-sort-entries nil ?T)])
858 "--"
859 ["Move Trees" org-mouse-move-tree :active nil]
860 ))))
862 (org-mouse-popup-global-menu))))))
864 ;; (defun org-mouse-at-regexp (regexp)
865 ;; (save-excursion
866 ;; (let ((point (point))
867 ;; (bol (progn (beginning-of-line) (point)))
868 ;; (eol (progn (end-of-line) (point))))
869 ;; (goto-char point)
870 ;; (re-search-backward regexp bol 1)
871 ;; (and (not (eolp))
872 ;; (progn (forward-char)
873 ;; (re-search-forward regexp eol t))
874 ;; (<= (match-beginning 0) point)))))
876 (defun org-mouse-mark-active ()
877 (and mark-active transient-mark-mode))
879 (defun org-mouse-in-region-p (pos)
880 (and (org-mouse-mark-active)
881 (>= pos (region-beginning))
882 (< pos (region-end))))
884 (defun org-mouse-down-mouse (event)
885 (interactive "e")
886 (setq this-command last-command)
887 (unless (and (= 1 (event-click-count event))
888 (org-mouse-in-region-p (posn-point (event-start event))))
889 (mouse-drag-region event)))
891 (add-hook 'org-mode-hook
892 '(lambda ()
893 (setq org-mouse-context-menu-function 'org-mouse-context-menu)
895 ; (define-key org-mouse-map [follow-link] 'mouse-face)
896 (define-key org-mouse-map (if (featurep 'xemacs) [button3] [mouse-3]) nil)
897 (define-key org-mode-map [mouse-3] 'org-mouse-show-context-menu)
898 (define-key org-mode-map [down-mouse-1] 'org-mouse-down-mouse)
899 (define-key org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree)
900 (define-key org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start)
901 (define-key org-mode-map [S-mouse-2] 'org-mouse-yank-link)
902 (define-key org-mode-map [drag-mouse-3] 'org-mouse-yank-link)
903 (define-key org-mouse-map [drag-mouse-3] 'org-mouse-move-tree)
904 (define-key org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start)
906 (font-lock-add-keywords nil
907 `((,outline-regexp
908 0 `(face org-link mouse-face highlight keymap ,org-mouse-map)
909 'prepend)
910 ("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +"
911 (1 `(face org-link keymap ,org-mouse-map mouse-face highlight) 'prepend))
912 ("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
913 (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t)))
916 (defadvice org-open-at-point (around org-mouse-open-at-point activate)
917 (let ((context (org-context)))
918 (cond
919 ((assq :headline-stars context) (org-cycle))
920 ((assq :checkbox context) (org-toggle-checkbox))
921 ((assq :item-bullet context)
922 (let ((org-cycle-include-plain-lists t)) (org-cycle)))
923 (t ad-do-it))))))
925 (defun org-mouse-move-tree-start (event)
926 (interactive "e")
927 (message "Same line: promote/demote, (***):move before, (text): make a child"))
930 (defun org-mouse-make-marker (position)
931 (with-current-buffer (window-buffer (posn-window position))
932 (copy-marker (posn-point position))))
934 (defun org-mouse-move-tree (event)
935 ;; todo: handle movements between different buffers
936 (interactive "e")
937 (save-excursion
938 (let* ((start (org-mouse-make-marker (event-start event)))
939 (end (org-mouse-make-marker (event-end event)))
940 (sbuf (marker-buffer start))
941 (ebuf (marker-buffer end)))
943 (when (and sbuf ebuf)
944 (set-buffer sbuf)
945 (goto-char start)
946 (org-back-to-heading)
947 (if (and (eq sbuf ebuf)
948 (equal
949 (point)
950 (save-excursion (goto-char end) (org-back-to-heading) (point))))
951 ;; if the same line then promote/demote
952 (if (>= end start) (org-demote-subtree) (org-promote-subtree))
953 ;; if different lines then move
954 (org-cut-subtree)
956 (set-buffer ebuf)
957 (goto-char end)
958 (org-back-to-heading)
959 (when (and (eq sbuf ebuf)
960 (equal
961 (point)
962 (save-excursion (goto-char start)
963 (org-back-to-heading) (point))))
964 (outline-end-of-subtree)
965 (end-of-line)
966 (if (eobp) (newline) (forward-char)))
968 (when (looking-at outline-regexp)
969 (let ((level (- (match-end 0) (match-beginning 0))))
970 (when (> end (match-end 0))
971 (outline-end-of-subtree)
972 (end-of-line)
973 (if (eobp) (newline) (forward-char))
974 (setq level (1+ level)))
975 (org-paste-subtree level)
976 (save-excursion
977 (outline-end-of-subtree)
978 (when (bolp) (delete-char -1))))))))))
981 (defun org-mouse-transform-to-outline ()
982 (interactive)
983 (org-back-to-heading)
984 (let ((minlevel 1000)
985 (replace-text (concat (match-string 0) "* ")))
986 (beginning-of-line 2)
987 (save-excursion
988 (while (not (or (eobp) (looking-at outline-regexp)))
989 (when (looking-at org-mouse-plain-list-regexp)
990 (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1)))))
991 (forward-line)))
992 (while (not (or (eobp) (looking-at outline-regexp)))
993 (when (and (looking-at org-mouse-plain-list-regexp)
994 (eq minlevel (- (match-end 1) (match-beginning 1))))
995 (replace-match replace-text))
996 (forward-line))))
998 (defvar _cmd) ;dynamically scoped from `org-with-remote-undo'.
1000 (defun org-mouse-do-remotely (command)
1001 ; (org-agenda-check-no-diary)
1002 (when (get-text-property (point) 'org-marker)
1003 (let* ((anticol (- (point-at-eol) (point)))
1004 (marker (get-text-property (point) 'org-marker))
1005 (buffer (marker-buffer marker))
1006 (pos (marker-position marker))
1007 (hdmarker (get-text-property (point) 'org-hd-marker))
1008 (buffer-read-only nil)
1009 (newhead "--- removed ---")
1010 (org-mouse-direct nil)
1011 (org-mouse-main-buffer (current-buffer)))
1012 (when (eq (with-current-buffer buffer major-mode) 'org-mode)
1013 (let ((endmarker (save-excursion
1014 (set-buffer buffer)
1015 (outline-end-of-subtree)
1016 (forward-char 1)
1017 (copy-marker (point)))))
1018 (org-with-remote-undo buffer
1019 (with-current-buffer buffer
1020 (widen)
1021 (goto-char pos)
1022 (org-show-hidden-entry)
1023 (save-excursion
1024 (and (outline-next-heading)
1025 (org-flag-heading nil))) ; show the next heading
1026 (org-back-to-heading)
1027 (setq marker (copy-marker (point)))
1028 (goto-char (max (point-at-bol) (- (point-at-eol) anticol)))
1029 (funcall command)
1030 (message "_cmd: %S" _cmd)
1031 (message "this-command: %S" this-command)
1032 (unless (eq (marker-position marker) (marker-position endmarker))
1033 (setq newhead (org-get-heading))))
1035 (beginning-of-line 1)
1036 (save-excursion
1037 (org-agenda-change-all-lines newhead hdmarker 'fixface))))
1038 t))))
1040 (defun org-mouse-agenda-context-menu (&optional event)
1041 (or (org-mouse-do-remotely 'org-mouse-context-menu)
1042 (popup-menu
1043 '("Agenda"
1044 ("Agenda Files")
1045 "--"
1046 ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo))
1047 :visible (if (eq last-command 'org-agenda-undo)
1048 org-agenda-pending-undo-list
1049 org-agenda-undo-list)]
1050 ["Rebuild Buffer" org-agenda-redo t]
1051 ["New Diary Entry"
1052 org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline) t]
1053 "--"
1054 ["Goto Today" org-agenda-goto-today
1055 (org-agenda-check-type nil 'agenda 'timeline) t]
1056 ["Display Calendar" org-agenda-goto-calendar
1057 (org-agenda-check-type nil 'agenda 'timeline) t]
1058 ("Calendar Commands"
1059 ["Phases of the Moon" org-agenda-phases-of-moon
1060 (org-agenda-check-type nil 'agenda 'timeline)]
1061 ["Sunrise/Sunset" org-agenda-sunrise-sunset
1062 (org-agenda-check-type nil 'agenda 'timeline)]
1063 ["Holidays" org-agenda-holidays
1064 (org-agenda-check-type nil 'agenda 'timeline)]
1065 ["Convert" org-agenda-convert-date
1066 (org-agenda-check-type nil 'agenda 'timeline)]
1067 "--"
1068 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
1069 "--"
1070 ["Day View" org-agenda-day-view
1071 :active (org-agenda-check-type nil 'agenda)
1072 :style radio :selected (equal org-agenda-ndays 1)]
1073 ["Week View" org-agenda-week-view
1074 :active (org-agenda-check-type nil 'agenda)
1075 :style radio :selected (equal org-agenda-ndays 7)]
1076 "--"
1077 ["Show Logbook entries" org-agenda-log-mode
1078 :style toggle :selected org-agenda-show-log
1079 :active (org-agenda-check-type nil 'agenda 'timeline)]
1080 ["Include Diary" org-agenda-toggle-diary
1081 :style toggle :selected org-agenda-include-diary
1082 :active (org-agenda-check-type nil 'agenda)]
1083 ["Use Time Grid" org-agenda-toggle-time-grid
1084 :style toggle :selected org-agenda-use-time-grid
1085 :active (org-agenda-check-type nil 'agenda)]
1086 ["Follow Mode" org-agenda-follow-mode
1087 :style toggle :selected org-agenda-follow-mode]
1088 "--"
1089 ["Quit" org-agenda-quit t]
1090 ["Exit and Release Buffers" org-agenda-exit t]
1091 ))))
1093 (defun org-mouse-get-gesture (event)
1094 (let ((startxy (posn-x-y (event-start event)))
1095 (endxy (posn-x-y (event-end event))))
1096 (if (< (car startxy) (car endxy)) :right :left)))
1099 ; (setq org-agenda-mode-hook nil)
1100 (add-hook 'org-agenda-mode-hook
1101 '(lambda ()
1102 (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu)
1103 (define-key org-agenda-keymap
1104 (if (featurep 'xemacs) [button3] [mouse-3])
1105 'org-mouse-show-context-menu)
1106 (define-key org-agenda-keymap [down-mouse-3] 'org-mouse-move-tree-start)
1107 (define-key org-agenda-keymap [C-mouse-4] 'org-agenda-earlier)
1108 (define-key org-agenda-keymap [C-mouse-5] 'org-agenda-later)
1109 (define-key org-agenda-keymap [drag-mouse-3]
1110 '(lambda (event) (interactive "e")
1111 (case (org-mouse-get-gesture event)
1112 (:left (org-agenda-earlier 1))
1113 (:right (org-agenda-later 1)))))))
1115 (provide 'org-mouse)
1117 ;; arch-tag: ff1ae557-3529-41a3-95c6-baaebdcc280f