org-colview.el 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. ;;; org-colview.el --- Column View in Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.02pre02
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs 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, or (at your option)
  13. ;; any later version.
  14. ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the face definitons for Org.
  26. ;;; Code:
  27. (eval-when-compile (require 'cl))
  28. (require 'org)
  29. ;;; Column View
  30. (defvar org-columns-overlays nil
  31. "Holds the list of current column overlays.")
  32. (defvar org-columns-current-fmt nil
  33. "Local variable, holds the currently active column format.")
  34. (make-variable-buffer-local 'org-columns-current-fmt)
  35. (defvar org-columns-current-fmt-compiled nil
  36. "Local variable, holds the currently active column format.
  37. This is the compiled version of the format.")
  38. (make-variable-buffer-local 'org-columns-current-fmt-compiled)
  39. (defvar org-columns-current-widths nil
  40. "Loval variable, holds the currently widths of fields.")
  41. (make-variable-buffer-local 'org-columns-current-widths)
  42. (defvar org-columns-current-maxwidths nil
  43. "Loval variable, holds the currently active maximum column widths.")
  44. (make-variable-buffer-local 'org-columns-current-maxwidths)
  45. (defvar org-columns-begin-marker (make-marker)
  46. "Points to the position where last a column creation command was called.")
  47. (defvar org-columns-top-level-marker (make-marker)
  48. "Points to the position where current columns region starts.")
  49. (defvar org-columns-map (make-sparse-keymap)
  50. "The keymap valid in column display.")
  51. (defun org-columns-content ()
  52. "Switch to contents view while in columns view."
  53. (interactive)
  54. (org-overview)
  55. (org-content))
  56. (org-defkey org-columns-map "c" 'org-columns-content)
  57. (org-defkey org-columns-map "o" 'org-overview)
  58. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  59. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  60. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  61. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  62. (org-defkey org-columns-map "v" 'org-columns-show-value)
  63. (org-defkey org-columns-map "q" 'org-columns-quit)
  64. (org-defkey org-columns-map "r" 'org-columns-redo)
  65. (org-defkey org-columns-map "g" 'org-columns-redo)
  66. (org-defkey org-columns-map [left] 'backward-char)
  67. (org-defkey org-columns-map "\M-b" 'backward-char)
  68. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  69. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  70. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  71. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  72. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  73. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  74. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  75. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  76. (org-defkey org-columns-map "<" 'org-columns-narrow)
  77. (org-defkey org-columns-map ">" 'org-columns-widen)
  78. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  79. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  80. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  81. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  82. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  83. '("Column"
  84. ["Edit property" org-columns-edit-value t]
  85. ["Next allowed value" org-columns-next-allowed-value t]
  86. ["Previous allowed value" org-columns-previous-allowed-value t]
  87. ["Show full value" org-columns-show-value t]
  88. ["Edit allowed values" org-columns-edit-allowed t]
  89. "--"
  90. ["Edit column attributes" org-columns-edit-attributes t]
  91. ["Increase column width" org-columns-widen t]
  92. ["Decrease column width" org-columns-narrow t]
  93. "--"
  94. ["Move column right" org-columns-move-right t]
  95. ["Move column left" org-columns-move-left t]
  96. ["Add column" org-columns-new t]
  97. ["Delete column" org-columns-delete t]
  98. "--"
  99. ["CONTENTS" org-columns-content t]
  100. ["OVERVIEW" org-overview t]
  101. ["Refresh columns display" org-columns-redo t]
  102. "--"
  103. ["Open link" org-columns-open-link t]
  104. "--"
  105. ["Quit" org-columns-quit t]))
  106. (defun org-columns-new-overlay (beg end &optional string face)
  107. "Create a new column overlay and add it to the list."
  108. (let ((ov (org-make-overlay beg end)))
  109. (org-overlay-put ov 'face (or face 'secondary-selection))
  110. (org-overlay-display ov string face)
  111. (push ov org-columns-overlays)
  112. ov))
  113. (defun org-columns-display-here (&optional props)
  114. "Overlay the current line with column display."
  115. (interactive)
  116. (let* ((fmt org-columns-current-fmt-compiled)
  117. (beg (point-at-bol))
  118. (level-face (save-excursion
  119. (beginning-of-line 1)
  120. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  121. (org-get-level-face 2))))
  122. (color (list :foreground
  123. (face-attribute
  124. (or level-face
  125. (and (eq major-mode 'org-agenda-mode)
  126. (get-text-property (point-at-bol) 'face))
  127. 'default) :foreground)))
  128. (face (list color 'org-column))
  129. pom property ass width f string ov column val modval)
  130. ;; Check if the entry is in another buffer.
  131. (unless props
  132. (if (eq major-mode 'org-agenda-mode)
  133. (setq pom (or (get-text-property (point) 'org-hd-marker)
  134. (get-text-property (point) 'org-marker))
  135. props (if pom (org-entry-properties pom) nil))
  136. (setq props (org-entry-properties nil))))
  137. ;; Walk the format
  138. (while (setq column (pop fmt))
  139. (setq property (car column)
  140. ass (if (equal property "ITEM")
  141. (cons "ITEM"
  142. (save-match-data
  143. (org-no-properties
  144. (org-remove-tabs
  145. (buffer-substring-no-properties
  146. (point-at-bol) (point-at-eol))))))
  147. (assoc property props))
  148. width (or (cdr (assoc property org-columns-current-maxwidths))
  149. (nth 2 column)
  150. (length property))
  151. f (format "%%-%d.%ds | " width width)
  152. val (or (cdr ass) "")
  153. modval (if (equal property "ITEM")
  154. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  155. string (format f (or modval val)))
  156. ;; Create the overlay
  157. (org-unmodified
  158. (setq ov (org-columns-new-overlay
  159. beg (setq beg (1+ beg)) string face))
  160. (org-overlay-put ov 'keymap org-columns-map)
  161. (org-overlay-put ov 'org-columns-key property)
  162. (org-overlay-put ov 'org-columns-value (cdr ass))
  163. (org-overlay-put ov 'org-columns-value-modified modval)
  164. (org-overlay-put ov 'org-columns-pom pom)
  165. (org-overlay-put ov 'org-columns-format f))
  166. (if (or (not (char-after beg))
  167. (equal (char-after beg) ?\n))
  168. (let ((inhibit-read-only t))
  169. (save-excursion
  170. (goto-char beg)
  171. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  172. ;; Make the rest of the line disappear.
  173. (org-unmodified
  174. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  175. (org-overlay-put ov 'invisible t)
  176. (org-overlay-put ov 'keymap org-columns-map)
  177. (org-overlay-put ov 'intangible t)
  178. (push ov org-columns-overlays)
  179. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  180. (org-overlay-put ov 'keymap org-columns-map)
  181. (push ov org-columns-overlays)
  182. (let ((inhibit-read-only t))
  183. (put-text-property (max (point-min) (1- (point-at-bol)))
  184. (min (point-max) (1+ (point-at-eol)))
  185. 'read-only "Type `e' to edit property")))))
  186. (defvar org-columns-full-header-line-format nil
  187. "Fthe full header line format, will be shifted by horizontal scrolling." )
  188. (defvar org-previous-header-line-format nil
  189. "The header line format before column view was turned on.")
  190. (defvar org-columns-inhibit-recalculation nil
  191. "Inhibit recomputing of columns on column view startup.")
  192. (defvar header-line-format)
  193. (defvar org-columns-previous-hscroll 0)
  194. (defun org-columns-display-here-title ()
  195. "Overlay the newline before the current line with the table title."
  196. (interactive)
  197. (let ((fmt org-columns-current-fmt-compiled)
  198. string (title "")
  199. property width f column str widths)
  200. (while (setq column (pop fmt))
  201. (setq property (car column)
  202. str (or (nth 1 column) property)
  203. width (or (cdr (assoc property org-columns-current-maxwidths))
  204. (nth 2 column)
  205. (length str))
  206. widths (push width widths)
  207. f (format "%%-%d.%ds | " width width)
  208. string (format f str)
  209. title (concat title string)))
  210. (setq title (concat
  211. (org-add-props " " nil 'display '(space :align-to 0))
  212. ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  213. (org-add-props title nil 'face 'org-column-title)))
  214. (org-set-local 'org-previous-header-line-format header-line-format)
  215. (org-set-local 'org-columns-current-widths (nreverse widths))
  216. (setq org-columns-full-header-line-format title)
  217. (setq org-columns-previous-hscroll -1)
  218. ; (org-columns-hscoll-title)
  219. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  220. (defun org-columns-hscoll-title ()
  221. "Set the header-line-format so that it scrolls along with the table."
  222. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  223. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  224. (setq header-line-format
  225. (concat (substring org-columns-full-header-line-format 0 1)
  226. (substring org-columns-full-header-line-format
  227. (1+ (window-hscroll))))
  228. org-columns-previous-hscroll (window-hscroll))
  229. (force-mode-line-update)))
  230. (defun org-columns-remove-overlays ()
  231. "Remove all currently active column overlays."
  232. (interactive)
  233. (when (marker-buffer org-columns-begin-marker)
  234. (with-current-buffer (marker-buffer org-columns-begin-marker)
  235. (when (local-variable-p 'org-previous-header-line-format)
  236. (setq header-line-format org-previous-header-line-format)
  237. (kill-local-variable 'org-previous-header-line-format)
  238. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  239. (move-marker org-columns-begin-marker nil)
  240. (move-marker org-columns-top-level-marker nil)
  241. (org-unmodified
  242. (mapc 'org-delete-overlay org-columns-overlays)
  243. (setq org-columns-overlays nil)
  244. (let ((inhibit-read-only t))
  245. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  246. (defun org-columns-cleanup-item (item fmt)
  247. "Remove from ITEM what is a column in the format FMT."
  248. (if (not org-complex-heading-regexp)
  249. item
  250. (when (string-match org-complex-heading-regexp item)
  251. (concat
  252. (org-add-props (concat (match-string 1 item) " ") nil
  253. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  254. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  255. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  256. " " (match-string 4 item)
  257. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  258. (defun org-columns-show-value ()
  259. "Show the full value of the property."
  260. (interactive)
  261. (let ((value (get-char-property (point) 'org-columns-value)))
  262. (message "Value is: %s" (or value ""))))
  263. (defun org-columns-quit ()
  264. "Remove the column overlays and in this way exit column editing."
  265. (interactive)
  266. (org-unmodified
  267. (org-columns-remove-overlays)
  268. (let ((inhibit-read-only t))
  269. (remove-text-properties (point-min) (point-max) '(read-only t))))
  270. (when (eq major-mode 'org-agenda-mode)
  271. (setq org-agenda-columns-active nil)
  272. (message
  273. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  274. (defun org-columns-check-computed ()
  275. "Check if this column value is computed.
  276. If yes, throw an error indicating that changing it does not make sense."
  277. (let ((val (get-char-property (point) 'org-columns-value)))
  278. (when (and (stringp val)
  279. (get-char-property 0 'org-computed val))
  280. (error "This value is computed from the entry's children"))))
  281. (defun org-columns-todo (&optional arg)
  282. "Change the TODO state during column view."
  283. (interactive "P")
  284. (org-columns-edit-value "TODO"))
  285. (defun org-columns-set-tags-or-toggle (&optional arg)
  286. "Toggle checkbox at point, or set tags for current headline."
  287. (interactive "P")
  288. (if (string-match "\\`\\[[ xX-]\\]\\'"
  289. (get-char-property (point) 'org-columns-value))
  290. (org-columns-next-allowed-value)
  291. (org-columns-edit-value "TAGS")))
  292. (defun org-columns-edit-value (&optional key)
  293. "Edit the value of the property at point in column view.
  294. Where possible, use the standard interface for changing this line."
  295. (interactive)
  296. (org-columns-check-computed)
  297. (let* ((external-key key)
  298. (col (current-column))
  299. (key (or key (get-char-property (point) 'org-columns-key)))
  300. (value (get-char-property (point) 'org-columns-value))
  301. (bol (point-at-bol)) (eol (point-at-eol))
  302. (pom (or (get-text-property bol 'org-hd-marker)
  303. (point))) ; keep despite of compiler waring
  304. (line-overlays
  305. (delq nil (mapcar (lambda (x)
  306. (and (eq (overlay-buffer x) (current-buffer))
  307. (>= (overlay-start x) bol)
  308. (<= (overlay-start x) eol)
  309. x))
  310. org-columns-overlays)))
  311. nval eval allowed)
  312. (cond
  313. ((equal key "CLOCKSUM")
  314. (error "This special column cannot be edited"))
  315. ((equal key "ITEM")
  316. (setq eval '(org-with-point-at pom
  317. (org-edit-headline))))
  318. ((equal key "TODO")
  319. (setq eval '(org-with-point-at pom
  320. (let ((current-prefix-arg
  321. (if external-key current-prefix-arg '(4))))
  322. (call-interactively 'org-todo)))))
  323. ((equal key "PRIORITY")
  324. (setq eval '(org-with-point-at pom
  325. (call-interactively 'org-priority))))
  326. ((equal key "TAGS")
  327. (setq eval '(org-with-point-at pom
  328. (let ((org-fast-tag-selection-single-key
  329. (if (eq org-fast-tag-selection-single-key 'expert)
  330. t org-fast-tag-selection-single-key)))
  331. (call-interactively 'org-set-tags)))))
  332. ((equal key "DEADLINE")
  333. (setq eval '(org-with-point-at pom
  334. (call-interactively 'org-deadline))))
  335. ((equal key "SCHEDULED")
  336. (setq eval '(org-with-point-at pom
  337. (call-interactively 'org-schedule))))
  338. (t
  339. (setq allowed (org-property-get-allowed-values pom key 'table))
  340. (if allowed
  341. (setq nval (completing-read "Value: " allowed nil t))
  342. (setq nval (read-string "Edit: " value)))
  343. (setq nval (org-trim nval))
  344. (when (not (equal nval value))
  345. (setq eval '(org-entry-put pom key nval)))))
  346. (when eval
  347. (let ((inhibit-read-only t))
  348. (org-unmodified
  349. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t)))
  350. (unwind-protect
  351. (progn
  352. (setq org-columns-overlays
  353. (org-delete-all line-overlays org-columns-overlays))
  354. (mapc 'org-delete-overlay line-overlays)
  355. (org-columns-eval eval))
  356. (org-columns-display-here))))
  357. (move-to-column col)
  358. (if (and (org-mode-p)
  359. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  360. (org-columns-update key))))
  361. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  362. "Edit the current headline, the part without TODO keyword, TAGS."
  363. (org-back-to-heading)
  364. (when (looking-at org-todo-line-regexp)
  365. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  366. (txt (match-string 3))
  367. (post "")
  368. txt2)
  369. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  370. (setq post (match-string 0 txt)
  371. txt (substring txt 0 (match-beginning 0))))
  372. (setq txt2 (read-string "Edit: " txt))
  373. (when (not (equal txt txt2))
  374. (beginning-of-line 1)
  375. (insert pre txt2 post)
  376. (delete-region (point) (point-at-eol))
  377. (org-set-tags nil t)))))
  378. (defun org-columns-edit-allowed ()
  379. "Edit the list of allowed values for the current property."
  380. (interactive)
  381. (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
  382. (get-text-property (point-at-bol) 'org-hd-marker)
  383. (point)))
  384. (key (get-char-property (point) 'org-columns-key))
  385. (key1 (concat key "_ALL"))
  386. (allowed (org-entry-get pom key1 t))
  387. nval)
  388. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  389. ;; FIXME: Write back to #+PROPERTY setting if that is needed.
  390. (setq nval (read-string "Allowed: " allowed))
  391. (org-entry-put
  392. (cond ((marker-position org-entry-property-inherited-from)
  393. org-entry-property-inherited-from)
  394. ((marker-position org-columns-top-level-marker)
  395. org-columns-top-level-marker)
  396. (t pom))
  397. key1 nval)))
  398. (defun org-columns-eval (form)
  399. (let (hidep)
  400. (save-excursion
  401. (beginning-of-line 1)
  402. ;; `next-line' is needed here, because it skips invisible line.
  403. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  404. (setq hidep (org-on-heading-p 1)))
  405. (eval form)
  406. (and hidep (hide-entry))))
  407. (defun org-columns-previous-allowed-value ()
  408. "Switch to the previous allowed value for this column."
  409. (interactive)
  410. (org-columns-next-allowed-value t))
  411. (defun org-columns-next-allowed-value (&optional previous)
  412. "Switch to the next allowed value for this column."
  413. (interactive)
  414. (org-columns-check-computed)
  415. (let* ((col (current-column))
  416. (key (get-char-property (point) 'org-columns-key))
  417. (value (get-char-property (point) 'org-columns-value))
  418. (bol (point-at-bol)) (eol (point-at-eol))
  419. (pom (or (get-text-property bol 'org-hd-marker)
  420. (point))) ; keep despite of compiler waring
  421. (line-overlays
  422. (delq nil (mapcar (lambda (x)
  423. (and (eq (overlay-buffer x) (current-buffer))
  424. (>= (overlay-start x) bol)
  425. (<= (overlay-start x) eol)
  426. x))
  427. org-columns-overlays)))
  428. (allowed (or (org-property-get-allowed-values pom key)
  429. (and (memq
  430. (nth 4 (assoc key org-columns-current-fmt-compiled))
  431. '(checkbox checkbox-n-of-m checkbox-percent))
  432. '("[ ]" "[X]"))))
  433. nval)
  434. (when (equal key "ITEM")
  435. (error "Cannot edit item headline from here"))
  436. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  437. (error "Allowed values for this property have not been defined"))
  438. (if (member key '("SCHEDULED" "DEADLINE"))
  439. (setq nval (if previous 'earlier 'later))
  440. (if previous (setq allowed (reverse allowed)))
  441. (if (member value allowed)
  442. (setq nval (car (cdr (member value allowed)))))
  443. (setq nval (or nval (car allowed)))
  444. (if (equal nval value)
  445. (error "Only one allowed value for this property")))
  446. (let ((inhibit-read-only t))
  447. (remove-text-properties (1- bol) eol '(read-only t))
  448. (unwind-protect
  449. (progn
  450. (setq org-columns-overlays
  451. (org-delete-all line-overlays org-columns-overlays))
  452. (mapc 'org-delete-overlay line-overlays)
  453. (org-columns-eval '(org-entry-put pom key nval)))
  454. (org-columns-display-here)))
  455. (move-to-column col)
  456. (cond
  457. ((equal major-mode 'org-agenda-mode)
  458. (org-agenda-redo))
  459. ((and (org-mode-p)
  460. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  461. (org-columns-update key)))))
  462. (defun org-verify-version (task)
  463. (cond
  464. ((eq task 'columns)
  465. (if (or (featurep 'xemacs)
  466. (< emacs-major-version 22))
  467. (error "Emacs 22 is required for the columns feature")))))
  468. (defun org-columns-open-link (&optional arg)
  469. (interactive "P")
  470. (let ((value (get-char-property (point) 'org-columns-value)))
  471. (org-open-link-from-string value arg)))
  472. (defun org-columns-get-format-and-top-level ()
  473. (let (fmt)
  474. (when (condition-case nil (org-back-to-heading) (error nil))
  475. (move-marker org-entry-property-inherited-from nil)
  476. (setq fmt (org-entry-get nil "COLUMNS" t)))
  477. (setq fmt (or fmt org-columns-default-format))
  478. (org-set-local 'org-columns-current-fmt fmt)
  479. (org-columns-compile-format fmt)
  480. (if (marker-position org-entry-property-inherited-from)
  481. (move-marker org-columns-top-level-marker
  482. org-entry-property-inherited-from)
  483. (move-marker org-columns-top-level-marker (point)))
  484. fmt))
  485. (defun org-columns ()
  486. "Turn on column view on an org-mode file."
  487. (interactive)
  488. (org-verify-version 'columns)
  489. (org-columns-remove-overlays)
  490. (move-marker org-columns-begin-marker (point))
  491. (let (beg end fmt cache maxwidths)
  492. (setq fmt (org-columns-get-format-and-top-level))
  493. (save-excursion
  494. (goto-char org-columns-top-level-marker)
  495. (setq beg (point))
  496. (unless org-columns-inhibit-recalculation
  497. (org-columns-compute-all))
  498. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  499. (point-max)))
  500. ;; Get and cache the properties
  501. (goto-char beg)
  502. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  503. (save-excursion
  504. (save-restriction
  505. (narrow-to-region beg end)
  506. (org-clock-sum))))
  507. (while (re-search-forward (concat "^" outline-regexp) end t)
  508. (push (cons (org-current-line) (org-entry-properties)) cache))
  509. (when cache
  510. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  511. (org-set-local 'org-columns-current-maxwidths maxwidths)
  512. (org-columns-display-here-title)
  513. (mapc (lambda (x)
  514. (goto-line (car x))
  515. (org-columns-display-here (cdr x)))
  516. cache)))))
  517. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  518. "Insert a new column, to the left of the current column."
  519. (interactive)
  520. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  521. cell)
  522. (setq prop (completing-read
  523. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  524. nil nil prop))
  525. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  526. (setq width (read-string "Column width: " (if width (number-to-string width))))
  527. (if (string-match "\\S-" width)
  528. (setq width (string-to-number width))
  529. (setq width nil))
  530. (setq fmt (completing-read "Summary [none]: "
  531. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  532. nil t))
  533. (if (string-match "\\S-" fmt)
  534. (setq fmt (intern fmt))
  535. (setq fmt nil))
  536. (if (eq fmt 'none) (setq fmt nil))
  537. (if editp
  538. (progn
  539. (setcar editp prop)
  540. (setcdr editp (list title width nil fmt)))
  541. (setq cell (nthcdr (1- (current-column))
  542. org-columns-current-fmt-compiled))
  543. (setcdr cell (cons (list prop title width nil fmt)
  544. (cdr cell))))
  545. (org-columns-store-format)
  546. (org-columns-redo)))
  547. (defun org-columns-delete ()
  548. "Delete the column at point from columns view."
  549. (interactive)
  550. (let* ((n (current-column))
  551. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  552. (when (y-or-n-p
  553. (format "Are you sure you want to remove column \"%s\"? " title))
  554. (setq org-columns-current-fmt-compiled
  555. (delq (nth n org-columns-current-fmt-compiled)
  556. org-columns-current-fmt-compiled))
  557. (org-columns-store-format)
  558. (org-columns-redo)
  559. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  560. (backward-char 1)))))
  561. (defun org-columns-edit-attributes ()
  562. "Edit the attributes of the current column."
  563. (interactive)
  564. (let* ((n (current-column))
  565. (info (nth n org-columns-current-fmt-compiled)))
  566. (apply 'org-columns-new info)))
  567. (defun org-columns-widen (arg)
  568. "Make the column wider by ARG characters."
  569. (interactive "p")
  570. (let* ((n (current-column))
  571. (entry (nth n org-columns-current-fmt-compiled))
  572. (width (or (nth 2 entry)
  573. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  574. (setq width (max 1 (+ width arg)))
  575. (setcar (nthcdr 2 entry) width)
  576. (org-columns-store-format)
  577. (org-columns-redo)))
  578. (defun org-columns-narrow (arg)
  579. "Make the column nrrower by ARG characters."
  580. (interactive "p")
  581. (org-columns-widen (- arg)))
  582. (defun org-columns-move-right ()
  583. "Swap this column with the one to the right."
  584. (interactive)
  585. (let* ((n (current-column))
  586. (cell (nthcdr n org-columns-current-fmt-compiled))
  587. e)
  588. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  589. (error "Cannot shift this column further to the right"))
  590. (setq e (car cell))
  591. (setcar cell (car (cdr cell)))
  592. (setcdr cell (cons e (cdr (cdr cell))))
  593. (org-columns-store-format)
  594. (org-columns-redo)
  595. (forward-char 1)))
  596. (defun org-columns-move-left ()
  597. "Swap this column with the one to the left."
  598. (interactive)
  599. (let* ((n (current-column)))
  600. (when (= n 0)
  601. (error "Cannot shift this column further to the left"))
  602. (backward-char 1)
  603. (org-columns-move-right)
  604. (backward-char 1)))
  605. (defun org-columns-store-format ()
  606. "Store the text version of the current columns format in appropriate place.
  607. This is either in the COLUMNS property of the node starting the current column
  608. display, or in the #+COLUMNS line of the current buffer."
  609. (let (fmt (cnt 0))
  610. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  611. (org-set-local 'org-columns-current-fmt fmt)
  612. (if (marker-position org-columns-top-level-marker)
  613. (save-excursion
  614. (goto-char org-columns-top-level-marker)
  615. (if (and (org-at-heading-p)
  616. (org-entry-get nil "COLUMNS"))
  617. (org-entry-put nil "COLUMNS" fmt)
  618. (goto-char (point-min))
  619. ;; Overwrite all #+COLUMNS lines....
  620. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  621. (setq cnt (1+ cnt))
  622. (replace-match (concat "#+COLUMNS: " fmt) t t))
  623. (unless (> cnt 0)
  624. (goto-char (point-min))
  625. (or (org-on-heading-p t) (outline-next-heading))
  626. (let ((inhibit-read-only t))
  627. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  628. (org-set-local 'org-columns-default-format fmt))))))
  629. (defvar org-overriding-columns-format nil
  630. "When set, overrides any other definition.")
  631. (defun org-columns-get-autowidth-alist (s cache)
  632. "Derive the maximum column widths from the format and the cache."
  633. (let ((start 0) rtn)
  634. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  635. (push (cons (match-string 1 s) 1) rtn)
  636. (setq start (match-end 0)))
  637. (mapc (lambda (x)
  638. (setcdr x (apply 'max
  639. (mapcar
  640. (lambda (y)
  641. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  642. cache))))
  643. rtn)
  644. rtn))
  645. (defun org-columns-compute-all ()
  646. "Compute all columns that have operators defined."
  647. (org-unmodified
  648. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  649. (let ((columns org-columns-current-fmt-compiled) col)
  650. (while (setq col (pop columns))
  651. (when (nth 3 col)
  652. (save-excursion
  653. (org-columns-compute (car col)))))))
  654. (defun org-columns-update (property)
  655. "Recompute PROPERTY, and update the columns display for it."
  656. (org-columns-compute property)
  657. (let (fmt val pos)
  658. (save-excursion
  659. (mapc (lambda (ov)
  660. (when (equal (org-overlay-get ov 'org-columns-key) property)
  661. (setq pos (org-overlay-start ov))
  662. (goto-char pos)
  663. (when (setq val (cdr (assoc property
  664. (get-text-property
  665. (point-at-bol) 'org-summaries))))
  666. (setq fmt (org-overlay-get ov 'org-columns-format))
  667. (org-overlay-put ov 'org-columns-value val)
  668. (org-overlay-put ov 'display (format fmt val)))))
  669. org-columns-overlays))))
  670. (defun org-columns-compute (property)
  671. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  672. (interactive)
  673. (let* ((re (concat "^" outline-regexp))
  674. (lmax 30) ; Does anyone use deeper levels???
  675. (lsum (make-vector lmax 0))
  676. (lflag (make-vector lmax nil))
  677. (level 0)
  678. (ass (assoc property org-columns-current-fmt-compiled))
  679. (format (nth 4 ass))
  680. (printf (nth 5 ass))
  681. (beg org-columns-top-level-marker)
  682. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  683. (save-excursion
  684. ;; Find the region to compute
  685. (goto-char beg)
  686. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  687. (goto-char end)
  688. ;; Walk the tree from the back and do the computations
  689. (while (re-search-backward re beg t)
  690. (setq sumpos (match-beginning 0)
  691. last-level level
  692. level (org-outline-level)
  693. val (org-entry-get nil property)
  694. valflag (and val (string-match "\\S-" val)))
  695. (cond
  696. ((< level last-level)
  697. ;; put the sum of lower levels here as a property
  698. (setq sum (aref lsum last-level) ; current sum
  699. flag (aref lflag last-level) ; any valid entries from children?
  700. str (org-columns-number-to-string sum format printf)
  701. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  702. useval (if flag str1 (if valflag val ""))
  703. sum-alist (get-text-property sumpos 'org-summaries))
  704. (if (assoc property sum-alist)
  705. (setcdr (assoc property sum-alist) useval)
  706. (push (cons property useval) sum-alist)
  707. (org-unmodified
  708. (add-text-properties sumpos (1+ sumpos)
  709. (list 'org-summaries sum-alist))))
  710. (when (and val (not (equal val (if flag str val))))
  711. (org-entry-put nil property (if flag str val)))
  712. ;; add current to current level accumulator
  713. (when (or flag valflag)
  714. (aset lsum level (+ (aref lsum level)
  715. (if flag sum (org-column-string-to-number
  716. (if flag str val) format))))
  717. (aset lflag level t))
  718. ;; clear accumulators for deeper levels
  719. (loop for l from (1+ level) to (1- lmax) do
  720. (aset lsum l 0)
  721. (aset lflag l nil)))
  722. ((>= level last-level)
  723. ;; add what we have here to the accumulator for this level
  724. (aset lsum level (+ (aref lsum level)
  725. (org-column-string-to-number (or val "0") format)))
  726. (and valflag (aset lflag level t)))
  727. (t (error "This should not happen")))))))
  728. (defun org-columns-redo ()
  729. "Construct the column display again."
  730. (interactive)
  731. (message "Recomputing columns...")
  732. (save-excursion
  733. (if (marker-position org-columns-begin-marker)
  734. (goto-char org-columns-begin-marker))
  735. (org-columns-remove-overlays)
  736. (if (org-mode-p)
  737. (call-interactively 'org-columns)
  738. (call-interactively 'org-agenda-columns)))
  739. (message "Recomputing columns...done"))
  740. (defun org-columns-not-in-agenda ()
  741. (if (eq major-mode 'org-agenda-mode)
  742. (error "This command is only allowed in Org-mode buffers")))
  743. (defun org-string-to-number (s)
  744. "Convert string to number, and interpret hh:mm:ss."
  745. (if (not (string-match ":" s))
  746. (string-to-number s)
  747. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  748. (while l
  749. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  750. sum)))
  751. (defun org-columns-number-to-string (n fmt &optional printf)
  752. "Convert a computed column number to a string value, according to FMT."
  753. (cond
  754. ((eq fmt 'add_times)
  755. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  756. (format "%d:%02d" h m)))
  757. ((eq fmt 'checkbox)
  758. (cond ((= n (floor n)) "[X]")
  759. ((> n 1.) "[-]")
  760. (t "[ ]")))
  761. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  762. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  763. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  764. (printf (format printf n))
  765. ((eq fmt 'currency)
  766. (format "%.2f" n))
  767. (t (number-to-string n))))
  768. (defun org-nofm-to-completion (n m &optional percent)
  769. (if (not percent)
  770. (format "[%d/%d]" n m)
  771. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  772. (defun org-column-string-to-number (s fmt)
  773. "Convert a column value to a number that can be used for column computing."
  774. (cond
  775. ((string-match ":" s)
  776. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  777. (while l
  778. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  779. sum))
  780. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  781. (if (equal s "[X]") 1. 0.000001))
  782. (t (string-to-number s))))
  783. (defun org-columns-uncompile-format (cfmt)
  784. "Turn the compiled columns format back into a string representation."
  785. (let ((rtn "") e s prop title op width fmt printf)
  786. (while (setq e (pop cfmt))
  787. (setq prop (car e)
  788. title (nth 1 e)
  789. width (nth 2 e)
  790. op (nth 3 e)
  791. fmt (nth 4 e)
  792. printf (nth 5 e))
  793. (cond
  794. ((eq fmt 'add_times) (setq op ":"))
  795. ((eq fmt 'checkbox) (setq op "X"))
  796. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  797. ((eq fmt 'checkbox-percent) (setq op "X%"))
  798. ((eq fmt 'add_numbers) (setq op "+"))
  799. ((eq fmt 'currency) (setq op "$")))
  800. (if (and op printf) (setq op (concat op ";" printf)))
  801. (if (equal title prop) (setq title nil))
  802. (setq s (concat "%" (if width (number-to-string width))
  803. prop
  804. (if title (concat "(" title ")"))
  805. (if op (concat "{" op "}"))))
  806. (setq rtn (concat rtn " " s)))
  807. (org-trim rtn)))
  808. (defun org-columns-compile-format (fmt)
  809. "Turn a column format string into an alist of specifications.
  810. The alist has one entry for each column in the format. The elements of
  811. that list are:
  812. property the property
  813. title the title field for the columns
  814. width the column width in characters, can be nil for automatic
  815. operator the operator if any
  816. format the output format for computed results, derived from operator
  817. printf a printf format for computed values"
  818. (let ((start 0) width prop title op f printf)
  819. (setq org-columns-current-fmt-compiled nil)
  820. (while (string-match
  821. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  822. fmt start)
  823. (setq start (match-end 0)
  824. width (match-string 1 fmt)
  825. prop (match-string 2 fmt)
  826. title (or (match-string 3 fmt) prop)
  827. op (match-string 4 fmt)
  828. f nil
  829. printf nil)
  830. (if width (setq width (string-to-number width)))
  831. (when (and op (string-match ";" op))
  832. (setq printf (substring op (match-end 0))
  833. op (substring op 0 (match-beginning 0))))
  834. (cond
  835. ((equal op "+") (setq f 'add_numbers))
  836. ((equal op "$") (setq f 'currency))
  837. ((equal op ":") (setq f 'add_times))
  838. ((equal op "X") (setq f 'checkbox))
  839. ((equal op "X/") (setq f 'checkbox-n-of-m))
  840. ((equal op "X%") (setq f 'checkbox-percent))
  841. )
  842. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  843. (setq org-columns-current-fmt-compiled
  844. (nreverse org-columns-current-fmt-compiled))))
  845. ;;; Dynamic block for Column view
  846. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  847. "Get the column view of the current buffer or subtree.
  848. The first optional argument MAXLEVEL sets the level limit. A
  849. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  850. empty rows, an empty row being one where all the column view
  851. specifiers except ITEM are empty. This function returns a list
  852. containing the title row and all other rows. Each row is a list
  853. of fields."
  854. (save-excursion
  855. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  856. (n (length title)) row tbl)
  857. (goto-char (point-min))
  858. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  859. (or (null maxlevel)
  860. (>= maxlevel
  861. (if org-odd-levels-only
  862. (/ (1+ (length (match-string 1))) 2)
  863. (length (match-string 1))))))
  864. (when (get-char-property (match-beginning 0) 'org-columns-key)
  865. (setq row nil)
  866. (loop for i from 0 to (1- n) do
  867. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  868. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  869. "")
  870. row))
  871. (setq row (nreverse row))
  872. (unless (and skip-empty-rows
  873. (eq 1 (length (delete "" (delete-dups row)))))
  874. (push row tbl))))
  875. (append (list title 'hline) (nreverse tbl)))))
  876. (defun org-dblock-write:columnview (params)
  877. "Write the column view table.
  878. PARAMS is a property list of parameters:
  879. :width enforce same column widths with <N> specifiers.
  880. :id the :ID: property of the entry where the columns view
  881. should be built, as a string. When `local', call locally.
  882. When `global' call column view with the cursor at the beginning
  883. of the buffer (usually this means that the whole buffer switches
  884. to column view).
  885. :hlines When t, insert a hline before each item. When a number, insert
  886. a hline before each level <= that number.
  887. :vlines When t, make each column a colgroup to enforce vertical lines.
  888. :maxlevel When set to a number, don't capture headlines below this level.
  889. :skip-empty-rows
  890. When t, skip rows where all specifiers other than ITEM are empty."
  891. (let ((pos (move-marker (make-marker) (point)))
  892. (hlines (plist-get params :hlines))
  893. (vlines (plist-get params :vlines))
  894. (maxlevel (plist-get params :maxlevel))
  895. (skip-empty-rows (plist-get params :skip-empty-rows))
  896. tbl id idpos nfields tmp)
  897. (save-excursion
  898. (save-restriction
  899. (when (setq id (plist-get params :id))
  900. (cond ((not id) nil)
  901. ((eq id 'global) (goto-char (point-min)))
  902. ((eq id 'local) nil)
  903. ((setq idpos (org-find-entry-with-id id))
  904. (goto-char idpos))
  905. (t (error "Cannot find entry with :ID: %s" id))))
  906. (org-columns)
  907. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  908. (setq nfields (length (car tbl)))
  909. (org-columns-quit)))
  910. (goto-char pos)
  911. (move-marker pos nil)
  912. (when tbl
  913. (when (plist-get params :hlines)
  914. (setq tmp nil)
  915. (while tbl
  916. (if (eq (car tbl) 'hline)
  917. (push (pop tbl) tmp)
  918. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  919. (if (and (not (eq (car tmp) 'hline))
  920. (or (eq hlines t)
  921. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  922. (push 'hline tmp)))
  923. (push (pop tbl) tmp)))
  924. (setq tbl (nreverse tmp)))
  925. (when vlines
  926. (setq tbl (mapcar (lambda (x)
  927. (if (eq 'hline x) x (cons "" x)))
  928. tbl))
  929. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  930. (setq pos (point))
  931. (insert (org-listtable-to-string tbl))
  932. (when (plist-get params :width)
  933. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  934. org-columns-current-widths "|")))
  935. (goto-char pos)
  936. (org-table-align))))
  937. (defun org-listtable-to-string (tbl)
  938. "Convert a listtable TBL to a string that contains the Org-mode table.
  939. The table still need to be alligned. The resulting string has no leading
  940. and tailing newline characters."
  941. (mapconcat
  942. (lambda (x)
  943. (cond
  944. ((listp x)
  945. (concat "|" (mapconcat 'identity x "|") "|"))
  946. ((eq x 'hline) "|-|")
  947. (t (error "Garbage in listtable: %s" x))))
  948. tbl "\n"))
  949. (defun org-insert-columns-dblock ()
  950. "Create a dynamic block capturing a column view table."
  951. (interactive)
  952. (let ((defaults '(:name "columnview" :hlines 1))
  953. (id (completing-read
  954. "Capture columns (local, global, entry with :ID: property) [local]: "
  955. (append '(("global") ("local"))
  956. (mapcar 'list (org-property-values "ID"))))))
  957. (if (equal id "") (setq id 'local))
  958. (if (equal id "global") (setq id 'global))
  959. (setq defaults (append defaults (list :id id)))
  960. (org-create-dblock defaults)
  961. (org-update-dblock)))
  962. ;;; Column view in the agenda
  963. (defvar org-agenda-view-columns-initially nil
  964. "When set, switch to columns view immediately after creating the agenda.")
  965. (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
  966. (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
  967. (defun org-agenda-columns ()
  968. "Turn on column view in the agenda."
  969. (interactive)
  970. (org-verify-version 'columns)
  971. (org-columns-remove-overlays)
  972. (move-marker org-columns-begin-marker (point))
  973. (let (fmt cache maxwidths m p a)
  974. (cond
  975. ((and (local-variable-p 'org-overriding-columns-format)
  976. org-overriding-columns-format)
  977. (setq fmt org-overriding-columns-format))
  978. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  979. (setq fmt (or (org-entry-get m "COLUMNS" t)
  980. (with-current-buffer (marker-buffer m)
  981. org-columns-default-format))))
  982. ((and (boundp 'org-columns-current-fmt)
  983. (local-variable-p 'org-columns-current-fmt)
  984. org-columns-current-fmt)
  985. (setq fmt org-columns-current-fmt))
  986. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  987. (setq m (get-text-property m 'org-hd-marker))
  988. (setq fmt (or (org-entry-get m "COLUMNS" t)
  989. (with-current-buffer (marker-buffer m)
  990. org-columns-default-format)))))
  991. (setq fmt (or fmt org-columns-default-format))
  992. (org-set-local 'org-columns-current-fmt fmt)
  993. (org-columns-compile-format fmt)
  994. (when org-agenda-columns-compute-summary-properties
  995. (org-agenda-colview-compute org-columns-current-fmt-compiled))
  996. (save-excursion
  997. ;; Get and cache the properties
  998. (goto-char (point-min))
  999. (while (not (eobp))
  1000. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  1001. (get-text-property (point) 'org-marker)))
  1002. (setq p (org-entry-properties m))
  1003. (when (or (not (setq a (assoc org-effort-property p)))
  1004. (not (string-match "\\S-" (or (cdr a) ""))))
  1005. ;; OK, the property is not defined. Use appointment duration?
  1006. (when (and org-agenda-columns-add-appointments-to-effort-sum
  1007. (setq d (get-text-property (point) 'duration)))
  1008. (setq d (org-minutes-to-hh:mm-string d))
  1009. (put-text-property 0 (length d) 'face 'org-warning d)
  1010. (push (cons org-effort-property d) p)))
  1011. (push (cons (org-current-line) p) cache))
  1012. (beginning-of-line 2))
  1013. (when cache
  1014. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  1015. (org-set-local 'org-columns-current-maxwidths maxwidths)
  1016. (org-columns-display-here-title)
  1017. (mapc (lambda (x)
  1018. (goto-line (car x))
  1019. (org-columns-display-here (cdr x)))
  1020. cache)
  1021. (when org-agenda-columns-show-summaries
  1022. (org-agenda-colview-summarize cache))))))
  1023. (defun org-agenda-colview-summarize (cache)
  1024. "Summarize the summarizable columns in column view in the agenda.
  1025. This will add overlays to the date lines, to show the summary for each day."
  1026. (let* ((fmt (mapcar (lambda (x)
  1027. (list (car x) (if (equal (car x) "CLOCKSUM")
  1028. 'add_times (nth 4 x))))
  1029. org-columns-current-fmt-compiled))
  1030. line c c1 stype props lsum entries prop v)
  1031. (catch 'exit
  1032. (when (delq nil (mapcar 'cadr fmt))
  1033. ;; OK, at least one summation column, it makes sense to try this
  1034. (goto-char (point-max))
  1035. (while t
  1036. (when (or (get-text-property (point) 'org-date-line)
  1037. (eq (get-text-property (point) 'face)
  1038. 'org-agenda-structure))
  1039. ;; OK, this is a date line that should be used
  1040. (setq line (org-current-line))
  1041. (setq entries nil c cache cache nil)
  1042. (while (setq c1 (pop c))
  1043. (if (> (car c1) line)
  1044. (push c1 entries)
  1045. (push c1 cache)))
  1046. ;; now ENTRIES are the ones we want to use, CACHE is the rest
  1047. ;; Compute the summaries for the properties we want,
  1048. ;; set nil properties for the rest.
  1049. (when (setq entries (mapcar 'cdr entries))
  1050. (setq props
  1051. (mapcar
  1052. (lambda (f)
  1053. (setq prop (car f) stype (nth 1 f))
  1054. (cond
  1055. ((equal prop "ITEM")
  1056. (cons prop (buffer-substring (point-at-bol)
  1057. (point-at-eol))))
  1058. ((not stype) (cons prop ""))
  1059. (t
  1060. ;; do the summary
  1061. (setq lsum 0)
  1062. (mapc (lambda (x)
  1063. (setq v (cdr (assoc prop x)))
  1064. (if v (setq lsum (+ lsum
  1065. (org-column-string-to-number
  1066. v stype)))))
  1067. entries)
  1068. (setq lsum (org-columns-number-to-string lsum stype))
  1069. (put-text-property
  1070. 0 (length lsum) 'face 'bold lsum)
  1071. (cons prop lsum))))
  1072. fmt))
  1073. (org-columns-display-here props)
  1074. (org-set-local 'org-agenda-columns-active t)))
  1075. (if (bobp) (throw 'exit t))
  1076. (beginning-of-line 0))))))
  1077. (defun org-agenda-colview-compute (fmt)
  1078. "Compute the relevant columns in the contributing source buffers."
  1079. (let ((files org-agenda-contributing-files)
  1080. (org-columns-begin-marker (make-marker))
  1081. (org-columns-top-level-marker (make-marker))
  1082. f fm a b)
  1083. (while (setq f (pop files))
  1084. (setq b (find-buffer-visiting f))
  1085. (with-current-buffer (or (buffer-base-buffer b) b)
  1086. (save-excursion
  1087. (save-restriction
  1088. (widen)
  1089. (org-unmodified
  1090. (remove-text-properties (point-min) (point-max)
  1091. '(org-summaries t)))
  1092. (goto-char (point-min))
  1093. (org-columns-get-format-and-top-level)
  1094. (while (setq fm (pop fmt))
  1095. (if (equal (car fm) "CLOCKSUM")
  1096. (org-clock-sum)
  1097. (when (and (nth 4 fm)
  1098. (setq a (assoc (car fm)
  1099. org-columns-current-fmt-compiled))
  1100. (equal (nth 4 a) (nth 4 fm)))
  1101. (org-columns-compute (car fm)))))))))))
  1102. (provide 'org-colview)
  1103. ;;; org-colview.el ends here