updated babel key binding documentation in manual
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-mouse.el
blob3a667deee9e87e95f9f4c5bd85c56603079112c1
1 ;;; org-mouse.el --- Better mouse support for org-mode
3 ;; Copyright (C) 2006-2011 Free Software Foundation
4 ;;
5 ;; Author: Piotr Zielinski <piotr dot zielinski at gmail dot com>
6 ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
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;; + Version number synchronization with Org-mode.
75 ;; Version 0.25
76 ;; + made compatible with org-mode 4.70 (thanks to Carsten for the patch)
78 ;; Version 0.24
79 ;; + minor changes to the table menu
81 ;; Version 0.23
82 ;; + preliminary support for tables and calculation marks
83 ;; + context menu support for org-agenda-undo & org-sort-entries
85 ;; Version 0.22
86 ;; + handles undo support for the agenda buffer (requires org-mode >=4.58)
88 ;; Version 0.21
89 ;; + selected text activates its context menu
90 ;; + shift-middleclick or right-drag inserts the text from the clipboard in the form of a link
92 ;; Version 0.20
93 ;; + the new "TODO Status" submenu replaces the "Cycle TODO" menu item
94 ;; + the TODO menu can now list occurrences of a specific TODO keyword
95 ;; + #+STARTUP line is now recognized
97 ;; Version 0.19
98 ;; + added support for dragging URLs to the org-buffer
100 ;; Version 0.18
101 ;; + added support for agenda blocks
103 ;; Version 0.17
104 ;; + toggle checkboxes with a single click
106 ;; Version 0.16
107 ;; + added support for checkboxes
109 ;; Version 0.15
110 ;; + org-mode now works with the Agenda buffer as well
112 ;; Version 0.14
113 ;; + added a menu option that converts plain list items to outline items
115 ;; Version 0.13
116 ;; + "Insert Heading" now inserts a sibling heading if the point is
117 ;; on "***" and a child heading otherwise
119 ;; Version 0.12
120 ;; + compatible with Emacs 21
121 ;; + custom agenda commands added to the main menu
122 ;; + moving trees should now work between windows in the same frame
124 ;; Version 0.11
125 ;; + fixed org-mouse-at-link (thanks to Carsten)
126 ;; + removed [follow-link] bindings
128 ;; Version 0.10
129 ;; + added a menu option to remove highlights
130 ;; + compatible with org-mode 4.21 now
132 ;; Version 0.08:
133 ;; + trees can be moved/promoted/demoted by dragging with the right
134 ;; mouse button (mouse-3)
135 ;; + small changes in the above function
137 ;; Versions 0.01 -- 0.07: (I don't remember)
139 ;;; Code:
141 (eval-when-compile (require 'cl))
142 (require 'org)
144 (defvar org-agenda-allow-remote-undo)
145 (defvar org-agenda-undo-list)
146 (defvar org-agenda-custom-commands)
147 (declare-function org-agenda-change-all-lines "org-agenda"
148 (newhead hdmarker &optional fixface just-this))
149 (declare-function org-verify-change-for-undo "org-agenda" (l1 l2))
150 (declare-function org-apply-on-list "org-list" (function init-value &rest args))
151 (declare-function org-agenda-earlier "org-agenda" (arg))
152 (declare-function org-agenda-later "org-agenda" (arg))
154 (defvar org-mouse-plain-list-regexp "\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
155 "Regular expression that matches a plain list.")
156 (defvar org-mouse-direct t
157 "Internal variable indicating whether the current action is direct.
159 If t, then the current action has been invoked directly through the buffer
160 it is intended to operate on. If nil, then the action has been invoked
161 indirectly, for example, through the agenda buffer.")
163 (defgroup org-mouse nil
164 "Mouse support for org-mode."
165 :tag "Org Mouse"
166 :group 'org)
168 (defcustom org-mouse-punctuation ":"
169 "Punctuation used when inserting text by drag and drop."
170 :group 'org-mouse
171 :type 'string)
173 (defcustom org-mouse-features
174 '(context-menu yank-link activate-stars activate-bullets activate-checkboxes)
175 "The features of org-mouse that should be activated.
176 Changing this variable requires a restart of Emacs to get activated."
177 :group 'org-mouse
178 :type '(set :greedy t
179 (const :tag "Mouse-3 shows context menu" context-menu)
180 (const :tag "C-mouse-1 and mouse-3 move trees" move-tree)
181 (const :tag "S-mouse-2 and drag-mouse-3 yank link" yank-link)
182 (const :tag "Activate headline stars" activate-stars)
183 (const :tag "Activate item bullets" activate-bullets)
184 (const :tag "Activate checkboxes" activate-checkboxes)))
186 (defun org-mouse-re-search-line (regexp)
187 "Search the current line for a given regular expression."
188 (beginning-of-line)
189 (re-search-forward regexp (point-at-eol) t))
191 (defun org-mouse-end-headline ()
192 "Go to the end of current headline (ignoring tags)."
193 (interactive)
194 (end-of-line)
195 (skip-chars-backward "\t ")
196 (when (org-looking-back ":[A-Za-z]+:")
197 (skip-chars-backward ":A-Za-z")
198 (skip-chars-backward "\t ")))
200 (defvar org-mouse-context-menu-function nil
201 "Function to create the context menu.
202 The value of this variable is the function invoked by
203 `org-mouse-context-menu' as the context menu.")
204 (make-variable-buffer-local 'org-mouse-context-menu-function)
206 (defun org-mouse-show-context-menu (event prefix)
207 "Invoke the context menu.
209 If the value of `org-mouse-context-menu-function' is a function, then
210 this function is called. Otherwise, the current major mode menu is used."
211 (interactive "@e \nP")
212 (if (and (= (event-click-count event) 1)
213 (or (not mark-active)
214 (sit-for (/ double-click-time 1000.0))))
215 (progn
216 (select-window (posn-window (event-start event)))
217 (when (not (org-mouse-mark-active))
218 (goto-char (posn-point (event-start event)))
219 (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook)))
220 (let ((redisplay-dont-pause t))
221 (sit-for 0)))
222 (if (functionp org-mouse-context-menu-function)
223 (funcall org-mouse-context-menu-function event)
224 (if (fboundp 'mouse-menu-major-mode-map)
225 (popup-menu (mouse-menu-major-mode-map) event prefix)
226 (org-no-warnings ; don't warn about fallback, obsolete since 23.1
227 (mouse-major-mode-menu event prefix)))))
228 (setq this-command 'mouse-save-then-kill)
229 (mouse-save-then-kill event)))
231 (defun org-mouse-line-position ()
232 "Return `:beginning' or `:middle' or `:end', depending on the point position.
234 If the point is at the end of the line, return `:end'.
235 If the point is separated from the beginning of the line only by white
236 space and *'s (`org-mouse-bolp'), return `:beginning'. Otherwise,
237 return `:middle'."
238 (cond
239 ((eolp) :end)
240 ((org-mouse-bolp) :beginning)
241 (t :middle)))
243 (defun org-mouse-empty-line ()
244 "Return non-nil iff the line contains only white space."
245 (save-excursion (beginning-of-line) (looking-at "[ \t]*$")))
247 (defun org-mouse-next-heading ()
248 "Go to the next heading.
249 If there is none, ensure that the point is at the beginning of an empty line."
250 (unless (outline-next-heading)
251 (beginning-of-line)
252 (unless (org-mouse-empty-line)
253 (end-of-line)
254 (newline))))
256 (defun org-mouse-insert-heading ()
257 "Insert a new heading, as `org-insert-heading'.
259 If the point is at the :beginning (`org-mouse-line-position') of the line,
260 insert the new heading before the current line. Otherwise, insert it
261 after the current heading."
262 (interactive)
263 (case (org-mouse-line-position)
264 (:beginning (beginning-of-line)
265 (org-insert-heading))
266 (t (org-mouse-next-heading)
267 (org-insert-heading))))
269 (defun org-mouse-timestamp-today (&optional shift units)
270 "Change the timestamp into SHIFT UNITS in the future.
272 For the acceptable UNITS, see `org-timestamp-change'."
273 (interactive)
274 (flet ((org-read-date (&rest rest) (current-time)))
275 (org-time-stamp nil))
276 (when shift
277 (org-timestamp-change shift units)))
279 (defun org-mouse-keyword-menu (keywords function &optional selected itemformat)
280 "A helper function.
282 Returns a menu fragment consisting of KEYWORDS. When a keyword
283 is selected by the user, FUNCTION is called with the selected
284 keyword as the only argument.
286 If SELECTED is nil, then all items are normal menu items. If
287 SELECTED is a function, then each item is a checkbox, which is
288 enabled for a given keyword iff (funcall SELECTED keyword) return
289 non-nil. If SELECTED is neither nil nor a function, then the
290 items are radio buttons. A radio button is enabled for the
291 keyword `equal' to SELECTED.
293 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
294 is a function, it is invoked with the keyword as the only
295 argument. If it is a string, it is interpreted as the format
296 string to (format ITEMFORMAT keyword). If it is neither a string
297 nor a function, elements of KEYWORDS are used directly."
298 (mapcar
299 `(lambda (keyword)
300 (vector (cond
301 ((functionp ,itemformat) (funcall ,itemformat keyword))
302 ((stringp ,itemformat) (format ,itemformat keyword))
303 (t keyword))
304 (list 'funcall ,function keyword)
305 :style (cond
306 ((null ,selected) t)
307 ((functionp ,selected) 'toggle)
308 (t 'radio))
309 :selected (if (functionp ,selected)
310 (and (funcall ,selected keyword) t)
311 (equal ,selected keyword))))
312 keywords))
314 (defun org-mouse-remove-match-and-spaces ()
315 "Remove the match, make just one space around the point."
316 (interactive)
317 (replace-match "")
318 (just-one-space))
320 (defvar rest)
321 (defun org-mouse-replace-match-and-surround (newtext &optional fixedcase
322 literal string subexp)
323 "The same as `replace-match', but surrounds the replacement with spaces."
324 (apply 'replace-match rest)
325 (save-excursion
326 (goto-char (match-beginning (or subexp 0)))
327 (just-one-space)
328 (goto-char (match-end (or subexp 0)))
329 (just-one-space)))
331 (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat
332 nosurround)
333 "A helper function.
335 Returns a menu fragment consisting of KEYWORDS. When a keyword
336 is selected, group GROUP of the current match is replaced by the
337 keyword. The method ensures that both ends of the replacement
338 are separated from the rest of the text in the buffer by
339 individual spaces (unless NOSURROUND is non-nil).
341 The final entry of the menu is always \"None\", which removes the
342 match.
344 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
345 is a function, it is invoked with the keyword as the only
346 argument. If it is a string, it is interpreted as the format
347 string to (format ITEMFORMAT keyword). If it is neither a string
348 nor a function, elements of KEYWORDS are used directly."
349 (setq group (or group 0))
350 (let ((replace (org-mouse-match-closure
351 (if nosurround 'replace-match
352 'org-mouse-replace-match-and-surround))))
353 (append
354 (org-mouse-keyword-menu
355 keywords
356 `(lambda (keyword) (funcall ,replace keyword t t nil ,group))
357 (match-string group)
358 itemformat)
359 `(["None" org-mouse-remove-match-and-spaces
360 :style radio
361 :selected ,(not (member (match-string group) keywords))]))))
363 (defun org-mouse-show-headlines ()
364 "Change the visibility of the current org buffer to only show headlines."
365 (interactive)
366 (let ((this-command 'org-cycle)
367 (last-command 'org-cycle)
368 (org-cycle-global-status nil))
369 (org-cycle '(4))
370 (org-cycle '(4))))
372 (defun org-mouse-show-overview ()
373 "Change visibility of current org buffer to first-level headlines only."
374 (interactive)
375 (let ((org-cycle-global-status nil))
376 (org-cycle '(4))))
378 (defun org-mouse-set-priority (priority)
379 "Set the priority of the current headline to PRIORITY."
380 (flet ((read-char-exclusive () priority))
381 (org-priority)))
383 (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]"
384 "Regular expression matching the priority indicator.
385 Differs from `org-priority-regexp' in that it doesn't contain the
386 leading '.*?'.")
388 (defun org-mouse-get-priority (&optional default)
389 "Return the priority of the current headline.
390 DEFAULT is returned if no priority is given in the headline."
391 (save-excursion
392 (if (org-mouse-re-search-line org-mouse-priority-regexp)
393 (match-string 1)
394 (when default (char-to-string org-default-priority)))))
396 (defun org-mouse-delete-timestamp ()
397 "Deletes the current timestamp as well as the preceding keyword.
398 SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:"
399 (when (or (org-at-date-range-p) (org-at-timestamp-p))
400 (replace-match "") ; delete the timestamp
401 (skip-chars-backward " :A-Z")
402 (when (looking-at " *[A-Z][A-Z]+:")
403 (replace-match ""))))
405 (defun org-mouse-looking-at (regexp skipchars &optional movechars)
406 (save-excursion
407 (let ((point (point)))
408 (if (looking-at regexp) t
409 (skip-chars-backward skipchars)
410 (forward-char (or movechars 0))
411 (when (looking-at regexp)
412 (> (match-end 0) point))))))
414 (defun org-mouse-priority-list ()
415 (loop for priority from ?A to org-lowest-priority
416 collect (char-to-string priority)))
418 (defun org-mouse-todo-menu (state)
419 "Create the menu with TODO keywords."
420 (append
421 (let ((kwds org-todo-keywords-1))
422 (org-mouse-keyword-menu
423 kwds
424 `(lambda (kwd) (org-todo kwd))
425 (lambda (kwd) (equal state kwd))))))
427 (defun org-mouse-tag-menu () ;todo
428 "Create the tags menu."
429 (append
430 (let ((tags (org-get-tags)))
431 (org-mouse-keyword-menu
432 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
433 `(lambda (tag)
434 (org-mouse-set-tags
435 (sort (if (member tag (quote ,tags))
436 (delete tag (quote ,tags))
437 (cons tag (quote ,tags)))
438 'string-lessp)))
439 `(lambda (tag) (member tag (quote ,tags)))
441 '("--"
442 ["Align Tags Here" (org-set-tags nil t) t]
443 ["Align Tags in Buffer" (org-set-tags t t) t]
444 ["Set Tags ..." (org-set-tags) t])))
446 (defun org-mouse-set-tags (tags)
447 (save-excursion
448 ;; remove existing tags first
449 (beginning-of-line)
450 (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)")
451 (replace-match ""))
453 ;; set new tags if any
454 (when tags
455 (end-of-line)
456 (insert " :" (mapconcat 'identity tags ":") ":")
457 (org-set-tags nil t))))
459 (defun org-mouse-insert-checkbox ()
460 (interactive)
461 (and (org-at-item-p)
462 (goto-char (match-end 0))
463 (unless (org-at-item-checkbox-p)
464 (delete-horizontal-space)
465 (insert " [ ] "))))
467 (defun org-mouse-agenda-type (type)
468 (case type
469 ('tags "Tags: ")
470 ('todo "TODO: ")
471 ('tags-tree "Tags tree: ")
472 ('todo-tree "TODO tree: ")
473 ('occur-tree "Occur tree: ")
474 (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)])))
561 (defun org-mouse-get-context (contextlist context)
562 (let ((contextdata (assq context contextlist)))
563 (when contextdata
564 (save-excursion
565 (goto-char (second contextdata))
566 (re-search-forward ".*" (third contextdata))))))
568 (defun org-mouse-for-each-item (funct)
569 ;; Functions called by `org-apply-on-list' need an argument
570 (let ((wrap-fun (lambda (c) (funcall funct))))
571 (when (ignore-errors (goto-char (org-in-item-p)))
572 (save-excursion (org-apply-on-list wrap-fun nil)))))
574 (defun org-mouse-bolp ()
575 "Return true if there only spaces, tabs, and '*' before point.
576 This means, between the beginning of line and the point."
577 (save-excursion
578 (skip-chars-backward " \t*") (bolp)))
580 (defun org-mouse-insert-item (text)
581 (case (org-mouse-line-position)
582 (:beginning ; insert before
583 (beginning-of-line)
584 (looking-at "[ \t]*")
585 (open-line 1)
586 (org-indent-to-column (- (match-end 0) (match-beginning 0)))
587 (insert "+ "))
588 (:middle ; insert after
589 (end-of-line)
590 (newline t)
591 (indent-relative)
592 (insert "+ "))
593 (:end ; insert text here
594 (skip-chars-backward " \t")
595 (kill-region (point) (point-at-eol))
596 (unless (org-looking-back org-mouse-punctuation)
597 (insert (concat org-mouse-punctuation " ")))))
598 (insert text)
599 (beginning-of-line))
601 (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate)
602 (if (eq major-mode 'org-mode)
603 (org-mouse-insert-item text)
604 ad-do-it))
606 (defadvice dnd-open-file (around org-mouse-dnd-open-file activate)
607 (if (eq major-mode 'org-mode)
608 (org-mouse-insert-item uri)
609 ad-do-it))
611 (defun org-mouse-match-closure (function)
612 (let ((match (match-data t)))
613 `(lambda (&rest rest)
614 (save-match-data
615 (set-match-data ',match)
616 (apply ',function rest)))))
618 (defun org-mouse-yank-link (click)
619 (interactive "e")
620 ;; Give temporary modes such as isearch a chance to turn off.
621 (run-hooks 'mouse-leave-buffer-hook)
622 (mouse-set-point click)
623 (setq mouse-selection-click-count 0)
624 (delete-horizontal-space)
625 (insert-for-yank (concat " [[" (current-kill 0) "]] ")))
627 (defun org-mouse-context-menu (&optional event)
628 (let ((stamp-prefixes (list org-deadline-string org-scheduled-string))
629 (contextlist (org-context)))
630 (flet ((get-context (context) (org-mouse-get-context contextlist context)))
631 (cond
632 ((org-mouse-mark-active)
633 (let ((region-string (buffer-substring (region-beginning) (region-end))))
634 (popup-menu
635 `(nil
636 ["Sparse Tree" (org-occur ',region-string)]
637 ["Find in Buffer" (occur ',region-string)]
638 ["Grep in Current Dir"
639 (grep (format "grep -rnH -e '%s' *" ',region-string))]
640 ["Grep in Parent Dir"
641 (grep (format "grep -rnH -e '%s' ../*" ',region-string))]
642 "--"
643 ["Convert to Link"
644 (progn (save-excursion (goto-char (region-beginning)) (insert "[["))
645 (save-excursion (goto-char (region-end)) (insert "]]")))]
646 ["Insert Link Here" (org-mouse-yank-link ',event)]))))
647 ((save-excursion (beginning-of-line) (looking-at "#\\+STARTUP: \\(.*\\)"))
648 (popup-menu
649 `(nil
650 ,@(org-mouse-list-options-menu (mapcar 'car org-startup-options)
651 'org-mode-restart))))
652 ((or (eolp)
653 (and (looking-at "\\( \\|\t\\)\\(+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$")
654 (org-looking-back " \\|\t")))
655 (org-mouse-popup-global-menu))
656 ((get-context :checkbox)
657 (popup-menu
658 '(nil
659 ["Toggle" org-toggle-checkbox t]
660 ["Remove" org-mouse-remove-match-and-spaces t]
662 ["All Clear" (org-mouse-for-each-item
663 (lambda ()
664 (when (save-excursion (org-at-item-checkbox-p))
665 (replace-match "[ ]"))))]
666 ["All Set" (org-mouse-for-each-item
667 (lambda ()
668 (when (save-excursion (org-at-item-checkbox-p))
669 (replace-match "[X]"))))]
670 ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t]
671 ["All Remove" (org-mouse-for-each-item
672 (lambda ()
673 (when (save-excursion (org-at-item-checkbox-p))
674 (org-mouse-remove-match-and-spaces))))]
676 ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_")
677 (member (match-string 0) org-todo-keywords-1))
678 (popup-menu
679 `(nil
680 ,@(org-mouse-todo-menu (match-string 0))
681 "--"
682 ["Check TODOs" org-show-todo-tree t]
683 ["List all TODO keywords" org-todo-list t]
684 [,(format "List only %s" (match-string 0))
685 (org-todo-list (match-string 0)) t]
687 ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z")
688 (member (match-string 0) stamp-prefixes))
689 (popup-menu
690 `(nil
691 ,@(org-mouse-keyword-replace-menu stamp-prefixes)
692 "--"
693 ["Check Deadlines" org-check-deadlines t]
695 ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority
696 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
697 (org-mouse-priority-list) 1 "Priority %s" t))))
698 ((get-context :link)
699 (popup-menu
700 '(nil
701 ["Open" org-open-at-point t]
702 ["Open in Emacs" (org-open-at-point t) t]
703 "--"
704 ["Copy link" (org-kill-new (match-string 0))]
705 ["Cut link"
706 (progn
707 (kill-region (match-beginning 0) (match-end 0))
708 (just-one-space))]
709 "--"
710 ["Grep for TODOs"
711 (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))]
712 ; ["Paste file link" ((insert "file:") (yank))]
714 ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags
715 (popup-menu
716 `(nil
717 [,(format "Display '%s'" (match-string 1))
718 (org-tags-view nil ,(match-string 1))]
719 [,(format "Sparse Tree '%s'" (match-string 1))
720 (org-tags-sparse-tree nil ,(match-string 1))]
721 "--"
722 ,@(org-mouse-tag-menu))))
723 ((org-at-timestamp-p)
724 (popup-menu
725 '(nil
726 ["Show Day" org-open-at-point t]
727 ["Change Timestamp" org-time-stamp t]
728 ["Delete Timestamp" (org-mouse-delete-timestamp) t]
729 ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)]
730 "--"
731 ["Set for Today" org-mouse-timestamp-today]
732 ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)]
733 ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)]
734 ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)]
735 ["Set in a Month" (org-mouse-timestamp-today 1 'month)]
736 "--"
737 ["+ 1 Day" (org-timestamp-change 1 'day)]
738 ["+ 1 Week" (org-timestamp-change 7 'day)]
739 ["+ 1 Month" (org-timestamp-change 1 'month)]
740 "--"
741 ["- 1 Day" (org-timestamp-change -1 'day)]
742 ["- 1 Week" (org-timestamp-change -7 'day)]
743 ["- 1 Month" (org-timestamp-change -1 'month)])))
744 ((get-context :table-special)
745 (let ((mdata (match-data)))
746 (incf (car mdata) 2)
747 (store-match-data mdata))
748 (message "match: %S" (match-string 0))
749 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
750 '(" " "!" "^" "_" "$" "#" "*" "'") 0
751 (lambda (mark)
752 (case (string-to-char mark)
753 (? "( ) Nothing Special")
754 (?! "(!) Column Names")
755 (?^ "(^) Field Names Above")
756 (?_ "(^) Field Names Below")
757 (?$ "($) Formula Parameters")
758 (?# "(#) Recalculation: Auto")
759 (?* "(*) Recalculation: Manual")
760 (?' "(') Recalculation: None"))) t))))
761 ((assq :table contextlist)
762 (popup-menu
763 '(nil
764 ["Align Table" org-ctrl-c-ctrl-c]
765 ["Blank Field" org-table-blank-field]
766 ["Edit Field" org-table-edit-field]
767 "--"
768 ("Column"
769 ["Move Column Left" org-metaleft]
770 ["Move Column Right" org-metaright]
771 ["Delete Column" org-shiftmetaleft]
772 ["Insert Column" org-shiftmetaright]
773 "--"
774 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle])
775 ("Row"
776 ["Move Row Up" org-metaup]
777 ["Move Row Down" org-metadown]
778 ["Delete Row" org-shiftmetaup]
779 ["Insert Row" org-shiftmetadown]
780 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
781 "--"
782 ["Insert Hline" org-table-insert-hline])
783 ("Rectangle"
784 ["Copy Rectangle" org-copy-special]
785 ["Cut Rectangle" org-cut-special]
786 ["Paste Rectangle" org-paste-special]
787 ["Fill Rectangle" org-table-wrap-region])
788 "--"
789 ["Set Column Formula" org-table-eval-formula]
790 ["Set Field Formula" (org-table-eval-formula '(4))]
791 ["Edit Formulas" org-table-edit-formulas]
792 "--"
793 ["Recalculate Line" org-table-recalculate]
794 ["Recalculate All" (org-table-recalculate '(4))]
795 ["Iterate All" (org-table-recalculate '(16))]
796 "--"
797 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks]
798 ["Sum Column/Rectangle" org-table-sum
799 :active (or (org-at-table-p) (org-region-active-p))]
800 ["Field Info" org-table-field-info]
801 ["Debug Formulas"
802 (setq org-table-formula-debug (not org-table-formula-debug))
803 :style toggle :selected org-table-formula-debug]
805 ((and (assq :headline contextlist) (not (eolp)))
806 (let ((priority (org-mouse-get-priority t)))
807 (popup-menu
808 `("Headline Menu"
809 ("Tags and Priorities"
810 ,@(org-mouse-keyword-menu
811 (org-mouse-priority-list)
812 #'(lambda (keyword)
813 (org-mouse-set-priority (string-to-char keyword)))
814 priority "Priority %s")
815 "--"
816 ,@(org-mouse-tag-menu))
817 ("TODO Status"
818 ,@(org-mouse-todo-menu (org-get-todo-state)))
819 ["Show Tags"
820 (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags))
821 :visible (not org-mouse-direct)]
822 ["Show Priority"
823 (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority))
824 :visible (not org-mouse-direct)]
825 ,@(if org-mouse-direct '("--") nil)
826 ["New Heading" org-mouse-insert-heading :visible org-mouse-direct]
827 ["Set Deadline"
828 (progn (org-mouse-end-headline) (insert " ") (org-deadline))
829 :active (not (save-excursion
830 (org-mouse-re-search-line org-deadline-regexp)))]
831 ["Schedule Task"
832 (progn (org-mouse-end-headline) (insert " ") (org-schedule))
833 :active (not (save-excursion
834 (org-mouse-re-search-line org-scheduled-regexp)))]
835 ["Insert Timestamp"
836 (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t]
837 ; ["Timestamp (inactive)" org-time-stamp-inactive t]
838 "--"
839 ["Archive Subtree" org-archive-subtree]
840 ["Cut Subtree" org-cut-special]
841 ["Copy Subtree" org-copy-special]
842 ["Paste Subtree" org-paste-special :visible org-mouse-direct]
843 ("Sort Children"
844 ["Alphabetically" (org-sort-entries nil ?a)]
845 ["Numerically" (org-sort-entries nil ?n)]
846 ["By Time/Date" (org-sort-entries nil ?t)]
847 "--"
848 ["Reverse Alphabetically" (org-sort-entries nil ?A)]
849 ["Reverse Numerically" (org-sort-entries nil ?N)]
850 ["Reverse By Time/Date" (org-sort-entries nil ?T)])
851 "--"
852 ["Move Trees" org-mouse-move-tree :active nil]
853 ))))
855 (org-mouse-popup-global-menu))))))
857 (defun org-mouse-mark-active ()
858 (and mark-active transient-mark-mode))
860 (defun org-mouse-in-region-p (pos)
861 (and (org-mouse-mark-active)
862 (>= pos (region-beginning))
863 (< pos (region-end))))
865 (defun org-mouse-down-mouse (event)
866 (interactive "e")
867 (setq this-command last-command)
868 (unless (and (= 1 (event-click-count event))
869 (org-mouse-in-region-p (posn-point (event-start event))))
870 (mouse-drag-region event)))
872 (add-hook 'org-mode-hook
873 #'(lambda ()
874 (setq org-mouse-context-menu-function 'org-mouse-context-menu)
876 (when (memq 'context-menu org-mouse-features)
877 (org-defkey org-mouse-map [mouse-3] nil)
878 (org-defkey org-mode-map [mouse-3] 'org-mouse-show-context-menu))
879 (org-defkey org-mode-map [down-mouse-1] 'org-mouse-down-mouse)
880 (when (memq 'context-menu org-mouse-features)
881 (org-defkey org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree)
882 (org-defkey org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start))
883 (when (memq 'yank-link org-mouse-features)
884 (org-defkey org-mode-map [S-mouse-2] 'org-mouse-yank-link)
885 (org-defkey org-mode-map [drag-mouse-3] 'org-mouse-yank-link))
886 (when (memq 'move-tree org-mouse-features)
887 (org-defkey org-mouse-map [drag-mouse-3] 'org-mouse-move-tree)
888 (org-defkey org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start))
890 (when (memq 'activate-stars org-mouse-features)
891 (font-lock-add-keywords
893 `((,org-outline-regexp
894 0 `(face org-link mouse-face highlight keymap ,org-mouse-map)
895 'prepend))
898 (when (memq 'activate-bullets org-mouse-features)
899 (font-lock-add-keywords
901 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +"
902 (1 `(face org-link keymap ,org-mouse-map mouse-face highlight)
903 'prepend)))
906 (when (memq 'activate-checkboxes org-mouse-features)
907 (font-lock-add-keywords
909 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
910 (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t)))
913 (defadvice org-open-at-point (around org-mouse-open-at-point activate)
914 (let ((context (org-context)))
915 (cond
916 ((assq :headline-stars context) (org-cycle))
917 ((assq :checkbox context) (org-toggle-checkbox))
918 ((assq :item-bullet context)
919 (let ((org-cycle-include-plain-lists t)) (org-cycle)))
920 (t ad-do-it))))))
922 (defun org-mouse-move-tree-start (event)
923 (interactive "e")
924 (message "Same line: promote/demote, (***):move before, (text): make a child"))
927 (defun org-mouse-make-marker (position)
928 (with-current-buffer (window-buffer (posn-window position))
929 (copy-marker (posn-point position))))
931 (defun org-mouse-move-tree (event)
932 ;; todo: handle movements between different buffers
933 (interactive "e")
934 (save-excursion
935 (let* ((start (org-mouse-make-marker (event-start event)))
936 (end (org-mouse-make-marker (event-end event)))
937 (sbuf (marker-buffer start))
938 (ebuf (marker-buffer end)))
940 (when (and sbuf ebuf)
941 (set-buffer sbuf)
942 (goto-char start)
943 (org-back-to-heading)
944 (if (and (eq sbuf ebuf)
945 (equal
946 (point)
947 (save-excursion (goto-char end) (org-back-to-heading) (point))))
948 ;; if the same line then promote/demote
949 (if (>= end start) (org-demote-subtree) (org-promote-subtree))
950 ;; if different lines then move
951 (org-cut-subtree)
953 (set-buffer ebuf)
954 (goto-char end)
955 (org-back-to-heading)
956 (when (and (eq sbuf ebuf)
957 (equal
958 (point)
959 (save-excursion (goto-char start)
960 (org-back-to-heading) (point))))
961 (outline-end-of-subtree)
962 (end-of-line)
963 (if (eobp) (newline) (forward-char)))
965 (when (looking-at org-outline-regexp)
966 (let ((level (- (match-end 0) (match-beginning 0))))
967 (when (> end (match-end 0))
968 (outline-end-of-subtree)
969 (end-of-line)
970 (if (eobp) (newline) (forward-char))
971 (setq level (1+ level)))
972 (org-paste-subtree level)
973 (save-excursion
974 (outline-end-of-subtree)
975 (when (bolp) (delete-char -1))))))))))
978 (defun org-mouse-transform-to-outline ()
979 (interactive)
980 (org-back-to-heading)
981 (let ((minlevel 1000)
982 (replace-text (concat (match-string 0) "* ")))
983 (beginning-of-line 2)
984 (save-excursion
985 (while (not (or (eobp) (looking-at org-outline-regexp)))
986 (when (looking-at org-mouse-plain-list-regexp)
987 (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1)))))
988 (forward-line)))
989 (while (not (or (eobp) (looking-at org-outline-regexp)))
990 (when (and (looking-at org-mouse-plain-list-regexp)
991 (eq minlevel (- (match-end 1) (match-beginning 1))))
992 (replace-match replace-text))
993 (forward-line))))
995 (defvar _cmd) ;dynamically scoped from `org-with-remote-undo'.
997 (defun org-mouse-do-remotely (command)
998 ; (org-agenda-check-no-diary)
999 (when (get-text-property (point) 'org-marker)
1000 (let* ((anticol (- (point-at-eol) (point)))
1001 (marker (get-text-property (point) 'org-marker))
1002 (buffer (marker-buffer marker))
1003 (pos (marker-position marker))
1004 (hdmarker (get-text-property (point) 'org-hd-marker))
1005 (buffer-read-only nil)
1006 (newhead "--- removed ---")
1007 (org-mouse-direct nil)
1008 (org-mouse-main-buffer (current-buffer)))
1009 (when (eq (with-current-buffer buffer major-mode) 'org-mode)
1010 (let ((endmarker (with-current-buffer buffer
1011 (outline-end-of-subtree)
1012 (forward-char 1)
1013 (copy-marker (point)))))
1014 (org-with-remote-undo buffer
1015 (with-current-buffer buffer
1016 (widen)
1017 (goto-char pos)
1018 (org-show-hidden-entry)
1019 (save-excursion
1020 (and (outline-next-heading)
1021 (org-flag-heading nil))) ; show the next heading
1022 (org-back-to-heading)
1023 (setq marker (copy-marker (point)))
1024 (goto-char (max (point-at-bol) (- (point-at-eol) anticol)))
1025 (funcall command)
1026 (message "_cmd: %S" _cmd)
1027 (message "this-command: %S" this-command)
1028 (unless (eq (marker-position marker) (marker-position endmarker))
1029 (setq newhead (org-get-heading))))
1031 (beginning-of-line 1)
1032 (save-excursion
1033 (org-agenda-change-all-lines newhead hdmarker 'fixface))))
1034 t))))
1036 (defun org-mouse-agenda-context-menu (&optional event)
1037 (or (org-mouse-do-remotely 'org-mouse-context-menu)
1038 (popup-menu
1039 '("Agenda"
1040 ("Agenda Files")
1041 "--"
1042 ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo))
1043 :visible (if (eq last-command 'org-agenda-undo)
1044 org-agenda-pending-undo-list
1045 org-agenda-undo-list)]
1046 ["Rebuild Buffer" org-agenda-redo t]
1047 ["New Diary Entry"
1048 org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline) t]
1049 "--"
1050 ["Goto Today" org-agenda-goto-today
1051 (org-agenda-check-type nil 'agenda 'timeline) t]
1052 ["Display Calendar" org-agenda-goto-calendar
1053 (org-agenda-check-type nil 'agenda 'timeline) t]
1054 ("Calendar Commands"
1055 ["Phases of the Moon" org-agenda-phases-of-moon
1056 (org-agenda-check-type nil 'agenda 'timeline)]
1057 ["Sunrise/Sunset" org-agenda-sunrise-sunset
1058 (org-agenda-check-type nil 'agenda 'timeline)]
1059 ["Holidays" org-agenda-holidays
1060 (org-agenda-check-type nil 'agenda 'timeline)]
1061 ["Convert" org-agenda-convert-date
1062 (org-agenda-check-type nil 'agenda 'timeline)]
1063 "--"
1064 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
1065 "--"
1066 ["Day View" org-agenda-day-view
1067 :active (org-agenda-check-type nil 'agenda)
1068 :style radio :selected (eq org-agenda-current-span 'day)]
1069 ["Week View" org-agenda-week-view
1070 :active (org-agenda-check-type nil 'agenda)
1071 :style radio :selected (eq org-agenda-current-span 'week)]
1072 "--"
1073 ["Show Logbook entries" org-agenda-log-mode
1074 :style toggle :selected org-agenda-show-log
1075 :active (org-agenda-check-type nil 'agenda 'timeline)]
1076 ["Include Diary" org-agenda-toggle-diary
1077 :style toggle :selected org-agenda-include-diary
1078 :active (org-agenda-check-type nil 'agenda)]
1079 ["Use Time Grid" org-agenda-toggle-time-grid
1080 :style toggle :selected org-agenda-use-time-grid
1081 :active (org-agenda-check-type nil 'agenda)]
1082 ["Follow Mode" org-agenda-follow-mode
1083 :style toggle :selected org-agenda-follow-mode]
1084 "--"
1085 ["Quit" org-agenda-quit t]
1086 ["Exit and Release Buffers" org-agenda-exit t]
1087 ))))
1089 (defun org-mouse-get-gesture (event)
1090 (let ((startxy (posn-x-y (event-start event)))
1091 (endxy (posn-x-y (event-end event))))
1092 (if (< (car startxy) (car endxy)) :right :left)))
1095 ; (setq org-agenda-mode-hook nil)
1096 (defvar org-agenda-mode-map)
1097 (add-hook 'org-agenda-mode-hook
1098 #'(lambda ()
1099 (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu)
1100 (org-defkey org-agenda-mode-map [mouse-3] 'org-mouse-show-context-menu)
1101 (org-defkey org-agenda-mode-map [down-mouse-3] 'org-mouse-move-tree-start)
1102 (org-defkey org-agenda-mode-map [C-mouse-4] 'org-agenda-earlier)
1103 (org-defkey org-agenda-mode-map [C-mouse-5] 'org-agenda-later)
1104 (org-defkey org-agenda-mode-map [drag-mouse-3]
1105 #'(lambda (event) (interactive "e")
1106 (case (org-mouse-get-gesture event)
1107 (:left (org-agenda-earlier 1))
1108 (:right (org-agenda-later 1)))))))
1110 (provide 'org-mouse)
1112 ;;; org-mouse.el ends here