org-mouse.el (39300B)
1 ;;; org-mouse.el --- Better mouse support for Org -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2006-2023 Free Software Foundation, Inc. 4 5 ;; Author: Piotr Zielinski <piotr dot zielinski at gmail dot com> 6 ;; Maintainer: Carsten Dominik <carsten.dominik@gmail.com> 7 8 ;; This file is part of GNU Emacs. 9 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. 14 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. 19 20 ;; You should have received a copy of the GNU General Public License 21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 22 23 ;;; Commentary: 24 ;; 25 ;; Org-mouse provides mouse support for org-mode. 26 ;; 27 ;; https://orgmode.org 28 ;; 29 ;; Org mouse implements the following features: 30 ;; * following links with the left mouse button 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 42 ;; 43 ;; Use 44 ;; --- 45 ;; 46 ;; To use this package, put the following line in your .emacs: 47 ;; 48 ;; (require 'org-mouse) 49 ;; 50 51 ;; FIXME: 52 ;; + deal with folding / unfolding issues 53 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.): 58 ;; 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 64 65 ;; Please email the maintainer with new feature suggestions / bugs 66 67 ;; History: 68 ;; 69 ;; Since version 5.10: Changes are listed in the general Org docs. 70 ;; 71 ;; Version 5.09;; + Version number synchronization with Org mode. 72 ;; 73 ;; Version 0.25 74 ;; + made compatible with Org 4.70 (thanks to Carsten for the patch) 75 ;; 76 ;; Version 0.24 77 ;; + minor changes to the table menu 78 ;; 79 ;; Version 0.23 80 ;; + preliminary support for tables and calculation marks 81 ;; + context menu support for org-agenda-undo & org-sort-entries 82 ;; 83 ;; Version 0.22 84 ;; + handles undo support for the agenda buffer (requires Org >=4.58) 85 ;; 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 89 ;; 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 94 ;; 95 ;; Version 0.19 96 ;; + added support for dragging URLs to the org-buffer 97 ;; 98 ;; Version 0.18 99 ;; + added support for agenda blocks 100 ;; 101 ;; Version 0.17 102 ;; + toggle checkboxes with a single click 103 ;; 104 ;; Version 0.16 105 ;; + added support for checkboxes 106 ;; 107 ;; Version 0.15 108 ;; + Org now works with the Agenda buffer as well 109 ;; 110 ;; Version 0.14 111 ;; + added a menu option that converts plain list items to outline items 112 ;; 113 ;; Version 0.13 114 ;; + "Insert Heading" now inserts a sibling heading if the point is 115 ;; on "***" and a child heading otherwise 116 ;; 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 121 ;; 122 ;; Version 0.11 123 ;; + fixed org-mouse-at-link (thanks to Carsten) 124 ;; + removed [follow-link] bindings 125 ;; 126 ;; Version 0.10 127 ;; + added a menu option to remove highlights 128 ;; + compatible with Org 4.21 now 129 ;; 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 134 ;; 135 ;; Versions 0.01 -- 0.07: (I don't remember) 136 137 ;;; Code: 138 139 (require 'org-macs) 140 (org-assert-version) 141 142 (require 'org) 143 (require 'cl-lib) 144 145 (defvar org-agenda-allow-remote-undo) 146 (defvar org-agenda-undo-list) 147 (defvar org-agenda-custom-commands) 148 (declare-function org-agenda-change-all-lines "org-agenda" 149 (newhead hdmarker &optional fixface just-this)) 150 (declare-function org-verify-change-for-undo "org-agenda" (l1 l2)) 151 (declare-function org-apply-on-list "org-list" (function init-value &rest args)) 152 (declare-function org-agenda-earlier "org-agenda" (arg)) 153 (declare-function org-agenda-later "org-agenda" (arg)) 154 155 (defvar org-mouse-main-buffer nil 156 "Active buffer for mouse operations.") 157 (defvar org-mouse-plain-list-regexp "\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) " 158 "Regular expression that matches a plain list.") 159 (defvar org-mouse-direct t 160 "Internal variable indicating whether the current action is direct. 161 162 If t, then the current action has been invoked directly through the buffer 163 it is intended to operate on. If nil, then the action has been invoked 164 indirectly, for example, through the agenda buffer.") 165 166 (defgroup org-mouse nil 167 "Mouse support for `org-mode'." 168 :tag "Org Mouse" 169 :group 'org) 170 171 (defcustom org-mouse-punctuation ":" 172 "Punctuation used when inserting text by drag and drop." 173 :type 'string) 174 175 (defcustom org-mouse-features 176 '(context-menu yank-link activate-stars activate-bullets activate-checkboxes) 177 "The features of org-mouse that should be activated. 178 Changing this variable requires a restart of Emacs to get activated." 179 :type '(set :greedy t 180 (const :tag "Mouse-3 shows context menu" context-menu) 181 (const :tag "C-mouse-1 and mouse-3 move trees" move-tree) 182 (const :tag "S-mouse-2 and drag-mouse-3 yank link" yank-link) 183 (const :tag "Activate headline stars" activate-stars) 184 (const :tag "Activate item bullets" activate-bullets) 185 (const :tag "Activate checkboxes" activate-checkboxes))) 186 187 (defun org-mouse-re-search-line (regexp) 188 "Search the current line for a given regular expression." 189 (beginning-of-line) 190 (re-search-forward regexp (line-end-position) t)) 191 192 (defun org-mouse-end-headline () 193 "Go to the end of current headline (ignoring tags)." 194 (interactive) 195 (end-of-line) 196 (skip-chars-backward "\t ") 197 (when (looking-back ":[A-Za-z]+:" (line-beginning-position)) 198 (skip-chars-backward ":A-Za-z") 199 (skip-chars-backward "\t "))) 200 201 (defvar-local org-mouse-context-menu-function nil 202 "Function to create the context menu. 203 The value of this variable is the function invoked by 204 `org-mouse-context-menu' as the context menu.") 205 206 (defun org-mouse-show-context-menu (event prefix) 207 "Invoke the context menu. 208 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 215 (/ (if (fboundp 'mouse-double-click-time) ; Emacs >= 29 216 (mouse-double-click-time) 217 double-click-time) 218 1000.0)))) 219 (progn 220 (select-window (posn-window (event-start event))) 221 (when (not (org-mouse-mark-active)) 222 (goto-char (posn-point (event-start event))) 223 (when (not (eolp)) (save-excursion (run-hooks 'post-command-hook))) 224 (sit-for 0)) 225 (if (functionp org-mouse-context-menu-function) 226 (funcall org-mouse-context-menu-function event) 227 (popup-menu (mouse-menu-major-mode-map) event prefix))) 228 (setq this-command 'mouse-save-then-kill) 229 (mouse-save-then-kill event))) 230 231 (defun org-mouse-line-position () 232 "Return `:beginning' or `:middle' or `:end', depending on the point position. 233 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))) 242 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]*$"))) 246 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)))) 255 256 (defun org-mouse-insert-heading () 257 "Insert a new heading, as `org-insert-heading'. 258 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 (cl-case (org-mouse-line-position) 264 (:beginning (beginning-of-line) 265 (org-insert-heading)) 266 (t (org-mouse-next-heading) 267 (org-insert-heading)))) 268 269 (defun org-mouse-timestamp-today (&optional shift units) 270 "Change the timestamp into SHIFT UNITS in the future. 271 272 For the acceptable UNITS, see `org-timestamp-change'." 273 (interactive) 274 (org-time-stamp nil) 275 (when shift (org-timestamp-change shift units))) 276 277 (defun org-mouse-keyword-menu (keywords function &optional selected itemformat) 278 "A helper function. 279 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. 283 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. 290 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 `(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)) 311 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)) 317 318 (defvar org-mouse-rest) 319 (defun org-mouse-replace-match-and-surround 320 (_newtext &optional _fixedcase _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))) 328 329 (defun org-mouse-keyword-replace-menu (keywords &optional group itemformat 330 nosurround) 331 "A helper function. 332 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). 338 339 The final entry of the menu is always \"None\", which removes the 340 match. 341 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))])))) 360 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)))) 369 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)))) 375 376 (defun org-mouse-set-priority (priority) 377 "Set the priority of the current headline to PRIORITY." 378 (org-priority priority)) 379 380 (defvar org-mouse-priority-regexp "\\[#\\([A-Z]\\)\\]" 381 "Regular expression matching the priority indicator. 382 Differs from `org-priority-regexp' in that it doesn't contain the 383 leading `.*?'.") 384 385 (defun org-mouse-get-priority (&optional default) 386 "Return the priority of the current headline. 387 DEFAULT is returned if no priority is given in the headline." 388 (save-excursion 389 (if (org-mouse-re-search-line org-mouse-priority-regexp) 390 (match-string 1) 391 (when default (char-to-string org-priority-default))))) 392 393 (defun org-mouse-delete-timestamp () 394 "Deletes the current timestamp as well as the preceding keyword. 395 SCHEDULED: or DEADLINE: or ANYTHINGLIKETHIS:" 396 (when (or (org-at-date-range-p) (org-at-timestamp-p 'lax)) 397 (replace-match "") ;delete the timestamp 398 (skip-chars-backward " :A-Z") 399 (when (looking-at " *[A-Z][A-Z]+:") 400 (replace-match "")))) 401 402 (defun org-mouse-looking-at (regexp skipchars &optional movechars) 403 (save-excursion 404 (let ((point (point))) 405 (if (looking-at regexp) t 406 (skip-chars-backward skipchars) 407 (forward-char (or movechars 0)) 408 (when (looking-at regexp) 409 (> (match-end 0) point)))))) 410 411 (defun org-mouse-priority-list () 412 (cl-loop for priority from ?A to org-priority-lowest 413 collect (char-to-string priority))) 414 415 (defun org-mouse-todo-menu (state) 416 "Create the menu with TODO keywords." 417 (append 418 (let ((kwds org-todo-keywords-1)) 419 (org-mouse-keyword-menu 420 kwds 421 #'org-todo 422 (lambda (kwd) (equal state kwd)))))) 423 424 (defun org-mouse-tag-menu () ;todo 425 "Create the tags menu." 426 (append 427 (let ((tags (org-get-tags nil t))) 428 (org-mouse-keyword-menu 429 (sort (mapcar #'car (org-get-buffer-tags)) #'string-lessp) 430 (lambda (tag) 431 (org-mouse-set-tags 432 (sort (if (member tag tags) 433 (delete tag tags) 434 (cons tag tags)) 435 #'string-lessp))) 436 (lambda (tag) (member tag tags)) 437 )) 438 '("--" 439 ["Align Tags Here" (org-align-tags) t] 440 ["Align Tags in Buffer" (org-align-tags t) t] 441 ["Set Tags ..." (org-set-tags-command) t]))) 442 443 (defun org-mouse-set-tags (tags) 444 (org-set-tags tags)) 445 446 (defun org-mouse-insert-checkbox () 447 (interactive) 448 (and (org-at-item-p) 449 (goto-char (match-end 0)) 450 (unless (org-at-item-checkbox-p) 451 (delete-horizontal-space) 452 (insert " [ ] ")))) 453 454 (defun org-mouse-agenda-type (type) 455 (pcase type 456 (`tags "Tags: ") 457 (`todo "TODO: ") 458 (`tags-tree "Tags tree: ") 459 (`todo-tree "TODO tree: ") 460 (`occur-tree "Occur tree: ") 461 (_ "Agenda command ???"))) 462 463 (defun org-mouse-list-options-menu (alloptions &optional function) 464 (let ((options (save-match-data 465 (split-string (match-string-no-properties 1))))) 466 (print options) 467 (cl-loop for name in alloptions 468 collect 469 (vector name 470 `(progn 471 (replace-match 472 (mapconcat 'identity 473 (sort (if (member ',name ',options) 474 (delete ',name ',options) 475 (cons ',name ',options)) 476 'string-lessp) 477 " ") 478 nil nil nil 1) 479 (when (functionp ',function) (funcall ',function))) 480 :style 'toggle 481 :selected (and (member name options) t))))) 482 483 (defun org-mouse-clip-text (text maxlength) 484 (if (> (length text) maxlength) 485 (concat (substring text 0 (- maxlength 3)) "...") 486 text)) 487 488 (defun org-mouse-popup-global-menu () 489 (popup-menu 490 `("Main Menu" 491 ["Show Overview" org-mouse-show-overview t] 492 ["Show Headlines" org-mouse-show-headlines t] 493 ["Show All" org-show-all t] 494 ["Remove Highlights" org-remove-occur-highlights 495 :visible org-occur-highlights] 496 "--" 497 ["Check Deadlines" 498 (if (functionp 'org-check-deadlines-and-todos) 499 (org-check-deadlines-and-todos org-deadline-warning-days) 500 (org-check-deadlines org-deadline-warning-days)) 501 t] 502 ["Check TODOs" org-show-todo-tree t] 503 ("Check Tags" 504 ,@(org-mouse-keyword-menu 505 (sort (mapcar #'car (org-get-buffer-tags)) #'string-lessp) 506 (lambda (tag) (org-tags-sparse-tree nil tag))) 507 "--" 508 ["Custom Tag ..." org-tags-sparse-tree t]) 509 ["Check Phrase ..." org-occur] 510 "--" 511 ["Display Agenda" org-agenda-list t] 512 ["Display TODO List" org-todo-list t] 513 ("Display Tags" 514 ,@(org-mouse-keyword-menu 515 (sort (mapcar #'car (org-get-buffer-tags)) #'string-lessp) 516 (lambda (tag) (org-tags-view nil tag))) 517 "--" 518 ["Custom Tag ..." org-tags-view t]) 519 ["Display Calendar" org-goto-calendar t] 520 "--" 521 ,@(org-mouse-keyword-menu 522 (mapcar #'car org-agenda-custom-commands) 523 (lambda (key) 524 (org-agenda nil (string-to-char key))) 525 nil 526 (lambda (key) 527 (let ((entry (assoc key org-agenda-custom-commands))) 528 (org-mouse-clip-text 529 (cond 530 ((stringp (nth 1 entry)) (nth 1 entry)) 531 ((stringp (nth 2 entry)) 532 (concat (org-mouse-agenda-type (nth 1 entry)) 533 (nth 2 entry))) 534 (t "Agenda Command `%s'")) 535 30)))) 536 "--" 537 ["Delete Blank Lines" delete-blank-lines 538 :visible (org-mouse-empty-line)] 539 ["Insert Checkbox" org-mouse-insert-checkbox 540 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))] 541 ["Insert Checkboxes" 542 (org-mouse-for-each-item 'org-mouse-insert-checkbox) 543 :visible (and (org-at-item-p) (not (org-at-item-checkbox-p)))] 544 ["Plain List to Outline" org-mouse-transform-to-outline 545 :visible (org-at-item-p)]))) 546 547 (defun org-mouse-get-context (contextlist context) 548 (let ((contextdata (assq context contextlist))) 549 (when contextdata 550 (save-excursion 551 (goto-char (nth 1 contextdata)) 552 (re-search-forward ".*" (nth 2 contextdata)))))) 553 554 (defun org-mouse-for-each-item (funct) 555 ;; Functions called by `org-apply-on-list' need an argument. 556 (let ((wrap-fun (lambda (_) (funcall funct)))) 557 (when (ignore-errors (goto-char (org-in-item-p))) 558 (save-excursion (org-apply-on-list wrap-fun nil))))) 559 560 (defun org-mouse-bolp () 561 "Return true if there only spaces, tabs, and `*' before point. 562 This means, between the beginning of line and the point." 563 (save-excursion 564 (skip-chars-backward " \t*") (bolp))) 565 566 (defun org-mouse-insert-item (text) 567 (cl-case (org-mouse-line-position) 568 (:beginning ; insert before 569 (beginning-of-line) 570 (looking-at "[ \t]*") 571 (open-line 1) 572 (indent-to-column (- (match-end 0) (match-beginning 0))) 573 (insert "+ ")) 574 (:middle ; insert after 575 (end-of-line) 576 (newline t) 577 (indent-relative) 578 (insert "+ ")) 579 (:end ; insert text here 580 (skip-chars-backward " \t") 581 (kill-region (point) (line-end-position)) 582 (unless (looking-back org-mouse-punctuation (line-beginning-position)) 583 (insert (concat org-mouse-punctuation " "))))) 584 (insert text) 585 (beginning-of-line)) 586 587 (advice-add 'dnd-insert-text :around #'org--mouse-dnd-insert-text) 588 (defun org--mouse-dnd-insert-text (orig-fun window action text &rest args) 589 (if (derived-mode-p 'org-mode) 590 (org-mouse-insert-item text) 591 (apply orig-fun window action text args))) 592 593 (advice-add 'dnd-open-file :around #'org--mouse-dnd-open-file) 594 (defun org--mouse-dnd-open-file (orig-fun uri &rest args) 595 (if (derived-mode-p 'org-mode) 596 (org-mouse-insert-item uri) 597 (apply orig-fun uri args))) 598 599 (defun org-mouse-match-closure (function) 600 (let ((match (match-data t))) 601 (lambda (&rest rest) 602 (save-match-data 603 (set-match-data match) 604 (apply function rest))))) 605 606 (defun org-mouse-yank-link (click) 607 (interactive "e") 608 ;; Give temporary modes such as isearch a chance to turn off. 609 (run-hooks 'mouse-leave-buffer-hook) 610 (mouse-set-point click) 611 (setq mouse-selection-click-count 0) 612 (delete-horizontal-space) 613 (insert-for-yank (concat " [[" (current-kill 0) "]] "))) 614 615 (defun org-mouse-context-menu (&optional event) 616 (let* ((stamp-prefixes (list org-deadline-string org-scheduled-string)) 617 (contextlist (org-context)) 618 (get-context (lambda (context) (org-mouse-get-context contextlist context)))) 619 (cond 620 ((org-mouse-mark-active) 621 (let ((region-string (buffer-substring (region-beginning) (region-end)))) 622 (popup-menu 623 `(nil 624 ["Sparse Tree" (org-occur ',region-string)] 625 ["Find in Buffer" (occur ',region-string)] 626 ["Grep in Current Dir" 627 (grep (format "grep -rnH -e '%s' *" ',region-string))] 628 ["Grep in Parent Dir" 629 (grep (format "grep -rnH -e '%s' ../*" ',region-string))] 630 "--" 631 ["Convert to Link" 632 (progn (save-excursion (goto-char (region-beginning)) (insert "[[")) 633 (save-excursion (goto-char (region-end)) (insert "]]")))] 634 ["Insert Link Here" (org-mouse-yank-link ',event)])))) 635 ((save-excursion (beginning-of-line) (looking-at "[ \t]*#\\+STARTUP: \\(.*\\)")) 636 (popup-menu 637 `(nil 638 ,@(org-mouse-list-options-menu (mapcar #'car org-startup-options) 639 'org-mode-restart)))) 640 ((or (eolp) 641 (and (looking-at "\\( \\|\t\\)\\(\\+:[0-9a-zA-Z_:]+\\)?\\( \\|\t\\)+$") 642 (looking-back " \\|\t" (- (point) 2) 643 (line-beginning-position)))) 644 (org-mouse-popup-global-menu)) 645 ((funcall get-context :checkbox) 646 (popup-menu 647 '(nil 648 ["Toggle" org-toggle-checkbox t] 649 ["Remove" org-mouse-remove-match-and-spaces t] 650 "" 651 ["All Clear" (org-mouse-for-each-item 652 (lambda () 653 (when (save-excursion (org-at-item-checkbox-p)) 654 (replace-match "[ ] "))))] 655 ["All Set" (org-mouse-for-each-item 656 (lambda () 657 (when (save-excursion (org-at-item-checkbox-p)) 658 (replace-match "[X] "))))] 659 ["All Toggle" (org-mouse-for-each-item 'org-toggle-checkbox) t] 660 ["All Remove" (org-mouse-for-each-item 661 (lambda () 662 (when (save-excursion (org-at-item-checkbox-p)) 663 (org-mouse-remove-match-and-spaces))))] 664 ))) 665 ((and (org-mouse-looking-at "\\b\\w+" "a-zA-Z0-9_") 666 (member (match-string 0) org-todo-keywords-1)) 667 (popup-menu 668 `(nil 669 ,@(org-mouse-todo-menu (match-string 0)) 670 "--" 671 ["Check TODOs" org-show-todo-tree t] 672 ["List all TODO keywords" org-todo-list t] 673 [,(format "List only %s" (match-string 0)) 674 (org-todo-list (match-string 0)) t] 675 ))) 676 ((and (org-mouse-looking-at "\\b[A-Z]+:" "A-Z") 677 (member (match-string 0) stamp-prefixes)) 678 (popup-menu 679 `(nil 680 ,@(org-mouse-keyword-replace-menu stamp-prefixes) 681 "--" 682 ["Check Deadlines" org-check-deadlines t] 683 ))) 684 ((org-mouse-looking-at org-mouse-priority-regexp "[]A-Z#") ; priority 685 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu 686 (org-mouse-priority-list) 1 "Priority %s" t)))) 687 ((funcall get-context :link) 688 (popup-menu 689 '(nil 690 ["Open" org-open-at-point t] 691 ["Open in Emacs" (org-open-at-point t) t] 692 "--" 693 ["Copy link" (org-kill-new (match-string 0))] 694 ["Cut link" 695 (progn 696 (kill-region (match-beginning 0) (match-end 0)) 697 (just-one-space))] 698 "--" 699 ["Grep for TODOs" 700 (grep (format "grep -nH -i 'todo\\|fixme' %s*" (match-string 2)))] 701 ; ["Paste file link" ((insert "file:") (yank))] 702 ))) 703 ((org-mouse-looking-at ":\\([A-Za-z0-9_]+\\):" "A-Za-z0-9_" -1) ;tags 704 (popup-menu 705 `(nil 706 [,(format-message "Display `%s'" (match-string 1)) 707 (org-tags-view nil ,(match-string 1))] 708 [,(format-message "Sparse Tree `%s'" (match-string 1)) 709 (org-tags-sparse-tree nil ,(match-string 1))] 710 "--" 711 ,@(org-mouse-tag-menu)))) 712 ((org-at-timestamp-p 'lax) 713 (popup-menu 714 '(nil 715 ["Show Day" org-open-at-point t] 716 ["Change Timestamp" org-time-stamp t] 717 ["Delete Timestamp" (org-mouse-delete-timestamp) t] 718 ["Compute Time Range" org-evaluate-time-range (org-at-date-range-p)] 719 "--" 720 ["Set for Today" org-mouse-timestamp-today] 721 ["Set for Tomorrow" (org-mouse-timestamp-today 1 'day)] 722 ["Set in 1 Week" (org-mouse-timestamp-today 7 'day)] 723 ["Set in 2 Weeks" (org-mouse-timestamp-today 14 'day)] 724 ["Set in a Month" (org-mouse-timestamp-today 1 'month)] 725 "--" 726 ["+ 1 Day" (org-timestamp-change 1 'day)] 727 ["+ 1 Week" (org-timestamp-change 7 'day)] 728 ["+ 1 Month" (org-timestamp-change 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 ((funcall get-context :table-special) 734 (let ((mdata (match-data))) 735 (cl-incf (car mdata) 2) 736 (store-match-data mdata)) 737 (message "match: %S" (match-string 0)) 738 (popup-menu `(nil ,@(org-mouse-keyword-replace-menu 739 '(" " "!" "^" "_" "$" "#" "*" "'") 0 740 (lambda (mark) 741 (cl-case (string-to-char mark) 742 (? "( ) Nothing Special") 743 (?! "(!) Column Names") 744 (?^ "(^) Field Names Above") 745 (?_ "(^) Field Names Below") 746 (?$ "($) Formula Parameters") 747 (?# "(#) Recalculation: Auto") 748 (?* "(*) Recalculation: Manual") 749 (?' "(') Recalculation: None"))) 750 t)))) 751 ((assq :table contextlist) 752 (popup-menu 753 '(nil 754 ["Align Table" org-ctrl-c-ctrl-c] 755 ["Blank Field" org-table-blank-field] 756 ["Edit Field" org-table-edit-field] 757 "--" 758 ("Column" 759 ["Move Column Left" org-metaleft] 760 ["Move Column Right" org-metaright] 761 ["Delete Column" org-shiftmetaleft] 762 ["Insert Column" org-shiftmetaright] 763 "--" 764 ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :selected org-table-limit-column-width :style toggle]) 765 ("Row" 766 ["Move Row Up" org-metaup] 767 ["Move Row Down" org-metadown] 768 ["Delete Row" org-shiftmetaup] 769 ["Insert Row" org-shiftmetadown] 770 ["Sort lines in region" org-table-sort-lines (org-at-table-p)] 771 "--" 772 ["Insert Hline" org-table-insert-hline]) 773 ("Rectangle" 774 ["Copy Rectangle" org-copy-special] 775 ["Cut Rectangle" org-cut-special] 776 ["Paste Rectangle" org-paste-special] 777 ["Fill Rectangle" org-table-wrap-region]) 778 "--" 779 ["Set Column Formula" org-table-eval-formula] 780 ["Set Field Formula" (org-table-eval-formula '(4))] 781 ["Edit Formulas" org-table-edit-formulas] 782 "--" 783 ["Recalculate Line" org-table-recalculate] 784 ["Recalculate All" (org-table-recalculate '(4))] 785 ["Iterate All" (org-table-recalculate '(16))] 786 "--" 787 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks] 788 ["Sum Column/Rectangle" org-table-sum 789 :active (or (org-at-table-p) (org-region-active-p))] 790 ["Field Info" org-table-field-info] 791 ["Debug Formulas" 792 (setq org-table-formula-debug (not org-table-formula-debug)) 793 :style toggle :selected org-table-formula-debug] 794 ))) 795 ((and (assq :headline contextlist) (not (eolp))) 796 (let ((priority (org-mouse-get-priority t))) 797 (popup-menu 798 `("Headline Menu" 799 ("Tags and Priorities" 800 ,@(org-mouse-keyword-menu 801 (org-mouse-priority-list) 802 (lambda (keyword) 803 (org-mouse-set-priority (string-to-char keyword))) 804 priority "Priority %s") 805 "--" 806 ,@(org-mouse-tag-menu)) 807 ("TODO Status" 808 ,@(org-mouse-todo-menu (org-get-todo-state))) 809 ["Show Tags" 810 (with-current-buffer org-mouse-main-buffer (org-agenda-show-tags)) 811 :visible (not org-mouse-direct)] 812 ["Show Priority" 813 (with-current-buffer org-mouse-main-buffer (org-agenda-show-priority)) 814 :visible (not org-mouse-direct)] 815 ,@(if org-mouse-direct '("--") nil) 816 ["New Heading" org-mouse-insert-heading :visible org-mouse-direct] 817 ["Set Deadline" 818 (progn (org-mouse-end-headline) (insert " ") (org-deadline)) 819 :active (not (save-excursion 820 (org-mouse-re-search-line org-deadline-regexp)))] 821 ["Schedule Task" 822 (progn (org-mouse-end-headline) (insert " ") (org-schedule)) 823 :active (not (save-excursion 824 (org-mouse-re-search-line org-scheduled-regexp)))] 825 ["Insert Timestamp" 826 (progn (org-mouse-end-headline) (insert " ") (org-time-stamp nil)) t] 827 ; ["Timestamp (inactive)" org-time-stamp-inactive t] 828 "--" 829 ["Archive Subtree" org-archive-subtree] 830 ["Cut Subtree" org-cut-special] 831 ["Copy Subtree" org-copy-special] 832 ["Paste Subtree" org-paste-special :visible org-mouse-direct] 833 ("Sort Children" 834 ["Alphabetically" (org-sort-entries nil ?a)] 835 ["Numerically" (org-sort-entries nil ?n)] 836 ["By Time/Date" (org-sort-entries nil ?t)] 837 "--" 838 ["Reverse Alphabetically" (org-sort-entries nil ?A)] 839 ["Reverse Numerically" (org-sort-entries nil ?N)] 840 ["Reverse By Time/Date" (org-sort-entries nil ?T)]) 841 "--" 842 ["Move Trees" org-mouse-move-tree :active nil] 843 )))) 844 (t 845 (org-mouse-popup-global-menu))))) 846 847 (defun org-mouse-mark-active () 848 (and mark-active transient-mark-mode)) 849 850 (defun org-mouse-in-region-p (pos) 851 (and (org-mouse-mark-active) 852 (>= pos (region-beginning)) 853 (< pos (region-end)))) 854 855 (defun org-mouse-down-mouse (event) 856 (interactive "e") 857 (setq this-command last-command) 858 (unless (and (= 1 (event-click-count event)) 859 (org-mouse-in-region-p (posn-point (event-start event)))) 860 (mouse-drag-region event))) 861 862 (add-hook 'org-mode-hook 863 (lambda () 864 (setq org-mouse-context-menu-function #'org-mouse-context-menu) 865 866 (when (memq 'context-menu org-mouse-features) 867 (org-defkey org-mouse-map [mouse-3] nil) 868 (org-defkey org-mode-map [mouse-3] #'org-mouse-show-context-menu)) 869 (org-defkey org-mode-map [down-mouse-1] #'org-mouse-down-mouse) 870 (when (memq 'context-menu org-mouse-features) 871 (org-defkey org-mouse-map [C-drag-mouse-1] #'org-mouse-move-tree) 872 (org-defkey org-mouse-map [C-down-mouse-1] #'org-mouse-move-tree-start)) 873 (when (memq 'yank-link org-mouse-features) 874 (org-defkey org-mode-map [S-mouse-2] #'org-mouse-yank-link) 875 (org-defkey org-mode-map [drag-mouse-3] #'org-mouse-yank-link)) 876 (when (memq 'move-tree org-mouse-features) 877 (org-defkey org-mouse-map [drag-mouse-3] #'org-mouse-move-tree) 878 (org-defkey org-mouse-map [down-mouse-3] #'org-mouse-move-tree-start)) 879 880 (when (memq 'activate-stars org-mouse-features) 881 (font-lock-add-keywords 882 nil 883 `((,org-outline-regexp 884 0 `(face org-link mouse-face highlight keymap ,org-mouse-map) 885 'prepend)) 886 t)) 887 888 (when (memq 'activate-bullets org-mouse-features) 889 (font-lock-add-keywords 890 nil 891 `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +" 892 (1 `(face org-link keymap ,org-mouse-map mouse-face highlight) 893 'prepend))) 894 t)) 895 896 (when (memq 'activate-checkboxes org-mouse-features) 897 (font-lock-add-keywords 898 nil 899 `(("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)" 900 (1 `(face nil keymap ,org-mouse-map mouse-face highlight) prepend))) 901 t)) 902 903 (advice-add 'org-open-at-point :around #'org--mouse-open-at-point))) 904 905 (defun org--mouse-open-at-point (orig-fun &rest args) 906 (let ((context (org-context))) 907 (cond 908 ((assq :headline-stars context) (org-cycle)) 909 ((assq :checkbox context) (org-toggle-checkbox)) 910 ((assq :item-bullet context) 911 (let ((org-cycle-include-plain-lists t)) (org-cycle))) 912 ((org-footnote-at-reference-p) nil) 913 (t (apply orig-fun args))))) 914 915 (defun org-mouse-move-tree-start (_event) 916 (interactive "e") 917 (message "Same line: promote/demote, (***):move before, (text): make a child")) 918 919 920 (defun org-mouse-make-marker (position) 921 (with-current-buffer (window-buffer (posn-window position)) 922 (copy-marker (posn-point position)))) 923 924 (defun org-mouse-move-tree (event) 925 ;; todo: handle movements between different buffers 926 (interactive "e") 927 (save-excursion 928 (let* ((start (org-mouse-make-marker (event-start event))) 929 (end (org-mouse-make-marker (event-end event))) 930 (sbuf (marker-buffer start)) 931 (ebuf (marker-buffer end))) 932 933 (when (and sbuf ebuf) 934 (set-buffer sbuf) 935 (goto-char start) 936 (org-back-to-heading) 937 (if (and (eq sbuf ebuf) 938 (equal 939 (point) 940 (save-excursion (goto-char end) (org-back-to-heading) (point)))) 941 ;; if the same line then promote/demote 942 (if (>= end start) (org-demote-subtree) (org-promote-subtree)) 943 ;; if different lines then move 944 (org-cut-subtree) 945 946 (set-buffer ebuf) 947 (goto-char end) 948 (org-back-to-heading) 949 (when (and (eq sbuf ebuf) 950 (equal 951 (point) 952 (save-excursion (goto-char start) 953 (org-back-to-heading) (point)))) 954 (progn (org-end-of-subtree nil t) 955 (unless (eobp) (backward-char))) 956 (end-of-line) 957 (if (eobp) (newline) (forward-char))) 958 959 (when (looking-at org-outline-regexp) 960 (let ((level (- (match-end 0) (match-beginning 0)))) 961 (when (> end (match-end 0)) 962 (progn (org-end-of-subtree nil t) 963 (unless (eobp) (backward-char))) 964 (end-of-line) 965 (if (eobp) (newline) (forward-char)) 966 (setq level (1+ level))) 967 (org-paste-subtree level) 968 (save-excursion 969 (progn (org-end-of-subtree nil t) 970 (unless (eobp) (backward-char))) 971 (when (bolp) (delete-char -1)))))))))) 972 973 974 (defun org-mouse-transform-to-outline () 975 (interactive) 976 (org-back-to-heading) 977 (let ((minlevel 1000) 978 (replace-text (concat (match-string 0) "* "))) 979 (beginning-of-line 2) 980 (save-excursion 981 (while (not (or (eobp) (looking-at org-outline-regexp))) 982 (when (looking-at org-mouse-plain-list-regexp) 983 (setq minlevel (min minlevel (- (match-end 1) (match-beginning 1))))) 984 (forward-line))) 985 (while (not (or (eobp) (looking-at org-outline-regexp))) 986 (when (and (looking-at org-mouse-plain-list-regexp) 987 (eq minlevel (- (match-end 1) (match-beginning 1)))) 988 (replace-match replace-text)) 989 (forward-line)))) 990 991 (defvar org-mouse-cmd) ;dynamically scoped from `org-with-remote-undo'. 992 993 (defun org-mouse-do-remotely (command) 994 ;; (org-agenda-check-no-diary) 995 (when (get-text-property (point) 'org-marker) 996 (let* ((anticol (- (line-end-position) (point))) 997 (marker (get-text-property (point) 'org-marker)) 998 (buffer (marker-buffer marker)) 999 (pos (marker-position marker)) 1000 (hdmarker (get-text-property (point) 'org-hd-marker)) 1001 (buffer-read-only nil) 1002 (newhead "--- removed ---") 1003 (org-mouse-direct nil) 1004 (org-mouse-main-buffer (current-buffer))) 1005 (when (eq (with-current-buffer buffer major-mode) 'org-mode) 1006 (let ((endmarker (with-current-buffer buffer 1007 (org-end-of-subtree nil t) 1008 (unless (eobp) (forward-char 1)) 1009 (point-marker)))) 1010 (org-with-remote-undo buffer 1011 (with-current-buffer buffer 1012 (widen) 1013 (goto-char pos) 1014 (org-fold-show-hidden-entry) 1015 (save-excursion 1016 (and (outline-next-heading) 1017 (org-fold-heading nil))) ; show the next heading 1018 (org-back-to-heading) 1019 (setq marker (point-marker)) 1020 (goto-char (max (line-beginning-position) (- (line-end-position) anticol))) 1021 (funcall command) 1022 (message "_cmd: %S" org-mouse-cmd) 1023 (message "this-command: %S" this-command) 1024 (unless (eq (marker-position marker) (marker-position endmarker)) 1025 (setq newhead (org-get-heading)))) 1026 1027 (beginning-of-line 1) 1028 (save-excursion 1029 (org-agenda-change-all-lines newhead hdmarker 'fixface)))) 1030 t)))) 1031 1032 (defun org-mouse-agenda-context-menu (&optional _event) 1033 (or (org-mouse-do-remotely 'org-mouse-context-menu) 1034 (popup-menu 1035 '("Agenda" 1036 ("Agenda Files") 1037 "--" 1038 ["Undo" (progn (message "last command: %S" last-command) (setq this-command 'org-agenda-undo) (org-agenda-undo)) 1039 :visible (if (eq last-command 'org-agenda-undo) 1040 org-agenda-pending-undo-list 1041 org-agenda-undo-list)] 1042 ["Rebuild Buffer" org-agenda-redo t] 1043 ["New Diary Entry" 1044 org-agenda-diary-entry (org-agenda-check-type nil 'agenda) t] 1045 "--" 1046 ["Goto Today" org-agenda-goto-today 1047 (org-agenda-check-type nil 'agenda) t] 1048 ["Display Calendar" org-agenda-goto-calendar 1049 (org-agenda-check-type nil 'agenda) t] 1050 ("Calendar Commands" 1051 ["Phases of the Moon" org-agenda-phases-of-moon 1052 (org-agenda-check-type nil 'agenda)] 1053 ["Sunrise/Sunset" org-agenda-sunrise-sunset 1054 (org-agenda-check-type nil 'agenda)] 1055 ["Holidays" org-agenda-holidays 1056 (org-agenda-check-type nil 'agenda)] 1057 ["Convert" org-agenda-convert-date 1058 (org-agenda-check-type nil 'agenda)] 1059 "--" 1060 ["Create iCalendar file" org-icalendar-combine-agenda-files t]) 1061 "--" 1062 ["Day View" org-agenda-day-view 1063 :active (org-agenda-check-type nil 'agenda) 1064 :style radio :selected (eq org-agenda-current-span 'day)] 1065 ["Week View" org-agenda-week-view 1066 :active (org-agenda-check-type nil 'agenda) 1067 :style radio :selected (eq org-agenda-current-span 'week)] 1068 "--" 1069 ["Show Logbook entries" org-agenda-log-mode 1070 :style toggle :selected org-agenda-show-log 1071 :active (org-agenda-check-type nil 'agenda)] 1072 ["Include Diary" org-agenda-toggle-diary 1073 :style toggle :selected org-agenda-include-diary 1074 :active (org-agenda-check-type nil 'agenda)] 1075 ["Use Time Grid" org-agenda-toggle-time-grid 1076 :style toggle :selected org-agenda-use-time-grid 1077 :active (org-agenda-check-type nil 'agenda)] 1078 ["Follow Mode" org-agenda-follow-mode 1079 :style toggle :selected org-agenda-follow-mode] 1080 "--" 1081 ["Quit" org-agenda-quit t] 1082 ["Exit and Release Buffers" org-agenda-exit t] 1083 )))) 1084 1085 (defun org-mouse-get-gesture (event) 1086 (let ((startxy (posn-x-y (event-start event))) 1087 (endxy (posn-x-y (event-end event)))) 1088 (if (< (car startxy) (car endxy)) :right :left))) 1089 1090 1091 ; (setq org-agenda-mode-hook nil) 1092 (defvar org-agenda-mode-map) 1093 (add-hook 'org-agenda-mode-hook 1094 (lambda () 1095 (setq org-mouse-context-menu-function #'org-mouse-agenda-context-menu) 1096 (org-defkey org-agenda-mode-map [mouse-3] #'org-mouse-show-context-menu) 1097 (org-defkey org-agenda-mode-map [down-mouse-3] #'org-mouse-move-tree-start) 1098 (org-defkey org-agenda-mode-map [C-mouse-4] #'org-agenda-earlier) 1099 (org-defkey org-agenda-mode-map [C-mouse-5] #'org-agenda-later) 1100 (org-defkey org-agenda-mode-map [drag-mouse-3] 1101 (lambda (event) (interactive "e") 1102 (cl-case (org-mouse-get-gesture event) 1103 (:left (org-agenda-earlier 1)) 1104 (:right (org-agenda-later 1))))))) 1105 1106 (provide 'org-mouse) 1107 1108 ;;; org-mouse.el ends here