org-colview.el 46 KB

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