org-list.el 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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
  125. bullet type of a list is changed, the new bullet type will be
  126. matched against this regexp. If it matches, there will be two
  127. spaces instead of one after the bullet in each item of the 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. Otherwise, look for `org-list-end-regexp'."
  135. :group 'org-plain-lists
  136. :type 'boolean)
  137. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  138. "Regexp matching the end of all plain list levels.
  139. It must start with \"^\" and end with \"\\n\". It defaults to 2
  140. blank lines. `org-empty-line-terminates-plain-lists' has
  141. precedence over it."
  142. :group 'org-plain-lists
  143. :type 'string)
  144. (defcustom org-list-automatic-rules '((bullet . t)
  145. (checkbox . t)
  146. (indent . t)
  147. (insert . t)
  148. (renumber . t))
  149. "Non-nil means apply set of rules when acting on lists.
  150. By default, automatic actions are taken when using
  151. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  152. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  153. \\[org-shiftmetaleft], \\[org-ctrl-c-minus],
  154. \\[org-toggle-checkbox] or \\[org-insert-todo-heading]. You can
  155. disable individually these rules by setting them to nil. Valid
  156. rules are:
  157. bullet when non-nil, cycling bullet do not allow lists at
  158. column 0 to have * as a bullet and descriptions lists
  159. to be numbered.
  160. checkbox when non-nil, checkbox statistics is updated each time
  161. you either insert a new checkbox or toggle a checkbox.
  162. It also prevents from inserting a checkbox in a
  163. description item.
  164. indent when non-nil indenting or outdenting list top-item with
  165. its subtree will move the whole list, all moves that
  166. would break list will be forbidden, and outdenting a
  167. list whose bullet is * to column 0 will change that
  168. bullet to -.
  169. insert when non-nil, trying to insert an item inside a block
  170. will insert it right before the block instead of
  171. throwing an error.
  172. renumber when non-nil, renumber ordered plain lists whenever it
  173. is modified. You can always use \\[org-ctrl-c-ctrl-c]
  174. to trigger renumbering."
  175. :group 'org-plain-lists
  176. :type '(alist :tag "Sets of rules"
  177. :key-type
  178. (choice
  179. (const :tag "Bullet" bullet)
  180. (const :tag "Checkbox" checkbox)
  181. (const :tag "Indent" indent)
  182. (const :tag "Insert" insert)
  183. (const :tag "Renumber" renumber))
  184. :value-type
  185. (boolean :tag "Activate" :value t)))
  186. (defcustom org-hierarchical-checkbox-statistics t
  187. "Non-nil means checkbox statistics counts only the state of direct children.
  188. When nil, all boxes below the cookie are counted.
  189. This can be set to nil on a per-node basis using a COOKIE_DATA property
  190. with the word \"recursive\" in the value."
  191. :group 'org-plain-lists
  192. :type 'boolean)
  193. (defcustom org-description-max-indent 20
  194. "Maximum indentation for the second line of a description list.
  195. When the indentation would be larger than this, it will become
  196. 5 characters instead."
  197. :group 'org-plain-lists
  198. :type 'integer)
  199. (defcustom org-list-radio-list-templates
  200. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  201. % END RECEIVE ORGLST %n
  202. \\begin{comment}
  203. #+ORGLST: SEND %n org-list-to-latex
  204. -
  205. \\end{comment}\n")
  206. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  207. @c END RECEIVE ORGLST %n
  208. @ignore
  209. #+ORGLST: SEND %n org-list-to-texinfo
  210. -
  211. @end ignore\n")
  212. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  213. <!-- END RECEIVE ORGLST %n -->
  214. <!--
  215. #+ORGLST: SEND %n org-list-to-html
  216. -
  217. -->\n"))
  218. "Templates for radio lists in different major modes.
  219. All occurrences of %n in a template will be replaced with the name of the
  220. list, obtained by prompting the user."
  221. :group 'org-plain-lists
  222. :type '(repeat
  223. (list (symbol :tag "Major mode")
  224. (string :tag "Format"))))
  225. ;;; Internal functions
  226. (defun org-list-end-re ()
  227. "Return the regex corresponding to the end of a list.
  228. It depends on `org-empty-line-terminates-plain-lists'."
  229. (if org-empty-line-terminates-plain-lists
  230. "^[ \t]*\n"
  231. org-list-end-regexp))
  232. (defun org-item-re (&optional general)
  233. "Return the correct regular expression for plain lists.
  234. If GENERAL is non-nil, return the general regexp independent of the value
  235. of `org-plain-list-ordered-item-terminator'."
  236. (cond
  237. ((or general (eq org-plain-list-ordered-item-terminator t))
  238. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  239. ((= org-plain-list-ordered-item-terminator ?.)
  240. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  241. ((= org-plain-list-ordered-item-terminator ?\))
  242. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  243. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  244. (defconst org-item-beginning-re (concat "^" (org-item-re))
  245. "Regexp matching the beginning of a plain list item.")
  246. (defun org-list-terminator-between (min max &optional firstp)
  247. "Find the position of a list ender between MIN and MAX, or nil.
  248. This function looks for `org-list-end-re' outside a block.
  249. If FIRSTP in non-nil, return the point at the beginning of the
  250. nearest valid terminator from min. Otherwise, return the point at
  251. the end of the nearest terminator from max."
  252. (save-excursion
  253. (let* ((start (if firstp min max))
  254. (end (if firstp max min))
  255. (search-fun (if firstp
  256. #'org-search-forward-unenclosed
  257. #'org-search-backward-unenclosed))
  258. (list-end-p (progn
  259. (goto-char start)
  260. (funcall search-fun (org-list-end-re) end t))))
  261. ;; Is there a valid list terminator somewhere ?
  262. (and list-end-p
  263. ;; we want to be on the first line of the list ender
  264. (match-beginning 0)))))
  265. (defun org-list-search-unenclosed-generic (search-fun regexp bound noerror count)
  266. "Search for REGEXP with SEARCH-FUN but don't stop inside blocks or at protected places."
  267. (let ((origin (point)))
  268. (cond
  269. ;; nothing found: return nil
  270. ((not (funcall search-fun regexp bound noerror count)) nil)
  271. ((or (save-match-data
  272. (org-in-regexps-block-p "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  273. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2))))
  274. (get-text-property (match-beginning 0) 'org-protected))
  275. ;; match is enclosed or protected: start again, searching one
  276. ;; occurrence away.
  277. (goto-char origin)
  278. (org-list-search-unenclosed-generic search-fun regexp bound noerror (1+ count)))
  279. ;; else return point.
  280. (t (point)))))
  281. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  282. "Like `re-search-backward' but don't stop inside blocks or at protected places."
  283. (org-list-search-unenclosed-generic #'re-search-backward regexp bound noerror 1))
  284. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  285. "Like `re-search-forward' but don't stop inside blocks or at protected places."
  286. (org-list-search-unenclosed-generic #'re-search-forward regexp bound noerror 1))
  287. (defun org-list-at-regexp-after-bullet-p (regexp)
  288. "Is point at a list item with REGEXP after bullet?"
  289. (and (org-at-item-p)
  290. (save-excursion
  291. (goto-char (match-end 0))
  292. ;; Ignore counter if any
  293. (when (looking-at "\\(?:\\[@start:[0-9]+\\][ \t]*\\)?")
  294. (goto-char (match-end 0)))
  295. (looking-at regexp))))
  296. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  297. "Return point at the beginning of next item at the same level.
  298. Search items using function SEARCH-FUN, from POS to LIMIT. It
  299. uses PRE-MOVE before search. Return nil if no item was found."
  300. (save-excursion
  301. (goto-char pos)
  302. (let ((ind (progn
  303. (org-beginning-of-item)
  304. (org-get-indentation)))
  305. (start (point-at-bol)))
  306. ;; We don't want to match the current line.
  307. (funcall pre-move)
  308. ;; Skip any sublist on the way
  309. (while (and (funcall search-fun org-item-beginning-re limit t)
  310. (> (org-get-indentation) ind)))
  311. (when (and (/= (point-at-bol) start) ; Have we moved ?
  312. (= (org-get-indentation) ind))
  313. (point-at-bol)))))
  314. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  315. "Insert a new list item at POS.
  316. If POS is before first character after bullet of the item, the
  317. new item will be created before the current one.
  318. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  319. after the bullet. Cursor will be after this text once the
  320. function ends."
  321. (goto-char pos)
  322. ;; Is point in a special block?
  323. (when (org-in-regexps-block-p
  324. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  325. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  326. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  327. ;; Rule in `org-list-automatic-rules' forbids insertion.
  328. (error "Cannot insert item inside a block.")
  329. ;; Else, move before it prior to add a new item.
  330. (end-of-line)
  331. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  332. (end-of-line 0)))
  333. (let* ((true-pos (point))
  334. (bullet (and (org-beginning-of-item)
  335. (org-list-bullet-string (org-get-bullet))))
  336. (ind (org-get-indentation))
  337. (before-p (progn
  338. ;; Description item: text starts after colons.
  339. (or (org-at-item-description-p)
  340. ;; At a checkbox: text starts after it.
  341. (org-at-item-checkbox-p)
  342. ;; Otherwise, text starts after bullet.
  343. (org-at-item-p))
  344. (<= true-pos (match-end 0))))
  345. ;; Guess number of blank lines used to separate items.
  346. (blank-lines-nb
  347. (let ((insert-blank-p
  348. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  349. (cond
  350. ;; Trivial cases where there should be none.
  351. ((or org-empty-line-terminates-plain-lists
  352. (not insert-blank-p)) 0)
  353. ;; When `org-blank-before-new-entry' says so, it is 1.
  354. ((eq insert-blank-p t) 1)
  355. ;; plain-list-item is 'auto. Count blank lines separating
  356. ;; neighbours items in list.
  357. (t (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  358. (cond
  359. ;; Is there a next item?
  360. (next-p (goto-char next-p)
  361. (org-back-over-empty-lines))
  362. ;; Is there a previous item?
  363. ((not (org-list-first-item-p)) (org-back-over-empty-lines))
  364. ;; Item alone: count lines separating it from parent, if any
  365. ((/= (org-list-top-point) (point-at-bol))
  366. (org-back-over-empty-lines))
  367. ;; No parent: no blank line.
  368. (t 0)))))))
  369. (insert-fun
  370. (lambda (text)
  371. ;; insert bullet above item in order to avoid bothering
  372. ;; with possible blank lines ending last item.
  373. (org-beginning-of-item)
  374. (indent-to-column ind)
  375. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  376. ;; Stay between after-bullet and before text.
  377. (save-excursion
  378. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  379. (unless before-p (org-move-item-down))
  380. (when checkbox (org-update-checkbox-count-maybe)))))
  381. (goto-char true-pos)
  382. (cond
  383. (before-p (funcall insert-fun nil)
  384. ;; Not taking advantage of renumbering while moving
  385. ;; down. Need to call it directly.
  386. (org-maybe-renumber-ordered-list) t)
  387. ;; Can't split item: insert bullet at the end of item.
  388. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  389. (funcall insert-fun nil) t)
  390. ;; else, insert a new bullet along with everything from point
  391. ;; down to last non-blank line of item.
  392. (t
  393. (delete-horizontal-space)
  394. ;; Get pos again in case previous command modified line.
  395. (let* ((pos (point))
  396. (end-before-blank (org-end-of-item-before-blank))
  397. (after-text
  398. (when (< pos end-before-blank)
  399. (prog1
  400. (delete-and-extract-region pos end-before-blank)
  401. ;; delete any blank line at and before point.
  402. (beginning-of-line)
  403. (while (looking-at "^[ \t]*$")
  404. (delete-region (point-at-bol) (1+ (point-at-eol)))
  405. (beginning-of-line 0))))))
  406. (funcall insert-fun after-text) t)))))
  407. ;;; Predicates
  408. (defun org-in-item-p ()
  409. "Is the cursor inside a plain list ?"
  410. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  411. (save-excursion
  412. (let* ((limit (save-excursion (outline-previous-heading)))
  413. ;; Move to eol so current line can be matched by `org-item-re'.
  414. (actual-pos (goto-char (point-at-eol)))
  415. (last-item-start (save-excursion
  416. (org-search-backward-unenclosed org-item-beginning-re limit t)))
  417. (list-ender (org-list-terminator-between last-item-start actual-pos)))
  418. ;; We are in a list when we are on an item line or when we can
  419. ;; find an item before point and there is no valid list ender
  420. ;; between it and the point.
  421. (and last-item-start
  422. (not list-ender))))))
  423. (defun org-list-first-item-p ()
  424. "Is this item the first item in a plain list?"
  425. (save-excursion
  426. (= (org-beginning-of-item) (org-beginning-of-item-list))))
  427. (defun org-at-item-p ()
  428. "Is point in a line starting a hand-formatted item?"
  429. (save-excursion
  430. (beginning-of-line) (looking-at org-item-beginning-re)))
  431. (defun org-at-item-bullet-p ()
  432. "Is point at the bullet of a plain list item?"
  433. (and (org-at-item-p)
  434. (not (member (char-after) '(?\ ?\t)))
  435. (< (point) (match-end 0))))
  436. (defun org-at-item-timer-p ()
  437. "Is point at a line starting a plain list item with a timer?"
  438. (org-list-at-regexp-after-bullet-p "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  439. (defun org-at-item-description-p ()
  440. "Is point at a description list item?"
  441. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  442. (defun org-at-item-checkbox-p ()
  443. "Is point at a line starting a plain-list item with a checklet?"
  444. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  445. (defun org-item-has-child-p ()
  446. "Does the current item have subitems?"
  447. (save-excursion
  448. (org-beginning-of-item)
  449. (let ((ind (org-get-indentation)))
  450. (org-end-of-item-or-at-child)
  451. (and (org-at-item-p)
  452. (> (org-get-indentation) ind)))))
  453. (defun org-checkbox-blocked-p ()
  454. "Is the current checkbox blocked from for being checked now?
  455. A checkbox is blocked if all of the following conditions are fulfilled:
  456. 1. The checkbox is not checked already.
  457. 2. The current entry has the ORDERED property set.
  458. 3. There is an unchecked checkbox in this entry before the current line."
  459. (catch 'exit
  460. (save-match-data
  461. (save-excursion
  462. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  463. (when (equal (match-string 1) "[X]")
  464. ;; the box is already checked!
  465. (throw 'exit nil))
  466. (let ((end (point-at-bol)))
  467. (condition-case nil (org-back-to-heading t)
  468. (error (throw 'exit nil)))
  469. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  470. (when (org-search-forward-unenclosed
  471. "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@start:[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
  472. (org-current-line)))))))
  473. ;;; Navigate
  474. (defun org-list-top-point ()
  475. "Return point at the top level item in a list, or nil if not in a list."
  476. (save-excursion
  477. (and (org-in-item-p)
  478. (let ((pos (point-at-eol))
  479. (bound (or (outline-previous-heading) (point-min))))
  480. ;; Is there some list above this one ? If so, go to its ending.
  481. ;; Otherwise, go back to the heading above or bob.
  482. (goto-char (or (org-list-terminator-between bound pos) bound))
  483. ;; From there, search down our list.
  484. (org-search-forward-unenclosed org-item-beginning-re pos t)
  485. (point-at-bol)))))
  486. (defun org-list-bottom-point ()
  487. "Return point just before list ending or nil if not in a list."
  488. (save-excursion
  489. (and (org-in-item-p)
  490. (let ((pos (org-beginning-of-item))
  491. (bound (or (and (let ((outline-regexp org-outline-regexp))
  492. ;; Use default regexp because folding
  493. ;; changes OUTLINE-REGEXP.
  494. (outline-next-heading))
  495. (skip-chars-backward " \t\r\n")
  496. (1+ (point-at-eol)))
  497. (point-max))))
  498. ;; The list ending is either first point matching
  499. ;; `org-list-end-re', point at first white-line before next
  500. ;; heading, or eob.
  501. (or (org-list-terminator-between pos bound t) bound)))))
  502. (defun org-beginning-of-item ()
  503. "Go to the beginning of the current hand-formatted item.
  504. If the cursor is not in an item, throw an error. Return point."
  505. (interactive)
  506. (if (not (org-in-item-p))
  507. (error "Not in an item")
  508. ;; Possibly match the current line.
  509. (end-of-line)
  510. (org-search-backward-unenclosed org-item-beginning-re nil t)
  511. (goto-char (point-at-bol))))
  512. (defun org-end-of-item ()
  513. "Go to the end of the current hand-formatted item.
  514. If the cursor is not in an item, throw an error."
  515. (interactive)
  516. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  517. (cond ((not (org-in-item-p)) (error "Not in an item"))
  518. (next-p (goto-char next-p))
  519. (t (org-end-of-item-list)))))
  520. (defun org-end-of-item-or-at-child ()
  521. "Move to the end of the item text, stops before the first child if any."
  522. (let ((limit (org-list-bottom-point)))
  523. (end-of-line)
  524. (goto-char
  525. (if (org-search-forward-unenclosed org-item-beginning-re limit t)
  526. (point-at-bol)
  527. limit))))
  528. (defun org-end-of-item-before-blank ()
  529. "Return point at end of item, before any blank line.
  530. Point returned is at eol."
  531. (save-excursion
  532. (org-end-of-item)
  533. (skip-chars-backward " \r\t\n")
  534. (point-at-eol)))
  535. (defun org-get-next-item (pos limit)
  536. "Get the point of the next item at the same level as POS.
  537. Stop searching at LIMIT. Return nil if no item is found. This
  538. function does not move point."
  539. (org-list-get-item-same-level
  540. #'org-search-forward-unenclosed pos limit #'end-of-line))
  541. (defun org-get-previous-item (pos limit)
  542. "Get the point of the previous item at the same level as POS.
  543. Stop searching at LIMIT. Return nil if no item is found. This
  544. function does not move point."
  545. (org-list-get-item-same-level
  546. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  547. (defun org-next-item ()
  548. "Move to the beginning of the next item.
  549. Item is at the same level in the current plain list. Error if not
  550. in a plain list, or if this is the last item in the list."
  551. (interactive)
  552. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  553. (if next-p
  554. (goto-char next-p)
  555. (error "On last item"))))
  556. (defun org-previous-item ()
  557. "Move to the beginning of the previous item.
  558. Item is at the same level in the current plain list. Error if not
  559. in a plain list, or if this is the first item in the list."
  560. (interactive)
  561. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  562. (if prev-p
  563. (goto-char prev-p)
  564. (error "On first item"))))
  565. (defun org-beginning-of-item-list ()
  566. "Go to the beginning item of the current list or sublist.
  567. Return point."
  568. (interactive)
  569. (org-beginning-of-item)
  570. (let ((limit (org-list-top-point))
  571. (move-up (lambda (pos bound)
  572. ;; prev-p: any item of same level before ?
  573. (let ((prev-p (org-get-previous-item pos bound)))
  574. ;; recurse until no more item of the same level
  575. ;; can be found.
  576. (if prev-p (funcall move-up prev-p bound) pos)))))
  577. ;; Go to the last item found and at bol in case we didn't move
  578. (goto-char (funcall move-up (point) limit))
  579. (goto-char (point-at-bol))))
  580. (defun org-end-of-item-list ()
  581. "Go to the end of the current list or sublist.
  582. Return point."
  583. (interactive)
  584. (org-beginning-of-item)
  585. (let ((limit (org-list-bottom-point))
  586. (ind (org-get-indentation))
  587. (get-last-item (lambda (pos bound)
  588. ;; next-p: any item of same level after ?
  589. (let ((next-p (org-get-next-item pos bound)))
  590. ;; recurse until no more item of the same level
  591. ;; can be found.
  592. (if next-p (funcall get-last-item next-p bound) pos)))))
  593. ;; Move to the last item of every list or sublist encountered, and
  594. ;; down to bol of a higher-level item, or limit.
  595. (while (and (/= (point) limit)
  596. (>= (org-get-indentation) ind))
  597. (goto-char (funcall get-last-item (point) limit))
  598. (end-of-line)
  599. (when (org-search-forward-unenclosed org-item-beginning-re limit 'move)
  600. (beginning-of-line)))
  601. (point)))
  602. ;;; Manipulate
  603. (defun org-list-exchange-items (beg-A beg-B)
  604. "Swap item starting at BEG-A with item starting at BEG-B.
  605. Blank lines at the end of items are left in place. Assumes
  606. BEG-A is lesser than BEG-B."
  607. (save-excursion
  608. (let* ((end-of-item-no-blank (lambda (pos)
  609. (goto-char pos)
  610. (goto-char (org-end-of-item-before-blank))))
  611. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  612. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  613. (body-A (buffer-substring beg-A end-A-no-blank))
  614. (body-B (buffer-substring beg-B end-B-no-blank))
  615. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  616. (goto-char beg-A)
  617. (delete-region beg-A end-B-no-blank)
  618. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  619. (defun org-move-item-down ()
  620. "Move the plain list item at point down, i.e. swap with following item.
  621. Subitems (items with larger indentation) are considered part of the item,
  622. so this really moves item trees."
  623. (interactive)
  624. (let ((pos (point))
  625. (col (current-column))
  626. (actual-item (org-beginning-of-item))
  627. (next-item (org-get-next-item (point) (save-excursion (org-end-of-item-list)))))
  628. (if (not next-item)
  629. (progn
  630. (goto-char pos)
  631. (error "Cannot move this item further down"))
  632. (org-list-exchange-items actual-item next-item)
  633. (org-maybe-renumber-ordered-list)
  634. (org-next-item)
  635. (move-to-column col))))
  636. (defun org-move-item-up ()
  637. "Move the plain list item at point up, i.e. swap with previous item.
  638. Subitems (items with larger indentation) are considered part of the item,
  639. so this really moves item trees."
  640. (interactive)
  641. (let ((pos (point))
  642. (col (current-column))
  643. (actual-item (org-beginning-of-item))
  644. (prev-item (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
  645. (if (not prev-item)
  646. (progn
  647. (goto-char pos)
  648. (error "Cannot move this item further up"))
  649. (org-list-exchange-items prev-item actual-item)
  650. (org-maybe-renumber-ordered-list)
  651. (move-to-column col))))
  652. (defun org-insert-item (&optional checkbox)
  653. "Insert a new item at the current level.
  654. If cursor is before first character after bullet of the item, the
  655. new item will be created before the current one. Return t when
  656. things worked, nil when we are not in an item, or item is
  657. invisible."
  658. (unless (or (not (org-in-item-p))
  659. (org-invisible-p))
  660. (if (save-excursion
  661. (org-beginning-of-item)
  662. (org-at-item-timer-p))
  663. ;; Timer list: delegate to `org-timer-item'.
  664. (progn (org-timer-item) t)
  665. ;; if we're in a description list, ask for the new term.
  666. (let ((desc-text (when (save-excursion
  667. (and (org-beginning-of-item)
  668. (org-at-item-description-p)))
  669. (concat (read-string "Term: ") " :: "))))
  670. ;; Don't insert a checkbox if checkbox rule is applied and it
  671. ;; is a description item.
  672. (org-list-insert-item-generic
  673. (point) (and checkbox
  674. (or (not desc-text)
  675. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  676. desc-text)))))
  677. ;;; Indentation
  678. (defun org-get-string-indentation (s)
  679. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  680. (let ((n -1) (i 0) (w tab-width) c)
  681. (catch 'exit
  682. (while (< (setq n (1+ n)) (length s))
  683. (setq c (aref s n))
  684. (cond ((= c ?\ ) (setq i (1+ i)))
  685. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  686. (t (throw 'exit t)))))
  687. i))
  688. (defun org-shift-item-indentation (delta)
  689. "Shift the indentation in current item by DELTA."
  690. (save-excursion
  691. (let ((beg (point-at-bol))
  692. (end (org-end-of-item)))
  693. (beginning-of-line 0)
  694. (while (> (point) beg)
  695. (when (looking-at "[ \t]*\\S-")
  696. ;; this is not an empty line
  697. (let ((i (org-get-indentation)))
  698. (when (and (> i 0) (> (+ i delta) 0))
  699. (indent-line-to (+ i delta)))))
  700. (beginning-of-line 0)))))
  701. (defvar org-last-indent-begin-marker (make-marker))
  702. (defvar org-last-indent-end-marker (make-marker))
  703. (defun org-outdent-item (arg)
  704. "Outdent a local list item, but not its children."
  705. (interactive "p")
  706. (org-indent-item-tree (- arg) 'no-subtree))
  707. (defun org-indent-item (arg)
  708. "Indent a local list item, but not its children."
  709. (interactive "p")
  710. (org-indent-item-tree arg 'no-subtree))
  711. (defun org-outdent-item-tree (arg &optional no-subtree)
  712. "Outdent a local list item including its children.
  713. If NO-SUBTREE is set, only outdent the item itself, not its children."
  714. (interactive "p")
  715. (org-indent-item-tree (- arg) no-subtree))
  716. (defun org-indent-item-tree (arg &optional no-subtree)
  717. "Indent a local list item including its children.
  718. If NO-SUBTREE is set, only indent the item itself, not its
  719. children. Return t if sucessful."
  720. (interactive "p")
  721. (unless (org-at-item-p)
  722. (error "Not on an item"))
  723. (let ((line (org-current-line))
  724. (col (current-column))
  725. (pos (point))
  726. (origin-ind (save-excursion
  727. (goto-char (org-list-top-point))
  728. (org-get-indentation)))
  729. beg end ind ind1 ind-pos bullet delta ind-down ind-up)
  730. ;; If moving a subtree, don't drag additional items on subsequent
  731. ;; moves.
  732. (if (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  733. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  734. (setq beg org-last-indent-begin-marker
  735. end org-last-indent-end-marker)
  736. (org-beginning-of-item)
  737. (setq beg (move-marker org-last-indent-begin-marker (point)))
  738. ;; Determine end point of indentation
  739. (if no-subtree (org-end-of-item-or-at-child) (org-end-of-item))
  740. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  741. ;; Get some information
  742. (goto-char beg)
  743. (setq ind-pos (org-item-indent-positions)
  744. bullet (cdr (car ind-pos))
  745. bul-up (cdr (nth 1 ind-pos))
  746. bul-down (cdr (nth 2 ind-pos))
  747. ind (caar ind-pos)
  748. ind-down (car (nth 2 ind-pos))
  749. ind-up (car (nth 1 ind-pos))
  750. delta (if (> arg 0)
  751. (if ind-down (- ind-down ind) 2)
  752. (if ind-up (- ind-up ind) -2)))
  753. ;; Check for error cases.
  754. (cond
  755. ((< (+ delta ind) 0)
  756. (goto-char pos)
  757. (error "Cannot outdent beyond margin"))
  758. ;; Apply indent rules if activated.
  759. ((cdr (assq 'indent org-list-automatic-rules))
  760. (cond
  761. ;; 1. If at top-point move the whole list. Moreover, if
  762. ;; *-list is going to column 0, change bullet to "-".
  763. ((and (= (point-at-bol) (org-list-top-point))
  764. (not no-subtree))
  765. (when (and (= (+ delta ind) 0) (equal bullet "*"))
  766. (org-fix-bullet-type (setq bullet "-")))
  767. (setq end (set-marker org-last-indent-end-marker (org-list-bottom-point))))
  768. ;; 2. Do not indent before top-item.
  769. ((< (+ delta ind) origin-ind)
  770. (goto-char pos)
  771. (error "Cannot outdent beyond top level item"))
  772. ;; 3. Do not indent the first item of a list.
  773. ((and (org-list-first-item-p) (> delta 0))
  774. (goto-char pos)
  775. (error "Cannot indent the beginning of a sublist"))
  776. ;; 4. Do not outdent item that has children without moving
  777. ;; subtree. If moving subtree, the rule applies to its last
  778. ;; sub-item.
  779. ((and (< delta 0)
  780. (save-excursion (goto-char (1- end)) (org-item-has-child-p)))
  781. (goto-char pos)
  782. (error "Cannot outdent an item having children")))))
  783. ;; Replace bullet of current item with the bullet it is going to
  784. ;; have if we're outdenting. This is needed to prevent indentation
  785. ;; problems of subtrees when outdenting changes bullet size.
  786. (when (< delta 0)
  787. (let ((new-bul (org-list-bullet-string (or bul-up bullet))))
  788. (org-list-replace-bullet new-bul)))
  789. ;; Proceed to reindentation.
  790. (while (< (point) end)
  791. (beginning-of-line)
  792. (skip-chars-forward " \t") (setq ind1 (current-column))
  793. (delete-region (point-at-bol) (point))
  794. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  795. (beginning-of-line 2))
  796. ;; Get back to original position, shifted by delta
  797. (goto-line line)
  798. (move-to-column (max (+ delta col) 0))
  799. ;; Fix and reorder all lists and sublists from list at point. If
  800. ;; it has a parent and we're indenting, renumber parent too.
  801. (save-excursion
  802. ;; Renumber parent list, if needed. No need for fixing bullets
  803. (org-beginning-of-item-list)
  804. (unless (or (< arg 0) (= (org-list-top-point) (point)))
  805. (beginning-of-line 0)
  806. (org-beginning-of-item)
  807. (org-maybe-renumber-ordered-list)))
  808. ;; Take care of list at point. When demoting, to determine bullet
  809. ;; of children, follow, in order: `org-list-demote-modify-bullet',
  810. ;; same bullet as others children, same bullet as before
  811. (org-fix-bullet-type
  812. (and (> arg 0)
  813. (or (cdr (assoc bullet org-list-demote-modify-bullet))
  814. bul-down)))
  815. (save-excursion
  816. (when (org-item-has-child-p)
  817. ;; Take care of child, or of every sublist if we're moving a
  818. ;; subtree.
  819. (org-end-of-item-or-at-child)
  820. (if no-subtree
  821. (org-fix-bullet-type)
  822. (let ((fix-list (lambda (i)
  823. (when (org-list-first-item-p)
  824. (org-fix-bullet-type
  825. (and (> arg 0)
  826. (cdr (assoc (org-get-bullet) org-list-demote-modify-bullet)))))
  827. (when (org-item-has-child-p)
  828. (org-end-of-item-or-at-child)
  829. (org-apply-on-list fix-list nil)))))
  830. (org-apply-on-list fix-list nil))))))
  831. t)
  832. (defun org-item-indent-positions ()
  833. "Return indentations and bullets relatives to a plain list item.
  834. This returns a list with three cons-cells containing indentation
  835. and bullet of: the item, the item after a promotion, and the item
  836. after being demoted. Assume cursor in item line."
  837. (let* ((pos (point))
  838. (init-bul (lambda (bullet)
  839. (if (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet)
  840. (concat "1" (match-string 1 bullet))
  841. bullet)))
  842. ;; Current item
  843. (item-cur (cons (org-get-indentation)
  844. (funcall init-bul (org-get-bullet))))
  845. ;; Parent
  846. (item-up (save-excursion
  847. (org-beginning-of-item-list)
  848. (unless (= (org-list-top-point) (point))
  849. (beginning-of-line 0)
  850. (org-beginning-of-item)
  851. (cons (org-get-indentation)
  852. (funcall init-bul (org-get-bullet))))))
  853. ;; Child of previous item, if any.
  854. (item-down (save-excursion
  855. (let ((prev-p (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
  856. (cond
  857. ((and prev-p (goto-char prev-p) (org-item-has-child-p))
  858. (progn
  859. (org-end-of-item-or-at-child)
  860. (cons (org-get-indentation)
  861. (funcall init-bul (org-get-bullet)))))
  862. ((and (goto-char pos) (org-item-has-child-p))
  863. (progn
  864. (org-end-of-item-or-at-child)
  865. (cons (org-get-indentation)
  866. (funcall init-bul (org-get-bullet)))))
  867. (t (org-at-item-p)
  868. (goto-char (match-end 0))
  869. (cons (current-column) (cdr item-cur))))))))
  870. (list item-cur item-up item-down)))
  871. (defvar org-tab-ind-state)
  872. (defun org-cycle-item-indentation ()
  873. (let ((org-adapt-indentation nil))
  874. (when (and (or (org-at-item-description-p) (org-at-item-checkbox-p) (org-at-item-p))
  875. (>= (match-end 0) (save-excursion
  876. (org-end-of-item-or-at-child)
  877. (skip-chars-backward " \r\t\n")
  878. (point))))
  879. (setq this-command 'org-cycle-item-indentation)
  880. ;; When in the middle of the cycle, try to outdent first. If it
  881. ;; fails, and point is still at initial position, indent. Else,
  882. ;; go back to original position.
  883. (if (eq last-command 'org-cycle-item-indentation)
  884. (cond
  885. ((ignore-errors (org-indent-item -1)))
  886. ((and (= (org-get-indentation) (car org-tab-ind-state))
  887. (ignore-errors (org-indent-item 1))))
  888. (t (back-to-indentation)
  889. (indent-to-column (car org-tab-ind-state))
  890. (end-of-line)
  891. (org-fix-bullet-type (nth 1 org-tab-ind-state))
  892. ;; Break cycle
  893. (setq this-command 'identity)))
  894. ;; If a cycle is starting, remember indentation and bullet,
  895. ;; then try to indent. If it fails, try to outdent.
  896. (setq org-tab-ind-state
  897. (list (org-get-indentation) (org-get-bullet)))
  898. (cond
  899. ((ignore-errors (org-indent-item 1)))
  900. ((ignore-errors (org-indent-item -1)))
  901. (t (error "Cannot move item"))))
  902. t)))
  903. ;;; Bullets
  904. (defun org-get-bullet ()
  905. (and (org-at-item-p)
  906. (org-trim (match-string 1))))
  907. (defun org-list-bullet-string (bullet)
  908. "Concatenate BULLET with an appropriate number of whitespaces.
  909. It determines the number of whitespaces to append by looking at
  910. `org-list-two-spaces-after-bullet-regexp'."
  911. (save-match-data
  912. (concat
  913. bullet " "
  914. ;; Do we need to concat another white space ?
  915. (when (and org-list-two-spaces-after-bullet-regexp
  916. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  917. " "))))
  918. (defun org-list-replace-bullet (new-bullet)
  919. "Replace current item's bullet with NEW-BULLET.
  920. Assume point is at item. Indent body if needed."
  921. (save-excursion
  922. (let ((old (progn
  923. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  924. (match-string 1))))
  925. (unless (equal new-bullet old)
  926. (replace-match new-bullet nil nil nil 1)
  927. ;; When bullet lengths are differents, move the whole
  928. ;; sublist accordingly
  929. (org-shift-item-indentation (- (length new-bullet) (length old)))))))
  930. (defun org-fix-bullet-type (&optional force-bullet)
  931. "Make sure all items in this list have the same bullet as the first item.
  932. Also, fix the indentation."
  933. (interactive)
  934. (unless (org-at-item-p) (error "This is not a list"))
  935. (org-preserve-lc
  936. (let* ((ini-bul (progn (org-beginning-of-item-list) (org-get-bullet)))
  937. (bullet (org-list-bullet-string (or force-bullet ini-bul)))
  938. (replace-bullet
  939. (lambda (result bullet)
  940. (org-list-replace-bullet bullet))))
  941. (org-apply-on-list replace-bullet nil bullet)
  942. (org-maybe-renumber-ordered-list))))
  943. (defun org-renumber-ordered-list (&optional arg)
  944. "Renumber an ordered plain list.
  945. Cursor needs to be in the first line of an item, the line that starts
  946. with something like \"1.\" or \"2)\". Start to count at ARG or 1."
  947. (interactive "p")
  948. (save-match-data
  949. (unless (and (org-at-item-p)
  950. (match-beginning 3))
  951. (error "This is not an ordered list"))
  952. (org-preserve-lc
  953. (let* ((item-fmt (progn
  954. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  955. (concat "%d" (or (match-string 1) "."))))
  956. ;; Here is the function applied at each item of the list.
  957. (renumber-item (lambda (counter fmt)
  958. (let* ((counter (or (save-excursion
  959. (and (org-at-item-p)
  960. (goto-char (match-end 0))
  961. (looking-at "\\[@start:\\([0-9]+\\)\\]")
  962. (string-to-number (match-string 1))))
  963. counter))
  964. (new (format fmt counter))
  965. (old (progn
  966. (looking-at org-item-beginning-re)
  967. (match-string 2)))
  968. (begin (match-beginning 2))
  969. (end (match-end 2)))
  970. (unless (equal new old)
  971. (delete-region begin end)
  972. (goto-char begin)
  973. (insert new)
  974. ;; In case item number went from 9. to 10.
  975. ;; or the other way.
  976. (org-shift-item-indentation (- (length new) (length old))))
  977. (1+ counter)))))
  978. (org-apply-on-list renumber-item (or arg 1) item-fmt)))))
  979. (defun org-maybe-renumber-ordered-list ()
  980. "Renumber the ordered list at point if setup allows it.
  981. This tests the if 'renumber rule is set in
  982. `org-list-automatic-rules' before doing the renumbering.
  983. Do not throw error on failure."
  984. (interactive)
  985. (when (cdr (assq 'renumber org-list-automatic-rules))
  986. (ignore-errors (org-renumber-ordered-list))))
  987. (defun org-cycle-list-bullet (&optional which)
  988. "Cycle through the different itemize/enumerate bullets.
  989. This cycle the entire list level through the sequence:
  990. `-' -> `+' -> `*' -> `1.' -> `1)'
  991. If WHICH is a valid string, use that as the new bullet. If WHICH
  992. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  993. 'previous, cycle backwards."
  994. (interactive "P")
  995. (org-preserve-lc
  996. (let* ((bullet (progn (org-beginning-of-item-list)
  997. (org-get-bullet)))
  998. (current (cond
  999. ((string-match "\\." bullet) "1.")
  1000. ((string-match ")" bullet) "1)")
  1001. (t bullet)))
  1002. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1003. (bullet-list (append '("-" "+" )
  1004. ;; *-bullets are not allowed at column 0
  1005. (unless (and bullet-rule-p
  1006. (looking-at "\\S-")) '("*"))
  1007. ;; Description items cannot be numbered
  1008. (unless (and bullet-rule-p
  1009. (or (eq org-plain-list-ordered-item-terminator ?.)
  1010. (org-at-item-description-p))) '("1)"))
  1011. (unless (and bullet-rule-p
  1012. (or (eq org-plain-list-ordered-item-terminator ?\))
  1013. (org-at-item-description-p))) '("1."))))
  1014. (len (length bullet-list))
  1015. (item-index (- len (length (member current bullet-list))))
  1016. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1017. (new (cond
  1018. ((member which bullet-list) which)
  1019. ((numberp which) (funcall get-value which))
  1020. ((eq 'previous which) (funcall get-value (1- item-index)))
  1021. (t (funcall get-value (1+ item-index))))))
  1022. (org-fix-bullet-type new))))
  1023. ;;; Checkboxes
  1024. (defun org-toggle-checkbox (&optional toggle-presence)
  1025. "Toggle the checkbox in the current line.
  1026. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1027. double prefix, set checkbox to [-].
  1028. When there is an active region, toggle status or presence of the
  1029. checkbox in the first line, and make every item in the region
  1030. have the same status or presence, respectively.
  1031. If the cursor is in a headline, apply this to all checkbox items
  1032. in the text below the heading, taking as reference the first item
  1033. in subtree."
  1034. (interactive "P")
  1035. ;; Bounds is a list of type (beg end single-p) where single-p is t
  1036. ;; when `org-toggle-checkbox' is applied to a single item. Only
  1037. ;; toggles on single items will return errors.
  1038. (let* ((bounds
  1039. (cond
  1040. ((org-region-active-p)
  1041. (list (region-beginning) (region-end) nil))
  1042. ((org-on-heading-p)
  1043. ;; In this case, reference line is the first item in subtree
  1044. (let ((limit (save-excursion (outline-next-heading) (point))))
  1045. (save-excursion
  1046. (org-search-forward-unenclosed org-item-beginning-re limit 'move)
  1047. (list (point) limit nil))))
  1048. ((org-at-item-p)
  1049. (list (point-at-bol) (point-at-eol) t))
  1050. (t (error "Not at an item or heading, and no active region"))))
  1051. ;; marker is needed because deleting checkboxes will change END
  1052. (end (copy-marker (nth 1 bounds)))
  1053. (single-p (nth 2 bounds))
  1054. (ref-presence (save-excursion (goto-char (car bounds)) (org-at-item-checkbox-p)))
  1055. (ref-status (equal (match-string 1) "[X]"))
  1056. (act-on-item
  1057. (lambda (ref-pres ref-stat)
  1058. (if (equal toggle-presence '(4))
  1059. (cond
  1060. ((and ref-pres (org-at-item-checkbox-p))
  1061. (replace-match ""))
  1062. ((and (not ref-pres)
  1063. (not (org-at-item-checkbox-p))
  1064. (org-at-item-p))
  1065. (goto-char (match-end 0))
  1066. ;; Ignore counter, if any
  1067. (when (looking-at "\\(?:\\[@start:[0-9]+\\][ \t]*\\)?")
  1068. (goto-char (match-end 0)))
  1069. (let ((desc-p (and (org-at-item-description-p)
  1070. (cdr (assq 'checkbox org-list-automatic-rules)))))
  1071. (cond
  1072. ((and single-p desc-p)
  1073. (error "Cannot add a checkbox in a description list"))
  1074. ((not desc-p) (insert "[ ] "))))))
  1075. (let ((blocked (org-checkbox-blocked-p)))
  1076. (cond
  1077. ((and blocked single-p)
  1078. (error "Checkbox blocked because of unchecked box in line %d" blocked))
  1079. (blocked nil)
  1080. ((org-at-item-checkbox-p)
  1081. (replace-match
  1082. (cond ((equal toggle-presence '(16)) "[-]")
  1083. (ref-stat "[ ]")
  1084. (t "[X]"))
  1085. t t nil 1))))))))
  1086. (save-excursion
  1087. (while (< (point) end)
  1088. (funcall act-on-item ref-presence ref-status)
  1089. (org-search-forward-unenclosed org-item-beginning-re end 'move)))
  1090. (org-update-checkbox-count-maybe)))
  1091. (defun org-reset-checkbox-state-subtree ()
  1092. "Reset all checkboxes in an entry subtree."
  1093. (interactive "*")
  1094. (save-restriction
  1095. (save-excursion
  1096. (org-narrow-to-subtree)
  1097. (org-show-subtree)
  1098. (goto-char (point-min))
  1099. (let ((end (point-max)))
  1100. (while (< (point) end)
  1101. (when (org-at-item-checkbox-p)
  1102. (replace-match "[ ]" t t nil 1))
  1103. (beginning-of-line 2))))
  1104. (org-update-checkbox-count-maybe)))
  1105. (defvar org-checkbox-statistics-hook nil
  1106. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1107. This hook runs even if 'checkbox rules in
  1108. `org-list-automatic-rules' do not apply, so it can be used to
  1109. implement alternative ways of collecting statistics
  1110. information.")
  1111. (defun org-update-checkbox-count-maybe ()
  1112. "Update checkbox statistics unless turned off by user."
  1113. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1114. (org-update-checkbox-count))
  1115. (run-hooks 'org-checkbox-statistics-hook))
  1116. (defun org-update-checkbox-count (&optional all)
  1117. "Update the checkbox statistics in the current section.
  1118. This will find all statistic cookies like [57%] and [6/12] and update them
  1119. with the current numbers. With optional prefix argument ALL, do this for
  1120. the whole buffer."
  1121. (interactive "P")
  1122. (save-excursion
  1123. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1124. (beg (condition-case nil
  1125. (progn (org-back-to-heading) (point))
  1126. (error (point-min))))
  1127. (end (move-marker (make-marker)
  1128. (progn (outline-next-heading) (point))))
  1129. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1130. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  1131. (re-find (concat re "\\|" re-box))
  1132. beg-cookie end-cookie is-percent c-on c-off lim new
  1133. eline curr-ind next-ind continue-from startsearch
  1134. (recursive
  1135. (or (not org-hierarchical-checkbox-statistics)
  1136. (string-match "\\<recursive\\>"
  1137. (or (ignore-errors
  1138. (org-entry-get nil "COOKIE_DATA"))
  1139. ""))))
  1140. (cstat 0))
  1141. (when all
  1142. (goto-char (point-min))
  1143. (outline-next-heading)
  1144. (setq beg (point) end (point-max)))
  1145. (goto-char end)
  1146. ;; find each statistics cookie
  1147. (while (and (org-search-backward-unenclosed re-find beg t)
  1148. (not (save-match-data
  1149. (and (org-on-heading-p)
  1150. (string-match "\\<todo\\>"
  1151. (downcase
  1152. (or (org-entry-get
  1153. nil "COOKIE_DATA")
  1154. "")))))))
  1155. (setq beg-cookie (match-beginning 1)
  1156. end-cookie (match-end 1)
  1157. cstat (+ cstat (if end-cookie 1 0))
  1158. startsearch (point-at-eol)
  1159. continue-from (match-beginning 0)
  1160. is-percent (match-beginning 2)
  1161. lim (cond
  1162. ((org-on-heading-p) (outline-next-heading) (point))
  1163. ((org-at-item-p) (org-end-of-item) (point))
  1164. (t nil))
  1165. c-on 0
  1166. c-off 0)
  1167. (when lim
  1168. ;; find first checkbox for this cookie and gather
  1169. ;; statistics from all that are at this indentation level
  1170. (goto-char startsearch)
  1171. (if (org-search-forward-unenclosed re-box lim t)
  1172. (progn
  1173. (org-beginning-of-item)
  1174. (setq curr-ind (org-get-indentation))
  1175. (setq next-ind curr-ind)
  1176. (while (and (bolp) (org-at-item-p)
  1177. (if recursive
  1178. (<= curr-ind next-ind)
  1179. (= curr-ind next-ind)))
  1180. (setq eline (point-at-eol))
  1181. (if (org-search-forward-unenclosed re-box eline t)
  1182. (if (member (match-string 2) '("[ ]" "[-]"))
  1183. (setq c-off (1+ c-off))
  1184. (setq c-on (1+ c-on))))
  1185. (if (not recursive)
  1186. ;; org-get-next-item goes through list-enders
  1187. ;; with proper limit.
  1188. (goto-char (or (org-get-next-item (point) lim) lim))
  1189. (end-of-line)
  1190. (when (org-search-forward-unenclosed org-item-beginning-re lim t)
  1191. (beginning-of-line)))
  1192. (setq next-ind (org-get-indentation)))))
  1193. (goto-char continue-from)
  1194. ;; update cookie
  1195. (when end-cookie
  1196. (setq new (if is-percent
  1197. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  1198. (format "[%d/%d]" c-on (+ c-on c-off))))
  1199. (goto-char beg-cookie)
  1200. (insert new)
  1201. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1202. ;; update items checkbox if it has one
  1203. (when (org-at-item-p)
  1204. (org-beginning-of-item)
  1205. (when (and (> (+ c-on c-off) 0)
  1206. (org-search-forward-unenclosed re-box (point-at-eol) t))
  1207. (setq beg-cookie (match-beginning 2)
  1208. end-cookie (match-end 2))
  1209. (delete-region beg-cookie end-cookie)
  1210. (goto-char beg-cookie)
  1211. (cond ((= c-off 0) (insert "[X]"))
  1212. ((= c-on 0) (insert "[ ]"))
  1213. (t (insert "[-]")))
  1214. )))
  1215. (goto-char continue-from))
  1216. (when (interactive-p)
  1217. (message "Checkbox statistics updated %s (%d places)"
  1218. (if all "in entire file" "in current outline entry") cstat)))))
  1219. (defun org-get-checkbox-statistics-face ()
  1220. "Select the face for checkbox statistics.
  1221. The face will be `org-done' when all relevant boxes are checked.
  1222. Otherwise it will be `org-todo'."
  1223. (if (match-end 1)
  1224. (if (equal (match-string 1) "100%")
  1225. 'org-checkbox-statistics-done
  1226. 'org-checkbox-statistics-todo)
  1227. (if (and (> (match-end 2) (match-beginning 2))
  1228. (equal (match-string 2) (match-string 3)))
  1229. 'org-checkbox-statistics-done
  1230. 'org-checkbox-statistics-todo)))
  1231. ;;; Misc Tools
  1232. (defun org-apply-on-list (function init-value &rest args)
  1233. "Call FUNCTION for each item of a the list under point.
  1234. FUNCTION must be called with at least one argument: a return
  1235. value that will contain the value returned by the function at the
  1236. previous item, plus ARGS extra arguments. INIT-VALUE will be the
  1237. value passed to the function at the first item of the list.
  1238. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1239. will return the number of items in the current list.
  1240. Sublists of the list are skipped. Cursor is always at the
  1241. beginning of the item."
  1242. (save-excursion
  1243. (let ((end (copy-marker (save-excursion (org-end-of-item-list))))
  1244. (next-p (make-marker))
  1245. (move-down-action
  1246. (lambda (pos value &rest args)
  1247. (goto-char pos)
  1248. (set-marker next-p (org-get-next-item pos end))
  1249. (let ((return-value (apply function value args)))
  1250. (if (marker-position next-p)
  1251. (apply move-down-action next-p return-value args)
  1252. return-value)))))
  1253. (apply move-down-action (org-beginning-of-item-list) init-value args))))
  1254. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1255. "Sort plain list items.
  1256. The cursor may be at any item of the list that should be sorted.
  1257. Sublists are not sorted. Checkboxes, if any, are ignored.
  1258. Sorting can be alphabetically, numerically, by date/time as given by
  1259. a time stamp, by a property or by priority.
  1260. The command prompts for the sorting type unless it has been given
  1261. to the function through the SORTING-TYPE argument, which needs to
  1262. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1263. meaning of each character:
  1264. n Numerically, by converting the beginning of the item to a number.
  1265. a Alphabetically. Only the first line of item is checked.
  1266. t By date/time, either the first active time stamp in the entry, if
  1267. any, or by the first inactive one. In a timer list, sort the timers.
  1268. Capital letters will reverse the sort order.
  1269. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1270. function to be called with point at the beginning of the record.
  1271. It must return either a string or a number that should serve as
  1272. the sorting key for that record.
  1273. Comparing entries ignores case by default. However, with an
  1274. optional argument WITH-CASE, the sorting considers case as well."
  1275. (interactive "P")
  1276. (let* ((case-func (if with-case 'identity 'downcase))
  1277. (start (org-beginning-of-item-list))
  1278. (end (save-excursion (org-end-of-item-list)))
  1279. (sorting-type
  1280. (progn
  1281. (message
  1282. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1283. (read-char-exclusive)))
  1284. (getkey-func (and (= (downcase sorting-type) ?f)
  1285. (org-icompleting-read "Sort using function: "
  1286. obarray 'fboundp t nil nil)
  1287. (intern getkey-func))))
  1288. (message "Sorting items...")
  1289. (save-restriction
  1290. (narrow-to-region start end)
  1291. (let* ((dcst (downcase sorting-type))
  1292. (case-fold-search nil)
  1293. (now (current-time))
  1294. (sort-func (cond
  1295. ((= dcst ?a) 'string<)
  1296. ((= dcst ?f) compare-func)
  1297. ((= dcst ?t) '<)
  1298. (t nil)))
  1299. (begin-record (lambda ()
  1300. (skip-chars-forward " \r\t\n")
  1301. (beginning-of-line)))
  1302. (end-record (lambda ()
  1303. (goto-char (org-end-of-item-before-blank))))
  1304. (value-to-sort
  1305. (lambda ()
  1306. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1307. (cond
  1308. ((= dcst ?n)
  1309. (string-to-number (buffer-substring (match-end 0)
  1310. (point-at-eol))))
  1311. ((= dcst ?a)
  1312. (buffer-substring (match-end 0) (point-at-eol)))
  1313. ((= dcst ?t)
  1314. (cond
  1315. ;; If it is a timer list, convert timer to seconds
  1316. ((org-at-item-timer-p)
  1317. (org-timer-hms-to-secs (match-string 1)))
  1318. ((or (org-search-forward-unenclosed org-ts-regexp
  1319. (point-at-eol) t)
  1320. (org-search-forward-unenclosed org-ts-regexp-both
  1321. (point-at-eol) t))
  1322. (org-time-string-to-seconds (match-string 0)))
  1323. (t (org-float-time now))))
  1324. ((= dcst ?f)
  1325. (if getkey-func
  1326. (let ((value (funcall getkey-func)))
  1327. (if (stringp value)
  1328. (funcall case-func value)
  1329. value))
  1330. (error "Invalid key function `%s'" getkey-func)))
  1331. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1332. (sort-subr (/= dcst sorting-type) begin-record end-record value-to-sort nil sort-func)
  1333. (org-maybe-renumber-ordered-list)
  1334. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1335. (message "Sorting items...done")))))
  1336. ;;; Send and receive lists
  1337. (defun org-list-parse-list (&optional delete)
  1338. "Parse the list at point and maybe DELETE it.
  1339. Return a list containing first level items as strings and
  1340. sublevels as a list of strings."
  1341. (let* ((start (goto-char (org-list-top-point)))
  1342. (end (org-list-bottom-point))
  1343. output itemsep ltype)
  1344. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1345. (save-excursion
  1346. (beginning-of-line)
  1347. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1348. ((org-at-item-description-p) 'descriptive)
  1349. (t 'unordered))))
  1350. (let* ((indent1 (org-get-indentation))
  1351. (nextitem (or (org-get-next-item (point) end) end))
  1352. (item (org-trim (buffer-substring (point) (org-end-of-item-or-at-child))))
  1353. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1354. (item (if (string-match "^\\(?:\\[@start:[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]" item)
  1355. (replace-match (if (equal (match-string 1 item) " ")
  1356. "CBOFF"
  1357. "CBON")
  1358. t nil item 1)
  1359. item)))
  1360. (push item output)
  1361. (when (> nextindent indent1)
  1362. (save-restriction
  1363. (narrow-to-region (point) nextitem)
  1364. (push (org-list-parse-list) output)))))
  1365. (when delete
  1366. (delete-region start end)
  1367. (save-match-data
  1368. (when (looking-at (org-list-end-re))
  1369. (replace-match "\n"))))
  1370. (setq output (nreverse output))
  1371. (push ltype output)))
  1372. (defun org-list-make-subtree ()
  1373. "Convert the plain list at point into a subtree."
  1374. (interactive)
  1375. (if (not (org-in-item-p))
  1376. (error "Not in a list.")
  1377. (goto-char (org-list-top-point))
  1378. (let ((list (org-list-parse-list t)) nstars)
  1379. (save-excursion
  1380. (if (ignore-errors
  1381. (org-back-to-heading))
  1382. (progn (org-search-forward-unenclosed org-complex-heading-regexp nil t)
  1383. (setq nstars (length (match-string 1))))
  1384. (setq nstars 0)))
  1385. (org-list-make-subtrees list (1+ nstars)))))
  1386. (defun org-list-make-subtrees (list level)
  1387. "Convert LIST into subtrees starting at LEVEL."
  1388. (if (symbolp (car list))
  1389. (org-list-make-subtrees (cdr list) level)
  1390. (mapcar (lambda (item)
  1391. (if (stringp item)
  1392. (insert (make-string
  1393. (if org-odd-levels-only
  1394. (1- (* 2 level)) level) ?*) " " item "\n")
  1395. (org-list-make-subtrees item (1+ level))))
  1396. list)))
  1397. (defun org-list-insert-radio-list ()
  1398. "Insert a radio list template appropriate for this major mode."
  1399. (interactive)
  1400. (let* ((e (assq major-mode org-list-radio-list-templates))
  1401. (txt (nth 1 e))
  1402. name pos)
  1403. (unless e (error "No radio list setup defined for %s" major-mode))
  1404. (setq name (read-string "List name: "))
  1405. (while (string-match "%n" txt)
  1406. (setq txt (replace-match name t t txt)))
  1407. (or (bolp) (insert "\n"))
  1408. (setq pos (point))
  1409. (insert txt)
  1410. (goto-char pos)))
  1411. (defun org-list-send-list (&optional maybe)
  1412. "Send a transformed version of this list to the receiver position.
  1413. With argument MAYBE, fail quietly if no transformation is defined for
  1414. this list."
  1415. (interactive)
  1416. (catch 'exit
  1417. (unless (org-at-item-p) (error "Not at a list"))
  1418. (save-excursion
  1419. (re-search-backward "#\\+ORGLST" nil t)
  1420. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1421. (if maybe
  1422. (throw 'exit nil)
  1423. (error "Don't know how to transform this list"))))
  1424. (let* ((name (match-string 1))
  1425. (transform (intern (match-string 2)))
  1426. (bottom-point
  1427. (save-excursion
  1428. (re-search-forward "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1429. (match-beginning 0)))
  1430. (top-point
  1431. (progn
  1432. (re-search-backward "#\\+ORGLST" nil t)
  1433. (re-search-forward org-item-beginning-re bottom-point t)
  1434. (match-beginning 0)))
  1435. (list (save-restriction
  1436. (narrow-to-region top-point bottom-point)
  1437. (org-list-parse-list)))
  1438. beg txt)
  1439. (unless (fboundp transform)
  1440. (error "No such transformation function %s" transform))
  1441. (let ((txt (funcall transform list)))
  1442. ;; Find the insertion place
  1443. (save-excursion
  1444. (goto-char (point-min))
  1445. (unless (re-search-forward
  1446. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1447. (error "Don't know where to insert translated list"))
  1448. (goto-char (match-beginning 0))
  1449. (beginning-of-line 2)
  1450. (setq beg (point))
  1451. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1452. (error "Cannot find end of insertion region"))
  1453. (delete-region beg (point-at-bol))
  1454. (goto-char beg)
  1455. (insert txt "\n")))
  1456. (message "List converted and installed at receiver location"))))
  1457. (defun org-list-to-generic (list params)
  1458. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1459. Valid parameters PARAMS are
  1460. :ustart String to start an unordered list
  1461. :uend String to end an unordered list
  1462. :ostart String to start an ordered list
  1463. :oend String to end an ordered list
  1464. :dstart String to start a descriptive list
  1465. :dend String to end a descriptive list
  1466. :dtstart String to start a descriptive term
  1467. :dtend String to end a descriptive term
  1468. :ddstart String to start a description
  1469. :ddend String to end a description
  1470. :splice When set to t, return only list body lines, don't wrap
  1471. them into :[u/o]start and :[u/o]end. Default is nil.
  1472. :istart String to start a list item
  1473. :iend String to end a list item
  1474. :isep String to separate items
  1475. :lsep String to separate sublists
  1476. :cboff String to insert for an unchecked checkbox
  1477. :cbon String to insert for a checked checkbox"
  1478. (interactive)
  1479. (let* ((p params) sublist
  1480. (splicep (plist-get p :splice))
  1481. (ostart (plist-get p :ostart))
  1482. (oend (plist-get p :oend))
  1483. (ustart (plist-get p :ustart))
  1484. (uend (plist-get p :uend))
  1485. (dstart (plist-get p :dstart))
  1486. (dend (plist-get p :dend))
  1487. (dtstart (plist-get p :dtstart))
  1488. (dtend (plist-get p :dtend))
  1489. (ddstart (plist-get p :ddstart))
  1490. (ddend (plist-get p :ddend))
  1491. (istart (plist-get p :istart))
  1492. (iend (plist-get p :iend))
  1493. (isep (plist-get p :isep))
  1494. (lsep (plist-get p :lsep))
  1495. (cbon (plist-get p :cbon))
  1496. (cboff (plist-get p :cboff)))
  1497. (let ((wrapper
  1498. (cond ((eq (car list) 'ordered)
  1499. (concat ostart "\n%s" oend "\n"))
  1500. ((eq (car list) 'unordered)
  1501. (concat ustart "\n%s" uend "\n"))
  1502. ((eq (car list) 'descriptive)
  1503. (concat dstart "\n%s" dend "\n"))))
  1504. rtn term defstart defend)
  1505. (while (setq sublist (pop list))
  1506. (cond ((symbolp sublist) nil)
  1507. ((stringp sublist)
  1508. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  1509. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1510. (match-string 1 sublist))))
  1511. (setq sublist (concat ddstart
  1512. (org-trim (substring sublist (match-end 0)))
  1513. ddend)))
  1514. (if (string-match "\\[CBON\\]" sublist)
  1515. (setq sublist (replace-match cbon t t sublist)))
  1516. (if (string-match "\\[CBOFF\\]" sublist)
  1517. (setq sublist (replace-match cboff t t sublist)))
  1518. (if (string-match "\\[-\\]" sublist)
  1519. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1520. (setq rtn (concat rtn istart term sublist iend isep)))
  1521. (t (setq rtn (concat rtn ;; previous list
  1522. lsep ;; list separator
  1523. (org-list-to-generic sublist p)
  1524. lsep ;; list separator
  1525. )))))
  1526. (format wrapper rtn))))
  1527. (defun org-list-to-latex (list &optional params)
  1528. "Convert LIST into a LaTeX list.
  1529. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1530. with overruling parameters for `org-list-to-generic'."
  1531. (org-list-to-generic
  1532. list
  1533. (org-combine-plists
  1534. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1535. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1536. :dstart "\\begin{description}" :dend "\\end{description}"
  1537. :dtstart "[" :dtend "]"
  1538. :ddstart "" :ddend ""
  1539. :istart "\\item " :iend ""
  1540. :isep "\n" :lsep "\n"
  1541. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1542. params)))
  1543. (defun org-list-to-html (list &optional params)
  1544. "Convert LIST into a HTML list.
  1545. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1546. with overruling parameters for `org-list-to-generic'."
  1547. (org-list-to-generic
  1548. list
  1549. (org-combine-plists
  1550. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1551. :ustart "<ul>" :uend "</ul>"
  1552. :dstart "<dl>" :dend "</dl>"
  1553. :dtstart "<dt>" :dtend "</dt>"
  1554. :ddstart "<dd>" :ddend "</dd>"
  1555. :istart "<li>" :iend "</li>"
  1556. :isep "\n" :lsep "\n"
  1557. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1558. params)))
  1559. (defun org-list-to-texinfo (list &optional params)
  1560. "Convert LIST into a Texinfo list.
  1561. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1562. with overruling parameters for `org-list-to-generic'."
  1563. (org-list-to-generic
  1564. list
  1565. (org-combine-plists
  1566. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1567. :ustart "@enumerate" :uend "@end enumerate"
  1568. :dstart "@table" :dend "@end table"
  1569. :dtstart "@item " :dtend "\n"
  1570. :ddstart "" :ddend ""
  1571. :istart "@item\n" :iend ""
  1572. :isep "\n" :lsep "\n"
  1573. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1574. params)))
  1575. (provide 'org-list)
  1576. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1577. ;;; org-list.el ends here