org-colview.el 53 KB

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