tldr-pages / tldr-emacs-extension Goto Github PK
View Code? Open in Web Editor NEWEmacs extension for TlDr.
License: GNU General Public License v3.0
Emacs extension for TlDr.
License: GNU General Public License v3.0
And let user to chose between tldr-lint
and custom parser. But by default tldr-lint
should be use as it's a standard way to detect problems in pages.
Reason: to avoid dependencies and make code supportable by ELisp developers.
Not to be extremely dependent on action function names.
Instead of {{-infinity..infinity}}
substitute {{any}}
placeholder.
(defun replace-regexp-entire-buffer (pattern replacement)
"Perform regular-expression replacement throughout buffer."
(save-excursion
(goto-char (point-min))
(while (re-search-forward pattern nil t)
(replace-match replacement))))
(defun tldr-remove-broken-ellipsis()
"Remove {{...}} placeholders in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "[ ]*{{\\.\\{3\\}}}[ ]*" "")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-remove-broken-numbers()
"Replace {{placeholder_number}} placeholders with {{placeholder}} in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "{{\\([^{}_]+\\)_+\\(?:[0-9]+\\)}}" "{{\\1}}")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-remove-broken-files()
"Remove {{file}}, {{filename}}, and {{file_name}} placeholders in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "[ ]*{{file_?\\(?:name\\)?\\(?:[0-9]+\\)}}[ ]*" "")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-remove-broken-directories()
"Remove {{dir}}, {{dirname}}, {{dir_name}}, {{directory}}, {{directoryname}}, and {{directory_name}} placeholders in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "[ ]*{{dir\\(?:ectory\\)?_?\\(?:name\\)?\\(?:[0-9]+\\)}}[ ]*" "")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-correct-broken-ellipsis()
"Replace {{placeholdernumber1}} {{placeholdernumber2}} ... placeholders with {{placeholdernumber1 placeholdernumber2 ...}} in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "{{\\([^{}]+\\)[0-9]+}}\\([ ]+\\(?:{{\\1[0-9]+}}\\)\\)+" "{{\\11 \\12 ...}}")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-correct-broken-numbers()
"Replace {{placeholder_number}} placeholders with {{placeholdernumber}} in the current buffer."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "{{\\([^{}_]+\\)_+\\([0-9]+\\)}}" "{{\\1\\2}}")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-correct-broken-files()
"Replace {{file}}, {{filename}}, and {{file_name}} placeholders with {{path/to/file}} in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "{{file_?\\(?:name\\)?\\([0-9]+\\)}}" "{{path/to/file\\1}}")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-correct-broken-directories()
"Replace {{dir}}, {{dirname}}, {{dir_name}}, {{directory}}, {{directoryname}}, and {{directory_name}} placeholders with {{path/to/directory}} in the current buffer.
Trailing numbers are respected too."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "{{dir\\(?:ectory\\)?_?\\(?:name\\)?\\([0-9]+\\)}}" "{{path/to/directory\\1}}")
(message "Save file to update list of TlDr errors")
)
)
(defun tldr-correct-broken-ranges()
"Replace {{from-to}} placeholders with {{from..to}} in the current buffer.
If `from` or `to` is missing then it's replaced with negative or positive infinity respectively."
(interactive)
(with-current-buffer (current-buffer)
(replace-regexp-entire-buffer "{{\\([0-9]+\\)-+\\([0-9]+\\)}}" "{{\\1..\\2}}")
(replace-regexp-entire-buffer "{{-+\\([0-9]+\\)}}" "{{-infinity..\\1}}")
(replace-regexp-entire-buffer "{{\\([0-9]+\\)-+}}" "{{\\1..infinity}}")
(replace-regexp-entire-buffer "{{-+}}" "{{-infinity..infinity}}")
(message "Save file to update list of TlDr errors")
)
)
Generate mnemonics for first occurrences of long/short options in descriptions. Letter case must be changed (now, but maybe later).
Mnemonic generation is strictly textual, it doesn't understand term semantics so it can generate mnemonics in a wrong place (where letters from some option occur).
The best example for it is the following code:
- Print just a first line to `stdout`:
`{{command}} | sed -n '1p'`
As already mentioned - function generating mnemonics doesn't know where it's more correct to add [n]
mnemonic: to Print
word or to line
. By default (this behavior is not customizable now) it adds mnemonic to the first appropriate term, Print
here: Pri[n]t
.
See this Reddit post for details.
Version: GNU Emacs 27.1
For --{{long-option}} {{long-option}}
substitute --{{long-option}} {{any}}
Here is how I made it work in my ~/.emacs
:
;;; flymake-tldr-lint.el --- A TlDr Flymake backend powered by tldr-lint -*- lexical-binding: t; -*-
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(require 'flymake)
(defgroup flymake-tldr-lint nil
"tldr-lint backend for Flymake."
:prefix "flymake-tldr-lint-"
:group 'tools)
(defcustom flymake-tldr-lint-program "tldr-lint"
"The name of the `tldr-lint' executable."
:type 'string)
(defcustom flymake-tldr-lint-ignored ""
"Comma separated list of ignored errors."
:type 'string)
(defvar-local flymake-tldr-lint--proc nil)
(defun flymake-tldr-lint--backend (report-fn &rest _args)
"tldr-lint backend for Flymake.
Check for problems, then call REPORT-FN with results."
(unless (executable-find flymake-tldr-lint-program)
(error "Could not find tldr-lint executable"))
(when (process-live-p flymake-tldr-lint--proc)
(kill-process flymake-tldr-lint--proc)
(setq flymake-tldr-lint--proc nil))
(let* ((source (current-buffer))
(filename (buffer-file-name source)))
(save-restriction
(widen)
(setq
flymake-tldr-lint--proc
(make-process
:name "tldr-lint-flymake" :noquery t :connection-type 'pipe
:buffer (generate-new-buffer " *tldr-lint-flymake*")
:command (remove nil (list flymake-tldr-lint-program
filename))
:sentinel
(lambda (proc _event)
(when (eq 'exit (process-status proc))
(unwind-protect
(if (with-current-buffer source (eq proc flymake-tldr-lint--proc))
(with-current-buffer (process-buffer proc)
(goto-char (point-min))
(cl-loop
while (search-forward-regexp
"^.+?:\\([0-9]+\\): \\(TLDR[0-9]+\\) \\(.*\\)$"
nil t)
for id = (match-string 2)
for msg = (match-string 3)
for (beg . end) = (flymake-diag-region
source
(string-to-number (match-string 1)) 1)
for type = :error
if (null (string-match (regexp-quote id) flymake-tldr-lint-ignored))
collect (flymake-make-diagnostic source
beg
end
type
(format "[%s] %s" id msg))
into diags
finally (funcall report-fn diags)))
(flymake-log :warning "Canceling obsolete check %s"
proc))
(kill-buffer (process-buffer proc))))))))))
;;;###autoload
(defun flymake-tldr-lint-load ()
"Add the tldr-lint backend into Flymake's diagnostic functions list."
(add-hook 'flymake-diagnostic-functions 'flymake-tldr-lint--backend nil t))
(provide 'flymake-tldr-lint)
(add-hook 'markdown-mode-hook 'flymake-tldr-lint-load)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages '(markdown-mode)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
This SO question helped me fix cl-loop
to hide ignored errors.
Add syntax highlighting for generic placeholders. It must be consistent with this extension.
Add the following singular snippets:
f
and file
-> {{path/to/file}}
d
and directory
-> {{path/to/directory}}
fod
and file_or_directory
-> {{path/to/file_or_directory}}
a
and argument
-> {{argument}}
c
and command
-> {{command}}
Add the following plural snippets:
fs
and files
-> {{path/to/file1 path/to/file2 ...}}
ds
and directories
-> {{path/to/directory1 path/to/directory2 ...}}
fods
and files_or_directories
-> {{path/to/file_or_directory1 path/to/file_or_directory2 ...}}
as
and arguments
-> {{argument1 argument2 ...}}
cs
and commands
-> {{command1 command2 ...}}
Introduce new defcustom
-based setting and enable snippets when it's not nil (it can be interpreted as true
).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.