org-colview.el 43 KB

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