org-mouse.el 38 KB

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