org-colview-xemacs.el 65 KB

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