org-mouse.el 34 KB

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