org-index.el 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. ;;; org-index.el --- A personal index for org and beyond
  2. ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. ;; Author: Marc Ihm <org-index@2484.de>
  4. ;; Version: 3.1.1
  5. ;; Keywords: outlines index
  6. ;; This file is not part of GNU Emacs.
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Purpose:
  22. ;;
  23. ;; Help to navigate org. Mark and find your favorite org-headings easily:
  24. ;; Create and update an index table of references and links. This table is
  25. ;; sorted by usage count, so that the builtin incremental occur presents
  26. ;; often used entries first.
  27. ;;
  28. ;; References are essentially small numbers (e.g. "R237" or "-455-"), as
  29. ;; created by this package; they are well suited to be used outside of
  30. ;; org (e.g. within folder names). Links are normal org-mode links.
  31. ;;
  32. ;;
  33. ;; Setup:
  34. ;;
  35. ;; - Add these lines to your .emacs (modify appropriately):
  36. ;;
  37. ;; (require 'org-index)
  38. ;; (org-index-default-keybindings) ; optional
  39. ;;
  40. ;; - Restart your Emacs to make these lines effective.
  41. ;;
  42. ;; - Invoke `org-index', which will assist in creating your index
  43. ;; table. The variable org-index-id will be persisted within your
  44. ;; customization file (typically .emacs).
  45. ;;
  46. ;;
  47. ;; Further reading:
  48. ;;
  49. ;; See the documentation of `org-index', which can also be read
  50. ;; by invoking `org-index' and choosing the help-command.
  51. ;;
  52. ;;
  53. ;; Updates:
  54. ;;
  55. ;; The latest tested version of this file can always be found at:
  56. ;;
  57. ;; http://orgmode.org/w/org-mode.git?p=org-mode.git;a=blob_plain;f=contrib/lisp/org-index.el;hb=HEAD
  58. ;;; Change Log:
  59. ;; [2015-01-20 Mo] Version 3.1.1:
  60. ;; - Bugfix for delete within occur
  61. ;;
  62. ;; [2015-01-19 Mo] Version 3.1.0:
  63. ;; - Rewrote command "occur" with overlays in an indirect buffer
  64. ;; - Removed function `org-index-copy-references-from-heading-to-property'
  65. ;; - introduced variable org-index-version
  66. ;;
  67. ;; [2014-12-14 Su] Version 3.0.2:
  68. ;; - Bugfixes in occur mode
  69. ;; - New function `org-index-copy-references-from-heading-to-property'
  70. ;;
  71. ;; [2014-12-10 We] Version 3.0.1:
  72. ;; - Bugfixes related with assistant
  73. ;; - Fix for editing of category
  74. ;;
  75. ;; [2014-12-07 Sa] Version 3.0.0:
  76. ;; - New commands "add" and "delete" to easily add and remove
  77. ;; the current node to or from your index.
  78. ;; - New command "example" to create an example index.
  79. ;; - Moved flags to a list within the same node as the index table;
  80. ;; this breaks compatibility to prior versions of the package.
  81. ;; - Several new flags that are explained within index node.
  82. ;; - Removed commands "reuse", "missing", "put", "goto",
  83. ;; "update", "link", "fill", "unhighlight"
  84. ;; - New function `org-index-default-keybindings'
  85. ;;
  86. ;; [2014-04-26 Sa] Version 2.4.3:
  87. ;; - Some Bug fixes and enhancements for occur-command
  88. ;; - Fixes for assistant to create index table
  89. ;;
  90. ;; [2014-02-01 Sa] Version 2.4.2:
  91. ;; - Follow mode in occur-buffer
  92. ;; - Reorder for x-columns
  93. ;;
  94. ;; [2014-01-02 Th] Version 2.4.0:
  95. ;; - New command "put" to store a nodes reference in a property
  96. ;; - New functions org-index-new-line and org-index-get-line
  97. ;; offer access to org-index from other lisp programs
  98. ;; - New flag p, new columns x1,x2 and x3
  99. ;; - Major Code refactoring
  100. ;; - Regression tests with ert
  101. ;; - Lots of bug fixes
  102. ;;
  103. ;; [2013-10-04 Fr] Version 2.3.2:
  104. ;; - Bug fix: index-table created by assistant is found after
  105. ;; restart of emacs instead of invoking assistant again
  106. ;;
  107. ;; [2013-07-20 Sa] Version 2.3.0:
  108. ;; - Renamed from "org-favtable" to "org-index"
  109. ;; - Added an assistant to set up the index table
  110. ;; - occur is now incremental, searching as you type
  111. ;; - simplified the documentation and help-system
  112. ;; - Saving keystrokes, as "+g237" is now valid input
  113. ;; - Many bug fixes
  114. ;;
  115. ;; [2013-02-28 Th] Version 2.2.0:
  116. ;; - Allowed shortcuts like "h237" for command "head" with argument "237"
  117. ;; - Integrated with org-mark-ring-goto
  118. ;;
  119. ;; [2013-01-25 Fr] Version 2.1.0:
  120. ;; - Added full support for links
  121. ;; - New commands "statistics"
  122. ;; - Renamed the package from "org-reftable" to "org-favtable"
  123. ;; - Additional columns are required (e.g. "link"). Error messages will
  124. ;; guide you
  125. ;;
  126. ;; [2012-12-07 Fr] Version 2.0.0:
  127. ;; - The format of the table of favorites has changed ! You need to bring
  128. ;; your existing table into the new format by hand (which however is
  129. ;; easy and explained below)
  130. ;; - Reference table can be sorted after usage count or date of last access
  131. ;; - Ask user explicitly, which command to invoke
  132. ;; - Renamed the package from "org-refer-by-number" to "org-reftable"
  133. ;;
  134. ;; [2012-09-22 Sa] Version 1.5.0:
  135. ;; - New command "sort" to sort a buffer or region by reference number
  136. ;; - New commands "highlight" and "unhighlight" to mark references
  137. ;;
  138. ;; [2012-07-13 Fr] Version 1.4.0:
  139. ;; - New command "head" to find a headline with a reference number
  140. ;;
  141. ;; [2012-04-28 Sa] Version 1.3.0:
  142. ;; - New commands occur and multi-occur
  143. ;; - All commands can now be invoked explicitly
  144. ;; - New documentation
  145. ;; - Many bugfixes
  146. ;;
  147. ;; [2011-12-10 Sa] Version 1.2.0:
  148. ;; - Fixed a bug, which lead to a loss of newly created reference numbers
  149. ;; - Introduced single and double prefix arguments
  150. ;; - Started this Change Log
  151. ;;; Code:
  152. (require 'org-table)
  153. (require 'cl)
  154. (defcustom org-index-id nil
  155. "Id of the Org-mode node, which contains the index table."
  156. :group 'org
  157. :group 'org-index)
  158. ;; Version of this package
  159. (defvar org-index-version "3.1.1" "Version of `org-index', format is major.minor.bugfix, where \"major\" is a change in index-table and \"minor\" are new features.")
  160. ;; Variables to hold the configuration of the index table
  161. (defvar org-index--maxref nil "Maximum number from reference table (e.g. \"153\").")
  162. (defvar org-index--head nil "Any header before number (e.g. \"R\").")
  163. (defvar org-index--tail nil "Tail after number (e.g. \"}\" or \")\".")
  164. (defvar org-index--numcols nil "Number of columns in index table.")
  165. (defvar org-index--ref-regex nil "Regular expression to match a reference.")
  166. (defvar org-index--ref-format nil "Format, that can print a reference.")
  167. (defvar org-index--columns nil "Columns of index-table.")
  168. (defvar org-index--special-columns nil "Columns with flags, that may appear only once.")
  169. (defvar org-index--flagged-columns nil "Columns with flags, that may appear multiple times.")
  170. (defvar org-index--buffer nil "Buffer of index table.")
  171. (defvar org-index--point nil "Position at start of headline of index table.")
  172. (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
  173. (defvar org-index--headings nil "Headlines of index-table as a string.")
  174. (defvar org-index-map nil "Keymap for shortcuts for some commands of `org-index'. Can be activated and filled by org-index-default-keybings.")
  175. ;; Variables to hold context and state
  176. (defvar org-index--last-action nil "Last action performed by `org-index'.")
  177. (defvar org-index--text-to-yank nil "Text, that can be yanked after call (mostly a reference).")
  178. (defvar org-index--last-ref nil "Last reference created or visited.")
  179. (defvar org-index--point-before nil "Point in buffer with index table.")
  180. (defvar org-index--point-saved nil "Saved point if we want to return.")
  181. (defvar org-index--category-before nil "Category of node before.")
  182. (defvar org-index--silent nil "Set to t, if user should not be queried.")
  183. (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
  184. (defvar org-index--below-cursor nil "Word below cursor.")
  185. (defvar org-index--within-node nil "True, if we are within node of the index table.")
  186. (defvar org-index--active-window-index nil "Active window with index table (if any).")
  187. (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
  188. (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
  189. (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
  190. (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
  191. (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
  192. ;; static information for this program package
  193. (defconst org-index--commands '(occur add delete head enter leave ref help example reorder sort multi-occur highlight statistics) "List of commands available.")
  194. (defconst org-index--required-flags '(sort) "Flags that are required.")
  195. (defconst org-index--single-flags '(sort point-on-add yank-after-add) "Flags, that may only appear once; these can appear as special-columns.")
  196. (defconst org-index--multiple-flags '(edit-on-add get-category-on-add get-heading-on-add) "Flags, that might appear multiple times.")
  197. (defconst org-index--all-flags (append org-index--single-flags org-index--multiple-flags) "All flags.")
  198. (defconst org-index--valid-headings '(ref link created last-accessed count keywords) "All valid headings.")
  199. (defconst org-index--required-headings org-index--valid-headings "All required headings.")
  200. (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
  201. (defconst org-index--sample-flags
  202. "
  203. - columns-and-flags :: associate columns of index table with flags
  204. - ref
  205. - yank-after-add
  206. - .category
  207. - get-category-on-add
  208. - edit-on-add
  209. - keywords
  210. - get-heading-on-add
  211. - edit-on-add
  212. - point-on-add
  213. - count
  214. - sort
  215. - last-accessed
  216. - created
  217. - link
  218. - all-columns-explained :: All columns of the index table and their meaning
  219. - ref :: The reference number; will be generated automatically
  220. - link :: link to the node, that this line represents
  221. - created :: When has this entry been created ?
  222. - last-accessed :: When has this entry been accessed last ?
  223. - count :: How many times has this entry been picked ?
  224. - keywords :: List of keywords, which may match your input during occur
  225. - all-flags-explained :: All flags, that can be associated with columns
  226. - sort :: Sort whole table after this column
  227. - yank-after-add :: Let this column be yanked after picking this line
  228. - edit-on-add :: Edit this line when adding a new one
  229. - point-on-add :: Point will land here, when adding a new line
  230. - get-category-on-add :: This column will receive the nodes category during add
  231. - get-heading-on-add :: This column will receive the nodes heading during add"
  232. "A sample string of flags.")
  233. (defun org-index (&optional command search)
  234. "Mark and find your favorite things and org-locations easily:
  235. Create and update an index table of references and links. When
  236. searching, frequently used entries appear at the top and entering
  237. some keywords narrows down to matching entries only, so that the
  238. right one can be spotted easily.
  239. References are essentially small numbers (e.g. \"R237\" or \"-455-\"),
  240. as created by this package; they are well suited to be used
  241. outside of org. Links are normal `org-mode' links.
  242. This is version 3.1.1 of org-index.el .
  243. The function `org-index' operates on a dedicated table, the index
  244. table, which lives within its own Org-mode node. The table and
  245. its containing node will be created, when you first invoke
  246. `org-index'. The node will have some comments, describing the
  247. columns of the index table and their associated flags. The index
  248. table is found through the id of the containing node, which is
  249. stored within the variable `org-index-id'.
  250. The function `org-index' is the only interactive function of this
  251. package and its main entry point; once invoked it asks, which of
  252. it subcommands to execute:
  253. occur: Incremental search, that shows matching lines from the
  254. index table, updated after every keystroke. You may enter a
  255. list of words seperated by space or comma (`,'), to select
  256. lines that contain all of the given words.
  257. add: Add the current node to your index, so that it can be
  258. found through the subcommand \"occur\".
  259. delete: Delete the current node from your index.
  260. head: Ask for a reference number and search for an entry, which
  261. either has this reference contained in its heading or within
  262. its property org-index-ref.
  263. enter: Enter index table and maybe go to a specific reference.
  264. leave: Leave the index table and return to your previous location.
  265. ref: Create a new reference.
  266. help: Show this text.
  267. example: Create a temporary index, that will not be saved, but
  268. may serve as an example.
  269. reorder: Temporarily reorder the index table, e.g. by count,
  270. reference or last access.
  271. sort: Sort a set of lines (either from the active region or the
  272. whole buffer) by references found in each line.
  273. multi-occur: Apply Emacs standard `multi-occur' operation on all
  274. `org-mode' buffers to search for the given reference.
  275. highlight: Highlight or unhiglight references in active region or buffer.
  276. statistics : Show some statistics (e.g. minimum and maximum
  277. reference) about index table.
  278. If you invoke `org-index' for the first time, an assistant will be
  279. invoked, that helps you to create your own, commented index.
  280. Use `org-index-default-keybindings' to establish convenient
  281. keyboard shortcuts.
  282. Optional argument COMMAND is a symbol naming the command to execute;
  283. SEARCH specifies search string for commands that need one."
  284. (interactive "P")
  285. (let ((org-index--silent nil) ; t, if user can be asked
  286. prefix-arg ; prefix arg
  287. link-id ; link of starting node, if required
  288. guarded-search ; with guard against additional digits
  289. search-ref ; search, if search is a reference
  290. search-link ; search, if search is a link
  291. reorder-once ; column to use for single time sorting
  292. kill-new-text ; text that will be appended to kill ring
  293. initial-ref-or-link ; initial position in index table
  294. message-text) ; text that will be issued as an explanation
  295. ;;
  296. ;; Initialize and parse
  297. ;;
  298. ;; creates index table, if necessary
  299. (org-index--verify-id)
  300. ;; Get configuration of index table
  301. (org-index--parse-table)
  302. ;; store context information
  303. (org-index--retrieve-context)
  304. ;;
  305. ;; Find out, what we are supposed to do
  306. ;;
  307. (when (equal command '(4))
  308. (setq prefix-arg command)
  309. (setq command nil))
  310. (if command
  311. (unless (memq command org-index--commands)
  312. (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
  313. command (mapconcat 'symbol-name org-index--commands ",")))
  314. (let ((r (org-index--read-command))) ; query user if not from argument
  315. (setq command (car r))
  316. (setq reorder-once (cdr r))))
  317. ;;
  318. ;; Get search, if required
  319. ;;
  320. ;; These actions need a search string:
  321. (when (memq command '(enter head multi-occur))
  322. ;; Maybe we've got a search string from the arguments
  323. (setq search (org-index--get-or-read-search search command))
  324. (when search
  325. (when (string-match org-index--ref-regex search)
  326. (setq search-ref search)
  327. (setq guarded-search (org-index--make-guarded-search search)))
  328. (when (string-match "^[a-fA-F0-9]\\{8\\}-[a-fA-F0-9]\\{4\\}-[a-fA-F0-9]\\{4\\}-[a-fA-F0-9]\\{4\\}-[a-fA-F0-9]\\{12\\}$" search)
  329. (setq search-link search))))
  330. ;;
  331. ;; Check for invalid combinations of arguments; try to be helpful
  332. ;;
  333. (when (and (eq command 'head)
  334. (not search-ref)
  335. (not search-link))
  336. (error "Can do 'head' only for a reference or link (not '%s'), try 'occur' to search for text" search))
  337. ;;
  338. ;; Sort and enter table
  339. ;;
  340. ;; Get link if required before moving in
  341. (if (eq command 'add)
  342. (setq link-id (org-id-get-create)))
  343. ;; Save initial ref or link for later return
  344. (if (and org-index--within-node
  345. (org-at-table-p))
  346. (setq initial-ref-or-link
  347. (or (org-index--get-field 'ref)
  348. (org-index--get-field 'link))))
  349. ;; These commands enter index table only temporarily
  350. (when (memq command '(occur multi-occur statistics example))
  351. (set-buffer org-index--buffer)
  352. (goto-char org-index--point)
  353. ;; Sort and align
  354. (org-index--sort reorder-once)
  355. (org-index--align))
  356. ;; These commands will leave user in index table after they are finished
  357. (when (memq command '(enter ref))
  358. ;; Support orgmode-standard of going back (buffer and position)
  359. (org-mark-ring-push)
  360. (pop-to-buffer-same-window org-index--buffer)
  361. (goto-char org-index--point)
  362. (org-index--unfold-buffer)
  363. ;; Sort and align
  364. (org-index--sort reorder-once)
  365. (org-index--align)
  366. ;; Remember position for leave
  367. (if org-index--point-before
  368. (setq org-index--point-saved org-index--point-before)))
  369. ;; prepare to return to initial position in index table
  370. (when initial-ref-or-link
  371. (while (and (org-at-table-p)
  372. (not (or
  373. (string= initial-ref-or-link (org-index--get-field 'ref))
  374. (string= initial-ref-or-link (org-index--get-field 'link)))))
  375. (forward-line))
  376. ;; did not find ref, go back to top
  377. (if (not (org-at-table-p)) (goto-char org-index--point)))
  378. ;;
  379. ;; Actually do, what is requested
  380. ;;
  381. (cond
  382. ((eq command 'help)
  383. ;; bring up help-buffer for this function
  384. (describe-function 'org-index))
  385. ((eq command 'multi-occur)
  386. (org-index--update-line search-ref)
  387. ;; Construct list of all org-buffers
  388. (let (buff org-buffers)
  389. (dolist (buff (buffer-list))
  390. (set-buffer buff)
  391. (if (string= major-mode "org-mode")
  392. (setq org-buffers (cons buff org-buffers))))
  393. ;; Do multi-occur
  394. (multi-occur org-buffers guarded-search)
  395. ;; Present results
  396. (if (get-buffer "*Occur*")
  397. (progn
  398. (setq message-text (format "multi-occur for '%s'" search))
  399. (other-window 1)
  400. (toggle-truncate-lines 1))
  401. (setq message-text (format "Did not find '%s'" search)))))
  402. ((eq command 'add)
  403. (setq kill-new-text (org-index--do-add link-id)))
  404. ((eq command 'delete)
  405. (org-index--do-delete))
  406. ((eq command 'head)
  407. (let (link)
  408. (if (and org-index--within-node
  409. (org-at-table-p))
  410. (setq link (org-index--get-field 'link)))
  411. (setq message-text (org-index--do-head search-ref (or link search-link)))))
  412. ((eq command 'leave)
  413. (setq kill-new-text org-index--text-to-yank)
  414. (setq org-index--text-to-yank nil)
  415. ;; If "leave" has been called two times in succession, make
  416. ;; org-mark-ring-goto believe it has been called two times too
  417. (if (eq org-index--last-action 'leave)
  418. (let ((this-command nil) (last-command nil))
  419. (org-mark-ring-goto 1))
  420. (org-mark-ring-goto))
  421. ;; Return to saved position in index buffer
  422. (when org-index--point-saved
  423. ;; buffer displayed in window need to set point there first
  424. (if (eq (window-buffer org-index--active-window-index)
  425. org-index--buffer)
  426. (set-window-point org-index--active-window-index (marker-position org-index--point-saved)))
  427. ;; set position in buffer in any case and second
  428. (with-current-buffer org-index--buffer
  429. (goto-char org-index--point-saved)))
  430. (setq org-index--point-saved nil))
  431. ((eq command 'enter)
  432. (goto-char org-index--below-hline)
  433. (if search
  434. ;; Go downward in table to requested reference
  435. (setq message-text (org-index--find-in-index search search-link))
  436. ;; simply go into table
  437. (setq message-text "At index table"))
  438. (recenter))
  439. ((eq command 'occur)
  440. (org-index--do-occur))
  441. ((eq command 'ref)
  442. (let (new)
  443. ;; add a new row
  444. (setq new (org-index--create-new-line (eq command 'ref)))
  445. ;; fill special columns with standard values
  446. (org-table-goto-column (org-index--column-num 'ref))
  447. (insert new)
  448. (setq org-index--last-ref new)
  449. (org-index--align)
  450. ;; goto point-field or first empty one or first field
  451. (if (org-index--special-column 'point-on-add)
  452. (org-table-goto-column (org-index--column-num (org-index--special-column 'point-on-add)))
  453. (unless (catch 'empty
  454. (dotimes (col org-index--numcols)
  455. (org-table-goto-column (+ col 1))
  456. (if (string= (org-trim (org-table-get-field)) "")
  457. (throw 'empty t))))
  458. ;; none found, goto first
  459. (org-table-goto-column 1)))
  460. (if org-index--active-region (setq kill-new-text org-index--active-region))
  461. (setq message-text (format "Adding a new row with ref '%s'" new))))
  462. ((eq command 'sort)
  463. ;; sort lines according to contained reference
  464. (let (begin end where)
  465. (catch 'aborted
  466. ;; either active region or whole buffer
  467. (if (and transient-mark-mode
  468. mark-active)
  469. ;; sort only region
  470. (progn
  471. (setq begin (region-beginning))
  472. (setq end (region-end))
  473. (setq where "region"))
  474. ;; sort whole buffer
  475. (setq begin (point-min))
  476. (setq end (point-max))
  477. (setq where "whole buffer")
  478. ;; make sure
  479. (unless (y-or-n-p "Sort whole buffer? ")
  480. (setq message-text "Sort aborted")
  481. (throw 'aborted nil)))
  482. (save-excursion
  483. (save-restriction
  484. (goto-char (point-min))
  485. (narrow-to-region begin end)
  486. (sort-subr nil 'forward-line 'end-of-line
  487. (lambda ()
  488. (if (looking-at (concat ".*"
  489. (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
  490. (string-to-number (match-string 1))
  491. 0))))
  492. (highlight-regexp org-index--ref-regex 'isearch)
  493. (setq message-text (format "Sorted %s from character %d to %d, %d lines"
  494. where begin end
  495. (count-lines begin end)))))))
  496. ((eq command 'highlight)
  497. (let ((where "buffer"))
  498. (save-excursion
  499. (save-restriction
  500. (when (and transient-mark-mode
  501. mark-active)
  502. (narrow-to-region (region-beginning) (region-end))
  503. (setq where "region"))
  504. (if prefix-arg
  505. (progn
  506. (unhighlight-regexp org-index--ref-regex)
  507. (setq message-text (format "Removed highlights for references in %s" where)))
  508. (highlight-regexp org-index--ref-regex 'isearch)
  509. (setq message-text (format "Highlighted references in %s" where)))))))
  510. ((eq command 'statistics)
  511. (setq message-text (org-index--do-statistics)))
  512. ((eq command 'example)
  513. (if (y-or-n-p "This assistand will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
  514. (org-index--create-index t)))
  515. (t (error "This is a bug: unmatched case '%s'" command)))
  516. ;; remember what we have done for next time
  517. (setq org-index--last-action command)
  518. ;; tell, what we have done and what can be yanked
  519. (if kill-new-text (setq kill-new-text
  520. (substring-no-properties kill-new-text)))
  521. (if (string= kill-new-text "") (setq kill-new-text nil))
  522. (let ((m (concat
  523. message-text
  524. (if (and message-text kill-new-text)
  525. " and r"
  526. (if kill-new-text "R" ""))
  527. (if kill-new-text (format "eady to yank '%s'" kill-new-text) ""))))
  528. (unless (string= m "")
  529. (message m)
  530. (setq org-index--message-text m)))
  531. (if kill-new-text (kill-new kill-new-text))))
  532. (defun org-index-default-keybindings ()
  533. "Set default keybindings for `org-index'.
  534. Establish the common prefix key `C-c i' which is followed by the
  535. first letter of a subcommand, so that `C-c i a' invokes the
  536. subcommand \"add\". Subcommands available are occur, add, delete,
  537. head, enter, leave and ref. As a special case `C-c i i' invokes
  538. `org-index' to let you choose."
  539. (interactive)
  540. (define-prefix-command 'org-index-map)
  541. (global-set-key (kbd "C-c i") 'org-index-map)
  542. (define-key org-index-map (kbd "i") (lambda () (interactive) (message nil) (org-index)))
  543. (define-key org-index-map (kbd "o") (lambda () (interactive) (message nil) (org-index 'occur)))
  544. (define-key org-index-map (kbd "a") (lambda () (interactive) (message nil) (org-index 'add)))
  545. (define-key org-index-map (kbd "d") (lambda () (interactive) (message nil) (org-index 'delete)))
  546. (define-key org-index-map (kbd "h") (lambda () (interactive) (message nil) (org-index 'head)))
  547. (define-key org-index-map (kbd "e") (lambda () (interactive) (message nil) (org-index 'enter)))
  548. (define-key org-index-map (kbd "l") (lambda () (interactive) (message nil) (org-index 'leave)))
  549. (define-key org-index-map (kbd "r") (lambda () (interactive) (message nil) (org-index 'ref))))
  550. (defun org-index-new-line (&rest keys-values)
  551. "Create a new line within the index table, returning its reference.
  552. The function takes a varying number of argument pairs; each pair
  553. is a symbol for an existing column heading followed by its value.
  554. Example:
  555. (org-index-new-line 'ref t 'link \"7f480c3e\")
  556. Passing \"'ref t\" will make the function create a new reference
  557. within the new line.
  558. Optional argument KEYS-VALUES specifies content of new line."
  559. (org-index--verify-id)
  560. (org-index--parse-table)
  561. (car (org-index--do-new-line keys-values)))
  562. (defun org-index--do-new-line (&rest keys-values)
  563. "Do the work for `org-index-new-line'.
  564. Optional argument KEYS-VALUES specifies content of new line."
  565. (let ((org-index--silent t))
  566. (save-excursion
  567. (org-index--retrieve-context)
  568. (with-current-buffer org-index--buffer
  569. (goto-char org-index--point)
  570. ;; check arguments early
  571. (let ((kvs keys-values)
  572. k v)
  573. (while kvs
  574. (setq k (car kvs))
  575. (setq v (cadr kvs))
  576. (if (eq k 'ref)
  577. (unless (memq v '(t nil))
  578. (error "Column 'ref' accepts only \"t\" or \"nil\""))
  579. (if (or (not (symbolp k))
  580. (and (symbolp v) (not (eq v t)) (not (eq v nil))))
  581. (error "Arguments must be alternation of key and value")))
  582. (unless (org-index--column-num k)
  583. (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
  584. (setq kvs (cddr kvs))))
  585. (if (and (not (plist-get keys-values 'ref))
  586. (not (stringp (plist-get keys-values 'link))))
  587. (error "Need a link when not creating a ref"))
  588. (let (ref yank)
  589. ;; create new line
  590. (setq ref (org-index--create-new-line (plist-get keys-values 'ref)))
  591. (plist-put keys-values 'ref (or ref ""))
  592. ;; fill columns
  593. (let ((kvs keys-values)
  594. k v n)
  595. (while kvs
  596. (setq k (car kvs))
  597. (setq v (cadr kvs))
  598. (setq n (org-index--column-num k))
  599. (org-table-goto-column n)
  600. (insert v)
  601. (setq kvs (cddr kvs))))
  602. ;; get column to yank
  603. (setq yank (org-trim (org-table-get-field (org-index--column-num (org-index--special-column 'yank-after-add)))))
  604. (org-index--sort)
  605. (cons ref yank))))))
  606. (defun org-index-get-line (type value)
  607. "Retrieve an existing line within the index table by ref or link.
  608. Return its contents as a property list.
  609. The function `plist-get' may be used to retrieve specific elements
  610. from the result.
  611. Example:
  612. (plist-get (org-index-get-line 'ref \"R12\") 'count)
  613. retrieves the value of the count-column for reference number 12.
  614. Argument TYPE is a symbol, either ref or link,
  615. argument VALUE specifies the value to search for."
  616. ;; check arguments
  617. (unless (memq type '(ref link))
  618. (error "Argument can only be 'ref' or 'link'"))
  619. (org-index--verify-id)
  620. (org-index--parse-table)
  621. (org-index--get-or-delete-line 'get type value))
  622. (defun org-index--get-or-delete-line (command type value)
  623. "Get or delete (according to COMMAND) a line by TYPE and VALUE."
  624. (let ((org-index--silent t)
  625. found)
  626. (save-excursion
  627. (org-index--retrieve-context)
  628. (with-current-buffer org-index--buffer
  629. (goto-char org-index--point)
  630. (goto-char org-index--below-hline)
  631. (while (and (not found)
  632. (org-at-table-p))
  633. (when (string= (org-index--get-field type)
  634. value)
  635. ;; found matching line
  636. (if (eq command 'get)
  637. ;; get its fields
  638. (mapc (lambda (x)
  639. (if (and (numberp (cdr x))
  640. (> (cdr x) 0))
  641. (setq found (cons (car x) (cons (or (org-index--get-field (car x)) "") found)))
  642. )) (reverse org-index--columns))
  643. ;; or delete it
  644. (let ((start (point)))
  645. (beginning-of-line)
  646. (forward-line)
  647. (delete-region start (point)))))
  648. (forward-line))))
  649. found))
  650. (defun org-index--read-command ()
  651. "Find out, what we are supposed to do."
  652. (let (reorder-once ; Column to use for single time sorting
  653. command
  654. input)
  655. ;; Ask user, what to do
  656. (while (progn
  657. (setq input
  658. (org-completing-read
  659. "Please choose: "
  660. (mapcar 'symbol-name org-index--commands)
  661. nil nil))
  662. ;; convert to symbol
  663. (setq command (intern input))
  664. ;; ask for reorder in loop, because we may have to ask for command right again
  665. (if (eq command 'reorder)
  666. (setq reorder-once
  667. (intern
  668. (org-icompleting-read
  669. "Please choose column to reorder index table once: "
  670. (list "ref" "count" "created" "last-accessed")
  671. nil t))))
  672. ;; maybe ask initial question again
  673. (eq command 'reorder)))
  674. (cons command reorder-once)))
  675. (defun org-index--get-or-read-search (search command)
  676. "Get SEARCH string, maybe read from user; respect COMMAND that will be executed."
  677. (let (search-from-table
  678. search-from-cursor)
  679. (unless search
  680. ;; Search string can come from several sources:
  681. ;; From link or ref columns of table
  682. (when (and org-index--within-node
  683. (org-at-table-p))
  684. (setq search-from-table (or (org-index--get-field 'link)
  685. (org-index--get-field 'ref))))
  686. ;; From string below cursor
  687. (when (and (not org-index--within-node)
  688. org-index--below-cursor
  689. (string-match (concat "\\(" org-index--ref-regex "\\)")
  690. org-index--below-cursor))
  691. (setq search-from-cursor (match-string 1 org-index--below-cursor)))
  692. ;; Depending on requested action, get search from one of the sources above
  693. (cond ((eq command 'enter)
  694. (setq search search-from-cursor))
  695. ((memq command '(head occur))
  696. (setq search (or search-from-table search-from-cursor)))))
  697. ;; From occur-buffer into index ?
  698. (unless search
  699. (if (and (string= (buffer-name) org-index--occur-buffer-name)
  700. (org-at-table-p))
  701. (setq search (org-index--get-field 'ref))))
  702. ;; If we still do not have a search string, ask user explicitly
  703. (unless search
  704. (if org-index--silent (error "Need to specify search, if silence is required"))
  705. (if (eq command 'enter)
  706. ;; accept single char commands or switch to reading a sequence of digits
  707. (let (char prompt)
  708. ;; read one character
  709. (while (not (memq char (append (number-sequence ?0 ?9) (list ?c ?l ?. ?\C-m))))
  710. ;; start with short prompt but give more help on next iteration
  711. (setq prompt "Please specify, where to go (0-9.l<return> or ? for help): ")
  712. (setq char (read-char prompt))
  713. (setq prompt "Digits specify a reference number to got to, `.' goes to index line of current node, `l' to last line created and <return> to top of index. Please choose: "))
  714. (if (memq char (number-sequence ?0 ?9))
  715. ;; read rest of digits
  716. (setq search (read-from-minibuffer "Search reference number: " (char-to-string char)))
  717. ;; decode single chars
  718. (if (eq char ?.) (setq search (org-id-get)))
  719. (if (eq char ?\C-m) (setq search nil))
  720. (if (eq char ?l) (setq search (number-to-string org-index--maxref)))))
  721. (setq search (read-from-minibuffer "Search reference number: "))))
  722. ;; Clean up and examine search string
  723. (when search
  724. (setq search (org-trim search))
  725. (if (string= search "") (setq search nil))
  726. (when search
  727. (if (string-match "^[0-9]+$" search)
  728. (setq search (concat org-index--head search org-index--tail)))))
  729. search))
  730. (defun org-index--verify-id ()
  731. "Check, that we have a valid id."
  732. ;; Check id
  733. (unless org-index-id
  734. (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet."))
  735. ;; Find node
  736. (let (marker)
  737. (setq marker (org-id-find org-index-id 'marker))
  738. (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
  739. ; Try again with new node
  740. (setq marker (org-id-find org-index-id 'marker))
  741. (unless marker (error "Could not create node"))
  742. (setq org-index--buffer (marker-buffer marker)
  743. org-index--point (marker-position marker))
  744. (move-marker marker nil)))
  745. (defun org-index--retrieve-context ()
  746. "Collect context information before starting with command."
  747. ;; Get the content of the active region or the word under cursor
  748. (setq org-index--active-region
  749. (if (and transient-mark-mode mark-active)
  750. (buffer-substring (region-beginning) (region-end))
  751. nil))
  752. (setq org-index--below-cursor (thing-at-point 'symbol))
  753. ;; get category of current node
  754. (setq org-index--category-before
  755. (save-excursion ; workaround: org-get-category does not give category when at end of buffer
  756. (beginning-of-line)
  757. (org-get-category)))
  758. ;; Find out, if we are within index table or not
  759. (setq org-index--within-node (string= (org-id-get) org-index-id))
  760. ;; Check and remember, if active window contains buffer with index table
  761. (if (eq (window-buffer) org-index--buffer)
  762. (setq org-index--active-window-index (selected-window)))
  763. ;; get current position in index-buffer
  764. (with-current-buffer org-index--buffer
  765. (setq org-index--point-before
  766. (if (string= (org-id-get) org-index-id)
  767. nil
  768. (point-marker)))))
  769. (defun org-index--parse-table ()
  770. "Parse content of index table."
  771. (let (ref-field
  772. link-field
  773. initial-point
  774. end-of-headings
  775. start-of-headings)
  776. (with-current-buffer org-index--buffer
  777. (setq org-index--maxref 0)
  778. (setq initial-point (point))
  779. (org-index--go-below-hline)
  780. (beginning-of-line)
  781. ;; get headings to display during occur
  782. (setq end-of-headings (point))
  783. (while (org-at-table-p) (forward-line -1))
  784. (forward-line)
  785. (setq start-of-headings (point))
  786. (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
  787. ;; count columns
  788. (org-table-goto-column 100)
  789. (setq org-index--numcols (- (org-table-current-column) 1))
  790. ;; go to top of table
  791. (while (org-at-table-p)
  792. (forward-line -1))
  793. (forward-line)
  794. ;; parse line of headings
  795. (org-index--parse-headings)
  796. (forward-line 2)
  797. ;; parse list of flags
  798. (goto-char org-index--point)
  799. (org-index--parse-flags)
  800. (org-index--go-below-hline)
  801. (setq org-index--below-hline (point-marker))
  802. ;; Retrieve any decorations around the number within the first nonempty ref-field
  803. (goto-char org-index--below-hline)
  804. (while (and (org-at-table-p)
  805. (not (setq ref-field (org-index--get-field 'ref))))
  806. (forward-line))
  807. ;; Some Checking
  808. (unless ref-field
  809. (org-index--report-index-error "Reference column is empty"))
  810. (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
  811. (org-index--report-index-error
  812. "First reference in index table ('%s') does not contain a number" ref-field))
  813. ;; These are the decorations used within the first ref of index
  814. (setq org-index--head (match-string 1 ref-field))
  815. (setq org-index--tail (match-string 3 ref-field))
  816. (setq org-index--ref-regex (concat (regexp-quote org-index--head)
  817. "\\([0-9]+\\)"
  818. (regexp-quote org-index--tail)))
  819. (setq org-index--ref-format (concat org-index--head "%d" org-index--tail))
  820. ;; Go through table to find maximum number and do some checking
  821. (let ((ref 0))
  822. (while (org-at-table-p)
  823. (setq ref-field (org-index--get-field 'ref))
  824. (setq link-field (org-index--get-field 'link))
  825. (when (and (not ref-field)
  826. (not link-field))
  827. (kill-whole-line)
  828. (message "Removing line from index-table with both ref and link empty"))
  829. (if ref-field
  830. (if (string-match org-index--ref-regex ref-field)
  831. ;; grab number
  832. (setq ref (string-to-number (match-string 1 ref-field)))
  833. (kill-whole-line)
  834. (message "Removing line from index-table whose ref does not contain a number")))
  835. ;; check, if higher ref
  836. (if (> ref org-index--maxref) (setq org-index--maxref ref))
  837. (forward-line 1)))
  838. ;; go back to initial position
  839. (goto-char initial-point))))
  840. (defun org-index--sort (&optional sort-column)
  841. "Sort index table maybe according to SORT-COLUMN."
  842. (unless sort-column (setq sort-column (org-index--special-column 'sort)))
  843. (let ((is-modified (buffer-modified-p))
  844. top
  845. bottom
  846. ref-field
  847. count-field)
  848. (unless buffer-read-only
  849. ;; get boundaries of table
  850. (goto-char org-index--below-hline)
  851. (forward-line 0)
  852. (setq top (point))
  853. (while (org-at-table-p) (forward-line))
  854. ;; Kill all empty rows at bottom
  855. (while (progn
  856. (forward-line -1)
  857. (org-table-goto-column 1)
  858. (and
  859. (not (org-index--get-field 'ref))
  860. (not (org-index--get-field 'link))))
  861. (org-table-kill-row))
  862. (forward-line 1)
  863. (setq bottom (point))
  864. (save-restriction
  865. (narrow-to-region top bottom)
  866. (goto-char top)
  867. (sort-subr t
  868. 'forward-line
  869. 'end-of-line
  870. (lambda ()
  871. (let (ref
  872. (ref-field (or (org-index--get-field 'ref) ""))
  873. (count-field (or (org-index--get-field 'count) "")))
  874. ;; get reference with leading zeroes, so it can be
  875. ;; sorted as text
  876. (string-match org-index--ref-regex ref-field)
  877. (setq ref (format
  878. "%06d"
  879. (string-to-number
  880. (or (match-string 1 ref-field)
  881. "0"))))
  882. ;; Construct different sort-keys according to
  883. ;; requested sort column
  884. (cond
  885. ((eq sort-column 'count)
  886. (concat (format
  887. "%08d"
  888. (string-to-number (or (org-index--get-field 'count)
  889. "")))
  890. ref))
  891. ((eq sort-column 'ref)
  892. ref)
  893. ((eq sort-column 'last-accessed)
  894. (concat (org-index--get-field sort-column)
  895. " "
  896. ref))
  897. (t (error "This is a bug: unmatched case '%s'" sort-column)))))
  898. nil 'string<))
  899. ;; restore modification state
  900. (set-buffer-modified-p is-modified))))
  901. (defun org-index--go-below-hline ()
  902. "Move below hline in index-table."
  903. (let ((count 0)
  904. (errstring (format "index table within node %s" org-index-id)))
  905. (goto-char org-index--point)
  906. ;; go to heading of node
  907. (while (not (org-at-heading-p)) (forward-line -1))
  908. (forward-line 1)
  909. ;; go to first table, but make sure we do not get into another node
  910. (while (and (not (org-at-table-p))
  911. (not (org-at-heading-p))
  912. (not (eobp)))
  913. (forward-line))
  914. ;; check, if there really is a table
  915. (unless (org-at-table-p)
  916. (org-index--create-missing-index "Cannot find %s." errstring))
  917. ;; go just after hline
  918. (while (and (not (org-at-table-hline-p))
  919. (org-at-table-p))
  920. (forward-line))
  921. (forward-line)
  922. ;; and check
  923. (unless (org-at-table-p)
  924. (org-index--report-index-error "Cannot find a hline within %s" errstring))
  925. (org-table-goto-column 1)))
  926. (defun org-index--align ()
  927. "Align index table without setting its buffer modified."
  928. (let ((is-modified (buffer-modified-p)))
  929. (unless buffer-read-only (org-table-align))
  930. (set-buffer-modified-p is-modified)
  931. (org-index--go-below-hline)
  932. (setq org-index--below-hline (point-marker))))
  933. (defun org-index--parse-headings ()
  934. "Parse headings of index table."
  935. (let (field ;; field content
  936. field-symbol ;; and as a symbol
  937. found)
  938. (setq org-index--columns nil)
  939. ;; For each column
  940. (dotimes (col org-index--numcols)
  941. (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
  942. (if (string= field "")
  943. (error "Column name cannot be empty"))
  944. (if (and (not (string= (substring field 0 1) "."))
  945. (not (member (intern field) org-index--valid-headings)))
  946. (error "Column name '%s' is not a valid heading (custom headings may start with a dot (e.g. '.foo')" field))
  947. (setq field-symbol (intern field))
  948. ;; check if heading has already appeared
  949. (if (assoc field-symbol org-index--columns)
  950. (org-index--report-index-error
  951. "'%s' appears two times as column heading" (downcase field))
  952. ;; add it to list at front, reverse later
  953. (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
  954. (setq org-index--columns (reverse org-index--columns))
  955. ;; check if all necessary headings have appeared
  956. (mapc (lambda (head)
  957. (unless (cdr (assoc head org-index--columns))
  958. (org-index--report-index-error "No column has heading '%s'" head)))
  959. org-index--required-headings))
  960. (defun org-index--parse-flags ()
  961. "Parse list of flags in index table."
  962. (let (parent parent-is-comment child)
  963. ;; reset configuration variables
  964. (setq org-index--special-columns nil)
  965. (setq org-index--flagged-columns nil)
  966. (org-index--goto-list "columns-and-flags" t)
  967. (forward-line 1)
  968. ;; outer loop over columns
  969. (while (and (setq parent (org-index--parse-list-item))
  970. parent
  971. (> (cdr (assoc :indent parent)) 0))
  972. (setq parent-is-comment (member (cdr (assoc :text parent)) '("all-columns-explained" "all-flags-explained")))
  973. ;; check, that we have a valid heading
  974. (unless (or parent-is-comment
  975. (assoc (cdr (assoc :sym parent)) org-index--columns))
  976. (org-index--report-index-error "'%s' is not a valid column" (cdr (assoc :text parent))))
  977. ;; inner loop over children
  978. (while (and (forward-line 1)
  979. (setq child (org-index--parse-list-item))
  980. child
  981. (> (cdr (assoc :indent child))
  982. (cdr (assoc :indent parent))))
  983. (unless parent-is-comment
  984. ;; check, that we have a valid flag
  985. (unless (memq (cdr (assoc :sym child)) org-index--all-flags)
  986. (org-index--report-index-error "'%s' is not a valid flag" (cdr (assoc :text child))))
  987. ;; process flag with respect to current index-column
  988. (if (memq (cdr (assoc :sym child)) org-index--single-flags)
  989. ;; Check, that none of org-index--single-flags appears twice
  990. (if (assoc (cdr (assoc :sym child)) org-index--special-columns)
  991. (org-index--report-index-error
  992. "More than one column is marked with flag '%s'" (cdr (assoc :text child)))
  993. ;; add it to list
  994. (setq org-index--special-columns (cons (cons (cdr (assoc :sym child)) (cdr (assoc :sym parent)))
  995. org-index--special-columns))))
  996. ;; all flags are stored in org-index--flagged-columns
  997. (let ((l (assoc (cdr (assoc :sym child)) org-index--flagged-columns))) ;; list of flag and columns, that carry this flag
  998. (unless l
  999. ;; no list of columns with this flag is present, create one
  1000. (setq org-index--flagged-columns
  1001. (cons (cons (cdr (assoc :sym child)) nil)
  1002. org-index--flagged-columns))
  1003. (setq l (car org-index--flagged-columns)))
  1004. ;; prepend this column to list of columns with this flag
  1005. (setcdr l (cons (cdr (assoc :sym parent)) (cdr l)))))))
  1006. ;; check, that all needed flags have been specified
  1007. (mapc (lambda (x)
  1008. (unless (assoc x org-index--special-columns)
  1009. (org-index--report-index-error "Required flag '%s' does not appear" (substring (symbol-name x) 1))))
  1010. org-index--required-flags)))
  1011. (defun org-index--goto-list (name &optional required non-top)
  1012. "Goto list NAME (maybe NON-TOP Level) in index node, err if REQUIRED list is not present."
  1013. (goto-char org-index--point)
  1014. ;; go to heading of node
  1015. (while (not (org-at-heading-p)) (forward-line -1))
  1016. (forward-line 1)
  1017. ;; go to named list
  1018. (while (and (not (let ((item (org-index--parse-list-item)))
  1019. (if item
  1020. (and (or non-top (= (cdr (assoc :indent item)) 0)) ;; accept only toplevel ?
  1021. (string= (cdr (assoc :text item)) name)) ;; with requested name
  1022. nil)))
  1023. (not (org-at-table-p))
  1024. (not (org-at-heading-p))
  1025. (not (eobp)))
  1026. (forward-line 1))
  1027. (if (org-at-item-p)
  1028. t
  1029. (if required
  1030. (org-index--report-index-error "Could not find required list '%s'" name)
  1031. nil)))
  1032. (defun org-index--parse-list-item ()
  1033. "Parse a list item into an assoc array (indent, checkbox, text, value)."
  1034. (if (looking-at org-list-full-item-re)
  1035. ;; retrieve interesting parts of list item from match data
  1036. (let (alist indent checkbox text value)
  1037. (setq indent (save-excursion
  1038. (goto-char (match-beginning 1))
  1039. (current-column)))
  1040. (decf indent (+ (save-match-data (org-current-level)) 1))
  1041. (add-to-list 'alist (cons :indent indent))
  1042. (setq checkbox (match-string 3))
  1043. (setq text (match-string 4))
  1044. (setq value (buffer-substring
  1045. (match-end 0)
  1046. (save-excursion (end-of-line) (point))))
  1047. (when (not text)
  1048. (setq text value)
  1049. (setq value nil))
  1050. (add-to-list 'alist (cons :text text))
  1051. (add-to-list 'alist (cons :value value))
  1052. (add-to-list 'alist (cons :sym (intern text)))
  1053. ;; clean up strings
  1054. (mapc (lambda (x) (if (stringp (cdr x)) (setf (cdr x) (org-trim (substring-no-properties (cdr x)))))) alist)
  1055. alist)
  1056. nil))
  1057. (defun org-index--create-missing-index (&rest reasons)
  1058. "Create a new empty index table with detailed explanation. Argument REASONS explains why."
  1059. (org-index--ask-before-create-index "Cannot find your index table: "
  1060. "new permanent" "."
  1061. reasons)
  1062. (org-index--create-index))
  1063. (defun org-index--report-index-error (&rest reasons)
  1064. "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
  1065. (org-index--ask-before-create-index "The existing index contains this error: "
  1066. "temporary" ", to compare with."
  1067. reasons)
  1068. (org-index--create-index t t))
  1069. (defun org-index--ask-before-create-index (explanation type for-what reasons)
  1070. ; checkdoc-params: (explanation type for-what reasons)
  1071. "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
  1072. (let (reason prompt)
  1073. (setq reason (apply 'format reasons))
  1074. (if org-index--silent (error (concat explanation reason))) ; cannot proceed without querying the user
  1075. (setq prompt (concat explanation reason "\n\n"
  1076. "However, this assistant can help you to create a "
  1077. type " index with detailed comments" for-what "\n\n"
  1078. "Do you want to proceed ?"))
  1079. (unless (let ((max-mini-window-height 1.0))
  1080. (y-or-n-p prompt))
  1081. (error explanation reason))))
  1082. (defun org-index--create-index (&optional temporary compare)
  1083. "Create a new empty index table with detailed explanation.
  1084. specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
  1085. (let (buffer
  1086. title
  1087. firstref
  1088. id)
  1089. (if temporary
  1090. (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
  1091. (buffer-name "*org-index-example-index*"))
  1092. (setq buffer (get-buffer-create buffer-name))
  1093. (with-current-buffer buffer
  1094. ;; but it needs a file for its index to be found
  1095. (unless (string= (buffer-file-name) file-name)
  1096. (set-visited-file-name file-name))
  1097. (rename-buffer buffer-name) ; name is change by line above
  1098. (erase-buffer)
  1099. (org-mode)))
  1100. (setq buffer (get-buffer (org-completing-read "Please choose the buffer, where the new node for the index table should be created; the new node will be inserted at its end.\n\nBuffer: " (mapcar 'buffer-name (org-buffer-list)) nil nil))))
  1101. (setq title (read-from-minibuffer "Please enter the title of the index node: "))
  1102. (while (progn
  1103. (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is a number preceeded by some non-digit chars and optionally followed by some more non-digit chars, e.g. 'R1', '-1-' or '#1#' (and your initial number does not need to be '1'). The format of your reference-numbers only needs to make sense for yourself, so that you can spot it easily in your texts or write it on a piece of paper; it should however not already appear to frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose: "))
  1104. (let (desc)
  1105. (when (string-match "[[:blank:]]" firstref)
  1106. (setq desc "Contains whitespace"))
  1107. (when (string-match "[[:cntrl:]]" firstref)
  1108. (setq desc "Contains control characters"))
  1109. (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
  1110. ;; firstref not okay, report details
  1111. (setq desc
  1112. (cond ((string= firstref "") "is empty")
  1113. ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
  1114. ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
  1115. ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
  1116. )))
  1117. (if desc
  1118. (progn
  1119. (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
  1120. t)
  1121. nil))))
  1122. (with-current-buffer buffer
  1123. (goto-char (point-max))
  1124. (insert (format "* %s %s\n" firstref title))
  1125. (if temporary
  1126. (insert "
  1127. Below you find your temporary index table, which will not last longer
  1128. than your current emacs session.
  1129. ")
  1130. (insert "
  1131. Below you find your initial index table, which will grow over time.
  1132. "))
  1133. (insert "
  1134. You may start using it by adding some lines. Just move to
  1135. another heading, invoke `org-index' and choose the command
  1136. \"add\". After adding a few nodes, try the command \"occur\"
  1137. to search among them.
  1138. To gain further insight you may invoke the subcommand \"help\", or
  1139. read the description of `org-index'.
  1140. Within the index table below, dhe sequence of columns does not
  1141. matter. You may reorder them any way you like. Columns are
  1142. found by their heading. You may also add your own columns.
  1143. Following these explanations there is the item-list
  1144. `columns-and-flags', which influences the behaviour of
  1145. `org-index'. See the explanations which are part of this list.
  1146. This node needs not be a top level node; its name is completely
  1147. at your choice; it is found through its ID only.
  1148. Remark: These lines of explanation can be removed at any time.
  1149. ")
  1150. (setq id (org-id-get-create))
  1151. (insert (format "
  1152. %s
  1153. | ref | .category | keywords | count | last-accessed | created | link |
  1154. | | | | | | | <4> |
  1155. |-----+-----------+----------+-------+---------------+---------+------|
  1156. | %s | | %s | | | %s | %s |
  1157. "
  1158. org-index--sample-flags
  1159. firstref
  1160. "This node"
  1161. (with-temp-buffer (org-insert-time-stamp nil nil t))
  1162. id))
  1163. ;; make sure, that node can be found
  1164. (org-id-add-location id (buffer-file-name))
  1165. (setq buffer-save-without-query t)
  1166. (basic-save-buffer)
  1167. (while (not (org-at-table-p)) (forward-line -1))
  1168. (unless buffer-read-only (org-table-align))
  1169. (while (not (org-at-heading-p)) (forward-line -1))
  1170. ;; read back some info about new index
  1171. (let ((org-index-id id))
  1172. (org-index--verify-id))
  1173. ;; present results to user
  1174. (if temporary
  1175. (progn
  1176. ;; Present existing and temporary index together
  1177. (when compare
  1178. (pop-to-buffer-same-window org-index--buffer)
  1179. (goto-char org-index--point)
  1180. (org-index--unfold-buffer)
  1181. (delete-other-windows)
  1182. (select-window (split-window-vertically)))
  1183. ;; show new index
  1184. (pop-to-buffer-same-window buffer)
  1185. (org-id-goto id)
  1186. (org-index--unfold-buffer)
  1187. (if compare
  1188. (error "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
  1189. (message "This is your new temporary index.")))
  1190. (progn
  1191. ;; Only show the new index
  1192. (pop-to-buffer-same-window buffer)
  1193. (delete-other-windows)
  1194. (org-id-goto id)
  1195. (org-index--unfold-buffer)
  1196. (setq org-index-id id)
  1197. (if (y-or-n-p "This is your new index table. It is already set for this Emacs session, so you may try it out. Do you want to save its id to make it available for future Emacs sessions too ? ")
  1198. (progn
  1199. (customize-save-variable 'org-index-id id)
  1200. (error "Saved org-index-id '%s' to %s" id (or custom-file
  1201. user-init-file)))
  1202. (let (sq)
  1203. (setq sq (format "(setq org-index-id \"%s\")" id))
  1204. (kill-new sq)
  1205. (error "Did not make the id of this new index permanent; you may want to put\n\n %s\n\ninto your own initialization; it is copied already, just yank it" sq))))))))
  1206. (defun org-index--unfold-buffer ()
  1207. "Helper function to unfold buffer."
  1208. (org-show-context)
  1209. (org-show-subtree)
  1210. (recenter 1)
  1211. (save-excursion
  1212. (org-back-to-heading)
  1213. (forward-line) ;; on property drawer
  1214. (org-cycle)
  1215. (org-index--goto-list "columns-and-flags")
  1216. (org-cycle)))
  1217. (defun org-index--update-line (ref-or-link)
  1218. "Update columns count and last-accessed in line REF-OR-LINK."
  1219. (let ((newcount 0)
  1220. initial)
  1221. (with-current-buffer org-index--buffer
  1222. (unless buffer-read-only
  1223. ;; search reference or link, if given (or assume, that we are already positioned right)
  1224. (when ref-or-link
  1225. (setq initial (point))
  1226. (goto-char org-index--below-hline)
  1227. (while (and (org-at-table-p)
  1228. (not (or (string= ref-or-link (org-index--get-field 'ref))
  1229. (string= ref-or-link (org-index--get-field 'link)))))
  1230. (forward-line)))
  1231. (if (not (org-at-table-p))
  1232. (error "Did not find reference or link '%s'" ref-or-link)
  1233. (org-index--update-current-line))
  1234. (if initial (goto-char initial))))))
  1235. (defun org-index--update-current-line ()
  1236. "Update current lines columns count and last-accessed."
  1237. (let (newcount (count-field (org-index--get-field 'count)))
  1238. ;; update count field only if number or empty
  1239. (when (or (not count-field)
  1240. (string-match "^[0-9]+$" count-field))
  1241. (setq newcount (+ 1 (string-to-number (or count-field "0"))))
  1242. (org-index--get-field 'count
  1243. (number-to-string newcount)))
  1244. ;; update timestamp
  1245. (org-table-goto-column (org-index--column-num 'last-accessed))
  1246. (org-table-blank-field)
  1247. (org-insert-time-stamp nil t t)))
  1248. (defun org-index--get-field (key &optional value)
  1249. "Retrieve field KEY from index table or set it to VALUE."
  1250. (let (field)
  1251. (save-excursion
  1252. (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value)))
  1253. (if (string= field "") (setq field nil))
  1254. (org-no-properties field))))
  1255. (defun org-index--column-num (key)
  1256. "Return number of column KEY."
  1257. (if (numberp key)
  1258. key
  1259. (cdr (assoc key org-index--columns))))
  1260. (defun org-index--special-column (key)
  1261. "Return column (not a number) for special column KEY."
  1262. (cdr (assoc key org-index--special-columns)))
  1263. (defun org-index--flag-p (flag column)
  1264. "Check if COLUMN has FLAG set."
  1265. (unless (memq flag org-index--all-flags)
  1266. (error (format "Internal error: unknown flag %s" (symbol-name flag))))
  1267. (memq column (assoc flag org-index--flagged-columns)))
  1268. (defun org-index--make-guarded-search (ref &optional dont-quote)
  1269. "Make robust search string from REF; DONT-QUOTE it, if requested."
  1270. (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
  1271. (defun org-index--do-statistics ()
  1272. "Compute statistics about index table."
  1273. (let ((total 0)
  1274. ref-field
  1275. ref
  1276. min
  1277. max
  1278. message)
  1279. ;; go through table and remove all refs, that we see
  1280. (goto-char org-index--below-hline)
  1281. (while (org-at-table-p)
  1282. ;; get ref-field and number
  1283. (setq ref-field (org-index--get-field 'ref))
  1284. (if (and ref-field
  1285. (string-match org-index--ref-regex ref-field))
  1286. (setq ref (string-to-number (match-string 1 ref-field))))
  1287. ;; record min and max
  1288. (if (or (not min) (< ref min)) (setq min ref))
  1289. (if (or (not max) (> ref max)) (setq max ref))
  1290. ;; count
  1291. (setq total (1+ total))
  1292. (forward-line))
  1293. (setq message (format "Found %d references from %s to %s."
  1294. total
  1295. (format org-index--ref-format min)
  1296. (format org-index--ref-format max)))
  1297. message))
  1298. (defun org-index--do-add (link-id)
  1299. "For current node (with id LINK-ID): add a new line to index table."
  1300. (let ((args (list 'ref t 'link link-id))
  1301. ref-and-yank content)
  1302. (unless (org-at-heading-p)
  1303. (error "Not at headline"))
  1304. ;; some fields want to be edited
  1305. (dolist (col-num org-index--columns)
  1306. (setq content "")
  1307. ;; copy heading ?
  1308. (if (org-index--flag-p 'get-heading-on-add (car col-num))
  1309. (setq content (nth 4 (org-heading-components))))
  1310. ;; copy category ?
  1311. (if (org-index--flag-p 'get-category-on-add (car col-num))
  1312. (setq content org-index--category-before))
  1313. (if (org-index--flag-p 'edit-on-add (car col-num))
  1314. (setq content (read-from-minibuffer
  1315. (format "Edit text for column '%s': " (symbol-name (car col-num)))
  1316. content)))
  1317. (if (not (string= content ""))
  1318. (setq args (append (list (car col-num) content) args))))
  1319. ;; new line in index table
  1320. (setq ref-and-yank (apply 'org-index--do-new-line args))
  1321. ;; insert reference
  1322. (org-entry-put (point) "org-index-ref" (car ref-and-yank))
  1323. (cdr ref-and-yank)))
  1324. (defun org-index--do-delete ()
  1325. "Perform command delete."
  1326. (unless (org-at-heading-p)
  1327. (error "Not at headline"))
  1328. (let ((ref (org-entry-get (point) "org-index-ref")))
  1329. ;; delete from index table
  1330. (org-index--get-or-delete-line 'delete 'ref ref)
  1331. ;; delete from property
  1332. (org-entry-delete (point) "org-index-ref")
  1333. ;; maybe delete from heading
  1334. (save-excursion
  1335. (end-of-line)
  1336. (let ((end (point)))
  1337. (beginning-of-line)
  1338. (when (search-forward ref end t)
  1339. (delete-char (- (length ref)))
  1340. (just-one-space))))))
  1341. (defun org-index--find-in-index (search &optional search-link)
  1342. "Find index line with ref or link SEARCH (decided by SEARCH-LINK)."
  1343. (let ((initial (point))
  1344. found text)
  1345. (while (and (not found)
  1346. (forward-line)
  1347. (org-at-table-p))
  1348. (save-excursion
  1349. (setq found
  1350. (string= search
  1351. (org-index--get-field
  1352. (if search-link 'link 'ref))))))
  1353. (if found
  1354. (progn
  1355. (setq text (format "Found index line '%s'" search))
  1356. (org-index--update-line nil)
  1357. (org-table-goto-column (org-index--column-num 'ref))
  1358. (if (looking-back " ") (backward-char))
  1359. ;; remember string to copy
  1360. (setq org-index--text-to-yank
  1361. (org-trim (org-table-get-field (org-index--column-num 'copy)))))
  1362. (setq text (format "Did not find index line '%s'" search))
  1363. (goto-char initial)
  1364. (forward-line))
  1365. text))
  1366. (defun org-index--do-head (ref link &optional other)
  1367. "Perform command head: Find node with REF or LINK and present it; if OTHER in separate window."
  1368. (if ref (setq org-index--last-ref ref))
  1369. (let (message marker)
  1370. ;; Prefer link if available
  1371. (if link
  1372. (setq marker (org-id-find link t))
  1373. (setq marker
  1374. (catch 'found
  1375. (message (format "Scanning headlines for '%s' ..." ref))
  1376. (org-map-entries
  1377. (lambda ()
  1378. (when (string= ref (org-entry-get (point) "org-index-ref"))
  1379. (throw 'found (point-marker))))
  1380. nil 'agenda)
  1381. nil)))
  1382. (if marker
  1383. (progn
  1384. (org-index--update-line (or link ref))
  1385. (if link
  1386. (setq message "Followed link")
  1387. (setq message (format "Found headline '%s'" ref)))
  1388. (let (cb)
  1389. (if other
  1390. (progn
  1391. (setq cb (current-buffer))
  1392. (pop-to-buffer (marker-buffer marker)))
  1393. (pop-to-buffer-same-window (marker-buffer marker)))
  1394. (goto-char marker)
  1395. (org-reveal t)
  1396. (org-show-entry)
  1397. (recenter)))
  1398. (if link
  1399. (setq message (format "Did not find link '%s'" link))
  1400. (setq message (format "Did not find headline '%s'." ref))))
  1401. message))
  1402. (defun org-index--do-occur ()
  1403. "Perform command occur."
  1404. (let ((word "") ; last word to search for growing and shrinking on keystrokes
  1405. (prompt "Search for: ")
  1406. (lines-wanted (window-body-height))
  1407. (lines-found 0) ; number of lines found
  1408. words ; list words that should match
  1409. occur-buffer
  1410. begin ; position of first line
  1411. narrow ; start of narrowed buffer
  1412. help-text ; cons with help text short and long
  1413. key-help ; for keys with special function
  1414. search-text ; description of text to search for
  1415. done ; true, if loop is done
  1416. in-c-backspace ; true, while processing C-backspace
  1417. show-headings ; true, if headings should be shown
  1418. help-overlay ; Overlay with help text
  1419. last-point ; Last position before end of search
  1420. key ; input from user
  1421. key-sequence) ; as a sequence
  1422. ;; make and show buffer
  1423. (if (get-buffer org-index--occur-buffer-name)
  1424. (kill-buffer org-index--occur-buffer-name))
  1425. (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
  1426. (pop-to-buffer-same-window occur-buffer)
  1427. ;; avoid modifying direct buffer
  1428. (setq buffer-read-only t)
  1429. (toggle-truncate-lines 1)
  1430. (setq font-lock-keywords-case-fold-search t)
  1431. (setq case-fold-search t)
  1432. ;; reset stack and overlays
  1433. (setq org-index--occur-stack nil)
  1434. (setq org-index--occur-tail-overlay nil)
  1435. ;; narrow to table rows and one line before
  1436. (goto-char (marker-position org-index--below-hline))
  1437. (forward-line 0)
  1438. (setq begin (point))
  1439. (forward-line -1)
  1440. (setq narrow (point))
  1441. (while (org-at-table-p)
  1442. (forward-line))
  1443. (narrow-to-region narrow (point))
  1444. (goto-char (point-min))
  1445. (forward-line)
  1446. ;; initialize help text
  1447. (setq help-text (cons
  1448. "Incremental occur; `?' toggles help and headlines.\n"
  1449. (concat
  1450. (org-index--wrap
  1451. (concat
  1452. "Normal keys add to search word; <space> or <comma> start additional word; <backspace> erases last char, <C-backspace> last word; <return> jumps to heading, <tab> jumps to heading in other window; all other keys end search.\n"))
  1453. org-index--headings)))
  1454. ;; insert overlay for help text and to cover unsearched lines
  1455. (setq help-overlay (make-overlay (point-min) begin))
  1456. (overlay-put help-overlay 'display (car help-text))
  1457. (overlay-put help-overlay 'face 'org-agenda-dimmed-todo-face)
  1458. (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
  1459. (overlay-put org-index--occur-tail-overlay 'invisible t)
  1460. (while (not done)
  1461. (if in-c-backspace
  1462. (setq key "<backspace>")
  1463. (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
  1464. ;; read key
  1465. (setq key-sequence
  1466. (vector (read-key
  1467. (format "%s%s%s"
  1468. prompt
  1469. search-text
  1470. (if (string= search-text "") "" " ")))))
  1471. (setq key (key-description key-sequence)))
  1472. (cond
  1473. ((string= key "<C-backspace>")
  1474. (setq in-c-backspace t))
  1475. ((member key (list "<backspace>" "DEL")) ; erase last char
  1476. (if (= (length word) 0)
  1477. ;; nothing more to delete from current word; try next
  1478. (progn
  1479. (setq word (car words))
  1480. (setq words (cdr words))
  1481. (setq in-c-backspace nil))
  1482. ;; unhighlight longer match
  1483. (unhighlight-regexp (regexp-quote word))
  1484. ;; some chars are left; shorten word
  1485. (setq word (substring word 0 -1))
  1486. (when (= (length word) 0) ; when nothing left, use next word from list
  1487. (setq word (car words))
  1488. (setq words (cdr words))
  1489. (setq in-c-backspace nil))
  1490. ;; free top list of overlays and remove list
  1491. (setq lines-found (or (org-index--unhide) lines-wanted))
  1492. (move-overlay org-index--occur-tail-overlay
  1493. (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  1494. (point-max))
  1495. (point-max))
  1496. ;; highlight shorter word
  1497. (unless (= (length word) 0)
  1498. (highlight-regexp (regexp-quote word) 'isearch))
  1499. ;; make sure, point is still visible
  1500. (goto-char begin)))
  1501. ((member key (list "SPC" ",")) ; space or comma: enter an additional search word
  1502. ;; push current word and clear, no need to change display
  1503. (setq words (cons word words))
  1504. (setq word ""))
  1505. ((string= key "?") ; question mark: toggle display of headlines and help
  1506. (setq help-text (cons (cdr help-text) (car help-text)))
  1507. (overlay-put help-overlay 'display (car help-text)))
  1508. ((and (= (length key) 1)
  1509. (aref printable-chars (elt key 0))) ; any printable char: add to current search word
  1510. ;; unhighlight short word
  1511. (unless (= (length word) 0)
  1512. (unhighlight-regexp (regexp-quote word)))
  1513. ;; add to word
  1514. (setq word (concat word key))
  1515. ;; make overlays to hide lines, that do not match longer word any more
  1516. (goto-char begin)
  1517. (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted))
  1518. (move-overlay org-index--occur-tail-overlay
  1519. (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  1520. (point-max))
  1521. (point-max))
  1522. (goto-char begin)
  1523. ;; highlight longer word
  1524. (highlight-regexp (regexp-quote word) 'isearch)
  1525. ;; make sure, point is on a visible line
  1526. (line-move -1 t)
  1527. (line-move 1 t))
  1528. ;; anything else terminates loop
  1529. (t (setq done t))))
  1530. ;; put back input event, that caused the loop to end
  1531. (unless (string= key "C-g")
  1532. (setq unread-command-events (listify-key-sequence key-sequence))
  1533. (message key))
  1534. ;; postprocessing
  1535. (setq last-point (point))
  1536. ;; For performance reasons do not show matching lines for rest of table. So not code here.
  1537. ;; make permanent copy
  1538. ;; copy visible lines
  1539. (let ((lines-collected 0)
  1540. keymap line all-lines)
  1541. (setq cursor-type t)
  1542. (goto-char begin)
  1543. ;; collect all visible lines
  1544. (while (and (not (eobp))
  1545. (< lines-collected lines-wanted))
  1546. ;; skip over invisible lines
  1547. (while (and (invisible-p (point))
  1548. (not (eobp)))
  1549. (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
  1550. (setq line (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
  1551. (unless (string= line "")
  1552. (incf lines-collected)
  1553. (setq all-lines (cons (concat line
  1554. "\n")
  1555. all-lines)))
  1556. (forward-line 1))
  1557. (kill-buffer org-index--occur-buffer-name) ; cannot keep this buffer; might become stale soon
  1558. ;; create new buffer
  1559. (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
  1560. (pop-to-buffer-same-window occur-buffer)
  1561. (insert "\n")
  1562. ;; prepare help text
  1563. (setq org-index--occur-help-overlay (make-overlay (point-min) (point-max)))
  1564. (setq org-index--occur-help-text
  1565. (cons
  1566. (org-index--wrap
  1567. (concat "Search is done; `?' toggles help and headlines.\n"))
  1568. (concat
  1569. (org-index--wrap (format (concat "Search is done. "
  1570. (if (< lines-collected lines-wanted)
  1571. " Showing all %d matches for "
  1572. " Showing one window of matches for ")
  1573. "\"" search-text
  1574. "\". <return> jumps to heading, <tab> jumps to heading in other window, subcommand \"enter\" to matching line in index.\n" )
  1575. (length all-lines)))
  1576. org-index--headings)))
  1577. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
  1578. (overlay-put org-index--occur-help-overlay 'face 'org-agenda-dimmed-todo-face)
  1579. ;; insert into new buffer
  1580. (save-excursion
  1581. (apply 'insert (reverse all-lines))
  1582. (if (= lines-collected lines-wanted)
  1583. (insert "\n(more lines omitted)\n")))
  1584. (org-mode)
  1585. (setq truncate-lines t)
  1586. (font-lock-fontify-buffer)
  1587. ;; highlight words
  1588. (setq case-fold-search t)
  1589. (setq font-lock-keywords-case-fold-search t)
  1590. (mapc (lambda (w) (unless (or (not w) (string= w "")) (highlight-regexp (regexp-quote w) 'isearch)))
  1591. (cons word words))
  1592. (setq buffer-read-only t)
  1593. ;; install keyboard-shortcuts
  1594. (setq keymap (make-sparse-keymap))
  1595. (set-keymap-parent keymap org-mode-map)
  1596. (mapc (lambda (x) (define-key keymap (kbd x)
  1597. (lambda () (interactive)
  1598. (message (org-index--occur-to-head)))))
  1599. (list "<return>" "RET"))
  1600. (define-key keymap (kbd "<tab>")
  1601. (lambda () (interactive)
  1602. (message (org-index--occur-to-head t))))
  1603. (define-key keymap (kbd "?")
  1604. (lambda () (interactive)
  1605. (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
  1606. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
  1607. (use-local-map keymap))))
  1608. (defun org-index--wrap (text)
  1609. "Wrap TEXT at fill column."
  1610. (with-temp-buffer
  1611. (insert text)
  1612. (fill-region (point-min) (point-max) nil t)
  1613. (buffer-string)))
  1614. (defun org-index--occur-to-head (&optional other)
  1615. "Helper for `org-index--occur', find heading with ref or link; if OTHER, in other window."
  1616. (org-index--do-head (org-index--get-field 'ref)
  1617. (org-index--get-field 'link)
  1618. other))
  1619. (defun org-index--hide-with-overlays (words lines-wanted)
  1620. "Hide text that is currently visible and does not match WORDS by creating overlays; leave LINES-WANTED lines visible."
  1621. (let ((symbol (intern (format "org-index-%d" (length org-index--occur-stack))))
  1622. (lines-found 0)
  1623. (end-of-visible (point))
  1624. overlay overlays start matched)
  1625. ;; main loop
  1626. (while (and (not (eobp))
  1627. (< lines-found lines-wanted))
  1628. ;; skip invisible lines
  1629. (while (and (not (eobp))
  1630. (and
  1631. (invisible-p (point))
  1632. (< (point) (overlay-start org-index--occur-tail-overlay))))
  1633. (goto-char (overlay-end (car (overlays-at (point))))))
  1634. ;; find stretch of lines, that are currently visible but should be invisible now
  1635. (setq matched nil)
  1636. (setq start (point))
  1637. (while (and (not (eobp))
  1638. (not
  1639. (and
  1640. (invisible-p (point))
  1641. (< (point) (overlay-start org-index--occur-tail-overlay))))
  1642. (not (and (org-index--test-words words)
  1643. (setq matched t)))) ; for its side effect
  1644. (forward-line 1))
  1645. ;; create overlay to hide this stretch
  1646. (when (< start (point)) ; avoid creating an empty overlay
  1647. (setq overlay (make-overlay start (point)))
  1648. (overlay-put overlay 'invisible symbol)
  1649. (setq overlays (cons overlay overlays)))
  1650. ;; skip and count line, that matched
  1651. (when matched
  1652. (forward-line 1)
  1653. (setq end-of-visible (point))
  1654. (incf lines-found)))
  1655. ;; put new list on top of stack
  1656. (setq org-index--occur-stack
  1657. (cons (list (cons :symbol symbol)
  1658. (cons :overlays overlays)
  1659. (cons :end-of-visible end-of-visible)
  1660. (cons :lines lines-found))
  1661. org-index--occur-stack))
  1662. ;; make lines invisible
  1663. (add-to-invisibility-spec symbol)
  1664. lines-found))
  1665. (defun org-index--unhide ()
  1666. "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
  1667. (when org-index--occur-stack
  1668. ;; make text visible again
  1669. (remove-from-invisibility-spec (cdr (assoc :symbol (car org-index--occur-stack))))
  1670. ;; delete overlays
  1671. (mapc (lambda (y)
  1672. (delete-overlay y))
  1673. (cdr (assoc :overlays (car org-index--occur-stack))))
  1674. ;; remove from stack
  1675. (setq org-index--occur-stack (cdr org-index--occur-stack))
  1676. ;; return number of lines, that are now visible
  1677. (if org-index--occur-stack (cdr (assoc :lines (car org-index--occur-stack))))))
  1678. (defun org-index--test-words (words)
  1679. "Test current line for match against WORDS."
  1680. (let (line)
  1681. (setq line (downcase (buffer-substring (line-beginning-position) (line-beginning-position 2))))
  1682. (catch 'not-found
  1683. (dolist (w words)
  1684. (or (search w line)
  1685. (throw 'not-found nil)))
  1686. t)))
  1687. (defun org-index--create-new-line (create-ref)
  1688. "Do the common work for `org-index-new-line' and `org-index'. CREATE-REF asks for new reference."
  1689. (let (new)
  1690. (when create-ref
  1691. ;; construct new reference
  1692. (unless new
  1693. (setq new (format "%s%d%s" org-index--head (1+ org-index--maxref) org-index--tail)))
  1694. ;; remember for org-mark-ring-goto
  1695. (setq org-index--text-to-yank new))
  1696. ;; insert ref or link as very first row
  1697. (goto-char org-index--below-hline)
  1698. (org-table-insert-row)
  1699. ;; insert some of the standard values
  1700. (org-table-goto-column (org-index--column-num 'created))
  1701. (org-insert-time-stamp nil nil t)
  1702. (org-table-goto-column (org-index--column-num 'count))
  1703. (insert "1")
  1704. new))
  1705. (defadvice org-mark-ring-goto (after org-index--advice-text-to-yank activate)
  1706. "Make text from `org-index' available for yank."
  1707. (when org-index--text-to-yank
  1708. (kill-new org-index--text-to-yank)
  1709. (message (format "Ready to yank '%s'" org-index--text-to-yank))
  1710. (setq org-index--text-to-yank nil)))
  1711. (provide 'org-index)
  1712. ;; Local Variables:
  1713. ;; fill-column: 75
  1714. ;; comment-column: 50
  1715. ;; lexical-binding: t
  1716. ;; End:
  1717. ;;; org-index.el ends here