org-colview-xemacs.el 65 KB

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