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