org-colview-xemacs.el 65 KB

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