Skip to content

Commit

Permalink
Add used language names to front of list
Browse files Browse the repository at this point in the history
Previously, when a truly new language was used, it was added to the
front of the list as returned by `markdown-gfm-get-corpus`.  However,
when a known language was used it was simply kept in position in the
list of recognized languages. This yielded unexpected results with ido,
helm, etc. Now, we add all used languages to the history list
`markdown-gfm-used-languages`.  This list is then prepended to the list
of recognized languages and returned by `markdown-gfm-get-corpus`.
  • Loading branch information
jrblevin committed May 5, 2016
1 parent b838d80 commit afc5544
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
27 changes: 9 additions & 18 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -3812,17 +3812,11 @@ if three backquotes inserted at the beginning of line."
"Web-Ontology-Language" "WebIDL" "X10" "XC" "XML" "XPages" "XProc" "XQuery"
"XS" "XSLT" "Xojo" "Xtend" "YAML" "Yacc" "Zephir" "Zimpl" "desktop" "eC" "edn"
"fish" "mupad" "nesC" "ooc" "reStructuredText" "wisp" "xBase")
"Language specifiers recognized by github's syntax highlighting features.")
"Language specifiers recognized by GitHub's syntax highlighting features.")

(defvar markdown-gfm-used-languages nil
"Languages in GFM code blocks which are not explicitly declared.
Known language are declared in
`markdown-gfm-recognized-languages' and
`markdown-gfm-additional-languages'.")
"Language names used in GFM code blocks.")
(make-variable-buffer-local 'markdown-gfm-used-languages)
(defvar markdown-gfm-last-used-language nil
"Last language used in the current buffer in GFM code blocks.")
(make-variable-buffer-local 'markdown-gfm-last-used-language)

(defun markdown-trim-whitespace (str)
(markdown-replace-regexp-in-string
Expand All @@ -3847,13 +3841,10 @@ Known language are declared in
(if markdown-gfm-downcase-languages (cl-mapcar #'downcase given-corpus)
given-corpus))))

(defun markdown-add-language-if-new (lang)
(let* ((cleaned-lang (markdown-clean-language-string lang))
(find-result
(cl-find cleaned-lang (markdown-gfm-get-corpus)
:test #'equal)))
(setq markdown-gfm-last-used-language cleaned-lang)
(unless find-result (push cleaned-lang markdown-gfm-used-languages))))
(defun markdown-gfm-add-used-language (lang)
"Clean LANG and add to list of used languages."
(add-to-list 'markdown-gfm-used-languages
(markdown-clean-language-string lang)))

(defun markdown-insert-gfm-code-block (&optional lang)
"Insert GFM code block for language LANG.
Expand All @@ -3867,12 +3858,12 @@ automatically in order to have the correct markup."
(markdown-clean-language-string
(completing-read
(format "Programming language [%s]: "
(or markdown-gfm-last-used-language "none"))
(or (car markdown-gfm-used-languages) "none"))
(markdown-gfm-get-corpus)
nil 'confirm nil
'markdown-gfm-language-history))
(quit "")))))
(unless (string= lang "") (markdown-add-language-if-new lang))
(unless (string= lang "") (markdown-gfm-add-used-language lang))
(when (> (length lang) 0) (setq lang (concat " " lang)))
(if (markdown-use-region-p)
(let ((b (region-beginning)) (e (region-end)))
Expand Down Expand Up @@ -3911,7 +3902,7 @@ automatically in order to have the correct markup."
(when (and (match-beginning 2) (match-end 2))
(buffer-substring-no-properties
(match-beginning 2) (match-end 2)))))
do (progn (when lang (markdown-add-language-if-new lang))
do (progn (when lang (markdown-gfm-add-used-language lang))
(goto-char (next-single-property-change (point) prop)))))))


Expand Down
8 changes: 6 additions & 2 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3741,6 +3741,8 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
(markdown-test-string-gfm "line 1\nline 2\n"
(end-of-line)
(markdown-insert-gfm-code-block "elisp")
(should (equal (car markdown-gfm-used-languages) "elisp"))
(should (equal (car (markdown-gfm-get-corpus)) "elisp"))
(should (string-equal (buffer-string)
"line 1\n\n``` elisp\n\n```\n\nline 2\n")))
;; Test with active region
Expand All @@ -3760,12 +3762,14 @@ Detail: https://github.com/jrblevin/markdown-mode/issues/79"
(markdown-gfm-parse-buffer-for-languages)
(should (equal markdown-gfm-used-languages
(list "MaDeUp" "LANGUAGES" "MADEUP")))
(should (equal markdown-gfm-last-used-language "MaDeUp"))
(should (equal (car markdown-gfm-used-languages) "MaDeUp"))
(should (equal (car (markdown-gfm-get-corpus)) "MaDeUp"))
(goto-char (point-max))
(markdown-insert-gfm-code-block "newlang")
(should (equal markdown-gfm-used-languages
(list "newlang" "MaDeUp" "LANGUAGES" "MADEUP")))
(should (equal markdown-gfm-last-used-language "newlang"))
(should (equal (car markdown-gfm-used-languages) "newlang"))
(should (equal (car (markdown-gfm-get-corpus)) "newlang"))
(let ((markdown-gfm-downcase-languages nil))
(should
(equal (markdown-gfm-get-corpus)
Expand Down

0 comments on commit afc5544

Please sign in to comment.