bin/foamFind: added '-edit' option to open file in a text editor

The editor must be specified by configuring the EDITOR environment
variable. For example to use the 'gedit' editor, the following entry
could be added to the user's .bashrc file:

export EDITOR=gedit
This commit is contained in:
Chris Greenshields
2024-04-19 20:50:58 +01:00
parent 2e9e37d67c
commit f9ac53aab4
3 changed files with 51 additions and 19 deletions

View File

@ -37,6 +37,7 @@ Usage: ${0##*/} [OPTIONS] <filename>
options:
-a | -applications search for the file from the \$FOAM_APP directory
-d | -dir <dir> specify search directory
-e | -edit open the file in a file editor, see below for info
-f | -files find wmake 'files' file associated with searched file
-h | -help help
-i | -isearch <string> searches files for a <string>, case insensitive
@ -48,8 +49,15 @@ options:
-t | -tutorials search for the file from the \$FOAM_TUTORIALS directory
Finds one or more files in OpenFOAM and optionally processes the contents by:
+ printing the file ('-print' option);
+ printing lines within the file matching a search string ('-search' option).
+ printing the file(s) ('-print' option);
+ printing lines within the file matching a search string ('-search' option);
+ opening a single (only) file in a text editor ('-edit' option).
The '-edit' option required the user to specify their choice of text editor by
setting the EDITOR environment variable. For example, to set the 'gedit' editor,
they could add to their \$HOME/.bashrc file, the line:
export EDITOR=gedit
With source code files, can locate the 'files' and 'options' files associated
with their compilation using 'wmake'.
@ -102,7 +110,7 @@ findWmakeFiles () {
for _w in $_wmakeFiles
do
_files="$(echo "$_files" \
"$(find "$_file" -name "$_w" -type f)" | xargs)"
"$(find "$_file" -name "$_w" -type f)" | xargs)"
done
[ "$_files" ] && break
@ -224,29 +232,42 @@ grepFile () {
dirError () {
cat<<EOF
Search directory specified with more than one option from '-applications', '-dir',
'-modules' and '-tutorials'. Please specify only one of these options.
Search directory specified with more than one option from '-applications',
'-dir', '-modules' and '-tutorials'. Please specify only one of these options.
EOF
}
editConfigError () {
cat<<EOF
The '-edit' option requires the user to specify their chosen text editor using
the EDITOR environment variable. For example, to set the 'gedit' editor, they
could add to their \$HOME/.bashrc file, the line:
export EDITOR=gedit
EOF
}
searchError () {
cat<<EOF
The '-search' and '-isearch' options are both be specified.
The '-search' and '-isearch' options are both specified.
Please specify only one of these options, either '-isearch' for a
case-insensitive search or '-search' for case-sensitive.
EOF
}
printError () {
optionsError () {
cat<<EOF
The '-search/-isearch' and '-print' options are both be specified.
Please specify only one of these options, either '-search/-isearch' to print
lines of files(s) matching an expression, or '-print' for the entire file(s).
Two or more of the '-search/-isearch', '-print' and '-edit' options are
specified. Please specify only one of these options, either '-search/-isearch'
to print lines of files(s) matching an expression, or '-print' or '-edit' to
view the entire file(s).
EOF
}
dir=""
print=""
edit=""
wmakeFiles=""
search=""
insensitive=""
@ -272,6 +293,12 @@ do
error "Specified directory '$dir' is not in OpenFOAM installation"
shift 2
;;
-e | -edit)
{ [ "$insensitive" ] || [ "$print" ]; } && error "$(optionsError)"
[ "$EDITOR" ] || error "$(editConfigError)"
edit="$(which "$EDITOR")"
shift
;;
-f | -files)
wmakeFiles="$wmakeFiles files"
shift
@ -282,7 +309,7 @@ do
-i | -isearch)
[ "$#" -ge 2 ] || error "'$1' option requires an argument"
[ "$insensitive" ] && error "$(searchError)"
[ "$print" ] && error "$(printError)"
{ [ "$print" ] || [ "$edit" ]; } && error "$(optionsError)"
search="$2"
insensitive=on
shift 2
@ -301,14 +328,14 @@ do
shift
;;
-p | -print)
[ "$insensitive" ] && error "$(printError)"
{ [ "$insensitive" ] || [ "$edit" ]; } && error "$(optionsError)"
print="yes"
shift
;;
-s | -search)
[ "$#" -ge 2 ] || error "'$1' option requires an argument"
[ "$insensitive" ] && error "$(searchError)"
[ "$print" ] && error "$(printError)"
{ [ "$print" ] || [ "$edit" ]; } && error "$(optionsError)"
search="$2"
insensitive=off
shift 2
@ -360,9 +387,9 @@ nParams "$files" > /dev/null || error "No file '$filename' found in '$dir'"
files="$(selectFile "$filename" "$files")" || error
# With '-files' and/or '-options', reuse file variable as the file
[ "$wmakeFiles" ] && \
{ [ "$wmakeFiles" ] || [ "$edit" ] ; } && \
[ "$(nParams "$files")" -ne 1 ] && \
error "With '-files' or '-options', a single <filename> must be specified"
error "With '-files', '-options' or '-edit', a single file must be selected"
[ "$wmakeFiles" ] &&
! files="$(findWmakeFiles "$files" "$wmakeFiles")" && error
@ -378,6 +405,9 @@ do
{ [ "$numbers" ] && cat -n "$f" || cat "$f" ; } && \
printf "<<<\n"
# Opening files in a editor
[ "$edit" ] && "$edit" "$f"
# Printing lines matching strings
[ "$search" ] || continue
! grepFile "$search" "$f" "$insensitive" test && \

View File

@ -208,6 +208,7 @@ $(declareLocals)
opts="\\
-applications \\
-dir \\
-edit \\
-files \\
-help \\
-isearch \\
@ -220,8 +221,8 @@ $(declareLocals)
for o in \$used ; do opts="\${opts/\$o/}" ; done
# use only one of '-isearch', '-search', '-print'
local actions="-isearch -search -print"
# use only one of '-edit', '-isearch', '-search', '-print'
local actions="-edit -isearch -search -print"
_optSet "\$used" "\$actions" && opts="\$(_removeOpts "\$opts" "\$actions")"
# use only one of '-applications', '-dir', '-modules', '-tutorials'

View File

@ -4470,6 +4470,7 @@ _foamFind_ ()
opts="\
-applications \
-dir \
-edit \
-files \
-help \
-isearch \
@ -4482,8 +4483,8 @@ _foamFind_ ()
for o in $used ; do opts="${opts/$o/}" ; done
# use only one of '-isearch', '-search', '-print'
local actions="-isearch -search -print"
# use only one of '-edit', '-isearch', '-search', '-print'
local actions="-edit -isearch -search -print"
_optSet "$used" "$actions" && opts="$(_removeOpts "$opts" "$actions")"
# use only one of '-applications', '-dir', '-modules', '-tutorials'