GithubHelp home page GithubHelp logo

tldr-emacs-extension's People

Contributors

syohex avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

syohex

tldr-emacs-extension's Issues

Add interactive functions to remove or fix potentially broken placeholders

(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")
  )
)

Automatic mnemonic creation

Generate mnemonics for first occurrences of long/short options in descriptions. Letter case must be changed (now, but maybe later).

Implementation notes

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.

Support TLDR error ignorance via new custom option

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 snippets for generic placeholders

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 ...}}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.