org-colview.el 48 KB

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