Skip to content

Commit 9bc2d19

Browse files
authored
Merge pull request #60 from jamescherti/fix-warnings
Fix warnings, add lexical-binding, enhance docstrings, and add *.elc …
2 parents 820a919 + 09430ef commit 9bc2d19

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

markdown-toc.el

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; markdown-toc.el --- A simple TOC generator for markdown file
1+
;;; markdown-toc.el --- A simple TOC generator for markdown file -*- lexical-binding: t; -*-
22

33
;; Copyright (C) 2014-2020 Antoine R. Dumont (@ardumont)
44

@@ -63,6 +63,7 @@
6363
(require 's)
6464
(require 'dash)
6565
(require 'markdown-mode)
66+
(require 'find-func) ; find-library-name
6667

6768
(defconst markdown-toc--toc-version "0.1.5" "Current version installed.")
6869

@@ -107,7 +108,7 @@ Example: '-' for unordered lists or '1.' for ordered lists."
107108
"User crafted function to manipulate toc-structure as user sees fit.
108109
109110
The toc-structure has the following form:
110-
'((0 . \"some markdown page title\")
111+
\\='((0 . \"some markdown page title\")
111112
(0 . \"main title\")
112113
(1 . \"Sources\")
113114
(2 . \"Marmalade (recommended)\")
@@ -126,7 +127,7 @@ The toc-structure has the following form:
126127
If the user wanted to remove the first element, it could for
127128
example define the following function:
128129
(custom-set-variables
129-
'(markdown-toc-user-toc-structure-manipulation-fn 'cdr))
130+
\\='(markdown-toc-user-toc-structure-manipulation-fn \\='cdr))
130131
131132
Default to identity function (do nothing)."
132133
:group 'markdown-toc
@@ -166,16 +167,15 @@ Default to identity function (do nothing)."
166167
(s-join "" it)))
167168

168169
(defconst markdown-toc--dash-protection-symbol "09876543214b825dc642cb6eb9a060e54bf8d69288fbee49041234567890"
169-
"Implementation detail to protect the - characters
170-
when converting to link.")
170+
"Implementation detail to protect the - characters when converting to link.")
171171

172172
(defconst markdown-toc--underscore-protection-symbol "afec96cafb7bc4b0e216bfe86db4bd6c4aab44bca19dd9999b11e162f595d711"
173-
"Implementation detail to protect the `_` characters
174-
when converting to link.")
173+
"Implementation detail to protect the `_` characters when converting to link.")
175174

176175
(defun markdown-toc--to-link (title &optional count)
177-
"Given a TITLE, return the markdown link associated."
178-
176+
"Given a TITLE, return the markdown link associated.
177+
If COUNT is provided and greater than 0, it appends '-COUNT' to the link to
178+
ensure uniqueness."
179179
(let ((count (if count count 0)))
180180
(format "[%s](#%s%s)" title
181181
(->> title
@@ -191,21 +191,25 @@ Default to identity function (do nothing)."
191191
(concat "-" (number-to-string count))
192192
""))))
193193

194-
(defun markdown--count-duplicate-titles (toc-structure)
195-
"Counts the number of times each title appeared in the toc structure and adds
196-
it to the TOC structure."
194+
(defun markdown-toc--count-duplicate-titles (toc-structure)
195+
"Counts the occurrences of each title in the TOC structure.
196+
Appends the count to each title, with the count starting at 0 for the first
197+
occurrence.
198+
TOC-STRUCTURE is a list of cons cells, where each cons cell contains:
199+
- The indentation level (an integer).
200+
- The title of the section (a string)."
197201
(-map-indexed (lambda (index n)
198-
(let* ((indent (car n))
199-
(title (cdr n))
200-
(count (--count (string= title (cdr it))
201-
(-take (+ index 1) toc-structure))))
202-
(list indent title (- count 1))))
203-
toc-structure))
202+
(let* ((indent (car n))
203+
(title (cdr n))
204+
(count (--count (string= title (cdr it))
205+
(-take (+ index 1) toc-structure))))
206+
(list indent title (- count 1))))
207+
toc-structure))
204208

205209
(defun markdown-toc--to-markdown-toc (level-title-toc-list)
206210
"Given LEVEL-TITLE-TOC-LIST, a list of pair level, title, return a TOC string."
207211
(->> level-title-toc-list
208-
markdown--count-duplicate-titles
212+
markdown-toc--count-duplicate-titles
209213
(--map (let ((nb-spaces (* markdown-toc-indentation-space (car it)))
210214
(title (car (cdr it)))
211215
(count (car (cdr (cdr it)))))
@@ -222,11 +226,14 @@ Return the end position if it exists, nil otherwise."
222226
(goto-char (point-min))
223227
(re-search-forward markdown-toc-header-toc-start nil t)))
224228

229+
225230
(defun markdown-toc--toc-start ()
226231
"Compute the toc's starting point."
227232
(save-excursion
228233
(goto-char (markdown-toc--toc-already-present-p))
229-
(point-at-bol)))
234+
(if (fboundp 'point-at-bol)
235+
(point-at-bol)
236+
(line-beginning-position))))
230237

231238
(defun markdown-toc--toc-end ()
232239
"Compute the toc's end point."
@@ -241,12 +248,16 @@ Return the end position if it exists, nil otherwise."
241248
markdown-toc--compute-full-toc))
242249

243250
(defun markdown-toc--delete-toc (&optional replace-toc-p)
244-
"Delets a TOC."
251+
"Deletes the table of contents (TOC) from the current buffer.
252+
253+
The function identifies the region defined by the TOC's start and end positions,
254+
and deletes it. If REPLACE-TOC-P is non-nil, it moves the point to the start of
255+
the deleted TOC region, effectively replacing it."
245256
(let ((region-start (markdown-toc--toc-start))
246257
(region-end (markdown-toc--toc-end)))
247258
(delete-region region-start (1+ region-end))
248259
(when replace-toc-p
249-
(goto-char region-start))))
260+
(goto-char region-start))))
250261

251262
(defun markdown-toc--compute-full-toc (toc)
252263
"Given the TOC's content, compute the full toc with comments and title."
@@ -257,11 +268,10 @@ Return the end position if it exists, nil otherwise."
257268
markdown-toc-header-toc-end))
258269

259270
;;;###autoload
260-
(defun markdown-toc-generate-toc (&optional replace-toc-p)
271+
(defun markdown-toc-generate-toc ()
261272
"Generate a TOC for markdown file at current point.
262-
Deletes any previous TOC.
263-
If called interactively with prefix arg REPLACE-TOC-P, replaces previous TOC."
264-
(interactive "P")
273+
Deletes any previous TOC."
274+
(interactive)
265275
(save-excursion
266276
(when (markdown-toc--toc-already-present-p)
267277
;; when toc already present, remove it
@@ -276,16 +286,16 @@ If called interactively with prefix arg REPLACE-TOC-P, replaces previous TOC."
276286

277287
;;;###autoload
278288
(defun markdown-toc-generate-or-refresh-toc ()
279-
"Generate a TOC for markdown file at current point or refreshes an already generated TOC."
289+
"Generate a TOC for markdown file at current point or refreshes it."
280290
(interactive)
281-
(markdown-toc-generate-toc t))
291+
(markdown-toc-generate-toc))
282292

283293
;;;###autoload
284294
(defun markdown-toc-refresh-toc ()
285295
"Refreshes an already generated TOC."
286296
(interactive)
287297
(when (markdown-toc--toc-already-present-p)
288-
(markdown-toc-generate-toc t)))
298+
(markdown-toc-generate-toc)))
289299

290300
;;;###autoload
291301
(defun markdown-toc-delete-toc ()
@@ -296,7 +306,7 @@ If called interactively with prefix arg REPLACE-TOC-P, replaces previous TOC."
296306

297307
(defun markdown-toc--read-title-out-of-link (link)
298308
"Extract the link title out of a markdown LINK title.
299-
This assumes no funky stuff in the markdown link format ` - [<title>](...) ` "
309+
This assumes no funky stuff in the markdown link format ` - [<title>](...) `"
300310
(->> link
301311
s-trim
302312
(s-chop-prefix "- [")
@@ -320,11 +330,16 @@ and returns nil. Otherwise, returns the level number."
320330
;;;###autoload
321331
(defun markdown-toc-follow-link-at-point ()
322332
"On a given toc link, navigate to the current markdown header.
323-
If the toc is misindented (according to markdown-toc-indentation-space`)
324-
or if not on a toc link, this does nothing.
325-
"
333+
If the toc is misindented (according to `markdown-toc-indentation-space')
334+
or if not on a toc link, this does nothing."
326335
(interactive)
327-
(let* ((full-title (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
336+
(let* ((full-title (buffer-substring-no-properties
337+
(if (fboundp 'point-at-bol)
338+
(point-at-bol)
339+
(line-beginning-position))
340+
(if (fboundp 'point-at-eol)
341+
(point-at-eol)
342+
(line-end-position))))
328343
(level (markdown-toc--title-level full-title)))
329344
(if level ;; nil if misindented or not on a title
330345
(let ((title (markdown-toc--read-title-out-of-link full-title)))
@@ -362,10 +377,6 @@ opens new issue in markdown-toc's github tracker."
362377

363378
(setq markdown-toc-mode-map
364379
(let ((map (make-sparse-keymap)))
365-
(define-key map (kbd "C-c m .") 'markdown-toc-follow-link-at-point)
366-
(define-key map (kbd "C-c m t") 'markdown-toc-generate-or-refresh-toc)
367-
(define-key map (kbd "C-c m d") 'markdown-toc-delete-toc)
368-
(define-key map (kbd "C-c m v") 'markdown-toc-version)
369380
map))
370381

371382
;;;###autoload

test/markdown-toc-test.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ For this, you need to install a snippet of code in your emacs configuration file
412412
#### Git
413413
#### Tar
414414
"
415-
(markdown-toc-generate-toc 'replace-old-toc)))))
415+
(markdown-toc-generate-toc)))))
416416

417417
(ert-deftest test-markdown-toc-generate-or-refresh-toc--with-existing-toc ()
418418
;; Update an existing TOC

0 commit comments

Comments
 (0)