(ess-find-exec-completions): Doc fix.
[ess.git] / lisp / ess-utils.el
blobdb32cb72acf24705bf814370c2aad2e1247ad828
1 ;;; ess-utils.el --- General Emacs utility functions used by ESS
3 ;; Copyright (C) 1998--2005 A.J. Rossini, Rich M. Heiberger, Martin
4 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
6 ;; Original Author: Martin Maechler <maechler@stat.math.ethz.ch>
7 ;; Created: 9 Sept 1998
8 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
10 ;; This file is part of ESS (Emacs Speaks Statistics).
12 ;; This file is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; This file is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;;-- Emacs Utilities --- Generally useful --- used by (but not requiring) ESS
28 (defun ess-inside-string-or-comment-p (pos)
29 "Return non-nil if POSition [defaults to (point)] is inside string or comment
30 (according to syntax). NOT OKAY for multi-line comments!!"
31 ;;FIXME (defun S-calculate-indent ..) in ./essl-s.el can do that ...
32 (interactive "d");point by default
33 (let ((pps (save-excursion
34 (parse-partial-sexp
35 (save-excursion (beginning-of-line) (point))
36 pos))))
37 (or (nth 3 pps) (nth 4 pps)))); 3: string, 4: comment
39 (defsubst ess-inside-string-p ()
40 "Return non-nil if point is inside string (according to syntax)."
41 (interactive)
42 (save-excursion
43 (nth 3 (parse-partial-sexp (line-beginning-position) (point)))))
45 ;; simple alternative to ess-read-object-name-default of ./ess-inf.el :
46 (defun ess-extract-word-name ()
47 "Get the word you're on."
48 (save-excursion
49 (re-search-forward "\\<\\w+\\>" nil t)
50 (buffer-substring (match-beginning 0) (match-end 0))))
52 (defun ess-rep-regexp (regexp to-string &optional fixedcase literal verbose)
53 "Instead of (replace-regexp..) -- do NOT replace in strings or comments.
54 If FIXEDCASE is non-nil, do *not* alter case of replacement text.
55 If LITERAL is non-nil, do *not* treat `\\' as special.
56 If VERBOSE is non-nil, (message ..) about replacements."
57 (let ((case-fold-search (and case-fold-search
58 (not fixedcase))); t <==> ignore case in search
59 (pl) (p))
60 (while (setq p (re-search-forward regexp nil t))
61 (cond ((not (ess-inside-string-or-comment-p (1- p)))
62 (if verbose
63 (let ((beg (match-beginning 0)))
64 (message "(beg,p)= (%d,%d) = %s"
65 beg p (buffer-substring beg p) )))
66 (replace-match to-string fixedcase literal)
67 ;;or (if verbose (setq pl (append pl (list p))))
68 )))
69 ;;or (if (and verbose pl)
70 ;;or (message "s/%s/%s/ at %s" regexp to-string pl))
71 ) )
73 (defun ess-replace-regexp-dump-to-src
74 (regexp to-string &optional dont-query verbose ensure-mode)
75 "Depending on dont-query, call `ess-rep-regexp' or `query-replace-regexp'
76 from the beginning of the buffer."
77 (save-excursion
78 (if (and ensure-mode
79 (not (equal major-mode 'ess-mode)))
80 (ess-mode))
81 (goto-char (point-min))
82 (if dont-query
83 (ess-rep-regexp regexp to-string nil nil verbose)
84 (query-replace-regexp regexp to-string nil))))
87 (defun ess-revert-wisely ()
88 "Revert from disk if file and buffer last modification times are different."
89 (interactive)
91 ; vc-revert-buffer acting strangely in Emacs 21.1; no longer used
93 ; Long-winded Explanation
95 ; Maybe I am being a little hard on 21.1, but it behaves differently.
96 ; Basically, revert means roll-back. But, for SAS purposes, you never
97 ; really want to roll-back. You want to refresh the buffer with the
98 ; disk file which is being modified in the background. So, we only
99 ; roll-back when the date/time stamp of the file is newer than the buffer
100 ; (technically, this is roll-ahead).
102 ; However, I was supporting a version control system (RCS) when I originally
103 ; wrote this function. I added functionality so that the roll-back was
104 ; performed by vc. This worked fine until 21.1. In 21.1 when you call this
105 ; function with vc/CVS, it actually rolls-back to the prior version of the
106 ; file rather than refreshing. Apparently, it ignores the file on disk.
107 ; This change actually makes some sense, but it isn't what we want.
109 ; Long-winded Explanation Ends (no longer interesting, will remove soon)
111 ; whether or not a revert is needed, force load local variables
112 ; for example, suppose that you change the local variables and then
113 ; save the file, a revert is unneeded, but a force load is
114 (hack-local-variables)
116 (if (not (verify-visited-file-modtime (current-buffer))) (progn
117 (revert-buffer t t)
119 nil))
121 ;; (cond ((and (fboundp 'vc-backend-deduce)
122 ;; (vc-backend-deduce (buffer-file-name))) (vc-revert-buffer))
123 ;; ((and (fboundp 'vc-backend)
124 ;; (vc-backend (buffer-file-name))) (vc-revert-buffer))
125 ;; (t (revert-buffer t t)))))
127 (defun ess-space-around (word &optional from verbose)
128 "Replace-regexp .. ensuring space around all occurences of WORD,
129 starting from FROM {defaults to (point)}."
130 (interactive "d\nP"); Defaults: point and prefix (C-u)
131 (save-excursion
132 (goto-char from)
133 (ess-rep-regexp (concat "\\([^ \t\n]\\)\\(\\<" word "\\>\\)")
134 "\\1 \\2" nil nil verbose)
135 (goto-char from)
136 (ess-rep-regexp (concat "\\(\\<" word "\\>\\)\\([^ \t\n]\\)")
137 "\\1 \\2" nil nil verbose)
141 (defun ess-time-string (&optional clock)
142 "Returns a string for use as a timestamp. + hr:min if CLOCK is non-nil,
143 like \"13 Mar 1992\". Redefine to taste."
144 (format-time-string (concat "%e %b %Y" (if clock ", %H:%M"))))
147 ;;- From: friedman@gnu.ai.mit.edu (Noah Friedman)
148 ;;- Date: 12 Feb 1995 21:30:56 -0500
149 ;;- Newsgroups: gnu.emacs.sources
150 ;;- Subject: nuke-trailing-whitespace
152 ;;- This is too trivial to make into a big todo with comments and copyright
153 ;;- notices whose length exceed the size of the actual code, so consider it
154 ;;- public domain. Its purpose is along similar lines to that of
155 ;;- `require-final-newline', which is built in. I hope the names make it
156 ;;- obvious.
158 ;; (add-hook 'write-file-hooks 'nuke-trailing-whitespace)
159 ;;or at least
160 ;; (add-hook 'ess-mode-hook
161 ;; '(lambda ()
162 ;; (add-hook 'local-write-file-hooks 'nuke-trailing-whitespace)))
164 (defvar ess-nuke-trailing-whitespace-p nil;disabled by default 'ask
165 "*[Dis]activates (ess-nuke-trailing-whitespace).
166 Disabled if `nil'; if `t', it works unconditionally, otherwise,
167 the user is queried.
168 Note that setting the default to `t' may not be a good idea when you edit
169 binary files!")
171 ;;; MM: Newer Emacsen now have delete-trailing-whitespace
172 ;;; -- but no customization like nuke-trailing-whitespace-p ..
173 (defun ess-nuke-trailing-whitespace ()
174 "Nuke all trailing whitespace in the buffer.
175 Whitespace in this case is just spaces or tabs.
176 This is a useful function to put on write-file-hooks.
178 If the variable `ess-nuke-trailing-whitespace-p' is `nil', this function is
179 disabled. If `t', unreservedly strip trailing whitespace.
180 If not `nil' and not `t', query for each instance."
181 (interactive)
182 (let ((bname (buffer-name)))
183 (cond ((or
184 (string= major-mode "rmail-mode")
185 (string= bname "RMAIL")
186 nil)); do nothing..
189 (and (not buffer-read-only)
190 ess-nuke-trailing-whitespace-p
191 (save-match-data
192 (save-excursion
193 (save-restriction
194 (widen)
195 (goto-char (point-min))
196 (cond ((eq ess-nuke-trailing-whitespace-p t)
197 (while (re-search-forward "[ \t]+$" (point-max) t)
198 (delete-region (match-beginning 0)
199 (match-end 0))))
201 (query-replace-regexp "[ \t]+$" "")))))))))
202 ;; always return nil, in case this is on write-file-hooks.
203 nil))
205 (defun ess-kermit-get (&optional ess-file-arg ess-dir-arg)
206 "Get a file with Kermit. WARNING: Experimental! From your *shell*
207 buffer, start kermit and then log in to the remote machine. Open
208 a file that starts with `ess-kermit-prefix'. From that buffer,
209 execute this command. It will retrieve a file from the remote
210 directory that you specify with the same name, but without the
211 `ess-kermit-prefix'."
213 (interactive)
215 ;; (save-match-data
216 (let ((ess-temp-file (if ess-file-arg ess-file-arg (buffer-name)))
217 (ess-temp-file-remote-directory ess-dir-arg))
219 (if (string-equal ess-kermit-prefix (substring ess-temp-file 0 1))
220 (progn
221 ;; I think there is a bug in the buffer-local variable handling in GNU Emacs 21.3
222 ;; Setting ess-kermit-remote-directory every time is somehow resetting it to the
223 ;; default on the second pass. So, here's a temporary work-around. It will fail
224 ;; if you change the default, so maybe this variable should not be customizable.
225 ;; In any case, there is also trouble with local variables in XEmacs 21.4.9 and
226 ;; 21.4.10. XEmacs 21.4.8 is fine.
227 (if ess-temp-file-remote-directory
228 (setq ess-kermit-remote-directory ess-temp-file-remote-directory)
230 (if (string-equal "." ess-kermit-remote-directory)
231 (setq ess-kermit-remote-directory (read-string "Remote directory to transfer file from: "
232 ess-kermit-remote-directory))))
234 (setq ess-temp-file-remote-directory ess-kermit-remote-directory)
235 ;; (setq ess-temp-file (substring ess-temp-file (match-end 0)))
236 (ess-sas-goto-shell)
237 (insert "cd " ess-temp-file-remote-directory "; " ess-kermit-command " -s "
238 (substring ess-temp-file 1) " -a " ess-temp-file)
239 (comint-send-input)
240 ;; (insert (read-string "Press Return to connect to Kermit: " nil nil "\C-\\c"))
241 ;; (comint-send-input)
242 ;; (insert (read-string "Press Return when Kermit is ready to recieve: " nil nil
243 ;; (concat "receive ]" ess-sas-temp-file)))
244 ;; (comint-send-input)
245 ;; (insert (read-string "Press Return when transfer is complete: " nil nil "c"))
246 ;; (comint-send-input)
247 (insert (read-string "Press Return when shell is ready: "))
248 (comint-send-input)
249 (switch-to-buffer (find-buffer-visiting ess-temp-file))
250 (ess-revert-wisely)
251 ))))
253 (defun ess-kermit-send ()
254 "Send a file with Kermit. WARNING: Experimental! From
255 a file that starts with `ess-kermit-prefix',
256 execute this command. It will transfer this file to the remote
257 directory with the same name, but without the `ess-kermit-prefix'."
259 (interactive)
261 ;; (save-match-data
262 (let ((ess-temp-file (expand-file-name (buffer-name)))
263 (ess-temp-file-remote-directory nil))
265 (if (string-equal ess-kermit-prefix (substring (file-name-nondirectory ess-temp-file) 0 1))
266 (progn
267 ;; I think there is a bug in the buffer-local variable handling in GNU Emacs 21.3
268 ;; Setting ess-kermit-remote-directory every time is somehow resetting it to the
269 ;; default on the second pass. Here's a temporary work-around. It will fail
270 ;; if you change the default, so maybe this variable should not be customizable.
271 ;; In any case, there is also trouble with local variables in XEmacs 21.4.9 and
272 ;; 21.4.10. XEmacs 21.4.8 is fine.
273 (if (string-equal "." ess-kermit-remote-directory)
274 (setq ess-kermit-remote-directory (read-string "Remote directory to transfer file to: "
275 ess-kermit-remote-directory)))
277 (setq ess-temp-file-remote-directory ess-kermit-remote-directory)
279 ;; (setq ess-temp-file (substring ess-temp-file (match-end 0)))
280 (ess-sas-goto-shell)
281 (insert "cd " ess-temp-file-remote-directory "; " ess-kermit-command " -a "
282 (substring (file-name-nondirectory ess-temp-file) 1) " -g " ess-temp-file)
283 (comint-send-input)
284 ;; (insert (read-string "Press Return to connect to Kermit: " nil nil "\C-\\c"))
285 ;; (comint-send-input)
286 ;; (insert (read-string "Press Return when Kermit is ready to recieve: " nil nil
287 ;; (concat "receive ]" ess-sas-temp-file)))
288 ;; (comint-send-input)
289 ;; (insert (read-string "Press Return when transfer is complete: " nil nil "c"))
290 ;; (comint-send-input)
291 (insert (read-string "Press Return when shell is ready: "))
292 (comint-send-input)
293 (switch-to-buffer (find-buffer-visiting ess-temp-file))
294 (ess-revert-wisely)
295 ))))
297 (defun ess-search-except (regexp &optional except backward)
298 "Search for a regexp, store as match 1, optionally ignore
299 strings that match exceptions."
300 (interactive)
302 (let ((continue t) (exit nil))
304 (while continue
305 (if (or (and backward (search-backward-regexp regexp nil t))
306 (and (not backward) (search-forward-regexp regexp nil t)))
307 (progn
308 (setq exit (match-string 1))
309 (setq continue (and except (string-match except exit)))
310 (if continue (setq exit nil)))
311 ;;else
312 (setq continue nil))
315 exit))
317 (defun ess-save-and-set-local-variables ()
318 "If buffer was modified, save file and set Local Variables if defined.
319 Return t if buffer was modified, nil otherwise."
320 (interactive)
322 (let ((ess-temp-point (point))
323 (ess-temp-return-value (buffer-modified-p)))
324 ;; if buffer has changed, save buffer now (before potential revert)
325 (if ess-temp-return-value (save-buffer))
327 ;; If Local Variables are defined, update them now
328 ;; since they may have changed since the last revert
329 ;; (save-excursion
330 (beginning-of-line -1)
331 (save-match-data
332 (if (search-forward "End:" nil t) (revert-buffer t t)))
333 ;; save-excursion doesn't save point in the presence of a revert
334 ;; so you need to do it yourself
335 (goto-char ess-temp-point)
337 ess-temp-return-value))
339 (defun ess-get-file-or-buffer (file-or-buffer)
340 "Return file-or-buffer if it is a buffer; otherwise return the buffer
341 associated with the file which must be qualified by it's path; if the
342 buffer does not exist, return nil."
343 (interactive)
345 (if file-or-buffer
346 (if (bufferp file-or-buffer) file-or-buffer
347 (find-buffer-visiting file-or-buffer))))
349 (defun ess-set-local-variables (alist &optional file-or-buffer)
350 "Set local variables from ALIST in current buffer; if file-or-buffer
351 is specified, perform action in that buffer."
352 (interactive)
354 (if file-or-buffer (set-buffer (ess-get-file-or-buffer file-or-buffer)))
356 (mapcar (lambda (pair)
357 (make-local-variable (car pair))
358 (set (car pair) (eval (cdr pair))))
359 alist))
361 (defun ess-clone-local-variables (from-file-or-buffer &optional to-file-or-buffer)
362 "Clone local variables from one buffer to another buffer, current buffer if nil."
363 (interactive)
365 (ess-set-local-variables
366 (ess-sas-create-local-variables-alist from-file-or-buffer)
367 to-file-or-buffer))
369 (defun ess-directory-sep (ess-dir-arg)
370 "Deprecated. Use file-name-as-directory instead.
371 Given a directory, pad with directory-separator character, if necessary."
372 (let ((ess-tmp-dir-last-char (substring ess-dir-arg -1)))
373 (if (or (equal ess-tmp-dir-last-char "/")
374 (and ess-microsoft-p (equal ess-tmp-dir-last-char "\\")))
375 ess-dir-arg
376 (concat ess-dir-arg (if ess-microsoft-p "\\" "/")))))
378 (defun ess-return-list (ess-arg)
379 "Given an item, if it is a list return it, otherwise return item in a list."
380 (if (listp ess-arg) ess-arg (list ess-arg)))
382 (defun ess-find-exec (ess-root-arg ess-root-dir)
383 "Given a root directory and the root of an executable file name, find it's full
384 name and path, if it exists, anywhere in the sub-tree."
385 (let* ((ess-tmp-dirs (directory-files ess-root-dir t "^[^.]"))
386 (ess-tmp-return (ess-find-exec-completions ess-root-arg ess-root-dir))
387 (ess-tmp-dirs-n (length ess-tmp-dirs))
388 (ess-tmp-dir nil)
389 (i 0))
391 (while (< i ess-tmp-dirs-n)
392 (setq ess-tmp-dir (nth i ess-tmp-dirs))
393 (setq i (+ i 1))
394 (if (file-directory-p ess-tmp-dir)
395 (setq ess-tmp-return (nconc ess-tmp-return
396 (ess-find-exec ess-root-arg ess-tmp-dir)))))
397 ess-tmp-return))
399 (defun ess-find-exec-completions (ess-root-arg &optional ess-exec-dir)
400 "Given the root of an executable file name, find all possible completions.
401 Search for the executables in ESS-EXEC-DIR which defaults to
402 `exec-path' if no value is given."
403 (let* ((ess-exec-path
404 (if ess-exec-dir (ess-return-list ess-exec-dir) exec-path))
405 (ess-tmp-exec nil)
406 (ess-tmp-path-count (length ess-exec-path))
407 (ess-tmp-dir nil)
408 (ess-tmp-files nil)
409 (ess-tmp-file nil)
410 (i 0) (j 0) (k 0))
412 (while (< i ess-tmp-path-count)
413 (setq ess-tmp-dir (nth i ess-exec-path))
414 (if (file-exists-p ess-tmp-dir) (progn
415 (setq ess-tmp-files (file-name-all-completions ess-root-arg ess-tmp-dir))
416 (setq j 0)
417 (setq k (length ess-tmp-files))
418 (while (< j k)
419 (setq ess-tmp-file (concat (file-name-as-directory ess-tmp-dir)
420 (nth j ess-tmp-files)))
421 (if (and (file-executable-p ess-tmp-file)
422 (not (file-directory-p ess-tmp-file)))
423 (setq ess-tmp-exec (nconc ess-tmp-exec (list ess-tmp-file))))
424 (setq j (+ j 1)))))
425 (setq i (+ i 1)))
426 ess-tmp-exec))
428 ;; (defun ess-uniq-list (items)
429 ;; "Remove all duplicate strings from the list ITEMS."
430 ;; ;; build up a new-list, only adding an item from ITEMS if it is not
431 ;; ;; already present in new-list.
432 ;; (let (new-list)
433 ;; (while items
434 ;; (if (not (member (car items) new-list))
435 ;; (setq new-list (cons (car items) new-list)))
436 ;; (setq items (cdr items)))
437 ;; new-list
438 ;; ))
440 ;; Copyright (C) 1994 Simon Marshall.
441 ;; Author: Simon Marshall <Simon.Marshall@mail.esrin.esa.it>
442 ;; LCD Archive Entry:
443 ;; unique|Simon Marshall|Simon.Marshall@mail.esrin.esa.it|
444 ;; Functions and commands to uniquify lists or buffer text (cf. sort).
445 ;; 23-Apr-1994|1.00|~/packages/unique.el.Z|
447 ;; MM: renamed from 'unique' to
448 (defun ess-unique (list predicate)
449 "Uniquify LIST, stably, deleting elements using PREDICATE.
450 Return the list with subsequent duplicate items removed by side effects.
451 PREDICATE is called with an element of LIST and a list of elements from LIST,
452 and should return the list of elements with occurrences of the element removed.
453 This function will work even if LIST is unsorted. See also `uniq'."
454 (let ((list list))
455 (while list
456 (setq list (setcdr list (funcall predicate (car list) (cdr list))))))
457 list)
458 (defun ess-uniq-list (items)
459 "Delete all duplicate entries in ITEMS list, calling `ess-unique'."
460 (ess-unique items 'delete))
463 (defun ess-flatten-list (&rest list)
464 "Take the arguments and flatten them into one long list."
465 ;; Taken from lpr.el
466 ;; `lpr-flatten-list' is defined here (copied from "message.el" and
467 ;; enhanced to handle dotted pairs as well) until we can get some
468 ;; sensible autoloads, or `flatten-list' gets put somewhere decent.
470 ;; (ess-flatten-list '((a . b) c (d . e) (f g h) i . j))
471 ;; => (a b c d e f g h i j)
472 (ess-flatten-list-1 list))
474 (defun ess-flatten-list-1 (list)
475 (cond
476 ((null list) (list))
477 ((consp list)
478 (append (ess-flatten-list-1 (car list))
479 (ess-flatten-list-1 (cdr list))))
480 (t (list list))))
482 (defun ess-delete-blank-lines ()
483 "Convert 2 or more lines of white space into one."
484 (interactive)
485 (save-excursion
486 (goto-char (point-min))
487 (save-match-data
488 (while (search-forward-regexp "^[ \t]*\n[ \t]*\n" nil t)
489 ;;(goto-char (match-beginning 0))
490 (delete-blank-lines)))))
492 (defun ess-do-auto-fill ()
493 "This is the same as \\[do-auto-fill] in GNU emacs 21.3, with one major
494 difference: if we could not find a suitable place to break the line,
495 we simply do not break it (instead of breaking after the first word)."
496 (let (fc justify bol give-up
497 (fill-prefix fill-prefix))
498 (if (or (not (setq justify (current-justification)))
499 (null (setq fc (current-fill-column)))
500 (and (eq justify 'left)
501 (<= (current-column) fc))
502 (save-excursion (beginning-of-line)
503 (setq bol (point))
504 (and auto-fill-inhibit-regexp
505 (looking-at auto-fill-inhibit-regexp))))
506 nil ;; Auto-filling not required
507 (if (memq justify '(full center right))
508 (save-excursion (unjustify-current-line)))
510 ;; Choose a fill-prefix automatically.
511 (if (and adaptive-fill-mode
512 (or (null fill-prefix) (string= fill-prefix "")))
513 (let ((prefix
514 (fill-context-prefix
515 (save-excursion (backward-paragraph 1) (point))
516 (save-excursion (forward-paragraph 1) (point)))))
517 (and prefix (not (equal prefix ""))
518 (setq fill-prefix prefix))))
520 (while (and (not give-up) (> (current-column) fc))
521 ;; Determine where to split the line.
522 (let* (after-prefix
523 (fill-point
524 (let ((opoint (point))
525 bounce
526 (first t))
527 (save-excursion
528 (beginning-of-line)
529 (setq after-prefix (point))
530 (and fill-prefix
531 (looking-at (regexp-quote fill-prefix))
532 (setq after-prefix (match-end 0)))
533 (move-to-column (1+ fc))
534 ;; Move back to the point where we can break the line.
535 ;; We break the line between word or
536 ;; after/before the character which has character
537 ;; category `|'. We search space, \c| followed by
538 ;; a character, or \c| following a character. If
539 ;; not found, place the point at beginning of line.
540 (while (or first
541 ;; If this is after period and a single space,
542 ;; move back once more--we don't want to break
543 ;; the line there and make it look like a
544 ;; sentence end.
545 (and (not (bobp))
546 (not bounce)
547 sentence-end-double-space
548 (save-excursion (forward-char -1)
549 (and (looking-at "\\. ")
550 (not (looking-at "\\. ")))))
551 (and (not (bobp))
552 (not bounce)
553 fill-nobreak-predicate
554 (funcall fill-nobreak-predicate)))
555 (setq first nil)
556 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
557 ;; If we find nowhere on the line to break it,
558 ;; do not break it. Set bounce to t
559 ;; so we will not keep going in this while loop.
560 (if (<= (point) after-prefix)
561 (setq bounce t)
562 (if (looking-at "[ \t]")
563 ;; Break the line at word boundary.
564 (skip-chars-backward " \t")
565 ;; Break the line after/before \c|.
566 (forward-char 1))))
567 (if enable-multibyte-characters
568 ;; If we are going to break the line after or
569 ;; before a non-ascii character, we may have
570 ;; to run a special function for the charset
571 ;; of the character to find the correct break
572 ;; point.
573 (if (not (and (eq (charset-after (1- (point))) 'ascii)
574 (eq (charset-after (point)) 'ascii)))
575 (fill-find-break-point after-prefix)))
577 ;; Let fill-point be set to the place where we end up.
578 ;; But move back before any whitespace here.
579 (skip-chars-backward " \t")
580 (point)))))
582 ;; See whether the place we found is any good.
583 (if (save-excursion
584 (goto-char fill-point)
585 (and (not (bolp))
586 ;; There is no use breaking at end of line.
587 (not (save-excursion (skip-chars-forward " ") (eolp)))
588 ;; It is futile to split at the end of the prefix
589 ;; since we would just insert the prefix again.
590 (not (and after-prefix (<= (point) after-prefix)))
591 ;; Don't split right after a comment starter
592 ;; since we would just make another comment starter.
593 (not (and comment-start-skip
594 (let ((limit (point)))
595 (beginning-of-line)
596 (and (re-search-forward comment-start-skip
597 limit t)
598 (eq (point) limit)))))))
599 ;; Ok, we have a useful place to break the line. Do it.
600 (let ((prev-column (current-column)))
601 ;; If point is at the fill-point, do not `save-excursion'.
602 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
603 ;; point will end up before it rather than after it.
604 (if (save-excursion
605 (skip-chars-backward " \t")
606 (= (point) fill-point))
607 (funcall comment-line-break-function t)
608 (save-excursion
609 (goto-char fill-point)
610 (funcall comment-line-break-function t)))
611 ;; Now do justification, if required
612 (if (not (eq justify 'left))
613 (save-excursion
614 (end-of-line 0)
615 (justify-current-line justify nil t)))
616 ;; If making the new line didn't reduce the hpos of
617 ;; the end of the line, then give up now;
618 ;; trying again will not help.
619 (if (>= (current-column) prev-column)
620 (setq give-up t)))
621 ;; No good place to break => stop trying.
622 (setq give-up t))))
623 ;; Justify last line.
624 (justify-current-line justify t t)
625 t)))
627 (provide 'ess-utils)