emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [off topic] List all non-latin characters in a buffer
Date: Tue, 16 Aug 2022 15:19:57 +0000	[thread overview]
Message-ID: <87leroqkpu.fsf@posteo.net> (raw)

Sorry for the offtopic, but I thought this homemade function I wrote
some time ago for my work might perhaps be useful to some Orgers. When
executed in a buffer, the `list-non-latin-chars' function opens a window
displaying a list of all the non (basic) Latin characters present in
that document. Each item in the list contains the character, its Unicode
canonical name, and its hexadecimal code. For example:

殿  CJK IDEOGRAPH-6BBF  #6bbf

Also, each item is a button (created with button.el). If the button
is activated, there are currently two options: a: execute occur on that
character in the document; b : execute describe-char on that character.

By default, the characters displayed in the list correspond to any
Unicode block other than basic-latin. Which means that the zero width
space character is included, a very famous character in this mailing
list :-)

And here is the code (lexical binding is required). Of course, feedback
welcome.

Best regards,

Juan Manuel

#+begin_src emacs-lisp
  (setq ext-chars-actions-list '((?a "Occur"
				      (lambda (buf char)
					(interactive)
					(with-current-buffer buf
					  (occur char))))
				  (?b "Describe char"
				      (lambda (buf char)
					(interactive)
					(with-current-buffer buf
					  (save-excursion
					    (goto-char (point-min))
					    (when (re-search-forward char nil t)
					      (describe-char (- (point) 1)))))))))

  (defun ext-chars-choose-action (buf char)
    (let ((opt (read-char-choice (concat "Escoger acción >>\n\n"
					 (mapconcat (lambda (item)
						      (format "%c: %s"
							      (car item) (nth 1 item)))
						    ext-chars-actions-list " --- "))
				 (mapcar #'car ext-chars-actions-list))))
      (apply (nth 2 (assoc opt ext-chars-actions-list))
	     (list buf char))))

  (defvar ext-chars-list nil)

  (defun list-non-latin-chars ()
    (interactive)
    (setq ext-chars-list nil)
    (let ((buf (buffer-name)))
      (save-excursion
	(goto-char (point-min))
	(while
	    (re-search-forward "\\([^\u0000-\u007F]\\)" nil t)
	  (add-to-list 'ext-chars-list (format "%s" (match-string 1))))
	(setq ext-chars-list-final
	      (mapcar (lambda (char)
			(let
			    ((char-name (get-char-code-property (string-to-char char) 'name))
			     ;; convert to hexadecimal
			     (char-code (format "#%x" (string-to-char char))))
			  (setq char (format  "%s\s\s%s\s\s%s" char char-name char-code))))
		      ext-chars-list))
	(let ((temp-buf (format "*non latin chars in %s*" buf)))
	  (when (get-buffer temp-buf)
	    (kill-buffer temp-buf))
	  (get-buffer-create temp-buf)
	  (set-buffer temp-buf)
          ;; necessary for Arabic, Hebrew, etc.
	  (setq bidi-display-reordering nil)
          ;; insert buttons list
	  (mapc (lambda (el)
		  (let ((char (when (string-match "^\\(.\\)\s" el)
				(match-string 1 el))))
		    (insert-button (format "%s" el)
				   'action (lambda (x) 
					     (interactive) 
					     (ext-chars-choose-action buf char)))
		    (insert "\n\n")))
		ext-chars-list-final)
	  (pop-to-buffer temp-buf)
	  (goto-char (point-min))
	  (view-mode)))))
#+end_src

-- 
--
------------------------------------------------------
Juan Manuel Macías 

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com




             reply	other threads:[~2022-08-16 15:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-16 15:19 Juan Manuel Macías [this message]
2022-08-19 14:50 ` [off topic] List all non-latin characters in a buffer Uwe Brauer
2022-09-09 14:15   ` Robert Pluim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87leroqkpu.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).