Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-mouse.el
blobedbc18750d81165f121bca9521a1f0e8c7a86054
1 ;;; org-mouse.el --- Better mouse support for org-mode
3 ;; Copyright (C) 2006, 2007, 2008, 2009 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.21a
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 (eval-when-compile (require 'cl))
141 (require 'org)
143 (defvar org-agenda-allow-remote-undo)
144 (defvar org-agenda-undo-list)
145 (defvar org-agenda-custom-commands)
146 (declare-function org-agenda-change-all-lines "org-agenda"
147 (newhead hdmarker &optional fixface just-this))
148 (declare-function org-verify-change-for-undo "org-agenda" (l1 l2))
150 (defvar org-mouse-plain-list-regexp "\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
151 "Regular expression that matches a plain list.")
152 (defvar org-mouse-direct t
153 "Internal variable indicating whether the current action is direct.
155 If t, then the current action has been invoked directly through the buffer
156 it is intended to operate on. If nil, then the action has been invoked
157 indirectly, for example, through the agenda buffer.")
159 (defgroup org-mouse nil
160 "Mouse support for org-mode."
161 :tag "Org Mouse"
162 :group 'org)
164 (defcustom org-mouse-punctuation ":"
165 "Punctuation used when inserting text by drag and drop."
166 :group 'org-mouse
167 :type 'string)
169 (defcustom org-mouse-features
170 '(context-menu yank-link activate-stars activate-bullets activate-checkboxes)
171 "The features of org-mouse that should be activated.
172 Changing this variable requires a restart of Emacs to get activated."
173 :group 'org-mouse
174 :type '(set :greedy t
175 (const :tag "Mouse-3 shows context menu" context-menu)
176 (const :tag "C-mouse-1 and mouse-3 move trees" move-tree)
177 (const :tag "S-mouse-2 and drag-mouse-3 yank link" yank-link)
178 (const :tag "Activate headline stars" activate-stars)
179 (const :tag "Activate item bullets" activate-bullets)
180 (const :tag "Activate checkboxes" activate-checkboxes)))
182 (defun org-mouse-re-search-line (regexp)
183 "Search the current line for a given regular expression."
184 (beginning-of-line)
185 (re-search-forward regexp (point-at-eol) t))
187 (defun org-mouse-end-headline ()
188 "Go to the end of current headline (ignoring tags)."
189 (interactive)
190 (end-of-line)
191 (skip-chars-backward "\t ")
192 (when (looking-back ":[A-Za-z]+:")
193 (skip-chars-backward ":A-Za-z")
194 (skip-chars-backward "\t ")))
196 (defvar org-mouse-context-menu-function nil
197 "Function to create the context menu.
198 The value of this variable is the function invoked by
199 `org-mouse-context-menu' as the context menu.")
200 (make-variable-buffer-local 'org-mouse-context-menu-function)
202 (defun org-mouse-show-context-menu (event prefix)
203 "Invoke the context menu.
205 If the value of `org-mouse-context-menu-function' is a function, then
206 this function is called. Otherwise, the current major mode menu is used."
207 (interactive "@e \nP")
208 (if (and (= (event-click-count event) 1)
209 (or (not mark-active)
210 (sit-for (/ double-click-time 1000.0))))
211 (progn
212 (select-window (posn-window (event-start event)))
213 (when (not (org-mouse-mark-active))
214 (goto-char (posn-point (event-start event)))
215 (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook)))
216 (let ((redisplay-dont-pause t))
217 (sit-for 0)))
218 (if (functionp org-mouse-context-menu-function)
219 (funcall org-mouse-context-menu-function event)
220 (if (fboundp 'mouse-menu-major-mode-map)
221 (popup-menu (mouse-menu-major-mode-map) event prefix)
222 (org-no-warnings ; don't warn about fallback, obsolete since 23.1
223 (mouse-major-mode-menu event prefix)))))
224 (setq this-command 'mouse-save-then-kill)
225 (mouse-save-then-kill event)))
227 (defun org-mouse-line-position ()
228 "Returns `:beginning' or `:middle' or `:end', depending on the point position.
230 If the point is at the end of the line, return `:end'.
231 If the point is separated from the beginning of the line only by white
232 space and *'s (`org-mouse-bolp'), return `:beginning'. Otherwise,
233 return `:middle'."
234 (cond
235 ((eolp) :end)
236 ((org-mouse-bolp) :beginning)
237 (t :middle)))
239 (defun org-mouse-empty-line ()
240 "Return non-nil iff the line contains only white space."
241 (save-excursion (beginning-of-line) (looking-at "[ \t]*$")))
243 (defun org-mouse-next-heading ()
244 "Go to the next heading.
245 If there is none, ensure that the point is at the beginning of an empty line."
246 (unless (outline-next-heading)
247 (beginning-of-line)
248 (unless (org-mouse-empty-line)
249 (end-of-line)
250 (newline))))
252 (defun org-mouse-insert-heading ()
253 "Insert a new heading, as `org-insert-heading'.
255 If the point is at the :beginning (`org-mouse-line-position') of the line,
256 insert the new heading before the current line. Otherwise, insert it
257 after the current heading."
258 (interactive)
259 (case (org-mouse-line-position)
260 (:beginning (beginning-of-line)
261 (org-insert-heading))
262 (t (org-mouse-next-heading)
263 (org-insert-heading))))
265 (defun org-mouse-timestamp-today (&optional shift units)
266 "Change the timestamp into SHIFT UNITS in the future.
268 For the acceptable UNITS, see `org-timestamp-change'."
269 (interactive)
270 (flet ((org-read-date (&rest rest) (current-time)))
271 (org-time-stamp nil))
272 (when shift
273 (org-timestamp-change shift units)))
275 (defun org-mouse-keyword-menu (keywords function &optional selected itemformat)
276 "A helper function.
278 Returns a menu fragment consisting of KEYWORDS. When a keyword
279 is selected by the user, FUNCTION is called with the selected
280 keyword as the only argument.
282 If SELECTED is nil, then all items are normal menu items. If
283 SELECTED is a function, then each item is a checkbox, which is
284 enabled for a given keyword iff (funcall SELECTED keyword) return
285 non-nil. If SELECTED is neither nil nor a function, then the
286 items are radio buttons. A radio button is enabled for the
287 keyword `equal' to SELECTED.
289 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
290 is a function, it is invoked with the keyword as the only
291 argument. If it is a string, it is interpreted as the format
292 string to (format ITEMFORMAT keyword). If it is neither a string
293 nor a function, elements of KEYWORDS are used directly. "
294 (mapcar
295 `(lambda (keyword)
296 (vector (cond
297 ((functionp ,itemformat) (funcall ,itemformat keyword))
298 ((stringp ,itemformat) (format ,itemformat keyword))
299 (t keyword))
300 (list 'funcall ,function keyword)
301 :style (cond
302 ((null ,selected) t)
303 ((functionp ,selected) 'toggle)
304 (t 'radio))
305 :selected (if (functionp ,selected)
306 (and (funcall ,selected keyword) t)
307 (equal ,selected keyword))))
308 keywords))
310 (defun org-mouse-remove-match-and-spaces ()
311 "Remove the match, make just one space around the point."
312 (interactive)
313 (replace-match "")
314 (just-one-space))
316 (defvar rest)
317 (defun org-mouse-replace-match-and-surround (newtext &optional fixedcase
318 literal string subexp)
319 "The same as `replace-match', but surrounds the replacement with spaces."
320 (apply 'replace-match rest)
321 (save-excursion
322 (goto-char (match-beginning (or subexp 0)))
323 (just-one-space)
324 (goto-char (match-end (or subexp 0)))
325 (just-one-space)))
328 (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat
329 nosurround)
330 "A helper function.
332 Returns a menu fragment consisting of KEYWORDS. When a keyword
333 is selected, group GROUP of the current match is replaced by the
334 keyword. The method ensures that both ends of the replacement
335 are separated from the rest of the text in the buffer by
336 individual spaces (unless NOSURROUND is non-nil).
338 The final entry of the menu is always \"None\", which removes the
339 match.
341 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
342 is a function, it is invoked with the keyword as the only
343 argument. If it is a string, it is interpreted as the format
344 string to (format ITEMFORMAT keyword). If it is neither a string
345 nor a function, elements of KEYWORDS are used directly.
347 (setq group (or group 0))
348 (let ((replace (org-mouse-match-closure
349 (if nosurround 'replace-match
350 'org-mouse-replace-match-and-surround))))
351 (append
352 (org-mouse-keyword-menu
353 keywords
354 `(lambda (keyword) (funcall ,replace keyword t t nil ,group))
355 (match-string group)
356 itemformat)
357 `(["None" org-mouse-remove-match-and-spaces
358 :style radio
359 :selected ,(not (member (match-string group) keywords))]))))
361 (defun org-mouse-show-headlines ()
362 "Change the visibility of the current org buffer to only show headlines."
363 (interactive)
364 (let ((this-command 'org-cycle)
365 (last-command 'org-cycle)
366 (org-cycle-global-status nil))
367 (org-cycle '(4))
368 (org-cycle '(4))))
370 (defun org-mouse-show-overview ()
371 "Change visibility of current org buffer to first-level headlines only."
372 (interactive)
373 (let ((org-cycle-global-status nil))
374 (org-cycle '(4))))
376 (defun org-mouse-set-priority (priority)
377 "Set the priority of the current headline to PRIORITY."
378 (flet ((read-char-exclusive () priority))
379 (org-priority)))
381 (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]"
382 "Regular expression matching the priority indicator.
383 Differs from `org-priority-regexp' in that it doesn't contain the
384 leading '.*?'.")
386 (defun org-mouse-get-priority (&optional default)
387 "Return the priority of the current headline.
388 DEFAULT is returned if no priority is given in the headline."
389 (save-excursion
390 (if (org-mouse-re-search-line org-mouse-priority-regexp)
391 (match-string 1)
392 (when default (char-to-string org-default-priority)))))
394 ;; (defun org-mouse-at-link ()
395 ;; (and (eq (get-text-property (point) 'face) 'org-link)
396 ;; (save-excursion
397 ;; (goto-char (previous-single-property-change (point) 'face))
398 ;; (or (looking-at org-bracket-link-regexp)
399 ;; (looking-at org-angle-link-re)
400 ;; (looking-at org-plain-link-re)))))
403 (defun org-mouse-delete-timestamp ()
404 "Deletes the current timestamp as well as the preceding keyword.
405 SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:"
406 (when (or (org-at-date-range-p) (org-at-timestamp-p))
407 (replace-match "") ; delete the timestamp
408 (skip-chars-backward " :A-Z")
409 (when (looking-at " *[A-Z][A-Z]+:")
410 (replace-match ""))))
412 (defun org-mouse-looking-at (regexp skipchars &optional movechars)
413 (save-excursion
414 (let ((point (point)))
415 (if (looking-at regexp) t
416 (skip-chars-backward skipchars)
417 (forward-char (or movechars 0))
418 (when (looking-at regexp)
419 (> (match-end 0) point))))))
421 (defun org-mouse-priority-list ()
422 (loop for priority from ?A to org-lowest-priority
423 collect (char-to-string priority)))
425 (defun org-mouse-tag-menu () ;todo
426 (append
427 (let ((tags (org-get-tags)))
428 (org-mouse-keyword-menu
429 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
430 `(lambda (tag)
431 (org-mouse-set-tags
432 (sort (if (member tag (quote ,tags))
433 (delete tag (quote ,tags))
434 (cons tag (quote ,tags)))
435 'string-lessp)))
436 `(lambda (tag) (member tag (quote ,tags)))
438 '("--"
439 ["Align Tags Here" (org-set-tags nil t) t]
440 ["Align Tags in Buffer" (org-set-tags t t) t]
441 ["Set Tags ..." (org-set-tags) t])))
445 (defun org-mouse-set-tags (tags)
446 (save-excursion
447 ;; remove existing tags first
448 (beginning-of-line)
449 (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)")
450 (replace-match ""))
452 ;; set new tags if any
453 (when tags
454 (end-of-line)
455 (insert " :" (mapconcat 'identity tags ":") ":")
456 (org-set-tags nil t))))
458 (defun org-mouse-insert-checkbox ()
459 (interactive)
460 (and (org-at-item-p)
461 (goto-char (match-end 0))
462 (unless (org-at-item-checkbox-p)
463 (delete-horizontal-space)
464 (insert " [ ] "))))
466 (defun org-mouse-agenda-type (type)
467 (case type
468 ('tags "Tags: ")
469 ('todo "TODO: ")
470 ('tags-tree "Tags tree: ")
471 ('todo-tree "TODO tree: ")
472 ('occur-tree "Occur tree: ")
473 (t "Agenda command ???")))
476 (defun org-mouse-list-options-menu (alloptions &optional function)
477 (let ((options (save-match-data
478 (split-string (match-string-no-properties 1)))))
479 (print options)
480 (loop for name in alloptions
481 collect
482 (vector name
483 `(progn
484 (replace-match
485 (mapconcat 'identity
486 (sort (if (member ',name ',options)
487 (delete ',name ',options)
488 (cons ',name ',options))
489 'string-lessp)
490 " ")
491 nil nil nil 1)
492 (when (functionp ',function) (funcall ',function)))
493 :style 'toggle
494 :selected (and (member name options) t)))))
496 (defun org-mouse-clip-text (text maxlength)
497 (if (> (length text) maxlength)
498 (concat (substring text 0 (- maxlength 3)) "...")
499 text))
501 (defun org-mouse-popup-global-menu ()
502 (popup-menu
503 `("Main Menu"
504 ["Show Overview" org-mouse-show-overview t]
505 ["Show Headlines" org-mouse-show-headlines t]
506 ["Show All" show-all t]
507 ["Remove Highlights" org-remove-occur-highlights
508 :visible org-occur-highlights]
509 "--"
510 ["Check Deadlines"
511 (if (functionp 'org-check-deadlines-and-todos)
512 (org-check-deadlines-and-todos org-deadline-warning-days)
513 (org-check-deadlines org-deadline-warning-days)) t]
514 ["Check TODOs" org-show-todo-tree t]
515 ("Check Tags"
516 ,@(org-mouse-keyword-menu
517 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
518 '(lambda (tag) (org-tags-sparse-tree nil tag)))
519 "--"
520 ["Custom Tag ..." org-tags-sparse-tree t])
521 ["Check Phrase ..." org-occur]
522 "--"
523 ["Display Agenda" org-agenda-list t]
524 ["Display Timeline" org-timeline t]
525 ["Display TODO List" org-todo-list t]
526 ("Display Tags"
527 ,@(org-mouse-keyword-menu
528 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
529 '(lambda (tag) (org-tags-view nil tag)))
530 "--"
531 ["Custom Tag ..." org-tags-view t])
532 ["Display Calendar" org-goto-calendar t]
533 "--"
534 ,@(org-mouse-keyword-menu
535 (mapcar 'car org-agenda-custom-commands)
536 '(lambda (key)
537 (eval `(flet ((read-char-exclusive () (string-to-char ,key)))
538 (org-agenda nil))))
540 '(lambda (key)
541 (let ((entry (assoc key org-agenda-custom-commands)))
542 (org-mouse-clip-text
543 (cond
544 ((stringp (nth 1 entry)) (nth 1 entry))
545 ((stringp (nth 2 entry))
546 (concat (org-mouse-agenda-type (nth 1 entry))
547 (nth 2 entry)))
548 (t "Agenda Command '%s'"))
549 30))))
550 "--"
551 ["Delete Blank Lines" delete-blank-lines
552 :visible (org-mouse-empty-line)]
553 ["Insert Checkbox" org-mouse-insert-checkbox
554 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
555 ["Insert Checkboxes"
556 (org-mouse-for-each-item 'org-mouse-insert-checkbox)
557 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
558 ["Plain List to Outline" org-mouse-transform-to-outline
559 :visible (org-at-item-p)])))
562 (defun org-mouse-get-context (contextlist context)
563 (let ((contextdata (assq context contextlist)))
564 (when contextdata
565 (save-excursion
566 (goto-char (second contextdata))
567 (re-search-forward ".*" (third contextdata))))))
569 (defun org-mouse-for-each-item (function)
570 (save-excursion
571 (ignore-errors
572 (while t (org-previous-item)))
573 (ignore-errors
574 (while t
575 (funcall function)
576 (org-next-item)))))
578 (defun org-mouse-bolp ()
579 "Returns true if there only spaces, tabs, and '*', between the beginning of line and the point"
580 (save-excursion
581 (skip-chars-backward " \t*") (bolp)))
583 (defun org-mouse-insert-item (text)
584 (case (org-mouse-line-position)
585 (:beginning ; insert before
586 (beginning-of-line)
587 (looking-at "[ \t]*")
588 (open-line 1)
589 (org-indent-to-column (- (match-end 0) (match-beginning 0)))
590 (insert "+ "))
592 (:middle ; insert after
593 (end-of-line)
594 (newline t)
595 (indent-relative)
596 (insert "+ "))
598 (:end ; insert text here
599 (skip-chars-backward " \t")
600 (kill-region (point) (point-at-eol))
601 (unless (looking-back org-mouse-punctuation)
602 (insert (concat org-mouse-punctuation " ")))))
604 (insert text)
605 (beginning-of-line))
607 (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate)
608 (if (eq major-mode 'org-mode)
609 (org-mouse-insert-item text)
610 ad-do-it))
612 (defadvice dnd-open-file (around org-mouse-dnd-open-file activate)
613 (if (eq major-mode 'org-mode)
614 (org-mouse-insert-item uri)
615 ad-do-it))
617 (defun org-mouse-match-closure (function)
618 (let ((match (match-data t)))
619 `(lambda (&rest rest)
620 (save-match-data
621 (set-match-data ',match)
622 (apply ',function rest)))))
624 (defun org-mouse-todo-keywords ()
625 (if (boundp 'org-todo-keywords-1) org-todo-keywords-1 org-todo-keywords))
627 (defun org-mouse-match-todo-keyword ()
628 (save-excursion
629 (org-back-to-heading)
630 (if (looking-at outline-regexp) (goto-char (match-end 0)))
631 (or (looking-at (concat " +" org-todo-regexp " *"))
632 (looking-at " \\( *\\)"))))
634 (defun org-mouse-yank-link (click)
635 (interactive "e")
636 ;; Give temporary modes such as isearch a chance to turn off.
637 (run-hooks 'mouse-leave-buffer-hook)
638 (mouse-set-point click)
639 (setq mouse-selection-click-count 0)
640 (delete-horizontal-space)
641 (insert-for-yank (concat " [[" (current-kill 0) "]] ")))
643 (defun org-mouse-context-menu (&optional event)
644 (let ((stamp-prefixes (list org-deadline-string org-scheduled-string))
645 (contextlist (org-context)))
646 (flet ((get-context (context) (org-mouse-get-context contextlist context)))
647 (cond
648 ((org-mouse-mark-active)
649 (let ((region-string (buffer-substring (region-beginning) (region-end))))
650 (popup-menu
651 `(nil
652 ["Sparse Tree" (org-occur ',region-string)]
653 ["Find in Buffer" (occur ',region-string)]
654 ["Grep in Current Dir"
655 (grep (format "grep -rnH -e '%s' *" ',region-string))]
656 ["Grep in Parent Dir"
657 (grep (format "grep -rnH -e '%s' ../*" ',region-string))]
658 "--"
659 ["Convert to Link"
660 (progn (save-excursion (goto-char (region-beginning)) (insert "[["))
661 (save-excursion (goto-char (region-end)) (insert "]]")))]
662 ["Insert Link Here" (org-mouse-yank-link ',event)]))))
664 ((save-excursion (beginning-of-line) (looking-at "#\\+STARTUP: \\(.*\\)"))
665 (popup-menu
666 `(nil
667 ,@(org-mouse-list-options-menu (mapcar 'car org-startup-options)
668 'org-mode-restart))))
669 ((or (eolp)
670 (and (looking-at "\\( \\|\t\\)\\(+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$")
671 (looking-back " \\|\t")))
672 (org-mouse-popup-global-menu))
673 ((get-context :checkbox)
674 (popup-menu
675 '(nil
676 ["Toggle" org-toggle-checkbox t]
677 ["Remove" org-mouse-remove-match-and-spaces t]
679 ["All Clear" (org-mouse-for-each-item
680 (lambda ()
681 (when (save-excursion (org-at-item-checkbox-p))
682 (replace-match "[ ]"))))]
683 ["All Set" (org-mouse-for-each-item
684 (lambda ()
685 (when (save-excursion (org-at-item-checkbox-p))
686 (replace-match "[X]"))))]
687 ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t]
688 ["All Remove" (org-mouse-for-each-item
689 (lambda ()
690 (when (save-excursion (org-at-item-checkbox-p))
691 (org-mouse-remove-match-and-spaces))))]
693 ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_")
694 (member (match-string 0) (org-mouse-todo-keywords)))
695 (popup-menu
696 `(nil
697 ,@(org-mouse-keyword-replace-menu (org-mouse-todo-keywords))
698 "--"
699 ["Check TODOs" org-show-todo-tree t]
700 ["List all TODO keywords" org-todo-list t]
701 [,(format "List only %s" (match-string 0))
702 (org-todo-list (match-string 0)) t]
704 ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z")
705 (member (match-string 0) stamp-prefixes))
706 (popup-menu
707 `(nil
708 ,@(org-mouse-keyword-replace-menu stamp-prefixes)
709 "--"
710 ["Check Deadlines" org-check-deadlines t]
712 ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority
713 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
714 (org-mouse-priority-list) 1 "Priority %s" t))))
715 ((get-context :link)
716 (popup-menu
717 '(nil
718 ["Open" org-open-at-point t]
719 ["Open in Emacs" (org-open-at-point t) t]
720 "--"
721 ["Copy link" (kill-new (match-string 0))]
722 ["Cut link"
723 (progn
724 (kill-region (match-beginning 0) (match-end 0))
725 (just-one-space))]
726 "--"
727 ["Grep for TODOs"
728 (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))]
729 ; ["Paste file link" ((insert "file:") (yank))]
731 ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags
732 (popup-menu
733 `(nil
734 [,(format "Display '%s'" (match-string 1))
735 (org-tags-view nil ,(match-string 1))]
736 [,(format "Sparse Tree '%s'" (match-string 1))
737 (org-tags-sparse-tree nil ,(match-string 1))]
738 "--"
739 ,@(org-mouse-tag-menu))))
740 ((org-at-timestamp-p)
741 (popup-menu
742 '(nil
743 ["Show Day" org-open-at-point t]
744 ["Change Timestamp" org-time-stamp t]
745 ["Delete Timestamp" (org-mouse-delete-timestamp) t]
746 ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)]
747 "--"
748 ["Set for Today" org-mouse-timestamp-today]
749 ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)]
750 ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)]
751 ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)]
752 ["Set in a Month" (org-mouse-timestamp-today 1 'month)]
753 "--"
754 ["+ 1 Day" (org-timestamp-change 1 'day)]
755 ["+ 1 Week" (org-timestamp-change 7 'day)]
756 ["+ 1 Month" (org-timestamp-change 1 'month)]
757 "--"
758 ["- 1 Day" (org-timestamp-change -1 'day)]
759 ["- 1 Week" (org-timestamp-change -7 'day)]
760 ["- 1 Month" (org-timestamp-change -1 'month)])))
761 ((get-context :table-special)
762 (let ((mdata (match-data)))
763 (incf (car mdata) 2)
764 (store-match-data mdata))
765 (message "match: %S" (match-string 0))
766 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
767 '(" " "!" "^" "_" "$" "#" "*" "'") 0
768 (lambda (mark)
769 (case (string-to-char mark)
770 (? "( ) Nothing Special")
771 (?! "(!) Column Names")
772 (?^ "(^) Field Names Above")
773 (?_ "(^) Field Names Below")
774 (?$ "($) Formula Parameters")
775 (?# "(#) Recalculation: Auto")
776 (?* "(*) Recalculation: Manual")
777 (?' "(') Recalculation: None"))) t))))
778 ((assq :table contextlist)
779 (popup-menu
780 '(nil
781 ["Align Table" org-ctrl-c-ctrl-c]
782 ["Blank Field" org-table-blank-field]
783 ["Edit Field" org-table-edit-field]
784 "--"
785 ("Column"
786 ["Move Column Left" org-metaleft]
787 ["Move Column Right" org-metaright]
788 ["Delete Column" org-shiftmetaleft]
789 ["Insert Column" org-shiftmetaright]
790 "--"
791 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle])
792 ("Row"
793 ["Move Row Up" org-metaup]
794 ["Move Row Down" org-metadown]
795 ["Delete Row" org-shiftmetaup]
796 ["Insert Row" org-shiftmetadown]
797 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
798 "--"
799 ["Insert Hline" org-table-insert-hline])
800 ("Rectangle"
801 ["Copy Rectangle" org-copy-special]
802 ["Cut Rectangle" org-cut-special]
803 ["Paste Rectangle" org-paste-special]
804 ["Fill Rectangle" org-table-wrap-region])
805 "--"
806 ["Set Column Formula" org-table-eval-formula]
807 ["Set Field Formula" (org-table-eval-formula '(4))]
808 ["Edit Formulas" org-table-edit-formulas]
809 "--"
810 ["Recalculate Line" org-table-recalculate]
811 ["Recalculate All" (org-table-recalculate '(4))]
812 ["Iterate All" (org-table-recalculate '(16))]
813 "--"
814 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks]
815 ["Sum Column/Rectangle" org-table-sum
816 :active (or (org-at-table-p) (org-region-active-p))]
817 ["Field Info" org-table-field-info]
818 ["Debug Formulas"
819 (setq org-table-formula-debug (not org-table-formula-debug))
820 :style toggle :selected org-table-formula-debug]
822 ((and (assq :headline contextlist) (not (eolp)))
823 (let ((priority (org-mouse-get-priority t)))
824 (popup-menu
825 `("Headline Menu"
826 ("Tags and Priorities"
827 ,@(org-mouse-keyword-menu
828 (org-mouse-priority-list)
829 '(lambda (keyword)
830 (org-mouse-set-priority (string-to-char keyword)))
831 priority "Priority %s")
832 "--"
833 ,@(org-mouse-tag-menu))
834 ("TODO Status"
835 ,@(progn (org-mouse-match-todo-keyword)
836 (org-mouse-keyword-replace-menu (org-mouse-todo-keywords)
837 1)))
838 ["Show Tags"
839 (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags))
840 :visible (not org-mouse-direct)]
841 ["Show Priority"
842 (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority))
843 :visible (not org-mouse-direct)]
844 ,@(if org-mouse-direct '("--") nil)
845 ["New Heading" org-mouse-insert-heading :visible org-mouse-direct]
846 ["Set Deadline"
847 (progn (org-mouse-end-headline) (insert " ") (org-deadline))
848 :active (not (save-excursion
849 (org-mouse-re-search-line org-deadline-regexp)))]
850 ["Schedule Task"
851 (progn (org-mouse-end-headline) (insert " ") (org-schedule))
852 :active (not (save-excursion
853 (org-mouse-re-search-line org-scheduled-regexp)))]
854 ["Insert Timestamp"
855 (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t]
856 ; ["Timestamp (inactive)" org-time-stamp-inactive t]
857 "--"
858 ["Archive Subtree" org-archive-subtree]
859 ["Cut Subtree" org-cut-special]
860 ["Copy Subtree" org-copy-special]
861 ["Paste Subtree" org-paste-special :visible org-mouse-direct]
862 ("Sort Children"
863 ["Alphabetically" (org-sort-entries nil ?a)]
864 ["Numerically" (org-sort-entries nil ?n)]
865 ["By Time/Date" (org-sort-entries nil ?t)]
866 "--"
867 ["Reverse Alphabetically" (org-sort-entries nil ?A)]
868 ["Reverse Numerically" (org-sort-entries nil ?N)]
869 ["Reverse By Time/Date" (org-sort-entries nil ?T)])
870 "--"
871 ["Move Trees" org-mouse-move-tree :active nil]
872 ))))
874 (org-mouse-popup-global-menu))))))
876 ;; (defun org-mouse-at-regexp (regexp)
877 ;; (save-excursion
878 ;; (let ((point (point))
879 ;; (bol (progn (beginning-of-line) (point)))
880 ;; (eol (progn (end-of-line) (point))))
881 ;; (goto-char point)
882 ;; (re-search-backward regexp bol 1)
883 ;; (and (not (eolp))
884 ;; (progn (forward-char)
885 ;; (re-search-forward regexp eol t))
886 ;; (<= (match-beginning 0) point)))))
888 (defun org-mouse-mark-active ()
889 (and mark-active transient-mark-mode))
891 (defun org-mouse-in-region-p (pos)
892 (and (org-mouse-mark-active)
893 (>= pos (region-beginning))
894 (< pos (region-end))))
896 (defun org-mouse-down-mouse (event)
897 (interactive "e")
898 (setq this-command last-command)
899 (unless (and (= 1 (event-click-count event))
900 (org-mouse-in-region-p (posn-point (event-start event))))
901 (mouse-drag-region event)))
903 (add-hook 'org-mode-hook
904 '(lambda ()
905 (setq org-mouse-context-menu-function 'org-mouse-context-menu)
907 (when (memq 'context-menu org-mouse-features)
908 (define-key org-mouse-map (if (featurep 'xemacs) [button3] [mouse-3]) nil)
909 (define-key org-mode-map [mouse-3] 'org-mouse-show-context-menu))
910 (define-key org-mode-map [down-mouse-1] 'org-mouse-down-mouse)
911 (when (memq 'context-menu org-mouse-features)
912 (define-key org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree)
913 (define-key org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start))
914 (when (memq 'yank-link org-mouse-features)
915 (define-key org-mode-map [S-mouse-2] 'org-mouse-yank-link)
916 (define-key org-mode-map [drag-mouse-3] 'org-mouse-yank-link))
917 (when (memq 'move-tree org-mouse-features)
918 (define-key org-mouse-map [drag-mouse-3] 'org-mouse-move-tree)
919 (define-key org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start))
921 (when (memq 'activate-stars org-mouse-features)
922 (font-lock-add-keywords
924 `((,outline-regexp
925 0 `(face org-link mouse-face highlight keymap ,org-mouse-map)
926 'prepend))
929 (when (memq 'activate-bullets org-mouse-features)
930 (font-lock-add-keywords
932 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +"
933 (1 `(face org-link keymap ,org-mouse-map mouse-face highlight)
934 'prepend)))
937 (when (memq 'activate-checkboxes org-mouse-features)
938 (font-lock-add-keywords
940 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
941 (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t)))
944 (defadvice org-open-at-point (around org-mouse-open-at-point activate)
945 (let ((context (org-context)))
946 (cond
947 ((assq :headline-stars context) (org-cycle))
948 ((assq :checkbox context) (org-toggle-checkbox))
949 ((assq :item-bullet context)
950 (let ((org-cycle-include-plain-lists t)) (org-cycle)))
951 (t ad-do-it))))))
953 (defun org-mouse-move-tree-start (event)
954 (interactive "e")
955 (message "Same line: promote/demote, (***):move before, (text): make a child"))
958 (defun org-mouse-make-marker (position)
959 (with-current-buffer (window-buffer (posn-window position))
960 (copy-marker (posn-point position))))
962 (defun org-mouse-move-tree (event)
963 ;; todo: handle movements between different buffers
964 (interactive "e")
965 (save-excursion
966 (let* ((start (org-mouse-make-marker (event-start event)))
967 (end (org-mouse-make-marker (event-end event)))
968 (sbuf (marker-buffer start))
969 (ebuf (marker-buffer end)))
971 (when (and sbuf ebuf)
972 (set-buffer sbuf)
973 (goto-char start)
974 (org-back-to-heading)
975 (if (and (eq sbuf ebuf)
976 (equal
977 (point)
978 (save-excursion (goto-char end) (org-back-to-heading) (point))))
979 ;; if the same line then promote/demote
980 (if (>= end start) (org-demote-subtree) (org-promote-subtree))
981 ;; if different lines then move
982 (org-cut-subtree)
984 (set-buffer ebuf)
985 (goto-char end)
986 (org-back-to-heading)
987 (when (and (eq sbuf ebuf)
988 (equal
989 (point)
990 (save-excursion (goto-char start)
991 (org-back-to-heading) (point))))
992 (outline-end-of-subtree)
993 (end-of-line)
994 (if (eobp) (newline) (forward-char)))
996 (when (looking-at outline-regexp)
997 (let ((level (- (match-end 0) (match-beginning 0))))
998 (when (> end (match-end 0))
999 (outline-end-of-subtree)
1000 (end-of-line)
1001 (if (eobp) (newline) (forward-char))
1002 (setq level (1+ level)))
1003 (org-paste-subtree level)
1004 (save-excursion
1005 (outline-end-of-subtree)
1006 (when (bolp) (delete-char -1))))))))))
1009 (defun org-mouse-transform-to-outline ()
1010 (interactive)
1011 (org-back-to-heading)
1012 (let ((minlevel 1000)
1013 (replace-text (concat (match-string 0) "* ")))
1014 (beginning-of-line 2)
1015 (save-excursion
1016 (while (not (or (eobp) (looking-at outline-regexp)))
1017 (when (looking-at org-mouse-plain-list-regexp)
1018 (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1)))))
1019 (forward-line)))
1020 (while (not (or (eobp) (looking-at outline-regexp)))
1021 (when (and (looking-at org-mouse-plain-list-regexp)
1022 (eq minlevel (- (match-end 1) (match-beginning 1))))
1023 (replace-match replace-text))
1024 (forward-line))))
1026 (defvar _cmd) ;dynamically scoped from `org-with-remote-undo'.
1028 (defun org-mouse-do-remotely (command)
1029 ; (org-agenda-check-no-diary)
1030 (when (get-text-property (point) 'org-marker)
1031 (let* ((anticol (- (point-at-eol) (point)))
1032 (marker (get-text-property (point) 'org-marker))
1033 (buffer (marker-buffer marker))
1034 (pos (marker-position marker))
1035 (hdmarker (get-text-property (point) 'org-hd-marker))
1036 (buffer-read-only nil)
1037 (newhead "--- removed ---")
1038 (org-mouse-direct nil)
1039 (org-mouse-main-buffer (current-buffer)))
1040 (when (eq (with-current-buffer buffer major-mode) 'org-mode)
1041 (let ((endmarker (save-excursion
1042 (set-buffer buffer)
1043 (outline-end-of-subtree)
1044 (forward-char 1)
1045 (copy-marker (point)))))
1046 (org-with-remote-undo buffer
1047 (with-current-buffer buffer
1048 (widen)
1049 (goto-char pos)
1050 (org-show-hidden-entry)
1051 (save-excursion
1052 (and (outline-next-heading)
1053 (org-flag-heading nil))) ; show the next heading
1054 (org-back-to-heading)
1055 (setq marker (copy-marker (point)))
1056 (goto-char (max (point-at-bol) (- (point-at-eol) anticol)))
1057 (funcall command)
1058 (message "_cmd: %S" _cmd)
1059 (message "this-command: %S" this-command)
1060 (unless (eq (marker-position marker) (marker-position endmarker))
1061 (setq newhead (org-get-heading))))
1063 (beginning-of-line 1)
1064 (save-excursion
1065 (org-agenda-change-all-lines newhead hdmarker 'fixface))))
1066 t))))
1068 (defun org-mouse-agenda-context-menu (&optional event)
1069 (or (org-mouse-do-remotely 'org-mouse-context-menu)
1070 (popup-menu
1071 '("Agenda"
1072 ("Agenda Files")
1073 "--"
1074 ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo))
1075 :visible (if (eq last-command 'org-agenda-undo)
1076 org-agenda-pending-undo-list
1077 org-agenda-undo-list)]
1078 ["Rebuild Buffer" org-agenda-redo t]
1079 ["New Diary Entry"
1080 org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline) t]
1081 "--"
1082 ["Goto Today" org-agenda-goto-today
1083 (org-agenda-check-type nil 'agenda 'timeline) t]
1084 ["Display Calendar" org-agenda-goto-calendar
1085 (org-agenda-check-type nil 'agenda 'timeline) t]
1086 ("Calendar Commands"
1087 ["Phases of the Moon" org-agenda-phases-of-moon
1088 (org-agenda-check-type nil 'agenda 'timeline)]
1089 ["Sunrise/Sunset" org-agenda-sunrise-sunset
1090 (org-agenda-check-type nil 'agenda 'timeline)]
1091 ["Holidays" org-agenda-holidays
1092 (org-agenda-check-type nil 'agenda 'timeline)]
1093 ["Convert" org-agenda-convert-date
1094 (org-agenda-check-type nil 'agenda 'timeline)]
1095 "--"
1096 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
1097 "--"
1098 ["Day View" org-agenda-day-view
1099 :active (org-agenda-check-type nil 'agenda)
1100 :style radio :selected (equal org-agenda-ndays 1)]
1101 ["Week View" org-agenda-week-view
1102 :active (org-agenda-check-type nil 'agenda)
1103 :style radio :selected (equal org-agenda-ndays 7)]
1104 "--"
1105 ["Show Logbook entries" org-agenda-log-mode
1106 :style toggle :selected org-agenda-show-log
1107 :active (org-agenda-check-type nil 'agenda 'timeline)]
1108 ["Include Diary" org-agenda-toggle-diary
1109 :style toggle :selected org-agenda-include-diary
1110 :active (org-agenda-check-type nil 'agenda)]
1111 ["Use Time Grid" org-agenda-toggle-time-grid
1112 :style toggle :selected org-agenda-use-time-grid
1113 :active (org-agenda-check-type nil 'agenda)]
1114 ["Follow Mode" org-agenda-follow-mode
1115 :style toggle :selected org-agenda-follow-mode]
1116 "--"
1117 ["Quit" org-agenda-quit t]
1118 ["Exit and Release Buffers" org-agenda-exit t]
1119 ))))
1121 (defun org-mouse-get-gesture (event)
1122 (let ((startxy (posn-x-y (event-start event)))
1123 (endxy (posn-x-y (event-end event))))
1124 (if (< (car startxy) (car endxy)) :right :left)))
1127 ; (setq org-agenda-mode-hook nil)
1128 (add-hook 'org-agenda-mode-hook
1129 '(lambda ()
1130 (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu)
1131 (define-key org-agenda-keymap
1132 (if (featurep 'xemacs) [button3] [mouse-3])
1133 'org-mouse-show-context-menu)
1134 (define-key org-agenda-keymap [down-mouse-3] 'org-mouse-move-tree-start)
1135 (define-key org-agenda-keymap [C-mouse-4] 'org-agenda-earlier)
1136 (define-key org-agenda-keymap [C-mouse-5] 'org-agenda-later)
1137 (define-key org-agenda-keymap [drag-mouse-3]
1138 '(lambda (event) (interactive "e")
1139 (case (org-mouse-get-gesture event)
1140 (:left (org-agenda-earlier 1))
1141 (:right (org-agenda-later 1)))))))
1143 (provide 'org-mouse)
1145 ;; arch-tag: ff1ae557-3529-41a3-95c6-baaebdcc280f
1147 ;;; org-mouse.el ends-here