home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.os.linux.suse      Suse is actually not that bad      138,051 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 136,931 of 138,051   
   Aragorn to All   
   Re: Kate - open file at EOF (1/2)   
   14 Mar 20 00:16:20   
   
   From: thorongil@telenet.be   
      
   On 13.03.2020 at 22:29, Batchman scribbled:   
      
   > Yes, I'm still lurking.   
   > How can I have Kate to open an existing text file   
   > and move to the end of the file, please?   
      
   Several years ago, I wrote a wrapper script for KWrite that did exactly   
   that.  Given that Kate accepts the same command line parameters as   
   KWrite, below is the wrapper script, adapted to Kate.   
      
   Copy and save this to a file in a directory that's in your $PATH, and   
   make the file executable.   
      
      
   ==== 8< cut below this line ================================   
      
      
   #!/bin/sh   
   #   
   # This little script will bring up Kate with the supplied file in   
   # it, opened with the cursor positioned behind the last character,   
   # just as if the user had pressed  after opening the file.   
   #   
   # Author                    : Aragorn (thorongil@telenet.be.invalid)   
   # Version                   : 3.1   
   # License                   : GNU GPL version 2 or later   
   #   
   ######################################################################   
   #   
   # This script depends on the presence of a few executables on the   
   # system, i.e.:   
   #   
   #       - a Bourne- or POSIX-compatible shell   
   #       - GNU wc, supplied by the GNU coreutils package   
   #       - Kate, a Qt-based GUI editor with syntax highlighting   
   #   
   # As such, this script will conduct a few sanity checks to see whether   
   # the latter two dependencies have been met; there's no point in   
   # checking for the presence of a Bourne- or POSIX-compatible shell   
   # because that should be supplied by default on all UNIX-like systems.   
   #   
   # The script will exit with more or less standardized exit codes -   
   # inspired by both common Bourne shell exit codes and the C-style exit   
   # codes in /usr/include/sysexits.h - in the event of a missing   
   # dependency or when invoked with an incorrect syntax.   
   #   
   ######################################################################   
   #   
   # Let's first define some variables.  We'll start with the exit codes.   
   #   
   SUCCESS=0   
   E_SYNTAX=2   
   E_IO=74   
   E_DEPENDENCY=127   
   #   
   #   
   # Now we define the dependencies, and we set a dummy variable for the   
   # dependency check.   
   #   
   KWR=$(which kate)   
   WC=$(which wc)   
   MISSING_DEP="nil"   
   #   
   #   
   # Let's provide for a couple of nicely formatted error messages by   
   # way of a few functions.  This is more handy in the event that the   
   # script must reference an error message more than once later on.   
   #   
   #   
   # First, we'll create a function that writes a generic error message.   
   #   
   e_generic()   
   {   
       echo   
       echo '  ' $(basename $0) 'encountered an error.'   
       echo   
   }   
   #   
   #   
   # The next function writes a dependency-specific error message.   
   #   
   e_dependency()   
   {   
       e_generic   
       echo '     This script relies on two dependencies, at least'   
       echo '     one of which was not met.'   
       echo   
       echo '     The following executable appears to be missing from'   
       echo '     your system:' $MISSING_DEP   
       echo   
       echo '     Aborting...'   
       echo   
       exit "${E_DEPENDENCY}"   
   }   
   #   
   #   
   # Given that this script is a wrapper for Kate, we must also include   
   # a nicely formatted error message for in the event that the biological   
   # unit between the keyboard and the chair is trying to execute this   
   # script from a character-mode terminal, rather than from an X11   
   # terminal.   
   #   
   e_display()   
   {   
       e_generic   
       echo '    This script is a wrapper for the Kate editor, which'   
       echo '    requires an X11 environment, while you appear to be in a'   
       echo '    character-mode only terminal.'   
       echo   
       echo '    Aborting...'   
       echo   
       exit "${E_IO}"   
   }   
   #   
   #   
   # We will also include a function to present the user with the correct   
   # syntax for this script, in the event that the user invoked the help   
   # function or forgot to supply a filename as a parameter.   
   #   
   synopsis()   
   {   
       echo   
       echo '   SYNOPSIS'   
       echo   
       echo '   ' $(basename $0) '[OPTIONS] FILENAME'   
       echo   
       echo '     Opens FILENAME in the Kate editor with the cursor'   
       echo '     positioned on the last character in the file.'   
       echo   
       echo   
       echo '   OPTIONS'   
       echo   
       echo '     -h, --help'   
       echo '          Displays this syntax message.'   
       echo   
       echo '     -n, --newline'   
       echo '          Adds a newline character to the end of FILENAME.'   
       echo   
       echo   
       echo '   EXAMPLES'   
       echo   
       echo '   ' $(basename $0) 'textfile.txt'   
       echo '   ' $(basename $0) '-n textfile.txt'   
       echo '   ' $(basename $0) '--newline textfile.txt'   
       echo   
   }   
   #   
   #   
   # And while we're at it, let's also provide for a nicely formatted   
   # syntax error message.   
   #   
   e_syntax()   
   {   
       e_generic   
       echo '   Incorrect usage.'   
       synopsis   
       echo '   Aborting...'   
       echo   
       exit "${E_SYNTAX}"   
   }   
   #   
   #   
   ######################################################################   
   #   
   # Now we've defined all our functions and variables, let's proceed   
   # with the sanity checks, each of which will abort the script upon   
   # failure.   
   #   
   #   
   # First thing to check is whether we're in an X11 environment.   
   #   
   if [ -z "${DISPLAY}" ]   
   then   
       e_display   
   fi   
   #   
   #   
   # Next up, the dependency check.   
   #   
   if [ -z "${KWR}" ]   
   then   
       MISSING_DEP="/usr/bin/kate"   
   fi   
   #   
   if [ -z "${WC}" ]   
   then   
       MISSING_DEP="/usr/bin/wc"   
   fi   
   #   
   if [ "${MISSING_DEP}" != "nil" ]   
   then   
       e_dependency   
   fi   
   #   
   #   
   # And let's now see whether we were invoked correctly.   
   #   
   if [ -z "${1}" ]   
   then   
       e_syntax   
   else   
       case "${1}" in   
   	"-h" | "--help"    )   
   	    synopsis   
   	    exit "$SUCCESS"   
   	    ;;   
   	"-n" | "--newline" )   
   	    if [ -z "${2}" ]   
   	    then   
   		e_syntax   
   	    else   
   		targetfile="${2}"   
   		printf "\n" >> "${targetfile}"   
   	    fi   
   	    ;;   
   	*                  )   
   	    targetfile="${1}"   
   	    ;;   
       esac   
   fi   
   #   
   #   
   ######################################################################   
   #   
   # So far the checks.  If we've come this far, then everything should   
   # work as desired and we can do the actual operation of opening up   
   # Kate and positioning the cursor.   
   #   
   # Since the point of this wrapper script is to start Kate with the   
   # cursor behind the last character of the target file,  we must first   
   # determine how many newline characters there are in the file, and we   
   # will assign this number to a variable, "${newlinecount}".  Then we   
   # will check for the length of the longest paragraph in the file -   
   # i.e. the longest continuous line of text without any newline   
   # characters in it - and assign that value to a second variable,   
   # "${linewidth}".  Using the character count of the longest paragraph   
   # in the file will help us position the cursor on the last character   
   # in the file even if the last paragraph is not the longest one.  This   
   # works whether we've appended a newline character to the end of the   
   # file or not.   
   #   
   newlinecount=$(wc -l < "${targetfile}")   
   linewidth=$(wc -L < "${targetfile}")   
   #   
   #   
   # Now we pass this information on to Kate, and we start Kate in the   
      
   [continued in next message]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca