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