org-colview-xemacs.el 65 KB

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