org-colview.el 56 KB

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