org-colview.el 57 KB

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