org-colview-xemacs.el 56 KB

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