From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.devel Subject: find longest lines during isearch Date: Wed, 23 Jan 2008 16:00:48 -0800 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1201132879 12057 80.91.229.12 (24 Jan 2008 00:01:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 24 Jan 2008 00:01:19 +0000 (UTC) To: "Emacs-Devel" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Jan 24 01:01:37 2008 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1JHpXD-0005pt-MR for ged-emacs-devel@m.gmane.org; Thu, 24 Jan 2008 01:01:36 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JHpWn-0001mO-QY for ged-emacs-devel@m.gmane.org; Wed, 23 Jan 2008 19:01:09 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JHpWj-0001kz-Q3 for emacs-devel@gnu.org; Wed, 23 Jan 2008 19:01:05 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JHpWi-0001jk-Qr for emacs-devel@gnu.org; Wed, 23 Jan 2008 19:01:05 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JHpWi-0001jV-L6 for emacs-devel@gnu.org; Wed, 23 Jan 2008 19:01:04 -0500 Original-Received: from rgminet01.oracle.com ([148.87.113.118]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1JHpWi-0003Uq-68 for emacs-devel@gnu.org; Wed, 23 Jan 2008 19:01:04 -0500 Original-Received: from agmgw1.us.oracle.com (agmgw1.us.oracle.com [152.68.180.212]) by rgminet01.oracle.com (Switch-3.2.4/Switch-3.1.6) with ESMTP id m0O010HX013268 for ; Wed, 23 Jan 2008 17:01:01 -0700 Original-Received: from acsmt351.oracle.com (acsmt351.oracle.com [141.146.40.151]) by agmgw1.us.oracle.com (Switch-3.2.0/Switch-3.2.0) with ESMTP id m0M7w8pa012325 for ; Wed, 23 Jan 2008 17:00:54 -0700 Original-Received: from dhcp-amer-csvpn-gw1-141-144-67-90.vpn.oracle.com by acsmt350.oracle.com with ESMTP id 3533389311201132838; Wed, 23 Jan 2008 16:00:38 -0800 X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-Brightmail-Tracker: AAAAAQAAAAI= X-Brightmail-Tracker: AAAAAQAAAAI= X-Whitelist: TRUE X-Whitelist: TRUE X-detected-kernel: by monty-python.gnu.org: Linux 2.4-2.6 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:87409 Archived-At: I use this several times a day. I bind it to `C-end' in `isearch-mode-map'. (`C-end' is generally not available from a terminal, however.) I also bind it to `C-x L' globally, but I nearly always use it with isearch. In particular, isearch's C-g is handy here. I know that Richard doesn't like to add more isearch bindings, but that's where I think this belongs. Any interest in adding it? Please give it a try. It works with a region or without, and whether the buffer is narrowed or not. the region is searched if it is active, but you can keep searching beyond it (you are notified when you exit the region). The target line is highlighted (until the next command). You can use this to see what it's like in isearch: (add-hook 'isearch-mode-hook (lambda () (when window-system (define-key isearch-mode-map [(control end)] 'goto-longest-line)))) (defun goto-longest-line (beg end) "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) (null (mark))) (list (point-min) (point-max)) (if (< (point) (mark)) (list (point) (mark)) (list (mark) (point))))) (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 this-command 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))) (beginning-of-line) (when (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 (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) 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) (if (= col max-width) (setq long-lines (cons line long-lines)) (setq long-lines (list line))) (setq 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 (require 'hl-line nil t) (let ((hl-line-mode t)) (hl-line-highlight)) (add-hook 'pre-command-hook #'hl-line-unhighlight nil t)) (when (interactive-p) (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))))))