org-list.el 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Bastien Guerry <bzg AT altern DOT org>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 7.01trans
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with plain lists in Org-mode.
  28. ;;; Code:
  29. (eval-when-compile
  30. (require 'cl))
  31. (require 'org-macs)
  32. (require 'org-compat)
  33. (defvar org-blank-before-new-entry)
  34. (defvar org-M-RET-may-split-line)
  35. (defvar org-complex-heading-regexp)
  36. (defvar org-odd-levels-only)
  37. (declare-function org-invisible-p "org" ())
  38. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  39. (declare-function outline-next-heading "outline" ())
  40. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  41. (declare-function org-back-over-empty-lines "org" ())
  42. (declare-function org-skip-whitespace "org" ())
  43. (declare-function org-trim "org" (s))
  44. (declare-function org-get-indentation "org" (&optional line))
  45. (declare-function org-timer-item "org-timer" (&optional arg))
  46. (declare-function org-combine-plists "org" (&rest plists))
  47. (declare-function org-entry-get "org"
  48. (pom property &optional inherit literal-nil))
  49. (declare-function org-narrow-to-subtree "org" ())
  50. (declare-function org-show-subtree "org" ())
  51. (defgroup org-plain-lists nil
  52. "Options concerning plain lists in Org-mode."
  53. :tag "Org Plain lists"
  54. :group 'org-structure)
  55. (defcustom org-cycle-include-plain-lists t
  56. "When t, make TAB cycle visibility on plain list items.
  57. Cycling plain lists works only when the cursor is on a plain list
  58. item. When the cursor is on an outline heading, plain lists are
  59. treated as text. This is the most stable way of handling this,
  60. which is why it is the default.
  61. When this is the symbol `integrate', then during cycling, plain
  62. list items will *temporarily* be interpreted as outline headlines
  63. with a level given by 1000+i where i is the indentation of the
  64. bullet. This setting can lead to strange effects when switching
  65. visibility to `children', because the first \"child\" in a
  66. subtree decides what children should be listed. If that first
  67. \"child\" is a plain list item with an implied large level
  68. number, all true children and grand children of the outline
  69. heading will be exposed in a children' view."
  70. :group 'org-plain-lists
  71. :type '(choice
  72. (const :tag "Never" nil)
  73. (const :tag "With cursor in plain list (recommended)" t)
  74. (const :tag "As children of outline headings" integrate)))
  75. (defcustom org-list-demote-modify-bullet nil
  76. "Default bullet type installed when demoting an item.
  77. This is an association list, for each bullet type, this alist will point
  78. to the bullet that should be used when this item is demoted.
  79. For example,
  80. (setq org-list-demote-modify-bullet
  81. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  82. will make
  83. + Movies
  84. + Silence of the Lambs
  85. + My Cousin Vinny
  86. + Books
  87. + The Hunt for Red October
  88. + The Road to Omaha
  89. into
  90. + Movies
  91. - Silence of the Lambs
  92. - My Cousin Vinny
  93. + Books
  94. - The Hunt for Red October
  95. - The Road to Omaha"
  96. :group 'org-plain-lists
  97. :type '(repeat
  98. (cons
  99. (choice :tag "If the current bullet is "
  100. (const "-")
  101. (const "+")
  102. (const "*")
  103. (const "1.")
  104. (const "1)"))
  105. (choice :tag "demotion will change it to"
  106. (const "-")
  107. (const "+")
  108. (const "*")
  109. (const "1.")
  110. (const "1)")))))
  111. (defcustom org-plain-list-ordered-item-terminator t
  112. "The character that makes a line with leading number an ordered list item.
  113. Valid values are ?. and ?\). To get both terminators, use t. While
  114. ?. may look nicer, it creates the danger that a line with leading
  115. number may be incorrectly interpreted as an item. ?\) therefore is
  116. the safe choice."
  117. :group 'org-plain-lists
  118. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  119. (const :tag "paren like in \"2)\"" ?\))
  120. (const :tab "both" t)))
  121. (defcustom org-list-two-spaces-after-bullet-regexp nil
  122. "A regular expression matching bullets that should have 2 spaces after them.
  123. When nil, no bullet will have two spaces after them.
  124. When a string, it will be used as a regular expression. When the bullet
  125. type of a list is changed, the new bullet type will be matched against this
  126. regexp. If it matches, there will be two spaces instead of one after
  127. the bullet in each item of he list."
  128. :group 'org-plain-lists
  129. :type '(choice
  130. (const :tag "never" nil)
  131. (regexp)))
  132. (defcustom org-empty-line-terminates-plain-lists nil
  133. "Non-nil means an empty line ends all plain list levels.
  134. This is currently effective only during export. It should also have
  135. an effect for indentation and plain list folding, but it does not.
  136. When nil, empty lines are part of the preceding item."
  137. :group 'org-plain-lists
  138. :type 'boolean)
  139. (defcustom org-auto-renumber-ordered-lists t
  140. "Non-nil means automatically renumber ordered plain lists.
  141. Renumbering happens when the sequence have been changed with
  142. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  143. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  144. :group 'org-plain-lists
  145. :type 'boolean)
  146. (defcustom org-provide-checkbox-statistics t
  147. "Non-nil means update checkbox statistics after insert and toggle.
  148. When this is set, checkbox statistics is updated each time you
  149. either insert a new checkbox with \\[org-insert-todo-heading] or
  150. toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
  151. :group 'org-plain-lists
  152. :type 'boolean)
  153. (defcustom org-hierarchical-checkbox-statistics t
  154. "Non-nil means checkbox statistics counts only the state of direct children.
  155. When nil, all boxes below the cookie are counted.
  156. This can be set to nil on a per-node basis using a COOKIE_DATA property
  157. with the word \"recursive\" in the value."
  158. :group 'org-plain-lists
  159. :type 'boolean)
  160. (defcustom org-description-max-indent 20
  161. "Maximum indentation for the second line of a description list.
  162. When the indentation would be larger than this, it will become
  163. 5 characters instead."
  164. :group 'org-plain-lists
  165. :type 'integer)
  166. (defvar org-list-beginning-re
  167. "^\\([ \t]*\\)\\([-+]\\|[0-9]+[.)]\\) +\\(.*\\)$")
  168. (defcustom org-list-radio-list-templates
  169. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  170. % END RECEIVE ORGLST %n
  171. \\begin{comment}
  172. #+ORGLST: SEND %n org-list-to-latex
  173. -
  174. \\end{comment}\n")
  175. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  176. @c END RECEIVE ORGLST %n
  177. @ignore
  178. #+ORGLST: SEND %n org-list-to-texinfo
  179. -
  180. @end ignore\n")
  181. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  182. <!-- END RECEIVE ORGLST %n -->
  183. <!--
  184. #+ORGLST: SEND %n org-list-to-html
  185. -
  186. -->\n"))
  187. "Templates for radio lists in different major modes.
  188. All occurrences of %n in a template will be replaced with the name of the
  189. list, obtained by prompting the user."
  190. :group 'org-plain-lists
  191. :type '(repeat
  192. (list (symbol :tag "Major mode")
  193. (string :tag "Format"))))
  194. ;;;; Plain list items, including checkboxes
  195. ;;; Plain list items
  196. (defun org-item-re (&optional general)
  197. "Return the correct regular expression for plain lists.
  198. If GENERAL is non-nil, return the general regexp independent of the value
  199. of `org-plain-list-ordered-item-terminator'."
  200. (cond
  201. ((or general (eq org-plain-list-ordered-item-terminator t))
  202. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  203. ((= org-plain-list-ordered-item-terminator ?.)
  204. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  205. ((= org-plain-list-ordered-item-terminator ?\))
  206. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  207. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  208. (defun org-at-item-p ()
  209. "Is point in a line starting a hand-formatted item?"
  210. (save-excursion
  211. (goto-char (point-at-bol))
  212. (looking-at (org-item-re))))
  213. (defun org-at-item-bullet-p ()
  214. "Is point at the bullet of a plain list item?"
  215. (and (org-at-item-p)
  216. (not (member (char-after) '(?\ ?\t)))
  217. (< (point) (match-end 0))))
  218. (defun org-in-item-p ()
  219. "Is the cursor inside a plain list item.
  220. Does not have to be the first line."
  221. (save-excursion
  222. (condition-case nil
  223. (progn
  224. (org-beginning-of-item)
  225. (org-at-item-p)
  226. t)
  227. (error nil))))
  228. (defun org-insert-item (&optional checkbox)
  229. "Insert a new item at the current level.
  230. Return t when things worked, nil when we are not in an item."
  231. (when (save-excursion
  232. (condition-case nil
  233. (progn
  234. (org-beginning-of-item)
  235. (org-at-item-p)
  236. (if (org-invisible-p) (error "Invisible item"))
  237. t)
  238. (error nil)))
  239. (let* ((bul (match-string 0))
  240. (descp (save-excursion (goto-char (match-beginning 0))
  241. (beginning-of-line 1)
  242. (save-match-data
  243. (and (looking-at "[ \t]*\\(.*?\\) ::")
  244. (match-string 1)))))
  245. (empty-line-p (save-excursion
  246. (goto-char (match-beginning 0))
  247. (and (not (bobp))
  248. (or (beginning-of-line 0) t)
  249. (save-match-data
  250. (looking-at "[ \t]*$")))))
  251. (timerp (and descp
  252. (save-match-data
  253. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
  254. descp))))
  255. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  256. (match-end 0)))
  257. (blank-a (if org-empty-line-terminates-plain-lists
  258. nil
  259. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  260. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  261. pos)
  262. (if descp (setq checkbox nil))
  263. (if timerp
  264. (progn (org-timer-item) t)
  265. (cond
  266. ((and (org-at-item-p) (<= (point) eow))
  267. ;; before the bullet
  268. (beginning-of-line 1)
  269. (open-line (if blank 2 1)))
  270. ((<= (point) eow)
  271. (beginning-of-line 1))
  272. (t
  273. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  274. (end-of-line 1)
  275. (delete-horizontal-space))
  276. (newline (if blank 2 1))))
  277. (insert bul
  278. (if checkbox "[ ]" "")
  279. (if descp (concat (if checkbox " " "")
  280. (read-string "Term: ") " :: ") ""))
  281. (just-one-space)
  282. (setq pos (point))
  283. (end-of-line 1)
  284. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  285. (org-maybe-renumber-ordered-list)
  286. (and checkbox (org-update-checkbox-count-maybe))
  287. t)))
  288. ;;; Checkboxes
  289. (defun org-at-item-checkbox-p ()
  290. "Is point at a line starting a plain-list item with a checklet?"
  291. (and (org-at-item-p)
  292. (save-excursion
  293. (goto-char (match-end 0))
  294. (skip-chars-forward " \t")
  295. (looking-at "\\[[- X]\\]"))))
  296. (defun org-toggle-checkbox (&optional toggle-presence)
  297. "Toggle the checkbox in the current line.
  298. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  299. With double prefix, set checkbox to [-].
  300. When there is an active region, toggle status or presence of the checkbox
  301. in the first line, and make every item in the region have the same
  302. status or presence, respectively.
  303. If the cursor is in a headline, apply this to all checkbox items in the
  304. text below the heading."
  305. (interactive "P")
  306. (catch 'exit
  307. (let (beg end status first-present first-status blocked)
  308. (cond
  309. ((org-region-active-p)
  310. (setq beg (region-beginning) end (region-end)))
  311. ((org-on-heading-p)
  312. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  313. ((org-at-item-checkbox-p)
  314. (save-excursion
  315. (if (equal toggle-presence '(4))
  316. (progn
  317. (replace-match "")
  318. (goto-char (match-beginning 0))
  319. (just-one-space))
  320. (when (setq blocked (org-checkbox-blocked-p))
  321. (error "Checkbox blocked because of unchecked box in line %d"
  322. blocked))
  323. (replace-match
  324. (cond ((equal toggle-presence '(16)) "[-]")
  325. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  326. (t "[ ]"))
  327. t t)))
  328. (throw 'exit t))
  329. ((org-at-item-p)
  330. ;; add a checkbox
  331. (save-excursion
  332. (goto-char (match-end 0))
  333. (insert "[ ] "))
  334. (throw 'exit t))
  335. (t (error "Not at a checkbox or heading, and no active region")))
  336. (setq end (move-marker (make-marker) end))
  337. (save-excursion
  338. (goto-char beg)
  339. (setq first-present (org-at-item-checkbox-p)
  340. first-status
  341. (save-excursion
  342. (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
  343. (equal (match-string 1) "[X]"))))
  344. (while (< (point) end)
  345. (if toggle-presence
  346. (cond
  347. ((and first-present (org-at-item-checkbox-p))
  348. (save-excursion
  349. (replace-match "")
  350. (goto-char (match-beginning 0))
  351. (just-one-space)))
  352. ((and (not first-present) (not (org-at-item-checkbox-p))
  353. (org-at-item-p))
  354. (save-excursion
  355. (goto-char (match-end 0))
  356. (insert "[ ] "))))
  357. (when (org-at-item-checkbox-p)
  358. (setq status (equal (match-string 0) "[X]"))
  359. (replace-match
  360. (if first-status "[ ]" "[X]") t t)))
  361. (beginning-of-line 2)))))
  362. (org-update-checkbox-count-maybe))
  363. (defun org-reset-checkbox-state-subtree ()
  364. "Reset all checkboxes in an entry subtree."
  365. (interactive "*")
  366. (save-restriction
  367. (save-excursion
  368. (org-narrow-to-subtree)
  369. (org-show-subtree)
  370. (goto-char (point-min))
  371. (let ((end (point-max)))
  372. (while (< (point) end)
  373. (when (org-at-item-checkbox-p)
  374. (replace-match "[ ]" t t))
  375. (beginning-of-line 2))))
  376. (org-update-checkbox-count-maybe)))
  377. (defun org-checkbox-blocked-p ()
  378. "Is the current checkbox blocked from for being checked now?
  379. A checkbox is blocked if all of the following conditions are fulfilled:
  380. 1. The checkbox is not checked already.
  381. 2. The current entry has the ORDERED property set.
  382. 3. There is an unchecked checkbox in this entry before the current line."
  383. (catch 'exit
  384. (save-match-data
  385. (save-excursion
  386. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  387. (when (equal (match-string 0) "[X]")
  388. ;; the box is already checked!
  389. (throw 'exit nil))
  390. (let ((end (point-at-bol)))
  391. (condition-case nil (org-back-to-heading t)
  392. (error (throw 'exit nil)))
  393. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  394. (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  395. (org-current-line)
  396. nil))))))
  397. (defvar org-checkbox-statistics-hook nil
  398. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  399. This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
  400. be used to implement alternative ways of collecting statistics information.")
  401. (defun org-update-checkbox-count-maybe ()
  402. "Update checkbox statistics unless turned off by user."
  403. (when org-provide-checkbox-statistics
  404. (org-update-checkbox-count))
  405. (run-hooks 'org-checkbox-statistics-hook))
  406. (defun org-update-checkbox-count (&optional all)
  407. "Update the checkbox statistics in the current section.
  408. This will find all statistic cookies like [57%] and [6/12] and update them
  409. with the current numbers. With optional prefix argument ALL, do this for
  410. the whole buffer."
  411. (interactive "P")
  412. (save-excursion
  413. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  414. (beg (condition-case nil
  415. (progn (org-back-to-heading) (point))
  416. (error (point-min))))
  417. (end (move-marker (make-marker)
  418. (progn (outline-next-heading) (point))))
  419. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  420. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  421. (re-find (concat re "\\|" re-box))
  422. beg-cookie end-cookie is-percent c-on c-off lim new
  423. eline curr-ind next-ind continue-from startsearch
  424. (recursive
  425. (or (not org-hierarchical-checkbox-statistics)
  426. (string-match "\\<recursive\\>"
  427. (or (ignore-errors
  428. (org-entry-get nil "COOKIE_DATA"))
  429. ""))))
  430. (cstat 0)
  431. )
  432. (when all
  433. (goto-char (point-min))
  434. (outline-next-heading)
  435. (setq beg (point) end (point-max)))
  436. (goto-char end)
  437. ;; find each statistics cookie
  438. (while (and (re-search-backward re-find beg t)
  439. (not (save-match-data
  440. (and (org-on-heading-p)
  441. (string-match "\\<todo\\>"
  442. (downcase
  443. (or (org-entry-get
  444. nil "COOKIE_DATA")
  445. "")))))))
  446. (setq beg-cookie (match-beginning 1)
  447. end-cookie (match-end 1)
  448. cstat (+ cstat (if end-cookie 1 0))
  449. startsearch (point-at-eol)
  450. continue-from (match-beginning 0)
  451. is-percent (match-beginning 2)
  452. lim (cond
  453. ((org-on-heading-p) (outline-next-heading) (point))
  454. ((org-at-item-p) (org-end-of-item) (point))
  455. (t nil))
  456. c-on 0
  457. c-off 0)
  458. (when lim
  459. ;; find first checkbox for this cookie and gather
  460. ;; statistics from all that are at this indentation level
  461. (goto-char startsearch)
  462. (if (re-search-forward re-box lim t)
  463. (progn
  464. (org-beginning-of-item)
  465. (setq curr-ind (org-get-indentation))
  466. (setq next-ind curr-ind)
  467. (while (and (bolp) (org-at-item-p)
  468. (if recursive
  469. (<= curr-ind next-ind)
  470. (= curr-ind next-ind)))
  471. (save-excursion (end-of-line) (setq eline (point)))
  472. (if (re-search-forward re-box eline t)
  473. (if (member (match-string 2) '("[ ]" "[-]"))
  474. (setq c-off (1+ c-off))
  475. (setq c-on (1+ c-on))))
  476. (if (not recursive)
  477. (org-end-of-item)
  478. (end-of-line)
  479. (when (re-search-forward org-list-beginning-re lim t)
  480. (beginning-of-line)))
  481. (setq next-ind (org-get-indentation)))))
  482. (goto-char continue-from)
  483. ;; update cookie
  484. (when end-cookie
  485. (setq new (if is-percent
  486. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  487. (format "[%d/%d]" c-on (+ c-on c-off))))
  488. (goto-char beg-cookie)
  489. (insert new)
  490. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  491. ;; update items checkbox if it has one
  492. (when (org-at-item-p)
  493. (org-beginning-of-item)
  494. (when (and (> (+ c-on c-off) 0)
  495. (re-search-forward re-box (point-at-eol) t))
  496. (setq beg-cookie (match-beginning 2)
  497. end-cookie (match-end 2))
  498. (delete-region beg-cookie end-cookie)
  499. (goto-char beg-cookie)
  500. (cond ((= c-off 0) (insert "[X]"))
  501. ((= c-on 0) (insert "[ ]"))
  502. (t (insert "[-]")))
  503. )))
  504. (goto-char continue-from))
  505. (when (interactive-p)
  506. (message "Checkbox statistics updated %s (%d places)"
  507. (if all "in entire file" "in current outline entry") cstat)))))
  508. (defun org-get-checkbox-statistics-face ()
  509. "Select the face for checkbox statistics.
  510. The face will be `org-done' when all relevant boxes are checked. Otherwise
  511. it will be `org-todo'."
  512. (if (match-end 1)
  513. (if (equal (match-string 1) "100%")
  514. 'org-checkbox-statistics-done
  515. 'org-checkbox-statistics-todo)
  516. (if (and (> (match-end 2) (match-beginning 2))
  517. (equal (match-string 2) (match-string 3)))
  518. 'org-checkbox-statistics-done
  519. 'org-checkbox-statistics-todo)))
  520. (defun org-beginning-of-item ()
  521. "Go to the beginning of the current hand-formatted item.
  522. If the cursor is not in an item, throw an error."
  523. (interactive)
  524. (let ((pos (point))
  525. (limit (save-excursion
  526. (condition-case nil
  527. (progn
  528. (org-back-to-heading)
  529. (beginning-of-line 2) (point))
  530. (error (point-min)))))
  531. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  532. ind ind1)
  533. (if (org-at-item-p)
  534. (beginning-of-line 1)
  535. (beginning-of-line 1)
  536. (skip-chars-forward " \t")
  537. (setq ind (current-column))
  538. (if (catch 'exit
  539. (while t
  540. (beginning-of-line 0)
  541. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  542. (if (looking-at "[ \t]*$")
  543. (setq ind1 ind-empty)
  544. (skip-chars-forward " \t")
  545. (setq ind1 (current-column)))
  546. (if (< ind1 ind)
  547. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  548. nil
  549. (goto-char pos)
  550. (error "Not in an item")))))
  551. (defun org-end-of-item ()
  552. "Go to the end of the current hand-formatted item.
  553. If the cursor is not in an item, throw an error."
  554. (interactive)
  555. (let* ((pos (point))
  556. ind1
  557. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  558. (limit (save-excursion (outline-next-heading) (point)))
  559. (ind (save-excursion
  560. (org-beginning-of-item)
  561. (skip-chars-forward " \t")
  562. (current-column)))
  563. (end (catch 'exit
  564. (while t
  565. (beginning-of-line 2)
  566. (if (eobp) (throw 'exit (point)))
  567. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  568. (if (looking-at "[ \t]*$")
  569. (setq ind1 ind-empty)
  570. (skip-chars-forward " \t")
  571. (setq ind1 (current-column)))
  572. (if (<= ind1 ind)
  573. (throw 'exit (point-at-bol)))))))
  574. (if end
  575. (goto-char end)
  576. (goto-char pos)
  577. (error "Not in an item"))))
  578. (defun org-end-of-item-text-before-children ()
  579. "Move to the end of the item text, stops before the first child if any.
  580. Assumes that the cursor is in the first line of an item."
  581. (goto-char
  582. (min (save-excursion (org-end-of-item) (point))
  583. (save-excursion
  584. (goto-char (point-at-eol))
  585. (if (re-search-forward (concat "^" (org-item-re t)) nil 'move)
  586. (match-beginning 0)
  587. (point-max))))))
  588. (defun org-next-item ()
  589. "Move to the beginning of the next item in the current plain list.
  590. Error if not at a plain list, or if this is the last item in the list."
  591. (interactive)
  592. (let (ind ind1 (pos (point)))
  593. (org-beginning-of-item)
  594. (setq ind (org-get-indentation))
  595. (org-end-of-item)
  596. (setq ind1 (org-get-indentation))
  597. (unless (and (org-at-item-p) (= ind ind1))
  598. (goto-char pos)
  599. (error "On last item"))))
  600. (defun org-previous-item ()
  601. "Move to the beginning of the previous item in the current plain list.
  602. Error if not at a plain list, or if this is the first item in the list."
  603. (interactive)
  604. (let (beg ind ind1 (pos (point)))
  605. (org-beginning-of-item)
  606. (setq beg (point))
  607. (setq ind (org-get-indentation))
  608. (goto-char beg)
  609. (catch 'exit
  610. (while t
  611. (beginning-of-line 0)
  612. (if (looking-at "[ \t]*$")
  613. nil
  614. (if (<= (setq ind1 (org-get-indentation)) ind)
  615. (throw 'exit t)))
  616. (if (bobp) (throw 'exit t))))
  617. (condition-case nil
  618. (if (or (not (org-at-item-p))
  619. (< ind1 (1- ind)))
  620. (error "")
  621. (org-beginning-of-item))
  622. (error (goto-char pos)
  623. (error "On first item")))))
  624. (defun org-first-list-item-p ()
  625. "Is this heading the first item in a plain list?"
  626. (unless (org-at-item-p)
  627. (error "Not at a plain list item"))
  628. (save-excursion
  629. (org-beginning-of-item)
  630. (= (point) (save-excursion (org-beginning-of-item-list)))))
  631. (defun org-move-item-down ()
  632. "Move the plain list item at point down, i.e. swap with following item.
  633. Subitems (items with larger indentation) are considered part of the item,
  634. so this really moves item trees."
  635. (interactive)
  636. (let ((col (current-column))
  637. (pos (point))
  638. beg beg0 end end0 ind ind1 txt ne-end ne-beg)
  639. (org-beginning-of-item)
  640. (setq beg0 (point))
  641. (save-excursion
  642. (setq ne-beg (org-back-over-empty-lines))
  643. (setq beg (point)))
  644. (goto-char beg0)
  645. (setq ind (org-get-indentation))
  646. (org-end-of-item)
  647. (setq end0 (point))
  648. (setq ind1 (org-get-indentation))
  649. (setq ne-end (org-back-over-empty-lines))
  650. (setq end (point))
  651. (goto-char beg0)
  652. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  653. ;; include less whitespace
  654. (save-excursion
  655. (goto-char beg)
  656. (forward-line (- ne-beg ne-end))
  657. (setq beg (point))))
  658. (goto-char end0)
  659. (if (and (org-at-item-p) (= ind ind1))
  660. (progn
  661. (org-end-of-item)
  662. (org-back-over-empty-lines)
  663. (setq txt (buffer-substring beg end))
  664. (save-excursion
  665. (delete-region beg end))
  666. (setq pos (point))
  667. (insert txt)
  668. (goto-char pos) (org-skip-whitespace)
  669. (org-maybe-renumber-ordered-list)
  670. (move-to-column col))
  671. (goto-char pos)
  672. (move-to-column col)
  673. (error "Cannot move this item further down"))))
  674. (defun org-move-item-up (arg)
  675. "Move the plain list item at point up, i.e. swap with previous item.
  676. Subitems (items with larger indentation) are considered part of the item,
  677. so this really moves item trees."
  678. (interactive "p")
  679. (let ((col (current-column)) (pos (point))
  680. beg beg0 end ind ind1 txt
  681. ne-beg ne-ins ins-end)
  682. (org-beginning-of-item)
  683. (setq beg0 (point))
  684. (setq ind (org-get-indentation))
  685. (save-excursion
  686. (setq ne-beg (org-back-over-empty-lines))
  687. (setq beg (point)))
  688. (goto-char beg0)
  689. (org-end-of-item)
  690. (org-back-over-empty-lines)
  691. (setq end (point))
  692. (goto-char beg0)
  693. (catch 'exit
  694. (while t
  695. (beginning-of-line 0)
  696. (if (looking-at "[ \t]*$")
  697. (if org-empty-line-terminates-plain-lists
  698. (progn
  699. (goto-char pos)
  700. (error "Cannot move this item further up"))
  701. nil)
  702. (if (<= (setq ind1 (org-get-indentation)) ind)
  703. (throw 'exit t)))))
  704. (condition-case nil
  705. (org-beginning-of-item)
  706. (error (goto-char beg0)
  707. (move-to-column col)
  708. (error "Cannot move this item further up")))
  709. (setq ind1 (org-get-indentation))
  710. (if (and (org-at-item-p) (= ind ind1))
  711. (progn
  712. (setq ne-ins (org-back-over-empty-lines))
  713. (setq txt (buffer-substring beg end))
  714. (save-excursion
  715. (delete-region beg end))
  716. (setq pos (point))
  717. (insert txt)
  718. (setq ins-end (point))
  719. (goto-char pos) (org-skip-whitespace)
  720. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  721. ;; Move whitespace back to beginning
  722. (save-excursion
  723. (goto-char ins-end)
  724. (let ((kill-whole-line t))
  725. (kill-line (- ne-ins ne-beg)) (point)))
  726. (insert (make-string (- ne-ins ne-beg) ?\n)))
  727. (org-maybe-renumber-ordered-list)
  728. (move-to-column col))
  729. (goto-char pos)
  730. (move-to-column col)
  731. (error "Cannot move this item further up"))))
  732. (defun org-maybe-renumber-ordered-list ()
  733. "Renumber the ordered list at point if setup allows it.
  734. This tests the user option `org-auto-renumber-ordered-lists' before
  735. doing the renumbering."
  736. (interactive)
  737. (when (and org-auto-renumber-ordered-lists
  738. (org-at-item-p))
  739. (if (match-beginning 3)
  740. (org-renumber-ordered-list 1)
  741. (org-fix-bullet-type))))
  742. (defun org-maybe-renumber-ordered-list-safe ()
  743. (condition-case nil
  744. (save-excursion
  745. (org-maybe-renumber-ordered-list))
  746. (error nil)))
  747. (defun org-cycle-list-bullet (&optional which)
  748. "Cycle through the different itemize/enumerate bullets.
  749. This cycle the entire list level through the sequence:
  750. `-' -> `+' -> `*' -> `1.' -> `1)'
  751. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  752. 0 means `-', 1 means `+' etc."
  753. (interactive "P")
  754. (org-preserve-lc
  755. (org-beginning-of-item-list)
  756. (org-at-item-p)
  757. (beginning-of-line 1)
  758. (let ((current (match-string 0))
  759. (prevp (eq which 'previous))
  760. new old)
  761. (setq new (cond
  762. ((and (numberp which)
  763. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  764. ((string-match "-" current) (if prevp "1)" "+"))
  765. ((string-match "\\+" current)
  766. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  767. ((string-match "\\*" current) (if prevp "+" "1."))
  768. ((string-match "\\." current)
  769. (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
  770. ((string-match ")" current) (if prevp "1." "-"))
  771. (t (error "This should not happen"))))
  772. (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
  773. (setq old (match-string 2))
  774. (replace-match (concat "\\1" new)))
  775. (org-shift-item-indentation (- (length new) (length old)))
  776. (org-fix-bullet-type)
  777. (org-maybe-renumber-ordered-list))))
  778. (defun org-get-string-indentation (s)
  779. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  780. (let ((n -1) (i 0) (w tab-width) c)
  781. (catch 'exit
  782. (while (< (setq n (1+ n)) (length s))
  783. (setq c (aref s n))
  784. (cond ((= c ?\ ) (setq i (1+ i)))
  785. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  786. (t (throw 'exit t)))))
  787. i))
  788. (defun org-renumber-ordered-list (arg)
  789. "Renumber an ordered plain list.
  790. Cursor needs to be in the first line of an item, the line that starts
  791. with something like \"1.\" or \"2)\"."
  792. (interactive "p")
  793. (unless (and (org-at-item-p)
  794. (match-beginning 3))
  795. (error "This is not an ordered list"))
  796. (let ((line (org-current-line))
  797. (col (current-column))
  798. (ind (org-get-string-indentation
  799. (buffer-substring (point-at-bol) (match-beginning 3))))
  800. ;; (term (substring (match-string 3) -1))
  801. ind1 (n (1- arg))
  802. fmt bobp old new delta)
  803. ;; find where this list begins
  804. (org-beginning-of-item-list)
  805. (setq bobp (bobp))
  806. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  807. (setq fmt (concat "%d" (or (match-string 1) ".")))
  808. (save-excursion
  809. (goto-char (match-end 0))
  810. (if (looking-at "[ \t]*\\[@start:\\([0-9]+\\)")
  811. (setq n (1- (string-to-number (match-string 1))))))
  812. (beginning-of-line 0)
  813. ;; walk forward and replace these numbers
  814. (catch 'exit
  815. (while t
  816. (catch 'next
  817. (if bobp (setq bobp nil) (beginning-of-line 2))
  818. (if (eobp) (throw 'exit nil))
  819. (if (looking-at "[ \t]*$") (throw 'next nil))
  820. (skip-chars-forward " \t") (setq ind1 (current-column))
  821. (if (> ind1 ind) (throw 'next t))
  822. (if (< ind1 ind) (throw 'exit t))
  823. (if (not (org-at-item-p)) (throw 'exit nil))
  824. (setq old (match-string 2))
  825. (delete-region (match-beginning 2) (match-end 2))
  826. (goto-char (match-beginning 2))
  827. (insert (setq new (format fmt (setq n (1+ n)))))
  828. (setq delta (- (length new) (length old)))
  829. (org-shift-item-indentation delta)
  830. (if (= (org-current-line) line) (setq col (+ col delta))))))
  831. (org-goto-line line)
  832. (org-move-to-column col)))
  833. (defvar org-suppress-item-indentation) ; dynamically scoped parameter
  834. (defun org-fix-bullet-type (&optional force-bullet)
  835. "Make sure all items in this list have the same bullet as the first item.
  836. Also, fix the indentation."
  837. (interactive)
  838. (unless (org-at-item-p) (error "This is not a list"))
  839. (let ((line (org-current-line))
  840. (chars-from-eol (- (point-at-eol) (point)))
  841. (ind (current-indentation))
  842. ind1 bullet oldbullet)
  843. ;; find where this list begins
  844. (org-beginning-of-item-list)
  845. (beginning-of-line 1)
  846. ;; find out what the bullet type is
  847. (looking-at "[ \t]*\\(\\S-+\\)")
  848. (setq bullet (concat (or force-bullet (match-string 1)) " "))
  849. (if (and org-list-two-spaces-after-bullet-regexp
  850. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  851. (setq bullet (concat bullet " ")))
  852. ;; walk forward and replace these numbers
  853. (beginning-of-line 0)
  854. (catch 'exit
  855. (while t
  856. (catch 'next
  857. (beginning-of-line 2)
  858. (if (eobp) (throw 'exit nil))
  859. (if (looking-at "[ \t]*$") (throw 'next nil))
  860. (skip-chars-forward " \t") (setq ind1 (current-column))
  861. (if (> ind1 ind) (throw 'next t))
  862. (if (< ind1 ind) (throw 'exit t))
  863. (if (not (org-at-item-p)) (throw 'exit nil))
  864. (skip-chars-forward " \t")
  865. (looking-at "\\S-+ *")
  866. (setq oldbullet (match-string 0))
  867. (unless (equal bullet oldbullet) (replace-match bullet))
  868. (org-shift-item-indentation (- (length bullet)
  869. (length oldbullet))))))
  870. (org-goto-line line)
  871. (goto-char (max (point-at-bol) (- (point-at-eol) chars-from-eol)))
  872. (if (string-match "[0-9]" bullet)
  873. (org-renumber-ordered-list 1))))
  874. (defun org-shift-item-indentation (delta)
  875. "Shift the indentation in current item by DELTA."
  876. (unless (org-bound-and-true-p org-suppress-item-indentation)
  877. (save-excursion
  878. (let ((beg (point-at-bol))
  879. (end (progn (org-end-of-item) (point)))
  880. i)
  881. (goto-char end)
  882. (beginning-of-line 0)
  883. (while (> (point) beg)
  884. (when (looking-at "[ \t]*\\S-")
  885. ;; this is not an empty line
  886. (setq i (org-get-indentation))
  887. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  888. (indent-line-to i)))
  889. (beginning-of-line 0))))))
  890. (defun org-beginning-of-item-list ()
  891. "Go to the beginning of the current item list.
  892. I.e. to the first item in this list."
  893. (interactive)
  894. (org-beginning-of-item)
  895. (let ((pos (point-at-bol))
  896. (ind (org-get-indentation))
  897. ind1)
  898. ;; find where this list begins
  899. (catch 'exit
  900. (while t
  901. (catch 'next
  902. (beginning-of-line 0)
  903. (if (looking-at "[ \t]*$")
  904. (throw (if (bobp) 'exit 'next) t))
  905. (skip-chars-forward " \t") (setq ind1 (current-column))
  906. (if (or (< ind1 ind)
  907. (and (= ind1 ind)
  908. (not (org-at-item-p)))
  909. (and (= (point-at-bol) (point-min))
  910. (setq pos (point-min))))
  911. (throw 'exit t)
  912. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  913. (goto-char pos)))
  914. (defun org-end-of-item-list ()
  915. "Go to the end of the current item list.
  916. I.e. to the text after the last item."
  917. (interactive)
  918. (org-beginning-of-item)
  919. (let ((pos (point-at-bol))
  920. (ind (org-get-indentation))
  921. ind1)
  922. ;; find where this list begins
  923. (catch 'exit
  924. (while t
  925. (catch 'next
  926. (beginning-of-line 2)
  927. (if (looking-at "[ \t]*$")
  928. (if (eobp)
  929. (progn (setq pos (point)) (throw 'exit t))
  930. (throw 'next t)))
  931. (skip-chars-forward " \t") (setq ind1 (current-column))
  932. (if (or (< ind1 ind)
  933. (and (= ind1 ind)
  934. (not (org-at-item-p)))
  935. (eobp))
  936. (progn
  937. (setq pos (point-at-bol))
  938. (throw 'exit t))))))
  939. (goto-char pos)))
  940. (defvar org-last-indent-begin-marker (make-marker))
  941. (defvar org-last-indent-end-marker (make-marker))
  942. (defun org-outdent-item (arg)
  943. "Outdent a local list item, but not its children."
  944. (interactive "p")
  945. (org-indent-item-tree (- arg) 'no-subtree))
  946. (defun org-indent-item (arg)
  947. "Indent a local list item, but not its children."
  948. (interactive "p")
  949. (org-indent-item-tree arg 'no-subtree))
  950. (defun org-outdent-item-tree (arg &optional no-subtree)
  951. "Outdent a local list item including its children.
  952. If NO-SUBTREE is set, only outdent the item itself, not its children."
  953. (interactive "p")
  954. (org-indent-item-tree (- arg) no-subtree))
  955. (defun org-indent-item-tree (arg &optional no-subtree)
  956. "Indent a local list item including its children.
  957. If NO-SUBTREE is set, only indent the item itself, not its children."
  958. (interactive "p")
  959. (and (org-region-active-p) (org-cursor-to-region-beginning))
  960. (unless (org-at-item-p)
  961. (error "Not on an item"))
  962. (let (beg end ind ind1 ind-bul delta ind-down ind-up firstp)
  963. (setq firstp (org-first-list-item-p))
  964. (save-excursion
  965. (setq end (and (org-region-active-p) (region-end)))
  966. (if (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  967. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  968. (setq beg org-last-indent-begin-marker
  969. end org-last-indent-end-marker)
  970. (org-beginning-of-item)
  971. (setq beg (move-marker org-last-indent-begin-marker (point)))
  972. (if no-subtree
  973. (org-end-of-item-text-before-children)
  974. (org-end-of-item))
  975. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  976. (goto-char beg)
  977. (setq ind-bul (org-item-indent-positions)
  978. ind (caar ind-bul)
  979. ind-down (car (nth 2 ind-bul))
  980. ind-up (car (nth 1 ind-bul))
  981. delta (if (> arg 0)
  982. (if ind-down (- ind-down ind) 2)
  983. (if ind-up (- ind-up ind) -2)))
  984. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  985. (while (< (point) end)
  986. (beginning-of-line 1)
  987. (skip-chars-forward " \t") (setq ind1 (current-column))
  988. (delete-region (point-at-bol) (point))
  989. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  990. (beginning-of-line 2)))
  991. (org-fix-bullet-type
  992. (and (> arg 0)
  993. (not firstp)
  994. (cdr (assoc (cdr (nth 0 ind-bul)) org-list-demote-modify-bullet))))
  995. (org-maybe-renumber-ordered-list-safe)
  996. (save-excursion
  997. (beginning-of-line 0)
  998. (condition-case nil (org-beginning-of-item) (error nil))
  999. (org-maybe-renumber-ordered-list-safe))))
  1000. (defun org-item-indent-positions ()
  1001. "Return indentation for plain list items.
  1002. This returns a list with three values: The current indentation, the
  1003. parent indentation and the indentation a child should have.
  1004. Assumes cursor in item line."
  1005. (let* ((bolpos (point-at-bol))
  1006. (ind (org-get-indentation))
  1007. (bullet (org-get-bullet))
  1008. ind-down ind-up bullet-up bullet-down pos)
  1009. (save-excursion
  1010. (org-beginning-of-item-list)
  1011. (skip-chars-backward "\n\r \t")
  1012. (when (org-in-item-p)
  1013. (org-beginning-of-item)
  1014. (setq ind-up (org-get-indentation))
  1015. (setq bullet-up (org-get-bullet))))
  1016. (setq pos (point))
  1017. (save-excursion
  1018. (cond
  1019. ((and (condition-case nil (progn (org-previous-item) t)
  1020. (error nil))
  1021. (or (forward-char 1) t)
  1022. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  1023. (setq ind-down (org-get-indentation)
  1024. bullet-down (org-get-bullet)))
  1025. ((and (goto-char pos)
  1026. (org-at-item-p))
  1027. (goto-char (match-end 0))
  1028. (skip-chars-forward " \t")
  1029. (setq ind-down (current-column)
  1030. bullet-down (org-get-bullet)))))
  1031. (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
  1032. (setq bullet-down (concat "1" (match-string 1 bullet-down))))
  1033. (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
  1034. (setq bullet-up (concat "1" (match-string 1 bullet-up))))
  1035. (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
  1036. (setq bullet (concat "1" (match-string 1 bullet))))
  1037. (list (cons ind bullet)
  1038. (cons ind-up bullet-up)
  1039. (cons ind-down bullet-down))))
  1040. (defvar org-tab-ind-state) ; defined in org.el
  1041. (defun org-cycle-item-indentation ()
  1042. (let ((org-suppress-item-indentation t)
  1043. (org-adapt-indentation nil))
  1044. (cond
  1045. ((and (looking-at "[ \t]*$")
  1046. (org-looking-back "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[).]\\)[ \t]+"))
  1047. (setq this-command 'org-cycle-item-indentation)
  1048. (if (eq last-command 'org-cycle-item-indentation)
  1049. (condition-case nil
  1050. (progn (org-outdent-item 1)
  1051. (if (equal org-tab-ind-state (org-get-indentation))
  1052. (org-outdent-item 1))
  1053. (end-of-line 1))
  1054. (error
  1055. (progn
  1056. (while (< (org-get-indentation) org-tab-ind-state)
  1057. (progn (org-indent-item 1) (end-of-line 1)))
  1058. (setq this-command 'org-cycle))))
  1059. (setq org-tab-ind-state (org-get-indentation))
  1060. (org-indent-item 1))
  1061. t))))
  1062. (defun org-get-bullet ()
  1063. (save-excursion
  1064. (goto-char (point-at-bol))
  1065. (and (looking-at
  1066. "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\(\\*\\)\\)\\( \\|$\\)")
  1067. (or (match-string 2) (match-string 4)))))
  1068. ;;; Send and receive lists
  1069. (defun org-list-parse-list (&optional delete)
  1070. "Parse the list at point and maybe DELETE it.
  1071. Return a list containing first level items as strings and
  1072. sublevels as a list of strings."
  1073. (let* ((item-beginning (org-list-item-beginning))
  1074. (start (car item-beginning))
  1075. (end (save-excursion
  1076. (goto-char (org-list-end (cdr item-beginning)))
  1077. (org-back-over-empty-lines)
  1078. (point)))
  1079. output itemsep ltype)
  1080. (while (re-search-forward org-list-beginning-re end t)
  1081. (goto-char (match-beginning 3))
  1082. (save-match-data
  1083. (cond ((string-match "[0-9]" (match-string 2))
  1084. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  1085. ltype 'ordered))
  1086. ((string-match "^.*::" (match-string 0))
  1087. (setq itemsep "[-+]" ltype 'descriptive))
  1088. (t (setq itemsep "[-+]" ltype 'unordered))))
  1089. (let* ((indent1 (match-string 1))
  1090. (nextitem (save-excursion
  1091. (save-match-data
  1092. (or (and (re-search-forward
  1093. (concat "^" indent1 itemsep " *?") end t)
  1094. (match-beginning 0)) end))))
  1095. (item (buffer-substring
  1096. (point)
  1097. (or (and (org-re-search-forward-unprotected
  1098. org-list-beginning-re end t)
  1099. (goto-char (match-beginning 0)))
  1100. (goto-char end))))
  1101. (nextindent (match-string 1))
  1102. (item (org-trim item))
  1103. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  1104. (replace-match (if (equal (match-string 1 item) " ")
  1105. "[CBOFF]"
  1106. "[CBON]")
  1107. t nil item)
  1108. item)))
  1109. (push item output)
  1110. (when (> (length nextindent)
  1111. (length indent1))
  1112. (narrow-to-region (point) nextitem)
  1113. (push (org-list-parse-list) output)
  1114. (widen))))
  1115. (when delete (delete-region start end))
  1116. (setq output (nreverse output))
  1117. (push ltype output)))
  1118. (defun org-list-item-beginning ()
  1119. "Find the beginning of the list item.
  1120. Return a cons which car is the beginning position of the item and
  1121. cdr is the indentation string."
  1122. (save-excursion
  1123. (if (not (or (looking-at org-list-beginning-re)
  1124. (re-search-backward
  1125. org-list-beginning-re nil t)))
  1126. (progn (goto-char (point-min)) (point))
  1127. (cons (match-beginning 0) (match-string 1)))))
  1128. (defun org-list-goto-true-beginning ()
  1129. "Go to the beginning of the list at point."
  1130. (beginning-of-line 1)
  1131. (while (looking-at org-list-beginning-re)
  1132. (beginning-of-line 0))
  1133. (progn
  1134. (re-search-forward org-list-beginning-re nil t)
  1135. (goto-char (match-beginning 0))))
  1136. (defun org-list-make-subtree ()
  1137. "Convert the plain list at point into a subtree."
  1138. (interactive)
  1139. (org-list-goto-true-beginning)
  1140. (let ((list (org-list-parse-list t)) nstars)
  1141. (save-excursion
  1142. (if (condition-case nil
  1143. (org-back-to-heading)
  1144. (error nil))
  1145. (progn (re-search-forward org-complex-heading-regexp nil t)
  1146. (setq nstars (length (match-string 1))))
  1147. (setq nstars 0)))
  1148. (org-list-make-subtrees list (1+ nstars))))
  1149. (defun org-list-make-subtrees (list level)
  1150. "Convert LIST into subtrees starting at LEVEL."
  1151. (if (symbolp (car list))
  1152. (org-list-make-subtrees (cdr list) level)
  1153. (mapcar (lambda (item)
  1154. (if (stringp item)
  1155. (insert (make-string
  1156. (if org-odd-levels-only
  1157. (1- (* 2 level)) level) ?*) " " item "\n")
  1158. (org-list-make-subtrees item (1+ level))))
  1159. list)))
  1160. (defun org-list-end (indent)
  1161. "Return the position of the end of the list.
  1162. INDENT is the indentation of the list, as a string."
  1163. (save-excursion
  1164. (catch 'exit
  1165. (while (or (looking-at org-list-beginning-re)
  1166. (looking-at (concat "^" indent "[ \t]+\\|^$"))
  1167. (> (or (get-text-property (point) 'original-indentation) -1)
  1168. (length indent)))
  1169. (if (eq (point) (point-max))
  1170. (throw 'exit (point-max)))
  1171. (forward-line 1)))
  1172. (point)))
  1173. (defun org-list-insert-radio-list ()
  1174. "Insert a radio list template appropriate for this major mode."
  1175. (interactive)
  1176. (let* ((e (assq major-mode org-list-radio-list-templates))
  1177. (txt (nth 1 e))
  1178. name pos)
  1179. (unless e (error "No radio list setup defined for %s" major-mode))
  1180. (setq name (read-string "List name: "))
  1181. (while (string-match "%n" txt)
  1182. (setq txt (replace-match name t t txt)))
  1183. (or (bolp) (insert "\n"))
  1184. (setq pos (point))
  1185. (insert txt)
  1186. (goto-char pos)))
  1187. (defun org-list-send-list (&optional maybe)
  1188. "Send a transformed version of this list to the receiver position.
  1189. With argument MAYBE, fail quietly if no transformation is defined for
  1190. this list."
  1191. (interactive)
  1192. (catch 'exit
  1193. (unless (org-at-item-p) (error "Not at a list"))
  1194. (save-excursion
  1195. (org-list-goto-true-beginning)
  1196. (beginning-of-line 0)
  1197. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1198. (if maybe
  1199. (throw 'exit nil)
  1200. (error "Don't know how to transform this list"))))
  1201. (let* ((name (match-string 1))
  1202. (transform (intern (match-string 2)))
  1203. (item-beginning (org-list-item-beginning))
  1204. (list (save-excursion (org-list-goto-true-beginning)
  1205. (org-list-parse-list)))
  1206. txt beg)
  1207. (unless (fboundp transform)
  1208. (error "No such transformation function %s" transform))
  1209. (let ((txt (funcall transform list)))
  1210. ;; Find the insertion place
  1211. (save-excursion
  1212. (goto-char (point-min))
  1213. (unless (re-search-forward
  1214. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1215. (error "Don't know where to insert translated list"))
  1216. (goto-char (match-beginning 0))
  1217. (beginning-of-line 2)
  1218. (setq beg (point))
  1219. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1220. (error "Cannot find end of insertion region"))
  1221. (beginning-of-line 1)
  1222. (delete-region beg (point))
  1223. (goto-char beg)
  1224. (insert txt "\n")))
  1225. (message "List converted and installed at receiver location"))))
  1226. (defun org-list-to-generic (list params)
  1227. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1228. Valid parameters PARAMS are
  1229. :ustart String to start an unordered list
  1230. :uend String to end an unordered list
  1231. :ostart String to start an ordered list
  1232. :oend String to end an ordered list
  1233. :dstart String to start a descriptive list
  1234. :dend String to end a descriptive list
  1235. :dtstart String to start a descriptive term
  1236. :dtend String to end a descriptive term
  1237. :ddstart String to start a description
  1238. :ddend String to end a description
  1239. :splice When set to t, return only list body lines, don't wrap
  1240. them into :[u/o]start and :[u/o]end. Default is nil.
  1241. :istart String to start a list item
  1242. :iend String to end a list item
  1243. :isep String to separate items
  1244. :lsep String to separate sublists
  1245. :cboff String to insert for an unchecked checkbox
  1246. :cbon String to insert for a checked checkbox"
  1247. (interactive)
  1248. (let* ((p params) sublist
  1249. (splicep (plist-get p :splice))
  1250. (ostart (plist-get p :ostart))
  1251. (oend (plist-get p :oend))
  1252. (ustart (plist-get p :ustart))
  1253. (uend (plist-get p :uend))
  1254. (dstart (plist-get p :dstart))
  1255. (dend (plist-get p :dend))
  1256. (dtstart (plist-get p :dtstart))
  1257. (dtend (plist-get p :dtend))
  1258. (ddstart (plist-get p :ddstart))
  1259. (ddend (plist-get p :ddend))
  1260. (istart (plist-get p :istart))
  1261. (iend (plist-get p :iend))
  1262. (isep (plist-get p :isep))
  1263. (lsep (plist-get p :lsep))
  1264. (cbon (plist-get p :cbon))
  1265. (cboff (plist-get p :cboff)))
  1266. (let ((wrapper
  1267. (cond ((eq (car list) 'ordered)
  1268. (concat ostart "\n%s" oend "\n"))
  1269. ((eq (car list) 'unordered)
  1270. (concat ustart "\n%s" uend "\n"))
  1271. ((eq (car list) 'descriptive)
  1272. (concat dstart "\n%s" dend "\n"))))
  1273. rtn term defstart defend)
  1274. (while (setq sublist (pop list))
  1275. (cond ((symbolp sublist) nil)
  1276. ((stringp sublist)
  1277. (when (string-match "^\\(.*\\) ::" sublist)
  1278. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1279. (match-string 1 sublist))))
  1280. (setq sublist (substring sublist (1+ (length term)))))
  1281. (if (string-match "\\[CBON\\]" sublist)
  1282. (setq sublist (replace-match cbon t t sublist)))
  1283. (if (string-match "\\[CBOFF\\]" sublist)
  1284. (setq sublist (replace-match cboff t t sublist)))
  1285. (if (string-match "\\[-\\]" sublist)
  1286. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1287. (setq rtn (concat rtn istart term ddstart
  1288. sublist ddend iend isep)))
  1289. (t (setq rtn (concat rtn ;; previous list
  1290. lsep ;; list separator
  1291. (org-list-to-generic sublist p)
  1292. lsep ;; list separator
  1293. )))))
  1294. (format wrapper rtn))))
  1295. (defun org-list-to-latex (list &optional params)
  1296. "Convert LIST into a LaTeX list.
  1297. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1298. with overruling parameters for `org-list-to-generic'."
  1299. (org-list-to-generic
  1300. list
  1301. (org-combine-plists
  1302. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1303. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1304. :dstart "\\begin{description}" :dend "\\end{description}"
  1305. :dtstart "[" :dtend "]"
  1306. :ddstart "" :ddend ""
  1307. :istart "\\item " :iend ""
  1308. :isep "\n" :lsep "\n"
  1309. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1310. params)))
  1311. (defun org-list-to-html (list &optional params)
  1312. "Convert LIST into a HTML list.
  1313. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1314. with overruling parameters for `org-list-to-generic'."
  1315. (org-list-to-generic
  1316. list
  1317. (org-combine-plists
  1318. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1319. :ustart "<ul>" :uend "</ul>"
  1320. :dstart "<dl>" :dend "</dl>"
  1321. :dtstart "<dt>" :dtend "</dt>"
  1322. :ddstart "<dd>" :ddend "</dd>"
  1323. :istart "<li>" :iend "</li>"
  1324. :isep "\n" :lsep "\n"
  1325. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1326. params)))
  1327. (defun org-list-to-texinfo (list &optional params)
  1328. "Convert LIST into a Texinfo list.
  1329. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1330. with overruling parameters for `org-list-to-generic'."
  1331. (org-list-to-generic
  1332. list
  1333. (org-combine-plists
  1334. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1335. :ustart "@enumerate" :uend "@end enumerate"
  1336. :dstart "@table" :dend "@end table"
  1337. :dtstart "@item " :dtend "\n"
  1338. :ddstart "" :ddend ""
  1339. :istart "@item\n" :iend ""
  1340. :isep "\n" :lsep "\n"
  1341. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1342. params)))
  1343. (provide 'org-list)
  1344. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1345. ;;; org-list.el ends here