Merge stuff from upsteam ruby-mode, part 1.
[emacs.git] / lisp / org / org-mouse.el
blobb467064b888043284a2466dddbeab566e4159339
1 ;;; org-mouse.el --- Better mouse support for org-mode
3 ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Piotr Zielinski <piotr dot zielinski at gmail dot com>
6 ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Org-mouse provides mouse support for org-mode.
27 ;; http://orgmode.org
29 ;; Org-mouse implements the following features:
30 ;; * following links with the left mouse button (in Emacs 22)
31 ;; * subtree expansion/collapse (org-cycle) with the left mouse button
32 ;; * several context menus on the right mouse button:
33 ;; + general text
34 ;; + headlines
35 ;; + timestamps
36 ;; + priorities
37 ;; + links
38 ;; + tags
39 ;; * promoting/demoting/moving subtrees with mouse-3
40 ;; + if the drag starts and ends in the same line then promote/demote
41 ;; + otherwise move the subtree
43 ;; Use
44 ;; ---
46 ;; To use this package, put the following line in your .emacs:
48 ;; (require 'org-mouse)
51 ;; FIXME:
52 ;; + deal with folding / unfolding issues
54 ;; TODO (This list is only theoretical, if you'd like to have some
55 ;; feature implemented or a bug fix please send me an email, even if
56 ;; something similar appears in the list below. This will help me get
57 ;; the priorities right.):
59 ;; + org-store-link, insert link
60 ;; + org tables
61 ;; + occur with the current word/tag (same menu item)
62 ;; + ctrl-c ctrl-c, for example, renumber the current list
63 ;; + internal links
65 ;; Please email the maintainer with new feature suggestions / bugs
67 ;; History:
69 ;; Since version 5.10: Changes are listed in the general org-mode docs.
71 ;; Version 5.09;; + Version number synchronization with Org-mode.
73 ;; Version 0.25
74 ;; + made compatible with org-mode 4.70 (thanks to Carsten for the patch)
76 ;; Version 0.24
77 ;; + minor changes to the table menu
79 ;; Version 0.23
80 ;; + preliminary support for tables and calculation marks
81 ;; + context menu support for org-agenda-undo & org-sort-entries
83 ;; Version 0.22
84 ;; + handles undo support for the agenda buffer (requires org-mode >=4.58)
86 ;; Version 0.21
87 ;; + selected text activates its context menu
88 ;; + shift-middleclick or right-drag inserts the text from the clipboard in the form of a link
90 ;; Version 0.20
91 ;; + the new "TODO Status" submenu replaces the "Cycle TODO" menu item
92 ;; + the TODO menu can now list occurrences of a specific TODO keyword
93 ;; + #+STARTUP line is now recognized
95 ;; Version 0.19
96 ;; + added support for dragging URLs to the org-buffer
98 ;; Version 0.18
99 ;; + added support for agenda blocks
101 ;; Version 0.17
102 ;; + toggle checkboxes with a single click
104 ;; Version 0.16
105 ;; + added support for checkboxes
107 ;; Version 0.15
108 ;; + org-mode now works with the Agenda buffer as well
110 ;; Version 0.14
111 ;; + added a menu option that converts plain list items to outline items
113 ;; Version 0.13
114 ;; + "Insert Heading" now inserts a sibling heading if the point is
115 ;; on "***" and a child heading otherwise
117 ;; Version 0.12
118 ;; + compatible with Emacs 21
119 ;; + custom agenda commands added to the main menu
120 ;; + moving trees should now work between windows in the same frame
122 ;; Version 0.11
123 ;; + fixed org-mouse-at-link (thanks to Carsten)
124 ;; + removed [follow-link] bindings
126 ;; Version 0.10
127 ;; + added a menu option to remove highlights
128 ;; + compatible with org-mode 4.21 now
130 ;; Version 0.08:
131 ;; + trees can be moved/promoted/demoted by dragging with the right
132 ;; mouse button (mouse-3)
133 ;; + small changes in the above function
135 ;; Versions 0.01 -- 0.07: (I don't remember)
137 ;;; Code:
139 (eval-when-compile (require 'cl))
140 (require 'org)
142 (defvar org-agenda-allow-remote-undo)
143 (defvar org-agenda-undo-list)
144 (defvar org-agenda-custom-commands)
145 (declare-function org-agenda-change-all-lines "org-agenda"
146 (newhead hdmarker &optional fixface just-this))
147 (declare-function org-verify-change-for-undo "org-agenda" (l1 l2))
148 (declare-function org-apply-on-list "org-list" (function init-value &rest args))
149 (declare-function org-agenda-earlier "org-agenda" (arg))
150 (declare-function org-agenda-later "org-agenda" (arg))
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 org-mouse-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 org-mouse-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)))
329 (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat
330 nosurround)
331 "A helper function.
333 Returns a menu fragment consisting of KEYWORDS. When a keyword
334 is selected, group GROUP of the current match is replaced by the
335 keyword. The method ensures that both ends of the replacement
336 are separated from the rest of the text in the buffer by
337 individual spaces (unless NOSURROUND is non-nil).
339 The final entry of the menu is always \"None\", which removes the
340 match.
342 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
343 is a function, it is invoked with the keyword as the only
344 argument. If it is a string, it is interpreted as the format
345 string to (format ITEMFORMAT keyword). If it is neither a string
346 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-delete-timestamp ()
395 "Deletes the current timestamp as well as the preceding keyword.
396 SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:"
397 (when (or (org-at-date-range-p) (org-at-timestamp-p))
398 (replace-match "") ; delete the timestamp
399 (skip-chars-backward " :A-Z")
400 (when (looking-at " *[A-Z][A-Z]+:")
401 (replace-match ""))))
403 (defun org-mouse-looking-at (regexp skipchars &optional movechars)
404 (save-excursion
405 (let ((point (point)))
406 (if (looking-at regexp) t
407 (skip-chars-backward skipchars)
408 (forward-char (or movechars 0))
409 (when (looking-at regexp)
410 (> (match-end 0) point))))))
412 (defun org-mouse-priority-list ()
413 (loop for priority from ?A to org-lowest-priority
414 collect (char-to-string priority)))
416 (defun org-mouse-todo-menu (state)
417 "Create the menu with TODO keywords."
418 (append
419 (let ((kwds org-todo-keywords-1))
420 (org-mouse-keyword-menu
421 kwds
422 `(lambda (kwd) (org-todo kwd))
423 (lambda (kwd) (equal state kwd))))))
425 (defun org-mouse-tag-menu () ;todo
426 "Create the tags menu."
427 (append
428 (let ((tags (org-get-tags)))
429 (org-mouse-keyword-menu
430 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
431 `(lambda (tag)
432 (org-mouse-set-tags
433 (sort (if (member tag (quote ,tags))
434 (delete tag (quote ,tags))
435 (cons tag (quote ,tags)))
436 'string-lessp)))
437 `(lambda (tag) (member tag (quote ,tags)))
439 '("--"
440 ["Align Tags Here" (org-set-tags nil t) t]
441 ["Align Tags in Buffer" (org-set-tags t t) t]
442 ["Set Tags ..." (org-set-tags) t])))
444 (defun org-mouse-set-tags (tags)
445 (save-excursion
446 ;; remove existing tags first
447 (beginning-of-line)
448 (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)")
449 (replace-match ""))
451 ;; set new tags if any
452 (when tags
453 (end-of-line)
454 (insert " :" (mapconcat 'identity tags ":") ":")
455 (org-set-tags nil t))))
457 (defun org-mouse-insert-checkbox ()
458 (interactive)
459 (and (org-at-item-p)
460 (goto-char (match-end 0))
461 (unless (org-at-item-checkbox-p)
462 (delete-horizontal-space)
463 (insert " [ ] "))))
465 (defun org-mouse-agenda-type (type)
466 (case type
467 ('tags "Tags: ")
468 ('todo "TODO: ")
469 ('tags-tree "Tags tree: ")
470 ('todo-tree "TODO tree: ")
471 ('occur-tree "Occur tree: ")
472 (t "Agenda command ???")))
474 (defun org-mouse-list-options-menu (alloptions &optional function)
475 (let ((options (save-match-data
476 (split-string (match-string-no-properties 1)))))
477 (print options)
478 (loop for name in alloptions
479 collect
480 (vector name
481 `(progn
482 (replace-match
483 (mapconcat 'identity
484 (sort (if (member ',name ',options)
485 (delete ',name ',options)
486 (cons ',name ',options))
487 'string-lessp)
488 " ")
489 nil nil nil 1)
490 (when (functionp ',function) (funcall ',function)))
491 :style 'toggle
492 :selected (and (member name options) t)))))
494 (defun org-mouse-clip-text (text maxlength)
495 (if (> (length text) maxlength)
496 (concat (substring text 0 (- maxlength 3)) "...")
497 text))
499 (defun org-mouse-popup-global-menu ()
500 (popup-menu
501 `("Main Menu"
502 ["Show Overview" org-mouse-show-overview t]
503 ["Show Headlines" org-mouse-show-headlines t]
504 ["Show All" show-all t]
505 ["Remove Highlights" org-remove-occur-highlights
506 :visible org-occur-highlights]
507 "--"
508 ["Check Deadlines"
509 (if (functionp 'org-check-deadlines-and-todos)
510 (org-check-deadlines-and-todos org-deadline-warning-days)
511 (org-check-deadlines org-deadline-warning-days)) t]
512 ["Check TODOs" org-show-todo-tree t]
513 ("Check Tags"
514 ,@(org-mouse-keyword-menu
515 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
516 #'(lambda (tag) (org-tags-sparse-tree nil tag)))
517 "--"
518 ["Custom Tag ..." org-tags-sparse-tree t])
519 ["Check Phrase ..." org-occur]
520 "--"
521 ["Display Agenda" org-agenda-list t]
522 ["Display Timeline" org-timeline t]
523 ["Display TODO List" org-todo-list t]
524 ("Display Tags"
525 ,@(org-mouse-keyword-menu
526 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
527 #'(lambda (tag) (org-tags-view nil tag)))
528 "--"
529 ["Custom Tag ..." org-tags-view t])
530 ["Display Calendar" org-goto-calendar t]
531 "--"
532 ,@(org-mouse-keyword-menu
533 (mapcar 'car org-agenda-custom-commands)
534 #'(lambda (key)
535 (eval `(flet ((read-char-exclusive () (string-to-char ,key)))
536 (org-agenda nil))))
538 #'(lambda (key)
539 (let ((entry (assoc key org-agenda-custom-commands)))
540 (org-mouse-clip-text
541 (cond
542 ((stringp (nth 1 entry)) (nth 1 entry))
543 ((stringp (nth 2 entry))
544 (concat (org-mouse-agenda-type (nth 1 entry))
545 (nth 2 entry)))
546 (t "Agenda Command '%s'"))
547 30))))
548 "--"
549 ["Delete Blank Lines" delete-blank-lines
550 :visible (org-mouse-empty-line)]
551 ["Insert Checkbox" org-mouse-insert-checkbox
552 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
553 ["Insert Checkboxes"
554 (org-mouse-for-each-item 'org-mouse-insert-checkbox)
555 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
556 ["Plain List to Outline" org-mouse-transform-to-outline
557 :visible (org-at-item-p)])))
559 (defun org-mouse-get-context (contextlist context)
560 (let ((contextdata (assq context contextlist)))
561 (when contextdata
562 (save-excursion
563 (goto-char (second contextdata))
564 (re-search-forward ".*" (third contextdata))))))
566 (defun org-mouse-for-each-item (funct)
567 ;; Functions called by `org-apply-on-list' need an argument
568 (let ((wrap-fun (lambda (c) (funcall funct))))
569 (when (ignore-errors (goto-char (org-in-item-p)))
570 (save-excursion (org-apply-on-list wrap-fun nil)))))
572 (defun org-mouse-bolp ()
573 "Return true if there only spaces, tabs, and '*' before point.
574 This means, between the beginning of line and the point."
575 (save-excursion
576 (skip-chars-backward " \t*") (bolp)))
578 (defun org-mouse-insert-item (text)
579 (case (org-mouse-line-position)
580 (:beginning ; insert before
581 (beginning-of-line)
582 (looking-at "[ \t]*")
583 (open-line 1)
584 (org-indent-to-column (- (match-end 0) (match-beginning 0)))
585 (insert "+ "))
586 (:middle ; insert after
587 (end-of-line)
588 (newline t)
589 (indent-relative)
590 (insert "+ "))
591 (:end ; insert text here
592 (skip-chars-backward " \t")
593 (kill-region (point) (point-at-eol))
594 (unless (org-looking-back org-mouse-punctuation)
595 (insert (concat org-mouse-punctuation " ")))))
596 (insert text)
597 (beginning-of-line))
599 (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate)
600 (if (eq major-mode 'org-mode)
601 (org-mouse-insert-item text)
602 ad-do-it))
604 (defadvice dnd-open-file (around org-mouse-dnd-open-file activate)
605 (if (eq major-mode 'org-mode)
606 (org-mouse-insert-item uri)
607 ad-do-it))
609 (defun org-mouse-match-closure (function)
610 (let ((match (match-data t)))
611 `(lambda (&rest rest)
612 (save-match-data
613 (set-match-data ',match)
614 (apply ',function rest)))))
616 (defun org-mouse-yank-link (click)
617 (interactive "e")
618 ;; Give temporary modes such as isearch a chance to turn off.
619 (run-hooks 'mouse-leave-buffer-hook)
620 (mouse-set-point click)
621 (setq mouse-selection-click-count 0)
622 (delete-horizontal-space)
623 (insert-for-yank (concat " [[" (current-kill 0) "]] ")))
625 (defun org-mouse-context-menu (&optional event)
626 (let ((stamp-prefixes (list org-deadline-string org-scheduled-string))
627 (contextlist (org-context)))
628 (flet ((get-context (context) (org-mouse-get-context contextlist context)))
629 (cond
630 ((org-mouse-mark-active)
631 (let ((region-string (buffer-substring (region-beginning) (region-end))))
632 (popup-menu
633 `(nil
634 ["Sparse Tree" (org-occur ',region-string)]
635 ["Find in Buffer" (occur ',region-string)]
636 ["Grep in Current Dir"
637 (grep (format "grep -rnH -e '%s' *" ',region-string))]
638 ["Grep in Parent Dir"
639 (grep (format "grep -rnH -e '%s' ../*" ',region-string))]
640 "--"
641 ["Convert to Link"
642 (progn (save-excursion (goto-char (region-beginning)) (insert "[["))
643 (save-excursion (goto-char (region-end)) (insert "]]")))]
644 ["Insert Link Here" (org-mouse-yank-link ',event)]))))
645 ((save-excursion (beginning-of-line) (looking-at "#\\+STARTUP: \\(.*\\)"))
646 (popup-menu
647 `(nil
648 ,@(org-mouse-list-options-menu (mapcar 'car org-startup-options)
649 'org-mode-restart))))
650 ((or (eolp)
651 (and (looking-at "\\( \\|\t\\)\\(+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$")
652 (org-looking-back " \\|\t")))
653 (org-mouse-popup-global-menu))
654 ((get-context :checkbox)
655 (popup-menu
656 '(nil
657 ["Toggle" org-toggle-checkbox t]
658 ["Remove" org-mouse-remove-match-and-spaces t]
660 ["All Clear" (org-mouse-for-each-item
661 (lambda ()
662 (when (save-excursion (org-at-item-checkbox-p))
663 (replace-match "[ ]"))))]
664 ["All Set" (org-mouse-for-each-item
665 (lambda ()
666 (when (save-excursion (org-at-item-checkbox-p))
667 (replace-match "[X]"))))]
668 ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t]
669 ["All Remove" (org-mouse-for-each-item
670 (lambda ()
671 (when (save-excursion (org-at-item-checkbox-p))
672 (org-mouse-remove-match-and-spaces))))]
674 ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_")
675 (member (match-string 0) org-todo-keywords-1))
676 (popup-menu
677 `(nil
678 ,@(org-mouse-todo-menu (match-string 0))
679 "--"
680 ["Check TODOs" org-show-todo-tree t]
681 ["List all TODO keywords" org-todo-list t]
682 [,(format "List only %s" (match-string 0))
683 (org-todo-list (match-string 0)) t]
685 ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z")
686 (member (match-string 0) stamp-prefixes))
687 (popup-menu
688 `(nil
689 ,@(org-mouse-keyword-replace-menu stamp-prefixes)
690 "--"
691 ["Check Deadlines" org-check-deadlines t]
693 ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority
694 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
695 (org-mouse-priority-list) 1 "Priority %s" t))))
696 ((get-context :link)
697 (popup-menu
698 '(nil
699 ["Open" org-open-at-point t]
700 ["Open in Emacs" (org-open-at-point t) t]
701 "--"
702 ["Copy link" (org-kill-new (match-string 0))]
703 ["Cut link"
704 (progn
705 (kill-region (match-beginning 0) (match-end 0))
706 (just-one-space))]
707 "--"
708 ["Grep for TODOs"
709 (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))]
710 ; ["Paste file link" ((insert "file:") (yank))]
712 ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags
713 (popup-menu
714 `(nil
715 [,(format "Display '%s'" (match-string 1))
716 (org-tags-view nil ,(match-string 1))]
717 [,(format "Sparse Tree '%s'" (match-string 1))
718 (org-tags-sparse-tree nil ,(match-string 1))]
719 "--"
720 ,@(org-mouse-tag-menu))))
721 ((org-at-timestamp-p)
722 (popup-menu
723 '(nil
724 ["Show Day" org-open-at-point t]
725 ["Change Timestamp" org-time-stamp t]
726 ["Delete Timestamp" (org-mouse-delete-timestamp) t]
727 ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)]
728 "--"
729 ["Set for Today" org-mouse-timestamp-today]
730 ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)]
731 ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)]
732 ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)]
733 ["Set in a Month" (org-mouse-timestamp-today 1 'month)]
734 "--"
735 ["+ 1 Day" (org-timestamp-change 1 'day)]
736 ["+ 1 Week" (org-timestamp-change 7 'day)]
737 ["+ 1 Month" (org-timestamp-change 1 'month)]
738 "--"
739 ["- 1 Day" (org-timestamp-change -1 'day)]
740 ["- 1 Week" (org-timestamp-change -7 'day)]
741 ["- 1 Month" (org-timestamp-change -1 'month)])))
742 ((get-context :table-special)
743 (let ((mdata (match-data)))
744 (incf (car mdata) 2)
745 (store-match-data mdata))
746 (message "match: %S" (match-string 0))
747 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
748 '(" " "!" "^" "_" "$" "#" "*" "'") 0
749 (lambda (mark)
750 (case (string-to-char mark)
751 (? "( ) Nothing Special")
752 (?! "(!) Column Names")
753 (?^ "(^) Field Names Above")
754 (?_ "(^) Field Names Below")
755 (?$ "($) Formula Parameters")
756 (?# "(#) Recalculation: Auto")
757 (?* "(*) Recalculation: Manual")
758 (?' "(') Recalculation: None"))) t))))
759 ((assq :table contextlist)
760 (popup-menu
761 '(nil
762 ["Align Table" org-ctrl-c-ctrl-c]
763 ["Blank Field" org-table-blank-field]
764 ["Edit Field" org-table-edit-field]
765 "--"
766 ("Column"
767 ["Move Column Left" org-metaleft]
768 ["Move Column Right" org-metaright]
769 ["Delete Column" org-shiftmetaleft]
770 ["Insert Column" org-shiftmetaright]
771 "--"
772 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle])
773 ("Row"
774 ["Move Row Up" org-metaup]
775 ["Move Row Down" org-metadown]
776 ["Delete Row" org-shiftmetaup]
777 ["Insert Row" org-shiftmetadown]
778 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
779 "--"
780 ["Insert Hline" org-table-insert-hline])
781 ("Rectangle"
782 ["Copy Rectangle" org-copy-special]
783 ["Cut Rectangle" org-cut-special]
784 ["Paste Rectangle" org-paste-special]
785 ["Fill Rectangle" org-table-wrap-region])
786 "--"
787 ["Set Column Formula" org-table-eval-formula]
788 ["Set Field Formula" (org-table-eval-formula '(4))]
789 ["Edit Formulas" org-table-edit-formulas]
790 "--"
791 ["Recalculate Line" org-table-recalculate]
792 ["Recalculate All" (org-table-recalculate '(4))]
793 ["Iterate All" (org-table-recalculate '(16))]
794 "--"
795 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks]
796 ["Sum Column/Rectangle" org-table-sum
797 :active (or (org-at-table-p) (org-region-active-p))]
798 ["Field Info" org-table-field-info]
799 ["Debug Formulas"
800 (setq org-table-formula-debug (not org-table-formula-debug))
801 :style toggle :selected org-table-formula-debug]
803 ((and (assq :headline contextlist) (not (eolp)))
804 (let ((priority (org-mouse-get-priority t)))
805 (popup-menu
806 `("Headline Menu"
807 ("Tags and Priorities"
808 ,@(org-mouse-keyword-menu
809 (org-mouse-priority-list)
810 #'(lambda (keyword)
811 (org-mouse-set-priority (string-to-char keyword)))
812 priority "Priority %s")
813 "--"
814 ,@(org-mouse-tag-menu))
815 ("TODO Status"
816 ,@(org-mouse-todo-menu (org-get-todo-state)))
817 ["Show Tags"
818 (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags))
819 :visible (not org-mouse-direct)]
820 ["Show Priority"
821 (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority))
822 :visible (not org-mouse-direct)]
823 ,@(if org-mouse-direct '("--") nil)
824 ["New Heading" org-mouse-insert-heading :visible org-mouse-direct]
825 ["Set Deadline"
826 (progn (org-mouse-end-headline) (insert " ") (org-deadline))
827 :active (not (save-excursion
828 (org-mouse-re-search-line org-deadline-regexp)))]
829 ["Schedule Task"
830 (progn (org-mouse-end-headline) (insert " ") (org-schedule))
831 :active (not (save-excursion
832 (org-mouse-re-search-line org-scheduled-regexp)))]
833 ["Insert Timestamp"
834 (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t]
835 ; ["Timestamp (inactive)" org-time-stamp-inactive t]
836 "--"
837 ["Archive Subtree" org-archive-subtree]
838 ["Cut Subtree" org-cut-special]
839 ["Copy Subtree" org-copy-special]
840 ["Paste Subtree" org-paste-special :visible org-mouse-direct]
841 ("Sort Children"
842 ["Alphabetically" (org-sort-entries nil ?a)]
843 ["Numerically" (org-sort-entries nil ?n)]
844 ["By Time/Date" (org-sort-entries nil ?t)]
845 "--"
846 ["Reverse Alphabetically" (org-sort-entries nil ?A)]
847 ["Reverse Numerically" (org-sort-entries nil ?N)]
848 ["Reverse By Time/Date" (org-sort-entries nil ?T)])
849 "--"
850 ["Move Trees" org-mouse-move-tree :active nil]
851 ))))
853 (org-mouse-popup-global-menu))))))
855 (defun org-mouse-mark-active ()
856 (and mark-active transient-mark-mode))
858 (defun org-mouse-in-region-p (pos)
859 (and (org-mouse-mark-active)
860 (>= pos (region-beginning))
861 (< pos (region-end))))
863 (defun org-mouse-down-mouse (event)
864 (interactive "e")
865 (setq this-command last-command)
866 (unless (and (= 1 (event-click-count event))
867 (org-mouse-in-region-p (posn-point (event-start event))))
868 (mouse-drag-region event)))
870 (add-hook 'org-mode-hook
871 #'(lambda ()
872 (setq org-mouse-context-menu-function 'org-mouse-context-menu)
874 (when (memq 'context-menu org-mouse-features)
875 (org-defkey org-mouse-map [mouse-3] nil)
876 (org-defkey org-mode-map [mouse-3] 'org-mouse-show-context-menu))
877 (org-defkey org-mode-map [down-mouse-1] 'org-mouse-down-mouse)
878 (when (memq 'context-menu org-mouse-features)
879 (org-defkey org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree)
880 (org-defkey org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start))
881 (when (memq 'yank-link org-mouse-features)
882 (org-defkey org-mode-map [S-mouse-2] 'org-mouse-yank-link)
883 (org-defkey org-mode-map [drag-mouse-3] 'org-mouse-yank-link))
884 (when (memq 'move-tree org-mouse-features)
885 (org-defkey org-mouse-map [drag-mouse-3] 'org-mouse-move-tree)
886 (org-defkey org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start))
888 (when (memq 'activate-stars org-mouse-features)
889 (font-lock-add-keywords
891 `((,org-outline-regexp
892 0 `(face org-link mouse-face highlight keymap ,org-mouse-map)
893 'prepend))
896 (when (memq 'activate-bullets org-mouse-features)
897 (font-lock-add-keywords
899 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +"
900 (1 `(face org-link keymap ,org-mouse-map mouse-face highlight)
901 'prepend)))
904 (when (memq 'activate-checkboxes org-mouse-features)
905 (font-lock-add-keywords
907 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
908 (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t)))
911 (defadvice org-open-at-point (around org-mouse-open-at-point activate)
912 (let ((context (org-context)))
913 (cond
914 ((assq :headline-stars context) (org-cycle))
915 ((assq :checkbox context) (org-toggle-checkbox))
916 ((assq :item-bullet context)
917 (let ((org-cycle-include-plain-lists t)) (org-cycle)))
918 ((org-footnote-at-reference-p) nil)
919 (t ad-do-it))))))
921 (defun org-mouse-move-tree-start (event)
922 (interactive "e")
923 (message "Same line: promote/demote, (***):move before, (text): make a child"))
926 (defun org-mouse-make-marker (position)
927 (with-current-buffer (window-buffer (posn-window position))
928 (copy-marker (posn-point position))))
930 (defun org-mouse-move-tree (event)
931 ;; todo: handle movements between different buffers
932 (interactive "e")
933 (save-excursion
934 (let* ((start (org-mouse-make-marker (event-start event)))
935 (end (org-mouse-make-marker (event-end event)))
936 (sbuf (marker-buffer start))
937 (ebuf (marker-buffer end)))
939 (when (and sbuf ebuf)
940 (set-buffer sbuf)
941 (goto-char start)
942 (org-back-to-heading)
943 (if (and (eq sbuf ebuf)
944 (equal
945 (point)
946 (save-excursion (goto-char end) (org-back-to-heading) (point))))
947 ;; if the same line then promote/demote
948 (if (>= end start) (org-demote-subtree) (org-promote-subtree))
949 ;; if different lines then move
950 (org-cut-subtree)
952 (set-buffer ebuf)
953 (goto-char end)
954 (org-back-to-heading)
955 (when (and (eq sbuf ebuf)
956 (equal
957 (point)
958 (save-excursion (goto-char start)
959 (org-back-to-heading) (point))))
960 (outline-end-of-subtree)
961 (end-of-line)
962 (if (eobp) (newline) (forward-char)))
964 (when (looking-at org-outline-regexp)
965 (let ((level (- (match-end 0) (match-beginning 0))))
966 (when (> end (match-end 0))
967 (outline-end-of-subtree)
968 (end-of-line)
969 (if (eobp) (newline) (forward-char))
970 (setq level (1+ level)))
971 (org-paste-subtree level)
972 (save-excursion
973 (outline-end-of-subtree)
974 (when (bolp) (delete-char -1))))))))))
977 (defun org-mouse-transform-to-outline ()
978 (interactive)
979 (org-back-to-heading)
980 (let ((minlevel 1000)
981 (replace-text (concat (match-string 0) "* ")))
982 (beginning-of-line 2)
983 (save-excursion
984 (while (not (or (eobp) (looking-at org-outline-regexp)))
985 (when (looking-at org-mouse-plain-list-regexp)
986 (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1)))))
987 (forward-line)))
988 (while (not (or (eobp) (looking-at org-outline-regexp)))
989 (when (and (looking-at org-mouse-plain-list-regexp)
990 (eq minlevel (- (match-end 1) (match-beginning 1))))
991 (replace-match replace-text))
992 (forward-line))))
994 (defvar org-mouse-cmd) ;dynamically scoped from `org-with-remote-undo'.
996 (defun org-mouse-do-remotely (command)
997 ; (org-agenda-check-no-diary)
998 (when (get-text-property (point) 'org-marker)
999 (let* ((anticol (- (point-at-eol) (point)))
1000 (marker (get-text-property (point) 'org-marker))
1001 (buffer (marker-buffer marker))
1002 (pos (marker-position marker))
1003 (hdmarker (get-text-property (point) 'org-hd-marker))
1004 (buffer-read-only nil)
1005 (newhead "--- removed ---")
1006 (org-mouse-direct nil)
1007 (org-mouse-main-buffer (current-buffer)))
1008 (when (eq (with-current-buffer buffer major-mode) 'org-mode)
1009 (let ((endmarker (with-current-buffer buffer
1010 (outline-end-of-subtree)
1011 (forward-char 1)
1012 (copy-marker (point)))))
1013 (org-with-remote-undo buffer
1014 (with-current-buffer buffer
1015 (widen)
1016 (goto-char pos)
1017 (org-show-hidden-entry)
1018 (save-excursion
1019 (and (outline-next-heading)
1020 (org-flag-heading nil))) ; show the next heading
1021 (org-back-to-heading)
1022 (setq marker (copy-marker (point)))
1023 (goto-char (max (point-at-bol) (- (point-at-eol) anticol)))
1024 (funcall command)
1025 (message "_cmd: %S" org-mouse-cmd)
1026 (message "this-command: %S" this-command)
1027 (unless (eq (marker-position marker) (marker-position endmarker))
1028 (setq newhead (org-get-heading))))
1030 (beginning-of-line 1)
1031 (save-excursion
1032 (org-agenda-change-all-lines newhead hdmarker 'fixface))))
1033 t))))
1035 (defun org-mouse-agenda-context-menu (&optional event)
1036 (or (org-mouse-do-remotely 'org-mouse-context-menu)
1037 (popup-menu
1038 '("Agenda"
1039 ("Agenda Files")
1040 "--"
1041 ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo))
1042 :visible (if (eq last-command 'org-agenda-undo)
1043 org-agenda-pending-undo-list
1044 org-agenda-undo-list)]
1045 ["Rebuild Buffer" org-agenda-redo t]
1046 ["New Diary Entry"
1047 org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline) t]
1048 "--"
1049 ["Goto Today" org-agenda-goto-today
1050 (org-agenda-check-type nil 'agenda 'timeline) t]
1051 ["Display Calendar" org-agenda-goto-calendar
1052 (org-agenda-check-type nil 'agenda 'timeline) t]
1053 ("Calendar Commands"
1054 ["Phases of the Moon" org-agenda-phases-of-moon
1055 (org-agenda-check-type nil 'agenda 'timeline)]
1056 ["Sunrise/Sunset" org-agenda-sunrise-sunset
1057 (org-agenda-check-type nil 'agenda 'timeline)]
1058 ["Holidays" org-agenda-holidays
1059 (org-agenda-check-type nil 'agenda 'timeline)]
1060 ["Convert" org-agenda-convert-date
1061 (org-agenda-check-type nil 'agenda 'timeline)]
1062 "--"
1063 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
1064 "--"
1065 ["Day View" org-agenda-day-view
1066 :active (org-agenda-check-type nil 'agenda)
1067 :style radio :selected (eq org-agenda-current-span 'day)]
1068 ["Week View" org-agenda-week-view
1069 :active (org-agenda-check-type nil 'agenda)
1070 :style radio :selected (eq org-agenda-current-span 'week)]
1071 "--"
1072 ["Show Logbook entries" org-agenda-log-mode
1073 :style toggle :selected org-agenda-show-log
1074 :active (org-agenda-check-type nil 'agenda 'timeline)]
1075 ["Include Diary" org-agenda-toggle-diary
1076 :style toggle :selected org-agenda-include-diary
1077 :active (org-agenda-check-type nil 'agenda)]
1078 ["Use Time Grid" org-agenda-toggle-time-grid
1079 :style toggle :selected org-agenda-use-time-grid
1080 :active (org-agenda-check-type nil 'agenda)]
1081 ["Follow Mode" org-agenda-follow-mode
1082 :style toggle :selected org-agenda-follow-mode]
1083 "--"
1084 ["Quit" org-agenda-quit t]
1085 ["Exit and Release Buffers" org-agenda-exit t]
1086 ))))
1088 (defun org-mouse-get-gesture (event)
1089 (let ((startxy (posn-x-y (event-start event)))
1090 (endxy (posn-x-y (event-end event))))
1091 (if (< (car startxy) (car endxy)) :right :left)))
1094 ; (setq org-agenda-mode-hook nil)
1095 (defvar org-agenda-mode-map)
1096 (add-hook 'org-agenda-mode-hook
1097 #'(lambda ()
1098 (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu)
1099 (org-defkey org-agenda-mode-map [mouse-3] 'org-mouse-show-context-menu)
1100 (org-defkey org-agenda-mode-map [down-mouse-3] 'org-mouse-move-tree-start)
1101 (org-defkey org-agenda-mode-map [C-mouse-4] 'org-agenda-earlier)
1102 (org-defkey org-agenda-mode-map [C-mouse-5] 'org-agenda-later)
1103 (org-defkey org-agenda-mode-map [drag-mouse-3]
1104 #'(lambda (event) (interactive "e")
1105 (case (org-mouse-get-gesture event)
1106 (:left (org-agenda-earlier 1))
1107 (:right (org-agenda-later 1)))))))
1109 (provide 'org-mouse)
1111 ;;; org-mouse.el ends here