Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-mouse.el
blob6fc2cc57581245b45b6c5f9efe9d158239d33a98
1 ;;; org-mouse.el --- Better mouse support for org-mode
3 ;; Copyright (C) 2006-2016 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 (looking-back ":[A-Za-z]+:" (line-beginning-position))
195 (skip-chars-backward ":A-Za-z")
196 (skip-chars-backward "\t ")))
198 (defvar-local 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.")
203 (defun org-mouse-show-context-menu (event prefix)
204 "Invoke the context menu.
206 If the value of `org-mouse-context-menu-function' is a function, then
207 this function is called. Otherwise, the current major mode menu is used."
208 (interactive "@e \nP")
209 (if (and (= (event-click-count event) 1)
210 (or (not mark-active)
211 (sit-for (/ double-click-time 1000.0))))
212 (progn
213 (select-window (posn-window (event-start event)))
214 (when (not (org-mouse-mark-active))
215 (goto-char (posn-point (event-start event)))
216 (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook)))
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 "Return `: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 (org-time-stamp nil)
271 (when shift (org-timestamp-change shift units)))
273 (defun org-mouse-keyword-menu (keywords function &optional selected itemformat)
274 "A helper function.
276 Returns a menu fragment consisting of KEYWORDS. When a keyword
277 is selected by the user, FUNCTION is called with the selected
278 keyword as the only argument.
280 If SELECTED is nil, then all items are normal menu items. If
281 SELECTED is a function, then each item is a checkbox, which is
282 enabled for a given keyword iff (funcall SELECTED keyword) return
283 non-nil. If SELECTED is neither nil nor a function, then the
284 items are radio buttons. A radio button is enabled for the
285 keyword `equal' to SELECTED.
287 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
288 is a function, it is invoked with the keyword as the only
289 argument. If it is a string, it is interpreted as the format
290 string to (format ITEMFORMAT keyword). If it is neither a string
291 nor a function, elements of KEYWORDS are used directly."
292 (mapcar
293 `(lambda (keyword)
294 (vector (cond
295 ((functionp ,itemformat) (funcall ,itemformat keyword))
296 ((stringp ,itemformat) (format ,itemformat keyword))
297 (t keyword))
298 (list 'funcall ,function keyword)
299 :style (cond
300 ((null ,selected) t)
301 ((functionp ,selected) 'toggle)
302 (t 'radio))
303 :selected (if (functionp ,selected)
304 (and (funcall ,selected keyword) t)
305 (equal ,selected keyword))))
306 keywords))
308 (defun org-mouse-remove-match-and-spaces ()
309 "Remove the match, make just one space around the point."
310 (interactive)
311 (replace-match "")
312 (just-one-space))
314 (defvar org-mouse-rest)
315 (defun org-mouse-replace-match-and-surround (newtext &optional fixedcase
316 literal string subexp)
317 "The same as `replace-match', but surrounds the replacement with spaces."
318 (apply 'replace-match org-mouse-rest)
319 (save-excursion
320 (goto-char (match-beginning (or subexp 0)))
321 (just-one-space)
322 (goto-char (match-end (or subexp 0)))
323 (just-one-space)))
325 (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat
326 nosurround)
327 "A helper function.
329 Returns a menu fragment consisting of KEYWORDS. When a keyword
330 is selected, group GROUP of the current match is replaced by the
331 keyword. The method ensures that both ends of the replacement
332 are separated from the rest of the text in the buffer by
333 individual spaces (unless NOSURROUND is non-nil).
335 The final entry of the menu is always \"None\", which removes the
336 match.
338 ITEMFORMAT governs formatting of the elements of KEYWORDS. If it
339 is a function, it is invoked with the keyword as the only
340 argument. If it is a string, it is interpreted as the format
341 string to (format ITEMFORMAT keyword). If it is neither a string
342 nor a function, elements of KEYWORDS are used directly."
343 (setq group (or group 0))
344 (let ((replace (org-mouse-match-closure
345 (if nosurround 'replace-match
346 'org-mouse-replace-match-and-surround))))
347 (append
348 (org-mouse-keyword-menu
349 keywords
350 `(lambda (keyword) (funcall ,replace keyword t t nil ,group))
351 (match-string group)
352 itemformat)
353 `(["None" org-mouse-remove-match-and-spaces
354 :style radio
355 :selected ,(not (member (match-string group) keywords))]))))
357 (defun org-mouse-show-headlines ()
358 "Change the visibility of the current org buffer to only show headlines."
359 (interactive)
360 (let ((this-command 'org-cycle)
361 (last-command 'org-cycle)
362 (org-cycle-global-status nil))
363 (org-cycle '(4))
364 (org-cycle '(4))))
366 (defun org-mouse-show-overview ()
367 "Change visibility of current org buffer to first-level headlines only."
368 (interactive)
369 (let ((org-cycle-global-status nil))
370 (org-cycle '(4))))
372 (defun org-mouse-set-priority (priority)
373 "Set the priority of the current headline to PRIORITY."
374 (org-priority priority))
376 (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]"
377 "Regular expression matching the priority indicator.
378 Differs from `org-priority-regexp' in that it doesn't contain the
379 leading `.*?'.")
381 (defun org-mouse-get-priority (&optional default)
382 "Return the priority of the current headline.
383 DEFAULT is returned if no priority is given in the headline."
384 (save-excursion
385 (if (org-mouse-re-search-line org-mouse-priority-regexp)
386 (match-string 1)
387 (when default (char-to-string org-default-priority)))))
389 (defun org-mouse-delete-timestamp ()
390 "Deletes the current timestamp as well as the preceding keyword.
391 SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:"
392 (when (or (org-at-date-range-p) (org-at-timestamp-p))
393 (replace-match "") ; delete the timestamp
394 (skip-chars-backward " :A-Z")
395 (when (looking-at " *[A-Z][A-Z]+:")
396 (replace-match ""))))
398 (defun org-mouse-looking-at (regexp skipchars &optional movechars)
399 (save-excursion
400 (let ((point (point)))
401 (if (looking-at regexp) t
402 (skip-chars-backward skipchars)
403 (forward-char (or movechars 0))
404 (when (looking-at regexp)
405 (> (match-end 0) point))))))
407 (defun org-mouse-priority-list ()
408 (loop for priority from ?A to org-lowest-priority
409 collect (char-to-string priority)))
411 (defun org-mouse-todo-menu (state)
412 "Create the menu with TODO keywords."
413 (append
414 (let ((kwds org-todo-keywords-1))
415 (org-mouse-keyword-menu
416 kwds
417 `(lambda (kwd) (org-todo kwd))
418 (lambda (kwd) (equal state kwd))))))
420 (defun org-mouse-tag-menu () ;todo
421 "Create the tags menu."
422 (append
423 (let ((tags (org-get-tags)))
424 (org-mouse-keyword-menu
425 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
426 `(lambda (tag)
427 (org-mouse-set-tags
428 (sort (if (member tag (quote ,tags))
429 (delete tag (quote ,tags))
430 (cons tag (quote ,tags)))
431 'string-lessp)))
432 `(lambda (tag) (member tag (quote ,tags)))
434 '("--"
435 ["Align Tags Here" (org-set-tags nil t) t]
436 ["Align Tags in Buffer" (org-set-tags t t) t]
437 ["Set Tags ..." (org-set-tags) t])))
439 (defun org-mouse-set-tags (tags)
440 (save-excursion
441 ;; remove existing tags first
442 (beginning-of-line)
443 (when (org-mouse-re-search-line ":\\(\\([A-Za-z_]+:\\)+\\)")
444 (replace-match ""))
446 ;; set new tags if any
447 (when tags
448 (end-of-line)
449 (insert " :" (mapconcat 'identity tags ":") ":")
450 (org-set-tags nil t))))
452 (defun org-mouse-insert-checkbox ()
453 (interactive)
454 (and (org-at-item-p)
455 (goto-char (match-end 0))
456 (unless (org-at-item-checkbox-p)
457 (delete-horizontal-space)
458 (insert " [ ] "))))
460 (defun org-mouse-agenda-type (type)
461 (case type
462 ('tags "Tags: ")
463 ('todo "TODO: ")
464 ('tags-tree "Tags tree: ")
465 ('todo-tree "TODO tree: ")
466 ('occur-tree "Occur tree: ")
467 (t "Agenda command ???")))
469 (defun org-mouse-list-options-menu (alloptions &optional function)
470 (let ((options (save-match-data
471 (split-string (match-string-no-properties 1)))))
472 (print options)
473 (loop for name in alloptions
474 collect
475 (vector name
476 `(progn
477 (replace-match
478 (mapconcat 'identity
479 (sort (if (member ',name ',options)
480 (delete ',name ',options)
481 (cons ',name ',options))
482 'string-lessp)
483 " ")
484 nil nil nil 1)
485 (when (functionp ',function) (funcall ',function)))
486 :style 'toggle
487 :selected (and (member name options) t)))))
489 (defun org-mouse-clip-text (text maxlength)
490 (if (> (length text) maxlength)
491 (concat (substring text 0 (- maxlength 3)) "...")
492 text))
494 (defun org-mouse-popup-global-menu ()
495 (popup-menu
496 `("Main Menu"
497 ["Show Overview" org-mouse-show-overview t]
498 ["Show Headlines" org-mouse-show-headlines t]
499 ["Show All" outline-show-all t]
500 ["Remove Highlights" org-remove-occur-highlights
501 :visible org-occur-highlights]
502 "--"
503 ["Check Deadlines"
504 (if (functionp 'org-check-deadlines-and-todos)
505 (org-check-deadlines-and-todos org-deadline-warning-days)
506 (org-check-deadlines org-deadline-warning-days)) t]
507 ["Check TODOs" org-show-todo-tree t]
508 ("Check Tags"
509 ,@(org-mouse-keyword-menu
510 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
511 #'(lambda (tag) (org-tags-sparse-tree nil tag)))
512 "--"
513 ["Custom Tag ..." org-tags-sparse-tree t])
514 ["Check Phrase ..." org-occur]
515 "--"
516 ["Display Agenda" org-agenda-list t]
517 ["Display Timeline" org-timeline t]
518 ["Display TODO List" org-todo-list t]
519 ("Display Tags"
520 ,@(org-mouse-keyword-menu
521 (sort (mapcar 'car (org-get-buffer-tags)) 'string-lessp)
522 #'(lambda (tag) (org-tags-view nil tag)))
523 "--"
524 ["Custom Tag ..." org-tags-view t])
525 ["Display Calendar" org-goto-calendar t]
526 "--"
527 ,@(org-mouse-keyword-menu
528 (mapcar 'car org-agenda-custom-commands)
529 #'(lambda (key)
530 (eval `(org-agenda nil (string-to-char ,key))))
532 #'(lambda (key)
533 (let ((entry (assoc key org-agenda-custom-commands)))
534 (org-mouse-clip-text
535 (cond
536 ((stringp (nth 1 entry)) (nth 1 entry))
537 ((stringp (nth 2 entry))
538 (concat (org-mouse-agenda-type (nth 1 entry))
539 (nth 2 entry)))
540 (t "Agenda Command `%s'"))
541 30))))
542 "--"
543 ["Delete Blank Lines" delete-blank-lines
544 :visible (org-mouse-empty-line)]
545 ["Insert Checkbox" org-mouse-insert-checkbox
546 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
547 ["Insert Checkboxes"
548 (org-mouse-for-each-item 'org-mouse-insert-checkbox)
549 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))]
550 ["Plain List to Outline" org-mouse-transform-to-outline
551 :visible (org-at-item-p)])))
553 (defun org-mouse-get-context (contextlist context)
554 (let ((contextdata (assq context contextlist)))
555 (when contextdata
556 (save-excursion
557 (goto-char (second contextdata))
558 (re-search-forward ".*" (third contextdata))))))
560 (defun org-mouse-for-each-item (funct)
561 ;; Functions called by `org-apply-on-list' need an argument
562 (let ((wrap-fun (lambda (c) (funcall funct))))
563 (when (ignore-errors (goto-char (org-in-item-p)))
564 (save-excursion (org-apply-on-list wrap-fun nil)))))
566 (defun org-mouse-bolp ()
567 "Return true if there only spaces, tabs, and `*' before point.
568 This means, between the beginning of line and the point."
569 (save-excursion
570 (skip-chars-backward " \t*") (bolp)))
572 (defun org-mouse-insert-item (text)
573 (case (org-mouse-line-position)
574 (:beginning ; insert before
575 (beginning-of-line)
576 (looking-at "[ \t]*")
577 (open-line 1)
578 (indent-to-column (- (match-end 0) (match-beginning 0)))
579 (insert "+ "))
580 (:middle ; insert after
581 (end-of-line)
582 (newline t)
583 (indent-relative)
584 (insert "+ "))
585 (:end ; insert text here
586 (skip-chars-backward " \t")
587 (kill-region (point) (point-at-eol))
588 (unless (looking-back org-mouse-punctuation (line-beginning-position))
589 (insert (concat org-mouse-punctuation " ")))))
590 (insert text)
591 (beginning-of-line))
593 (defadvice dnd-insert-text (around org-mouse-dnd-insert-text activate)
594 (if (derived-mode-p 'org-mode)
595 (org-mouse-insert-item text)
596 ad-do-it))
598 (defadvice dnd-open-file (around org-mouse-dnd-open-file activate)
599 (if (derived-mode-p 'org-mode)
600 (org-mouse-insert-item uri)
601 ad-do-it))
603 (defun org-mouse-match-closure (function)
604 (let ((match (match-data t)))
605 `(lambda (&rest rest)
606 (save-match-data
607 (set-match-data ',match)
608 (apply ',function rest)))))
610 (defun org-mouse-yank-link (click)
611 (interactive "e")
612 ;; Give temporary modes such as isearch a chance to turn off.
613 (run-hooks 'mouse-leave-buffer-hook)
614 (mouse-set-point click)
615 (setq mouse-selection-click-count 0)
616 (delete-horizontal-space)
617 (insert-for-yank (concat " [[" (current-kill 0) "]] ")))
619 (defun org-mouse-context-menu (&optional event)
620 (let* ((stamp-prefixes (list org-deadline-string org-scheduled-string))
621 (contextlist (org-context))
622 (get-context (lambda (context) (org-mouse-get-context contextlist context))))
623 (cond
624 ((org-mouse-mark-active)
625 (let ((region-string (buffer-substring (region-beginning) (region-end))))
626 (popup-menu
627 `(nil
628 ["Sparse Tree" (org-occur ',region-string)]
629 ["Find in Buffer" (occur ',region-string)]
630 ["Grep in Current Dir"
631 (grep (format "grep -rnH -e '%s' *" ',region-string))]
632 ["Grep in Parent Dir"
633 (grep (format "grep -rnH -e '%s' ../*" ',region-string))]
634 "--"
635 ["Convert to Link"
636 (progn (save-excursion (goto-char (region-beginning)) (insert "[["))
637 (save-excursion (goto-char (region-end)) (insert "]]")))]
638 ["Insert Link Here" (org-mouse-yank-link ',event)]))))
639 ((save-excursion (beginning-of-line) (looking-at "[ \t]*#\\+STARTUP: \\(.*\\)"))
640 (popup-menu
641 `(nil
642 ,@(org-mouse-list-options-menu (mapcar 'car org-startup-options)
643 'org-mode-restart))))
644 ((or (eolp)
645 (and (looking-at "\\( \\|\t\\)\\(+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$")
646 (looking-back " \\|\t" (- (point) 2)
647 (line-beginning-position))))
648 (org-mouse-popup-global-menu))
649 ((funcall get-context :checkbox)
650 (popup-menu
651 '(nil
652 ["Toggle" org-toggle-checkbox t]
653 ["Remove" org-mouse-remove-match-and-spaces t]
655 ["All Clear" (org-mouse-for-each-item
656 (lambda ()
657 (when (save-excursion (org-at-item-checkbox-p))
658 (replace-match "[ ] "))))]
659 ["All Set" (org-mouse-for-each-item
660 (lambda ()
661 (when (save-excursion (org-at-item-checkbox-p))
662 (replace-match "[X] "))))]
663 ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t]
664 ["All Remove" (org-mouse-for-each-item
665 (lambda ()
666 (when (save-excursion (org-at-item-checkbox-p))
667 (org-mouse-remove-match-and-spaces))))]
669 ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_")
670 (member (match-string 0) org-todo-keywords-1))
671 (popup-menu
672 `(nil
673 ,@(org-mouse-todo-menu (match-string 0))
674 "--"
675 ["Check TODOs" org-show-todo-tree t]
676 ["List all TODO keywords" org-todo-list t]
677 [,(format "List only %s" (match-string 0))
678 (org-todo-list (match-string 0)) t]
680 ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z")
681 (member (match-string 0) stamp-prefixes))
682 (popup-menu
683 `(nil
684 ,@(org-mouse-keyword-replace-menu stamp-prefixes)
685 "--"
686 ["Check Deadlines" org-check-deadlines t]
688 ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority
689 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
690 (org-mouse-priority-list) 1 "Priority %s" t))))
691 ((funcall get-context :link)
692 (popup-menu
693 '(nil
694 ["Open" org-open-at-point t]
695 ["Open in Emacs" (org-open-at-point t) t]
696 "--"
697 ["Copy link" (org-kill-new (match-string 0))]
698 ["Cut link"
699 (progn
700 (kill-region (match-beginning 0) (match-end 0))
701 (just-one-space))]
702 "--"
703 ["Grep for TODOs"
704 (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))]
705 ; ["Paste file link" ((insert "file:") (yank))]
707 ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags
708 (popup-menu
709 `(nil
710 [,(format-message "Display `%s'" (match-string 1))
711 (org-tags-view nil ,(match-string 1))]
712 [,(format-message "Sparse Tree `%s'" (match-string 1))
713 (org-tags-sparse-tree nil ,(match-string 1))]
714 "--"
715 ,@(org-mouse-tag-menu))))
716 ((org-at-timestamp-p)
717 (popup-menu
718 '(nil
719 ["Show Day" org-open-at-point t]
720 ["Change Timestamp" org-time-stamp t]
721 ["Delete Timestamp" (org-mouse-delete-timestamp) t]
722 ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)]
723 "--"
724 ["Set for Today" org-mouse-timestamp-today]
725 ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)]
726 ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)]
727 ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)]
728 ["Set in a Month" (org-mouse-timestamp-today 1 'month)]
729 "--"
730 ["+ 1 Day" (org-timestamp-change 1 'day)]
731 ["+ 1 Week" (org-timestamp-change 7 'day)]
732 ["+ 1 Month" (org-timestamp-change 1 'month)]
733 "--"
734 ["- 1 Day" (org-timestamp-change -1 'day)]
735 ["- 1 Week" (org-timestamp-change -7 'day)]
736 ["- 1 Month" (org-timestamp-change -1 'month)])))
737 ((funcall get-context :table-special)
738 (let ((mdata (match-data)))
739 (incf (car mdata) 2)
740 (store-match-data mdata))
741 (message "match: %S" (match-string 0))
742 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu
743 '(" " "!" "^" "_" "$" "#" "*" "'") 0
744 (lambda (mark)
745 (case (string-to-char mark)
746 (? "( ) Nothing Special")
747 (?! "(!) Column Names")
748 (?^ "(^) Field Names Above")
749 (?_ "(^) Field Names Below")
750 (?$ "($) Formula Parameters")
751 (?# "(#) Recalculation: Auto")
752 (?* "(*) Recalculation: Manual")
753 (?' "(') Recalculation: None"))) t))))
754 ((assq :table contextlist)
755 (popup-menu
756 '(nil
757 ["Align Table" org-ctrl-c-ctrl-c]
758 ["Blank Field" org-table-blank-field]
759 ["Edit Field" org-table-edit-field]
760 "--"
761 ("Column"
762 ["Move Column Left" org-metaleft]
763 ["Move Column Right" org-metaright]
764 ["Delete Column" org-shiftmetaleft]
765 ["Insert Column" org-shiftmetaright]
766 "--"
767 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle])
768 ("Row"
769 ["Move Row Up" org-metaup]
770 ["Move Row Down" org-metadown]
771 ["Delete Row" org-shiftmetaup]
772 ["Insert Row" org-shiftmetadown]
773 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
774 "--"
775 ["Insert Hline" org-table-insert-hline])
776 ("Rectangle"
777 ["Copy Rectangle" org-copy-special]
778 ["Cut Rectangle" org-cut-special]
779 ["Paste Rectangle" org-paste-special]
780 ["Fill Rectangle" org-table-wrap-region])
781 "--"
782 ["Set Column Formula" org-table-eval-formula]
783 ["Set Field Formula" (org-table-eval-formula '(4))]
784 ["Edit Formulas" org-table-edit-formulas]
785 "--"
786 ["Recalculate Line" org-table-recalculate]
787 ["Recalculate All" (org-table-recalculate '(4))]
788 ["Iterate All" (org-table-recalculate '(16))]
789 "--"
790 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks]
791 ["Sum Column/Rectangle" org-table-sum
792 :active (or (org-at-table-p) (org-region-active-p))]
793 ["Field Info" org-table-field-info]
794 ["Debug Formulas"
795 (setq org-table-formula-debug (not org-table-formula-debug))
796 :style toggle :selected org-table-formula-debug]
798 ((and (assq :headline contextlist) (not (eolp)))
799 (let ((priority (org-mouse-get-priority t)))
800 (popup-menu
801 `("Headline Menu"
802 ("Tags and Priorities"
803 ,@(org-mouse-keyword-menu
804 (org-mouse-priority-list)
805 #'(lambda (keyword)
806 (org-mouse-set-priority (string-to-char keyword)))
807 priority "Priority %s")
808 "--"
809 ,@(org-mouse-tag-menu))
810 ("TODO Status"
811 ,@(org-mouse-todo-menu (org-get-todo-state)))
812 ["Show Tags"
813 (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags))
814 :visible (not org-mouse-direct)]
815 ["Show Priority"
816 (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority))
817 :visible (not org-mouse-direct)]
818 ,@(if org-mouse-direct '("--") nil)
819 ["New Heading" org-mouse-insert-heading :visible org-mouse-direct]
820 ["Set Deadline"
821 (progn (org-mouse-end-headline) (insert " ") (org-deadline))
822 :active (not (save-excursion
823 (org-mouse-re-search-line org-deadline-regexp)))]
824 ["Schedule Task"
825 (progn (org-mouse-end-headline) (insert " ") (org-schedule))
826 :active (not (save-excursion
827 (org-mouse-re-search-line org-scheduled-regexp)))]
828 ["Insert Timestamp"
829 (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t]
830 ; ["Timestamp (inactive)" org-time-stamp-inactive t]
831 "--"
832 ["Archive Subtree" org-archive-subtree]
833 ["Cut Subtree" org-cut-special]
834 ["Copy Subtree" org-copy-special]
835 ["Paste Subtree" org-paste-special :visible org-mouse-direct]
836 ("Sort Children"
837 ["Alphabetically" (org-sort-entries nil ?a)]
838 ["Numerically" (org-sort-entries nil ?n)]
839 ["By Time/Date" (org-sort-entries nil ?t)]
840 "--"
841 ["Reverse Alphabetically" (org-sort-entries nil ?A)]
842 ["Reverse Numerically" (org-sort-entries nil ?N)]
843 ["Reverse By Time/Date" (org-sort-entries nil ?T)])
844 "--"
845 ["Move Trees" org-mouse-move-tree :active nil]
846 ))))
848 (org-mouse-popup-global-menu)))))
850 (defun org-mouse-mark-active ()
851 (and mark-active transient-mark-mode))
853 (defun org-mouse-in-region-p (pos)
854 (and (org-mouse-mark-active)
855 (>= pos (region-beginning))
856 (< pos (region-end))))
858 (defun org-mouse-down-mouse (event)
859 (interactive "e")
860 (setq this-command last-command)
861 (unless (and (= 1 (event-click-count event))
862 (org-mouse-in-region-p (posn-point (event-start event))))
863 (mouse-drag-region event)))
865 (add-hook 'org-mode-hook
866 #'(lambda ()
867 (setq org-mouse-context-menu-function 'org-mouse-context-menu)
869 (when (memq 'context-menu org-mouse-features)
870 (org-defkey org-mouse-map [mouse-3] nil)
871 (org-defkey org-mode-map [mouse-3] 'org-mouse-show-context-menu))
872 (org-defkey org-mode-map [down-mouse-1] 'org-mouse-down-mouse)
873 (when (memq 'context-menu org-mouse-features)
874 (org-defkey org-mouse-map [C-drag-mouse-1] 'org-mouse-move-tree)
875 (org-defkey org-mouse-map [C-down-mouse-1] 'org-mouse-move-tree-start))
876 (when (memq 'yank-link org-mouse-features)
877 (org-defkey org-mode-map [S-mouse-2] 'org-mouse-yank-link)
878 (org-defkey org-mode-map [drag-mouse-3] 'org-mouse-yank-link))
879 (when (memq 'move-tree org-mouse-features)
880 (org-defkey org-mouse-map [drag-mouse-3] 'org-mouse-move-tree)
881 (org-defkey org-mouse-map [down-mouse-3] 'org-mouse-move-tree-start))
883 (when (memq 'activate-stars org-mouse-features)
884 (font-lock-add-keywords
886 `((,org-outline-regexp
887 0 `(face org-link mouse-face highlight keymap ,org-mouse-map)
888 'prepend))
891 (when (memq 'activate-bullets org-mouse-features)
892 (font-lock-add-keywords
894 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +"
895 (1 `(face org-link keymap ,org-mouse-map mouse-face highlight)
896 'prepend)))
899 (when (memq 'activate-checkboxes org-mouse-features)
900 (font-lock-add-keywords
902 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
903 (2 `(face bold keymap ,org-mouse-map mouse-face highlight) t)))
906 (defadvice org-open-at-point (around org-mouse-open-at-point activate)
907 (let ((context (org-context)))
908 (cond
909 ((assq :headline-stars context) (org-cycle))
910 ((assq :checkbox context) (org-toggle-checkbox))
911 ((assq :item-bullet context)
912 (let ((org-cycle-include-plain-lists t)) (org-cycle)))
913 ((org-footnote-at-reference-p) nil)
914 (t ad-do-it))))))
916 (defun org-mouse-move-tree-start (event)
917 (interactive "e")
918 (message "Same line: promote/demote, (***):move before, (text): make a child"))
921 (defun org-mouse-make-marker (position)
922 (with-current-buffer (window-buffer (posn-window position))
923 (copy-marker (posn-point position))))
925 (defun org-mouse-move-tree (event)
926 ;; todo: handle movements between different buffers
927 (interactive "e")
928 (save-excursion
929 (let* ((start (org-mouse-make-marker (event-start event)))
930 (end (org-mouse-make-marker (event-end event)))
931 (sbuf (marker-buffer start))
932 (ebuf (marker-buffer end)))
934 (when (and sbuf ebuf)
935 (set-buffer sbuf)
936 (goto-char start)
937 (org-back-to-heading)
938 (if (and (eq sbuf ebuf)
939 (equal
940 (point)
941 (save-excursion (goto-char end) (org-back-to-heading) (point))))
942 ;; if the same line then promote/demote
943 (if (>= end start) (org-demote-subtree) (org-promote-subtree))
944 ;; if different lines then move
945 (org-cut-subtree)
947 (set-buffer ebuf)
948 (goto-char end)
949 (org-back-to-heading)
950 (when (and (eq sbuf ebuf)
951 (equal
952 (point)
953 (save-excursion (goto-char start)
954 (org-back-to-heading) (point))))
955 (progn (org-end-of-subtree nil t)
956 (unless (eobp) (backward-char)))
957 (end-of-line)
958 (if (eobp) (newline) (forward-char)))
960 (when (looking-at org-outline-regexp)
961 (let ((level (- (match-end 0) (match-beginning 0))))
962 (when (> end (match-end 0))
963 (progn (org-end-of-subtree nil t)
964 (unless (eobp) (backward-char)))
965 (end-of-line)
966 (if (eobp) (newline) (forward-char))
967 (setq level (1+ level)))
968 (org-paste-subtree level)
969 (save-excursion
970 (progn (org-end-of-subtree nil t)
971 (unless (eobp) (backward-char)))
972 (when (bolp) (delete-char -1))))))))))
975 (defun org-mouse-transform-to-outline ()
976 (interactive)
977 (org-back-to-heading)
978 (let ((minlevel 1000)
979 (replace-text (concat (match-string 0) "* ")))
980 (beginning-of-line 2)
981 (save-excursion
982 (while (not (or (eobp) (looking-at org-outline-regexp)))
983 (when (looking-at org-mouse-plain-list-regexp)
984 (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1)))))
985 (forward-line)))
986 (while (not (or (eobp) (looking-at org-outline-regexp)))
987 (when (and (looking-at org-mouse-plain-list-regexp)
988 (eq minlevel (- (match-end 1) (match-beginning 1))))
989 (replace-match replace-text))
990 (forward-line))))
992 (defvar org-mouse-cmd) ;dynamically scoped from `org-with-remote-undo'.
994 (defun org-mouse-do-remotely (command)
995 ; (org-agenda-check-no-diary)
996 (when (get-text-property (point) 'org-marker)
997 (let* ((anticol (- (point-at-eol) (point)))
998 (marker (get-text-property (point) 'org-marker))
999 (buffer (marker-buffer marker))
1000 (pos (marker-position marker))
1001 (hdmarker (get-text-property (point) 'org-hd-marker))
1002 (buffer-read-only nil)
1003 (newhead "--- removed ---")
1004 (org-mouse-direct nil)
1005 (org-mouse-main-buffer (current-buffer)))
1006 (when (eq (with-current-buffer buffer major-mode) 'org-mode)
1007 (let ((endmarker (with-current-buffer buffer
1008 (org-end-of-subtree nil t)
1009 (unless (eobp) (forward-char 1))
1010 (point-marker))))
1011 (org-with-remote-undo buffer
1012 (with-current-buffer buffer
1013 (widen)
1014 (goto-char pos)
1015 (org-show-hidden-entry)
1016 (save-excursion
1017 (and (outline-next-heading)
1018 (org-flag-heading nil))) ; show the next heading
1019 (org-back-to-heading)
1020 (setq marker (point-marker))
1021 (goto-char (max (point-at-bol) (- (point-at-eol) anticol)))
1022 (funcall command)
1023 (message "_cmd: %S" org-mouse-cmd)
1024 (message "this-command: %S" this-command)
1025 (unless (eq (marker-position marker) (marker-position endmarker))
1026 (setq newhead (org-get-heading))))
1028 (beginning-of-line 1)
1029 (save-excursion
1030 (org-agenda-change-all-lines newhead hdmarker 'fixface))))
1031 t))))
1033 (defun org-mouse-agenda-context-menu (&optional event)
1034 (or (org-mouse-do-remotely 'org-mouse-context-menu)
1035 (popup-menu
1036 '("Agenda"
1037 ("Agenda Files")
1038 "--"
1039 ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo))
1040 :visible (if (eq last-command 'org-agenda-undo)
1041 org-agenda-pending-undo-list
1042 org-agenda-undo-list)]
1043 ["Rebuild Buffer" org-agenda-redo t]
1044 ["New Diary Entry"
1045 org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline) t]
1046 "--"
1047 ["Goto Today" org-agenda-goto-today
1048 (org-agenda-check-type nil 'agenda 'timeline) t]
1049 ["Display Calendar" org-agenda-goto-calendar
1050 (org-agenda-check-type nil 'agenda 'timeline) t]
1051 ("Calendar Commands"
1052 ["Phases of the Moon" org-agenda-phases-of-moon
1053 (org-agenda-check-type nil 'agenda 'timeline)]
1054 ["Sunrise/Sunset" org-agenda-sunrise-sunset
1055 (org-agenda-check-type nil 'agenda 'timeline)]
1056 ["Holidays" org-agenda-holidays
1057 (org-agenda-check-type nil 'agenda 'timeline)]
1058 ["Convert" org-agenda-convert-date
1059 (org-agenda-check-type nil 'agenda 'timeline)]
1060 "--"
1061 ["Create iCalendar file" org-icalendar-combine-agenda-files t])
1062 "--"
1063 ["Day View" org-agenda-day-view
1064 :active (org-agenda-check-type nil 'agenda)
1065 :style radio :selected (eq org-agenda-current-span 'day)]
1066 ["Week View" org-agenda-week-view
1067 :active (org-agenda-check-type nil 'agenda)
1068 :style radio :selected (eq org-agenda-current-span 'week)]
1069 "--"
1070 ["Show Logbook entries" org-agenda-log-mode
1071 :style toggle :selected org-agenda-show-log
1072 :active (org-agenda-check-type nil 'agenda 'timeline)]
1073 ["Include Diary" org-agenda-toggle-diary
1074 :style toggle :selected org-agenda-include-diary
1075 :active (org-agenda-check-type nil 'agenda)]
1076 ["Use Time Grid" org-agenda-toggle-time-grid
1077 :style toggle :selected org-agenda-use-time-grid
1078 :active (org-agenda-check-type nil 'agenda)]
1079 ["Follow Mode" org-agenda-follow-mode
1080 :style toggle :selected org-agenda-follow-mode]
1081 "--"
1082 ["Quit" org-agenda-quit t]
1083 ["Exit and Release Buffers" org-agenda-exit t]
1084 ))))
1086 (defun org-mouse-get-gesture (event)
1087 (let ((startxy (posn-x-y (event-start event)))
1088 (endxy (posn-x-y (event-end event))))
1089 (if (< (car startxy) (car endxy)) :right :left)))
1092 ; (setq org-agenda-mode-hook nil)
1093 (defvar org-agenda-mode-map)
1094 (add-hook 'org-agenda-mode-hook
1095 #'(lambda ()
1096 (setq org-mouse-context-menu-function 'org-mouse-agenda-context-menu)
1097 (org-defkey org-agenda-mode-map [mouse-3] 'org-mouse-show-context-menu)
1098 (org-defkey org-agenda-mode-map [down-mouse-3] 'org-mouse-move-tree-start)
1099 (org-defkey org-agenda-mode-map [C-mouse-4] 'org-agenda-earlier)
1100 (org-defkey org-agenda-mode-map [C-mouse-5] 'org-agenda-later)
1101 (org-defkey org-agenda-mode-map [drag-mouse-3]
1102 #'(lambda (event) (interactive "e")
1103 (case (org-mouse-get-gesture event)
1104 (:left (org-agenda-earlier 1))
1105 (:right (org-agenda-later 1)))))))
1107 (provide 'org-mouse)
1109 ;;; org-mouse.el ends here