org-colview.el 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. ;;; org-colview.el --- Column View in Org-mode
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains the column view for Org.
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'org)
  26. (declare-function org-agenda-redo "org-agenda" ())
  27. (declare-function org-agenda-do-context-action "org-agenda" ())
  28. (when (featurep 'xemacs)
  29. (error "Do not load this file into XEmacs, use 'org-colview-xemacs.el'."))
  30. ;;; Column View
  31. (defvar org-columns-overlays nil
  32. "Holds the list of current column overlays.")
  33. (defvar org-columns-current-fmt nil
  34. "Local variable, holds the currently active column format.")
  35. (make-variable-buffer-local 'org-columns-current-fmt)
  36. (defvar org-columns-current-fmt-compiled nil
  37. "Local variable, holds the currently active column format.
  38. This is the compiled version of the format.")
  39. (make-variable-buffer-local 'org-columns-current-fmt-compiled)
  40. (defvar org-columns-current-widths nil
  41. "Loval variable, holds the currently widths of fields.")
  42. (make-variable-buffer-local 'org-columns-current-widths)
  43. (defvar org-columns-current-maxwidths nil
  44. "Loval variable, holds the currently active maximum column widths.")
  45. (make-variable-buffer-local 'org-columns-current-maxwidths)
  46. (defvar org-columns-begin-marker (make-marker)
  47. "Points to the position where last a column creation command was called.")
  48. (defvar org-columns-top-level-marker (make-marker)
  49. "Points to the position where current columns region starts.")
  50. (defvar org-columns-map (make-sparse-keymap)
  51. "The keymap valid in column display.")
  52. (defun org-columns-content ()
  53. "Switch to contents view while in columns view."
  54. (interactive)
  55. (org-overview)
  56. (org-content))
  57. (org-defkey org-columns-map "c" 'org-columns-content)
  58. (org-defkey org-columns-map "o" 'org-overview)
  59. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  60. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  61. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  62. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  63. (org-defkey org-columns-map "v" 'org-columns-show-value)
  64. (org-defkey org-columns-map "q" 'org-columns-quit)
  65. (org-defkey org-columns-map "r" 'org-columns-redo)
  66. (org-defkey org-columns-map "g" 'org-columns-redo)
  67. (org-defkey org-columns-map [left] 'backward-char)
  68. (org-defkey org-columns-map "\M-b" 'backward-char)
  69. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  70. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  71. (org-defkey org-columns-map "\M-f"
  72. (lambda () (interactive) (goto-char (1+ (point)))))
  73. (org-defkey org-columns-map [right]
  74. (lambda () (interactive) (goto-char (1+ (point)))))
  75. (org-defkey org-columns-map [down]
  76. (lambda () (interactive)
  77. (let ((col (current-column)))
  78. (beginning-of-line 2)
  79. (while (and (org-invisible-p2) (not (eobp)))
  80. (beginning-of-line 2))
  81. (move-to-column col)
  82. (if (eq major-mode 'org-agenda-mode)
  83. (org-agenda-do-context-action)))))
  84. (org-defkey org-columns-map [up]
  85. (lambda () (interactive)
  86. (let ((col (current-column)))
  87. (beginning-of-line 0)
  88. (while (and (org-invisible-p2) (not (bobp)))
  89. (beginning-of-line 0))
  90. (move-to-column col)
  91. (if (eq major-mode 'org-agenda-mode)
  92. (org-agenda-do-context-action)))))
  93. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  94. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  95. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  96. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  97. (org-defkey org-columns-map "<" 'org-columns-narrow)
  98. (org-defkey org-columns-map ">" 'org-columns-widen)
  99. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  100. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  101. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  102. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  103. (dotimes (i 10)
  104. (org-defkey org-columns-map (number-to-string i)
  105. `(lambda () (interactive)
  106. (org-columns-next-allowed-value nil ,i))))
  107. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  108. '("Column"
  109. ["Edit property" org-columns-edit-value t]
  110. ["Next allowed value" org-columns-next-allowed-value t]
  111. ["Previous allowed value" org-columns-previous-allowed-value t]
  112. ["Show full value" org-columns-show-value t]
  113. ["Edit allowed values" org-columns-edit-allowed t]
  114. "--"
  115. ["Edit column attributes" org-columns-edit-attributes t]
  116. ["Increase column width" org-columns-widen t]
  117. ["Decrease column width" org-columns-narrow t]
  118. "--"
  119. ["Move column right" org-columns-move-right t]
  120. ["Move column left" org-columns-move-left t]
  121. ["Add column" org-columns-new t]
  122. ["Delete column" org-columns-delete t]
  123. "--"
  124. ["CONTENTS" org-columns-content t]
  125. ["OVERVIEW" org-overview t]
  126. ["Refresh columns display" org-columns-redo t]
  127. "--"
  128. ["Open link" org-columns-open-link t]
  129. "--"
  130. ["Quit" org-columns-quit t]))
  131. (defun org-columns-new-overlay (beg end &optional string face)
  132. "Create a new column overlay and add it to the list."
  133. (let ((ov (make-overlay beg end)))
  134. (overlay-put ov 'face (or face 'secondary-selection))
  135. (org-overlay-display ov string face)
  136. (push ov org-columns-overlays)
  137. ov))
  138. (defun org-columns-display-here (&optional props dateline)
  139. "Overlay the current line with column display."
  140. (interactive)
  141. (let* ((fmt org-columns-current-fmt-compiled)
  142. (beg (point-at-bol))
  143. (level-face (save-excursion
  144. (beginning-of-line 1)
  145. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  146. (org-get-level-face 2))))
  147. (ref-face (or level-face
  148. (and (eq major-mode 'org-agenda-mode)
  149. (get-text-property (point-at-bol) 'face))
  150. 'default))
  151. (color (list :foreground (face-attribute ref-face :foreground)))
  152. (face (list color 'org-column ref-face))
  153. (face1 (list color 'org-agenda-column-dateline ref-face))
  154. (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
  155. pom property ass width f string ov column val modval s2 title calc)
  156. ;; Check if the entry is in another buffer.
  157. (unless props
  158. (if (eq major-mode 'org-agenda-mode)
  159. (setq pom (or (org-get-at-bol 'org-hd-marker)
  160. (org-get-at-bol 'org-marker))
  161. props (if pom (org-entry-properties pom) nil))
  162. (setq props (org-entry-properties nil))))
  163. ;; Walk the format
  164. (while (setq column (pop fmt))
  165. (setq property (car column)
  166. title (nth 1 column)
  167. ass (if (equal property "ITEM")
  168. (cons "ITEM"
  169. ;; When in a buffer, get the whole line,
  170. ;; we'll clean it later…
  171. (if (eq major-mode 'org-mode)
  172. (save-match-data
  173. (org-no-properties
  174. (org-remove-tabs
  175. (buffer-substring-no-properties
  176. (point-at-bol) (point-at-eol)))))
  177. ;; In agenda, just get the `txt' property
  178. (org-no-properties
  179. (org-get-at-bol 'txt))))
  180. (assoc property props))
  181. width (or (cdr (assoc property org-columns-current-maxwidths))
  182. (nth 2 column)
  183. (length property))
  184. f (format "%%-%d.%ds | " width width)
  185. calc (nth 7 column)
  186. val (or (cdr ass) "")
  187. modval (cond ((and org-columns-modify-value-for-display-function
  188. (functionp
  189. org-columns-modify-value-for-display-function))
  190. (funcall org-columns-modify-value-for-display-function
  191. title val))
  192. ((equal property "ITEM")
  193. (org-columns-cleanup-item
  194. val org-columns-current-fmt-compiled
  195. (or org-complex-heading-regexp cphr)))
  196. ((and calc (functionp calc)
  197. (not (string= val ""))
  198. (not (get-text-property 0 'org-computed val)))
  199. (org-columns-number-to-string
  200. (funcall calc (org-columns-string-to-number
  201. val (nth 4 column)))
  202. (nth 4 column)))))
  203. (setq s2 (org-columns-add-ellipses (or modval val) width))
  204. (setq string (format f s2))
  205. ;; Create the overlay
  206. (org-unmodified
  207. (setq ov (org-columns-new-overlay
  208. beg (setq beg (1+ beg)) string (if dateline face1 face)))
  209. (overlay-put ov 'keymap org-columns-map)
  210. (overlay-put ov 'org-columns-key property)
  211. (overlay-put ov 'org-columns-value (cdr ass))
  212. (overlay-put ov 'org-columns-value-modified modval)
  213. (overlay-put ov 'org-columns-pom pom)
  214. (overlay-put ov 'org-columns-format f)
  215. (overlay-put ov 'line-prefix "")
  216. (overlay-put ov 'wrap-prefix ""))
  217. (if (or (not (char-after beg))
  218. (equal (char-after beg) ?\n))
  219. (let ((inhibit-read-only t))
  220. (save-excursion
  221. (goto-char beg)
  222. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  223. ;; Make the rest of the line disappear.
  224. (org-unmodified
  225. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  226. (overlay-put ov 'invisible t)
  227. (overlay-put ov 'keymap org-columns-map)
  228. (overlay-put ov 'intangible t)
  229. (overlay-put ov 'line-prefix "")
  230. (overlay-put ov 'wrap-prefix "")
  231. (push ov org-columns-overlays)
  232. (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  233. (overlay-put ov 'keymap org-columns-map)
  234. (push ov org-columns-overlays)
  235. (let ((inhibit-read-only t))
  236. (put-text-property (max (point-min) (1- (point-at-bol)))
  237. (min (point-max) (1+ (point-at-eol)))
  238. 'read-only "Type `e' to edit property")))))
  239. (defun org-columns-add-ellipses (string width)
  240. "Truncate STRING with WIDTH characters, with ellipses."
  241. (cond
  242. ((<= (length string) width) string)
  243. ((<= width (length org-columns-ellipses))
  244. (substring org-columns-ellipses 0 width))
  245. (t (concat (substring string 0 (- width (length org-columns-ellipses)))
  246. org-columns-ellipses))))
  247. (defvar org-columns-full-header-line-format nil
  248. "The full header line format, will be shifted by horizontal scrolling." )
  249. (defvar org-previous-header-line-format nil
  250. "The header line format before column view was turned on.")
  251. (defvar org-columns-inhibit-recalculation nil
  252. "Inhibit recomputing of columns on column view startup.")
  253. (defvar org-columns-flyspell-was-active nil
  254. "Remember the state of `flyspell-mode' before column view.
  255. Flyspell-mode can cause problems in columns view, so it is turned off
  256. for the duration of the command.")
  257. (defvar header-line-format)
  258. (defvar org-columns-previous-hscroll 0)
  259. (defun org-columns-display-here-title ()
  260. "Overlay the newline before the current line with the table title."
  261. (interactive)
  262. (let ((fmt org-columns-current-fmt-compiled)
  263. string (title "")
  264. property width f column str widths)
  265. (while (setq column (pop fmt))
  266. (setq property (car column)
  267. str (or (nth 1 column) property)
  268. width (or (cdr (assoc property org-columns-current-maxwidths))
  269. (nth 2 column)
  270. (length str))
  271. widths (push width widths)
  272. f (format "%%-%d.%ds | " width width)
  273. string (format f str)
  274. title (concat title string)))
  275. (setq title (concat
  276. (org-add-props " " nil 'display '(space :align-to 0))
  277. ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  278. (org-add-props title nil 'face 'org-column-title)))
  279. (org-set-local 'org-previous-header-line-format header-line-format)
  280. (org-set-local 'org-columns-current-widths (nreverse widths))
  281. (setq org-columns-full-header-line-format title)
  282. (setq org-columns-previous-hscroll -1)
  283. ; (org-columns-hscoll-title)
  284. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  285. (defun org-columns-hscoll-title ()
  286. "Set the `header-line-format' so that it scrolls along with the table."
  287. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  288. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  289. (setq header-line-format
  290. (concat (substring org-columns-full-header-line-format 0 1)
  291. (substring org-columns-full-header-line-format
  292. (1+ (window-hscroll))))
  293. org-columns-previous-hscroll (window-hscroll))
  294. (force-mode-line-update)))
  295. (defvar org-colview-initial-truncate-line-value nil
  296. "Remember the value of `truncate-lines' across colview.")
  297. (defun org-columns-remove-overlays ()
  298. "Remove all currently active column overlays."
  299. (interactive)
  300. (when (marker-buffer org-columns-begin-marker)
  301. (with-current-buffer (marker-buffer org-columns-begin-marker)
  302. (when (local-variable-p 'org-previous-header-line-format)
  303. (setq header-line-format org-previous-header-line-format)
  304. (kill-local-variable 'org-previous-header-line-format)
  305. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  306. (move-marker org-columns-begin-marker nil)
  307. (move-marker org-columns-top-level-marker nil)
  308. (org-unmodified
  309. (mapc 'delete-overlay org-columns-overlays)
  310. (setq org-columns-overlays nil)
  311. (let ((inhibit-read-only t))
  312. (remove-text-properties (point-min) (point-max) '(read-only t))))
  313. (when org-columns-flyspell-was-active
  314. (flyspell-mode 1))
  315. (when (local-variable-p 'org-colview-initial-truncate-line-value)
  316. (setq truncate-lines org-colview-initial-truncate-line-value)))))
  317. (defun org-columns-cleanup-item (item fmt cphr)
  318. "Remove from ITEM what is a column in the format FMT.
  319. CPHR is the complex heading regexp to use for parsing ITEM."
  320. (let (fixitem)
  321. (if (not cphr)
  322. item
  323. (unless (string-match "^\*+ " item)
  324. (setq item (concat "* " item) fixitem t))
  325. (if (string-match cphr item)
  326. (setq item
  327. (concat
  328. (org-add-props (match-string 1 item) nil
  329. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  330. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  331. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  332. " " (save-match-data (org-columns-compact-links (match-string 4 item)))
  333. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
  334. (add-text-properties
  335. 0 (1+ (match-end 1))
  336. (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  337. item))
  338. (if fixitem (replace-regexp-in-string "^\*+ " "" item) item))))
  339. (defun org-columns-compact-links (s)
  340. "Replace [[link][desc]] with [desc] or [link]."
  341. (while (string-match org-bracket-link-regexp s)
  342. (setq s (replace-match
  343. (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
  344. t t s)))
  345. s)
  346. (defun org-columns-show-value ()
  347. "Show the full value of the property."
  348. (interactive)
  349. (let ((value (get-char-property (point) 'org-columns-value)))
  350. (message "Value is: %s" (or value ""))))
  351. (defvar org-agenda-columns-active) ;; defined in org-agenda.el
  352. (defun org-columns-quit ()
  353. "Remove the column overlays and in this way exit column editing."
  354. (interactive)
  355. (org-unmodified
  356. (org-columns-remove-overlays)
  357. (let ((inhibit-read-only t))
  358. (remove-text-properties (point-min) (point-max) '(read-only t))))
  359. (when (eq major-mode 'org-agenda-mode)
  360. (setq org-agenda-columns-active nil)
  361. (message
  362. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  363. (defun org-columns-check-computed ()
  364. "Check if this column value is computed.
  365. If yes, throw an error indicating that changing it does not make sense."
  366. (let ((val (get-char-property (point) 'org-columns-value)))
  367. (when (and (stringp val)
  368. (get-char-property 0 'org-computed val))
  369. (error "This value is computed from the entry's children"))))
  370. (defun org-columns-todo (&optional arg)
  371. "Change the TODO state during column view."
  372. (interactive "P")
  373. (org-columns-edit-value "TODO"))
  374. (defun org-columns-set-tags-or-toggle (&optional arg)
  375. "Toggle checkbox at point, or set tags for current headline."
  376. (interactive "P")
  377. (if (string-match "\\`\\[[ xX-]\\]\\'"
  378. (get-char-property (point) 'org-columns-value))
  379. (org-columns-next-allowed-value)
  380. (org-columns-edit-value "TAGS")))
  381. (defun org-columns-edit-value (&optional key)
  382. "Edit the value of the property at point in column view.
  383. Where possible, use the standard interface for changing this line."
  384. (interactive)
  385. (org-columns-check-computed)
  386. (let* ((col (current-column))
  387. (key (or key (get-char-property (point) 'org-columns-key)))
  388. (value (get-char-property (point) 'org-columns-value))
  389. (bol (point-at-bol)) (eol (point-at-eol))
  390. (pom (or (get-text-property bol 'org-hd-marker)
  391. (point))) ; keep despite of compiler waring
  392. (line-overlays
  393. (delq nil (mapcar (lambda (x)
  394. (and (eq (overlay-buffer x) (current-buffer))
  395. (>= (overlay-start x) bol)
  396. (<= (overlay-start x) eol)
  397. x))
  398. org-columns-overlays)))
  399. (org-columns-time (time-to-number-of-days (current-time)))
  400. nval eval allowed)
  401. (cond
  402. ((equal key "CLOCKSUM")
  403. (error "This special column cannot be edited"))
  404. ((equal key "ITEM")
  405. (setq eval '(org-with-point-at pom
  406. (org-edit-headline))))
  407. ((equal key "TODO")
  408. (setq eval '(org-with-point-at
  409. pom
  410. (call-interactively 'org-todo))))
  411. ((equal key "PRIORITY")
  412. (setq eval '(org-with-point-at pom
  413. (call-interactively 'org-priority))))
  414. ((equal key "TAGS")
  415. (setq eval '(org-with-point-at pom
  416. (let ((org-fast-tag-selection-single-key
  417. (if (eq org-fast-tag-selection-single-key 'expert)
  418. t org-fast-tag-selection-single-key)))
  419. (call-interactively 'org-set-tags)))))
  420. ((equal key "DEADLINE")
  421. (setq eval '(org-with-point-at pom
  422. (call-interactively 'org-deadline))))
  423. ((equal key "SCHEDULED")
  424. (setq eval '(org-with-point-at pom
  425. (call-interactively 'org-schedule))))
  426. ((equal key "BEAMER_env")
  427. (setq eval '(org-with-point-at pom
  428. (call-interactively 'org-beamer-select-environment))))
  429. (t
  430. (setq allowed (org-property-get-allowed-values pom key 'table))
  431. (if allowed
  432. (setq nval (org-icompleting-read
  433. "Value: " allowed nil
  434. (not (get-text-property 0 'org-unrestricted
  435. (caar allowed)))))
  436. (setq nval (read-string "Edit: " value)))
  437. (setq nval (org-trim nval))
  438. (when (not (equal nval value))
  439. (setq eval '(org-entry-put pom key nval)))))
  440. (when eval
  441. (cond
  442. ((equal major-mode 'org-agenda-mode)
  443. (org-columns-eval eval)
  444. ;; The following let preserves the current format, and makes sure
  445. ;; that in only a single file things need to be updated.
  446. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  447. (buffer (marker-buffer pom))
  448. (org-agenda-contributing-files
  449. (list (with-current-buffer buffer
  450. (buffer-file-name (buffer-base-buffer))))))
  451. (org-agenda-columns)))
  452. (t
  453. (let ((inhibit-read-only t))
  454. (org-unmodified
  455. (remove-text-properties
  456. (max (point-min) (1- bol)) eol '(read-only t)))
  457. (unwind-protect
  458. (progn
  459. (setq org-columns-overlays
  460. (org-delete-all line-overlays org-columns-overlays))
  461. (mapc 'delete-overlay line-overlays)
  462. (org-columns-eval eval))
  463. (org-columns-display-here)))
  464. (org-move-to-column col)
  465. (if (and (eq major-mode 'org-mode)
  466. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  467. (org-columns-update key)))))))
  468. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  469. "Edit the current headline, the part without TODO keyword, TAGS."
  470. (org-back-to-heading)
  471. (when (looking-at org-todo-line-regexp)
  472. (let ((pos (point))
  473. (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  474. (txt (match-string 3))
  475. (post "")
  476. txt2)
  477. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
  478. (setq post (match-string 0 txt)
  479. txt (substring txt 0 (match-beginning 0))))
  480. (setq txt2 (read-string "Edit: " txt))
  481. (when (not (equal txt txt2))
  482. (goto-char pos)
  483. (insert pre txt2 post)
  484. (delete-region (point) (point-at-eol))
  485. (org-set-tags nil t)))))
  486. (defun org-columns-edit-allowed ()
  487. "Edit the list of allowed values for the current property."
  488. (interactive)
  489. (let* ((pom (or (org-get-at-bol 'org-marker)
  490. (org-get-at-bol 'org-hd-marker)
  491. (point)))
  492. (key (get-char-property (point) 'org-columns-key))
  493. (key1 (concat key "_ALL"))
  494. (allowed (org-entry-get pom key1 t))
  495. nval)
  496. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  497. ;; FIXME: Write back to #+PROPERTY setting if that is needed.
  498. (setq nval (read-string "Allowed: " allowed))
  499. (org-entry-put
  500. (cond ((marker-position org-entry-property-inherited-from)
  501. org-entry-property-inherited-from)
  502. ((marker-position org-columns-top-level-marker)
  503. org-columns-top-level-marker)
  504. (t pom))
  505. key1 nval)))
  506. (defun org-columns-eval (form)
  507. (let (hidep)
  508. (save-excursion
  509. (beginning-of-line 1)
  510. ;; `next-line' is needed here, because it skips invisible line.
  511. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  512. (setq hidep (org-at-heading-p 1)))
  513. (eval form)
  514. (and hidep (hide-entry))))
  515. (defun org-columns-previous-allowed-value ()
  516. "Switch to the previous allowed value for this column."
  517. (interactive)
  518. (org-columns-next-allowed-value t))
  519. (defun org-columns-next-allowed-value (&optional previous nth)
  520. "Switch to the next allowed value for this column.
  521. When PREVIOUS is set, go to the previous value. When NTH is
  522. an integer, select that value."
  523. (interactive)
  524. (org-columns-check-computed)
  525. (let* ((col (current-column))
  526. (key (get-char-property (point) 'org-columns-key))
  527. (value (get-char-property (point) 'org-columns-value))
  528. (bol (point-at-bol)) (eol (point-at-eol))
  529. (pom (or (get-text-property bol 'org-hd-marker)
  530. (point))) ; keep despite of compiler waring
  531. (line-overlays
  532. (delq nil (mapcar (lambda (x)
  533. (and (eq (overlay-buffer x) (current-buffer))
  534. (>= (overlay-start x) bol)
  535. (<= (overlay-start x) eol)
  536. x))
  537. org-columns-overlays)))
  538. (allowed (or (org-property-get-allowed-values pom key)
  539. (and (memq
  540. (nth 4 (assoc key org-columns-current-fmt-compiled))
  541. '(checkbox checkbox-n-of-m checkbox-percent))
  542. '("[ ]" "[X]"))
  543. (org-colview-construct-allowed-dates value)))
  544. nval)
  545. (when (integerp nth)
  546. (setq nth (1- nth))
  547. (if (= nth -1) (setq nth 9)))
  548. (when (equal key "ITEM")
  549. (error "Cannot edit item headline from here"))
  550. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  551. (error "Allowed values for this property have not been defined"))
  552. (if (member key '("SCHEDULED" "DEADLINE"))
  553. (setq nval (if previous 'earlier 'later))
  554. (if previous (setq allowed (reverse allowed)))
  555. (cond
  556. (nth
  557. (setq nval (nth nth allowed))
  558. (if (not nval)
  559. (error "There are only %d allowed values for property `%s'"
  560. (length allowed) key)))
  561. ((member value allowed)
  562. (setq nval (or (car (cdr (member value allowed)))
  563. (car allowed)))
  564. (if (equal nval value)
  565. (error "Only one allowed value for this property")))
  566. (t (setq nval (car allowed)))))
  567. (cond
  568. ((equal major-mode 'org-agenda-mode)
  569. (org-columns-eval '(org-entry-put pom key nval))
  570. ;; The following let preserves the current format, and makes sure
  571. ;; that in only a single file things need to be updated.
  572. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  573. (buffer (marker-buffer pom))
  574. (org-agenda-contributing-files
  575. (list (with-current-buffer buffer
  576. (buffer-file-name (buffer-base-buffer))))))
  577. (org-agenda-columns)))
  578. (t
  579. (let ((inhibit-read-only t))
  580. (remove-text-properties (1- bol) eol '(read-only t))
  581. (unwind-protect
  582. (progn
  583. (setq org-columns-overlays
  584. (org-delete-all line-overlays org-columns-overlays))
  585. (mapc 'delete-overlay line-overlays)
  586. (org-columns-eval '(org-entry-put pom key nval)))
  587. (org-columns-display-here)))
  588. (org-move-to-column col)
  589. (and (nth 3 (assoc key org-columns-current-fmt-compiled))
  590. (org-columns-update key))))))
  591. (defun org-colview-construct-allowed-dates (s)
  592. "Construct a list of three dates around the date in S.
  593. This respects the format of the time stamp in S, active or non-active,
  594. and also including time or not. S must be just a time stamp, no text
  595. around it."
  596. (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
  597. (let* ((time (org-parse-time-string s 'nodefaults))
  598. (active (equal (string-to-char s) ?<))
  599. (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
  600. time-before time-after)
  601. (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  602. (setf (car time) (or (car time) 0))
  603. (setf (nth 1 time) (or (nth 1 time) 0))
  604. (setf (nth 2 time) (or (nth 2 time) 0))
  605. (setq time-before (copy-sequence time))
  606. (setq time-after (copy-sequence time))
  607. (setf (nth 3 time-before) (1- (nth 3 time)))
  608. (setf (nth 3 time-after) (1+ (nth 3 time)))
  609. (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
  610. (list time-before time time-after)))))
  611. (defun org-verify-version (task)
  612. (cond
  613. ((eq task 'columns)
  614. (if (or (featurep 'xemacs)
  615. (< emacs-major-version 22))
  616. (error "Emacs 22 is required for the columns feature")))))
  617. (defun org-columns-open-link (&optional arg)
  618. (interactive "P")
  619. (let ((value (get-char-property (point) 'org-columns-value)))
  620. (org-open-link-from-string value arg)))
  621. (defun org-columns-get-format-and-top-level ()
  622. (let (fmt)
  623. (when (condition-case nil (org-back-to-heading) (error nil))
  624. (setq fmt (org-entry-get nil "COLUMNS" t)))
  625. (setq fmt (or fmt org-columns-default-format))
  626. (org-set-local 'org-columns-current-fmt fmt)
  627. (org-columns-compile-format fmt)
  628. (if (marker-position org-entry-property-inherited-from)
  629. (move-marker org-columns-top-level-marker
  630. org-entry-property-inherited-from)
  631. (move-marker org-columns-top-level-marker (point)))
  632. fmt))
  633. (defun org-columns ()
  634. "Turn on column view on an org-mode file."
  635. (interactive)
  636. (org-verify-version 'columns)
  637. (org-columns-remove-overlays)
  638. (move-marker org-columns-begin-marker (point))
  639. (let ((org-columns-time (time-to-number-of-days (current-time)))
  640. beg end fmt cache maxwidths)
  641. (setq fmt (org-columns-get-format-and-top-level))
  642. (save-excursion
  643. (goto-char org-columns-top-level-marker)
  644. (setq beg (point))
  645. (unless org-columns-inhibit-recalculation
  646. (org-columns-compute-all))
  647. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  648. (point-max)))
  649. ;; Get and cache the properties
  650. (goto-char beg)
  651. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  652. (save-excursion
  653. (save-restriction
  654. (narrow-to-region beg end)
  655. (org-clock-sum))))
  656. (while (re-search-forward org-outline-regexp-bol end t)
  657. (if (and org-columns-skip-archived-trees
  658. (looking-at (concat ".*:" org-archive-tag ":")))
  659. (org-end-of-subtree t)
  660. (push (cons (org-current-line) (org-entry-properties)) cache)))
  661. (when cache
  662. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  663. (org-set-local 'org-columns-current-maxwidths maxwidths)
  664. (org-columns-display-here-title)
  665. (when (org-set-local 'org-columns-flyspell-was-active
  666. (org-bound-and-true-p flyspell-mode))
  667. (flyspell-mode 0))
  668. (unless (local-variable-p 'org-colview-initial-truncate-line-value)
  669. (org-set-local 'org-colview-initial-truncate-line-value
  670. truncate-lines))
  671. (setq truncate-lines t)
  672. (mapc (lambda (x)
  673. (org-goto-line (car x))
  674. (org-columns-display-here (cdr x)))
  675. cache)))))
  676. (eval-when-compile (defvar org-columns-time))
  677. (defvar org-columns-compile-map
  678. '(("none" none +)
  679. (":" add_times +)
  680. ("+" add_numbers +)
  681. ("$" currency +)
  682. ("X" checkbox +)
  683. ("X/" checkbox-n-of-m +)
  684. ("X%" checkbox-percent +)
  685. ("max" max_numbers max)
  686. ("min" min_numbers min)
  687. ("mean" mean_numbers
  688. (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  689. (":max" max_times max)
  690. (":min" min_times min)
  691. (":mean" mean_times
  692. (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  693. ("@min" min_age min (lambda (x) (- org-columns-time x)))
  694. ("@max" max_age max (lambda (x) (- org-columns-time x)))
  695. ("@mean" mean_age
  696. (lambda (&rest x) (/ (apply '+ x) (float (length x))))
  697. (lambda (x) (- org-columns-time x)))
  698. ("est+" estimate org-estimate-combine))
  699. "Operator <-> format,function,calc map.
  700. Used to compile/uncompile columns format and completing read in
  701. interactive function `org-columns-new'.
  702. operator string used in #+COLUMNS definition describing the
  703. summary type
  704. format symbol describing summary type selected interactively in
  705. `org-columns-new' and internally in
  706. `org-columns-number-to-string' and
  707. `org-columns-string-to-number'
  708. function called with a list of values as argument to calculate
  709. the summary value
  710. calc function called on every element before summarizing. This is
  711. optional and should only be specified if needed")
  712. (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
  713. "Insert a new column, to the left of the current column."
  714. (interactive)
  715. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  716. cell)
  717. (setq prop (org-icompleting-read
  718. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  719. nil nil prop))
  720. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  721. (setq width (read-string "Column width: " (if width (number-to-string width))))
  722. (if (string-match "\\S-" width)
  723. (setq width (string-to-number width))
  724. (setq width nil))
  725. (setq fmt (org-icompleting-read
  726. "Summary [none]: "
  727. (mapcar (lambda (x) (list (symbol-name (cadr x))))
  728. org-columns-compile-map)
  729. nil t))
  730. (setq fmt (intern fmt)
  731. fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
  732. (if (eq fmt 'none) (setq fmt nil))
  733. (if editp
  734. (progn
  735. (setcar editp prop)
  736. (setcdr editp (list title width nil fmt nil fun)))
  737. (setq cell (nthcdr (1- (current-column))
  738. org-columns-current-fmt-compiled))
  739. (setcdr cell (cons (list prop title width nil fmt nil
  740. (car fun) (cadr fun))
  741. (cdr cell))))
  742. (org-columns-store-format)
  743. (org-columns-redo)))
  744. (defun org-columns-delete ()
  745. "Delete the column at point from columns view."
  746. (interactive)
  747. (let* ((n (current-column))
  748. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  749. (when (y-or-n-p
  750. (format "Are you sure you want to remove column \"%s\"? " title))
  751. (setq org-columns-current-fmt-compiled
  752. (delq (nth n org-columns-current-fmt-compiled)
  753. org-columns-current-fmt-compiled))
  754. (org-columns-store-format)
  755. (org-columns-redo)
  756. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  757. (backward-char 1)))))
  758. (defun org-columns-edit-attributes ()
  759. "Edit the attributes of the current column."
  760. (interactive)
  761. (let* ((n (current-column))
  762. (info (nth n org-columns-current-fmt-compiled)))
  763. (apply 'org-columns-new info)))
  764. (defun org-columns-widen (arg)
  765. "Make the column wider by ARG characters."
  766. (interactive "p")
  767. (let* ((n (current-column))
  768. (entry (nth n org-columns-current-fmt-compiled))
  769. (width (or (nth 2 entry)
  770. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  771. (setq width (max 1 (+ width arg)))
  772. (setcar (nthcdr 2 entry) width)
  773. (org-columns-store-format)
  774. (org-columns-redo)))
  775. (defun org-columns-narrow (arg)
  776. "Make the column narrower by ARG characters."
  777. (interactive "p")
  778. (org-columns-widen (- arg)))
  779. (defun org-columns-move-right ()
  780. "Swap this column with the one to the right."
  781. (interactive)
  782. (let* ((n (current-column))
  783. (cell (nthcdr n org-columns-current-fmt-compiled))
  784. e)
  785. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  786. (error "Cannot shift this column further to the right"))
  787. (setq e (car cell))
  788. (setcar cell (car (cdr cell)))
  789. (setcdr cell (cons e (cdr (cdr cell))))
  790. (org-columns-store-format)
  791. (org-columns-redo)
  792. (forward-char 1)))
  793. (defun org-columns-move-left ()
  794. "Swap this column with the one to the left."
  795. (interactive)
  796. (let* ((n (current-column)))
  797. (when (= n 0)
  798. (error "Cannot shift this column further to the left"))
  799. (backward-char 1)
  800. (org-columns-move-right)
  801. (backward-char 1)))
  802. (defun org-columns-store-format ()
  803. "Store the text version of the current columns format in appropriate place.
  804. This is either in the COLUMNS property of the node starting the current column
  805. display, or in the #+COLUMNS line of the current buffer."
  806. (let (fmt (cnt 0))
  807. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  808. (org-set-local 'org-columns-current-fmt fmt)
  809. (if (marker-position org-columns-top-level-marker)
  810. (save-excursion
  811. (goto-char org-columns-top-level-marker)
  812. (if (and (org-at-heading-p)
  813. (org-entry-get nil "COLUMNS"))
  814. (org-entry-put nil "COLUMNS" fmt)
  815. (goto-char (point-min))
  816. ;; Overwrite all #+COLUMNS lines....
  817. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  818. (setq cnt (1+ cnt))
  819. (replace-match (concat "#+COLUMNS: " fmt) t t))
  820. (unless (> cnt 0)
  821. (goto-char (point-min))
  822. (or (org-at-heading-p t) (outline-next-heading))
  823. (let ((inhibit-read-only t))
  824. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  825. (org-set-local 'org-columns-default-format fmt))))))
  826. (defvar org-agenda-overriding-columns-format nil
  827. "When set, overrides any other format definition for the agenda.
  828. Don't set this, this is meant for dynamic scoping.")
  829. (defun org-columns-get-autowidth-alist (s cache)
  830. "Derive the maximum column widths from the format and the cache."
  831. (let ((start 0) rtn)
  832. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  833. (push (cons (match-string 1 s) 1) rtn)
  834. (setq start (match-end 0)))
  835. (mapc (lambda (x)
  836. (setcdr x (apply 'max
  837. (mapcar
  838. (lambda (y)
  839. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  840. cache))))
  841. rtn)
  842. rtn))
  843. (defun org-columns-compute-all ()
  844. "Compute all columns that have operators defined."
  845. (org-unmodified
  846. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  847. (let ((columns org-columns-current-fmt-compiled)
  848. (org-columns-time (time-to-number-of-days (current-time)))
  849. col)
  850. (while (setq col (pop columns))
  851. (when (nth 3 col)
  852. (save-excursion
  853. (org-columns-compute (car col)))))))
  854. (defun org-columns-update (property)
  855. "Recompute PROPERTY, and update the columns display for it."
  856. (org-columns-compute property)
  857. (let (fmt val pos)
  858. (save-excursion
  859. (mapc (lambda (ov)
  860. (when (equal (overlay-get ov 'org-columns-key) property)
  861. (setq pos (overlay-start ov))
  862. (goto-char pos)
  863. (when (setq val (cdr (assoc property
  864. (get-text-property
  865. (point-at-bol) 'org-summaries))))
  866. (setq fmt (overlay-get ov 'org-columns-format))
  867. (overlay-put ov 'org-columns-value val)
  868. (overlay-put ov 'display (format fmt val)))))
  869. org-columns-overlays))))
  870. (defun org-columns-compute (property)
  871. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  872. (interactive)
  873. (let* ((re org-outline-regexp-bol)
  874. (lmax 30) ; Does anyone use deeper levels???
  875. (lvals (make-vector lmax nil))
  876. (lflag (make-vector lmax nil))
  877. (level 0)
  878. (ass (assoc property org-columns-current-fmt-compiled))
  879. (format (nth 4 ass))
  880. (printf (nth 5 ass))
  881. (fun (nth 6 ass))
  882. (calc (or (nth 7 ass) 'identity))
  883. (beg org-columns-top-level-marker)
  884. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  885. (save-excursion
  886. ;; Find the region to compute
  887. (goto-char beg)
  888. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  889. (goto-char end)
  890. ;; Walk the tree from the back and do the computations
  891. (while (re-search-backward re beg t)
  892. (setq sumpos (match-beginning 0)
  893. last-level level
  894. level (org-outline-level)
  895. val (org-entry-get nil property)
  896. valflag (and val (string-match "\\S-" val)))
  897. (cond
  898. ((< level last-level)
  899. ;; put the sum of lower levels here as a property
  900. (setq sum (when (aref lvals last-level)
  901. (apply fun (aref lvals last-level)))
  902. flag (aref lflag last-level) ; any valid entries from children?
  903. str (org-columns-number-to-string sum format printf)
  904. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  905. useval (if flag str1 (if valflag val ""))
  906. sum-alist (get-text-property sumpos 'org-summaries))
  907. (if (assoc property sum-alist)
  908. (setcdr (assoc property sum-alist) useval)
  909. (push (cons property useval) sum-alist)
  910. (org-unmodified
  911. (add-text-properties sumpos (1+ sumpos)
  912. (list 'org-summaries sum-alist))))
  913. (when (and val (not (equal val (if flag str val))))
  914. (org-entry-put nil property (if flag str val)))
  915. ;; add current to current level accumulator
  916. (when (or flag valflag)
  917. (push (if flag
  918. sum
  919. (funcall calc (org-columns-string-to-number
  920. (if flag str val) format)))
  921. (aref lvals level))
  922. (aset lflag level t))
  923. ;; clear accumulators for deeper levels
  924. (loop for l from (1+ level) to (1- lmax) do
  925. (aset lvals l nil)
  926. (aset lflag l nil)))
  927. ((>= level last-level)
  928. ;; add what we have here to the accumulator for this level
  929. (when valflag
  930. (push (funcall calc (org-columns-string-to-number val format))
  931. (aref lvals level))
  932. (aset lflag level t)))
  933. (t (error "This should not happen")))))))
  934. (defun org-columns-redo ()
  935. "Construct the column display again."
  936. (interactive)
  937. (message "Recomputing columns...")
  938. (let ((line (org-current-line))
  939. (col (current-column)))
  940. (save-excursion
  941. (if (marker-position org-columns-begin-marker)
  942. (goto-char org-columns-begin-marker))
  943. (org-columns-remove-overlays)
  944. (if (eq major-mode 'org-mode)
  945. (call-interactively 'org-columns)
  946. (org-agenda-redo)
  947. (call-interactively 'org-agenda-columns)))
  948. (org-goto-line line)
  949. (move-to-column col))
  950. (message "Recomputing columns...done"))
  951. (defun org-columns-not-in-agenda ()
  952. (if (eq major-mode 'org-agenda-mode)
  953. (error "This command is only allowed in Org-mode buffers")))
  954. (defun org-string-to-number (s)
  955. "Convert string to number, and interpret hh:mm:ss."
  956. (if (not (string-match ":" s))
  957. (string-to-number s)
  958. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  959. (while l
  960. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  961. sum)))
  962. (defun org-columns-number-to-string (n fmt &optional printf)
  963. "Convert a computed column number to a string value, according to FMT."
  964. (cond
  965. ((memq fmt '(estimate)) (org-estimate-print n printf))
  966. ((not (numberp n)) "")
  967. ((memq fmt '(add_times max_times min_times mean_times))
  968. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  969. (format org-time-clocksum-format h m)))
  970. ((eq fmt 'checkbox)
  971. (cond ((= n (floor n)) "[X]")
  972. ((> n 1.) "[-]")
  973. (t "[ ]")))
  974. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  975. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  976. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  977. (printf (format printf n))
  978. ((eq fmt 'currency)
  979. (format "%.2f" n))
  980. ((memq fmt '(min_age max_age mean_age))
  981. (org-format-time-period n))
  982. (t (number-to-string n))))
  983. (defun org-nofm-to-completion (n m &optional percent)
  984. (if (not percent)
  985. (format "[%d/%d]" n m)
  986. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  987. (defun org-columns-string-to-number (s fmt)
  988. "Convert a column value to a number that can be used for column computing."
  989. (if s
  990. (cond
  991. ((memq fmt '(min_age max_age mean_age))
  992. (cond ((string= s "") org-columns-time)
  993. ((string-match
  994. "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
  995. s)
  996. (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
  997. (string-to-number (match-string 2 s))))
  998. (string-to-number (match-string 3 s))))
  999. (string-to-number (match-string 4 s))))
  1000. (t (time-to-number-of-days (apply 'encode-time
  1001. (org-parse-time-string s t))))))
  1002. ((string-match ":" s)
  1003. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1004. (while l
  1005. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1006. sum))
  1007. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  1008. (if (equal s "[X]") 1. 0.000001))
  1009. ((memq fmt '(estimate)) (org-string-to-estimate s))
  1010. (t (string-to-number s)))))
  1011. (defun org-columns-uncompile-format (cfmt)
  1012. "Turn the compiled columns format back into a string representation."
  1013. (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
  1014. (while (setq e (pop cfmt))
  1015. (setq prop (car e)
  1016. title (nth 1 e)
  1017. width (nth 2 e)
  1018. op (nth 3 e)
  1019. fmt (nth 4 e)
  1020. printf (nth 5 e)
  1021. fun (nth 6 e)
  1022. calc (nth 7 e))
  1023. (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
  1024. (setq op (car op-match)))
  1025. (if (and op printf) (setq op (concat op ";" printf)))
  1026. (if (equal title prop) (setq title nil))
  1027. (setq s (concat "%" (if width (number-to-string width))
  1028. prop
  1029. (if title (concat "(" title ")"))
  1030. (if op (concat "{" op "}"))))
  1031. (setq rtn (concat rtn " " s)))
  1032. (org-trim rtn)))
  1033. (defun org-columns-compile-format (fmt)
  1034. "Turn a column format string into an alist of specifications.
  1035. The alist has one entry for each column in the format. The elements of
  1036. that list are:
  1037. property the property
  1038. title the title field for the columns
  1039. width the column width in characters, can be nil for automatic
  1040. operator the operator if any
  1041. format the output format for computed results, derived from operator
  1042. printf a printf format for computed values
  1043. fun the lisp function to compute summary values, derived from operator
  1044. calc function to get values from base elements"
  1045. (let ((start 0) width prop title op op-match f printf fun calc)
  1046. (setq org-columns-current-fmt-compiled nil)
  1047. (while (string-match
  1048. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  1049. fmt start)
  1050. (setq start (match-end 0)
  1051. width (match-string 1 fmt)
  1052. prop (match-string 2 fmt)
  1053. title (or (match-string 3 fmt) prop)
  1054. op (match-string 4 fmt)
  1055. f nil
  1056. printf nil
  1057. fun '+
  1058. calc nil)
  1059. (if width (setq width (string-to-number width)))
  1060. (when (and op (string-match ";" op))
  1061. (setq printf (substring op (match-end 0))
  1062. op (substring op 0 (match-beginning 0))))
  1063. (when (setq op-match (assoc op org-columns-compile-map))
  1064. (setq f (cadr op-match)
  1065. fun (caddr op-match)
  1066. calc (cadddr op-match)))
  1067. (push (list prop title width op f printf fun calc)
  1068. org-columns-current-fmt-compiled))
  1069. (setq org-columns-current-fmt-compiled
  1070. (nreverse org-columns-current-fmt-compiled))))
  1071. ;;; Dynamic block for Column view
  1072. (defvar org-heading-regexp) ; defined in org.el
  1073. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  1074. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  1075. "Get the column view of the current buffer or subtree.
  1076. The first optional argument MAXLEVEL sets the level limit. A
  1077. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  1078. empty rows, an empty row being one where all the column view
  1079. specifiers except ITEM are empty. This function returns a list
  1080. containing the title row and all other rows. Each row is a list
  1081. of fields."
  1082. (save-excursion
  1083. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1084. (re-comment (format org-heading-keyword-regexp-format
  1085. org-comment-string))
  1086. (re-archive (concat ".*:" org-archive-tag ":"))
  1087. (n (length title)) row tbl)
  1088. (goto-char (point-min))
  1089. (while (re-search-forward org-heading-regexp nil t)
  1090. (catch 'next
  1091. (when (and (or (null maxlevel)
  1092. (>= maxlevel
  1093. (if org-odd-levels-only
  1094. (/ (1+ (length (match-string 1))) 2)
  1095. (length (match-string 1)))))
  1096. (get-char-property (match-beginning 0) 'org-columns-key))
  1097. (when (save-excursion
  1098. (goto-char (point-at-bol))
  1099. (or (looking-at re-comment)
  1100. (looking-at re-archive)))
  1101. (org-end-of-subtree t)
  1102. (throw 'next t))
  1103. (setq row nil)
  1104. (loop for i from 0 to (1- n) do
  1105. (push
  1106. (org-quote-vert
  1107. (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  1108. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  1109. ""))
  1110. row))
  1111. (setq row (nreverse row))
  1112. (unless (and skip-empty-rows
  1113. (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
  1114. (push row tbl)))))
  1115. (append (list title 'hline) (nreverse tbl)))))
  1116. (defun org-dblock-write:columnview (params)
  1117. "Write the column view table.
  1118. PARAMS is a property list of parameters:
  1119. :width enforce same column widths with <N> specifiers.
  1120. :id the :ID: property of the entry where the columns view
  1121. should be built. When the symbol `local', call locally.
  1122. When `global' call column view with the cursor at the beginning
  1123. of the buffer (usually this means that the whole buffer switches
  1124. to column view). When \"file:path/to/file.org\", invoke column
  1125. view at the start of that file. Otherwise, the ID is located
  1126. using `org-id-find'.
  1127. :hlines When t, insert a hline before each item. When a number, insert
  1128. a hline before each level <= that number.
  1129. :vlines When t, make each column a colgroup to enforce vertical lines.
  1130. :maxlevel When set to a number, don't capture headlines below this level.
  1131. :skip-empty-rows
  1132. When t, skip rows where all specifiers other than ITEM are empty."
  1133. (let ((pos (move-marker (make-marker) (point)))
  1134. (hlines (plist-get params :hlines))
  1135. (vlines (plist-get params :vlines))
  1136. (maxlevel (plist-get params :maxlevel))
  1137. (content-lines (org-split-string (plist-get params :content) "\n"))
  1138. (skip-empty-rows (plist-get params :skip-empty-rows))
  1139. tbl id idpos nfields tmp recalc line
  1140. id-as-string view-file view-pos)
  1141. (when (setq id (plist-get params :id))
  1142. (setq id-as-string (cond ((numberp id) (number-to-string id))
  1143. ((symbolp id) (symbol-name id))
  1144. ((stringp id) id)
  1145. (t "")))
  1146. (cond ((not id) nil)
  1147. ((eq id 'global) (setq view-pos (point-min)))
  1148. ((eq id 'local))
  1149. ((string-match "^file:\\(.*\\)" id-as-string)
  1150. (setq view-file (match-string 1 id-as-string)
  1151. view-pos 1)
  1152. (unless (file-exists-p view-file)
  1153. (error "No such file: \"%s\"" id-as-string)))
  1154. ((setq idpos (org-find-entry-with-id id))
  1155. (setq view-pos idpos))
  1156. ((setq idpos (org-id-find id))
  1157. (setq view-file (car idpos))
  1158. (setq view-pos (cdr idpos)))
  1159. (t (error "Cannot find entry with :ID: %s" id))))
  1160. (with-current-buffer (if view-file
  1161. (get-file-buffer view-file)
  1162. (current-buffer))
  1163. (save-excursion
  1164. (save-restriction
  1165. (widen)
  1166. (goto-char (or view-pos (point)))
  1167. (org-columns)
  1168. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  1169. (setq nfields (length (car tbl)))
  1170. (org-columns-quit))))
  1171. (goto-char pos)
  1172. (move-marker pos nil)
  1173. (when tbl
  1174. (when (plist-get params :hlines)
  1175. (setq tmp nil)
  1176. (while tbl
  1177. (if (eq (car tbl) 'hline)
  1178. (push (pop tbl) tmp)
  1179. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  1180. (if (and (not (eq (car tmp) 'hline))
  1181. (or (eq hlines t)
  1182. (and (numberp hlines)
  1183. (<= (- (match-end 1) (match-beginning 1))
  1184. hlines))))
  1185. (push 'hline tmp)))
  1186. (push (pop tbl) tmp)))
  1187. (setq tbl (nreverse tmp)))
  1188. (when vlines
  1189. (setq tbl (mapcar (lambda (x)
  1190. (if (eq 'hline x) x (cons "" x)))
  1191. tbl))
  1192. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  1193. (setq pos (point))
  1194. (when content-lines
  1195. (while (string-match "^#" (car content-lines))
  1196. (insert (pop content-lines) "\n")))
  1197. (insert (org-listtable-to-string tbl))
  1198. (when (plist-get params :width)
  1199. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  1200. org-columns-current-widths "|")))
  1201. (while (setq line (pop content-lines))
  1202. (when (string-match "^#" line)
  1203. (insert "\n" line)
  1204. (when (string-match "^[ \t]*#\\+TBLFM" line)
  1205. (setq recalc t))))
  1206. (if recalc
  1207. (progn (goto-char pos) (org-table-recalculate 'all))
  1208. (goto-char pos)
  1209. (org-table-align)))))
  1210. (defun org-listtable-to-string (tbl)
  1211. "Convert a listtable TBL to a string that contains the Org-mode table.
  1212. The table still need to be aligned. The resulting string has no leading
  1213. and tailing newline characters."
  1214. (mapconcat
  1215. (lambda (x)
  1216. (cond
  1217. ((listp x)
  1218. (concat "|" (mapconcat 'identity x "|") "|"))
  1219. ((eq x 'hline) "|-|")
  1220. (t (error "Garbage in listtable: %s" x))))
  1221. tbl "\n"))
  1222. (defun org-insert-columns-dblock ()
  1223. "Create a dynamic block capturing a column view table."
  1224. (interactive)
  1225. (let ((defaults '(:name "columnview" :hlines 1))
  1226. (id (org-icompleting-read
  1227. "Capture columns (local, global, entry with :ID: property) [local]: "
  1228. (append '(("global") ("local"))
  1229. (mapcar 'list (org-property-values "ID"))))))
  1230. (if (equal id "") (setq id 'local))
  1231. (if (equal id "global") (setq id 'global))
  1232. (setq defaults (append defaults (list :id id)))
  1233. (org-create-dblock defaults)
  1234. (org-update-dblock)))
  1235. ;;; Column view in the agenda
  1236. (defvar org-agenda-view-columns-initially nil
  1237. "When set, switch to columns view immediately after creating the agenda.")
  1238. (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
  1239. (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
  1240. (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
  1241. (defun org-agenda-columns ()
  1242. "Turn on or update column view in the agenda."
  1243. (interactive)
  1244. (org-verify-version 'columns)
  1245. (org-columns-remove-overlays)
  1246. (move-marker org-columns-begin-marker (point))
  1247. (let ((org-columns-time (time-to-number-of-days (current-time)))
  1248. cache maxwidths m p a d fmt)
  1249. (cond
  1250. ((and (boundp 'org-agenda-overriding-columns-format)
  1251. org-agenda-overriding-columns-format)
  1252. (setq fmt org-agenda-overriding-columns-format)
  1253. (org-set-local 'org-agenda-overriding-columns-format fmt))
  1254. ((setq m (org-get-at-bol 'org-hd-marker))
  1255. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1256. (with-current-buffer (marker-buffer m)
  1257. org-columns-default-format))))
  1258. ((and (boundp 'org-columns-current-fmt)
  1259. (local-variable-p 'org-columns-current-fmt)
  1260. org-columns-current-fmt)
  1261. (setq fmt org-columns-current-fmt))
  1262. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  1263. (setq m (get-text-property m 'org-hd-marker))
  1264. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1265. (with-current-buffer (marker-buffer m)
  1266. org-columns-default-format)))))
  1267. (setq fmt (or fmt org-columns-default-format))
  1268. (org-set-local 'org-columns-current-fmt fmt)
  1269. (org-columns-compile-format fmt)
  1270. (when org-agenda-columns-compute-summary-properties
  1271. (org-agenda-colview-compute org-columns-current-fmt-compiled))
  1272. (save-excursion
  1273. ;; Get and cache the properties
  1274. (goto-char (point-min))
  1275. (while (not (eobp))
  1276. (when (setq m (or (org-get-at-bol 'org-hd-marker)
  1277. (org-get-at-bol 'org-marker)))
  1278. (setq p (org-entry-properties m))
  1279. (when (or (not (setq a (assoc org-effort-property p)))
  1280. (not (string-match "\\S-" (or (cdr a) ""))))
  1281. ;; OK, the property is not defined. Use appointment duration?
  1282. (when (and org-agenda-columns-add-appointments-to-effort-sum
  1283. (setq d (get-text-property (point) 'duration)))
  1284. (setq d (org-minutes-to-hh:mm-string d))
  1285. (put-text-property 0 (length d) 'face 'org-warning d)
  1286. (push (cons org-effort-property d) p)))
  1287. (push (cons (org-current-line) p) cache))
  1288. (beginning-of-line 2))
  1289. (when cache
  1290. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  1291. (org-set-local 'org-columns-current-maxwidths maxwidths)
  1292. (org-columns-display-here-title)
  1293. (when (org-set-local 'org-columns-flyspell-was-active
  1294. (org-bound-and-true-p flyspell-mode))
  1295. (flyspell-mode 0))
  1296. (mapc (lambda (x)
  1297. (org-goto-line (car x))
  1298. (org-columns-display-here (cdr x)))
  1299. cache)
  1300. (when org-agenda-columns-show-summaries
  1301. (org-agenda-colview-summarize cache))))))
  1302. (defun org-agenda-colview-summarize (cache)
  1303. "Summarize the summarizable columns in column view in the agenda.
  1304. This will add overlays to the date lines, to show the summary for each day."
  1305. (let* ((fmt (mapcar (lambda (x)
  1306. (if (equal (car x) "CLOCKSUM")
  1307. (list "CLOCKSUM" (nth 1 x) (nth 2 x) ":" 'add_times
  1308. nil '+ nil)
  1309. x))
  1310. org-columns-current-fmt-compiled))
  1311. line c c1 stype calc sumfunc props lsum entries prop v title)
  1312. (catch 'exit
  1313. (when (delq nil (mapcar 'cadr fmt))
  1314. ;; OK, at least one summation column, it makes sense to try this
  1315. (goto-char (point-max))
  1316. (while t
  1317. (when (or (get-text-property (point) 'org-date-line)
  1318. (eq (get-text-property (point) 'face)
  1319. 'org-agenda-structure))
  1320. ;; OK, this is a date line that should be used
  1321. (setq line (org-current-line))
  1322. (setq entries nil c cache cache nil)
  1323. (while (setq c1 (pop c))
  1324. (if (> (car c1) line)
  1325. (push c1 entries)
  1326. (push c1 cache)))
  1327. ;; now ENTRIES are the ones we want to use, CACHE is the rest
  1328. ;; Compute the summaries for the properties we want,
  1329. ;; set nil properties for the rest.
  1330. (when (setq entries (mapcar 'cdr entries))
  1331. (setq props
  1332. (mapcar
  1333. (lambda (f)
  1334. (setq prop (car f)
  1335. title (nth 1 f)
  1336. stype (nth 4 f)
  1337. sumfunc (nth 6 f)
  1338. calc (or (nth 7 f) 'identity))
  1339. (cond
  1340. ((equal prop "ITEM")
  1341. (cons prop (buffer-substring (point-at-bol)
  1342. (point-at-eol))))
  1343. ((not stype) (cons prop ""))
  1344. (t ;; do the summary
  1345. (setq lsum nil)
  1346. (dolist (x entries)
  1347. (setq v (cdr (assoc prop x)))
  1348. (if v
  1349. (push
  1350. (funcall
  1351. (if (not (get-text-property 0 'org-computed v))
  1352. calc
  1353. 'identity)
  1354. (org-columns-string-to-number
  1355. v stype))
  1356. lsum)))
  1357. (setq lsum (remove nil lsum))
  1358. (setq lsum
  1359. (cond ((> (length lsum) 1)
  1360. (org-columns-number-to-string
  1361. (apply sumfunc lsum) stype))
  1362. ((eq (length lsum) 1)
  1363. (org-columns-number-to-string
  1364. (car lsum) stype))
  1365. (t "")))
  1366. (put-text-property 0 (length lsum) 'face 'bold lsum)
  1367. (unless (eq calc 'identity)
  1368. (put-text-property 0 (length lsum) 'org-computed t lsum))
  1369. (cons prop lsum))))
  1370. fmt))
  1371. (org-columns-display-here props 'dateline)
  1372. (org-set-local 'org-agenda-columns-active t)))
  1373. (if (bobp) (throw 'exit t))
  1374. (beginning-of-line 0))))))
  1375. (defun org-agenda-colview-compute (fmt)
  1376. "Compute the relevant columns in the contributing source buffers."
  1377. (let ((files org-agenda-contributing-files)
  1378. (org-columns-begin-marker (make-marker))
  1379. (org-columns-top-level-marker (make-marker))
  1380. f fm a b)
  1381. (while (setq f (pop files))
  1382. (setq b (find-buffer-visiting f))
  1383. (with-current-buffer (or (buffer-base-buffer b) b)
  1384. (save-excursion
  1385. (save-restriction
  1386. (widen)
  1387. (org-unmodified
  1388. (remove-text-properties (point-min) (point-max)
  1389. '(org-summaries t)))
  1390. (goto-char (point-min))
  1391. (org-columns-get-format-and-top-level)
  1392. (while (setq fm (pop fmt))
  1393. (if (equal (car fm) "CLOCKSUM")
  1394. (org-clock-sum)
  1395. (when (and (nth 4 fm)
  1396. (setq a (assoc (car fm)
  1397. org-columns-current-fmt-compiled))
  1398. (equal (nth 4 a) (nth 4 fm)))
  1399. (org-columns-compute (car fm)))))))))))
  1400. (defun org-format-time-period (interval)
  1401. "Convert time in fractional days to days/hours/minutes/seconds."
  1402. (if (numberp interval)
  1403. (let* ((days (floor interval))
  1404. (frac-hours (* 24 (- interval days)))
  1405. (hours (floor frac-hours))
  1406. (minutes (floor (* 60 (- frac-hours hours))))
  1407. (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
  1408. (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
  1409. ""))
  1410. (defun org-estimate-mean-and-var (v)
  1411. "Return the mean and variance of an estimate."
  1412. (let* ((low (float (car v)))
  1413. (high (float (cadr v)))
  1414. (mean (/ (+ low high) 2.0))
  1415. (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
  1416. (list mean var)))
  1417. (defun org-estimate-combine (&rest el)
  1418. "Combine a list of estimates, using mean and variance.
  1419. The mean and variance of the result will be the sum of the means
  1420. and variances (respectively) of the individual estimates."
  1421. (let ((mean 0)
  1422. (var 0))
  1423. (mapc (lambda (e)
  1424. (let ((stats (org-estimate-mean-and-var e)))
  1425. (setq mean (+ mean (car stats)))
  1426. (setq var (+ var (cadr stats)))))
  1427. el)
  1428. (let ((stdev (sqrt var)))
  1429. (list (- mean stdev) (+ mean stdev)))))
  1430. (defun org-estimate-print (e &optional fmt)
  1431. "Prepare a string representation of an estimate.
  1432. This formats these numbers as two numbers with a \"-\" between them."
  1433. (if (null fmt) (set 'fmt "%.0f"))
  1434. (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
  1435. (defun org-string-to-estimate (s)
  1436. "Convert a string to an estimate.
  1437. The string should be two numbers joined with a \"-\"."
  1438. (if (string-match "\\(.*\\)-\\(.*\\)" s)
  1439. (list (string-to-number (match-string 1 s))
  1440. (string-to-number(match-string 2 s)))
  1441. (list (string-to-number s) (string-to-number s))))
  1442. (provide 'org-colview)
  1443. ;;; org-colview.el ends here