unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Hongyi Zhao <hongyi.zhao@gmail.com>,
	help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: RE: [External] : Find the longest word in the word list file.
Date: Wed, 11 Aug 2021 01:51:21 +0000	[thread overview]
Message-ID: <SJ0PR10MB548894781E11BB25770FE674F3F89@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <CAGP6POKgSxB8vi9K7Vs7Ge0DpFdMQKC9s5ijba4GeP-EpdGdhw@mail.gmail.com>

> I have an English word list file that stores words in a
> one-word-per-line format. Now I want to find the longest word in the
> word list file. For example, I can use standard UNIX tools to
> accomplish this with the following simple commands:
> 
> $ awk '$0 ~ /^[[:alpha:]]+$/ { print $0, length($0) }'
> american-english-exhaustive | \
> sort -k2n | tail -1
> Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch 58
> 
> But in Emacs, what is the elisp implementation for the above task?

I use this, from `misc-cmds.el' (https://www.emacswiki.org/emacs/download/misc-cmds.el).

I bind it to `C-<end>' in `isearch-mode-map'.  It takes you to the longest line after the cursor (repeatable).  Since you have one word per line it should do what you want.  Of course if you just want a code snippet you can dig that out of it also.

(defun goto-longest-line (beg end &optional msgp)
  "Go to the first of the longest lines in the region or buffer.
If the region is active, it is checked.
If not, the buffer (or its restriction) is checked.

Returns a list of three elements:

 (LINE LINE-LENGTH OTHER-LINES LINES-CHECKED)

LINE is the first of the longest lines measured.
LINE-LENGTH is the length of LINE.
OTHER-LINES is a list of other lines checked that are as long as LINE.
LINES-CHECKED is the number of lines measured.

Interactively, a message displays this information.

If there is only one line in the active region, then the region is
deactivated after this command, and the message mentions only LINE and
LINE-LENGTH.

If this command is repeated, it checks for the longest line after the
cursor.  That is *not* necessarily the longest line other than the
current line.  That longest line could be before or after the current
line.

To search only from the current line forward, not throughout the
buffer, you can use `C-SPC' to set the mark, then use this
\(repeatedly)."
  (interactive
   (if (or (not mark-active)  (not (< (region-beginning) (region-end))))
       (list (point-min) (point-max))
     (if (< (point) (mark))
         (list (point) (mark) 'MSGP)
       (list (mark) (point) 'MSGP))))
  (when (and (not mark-active)  (= beg end)) (error "The buffer is empty"))
  (when (and mark-active  (> (point) (mark))) (exchange-point-and-mark))
  (when (< end beg) (setq end  (prog1 beg (setq beg  end))))
  (when (eq 'goto-longest-line last-command)
    (forward-line 1) (setq beg  (point)))
  (goto-char beg)
  (when (eobp) (error "End of buffer"))
  (cond ((<= end (save-excursion (goto-char beg) (forward-line 1) (point)))
         (let ((inhibit-field-text-motion  t))  (beginning-of-line))
         (when (and (> emacs-major-version 21)  (require 'hl-line nil t))
           (let ((hl-line-mode  t))  (hl-line-highlight))
           (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
         (let ((lineno  (line-number-at-pos))
               (chars   (let ((inhibit-field-text-motion  t))
                          (save-excursion (end-of-line) (current-column)))))
           (message "Only line %d: %d chars" lineno chars)
           (let ((visible-bell  t))  (ding))
           (setq mark-active  nil)
           (list lineno chars nil 1)))
        (t
         (let* ((start-line                 (line-number-at-pos))
                (max-width                  0)
                (line                       start-line)
                (inhibit-field-text-motion  t)
                long-lines col)
           (when (eobp) (error "End of buffer"))
           (while (and (not (eobp))  (< (point) end))
             (end-of-line)
             (setq col  (current-column))
             (when (>= col max-width)
               (setq long-lines  (if (= col max-width)
                                     (cons line long-lines)
                                   (list line))
                     max-width   col))
             (forward-line 1)
             (setq line  (1+ line)))
           (setq long-lines  (nreverse long-lines))
           (let ((lines  long-lines))
             (while (and lines  (> start-line (car lines))) (pop lines))
             (goto-char (point-min))
             (when (car lines) (forward-line (1- (car lines)))))
           (when (and (> emacs-major-version 21)  (require 'hl-line nil t))
             (let ((hl-line-mode  t))  (hl-line-highlight))
             (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
           (when msgp
             (let ((others  (cdr long-lines)))
               (message "Line %d: %d chars%s (%d lines measured)"
                        (car long-lines) max-width
                        (concat
                         (and others
                              (format ", Others: {%s}" (mapconcat
                                                        (lambda (line) (format "%d" line))
                                                        (cdr long-lines) ", "))))
                        (- line start-line))))
           (list (car long-lines) max-width (cdr long-lines) (- line start-line))))))

  reply	other threads:[~2021-08-11  1:51 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10 23:14 Find the longest word in the word list file Hongyi Zhao
2021-08-11  1:51 ` Drew Adams [this message]
2021-08-11  3:00   ` [External] : " Hongyi Zhao
2021-08-11  6:46     ` tomas
2021-08-11  8:58       ` Hongyi Zhao
2021-08-11 10:03         ` tomas
2021-08-11  9:52       ` Arthur Miller
2021-08-11 10:06         ` tomas
2021-08-11 13:32           ` Hongyi Zhao
2021-08-11 13:58 ` Stephen Berman
2021-08-11 14:17   ` Hongyi Zhao
2021-08-11 14:27     ` Stephen Berman
2021-08-11 16:09 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  2:10   ` Hongyi Zhao
2021-08-12  2:29     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  3:13       ` Hongyi Zhao
2021-08-12  3:17         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  3:34           ` Hongyi Zhao
2021-08-12  3:41             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  3:52               ` Hongyi Zhao
2021-08-12  4:05                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  4:25                   ` Hongyi Zhao
2021-08-12  4:38                     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  4:53                       ` Hongyi Zhao
2021-08-12  5:27                         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  6:25                           ` Hongyi Zhao
2021-08-12  6:46                             ` Hongyi Zhao
2021-08-12  6:51                             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12  6:59                               ` Hongyi Zhao
2021-08-12  2:53     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 14:47       ` Kevin Vigouroux via Users list for the GNU Emacs text editor
2021-08-12 15:45         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 15:53           ` Kevin Vigouroux via Users list for the GNU Emacs text editor
2021-08-12 16:10             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-08-12 16:24               ` Kevin Vigouroux via Users list for the GNU Emacs text editor

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.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=SJ0PR10MB548894781E11BB25770FE674F3F89@SJ0PR10MB5488.namprd10.prod.outlook.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=hongyi.zhao@gmail.com \
    /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.
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).