1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497 |
- (require 'org-table)
- (require 'cl)
- (defcustom org-index-id nil
- "Id of the Org-mode node, which contains the index table."
- :group 'org
- :group 'org-index)
- (defvar org-index-version "4.2.1" "Version of `org-index', format is major.minor.bugfix, where \"major\" is a change in index-table and \"minor\" are new features.")
- (defvar org-index--maxref nil "Maximum number from reference table (e.g. '153').")
- (defvar org-index--head nil "Any header before number (e.g. 'R').")
- (defvar org-index--tail nil "Tail after number (e.g. '}' or ')'.")
- (defvar org-index--numcols nil "Number of columns in index table.")
- (defvar org-index--ref-regex nil "Regular expression to match a reference.")
- (defvar org-index--ref-format nil "Format, that can print a reference.")
- (defvar org-index--columns nil "Columns of index-table.")
- (defvar org-index--special-columns nil "Columns with flags, that may appear only once.")
- (defvar org-index--flagged-columns nil "Columns with flags, that may appear multiple times.")
- (defvar org-index--buffer nil "Buffer of index table.")
- (defvar org-index--point nil "Position at start of headline of index table.")
- (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
- (defvar org-index--headings nil "Headlines of index-table as a string.")
- (defvar org-index--headings-visible nil "Visible part of headlines of index-table as a string.")
- (defvar org-index--keymap nil "Keymap for shortcuts for some commands of `org-index'. Filled and activated by `org-index-default-keybings'.")
- (defvar org-index--last-ref nil "Last reference created or visited.")
- (defvar org-index--category-before nil "Category of node before.")
- (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
- (defvar org-index--below-cursor nil "Word below cursor.")
- (defvar org-index--within-node nil "True, if we are within node of the index table.")
- (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
- (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
- (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
- (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
- (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
- (defvar org-index--last-sort nil "Last column, the index has been sorted after.")
- (defvar org-index--sort-timer nil "Timer to sort index in correct order.")
- (defvar org-index--aligned nil "Remember for this Emacs session, if table has been aligned at least once.")
- (defconst org-index--commands '(occur add delete head ping enter ref help example sort multi-occur highlight maintain) "List of commands available.")
- (defconst org-index--required-flags '(sort) "Flags that are required.")
- (defconst org-index--single-flags '(sort point-on-add yank-after-add shift-ref-and-date-on-add) "Flags, that may only appear once; these can appear as special-columns.")
- (defconst org-index--multiple-flags '(edit-on-add) "Flags, that might appear multiple times.")
- (defconst org-index--all-flags (append org-index--single-flags org-index--multiple-flags) "All flags.")
- (defconst org-index--required-headings '(ref id created last-accessed count) "All required headings.")
- (defconst org-index--valid-headings (append org-index--required-headings '(keywords category level)) "All valid headings.")
- (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
- (defconst org-index--sort-idle-delay 300 "Delay in seconds after which buffer will sorted.")
- (defvar org-index-default-keybindings-list '(("a" . 'add) ("i " . nil) ("o" . 'occur) ("a" . 'add) ("d" . 'delete) ("h" . 'head) ("e" . 'enter) ("p." . 'ping) ("r" . 'ref) ("?" . 'help)) "One-letter short cuts for selected subcommands of `org-index', put in effect by `org-index-default-keybindings'")
- (defconst org-index--sample-flags
- "
- - columns-and-flags :: associate columns of index table with flags. Do not remove.
- - ref
- - yank-after-add
- - category
- - edit-on-add
- - keywords
- - edit-on-add
- - point-on-add
- - count
- - sort
- - last-accessed
- - created
- - id
- - all-columns-explained :: All columns of the index table and their meaning.
- - ref :: The reference number; will be generated automatically.
- - id :: id of the node, that this line represents
- - created :: When has this entry been created ?
- - last-accessed :: When has this entry been accessed last ?
- - count :: How many times has this entry been picked ?
- - keywords :: Optional column, suggested to keep a list of keywords,
- which may match your input during occur. While adding a line to your index,
- this column will be filled with the nodes heading.
- - category :: (optional) column to store the category of newly added nodes.
- - level :: Nesting level of node
- - Any name starting with a dot (`.') :: No predefined meaning,
- depends on its flags.
- - all-flags-explained :: All flags, that can be associated with columns.
- - sort :: Sort whole table according to this column.
- - yank-after-add :: This column will be yanked after picking this line during
- occur.
- - edit-on-add :: This field will be presented for editing, when adding
- a new line to your index.
- - point-on-add :: Point will land here, when adding a new line, e.g. with
- command ref.
- - shift-ref-and-date-on-add :: Remove leading reference and timestamp on add."
- "A sample string of flags.")
- (defmacro org-index--on (column value &rest body)
- "Execute the forms in BODY with point on index line whose COLUMN is VALUE.
- The value returned is the value of the last form in BODY or nil,
- if VALUE cannot be found."
- (declare (indent 2) (debug t))
- (let ((pointvar (make-symbol "point"))
- (foundvar (make-symbol "found"))
- (retvar (make-symbol "ret")))
- `(save-current-buffer
- (set-buffer org-index--buffer)
- (setq ,pointvar (point))
- (setq ,foundvar nil)
- (setq ,retvar nil)
- (setq ,foundvar (org-index--go ,column ,value))
- (when ,foundvar
- (setq ,retvar (progn ,@body)))
-
- (goto-char ,pointvar)
-
- ,retvar)))
- (defun org-index (&optional command search-ref arg)
- "Fast search for selected org headings and things outside of org.
- This package creates and updates an index table of headings or
- keywords, references and ids, where each line points to a heading
- within org or references something outside. This table is sorted by
- usage count, so that frequently used lines appear among the first
- search results.
- References are essentially small numbers (e.g. 'R237' or '--455--'), as
- created by this package; they are well suited to be used outside of
- org, e.g. in folder names, issue trackers or on printed documents.
- On first invocation `org-index' will guide you to create a dedicated node
- for its index table and its configuration flags.
- For basic usage, subcommands 'add' and 'occur' are most important.
- This is version 4.2.1 of org-index.el.
- \\<org-mode-map>
- The function `org-index' operates on a dedicated table, the index
- table, which lives within its own Org-mode node. The table and
- its containing node will be created, when you first invoke
- `org-index'. The node also contains a commented list, describing
- the columns of the index table and their associated flags. The
- node is found through its id, which is stored within the variable
- `org-index-id'.
- The function `org-index' is the only interactive function of this
- package and its main entry point; it will present you with a list
- of subcommands to choose from:
- occur: Incremental search, that shows matching lines from the
- index table. It is updated after every keystroke. You may
- enter a list of words seperated by space or comma (`,'), to
- select lines that contain all of the given words.
- add: Add the current node to your index, so that it can be
- found through the subcommand \"occur\". Update index,
- if node has already been present.
- delete: Delete the current node from your index.
- head: Ask for a reference number and search for this heading.
- enter: Enter index table and maybe go to a specific reference;
- use `org-mark-ring-goto' (\\[org-mark-ring-goto]) to go back.
- ping: Echo line from index table for current node or first of
- its ancestor from index.
- ref: Create a new reference.
- help: Show this text.
- example: Create a temporary index, that will not be saved, but
- may serve as an example.
- sort: Sort lines in index, in region or buffer by contained
- reference, or sort index by count, reference or last access.
- multi-occur: Apply Emacs standard `multi-occur' operation on all
- `org-mode' buffers to search for the given reference.
- highlight: Highlight or unhiglight references in active region or buffer.
- Call with prefix argument (`C-u') to remove highlights.
- maintain: Offers some choices to check, update or fix your index.
- If you invoke `org-index' for the first time, an assistant will be
- invoked, that helps you to create your own, commented index.
- Use `org-index-default-keybindings' to establish convenient
- keyboard shortcuts.
- See the commented list of flags within your index node for ways to
- modify the behaviour of org-index.
- A numeric prefix argument is used as a reference number for
- commands, that need one (e.g. 'head').
- Optional arguments for use from elisp: COMMAND is a symbol naming
- the command to execute. SEARCH-REF specifies a reference to
- search for, if needed. ARG allows passing in a prefix argument
- as in interactive calls."
- (interactive "i\ni\nP")
- (let (search-id
- sort-what
- kill-new-text
- message-text)
-
-
-
-
- (org-index--verify-id)
-
- (org-index--parse-table)
-
- (org-index--retrieve-context)
-
-
-
-
- (unless org-index--last-sort (setq org-index--last-sort (org-index--special-column 'sort)))
-
- (unless org-index--sort-timer
- (setq org-index--sort-timer
- (run-with-idle-timer org-index--sort-idle-delay t 'org-index--sort-silent)))
-
-
-
-
- (if command
- (unless (memq command org-index--commands)
- (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
- command (mapconcat 'symbol-name org-index--commands ",")))
- (setq command (intern (org-completing-read
- "Please choose: "
- (mapcar 'symbol-name org-index--commands)))))
-
-
-
-
-
- (if (and (not search-ref)
- (numberp arg))
- (setq search-ref (format "%s%d%s" org-index--head arg org-index--tail)))
-
-
- (when (memq command '(enter head multi-occur))
-
- (unless search-ref
- (if org-index--within-node
- (if (org-at-table-p)
- (setq search-ref (org-index--get-or-set-field 'ref)))
-
- (if (and org-index--below-cursor
- (string-match (concat "\\(" org-index--ref-regex "\\)")
- org-index--below-cursor))
- (setq search-ref (match-string 1 org-index--below-cursor)))))
-
-
- (unless search-ref
- (if (eq command 'enter)
- (let ((r (org-index--read-search-for-enter)))
- (setq search-ref (car r))
- (setq search-id (cdr r)))
- (setq search-ref (read-from-minibuffer "Search reference number: "))))
-
- (when search-ref
- (setq search-ref (org-trim search-ref))
- (if (string-match "^[0-9]+$" search-ref)
- (setq search-ref (concat org-index--head search-ref org-index--tail)))
- (if (string= search-ref "") (setq search-ref nil)))
- (if (and (not search-ref)
- (not (eq command 'enter)))
- (error "Command %s needs a reference number" command)))
-
-
-
-
-
- (when (eq command 'sort)
- (setq sort-what (intern (org-completing-read "You may sort:\n - index : your index table by various columns\n - region : the active region by contained reference\n - buffer : the whole current buffer\nPlease choose what to sort: " (list "index" "region" "buffer") nil t))))
-
-
-
-
-
-
- (when (and (memq command '(occur head enter ref example sort maintain))
- (not (string= (buffer-name) org-index--occur-buffer-name)))
- (org-mark-ring-push))
-
- (when (or (memq command '(enter ref maintain))
- (and (eq command 'sort)
- (eq sort-what 'index)))
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--point)
- (org-index--unfold-buffer))
-
-
-
- (cond
- ((eq command 'help)
-
- (describe-function 'org-index))
- ((eq command 'multi-occur)
-
- (let (buff org-buffers)
- (dolist (buff (buffer-list))
- (set-buffer buff)
- (if (string= major-mode "org-mode")
- (setq org-buffers (cons buff org-buffers))))
-
- (multi-occur org-buffers (org-index--make-guarded-search search-ref))
-
- (if (get-buffer "*Occur*")
- (progn
- (setq message-text (format "multi-occur for '%s'" search-ref))
- (other-window 1)
- (toggle-truncate-lines 1))
- (setq message-text (format "Did not find '%s'" search-ref)))))
- ((eq command 'add)
- (let ((r (org-index--do-add-or-update)))
- (setq message-text (car r))
- (setq kill-new-text (cdr r))))
- ((eq command 'delete)
- (setq message-text (org-index--do-delete)))
- ((eq command 'head)
- (if (and org-index--within-node
- (org-at-table-p))
- (setq search-id (org-index--get-or-set-field 'id)))
- (setq search-id (or search-id (org-index--id-from-ref search-ref)))
- (setq message-text
- (if search-id
- (org-index--do-head search-ref search-id)
- (message "Current line has no id."))))
- ((eq command 'enter)
- (goto-char org-index--below-hline)
- (setq message-text
- (if search-ref
- (if (org-index--go 'ref search-ref)
- (progn
- (org-index--update-current-line)
- (org-table-goto-column (org-index--column-num 'ref))
- (format "Found index line '%s'" search-ref))
- (format "Did not find index line with reference '%s'" search-ref))
- (if search-id
- (if (org-index--go 'id search-id)
- (progn
- (org-index--update-current-line)
- (org-table-goto-column (org-index--column-num 'ref))
- (format "Found index line '%s'" (org-index--get-or-set-field 'ref)))
- (format "Did not find index line with id '%s'" search-id))
-
- (setq message-text "At index table"))))
- (recenter))
- ((eq command 'ping)
- (let ((moved-up 0) id info reached-top)
- (unless (string= major-mode "org-mode") (error "No node at point"))
-
- (setq id (if search-ref
- (org-index--id-from-ref search-ref)
- (org-id-get)))
-
- (save-excursion
- (outline-back-to-heading)
- (while (not (or info
- reached-top))
- (if id
- (setq info (org-index--on 'id id
- (mapcar (lambda (x) (org-index--get-or-set-field x))
- (list 'ref 'count 'created 'last-accessed 'category 'keywords 'ref)))))
- (setq reached-top (= (org-outline-level) 1))
- (unless (or info
- reached-top)
- (outline-up-heading 1 t)
- (incf moved-up))
- (setq id (org-id-get))))
-
- (if info
- (progn
- (setq message-text
- (apply 'format
- (append (list "'%s'%shas been accessed %s times between %s and %s; category is '%s', keywords are '%s'"
- (pop info)
- (if (> moved-up 0) (format " (parent node, %d level up) " moved-up) " "))
- info)))
- (setq kill-new-text (car (last info))))
- (setq message-text "Neither this node nor any of its parents is part of index"))))
- ((eq command 'occur)
- (set-buffer org-index--buffer)
- (org-index--do-occur))
- ((eq command 'ref)
- (let (new)
-
- (setq new (org-index--create-new-line))
-
- (org-table-goto-column (org-index--column-num 'ref))
- (insert new)
- (setq org-index--last-ref new)
-
- (if (org-index--special-column 'point-on-add)
- (org-table-goto-column (org-index--column-num (org-index--special-column 'point-on-add)))
- (unless (catch 'empty
- (dotimes (col org-index--numcols)
- (org-table-goto-column (+ col 1))
- (if (string= (org-trim (org-table-get-field)) "")
- (throw 'empty t))))
-
- (org-table-goto-column 1)))
- (if org-index--active-region (setq kill-new-text org-index--active-region))
- (setq message-text (format "Adding a new row with ref '%s'" new))))
- ((eq command 'sort)
- (let ((columns (list "ref" "count" "created" "last-accessed" "id"))
- sort groups-and-counts)
- (cond
- ((eq sort-what 'index)
- (setq sort
- (intern
- (org-icompleting-read
- "Please choose column to sort index table: "
- (append (copy-list columns) (list "group-by"))
- nil t nil nil (symbol-name (org-index--special-column 'sort)))))
- (when (eq sort 'group-by)
- (setq sort
- (intern
- (org-icompleting-read
- "Please choose column to group index table by: "
- columns
- nil t nil nil (symbol-name (org-index--special-column 'sort)))))
- (setq groups-and-counts (org-index--collect-sort-groups sort)))
- (org-index--do-sort-index sort (first groups-and-counts))
- (org-table-goto-column (org-index--column-num sort))
-
- (with-current-buffer org-index--buffer
- (add-hook 'before-save-hook 'org-index--sort-silent t))
-
- (setq message-text
- (format
- (concat "Your index has been sorted temporarily by %s and will be sorted again by %s after %d seconds of idle time"
- (if groups-and-counts
- "; %d groups with equal %s and a total of %d lines have been found"
- ""))
- (symbol-name sort)
- (org-index--special-column 'sort)
- org-index--sort-idle-delay
- (second groups-and-counts)
- (symbol-name sort)
- (third groups-and-counts))))
- ((memq sort-what '(region buffer))
- (org-index--do-sort-lines sort-what)
- (setq message-text (format "Sorted %s by contained references" sort-what))))))
- ((eq command 'highlight)
- (let ((where "buffer"))
- (save-excursion
- (save-restriction
- (when (and transient-mark-mode
- mark-active)
- (narrow-to-region (region-beginning) (region-end))
- (setq where "region"))
- (if arg
- (progn
- (unhighlight-regexp org-index--ref-regex)
- (setq message-text (format "Removed highlights for references in %s" where)))
- (highlight-regexp org-index--ref-regex 'isearch)
- (setq message-text (format "Highlighted references in %s" where)))))))
- ((eq command 'maintain)
- (setq message-text (org-index--do-maintain)))
-
- ((eq command 'example)
- (if (y-or-n-p "This assistant will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
- (org-index--create-index t)))
- (t (error "Unknown subcommand '%s'" command)))
-
- (if kill-new-text (setq kill-new-text
- (substring-no-properties kill-new-text)))
- (if (string= kill-new-text "") (setq kill-new-text nil))
- (let ((m (concat
- message-text
- (if (and message-text kill-new-text)
- " and r"
- (if kill-new-text "R" ""))
- (if kill-new-text (format "eady to yank '%s'." kill-new-text) (if message-text "." "")))))
- (unless (string= m "")
- (message m)
- (setq org-index--message-text m)))
- (if kill-new-text (kill-new kill-new-text))))
- (defun org-index-default-keybindings (&optional prefix)
- "Set default keybindings for `org-index'.
- Invoke subcommands of org index with a single key
- sequence. Establish the common prefix key 'C-c i' which should be
- followed by the first letter of a subcommand.
- The ist of letters and subcommands is specified in within
- `org-index-default-keybindings-list'.
-
- See `org-index' for a description of all subcommands.
- Optional argument PREFIX specifies common prefix, defaults to 'C-c i'"
- (interactive)
- (define-prefix-command 'org-index--keymap)
-
- (global-set-key (kbd (or prefix "C-c i")) 'org-index--keymap)
-
- (mapcar
- (lambda (x)
-
- (mapcar (lambda (c)
- (define-key org-index--keymap (kbd (char-to-string c))
- `(lambda (arg) (interactive "P")
- (message nil)
- (org-index ,(cdr x) nil arg))))
- (car x)))
- org-index-default-keybindings-list))
- (defun org-index-new-line (&rest keys-values)
- "Create a new line within the index table, returning its reference.
- The function takes a varying number of argument pairs; each pair
- is a symbol for an existing column heading followed by its value.
- The return value is the new reference.
- Example:
- (message \"Created reference %s\"
- (org-index-new-line 'keywords \"foo bar\" 'category \"baz\"))
- Optional argument KEYS-VALUES specifies content of new line."
- (org-index--verify-id)
- (org-index--parse-table)
- (car (apply 'org-index--do-new-line keys-values)))
- (defun org-index--do-new-line (&rest keys-values)
- "Do the work for `org-index-new-line'.
- Optional argument KEYS-VALUES specifies content of new line."
- (save-excursion
- (org-index--retrieve-context)
- (with-current-buffer org-index--buffer
- (goto-char org-index--point)
-
- (let ((kvs keys-values)
- k v)
- (while kvs
- (setq k (car kvs))
- (setq v (cadr kvs))
- (if (eq k 'ref)
- (unless (memq v '(t nil))
- (error "Column 'ref' accepts only \"t\" or \"nil\""))
- (if (or (not (symbolp k))
- (and (symbolp v) (not (eq v t)) (not (eq v nil))))
- (error "Arguments must be alternation of key and value")))
- (unless (org-index--column-num k)
- (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
- (setq kvs (cddr kvs))))
- (let (ref yank)
-
- (setq ref (org-index--create-new-line))
- (plist-put keys-values 'ref ref)
-
- (let ((kvs keys-values)
- k v n)
- (while kvs
- (setq k (car kvs))
- (setq v (cadr kvs))
- (org-table-goto-column (org-index--column-num k))
- (insert (org-trim v))
- (setq kvs (cddr kvs))))
-
- (org-index--promote-current-line)
- (org-index--align-and-fontify-current-line)
-
-
- (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add)))
- (cons ref yank)))))
- (defun org-index-get-line (column value)
- "Retrieve an existing line within the index table by ref or id.
- Return its contents as a property list.
- The function `plist-get' may be used to retrieve specific elements
- from the result.
- Example:
- (plist-get (org-index-get-line 'ref \"R12\") 'count)
- retrieves the value of the count-column for reference number 12.
- Argument COLUMN is a symbol, either ref or id,
- argument VALUE specifies the value to search for."
-
- (unless (memq column '(ref id))
- (error "Argument column can only be 'ref' or 'id'"))
- (unless value
- (error "Need a value to search for"))
-
- (org-index--verify-id)
- (org-index--parse-table)
- (org-index--get-line column value))
- (defun org-index--get-line (column value)
- "Find a line by ID, return its contents.
- Argument COLUMN and VALUE specify line to get."
- (let (content)
- (org-index--on
- column value
- (mapc (lambda (x)
- (if (and (numberp (cdr x))
- (> (cdr x) 0))
- (setq content (cons (car x) (cons (or (org-index--get-or-set-field (car x)) "") content)))))
- (reverse org-index--columns)))
- content))
- (defun org-index--delete-line (id)
- "Delete a line specified by ID."
- (let (content)
- (org-index--on
- 'id id
- (let ((start (line-beginning-position)))
- (beginning-of-line)
- (forward-line)
- (delete-region start (point))
- t))))
- (defun org-index--ref-from-id (id)
- "Get reference from line ID."
- (org-index--on 'id id (org-index--get-or-set-field 'ref)))
- (defun org-index--id-from-ref (ref)
- "Get id from line REF."
- (org-index--on 'ref ref (org-index--get-or-set-field 'id)))
- (defun org-index--read-search-for-enter ()
- "Special input routine for command enter."
-
- (let (char prompt search-ref search-id)
-
-
- (setq prompt "Please specify, where to go in index (0-9.,space,backspace,return or ? for help): ")
-
-
- (while (not (memq char (append (number-sequence ?0 ?9) (list ?\d ?\b ?\r ?\j ?\s ?.))))
- (setq char (read-char prompt))
- (setq prompt "Go to index table and specific position. Digits specify a reference number to got to, <space> goes to top of index, <backspace> or <delete> to last line created and <return> or `.' to index line of current node. Please choose: "))
-
- (if (memq char (number-sequence ?0 ?9))
-
- (setq search-ref (read-from-minibuffer "Search reference number: " (char-to-string char))))
-
- (if (memq char '(?\r ?\n ?.)) (setq search-id (org-id-get)))
- (if (memq char '(?\d ?\b)) (setq search-ref (number-to-string org-index--maxref)))
- (cons search-ref search-id)))
- (defun org-index--verify-id ()
- "Check, that we have a valid id."
-
- (unless org-index-id
- (let ((answer (org-completing-read "Cannot find an index (org-index-id is not set). You may:\n - read-help : to learn more about org-index\n - create-index : invoke an assistant to create an initial index\nPlease choose: " (list "read-help" "create-index") nil t nil nil "read-help")))
- (if (string= "create-index" answer)
- (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet.")
- (describe-function 'org-index))))
-
- (let (marker)
- (setq marker (org-id-find org-index-id 'marker))
- (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
-
- (setq marker (org-id-find org-index-id 'marker))
- (unless marker (error "Could not create node"))
- (setq org-index--buffer (marker-buffer marker)
- org-index--point (marker-position marker))
- (move-marker marker nil)))
- (defun org-index--retrieve-context ()
- "Collect context information before starting with command."
-
- (setq org-index--active-region
- (if (and transient-mark-mode mark-active)
- (buffer-substring (region-beginning) (region-end))
- nil))
- (setq org-index--below-cursor (thing-at-point 'symbol))
-
- (setq org-index--category-before
- (save-excursion
- (beginning-of-line)
- (org-get-category (point) t)))
-
- (setq org-index--within-node (string= (org-id-get) org-index-id)))
- (defun org-index--parse-table ()
- "Parse content of index table."
- (let (ref-field
- id-field
- initial-point
- end-of-headings
- start-of-headings)
- (with-current-buffer org-index--buffer
- (setq org-index--maxref 0)
- (setq initial-point (point))
- (org-index--go-below-hline)
-
- (unless org-index--aligned
- (org-table-align)
- (let ((is-modified (buffer-modified-p))
- (below (point)))
- (while (org-at-table-p)
- (forward-line))
- (font-lock-fontify-region below (point))
- (org-index--go-below-hline)
- (setq org-index--aligned t)
- (set-buffer-modified-p is-modified)))
-
- (org-index--go-below-hline)
- (setq org-index--below-hline (point-marker))
- (beginning-of-line)
-
- (setq end-of-headings (point))
- (while (org-at-table-p) (forward-line -1))
- (forward-line)
- (setq start-of-headings (point))
- (setq org-index--headings-visible (substring-no-properties (org-index--copy-visible start-of-headings end-of-headings)))
- (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
-
- (org-table-goto-column 100)
- (setq org-index--numcols (- (org-table-current-column) 1))
-
- (while (org-at-table-p)
- (forward-line -1))
- (forward-line)
-
- (org-index--parse-headings)
-
- (goto-char org-index--point)
- (org-index--parse-flags)
-
- (goto-char org-index--below-hline)
- (while (and (org-at-table-p)
- (not (setq ref-field (org-index--get-or-set-field 'ref))))
- (forward-line))
-
- (unless ref-field
- (org-index--report-index-error "Reference column is empty"))
- (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
- (org-index--report-index-error
- "First reference in index table ('%s') does not contain a number" ref-field))
-
- (setq org-index--head (match-string 1 ref-field))
- (setq org-index--tail (match-string 3 ref-field))
- (setq org-index--ref-regex (concat (regexp-quote org-index--head)
- "\\([0-9]+\\)"
- (regexp-quote org-index--tail)))
- (setq org-index--ref-format (concat org-index--head "%d" org-index--tail))
-
- (let ((ref 0))
- (while (org-at-table-p)
- (setq ref-field (org-index--get-or-set-field 'ref))
- (setq id-field (org-index--get-or-set-field 'id))
- (when (and (not ref-field)
- (not id-field))
- (kill-whole-line)
- (message "Removing line from index-table with both ref and id empty"))
- (if ref-field
- (if (string-match org-index--ref-regex ref-field)
-
- (setq ref (string-to-number (match-string 1 ref-field)))
- (kill-whole-line)
- (message "Removing line from index-table whose ref does not contain a number")))
-
- (if (> ref org-index--maxref) (setq org-index--maxref ref))
- (forward-line 1)))
-
- (goto-char initial-point))))
- (defun org-index--do-maintain ()
- "Choose among and perform some tasks to maintain index."
- (let ((check-what) (max-mini-window-height 1.0) message-text)
- (setq check-what (intern (org-completing-read "These checks and fixes are available:\n - statistics : compute statistics about index table\n - check : check ids by visiting their nodes\n - duplicates : check index for duplicate rows (any column)\n - clean : remove obsolete property org-index-id\n - update : update content of index lines, with an id \nPlease choose: " (list "statistics" "check" "duplicates" "clean" "update") nil t nil nil "statistics")))
- (message nil)
-
- (cond
- ((eq check-what 'check)
- (setq message-text (or (org-index--check-ids)
- "No problems found")))
- ((eq check-what 'statistics)
- (setq message-text (org-index--do-statistics)))
- ((eq check-what 'duplicates)
- (setq message-text "Finding duplcates can be done by sorting your index appropriately: Choose 'group-by' and select a column; rows will then be sorted together, if they have the same value within the coosen column."))
- ((eq check-what 'clean)
- (let ((lines 0))
- (org-map-entries
- (lambda ()
- (when (org-entry-get (point) "org-index-ref")
- (incf lines)
- (org-entry-delete (point) "org-index-ref")))
- nil 'agenda)
- (setq message-text (format "Removed property 'org-index-ref' from %d lines" lines))))
-
- ((eq check-what 'update)
- (if (y-or-n-p "Updating your index will overwrite certain columns with content from the associated heading and category. If unsure, you may try this for a single, already existing line of your index by doing `add' from within your index. Are you SURE to proceed for ALL INDEX LINES ? ")
- (setq message-text (org-index--update-all-lines))
- (setq message-text "Canceled."))))
- message-text))
- (defun org-index--do-sort-index (sort &optional groups)
- "Sort index table according to SORT, optinally with GROUPS."
- (let ((is-modified (buffer-modified-p))
- top
- bottom
- ref-field
- count-field)
- (unless buffer-read-only
- (message "Sorting table for %s..." (symbol-name sort))
- (undo-boundary)
- (let ((message-log-max nil))
-
- (goto-char org-index--below-hline)
- (forward-line 0)
- (setq top (point))
- (while (org-at-table-p) (forward-line))
-
- (while (progn
- (forward-line -1)
- (org-table-goto-column 1)
- (and
- (not (org-index--get-or-set-field 'ref))
- (not (org-index--get-or-set-field 'id))))
- (org-table-kill-row))
- (forward-line 1)
- (setq bottom (point))
-
-
- (save-restriction
- (narrow-to-region top bottom)
- (goto-char top)
- (sort-subr t
- 'forward-line
- 'end-of-line
- (lambda ()
- (concat
- (if groups
- (format "%06d-" (cdr (assoc (org-index--get-or-set-field sort) groups)))
- "")
- (org-index--get-sort-key sort t)))
- nil
- 'string<)
- (goto-char (point-min))
-
- (set-buffer-modified-p is-modified)))
- (setq org-index--last-sort sort))))
- (defun org-index--collect-sort-groups (sort)
- "Collect groups to SORT for."
- (let ((count-groups 0) (count-lines 0)
- groups key key-value)
-
- (org-index--on
- nil nil
- (while (org-at-table-p)
- (setq key (org-index--get-or-set-field sort))
- (setq key-value (assoc key groups))
- (if key-value
- (progn
- (incf (cdr key-value)))
- (setq groups (cons (cons key 1) groups)))
- (forward-line)))
- (mapc (lambda (x) (when (> (cdr x) 1)
- (incf count-groups)
- (incf count-lines (cdr x))))
- groups)
- (list groups count-groups count-lines)))
- (defun org-index--do-sort-lines (what)
- "Sort lines in WHAT according to contained reference."
- (save-restriction
- (cond
- ((eq what 'region)
- (if (region-active-p)
- (narrow-to-region (region-beginning) (region-end))
- (error "No active region, cannot sort")))
- ((eq what 'buffer)
- (unless (y-or-n-p "Sort whole current buffer ? ")
- (error "Canceled"))
- (narrow-to-region (point-min) (point-max))))
- (goto-char (point-min))
- (sort-subr nil 'forward-line 'end-of-line
- (lambda ()
- (if (looking-at (concat ".*"
- (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
- (string-to-number (match-string 1))
- 0)))))
- (defun org-index--go-below-hline ()
- "Move below hline in index-table."
- (let ((count 0)
- (errstring (format "index table within node %s" org-index-id)))
- (goto-char org-index--point)
-
- (while (not (org-at-heading-p)) (forward-line -1))
- (forward-line 1)
-
- (while (and (not (org-at-table-p))
- (not (org-at-heading-p))
- (not (eobp)))
- (forward-line))
-
- (unless (org-at-table-p)
- (org-index--create-missing-index "Cannot find %s." errstring))
-
- (while (and (not (org-at-table-hline-p))
- (org-at-table-p))
- (forward-line))
- (forward-line)
-
- (unless (org-at-table-p)
- (org-index--report-index-error "Cannot find a hline within %s" errstring))
- (org-table-goto-column 1)))
- (defun org-index--parse-headings ()
- "Parse headings of index table."
- (let (field
- field-symbol
- found)
- (setq org-index--columns nil)
-
- (dotimes (col org-index--numcols)
- (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
- (if (string= field "")
- (error "Heading of column cannot be empty"))
- (if (and (not (string= (substring field 0 1) "."))
- (not (member (intern field) org-index--valid-headings)))
- (if (string= field "link")
-
- (progn
-
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--below-hline)
- (org-reveal t)
-
- (while (org-at-table-p)
- (forward-line -1))
- (forward-line)
- (org-table-goto-column (+ 1 col))
- (error "Column 'link' should be named 'id' with recent versions of org-index,\nplease adjust your table (cursor is already positioned right)"))
- (error "Column name '%s' is not a valid heading (custom headings may start with a dot, e.g. '.foo')" field)))
- (setq field-symbol (intern field))
-
- (if (assoc field-symbol org-index--columns)
- (org-index--report-index-error
- "'%s' appears two times as column heading" (downcase field))
-
- (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
- (setq org-index--columns (reverse org-index--columns))
-
- (mapc (lambda (head)
- (unless (cdr (assoc head org-index--columns))
- (org-index--report-index-error "No column has heading '%s'" head)))
- org-index--required-headings))
- (defun org-index--parse-flags ()
- "Parse list of flags in index table."
- (let (parent parent-is-comment child)
-
- (setq org-index--special-columns nil)
- (setq org-index--flagged-columns nil)
- (org-index--goto-list "columns-and-flags" t)
- (forward-line 1)
-
- (while (and (setq parent (org-index--parse-list-item))
- parent
- (> (cdr (assoc :indent parent)) 0))
- (setq parent-is-comment (member (cdr (assoc :text parent)) '("all-columns-explained" "all-flags-explained")))
-
- (unless (or parent-is-comment
- (assoc (cdr (assoc :sym parent)) org-index--columns))
- (when (string= "link" (cdr (assoc :text parent)))
- (pop-to-buffer-same-window org-index--buffer)
- (org-reveal t)
- (error "Flag 'link' should be named 'id' with recent versions of org-index,\nplease adjust this flag (cursor is already positioned right)"))
- (org-index--report-index-error "'%s' appears within flags, but not as a index column. " (cdr (assoc :text parent))))
-
- (while (and (forward-line 1)
- (setq child (org-index--parse-list-item))
- child
- (> (cdr (assoc :indent child))
- (cdr (assoc :indent parent))))
- (unless parent-is-comment
-
- (unless (memq (cdr (assoc :sym child)) org-index--all-flags)
- (org-index--report-index-error "'%s' is not a valid flag" (cdr (assoc :text child))))
-
- (if (memq (cdr (assoc :sym child)) org-index--single-flags)
-
- (if (assoc (cdr (assoc :sym child)) org-index--special-columns)
- (org-index--report-index-error
- "More than one column is marked with flag '%s'" (cdr (assoc :text child)))
-
- (setq org-index--special-columns (cons (cons (cdr (assoc :sym child)) (cdr (assoc :sym parent)))
- org-index--special-columns))))
-
- (let ((l (assoc (cdr (assoc :sym child)) org-index--flagged-columns)))
- (unless l
-
- (setq org-index--flagged-columns
- (cons (cons (cdr (assoc :sym child)) nil)
- org-index--flagged-columns))
- (setq l (car org-index--flagged-columns)))
-
- (setcdr l (cons (cdr (assoc :sym parent)) (cdr l)))))))
-
- (mapc (lambda (x)
- (unless (assoc x org-index--special-columns)
- (org-index--report-index-error "Required flag '%s' does not appear" (substring (symbol-name x) 1))))
- org-index--required-flags)))
- (defun org-index--goto-list (name &optional required non-top)
- "Goto list NAME (maybe NON-TOP Level) in index node, err if REQUIRED list is not present."
- (goto-char org-index--point)
-
- (while (not (org-at-heading-p)) (forward-line -1))
- (forward-line 1)
-
- (while (and (not (let ((item (org-index--parse-list-item)))
- (if item
- (and (or non-top (= (cdr (assoc :indent item)) 0))
- (string= (cdr (assoc :text item)) name))
- nil)))
- (not (org-at-table-p))
- (not (org-at-heading-p))
- (not (eobp)))
- (forward-line 1))
- (if (org-at-item-p)
- t
- (if required
- (org-index--report-index-error "Could not find required list '%s'" name)
- nil)))
- (defun org-index--parse-list-item ()
- "Parse a list item into an assoc array (indent, checkbox, text, value)."
-
- (if (looking-at org-list-full-item-re)
-
- (let (indent checkbox text value next-line)
- (setq indent
- (- (save-excursion (goto-char (match-beginning 1)) (current-column))
- (save-match-data (org-current-level))
- 1))
- (setq checkbox (match-string 3))
- (setq text (match-string 4))
- (set (if text 'value 'text) (buffer-substring (match-end 0) (line-end-position)))
-
- (forward-line 1)
- (if (looking-at org-list-full-item-re)
- (forward-line -1)
- (setq next-line (buffer-substring (line-beginning-position) (line-end-position))))
-
-
- (mapc (lambda (x)
- (if (stringp (symbol-value x))
- (set x (org-trim (substring-no-properties (symbol-value x))))))
- '(text value next-line))
- (if next-line (setq text (concat text " " next-line)))
-
- (list (cons :indent indent) (cons :text text) (cons :value value) (cons :sym (intern text))))
- nil))
- (defun org-index--create-missing-index (&rest reasons)
- "Create a new empty index table with detailed explanation. Argument REASONS explains why."
- (org-index--ask-before-create-index "Cannot find your index table: "
- "new permanent" "."
- reasons)
- (org-index--create-index))
- (defun org-index--report-index-error (&rest reasons)
- "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
- (when org-index--buffer
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--below-hline)
- (org-reveal t))
- (org-index--ask-before-create-index "The existing index contains this error: "
- "temporary" ", to compare with."
- reasons)
- (org-index--create-index t t))
- (defun org-index--ask-before-create-index (explanation type for-what reasons)
-
- "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
- (let (reason prompt)
- (setq reason (apply 'format reasons))
- (setq prompt (concat explanation reason "\n\n"
- "However, this assistant can help you to create a "
- type " index with detailed comments" for-what "\n\n"
- "Do you want to proceed ?"))
- (unless (let ((max-mini-window-height 1.0))
- (y-or-n-p prompt))
- (error (concat explanation reason)))))
- (defun org-index--create-index (&optional temporary compare)
- "Create a new empty index table with detailed explanation.
- specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
- (let (buffer
- title
- firstref
- id)
- (if temporary
- (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
- (buffer-name "*org-index-example-index*"))
- (setq buffer (get-buffer-create buffer-name))
- (with-current-buffer buffer
-
- (unless (string= (buffer-file-name) file-name)
- (set-visited-file-name file-name))
- (rename-buffer buffer-name)
- (erase-buffer)
- (org-mode)))
- (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))))))
- (setq title (read-from-minibuffer "Please enter the title of the index node: "))
- (while (progn
- (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: "))
- (let (desc)
- (when (string-match "[[:blank:]]" firstref)
- (setq desc "Contains whitespace"))
- (when (string-match "[[:cntrl:]]" firstref)
- (setq desc "Contains control characters"))
- (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
-
- (setq desc
- (cond ((string= firstref "") "is empty")
- ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
- ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
- ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
- )))
- (if desc
- (progn
- (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
- t)
- nil))))
- (with-current-buffer buffer
- (goto-char (point-max))
- (insert (format "* %s %s\n" firstref title))
- (if temporary
- (insert "
- Below you find your temporary index table, which WILL NOT LAST LONGER
- THAN YOUR CURRENT EMACS SESSION.
- ")
- (insert "
- Below you find your initial index table, which will grow over time.
- "))
- (insert "
- You may start using it by adding some lines. Just move to
- another heading, invoke `org-index' and choose the command
- 'add'. After adding a few nodes, try the command 'occur'
- to search among them.
- To gain further insight you may invoke the subcommand 'help', or
- read the description of `org-index'.
- Within the index table below, dhe sequence of columns does not
- matter. You may reorder them in any way you please. Columns are
- found by their heading. You may also add your own columns,
- which should start with a dot (e.g. '.custom').
- Following this explanations you will find the item-list
- `columns-and-flags', which influences the behaviour of
- `org-index'. See the explanations which are part of this list.
- This node needs not be a top level node; its name is completely
- at your choice; it is found through its ID only.
- ")
- (unless temporary
- (insert "
- Remark: These lines of explanation can be removed at any time.
- "))
- (setq id (org-id-get-create))
- (insert (format "
- %s
- | ref | category | keywords | count | last-accessed | created | id |
- | | | | | | | <4> |
- |-----+-----------+----------+-------+---------------+---------+------|
- | %s | | %s | | | %s | %s |
- "
- org-index--sample-flags
- firstref
- "This node"
- (with-temp-buffer (org-insert-time-stamp nil nil t))
- id))
-
- (org-id-add-location id (buffer-file-name))
- (setq buffer-save-without-query t)
- (basic-save-buffer)
- (while (not (org-at-table-p)) (forward-line -1))
- (unless buffer-read-only (org-table-align))
- (while (not (org-at-heading-p)) (forward-line -1))
-
- (let ((org-index-id id))
- (org-index--verify-id))
-
- (setq org-index-id id)
-
- (if temporary
- (progn
-
- (when compare
- (pop-to-buffer-same-window org-index--buffer)
- (goto-char org-index--point)
- (org-index--unfold-buffer)
- (delete-other-windows)
- (select-window (split-window-vertically)))
-
- (pop-to-buffer-same-window buffer)
- (org-id-goto id)
- (org-index--unfold-buffer)
- (if compare
- (error "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
- (message "This is your new temporary index.")))
- (progn
-
- (pop-to-buffer-same-window buffer)
- (delete-other-windows)
- (org-id-goto id)
- (org-index--unfold-buffer)
- (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 ? ")
- (progn
- (customize-save-variable 'org-index-id id)
- (error "Saved org-index-id '%s' to %s" id (or custom-file
- user-init-file)))
- (let (sq)
- (setq sq (format "(setq org-index-id \"%s\")" id))
- (kill-new sq)
- (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))))))))
- (defun org-index--unfold-buffer ()
- "Helper function to unfold buffer."
- (org-show-context)
- (org-show-subtree)
- (recenter 1)
- (save-excursion
- (org-back-to-heading)
- (forward-line)
- (org-cycle)
- (org-index--goto-list "columns-and-flags")
- (org-cycle)))
- (defun org-index--update-line (&optional ref-or-id)
- "Update columns count and last-accessed in line REF-OR-ID."
- (let ((newcount 0)
- initial)
- (with-current-buffer org-index--buffer
- (unless buffer-read-only
-
- (when ref-or-id
- (setq initial (point))
- (goto-char org-index--below-hline)
- (while (and (org-at-table-p)
- (not (or (string= ref-or-id (org-index--get-or-set-field 'ref))
- (string= ref-or-id (org-index--get-or-set-field 'id)))))
- (forward-line)))
- (if (not (org-at-table-p))
- (error "Did not find reference or id '%s'" ref-or-id)
- (org-index--update-current-line))
- (if initial (goto-char initial))))))
- (defun org-index--update-current-line ()
- "Update current lines columns count and last-accessed."
- (let (newcount (count-field (org-index--get-or-set-field 'count)))
-
- (when (or (not count-field)
- (string-match "^[0-9]+$" count-field))
- (setq newcount (+ 1 (string-to-number (or count-field "0"))))
- (org-index--get-or-set-field 'count
- (number-to-string newcount)))
-
- (org-table-goto-column (org-index--column-num 'last-accessed))
- (org-table-blank-field)
- (org-insert-time-stamp nil t t)
-
- (org-index--promote-current-line)
- (org-index--align-and-fontify-current-line)))
- (defun org-index--align-and-fontify-current-line ()
- "Make current line blend well among others."
- (let ((line (substring-no-properties (delete-and-extract-region (line-beginning-position) (line-end-position)))))
-
- (insert (with-temp-buffer
- (org-set-font-lock-defaults)
- (insert org-index--headings-visible)
- (goto-char (point-min))
-
- (search-forward "|")
- (replace-string " " "." nil (point) (line-end-position))
- (replace-string ".|." " | " nil (line-beginning-position) (line-end-position))
- (replace-string "|." "| " nil (line-beginning-position) (line-end-position))
- (goto-char (point-max))
- (insert line)
- (forward-line 0)
- (org-table-align)
- (font-lock-fontify-region (point-min) (point-max))
- (goto-char (point-max))
- (forward-line -1)
- (buffer-substring (line-beginning-position) (line-end-position))))))
- (defun org-index--promote-current-line ()
- "Move current line up in table according to changed sort fields."
- (let (begin end key
- (to-skip 0))
- (forward-line 0)
- (setq key (org-index--get-sort-key))
- (setq begin (point))
- (setq end (line-beginning-position 2))
- (forward-line -1)
- (while (and (org-at-table-p)
- (not (org-at-table-hline-p))
- (string< (org-index--get-sort-key) key))
- (incf to-skip)
- (forward-line -1))
- (forward-line 1)
-
- (when (> to-skip 0)
- (insert (delete-and-extract-region begin end))
- (forward-line -1))))
- (defun org-index--get-sort-key (&optional sort with-ref)
- "Get value for sorting from column SORT, optional WITH-REF."
- (let (ref
- ref-field
- key)
- (unless sort (setq sort org-index--last-sort))
- (when (or with-ref
- (eq sort 'ref))
-
-
- (setq ref-field (org-index--get-or-set-field 'ref))
- (string-match org-index--ref-regex ref-field)
- (setq ref (format
- "%06d"
- (string-to-number
- (or (match-string 1 ref-field)
- "0")))))
- (setq key
- (cond
- ((eq sort 'count)
- (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))
- ((eq sort 'ref)
- ref)
- ((eq sort 'id)
- (org-index--get-or-set-field sort))
- ((eq sort 'last-accessed)
- (org-index--get-or-set-field sort))
- ((eq sort 'created)
- (org-index--get-or-set-field sort))
- (t (error "This is a bug: unmatched case '%s'" sort))))
- (if with-ref (setq key (concat key ref)))
- key))
- (defun org-index--get-or-set-field (key &optional value)
- "Retrieve field KEY from index table or set it to VALUE."
- (let (field)
- (save-excursion
- (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value)))
- (if (string= field "") (setq field nil))
- (org-no-properties field))))
- (defun org-index--column-num (key)
- "Return number of column KEY."
- (if (numberp key)
- key
- (cdr (assoc key org-index--columns))))
- (defun org-index--special-column (key)
- "Return column (not a number) for special column KEY."
- (cdr (assoc key org-index--special-columns)))
- (defun org-index--flag-p (flag column)
- "Check if COLUMN has FLAG set."
- (unless (memq flag org-index--all-flags)
- (error (format "Internal error: unknown flag %s" (symbol-name flag))))
- (memq column (assoc flag org-index--flagged-columns)))
- (defun org-index--make-guarded-search (ref &optional dont-quote)
- "Make robust search string from REF; DONT-QUOTE it, if requested."
- (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
- (defun org-index--do-statistics ()
- "Compute statistics about index table."
- (let ((total 0)
- ref-field ref min max message)
-
- (goto-char org-index--below-hline)
- (while (org-at-table-p)
-
- (setq ref-field (org-index--get-or-set-field 'ref))
- (if (and ref-field
- (string-match org-index--ref-regex ref-field))
- (setq ref (string-to-number (match-string 1 ref-field))))
-
- (if (or (not min) (< ref min)) (setq min ref))
- (if (or (not max) (> ref max)) (setq max ref))
-
- (setq total (1+ total))
- (forward-line))
- (setq message (format "First reference is %s, last %s; %d values in between, %d of them are used (%d percent)"
- (format org-index--ref-format min)
- (format org-index--ref-format max)
- (1+ (- max min))
- total
- (truncate (* 100 (/ (float total) (1+ (- max min)))))
-
- ))
- (goto-char org-index--below-hline)
- message))
- (defun org-index--do-add-or-update ()
- "For current node or current line in index, add a new line to index table or update existing."
- (let* (id ref args yank ref-and-yank)
-
- (if org-index--within-node
- (progn
- (unless (org-at-table-p)
- (error "Within index node but not on table"))
- (setq id (org-index--get-or-set-field 'id))
- (setq ref (org-index--get-or-set-field 'ref))
- (setq args (org-index--collect-values-for-add-update-remote id))
- (org-index--write-fields-for-add-update args)
- (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add)))
-
- (cons (format "Updated index line %s" ref) yank))
- (unless (org-at-heading-p)
- (error "Not at headline"))
- (setq id (org-id-get-create))
- (setq ref (org-index--on 'id id (org-index--get-or-set-field 'ref)))
- (setq args (org-index--collect-values-for-add-update id ref))
- (if ref
-
- (let ((kvs args)
- found-and-message)
- (org-index--on
- 'ref ref
- (org-index--write-fields-for-add-update args)
- (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add))))
-
- (cons (format "Updated index line %s" ref) yank))
-
- (setq ref-and-yank (apply 'org-index--do-new-line args))
- (cons (format "Added index line %s" (car ref-and-yank)) (concat (cdr ref-and-yank) " "))))))
- (defun org-index--check-ids ()
- "Check, that ids really point to a node."
- (let ((lines 0)
- id ids marker)
-
- (goto-char org-index--below-hline)
- (catch 'problem
- (while (org-at-table-p)
- (when (setq id (org-index--get-or-set-field 'id))
-
- (when (member id ids)
- (org-table-goto-column (org-index--column-num 'id))
- (throw 'problem "This id appears twice in index; please use command 'maintain' to check for duplicate ids"))
- (incf lines)
- (setq ids (cons id ids))
-
- (setq marker (org-id-find id t))
- (unless marker
- (org-table-goto-column (org-index--column-num 'id))
- (throw 'problem "This id cannot be found")))
- (forward-line))
- (goto-char org-index--below-hline)
- nil)))
-
- (defun org-index--update-all-lines ()
- "Update all lines of index at once."
- (let ((lines 0)
- id ref kvs)
-
-
- (or
- (org-index--check-ids)
- (progn
- (goto-char org-index--below-hline)
- (while (org-at-table-p)
-
-
- (when (setq id (org-index--get-or-set-field 'id))
- (setq ref (org-index--get-or-set-field 'ref))
- (setq kvs (org-index--collect-values-for-add-update-remote id))
- (org-index--write-fields-for-add-update kvs)
- (incf lines))
- (forward-line))
- (goto-char org-index--below-hline)
- (org-table-align)
- (format "Updated %d lines" lines)))))
- (defun org-index--collect-values-for-add-update (id &optional silent category)
- "Collect values for adding or updating line specified by ID, do not ask if SILENT, use CATEGORY, if given."
-
- (let ((args (list 'ref t 'id id))
- content)
-
- (dolist (col-num org-index--columns)
-
- (setq content "")
-
- (if (eq (car col-num) 'keywords)
- (setq content (nth 4 (org-heading-components))))
-
- (if (eq (car col-num) 'category)
- (setq content (or category org-index--category-before)))
- (if (eq (car col-num) 'level)
- (setq content (number-to-string (org-outline-level))))
-
-
- (if (org-index--flag-p 'shift-ref-and-date-on-add (car col-num))
- (dotimes (i 2)
- (if (or (string-match (concat "^\\s-*" org-index--ref-regex) content)
- (string-match (concat org-ts-regexp-both) content))
- (setq content (substring content (match-end 0))))))
-
- (if (and (not silent)
- (org-index--flag-p 'edit-on-add (car col-num)))
- (setq content (read-from-minibuffer
- (format "Edit text for column '%s': " (symbol-name (car col-num)))
- content)))
-
- (if (not (string= content ""))
- (setq args (append (list (car col-num) content) args))))
- args))
- (defun org-index--collect-values-for-add-update-remote (id)
- "Wrap `org-index--collect-values-for-add-update' by prior moving to remote node identified by ID."
-
- (let (marker point args)
- (setq marker (org-id-find id t))
-
- (with-current-buffer (marker-buffer marker)
- (setq point (point))
- (goto-char marker)
- (setq args (org-index--collect-values-for-add-update id t (org-get-category (point) t)))
- (goto-char point))
- args))
- (defun org-index--write-fields-for-add-update (kvs)
- "Update current line with values from KVS (keys-values)."
- (while kvs
- (unless (eq (car kvs) 'ref)
- (org-index--get-or-set-field (car kvs) (org-trim (cadr kvs))))
- (setq kvs (cddr kvs))))
- (defun org-index--do-delete ()
- "Perform command delete."
- (unless (org-at-heading-p)
- (error "Not at headline"))
- (let* ((id (org-entry-get (point) "ID"))
- (ref (org-index--ref-from-id id)))
-
- (if ref
- (save-excursion
- (end-of-line)
- (let ((end (point)))
- (beginning-of-line)
- (when (search-forward ref end t)
- (delete-char (- (length ref)))
- (just-one-space)))))
-
- (if (org-index--delete-line id)
- (format "Deleted index line %s" ref)
- (format "Did not find id %s in index" id))))
- (defun org-index--go (&optional column value)
- "Position cursor on index line where COLUMN equals VALUE.
- Return t or nil, leave point on line or at top of table, needs to be in buffer initially."
- (let (found text)
- (unless (eq (current-buffer) org-index--buffer)
- (error "This is a bug: Not in index buffer"))
-
- (goto-char org-index--below-hline)
- (if column
- (progn
- (forward-line -1)
- (while (and (not found)
- (forward-line)
- (org-at-table-p))
- (setq found (string= value (org-index--get-or-set-field column)))))
- (setq found t))
-
- (if found
- t
- (goto-char org-index--below-hline)
- nil)))
- (defun org-index--do-head (ref id &optional other)
- "Perform command head: Find node with REF or ID and present it.
- If OTHER in separate window."
-
- (setq org-index--last-ref ref)
- (let (message marker)
- (setq marker (org-id-find id t))
- (if marker
- (progn
- (org-index--update-line id)
- (let (cb)
- (if other
- (progn
- (setq cb (current-buffer))
- (pop-to-buffer (marker-buffer marker)))
- (pop-to-buffer-same-window (marker-buffer marker)))
-
- (goto-char marker)
- (org-reveal t)
- (org-show-entry)
- (recenter))
- (setq message (format "Found headline %s" ref)))
- (setq message (format "Did not find headline %s" ref)))))
- (defun org-index--do-occur ()
- "Perform command occur."
- (let ((word "")
- (prompt "Search for: ")
- (lines-wanted (window-body-height))
- (lines-found 0)
- words
- occur-buffer
- begin
- narrow
- help-text
- key-help
- search-text
- done
- in-c-backspace
- show-headings
- help-overlay
- last-point
- key
- key-sequence)
-
-
- (if (get-buffer org-index--occur-buffer-name)
- (kill-buffer org-index--occur-buffer-name))
- (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
- (pop-to-buffer-same-window occur-buffer)
-
- (setq buffer-read-only t)
- (toggle-truncate-lines 1)
- (setq font-lock-keywords-case-fold-search t)
- (setq case-fold-search t)
-
- (setq org-index--occur-stack nil)
- (setq org-index--occur-tail-overlay nil)
-
-
- (goto-char (marker-position org-index--below-hline))
- (forward-line 0)
- (setq begin (point))
- (forward-line -1)
- (setq narrow (point))
- (while (org-at-table-p)
- (forward-line))
- (narrow-to-region narrow (point))
- (goto-char (point-min))
- (forward-line)
-
- (setq help-text (cons
- "Incremental occur; `?' toggles help and headlines.\n"
- (concat
- (org-index--wrap
- (concat
- "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"))
- org-index--headings)))
-
-
- (setq help-overlay (make-overlay (point-min) begin))
- (overlay-put help-overlay 'display (car help-text))
- (overlay-put help-overlay 'face 'org-agenda-dimmed-todo-face)
- (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
- (overlay-put org-index--occur-tail-overlay 'invisible t)
- (while (not done)
- (if in-c-backspace
- (setq key "<backspace>")
- (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
-
- (setq key-sequence
- (vector (read-key
- (format "%s%s%s"
- prompt
- search-text
- (if (string= search-text "") "" " ")))))
- (setq key (key-description key-sequence)))
- (cond
- ((string= key "<C-backspace>")
- (setq in-c-backspace t))
- ((member key (list "<backspace>" "DEL"))
- (if (= (length word) 0)
-
- (progn
- (setq word (car words))
- (setq words (cdr words))
- (setq in-c-backspace nil))
-
- (unhighlight-regexp (regexp-quote word))
-
- (setq word (substring word 0 -1))
- (when (= (length word) 0)
- (setq word (car words))
- (setq words (cdr words))
- (setq in-c-backspace nil))
-
- (setq lines-found (or (org-index--unhide) lines-wanted))
- (move-overlay org-index--occur-tail-overlay
- (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
- (point-max))
- (point-max))
-
-
-
- (unless (= (length word) 0)
- (highlight-regexp (regexp-quote word) 'isearch))
-
- (goto-char begin)))
- ((member key (list "SPC" ","))
-
- (setq words (cons word words))
- (setq word ""))
- ((string= key "?")
- (setq help-text (cons (cdr help-text) (car help-text)))
- (overlay-put help-overlay 'display (car help-text)))
- ((and (= (length key) 1)
- (aref printable-chars (elt key 0)))
-
- (unless (= (length word) 0)
- (unhighlight-regexp (regexp-quote word)))
-
- (setq word (concat word key))
-
-
- (goto-char begin)
- (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted))
- (move-overlay org-index--occur-tail-overlay
- (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
- (point-max))
- (point-max))
-
- (goto-char begin)
-
-
- (highlight-regexp (regexp-quote word) 'isearch)
-
- (line-move -1 t)
- (line-move 1 t))
-
- (t (setq done t))))
-
- (unless (string= key "C-g")
- (setq unread-command-events (listify-key-sequence key-sequence))
- (message key))
-
-
- (setq last-point (point))
-
-
-
-
-
- (let ((lines-collected 0)
- keymap line all-lines end-of-head)
- (setq cursor-type t)
- (goto-char begin)
-
- (while (and (not (eobp))
- (< lines-collected lines-wanted))
-
- (while (and (invisible-p (point))
- (not (eobp)))
- (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
- (setq line (buffer-substring (line-beginning-position) (line-end-position)))
- (unless (string= line "")
- (incf lines-collected)
- (setq all-lines (cons (concat line
- "\n")
- all-lines)))
- (forward-line 1))
-
- (kill-buffer org-index--occur-buffer-name)
-
- (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
- (pop-to-buffer-same-window occur-buffer)
- (insert org-index--headings)
- (setq end-of-head (point))
-
- (save-excursion
- (apply 'insert (reverse all-lines))
- (if (= lines-collected lines-wanted)
- (insert "\n(more lines omitted)\n")))
-
- (org-mode)
- (setq truncate-lines t)
- (if (org-at-table-p) (org-table-align))
- (font-lock-fontify-buffer)
-
- (setq org-index--occur-help-overlay (make-overlay (point-min) end-of-head))
- (setq org-index--occur-help-text
- (cons
- (org-index--wrap
- (concat "Search is done; `?' toggles help and headlines.\n"))
- (concat
- (org-index--wrap (format (concat "Search is done. "
- (if (< lines-collected lines-wanted)
- " Showing all %d matches for "
- " Showing one window of matches for ")
- "\"" search-text
- "\". <return> jumps to heading, <tab> jumps to heading in other window, <S-return> to matching line in index, <space> increments count.\n" )
- (length all-lines)))
- org-index--headings)))
-
- (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
- (overlay-put org-index--occur-help-overlay 'face 'org-agenda-dimmed-todo-face)
-
- (setq case-fold-search t)
- (setq font-lock-keywords-case-fold-search t)
- (mapc (lambda (w) (unless (or (not w) (string= w "")) (highlight-regexp (regexp-quote w) 'isearch)))
- (cons word words))
- (setq buffer-read-only t)
-
- (setq keymap (make-sparse-keymap))
- (set-keymap-parent keymap org-mode-map)
- (mapc (lambda (x) (define-key keymap (kbd x)
- (lambda () (interactive)
- (message "%s" (org-index--occur-to-head)))))
- (list "<return>" "RET"))
- (define-key keymap (kbd "<tab>")
- (lambda () (interactive)
- (message (org-index--occur-to-head t))))
-
- (define-key keymap (kbd "SPC")
- (lambda () (interactive)
-
- (let ((ref (org-index--get-or-set-field 'ref))
- count)
- (org-index--on
- 'ref ref
- (setq count (+ 1 (string-to-number (org-index--get-or-set-field 'count))))
- (org-index--get-or-set-field 'count (number-to-string count))
- (org-index--promote-current-line)
- (org-index--align-and-fontify-current-line))
-
- (let ((inhibit-read-only t))
- (org-index--get-or-set-field 'count (number-to-string count)))
- (message "Incremented count to %d" count))))
-
- (define-key keymap (kbd "<S-return>")
- (lambda () (interactive)
- (org-index 'enter (org-index--get-or-set-field 'ref))))
-
- (define-key keymap (kbd "?")
- (lambda () (interactive)
- (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
- (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
-
- (use-local-map keymap))))
- (defun org-index--wrap (text)
- "Wrap TEXT at fill column."
- (with-temp-buffer
- (insert text)
- (fill-region (point-min) (point-max) nil t)
- (buffer-string)))
- (defun org-index--occur-to-head (&optional other)
- "Helper for `org-index--occur', find heading with ref or id; if OTHER, in other window."
- (let ((ref (org-index--get-or-set-field 'ref))
- (id (org-index--get-or-set-field 'id)))
- (if id
- (org-index--do-head ref id other)
- (message "Current line has no id."))))
- (defun org-index--hide-with-overlays (words lines-wanted)
- "Hide text that is currently visible and does not match WORDS by creating overlays; leave LINES-WANTED lines visible."
- (let ((lines-found 0)
- (end-of-visible (point))
- overlay overlays start matched)
-
- (while (and (not (eobp))
- (< lines-found lines-wanted))
-
- (while (and (not (eobp))
- (and
- (invisible-p (point))
- (< (point) (overlay-start org-index--occur-tail-overlay))))
- (goto-char (overlay-end (car (overlays-at (point))))))
-
- (setq matched nil)
- (setq start (point))
- (while (and (not (eobp))
- (not
- (and
- (invisible-p (point))
- (< (point) (overlay-start org-index--occur-tail-overlay))))
- (not (and (org-index--test-words words)
- (setq matched t))))
- (forward-line 1))
-
- (when (< start (point))
- (setq overlay (make-overlay start (point)))
- (overlay-put overlay 'invisible t)
- (setq overlays (cons overlay overlays)))
-
- (when matched
- (forward-line 1)
- (setq end-of-visible (point))
- (incf lines-found)))
-
-
- (setq org-index--occur-stack
- (cons (list (cons :overlays overlays)
- (cons :end-of-visible end-of-visible)
- (cons :lines lines-found))
- org-index--occur-stack))
- lines-found))
- (defun org-index--unhide ()
- "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
- (when org-index--occur-stack
-
- (mapc (lambda (y)
- (delete-overlay y))
- (cdr (assoc :overlays (car org-index--occur-stack))))
-
- (setq org-index--occur-stack (cdr org-index--occur-stack))
-
- (if org-index--occur-stack (cdr (assoc :lines (car org-index--occur-stack))))))
- (defun org-index--test-words (words)
- "Test current line for match against WORDS."
- (let (line)
- (setq line (downcase (buffer-substring (line-beginning-position) (line-beginning-position 2))))
- (catch 'not-found
- (dolist (w words)
- (or (search w line)
- (throw 'not-found nil)))
- t)))
- (defun org-index--create-new-line ()
- "Do the common work for `org-index-new-line' and `org-index'."
- (let (new)
-
- (unless new
- (setq new (format "%s%d%s" org-index--head (1+ org-index--maxref) org-index--tail)))
-
- (goto-char org-index--below-hline)
- (if (eq (org-index--special-column 'sort) 'count)
- (progn
- (while (org-at-table-p)
- (forward-line))
- (forward-line -1)
- (org-table-insert-row t))
- (org-table-insert-row))
-
- (org-table-goto-column (org-index--column-num 'created))
- (org-insert-time-stamp nil nil t)
- (org-table-goto-column (org-index--column-num 'count))
- (insert "1")
- new))
- (defun org-index--sort-silent ()
- "Sort index for default column to remove any effects of temporary sorting."
- (save-excursion
- (org-index--verify-id)
- (org-index--parse-table)
- (org-index--on nil nil
- (org-index--do-sort-index (org-index--special-column 'sort))
- (org-table-align)
- (remove-hook 'before-save-hook 'org-index--sort-silent))))
- (defun org-index--copy-visible (beg end)
- "Copy the visible parts of the region without adding it to kill-ring; copy of `org-copy-visible'"
- (let (snippets s)
- (save-excursion
- (save-restriction
- (narrow-to-region beg end)
- (setq s (goto-char (point-min)))
- (while (not (= (point) (point-max)))
- (goto-char (org-find-invisible))
- (push (buffer-substring s (point)) snippets)
- (setq s (goto-char (org-find-visible))))))
- (apply 'concat (nreverse snippets))))
- (provide 'org-index)
|