org-colview-xemacs.el 66 KB

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